OSDN Git Service

PR c++/26151
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
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 \f
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 C++ token.  */
49
50 typedef struct cp_token GTY (())
51 {
52   /* The kind of token.  */
53   ENUM_BITFIELD (cpp_ttype) type : 8;
54   /* If this token is a keyword, this value indicates which keyword.
55      Otherwise, this value is RID_MAX.  */
56   ENUM_BITFIELD (rid) keyword : 8;
57   /* Token flags.  */
58   unsigned char flags;
59   /* Identifier for the pragma.  */
60   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
61   /* True if this token is from a system header.  */
62   BOOL_BITFIELD in_system_header : 1;
63   /* True if this token is from a context where it is implicitly extern "C" */
64   BOOL_BITFIELD implicit_extern_c : 1;
65   /* True for a CPP_NAME token that is not a keyword (i.e., for which
66      KEYWORD is RID_MAX) iff this name was looked up and found to be
67      ambiguous.  An error has already been reported.  */
68   BOOL_BITFIELD ambiguous_p : 1;
69   /* The value associated with this token, if any.  */
70   tree value;
71   /* The location at which this token was found.  */
72   location_t location;
73 } cp_token;
74
75 /* We use a stack of token pointer for saving token sets.  */
76 typedef struct cp_token *cp_token_position;
77 DEF_VEC_P (cp_token_position);
78 DEF_VEC_ALLOC_P (cp_token_position,heap);
79
80 static const cp_token eof_token =
81 {
82   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, NULL_TREE,
83 #if USE_MAPPED_LOCATION
84   0
85 #else
86   {0, 0}
87 #endif
88 };
89
90 /* The cp_lexer structure represents the C++ lexer.  It is responsible
91    for managing the token stream from the preprocessor and supplying
92    it to the parser.  Tokens are never added to the cp_lexer after
93    it is created.  */
94
95 typedef struct cp_lexer GTY (())
96 {
97   /* The memory allocated for the buffer.  NULL if this lexer does not
98      own the token buffer.  */
99   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
100   /* If the lexer owns the buffer, this is the number of tokens in the
101      buffer.  */
102   size_t buffer_length;
103
104   /* A pointer just past the last available token.  The tokens
105      in this lexer are [buffer, last_token).  */
106   cp_token_position GTY ((skip)) last_token;
107
108   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
109      no more available tokens.  */
110   cp_token_position GTY ((skip)) next_token;
111
112   /* A stack indicating positions at which cp_lexer_save_tokens was
113      called.  The top entry is the most recent position at which we
114      began saving tokens.  If the stack is non-empty, we are saving
115      tokens.  */
116   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
117
118   /* The next lexer in a linked list of lexers.  */
119   struct cp_lexer *next;
120
121   /* True if we should output debugging information.  */
122   bool debugging_p;
123
124   /* True if we're in the context of parsing a pragma, and should not
125      increment past the end-of-line marker.  */
126   bool in_pragma;
127 } cp_lexer;
128
129 /* cp_token_cache is a range of tokens.  There is no need to represent
130    allocate heap memory for it, since tokens are never removed from the
131    lexer's array.  There is also no need for the GC to walk through
132    a cp_token_cache, since everything in here is referenced through
133    a lexer.  */
134
135 typedef struct cp_token_cache GTY(())
136 {
137   /* The beginning of the token range.  */
138   cp_token * GTY((skip)) first;
139
140   /* Points immediately after the last token in the range.  */
141   cp_token * GTY ((skip)) last;
142 } cp_token_cache;
143
144 /* Prototypes.  */
145
146 static cp_lexer *cp_lexer_new_main
147   (void);
148 static cp_lexer *cp_lexer_new_from_tokens
149   (cp_token_cache *tokens);
150 static void cp_lexer_destroy
151   (cp_lexer *);
152 static int cp_lexer_saving_tokens
153   (const cp_lexer *);
154 static cp_token_position cp_lexer_token_position
155   (cp_lexer *, bool);
156 static cp_token *cp_lexer_token_at
157   (cp_lexer *, cp_token_position);
158 static void cp_lexer_get_preprocessor_token
159   (cp_lexer *, cp_token *);
160 static inline cp_token *cp_lexer_peek_token
161   (cp_lexer *);
162 static cp_token *cp_lexer_peek_nth_token
163   (cp_lexer *, size_t);
164 static inline bool cp_lexer_next_token_is
165   (cp_lexer *, enum cpp_ttype);
166 static bool cp_lexer_next_token_is_not
167   (cp_lexer *, enum cpp_ttype);
168 static bool cp_lexer_next_token_is_keyword
169   (cp_lexer *, enum rid);
170 static cp_token *cp_lexer_consume_token
171   (cp_lexer *);
172 static void cp_lexer_purge_token
173   (cp_lexer *);
174 static void cp_lexer_purge_tokens_after
175   (cp_lexer *, cp_token_position);
176 static void cp_lexer_save_tokens
177   (cp_lexer *);
178 static void cp_lexer_commit_tokens
179   (cp_lexer *);
180 static void cp_lexer_rollback_tokens
181   (cp_lexer *);
182 #ifdef ENABLE_CHECKING
183 static void cp_lexer_print_token
184   (FILE *, cp_token *);
185 static inline bool cp_lexer_debugging_p
186   (cp_lexer *);
187 static void cp_lexer_start_debugging
188   (cp_lexer *) ATTRIBUTE_UNUSED;
189 static void cp_lexer_stop_debugging
190   (cp_lexer *) ATTRIBUTE_UNUSED;
191 #else
192 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
193    about passing NULL to functions that require non-NULL arguments
194    (fputs, fprintf).  It will never be used, so all we need is a value
195    of the right type that's guaranteed not to be NULL.  */
196 #define cp_lexer_debug_stream stdout
197 #define cp_lexer_print_token(str, tok) (void) 0
198 #define cp_lexer_debugging_p(lexer) 0
199 #endif /* ENABLE_CHECKING */
200
201 static cp_token_cache *cp_token_cache_new
202   (cp_token *, cp_token *);
203
204 static void cp_parser_initial_pragma
205   (cp_token *);
206
207 /* Manifest constants.  */
208 #define CP_LEXER_BUFFER_SIZE 10000
209 #define CP_SAVED_TOKEN_STACK 5
210
211 /* A token type for keywords, as opposed to ordinary identifiers.  */
212 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
213
214 /* A token type for template-ids.  If a template-id is processed while
215    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
216    the value of the CPP_TEMPLATE_ID is whatever was returned by
217    cp_parser_template_id.  */
218 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
219
220 /* A token type for nested-name-specifiers.  If a
221    nested-name-specifier is processed while parsing tentatively, it is
222    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
223    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
224    cp_parser_nested_name_specifier_opt.  */
225 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
226
227 /* A token type for tokens that are not tokens at all; these are used
228    to represent slots in the array where there used to be a token
229    that has now been deleted.  */
230 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
231
232 /* The number of token types, including C++-specific ones.  */
233 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
234
235 /* Variables.  */
236
237 #ifdef ENABLE_CHECKING
238 /* The stream to which debugging output should be written.  */
239 static FILE *cp_lexer_debug_stream;
240 #endif /* ENABLE_CHECKING */
241
242 /* Create a new main C++ lexer, the lexer that gets tokens from the
243    preprocessor.  */
244
245 static cp_lexer *
246 cp_lexer_new_main (void)
247 {
248   cp_token first_token;
249   cp_lexer *lexer;
250   cp_token *pos;
251   size_t alloc;
252   size_t space;
253   cp_token *buffer;
254
255   /* It's possible that parsing the first pragma will load a PCH file,
256      which is a GC collection point.  So we have to do that before
257      allocating any memory.  */
258   cp_parser_initial_pragma (&first_token);
259
260   /* Tell c_lex_with_flags not to merge string constants.  */
261   c_lex_return_raw_strings = true;
262
263   c_common_no_more_pch ();
264
265   /* Allocate the memory.  */
266   lexer = GGC_CNEW (cp_lexer);
267
268 #ifdef ENABLE_CHECKING
269   /* Initially we are not debugging.  */
270   lexer->debugging_p = false;
271 #endif /* ENABLE_CHECKING */
272   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
273                                    CP_SAVED_TOKEN_STACK);
274
275   /* Create the buffer.  */
276   alloc = CP_LEXER_BUFFER_SIZE;
277   buffer = GGC_NEWVEC (cp_token, alloc);
278
279   /* Put the first token in the buffer.  */
280   space = alloc;
281   pos = buffer;
282   *pos = first_token;
283
284   /* Get the remaining tokens from the preprocessor.  */
285   while (pos->type != CPP_EOF)
286     {
287       pos++;
288       if (!--space)
289         {
290           space = alloc;
291           alloc *= 2;
292           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
293           pos = buffer + space;
294         }
295       cp_lexer_get_preprocessor_token (lexer, pos);
296     }
297   lexer->buffer = buffer;
298   lexer->buffer_length = alloc - space;
299   lexer->last_token = pos;
300   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
301
302   /* Subsequent preprocessor diagnostics should use compiler
303      diagnostic functions to get the compiler source location.  */
304   cpp_get_options (parse_in)->client_diagnostic = true;
305   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
306
307   gcc_assert (lexer->next_token->type != CPP_PURGED);
308   return lexer;
309 }
310
311 /* Create a new lexer whose token stream is primed with the tokens in
312    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
313
314 static cp_lexer *
315 cp_lexer_new_from_tokens (cp_token_cache *cache)
316 {
317   cp_token *first = cache->first;
318   cp_token *last = cache->last;
319   cp_lexer *lexer = GGC_CNEW (cp_lexer);
320
321   /* We do not own the buffer.  */
322   lexer->buffer = NULL;
323   lexer->buffer_length = 0;
324   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
325   lexer->last_token = last;
326
327   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
328                                    CP_SAVED_TOKEN_STACK);
329
330 #ifdef ENABLE_CHECKING
331   /* Initially we are not debugging.  */
332   lexer->debugging_p = false;
333 #endif
334
335   gcc_assert (lexer->next_token->type != CPP_PURGED);
336   return lexer;
337 }
338
339 /* Frees all resources associated with LEXER.  */
340
341 static void
342 cp_lexer_destroy (cp_lexer *lexer)
343 {
344   if (lexer->buffer)
345     ggc_free (lexer->buffer);
346   VEC_free (cp_token_position, heap, lexer->saved_tokens);
347   ggc_free (lexer);
348 }
349
350 /* Returns nonzero if debugging information should be output.  */
351
352 #ifdef ENABLE_CHECKING
353
354 static inline bool
355 cp_lexer_debugging_p (cp_lexer *lexer)
356 {
357   return lexer->debugging_p;
358 }
359
360 #endif /* ENABLE_CHECKING */
361
362 static inline cp_token_position
363 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
364 {
365   gcc_assert (!previous_p || lexer->next_token != &eof_token);
366
367   return lexer->next_token - previous_p;
368 }
369
370 static inline cp_token *
371 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
372 {
373   return pos;
374 }
375
376 /* nonzero if we are presently saving tokens.  */
377
378 static inline int
379 cp_lexer_saving_tokens (const cp_lexer* lexer)
380 {
381   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
382 }
383
384 /* Store the next token from the preprocessor in *TOKEN.  Return true
385    if we reach EOF.  */
386
387 static void
388 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
389                                  cp_token *token)
390 {
391   static int is_extern_c = 0;
392
393    /* Get a new token from the preprocessor.  */
394   token->type
395     = c_lex_with_flags (&token->value, &token->location, &token->flags);
396   token->keyword = RID_MAX;
397   token->pragma_kind = PRAGMA_NONE;
398   token->in_system_header = in_system_header;
399
400   /* On some systems, some header files are surrounded by an
401      implicit extern "C" block.  Set a flag in the token if it
402      comes from such a header.  */
403   is_extern_c += pending_lang_change;
404   pending_lang_change = 0;
405   token->implicit_extern_c = is_extern_c > 0;
406
407   /* Check to see if this token is a keyword.  */
408   if (token->type == CPP_NAME)
409     {
410       if (C_IS_RESERVED_WORD (token->value))
411         {
412           /* Mark this token as a keyword.  */
413           token->type = CPP_KEYWORD;
414           /* Record which keyword.  */
415           token->keyword = C_RID_CODE (token->value);
416           /* Update the value.  Some keywords are mapped to particular
417              entities, rather than simply having the value of the
418              corresponding IDENTIFIER_NODE.  For example, `__const' is
419              mapped to `const'.  */
420           token->value = ridpointers[token->keyword];
421         }
422       else
423         {
424           token->ambiguous_p = false;
425           token->keyword = RID_MAX;
426         }
427     }
428   /* Handle Objective-C++ keywords.  */
429   else if (token->type == CPP_AT_NAME)
430     {
431       token->type = CPP_KEYWORD;
432       switch (C_RID_CODE (token->value))
433         {
434         /* Map 'class' to '@class', 'private' to '@private', etc.  */
435         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
436         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
437         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
438         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
439         case RID_THROW: token->keyword = RID_AT_THROW; break;
440         case RID_TRY: token->keyword = RID_AT_TRY; break;
441         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
442         default: token->keyword = C_RID_CODE (token->value);
443         }
444     }
445   else if (token->type == CPP_PRAGMA)
446     {
447       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
448       token->pragma_kind = TREE_INT_CST_LOW (token->value);
449       token->value = NULL;
450     }
451 }
452
453 /* Update the globals input_location and in_system_header from TOKEN.  */
454 static inline void
455 cp_lexer_set_source_position_from_token (cp_token *token)
456 {
457   if (token->type != CPP_EOF)
458     {
459       input_location = token->location;
460       in_system_header = token->in_system_header;
461     }
462 }
463
464 /* Return a pointer to the next token in the token stream, but do not
465    consume it.  */
466
467 static inline cp_token *
468 cp_lexer_peek_token (cp_lexer *lexer)
469 {
470   if (cp_lexer_debugging_p (lexer))
471     {
472       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
473       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
474       putc ('\n', cp_lexer_debug_stream);
475     }
476   return lexer->next_token;
477 }
478
479 /* Return true if the next token has the indicated TYPE.  */
480
481 static inline bool
482 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
483 {
484   return cp_lexer_peek_token (lexer)->type == type;
485 }
486
487 /* Return true if the next token does not have the indicated TYPE.  */
488
489 static inline bool
490 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
491 {
492   return !cp_lexer_next_token_is (lexer, type);
493 }
494
495 /* Return true if the next token is the indicated KEYWORD.  */
496
497 static inline bool
498 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
499 {
500   return cp_lexer_peek_token (lexer)->keyword == keyword;
501 }
502
503 /* Return a pointer to the Nth token in the token stream.  If N is 1,
504    then this is precisely equivalent to cp_lexer_peek_token (except
505    that it is not inline).  One would like to disallow that case, but
506    there is one case (cp_parser_nth_token_starts_template_id) where
507    the caller passes a variable for N and it might be 1.  */
508
509 static cp_token *
510 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
511 {
512   cp_token *token;
513
514   /* N is 1-based, not zero-based.  */
515   gcc_assert (n > 0);
516   
517   if (cp_lexer_debugging_p (lexer))
518     fprintf (cp_lexer_debug_stream,
519              "cp_lexer: peeking ahead %ld at token: ", (long)n);
520
521   --n;
522   token = lexer->next_token;
523   gcc_assert (!n || token != &eof_token);
524   while (n != 0)
525     {
526       ++token;
527       if (token == lexer->last_token)
528         {
529           token = (cp_token *)&eof_token;
530           break;
531         }
532
533       if (token->type != CPP_PURGED)
534         --n;
535     }
536
537   if (cp_lexer_debugging_p (lexer))
538     {
539       cp_lexer_print_token (cp_lexer_debug_stream, token);
540       putc ('\n', cp_lexer_debug_stream);
541     }
542
543   return token;
544 }
545
546 /* Return the next token, and advance the lexer's next_token pointer
547    to point to the next non-purged token.  */
548
549 static cp_token *
550 cp_lexer_consume_token (cp_lexer* lexer)
551 {
552   cp_token *token = lexer->next_token;
553
554   gcc_assert (token != &eof_token);
555   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
556
557   do
558     {
559       lexer->next_token++;
560       if (lexer->next_token == lexer->last_token)
561         {
562           lexer->next_token = (cp_token *)&eof_token;
563           break;
564         }
565
566     }
567   while (lexer->next_token->type == CPP_PURGED);
568
569   cp_lexer_set_source_position_from_token (token);
570
571   /* Provide debugging output.  */
572   if (cp_lexer_debugging_p (lexer))
573     {
574       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
575       cp_lexer_print_token (cp_lexer_debug_stream, token);
576       putc ('\n', cp_lexer_debug_stream);
577     }
578
579   return token;
580 }
581
582 /* Permanently remove the next token from the token stream, and
583    advance the next_token pointer to refer to the next non-purged
584    token.  */
585
586 static void
587 cp_lexer_purge_token (cp_lexer *lexer)
588 {
589   cp_token *tok = lexer->next_token;
590
591   gcc_assert (tok != &eof_token);
592   tok->type = CPP_PURGED;
593   tok->location = UNKNOWN_LOCATION;
594   tok->value = NULL_TREE;
595   tok->keyword = RID_MAX;
596
597   do
598     {
599       tok++;
600       if (tok == lexer->last_token)
601         {
602           tok = (cp_token *)&eof_token;
603           break;
604         }
605     }
606   while (tok->type == CPP_PURGED);
607   lexer->next_token = tok;
608 }
609
610 /* Permanently remove all tokens after TOK, up to, but not
611    including, the token that will be returned next by
612    cp_lexer_peek_token.  */
613
614 static void
615 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
616 {
617   cp_token *peek = lexer->next_token;
618
619   if (peek == &eof_token)
620     peek = lexer->last_token;
621
622   gcc_assert (tok < peek);
623
624   for ( tok += 1; tok != peek; tok += 1)
625     {
626       tok->type = CPP_PURGED;
627       tok->location = UNKNOWN_LOCATION;
628       tok->value = NULL_TREE;
629       tok->keyword = RID_MAX;
630     }
631 }
632
633 /* Begin saving tokens.  All tokens consumed after this point will be
634    preserved.  */
635
636 static void
637 cp_lexer_save_tokens (cp_lexer* lexer)
638 {
639   /* Provide debugging output.  */
640   if (cp_lexer_debugging_p (lexer))
641     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
642
643   VEC_safe_push (cp_token_position, heap,
644                  lexer->saved_tokens, lexer->next_token);
645 }
646
647 /* Commit to the portion of the token stream most recently saved.  */
648
649 static void
650 cp_lexer_commit_tokens (cp_lexer* lexer)
651 {
652   /* Provide debugging output.  */
653   if (cp_lexer_debugging_p (lexer))
654     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
655
656   VEC_pop (cp_token_position, lexer->saved_tokens);
657 }
658
659 /* Return all tokens saved since the last call to cp_lexer_save_tokens
660    to the token stream.  Stop saving tokens.  */
661
662 static void
663 cp_lexer_rollback_tokens (cp_lexer* lexer)
664 {
665   /* Provide debugging output.  */
666   if (cp_lexer_debugging_p (lexer))
667     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
668
669   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
670 }
671
672 /* Print a representation of the TOKEN on the STREAM.  */
673
674 #ifdef ENABLE_CHECKING
675
676 static void
677 cp_lexer_print_token (FILE * stream, cp_token *token)
678 {
679   /* We don't use cpp_type2name here because the parser defines
680      a few tokens of its own.  */
681   static const char *const token_names[] = {
682     /* cpplib-defined token types */
683 #define OP(e, s) #e,
684 #define TK(e, s) #e,
685     TTYPE_TABLE
686 #undef OP
687 #undef TK
688     /* C++ parser token types - see "Manifest constants", above.  */
689     "KEYWORD",
690     "TEMPLATE_ID",
691     "NESTED_NAME_SPECIFIER",
692     "PURGED"
693   };
694
695   /* If we have a name for the token, print it out.  Otherwise, we
696      simply give the numeric code.  */
697   gcc_assert (token->type < ARRAY_SIZE(token_names));
698   fputs (token_names[token->type], stream);
699
700   /* For some tokens, print the associated data.  */
701   switch (token->type)
702     {
703     case CPP_KEYWORD:
704       /* Some keywords have a value that is not an IDENTIFIER_NODE.
705          For example, `struct' is mapped to an INTEGER_CST.  */
706       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
707         break;
708       /* else fall through */
709     case CPP_NAME:
710       fputs (IDENTIFIER_POINTER (token->value), stream);
711       break;
712
713     case CPP_STRING:
714     case CPP_WSTRING:
715       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
716       break;
717
718     default:
719       break;
720     }
721 }
722
723 /* Start emitting debugging information.  */
724
725 static void
726 cp_lexer_start_debugging (cp_lexer* lexer)
727 {
728   lexer->debugging_p = true;
729 }
730
731 /* Stop emitting debugging information.  */
732
733 static void
734 cp_lexer_stop_debugging (cp_lexer* lexer)
735 {
736   lexer->debugging_p = false;
737 }
738
739 #endif /* ENABLE_CHECKING */
740
741 /* Create a new cp_token_cache, representing a range of tokens.  */
742
743 static cp_token_cache *
744 cp_token_cache_new (cp_token *first, cp_token *last)
745 {
746   cp_token_cache *cache = GGC_NEW (cp_token_cache);
747   cache->first = first;
748   cache->last = last;
749   return cache;
750 }
751
752 \f
753 /* Decl-specifiers.  */
754
755 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
756
757 static void
758 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
759 {
760   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
761 }
762
763 /* Declarators.  */
764
765 /* Nothing other than the parser should be creating declarators;
766    declarators are a semi-syntactic representation of C++ entities.
767    Other parts of the front end that need to create entities (like
768    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
769
770 static cp_declarator *make_call_declarator
771   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
772 static cp_declarator *make_array_declarator
773   (cp_declarator *, tree);
774 static cp_declarator *make_pointer_declarator
775   (cp_cv_quals, cp_declarator *);
776 static cp_declarator *make_reference_declarator
777   (cp_cv_quals, cp_declarator *);
778 static cp_parameter_declarator *make_parameter_declarator
779   (cp_decl_specifier_seq *, cp_declarator *, tree);
780 static cp_declarator *make_ptrmem_declarator
781   (cp_cv_quals, tree, cp_declarator *);
782
783 cp_declarator *cp_error_declarator;
784
785 /* The obstack on which declarators and related data structures are
786    allocated.  */
787 static struct obstack declarator_obstack;
788
789 /* Alloc BYTES from the declarator memory pool.  */
790
791 static inline void *
792 alloc_declarator (size_t bytes)
793 {
794   return obstack_alloc (&declarator_obstack, bytes);
795 }
796
797 /* Allocate a declarator of the indicated KIND.  Clear fields that are
798    common to all declarators.  */
799
800 static cp_declarator *
801 make_declarator (cp_declarator_kind kind)
802 {
803   cp_declarator *declarator;
804
805   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
806   declarator->kind = kind;
807   declarator->attributes = NULL_TREE;
808   declarator->declarator = NULL;
809
810   return declarator;
811 }
812
813 /* Make a declarator for a generalized identifier.  If
814    QUALIFYING_SCOPE is non-NULL, the identifier is
815    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
816    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
817    is, if any.   */
818
819 static cp_declarator *
820 make_id_declarator (tree qualifying_scope, tree unqualified_name,
821                     special_function_kind sfk)
822 {
823   cp_declarator *declarator;
824
825   /* It is valid to write:
826
827        class C { void f(); };
828        typedef C D;
829        void D::f();
830
831      The standard is not clear about whether `typedef const C D' is
832      legal; as of 2002-09-15 the committee is considering that
833      question.  EDG 3.0 allows that syntax.  Therefore, we do as
834      well.  */
835   if (qualifying_scope && TYPE_P (qualifying_scope))
836     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
837
838   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
839               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
840               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
841
842   declarator = make_declarator (cdk_id);
843   declarator->u.id.qualifying_scope = qualifying_scope;
844   declarator->u.id.unqualified_name = unqualified_name;
845   declarator->u.id.sfk = sfk;
846
847   return declarator;
848 }
849
850 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
851    of modifiers such as const or volatile to apply to the pointer
852    type, represented as identifiers.  */
853
854 cp_declarator *
855 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
856 {
857   cp_declarator *declarator;
858
859   declarator = make_declarator (cdk_pointer);
860   declarator->declarator = target;
861   declarator->u.pointer.qualifiers = cv_qualifiers;
862   declarator->u.pointer.class_type = NULL_TREE;
863
864   return declarator;
865 }
866
867 /* Like make_pointer_declarator -- but for references.  */
868
869 cp_declarator *
870 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
871 {
872   cp_declarator *declarator;
873
874   declarator = make_declarator (cdk_reference);
875   declarator->declarator = target;
876   declarator->u.pointer.qualifiers = cv_qualifiers;
877   declarator->u.pointer.class_type = NULL_TREE;
878
879   return declarator;
880 }
881
882 /* Like make_pointer_declarator -- but for a pointer to a non-static
883    member of CLASS_TYPE.  */
884
885 cp_declarator *
886 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
887                         cp_declarator *pointee)
888 {
889   cp_declarator *declarator;
890
891   declarator = make_declarator (cdk_ptrmem);
892   declarator->declarator = pointee;
893   declarator->u.pointer.qualifiers = cv_qualifiers;
894   declarator->u.pointer.class_type = class_type;
895
896   return declarator;
897 }
898
899 /* Make a declarator for the function given by TARGET, with the
900    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
901    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
902    indicates what exceptions can be thrown.  */
903
904 cp_declarator *
905 make_call_declarator (cp_declarator *target,
906                       cp_parameter_declarator *parms,
907                       cp_cv_quals cv_qualifiers,
908                       tree exception_specification)
909 {
910   cp_declarator *declarator;
911
912   declarator = make_declarator (cdk_function);
913   declarator->declarator = target;
914   declarator->u.function.parameters = parms;
915   declarator->u.function.qualifiers = cv_qualifiers;
916   declarator->u.function.exception_specification = exception_specification;
917
918   return declarator;
919 }
920
921 /* Make a declarator for an array of BOUNDS elements, each of which is
922    defined by ELEMENT.  */
923
924 cp_declarator *
925 make_array_declarator (cp_declarator *element, tree bounds)
926 {
927   cp_declarator *declarator;
928
929   declarator = make_declarator (cdk_array);
930   declarator->declarator = element;
931   declarator->u.array.bounds = bounds;
932
933   return declarator;
934 }
935
936 cp_parameter_declarator *no_parameters;
937
938 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
939    DECLARATOR and DEFAULT_ARGUMENT.  */
940
941 cp_parameter_declarator *
942 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
943                            cp_declarator *declarator,
944                            tree default_argument)
945 {
946   cp_parameter_declarator *parameter;
947
948   parameter = ((cp_parameter_declarator *)
949                alloc_declarator (sizeof (cp_parameter_declarator)));
950   parameter->next = NULL;
951   if (decl_specifiers)
952     parameter->decl_specifiers = *decl_specifiers;
953   else
954     clear_decl_specs (&parameter->decl_specifiers);
955   parameter->declarator = declarator;
956   parameter->default_argument = default_argument;
957   parameter->ellipsis_p = false;
958
959   return parameter;
960 }
961
962 /* The parser.  */
963
964 /* Overview
965    --------
966
967    A cp_parser parses the token stream as specified by the C++
968    grammar.  Its job is purely parsing, not semantic analysis.  For
969    example, the parser breaks the token stream into declarators,
970    expressions, statements, and other similar syntactic constructs.
971    It does not check that the types of the expressions on either side
972    of an assignment-statement are compatible, or that a function is
973    not declared with a parameter of type `void'.
974
975    The parser invokes routines elsewhere in the compiler to perform
976    semantic analysis and to build up the abstract syntax tree for the
977    code processed.
978
979    The parser (and the template instantiation code, which is, in a
980    way, a close relative of parsing) are the only parts of the
981    compiler that should be calling push_scope and pop_scope, or
982    related functions.  The parser (and template instantiation code)
983    keeps track of what scope is presently active; everything else
984    should simply honor that.  (The code that generates static
985    initializers may also need to set the scope, in order to check
986    access control correctly when emitting the initializers.)
987
988    Methodology
989    -----------
990
991    The parser is of the standard recursive-descent variety.  Upcoming
992    tokens in the token stream are examined in order to determine which
993    production to use when parsing a non-terminal.  Some C++ constructs
994    require arbitrary look ahead to disambiguate.  For example, it is
995    impossible, in the general case, to tell whether a statement is an
996    expression or declaration without scanning the entire statement.
997    Therefore, the parser is capable of "parsing tentatively."  When the
998    parser is not sure what construct comes next, it enters this mode.
999    Then, while we attempt to parse the construct, the parser queues up
1000    error messages, rather than issuing them immediately, and saves the
1001    tokens it consumes.  If the construct is parsed successfully, the
1002    parser "commits", i.e., it issues any queued error messages and
1003    the tokens that were being preserved are permanently discarded.
1004    If, however, the construct is not parsed successfully, the parser
1005    rolls back its state completely so that it can resume parsing using
1006    a different alternative.
1007
1008    Future Improvements
1009    -------------------
1010
1011    The performance of the parser could probably be improved substantially.
1012    We could often eliminate the need to parse tentatively by looking ahead
1013    a little bit.  In some places, this approach might not entirely eliminate
1014    the need to parse tentatively, but it might still speed up the average
1015    case.  */
1016
1017 /* Flags that are passed to some parsing functions.  These values can
1018    be bitwise-ored together.  */
1019
1020 typedef enum cp_parser_flags
1021 {
1022   /* No flags.  */
1023   CP_PARSER_FLAGS_NONE = 0x0,
1024   /* The construct is optional.  If it is not present, then no error
1025      should be issued.  */
1026   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1027   /* When parsing a type-specifier, do not allow user-defined types.  */
1028   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1029 } cp_parser_flags;
1030
1031 /* The different kinds of declarators we want to parse.  */
1032
1033 typedef enum cp_parser_declarator_kind
1034 {
1035   /* We want an abstract declarator.  */
1036   CP_PARSER_DECLARATOR_ABSTRACT,
1037   /* We want a named declarator.  */
1038   CP_PARSER_DECLARATOR_NAMED,
1039   /* We don't mind, but the name must be an unqualified-id.  */
1040   CP_PARSER_DECLARATOR_EITHER
1041 } cp_parser_declarator_kind;
1042
1043 /* The precedence values used to parse binary expressions.  The minimum value
1044    of PREC must be 1, because zero is reserved to quickly discriminate
1045    binary operators from other tokens.  */
1046
1047 enum cp_parser_prec
1048 {
1049   PREC_NOT_OPERATOR,
1050   PREC_LOGICAL_OR_EXPRESSION,
1051   PREC_LOGICAL_AND_EXPRESSION,
1052   PREC_INCLUSIVE_OR_EXPRESSION,
1053   PREC_EXCLUSIVE_OR_EXPRESSION,
1054   PREC_AND_EXPRESSION,
1055   PREC_EQUALITY_EXPRESSION,
1056   PREC_RELATIONAL_EXPRESSION,
1057   PREC_SHIFT_EXPRESSION,
1058   PREC_ADDITIVE_EXPRESSION,
1059   PREC_MULTIPLICATIVE_EXPRESSION,
1060   PREC_PM_EXPRESSION,
1061   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1062 };
1063
1064 /* A mapping from a token type to a corresponding tree node type, with a
1065    precedence value.  */
1066
1067 typedef struct cp_parser_binary_operations_map_node
1068 {
1069   /* The token type.  */
1070   enum cpp_ttype token_type;
1071   /* The corresponding tree code.  */
1072   enum tree_code tree_type;
1073   /* The precedence of this operator.  */
1074   enum cp_parser_prec prec;
1075 } cp_parser_binary_operations_map_node;
1076
1077 /* The status of a tentative parse.  */
1078
1079 typedef enum cp_parser_status_kind
1080 {
1081   /* No errors have occurred.  */
1082   CP_PARSER_STATUS_KIND_NO_ERROR,
1083   /* An error has occurred.  */
1084   CP_PARSER_STATUS_KIND_ERROR,
1085   /* We are committed to this tentative parse, whether or not an error
1086      has occurred.  */
1087   CP_PARSER_STATUS_KIND_COMMITTED
1088 } cp_parser_status_kind;
1089
1090 typedef struct cp_parser_expression_stack_entry
1091 {
1092   tree lhs;
1093   enum tree_code tree_type;
1094   int prec;
1095 } cp_parser_expression_stack_entry;
1096
1097 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1098    entries because precedence levels on the stack are monotonically
1099    increasing.  */
1100 typedef struct cp_parser_expression_stack_entry
1101   cp_parser_expression_stack[NUM_PREC_VALUES];
1102
1103 /* Context that is saved and restored when parsing tentatively.  */
1104 typedef struct cp_parser_context GTY (())
1105 {
1106   /* If this is a tentative parsing context, the status of the
1107      tentative parse.  */
1108   enum cp_parser_status_kind status;
1109   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1110      that are looked up in this context must be looked up both in the
1111      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1112      the context of the containing expression.  */
1113   tree object_type;
1114
1115   /* The next parsing context in the stack.  */
1116   struct cp_parser_context *next;
1117 } cp_parser_context;
1118
1119 /* Prototypes.  */
1120
1121 /* Constructors and destructors.  */
1122
1123 static cp_parser_context *cp_parser_context_new
1124   (cp_parser_context *);
1125
1126 /* Class variables.  */
1127
1128 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1129
1130 /* The operator-precedence table used by cp_parser_binary_expression.
1131    Transformed into an associative array (binops_by_token) by
1132    cp_parser_new.  */
1133
1134 static const cp_parser_binary_operations_map_node binops[] = {
1135   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1136   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1137
1138   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1139   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1140   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1141
1142   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1143   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1144
1145   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1146   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1147
1148   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1149   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1150   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1151   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1152   { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1153   { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1154
1155   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1156   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1157
1158   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1159
1160   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1161
1162   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1163
1164   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1165
1166   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1167 };
1168
1169 /* The same as binops, but initialized by cp_parser_new so that
1170    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1171    for speed.  */
1172 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1173
1174 /* Constructors and destructors.  */
1175
1176 /* Construct a new context.  The context below this one on the stack
1177    is given by NEXT.  */
1178
1179 static cp_parser_context *
1180 cp_parser_context_new (cp_parser_context* next)
1181 {
1182   cp_parser_context *context;
1183
1184   /* Allocate the storage.  */
1185   if (cp_parser_context_free_list != NULL)
1186     {
1187       /* Pull the first entry from the free list.  */
1188       context = cp_parser_context_free_list;
1189       cp_parser_context_free_list = context->next;
1190       memset (context, 0, sizeof (*context));
1191     }
1192   else
1193     context = GGC_CNEW (cp_parser_context);
1194
1195   /* No errors have occurred yet in this context.  */
1196   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1197   /* If this is not the bottomost context, copy information that we
1198      need from the previous context.  */
1199   if (next)
1200     {
1201       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1202          expression, then we are parsing one in this context, too.  */
1203       context->object_type = next->object_type;
1204       /* Thread the stack.  */
1205       context->next = next;
1206     }
1207
1208   return context;
1209 }
1210
1211 /* The cp_parser structure represents the C++ parser.  */
1212
1213 typedef struct cp_parser GTY(())
1214 {
1215   /* The lexer from which we are obtaining tokens.  */
1216   cp_lexer *lexer;
1217
1218   /* The scope in which names should be looked up.  If NULL_TREE, then
1219      we look up names in the scope that is currently open in the
1220      source program.  If non-NULL, this is either a TYPE or
1221      NAMESPACE_DECL for the scope in which we should look.  It can
1222      also be ERROR_MARK, when we've parsed a bogus scope.
1223
1224      This value is not cleared automatically after a name is looked
1225      up, so we must be careful to clear it before starting a new look
1226      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1227      will look up `Z' in the scope of `X', rather than the current
1228      scope.)  Unfortunately, it is difficult to tell when name lookup
1229      is complete, because we sometimes peek at a token, look it up,
1230      and then decide not to consume it.   */
1231   tree scope;
1232
1233   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1234      last lookup took place.  OBJECT_SCOPE is used if an expression
1235      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1236      respectively.  QUALIFYING_SCOPE is used for an expression of the
1237      form "X::Y"; it refers to X.  */
1238   tree object_scope;
1239   tree qualifying_scope;
1240
1241   /* A stack of parsing contexts.  All but the bottom entry on the
1242      stack will be tentative contexts.
1243
1244      We parse tentatively in order to determine which construct is in
1245      use in some situations.  For example, in order to determine
1246      whether a statement is an expression-statement or a
1247      declaration-statement we parse it tentatively as a
1248      declaration-statement.  If that fails, we then reparse the same
1249      token stream as an expression-statement.  */
1250   cp_parser_context *context;
1251
1252   /* True if we are parsing GNU C++.  If this flag is not set, then
1253      GNU extensions are not recognized.  */
1254   bool allow_gnu_extensions_p;
1255
1256   /* TRUE if the `>' token should be interpreted as the greater-than
1257      operator.  FALSE if it is the end of a template-id or
1258      template-parameter-list.  */
1259   bool greater_than_is_operator_p;
1260
1261   /* TRUE if default arguments are allowed within a parameter list
1262      that starts at this point. FALSE if only a gnu extension makes
1263      them permissible.  */
1264   bool default_arg_ok_p;
1265
1266   /* TRUE if we are parsing an integral constant-expression.  See
1267      [expr.const] for a precise definition.  */
1268   bool integral_constant_expression_p;
1269
1270   /* TRUE if we are parsing an integral constant-expression -- but a
1271      non-constant expression should be permitted as well.  This flag
1272      is used when parsing an array bound so that GNU variable-length
1273      arrays are tolerated.  */
1274   bool allow_non_integral_constant_expression_p;
1275
1276   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1277      been seen that makes the expression non-constant.  */
1278   bool non_integral_constant_expression_p;
1279
1280   /* TRUE if local variable names and `this' are forbidden in the
1281      current context.  */
1282   bool local_variables_forbidden_p;
1283
1284   /* TRUE if the declaration we are parsing is part of a
1285      linkage-specification of the form `extern string-literal
1286      declaration'.  */
1287   bool in_unbraced_linkage_specification_p;
1288
1289   /* TRUE if we are presently parsing a declarator, after the
1290      direct-declarator.  */
1291   bool in_declarator_p;
1292
1293   /* TRUE if we are presently parsing a template-argument-list.  */
1294   bool in_template_argument_list_p;
1295
1296   /* TRUE if we are presently parsing the body of an
1297      iteration-statement.  */
1298   bool in_iteration_statement_p;
1299
1300   /* TRUE if we are presently parsing the body of a switch
1301      statement.  */
1302   bool in_switch_statement_p;
1303
1304   /* TRUE if we are parsing a type-id in an expression context.  In
1305      such a situation, both "type (expr)" and "type (type)" are valid
1306      alternatives.  */
1307   bool in_type_id_in_expr_p;
1308
1309   /* TRUE if we are currently in a header file where declarations are
1310      implicitly extern "C".  */
1311   bool implicit_extern_c;
1312
1313   /* TRUE if strings in expressions should be translated to the execution
1314      character set.  */
1315   bool translate_strings_p;
1316
1317   /* If non-NULL, then we are parsing a construct where new type
1318      definitions are not permitted.  The string stored here will be
1319      issued as an error message if a type is defined.  */
1320   const char *type_definition_forbidden_message;
1321
1322   /* A list of lists. The outer list is a stack, used for member
1323      functions of local classes. At each level there are two sub-list,
1324      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1325      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1326      TREE_VALUE's. The functions are chained in reverse declaration
1327      order.
1328
1329      The TREE_PURPOSE sublist contains those functions with default
1330      arguments that need post processing, and the TREE_VALUE sublist
1331      contains those functions with definitions that need post
1332      processing.
1333
1334      These lists can only be processed once the outermost class being
1335      defined is complete.  */
1336   tree unparsed_functions_queues;
1337
1338   /* The number of classes whose definitions are currently in
1339      progress.  */
1340   unsigned num_classes_being_defined;
1341
1342   /* The number of template parameter lists that apply directly to the
1343      current declaration.  */
1344   unsigned num_template_parameter_lists;
1345 } cp_parser;
1346
1347 /* Prototypes.  */
1348
1349 /* Constructors and destructors.  */
1350
1351 static cp_parser *cp_parser_new
1352   (void);
1353
1354 /* Routines to parse various constructs.
1355
1356    Those that return `tree' will return the error_mark_node (rather
1357    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1358    Sometimes, they will return an ordinary node if error-recovery was
1359    attempted, even though a parse error occurred.  So, to check
1360    whether or not a parse error occurred, you should always use
1361    cp_parser_error_occurred.  If the construct is optional (indicated
1362    either by an `_opt' in the name of the function that does the
1363    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1364    the construct is not present.  */
1365
1366 /* Lexical conventions [gram.lex]  */
1367
1368 static tree cp_parser_identifier
1369   (cp_parser *);
1370 static tree cp_parser_string_literal
1371   (cp_parser *, bool, bool);
1372
1373 /* Basic concepts [gram.basic]  */
1374
1375 static bool cp_parser_translation_unit
1376   (cp_parser *);
1377
1378 /* Expressions [gram.expr]  */
1379
1380 static tree cp_parser_primary_expression
1381   (cp_parser *, bool, bool, bool, cp_id_kind *);
1382 static tree cp_parser_id_expression
1383   (cp_parser *, bool, bool, bool *, bool);
1384 static tree cp_parser_unqualified_id
1385   (cp_parser *, bool, bool, bool);
1386 static tree cp_parser_nested_name_specifier_opt
1387   (cp_parser *, bool, bool, bool, bool);
1388 static tree cp_parser_nested_name_specifier
1389   (cp_parser *, bool, bool, bool, bool);
1390 static tree cp_parser_class_or_namespace_name
1391   (cp_parser *, bool, bool, bool, bool, bool);
1392 static tree cp_parser_postfix_expression
1393   (cp_parser *, bool, bool);
1394 static tree cp_parser_postfix_open_square_expression
1395   (cp_parser *, tree, bool);
1396 static tree cp_parser_postfix_dot_deref_expression
1397   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1398 static tree cp_parser_parenthesized_expression_list
1399   (cp_parser *, bool, bool, bool *);
1400 static void cp_parser_pseudo_destructor_name
1401   (cp_parser *, tree *, tree *);
1402 static tree cp_parser_unary_expression
1403   (cp_parser *, bool, bool);
1404 static enum tree_code cp_parser_unary_operator
1405   (cp_token *);
1406 static tree cp_parser_new_expression
1407   (cp_parser *);
1408 static tree cp_parser_new_placement
1409   (cp_parser *);
1410 static tree cp_parser_new_type_id
1411   (cp_parser *, tree *);
1412 static cp_declarator *cp_parser_new_declarator_opt
1413   (cp_parser *);
1414 static cp_declarator *cp_parser_direct_new_declarator
1415   (cp_parser *);
1416 static tree cp_parser_new_initializer
1417   (cp_parser *);
1418 static tree cp_parser_delete_expression
1419   (cp_parser *);
1420 static tree cp_parser_cast_expression
1421   (cp_parser *, bool, bool);
1422 static tree cp_parser_binary_expression
1423   (cp_parser *, bool);
1424 static tree cp_parser_question_colon_clause
1425   (cp_parser *, tree);
1426 static tree cp_parser_assignment_expression
1427   (cp_parser *, bool);
1428 static enum tree_code cp_parser_assignment_operator_opt
1429   (cp_parser *);
1430 static tree cp_parser_expression
1431   (cp_parser *, bool);
1432 static tree cp_parser_constant_expression
1433   (cp_parser *, bool, bool *);
1434 static tree cp_parser_builtin_offsetof
1435   (cp_parser *);
1436
1437 /* Statements [gram.stmt.stmt]  */
1438
1439 static void cp_parser_statement
1440   (cp_parser *, tree, bool);
1441 static tree cp_parser_labeled_statement
1442   (cp_parser *, tree, bool);
1443 static tree cp_parser_expression_statement
1444   (cp_parser *, tree);
1445 static tree cp_parser_compound_statement
1446   (cp_parser *, tree, bool);
1447 static void cp_parser_statement_seq_opt
1448   (cp_parser *, tree);
1449 static tree cp_parser_selection_statement
1450   (cp_parser *);
1451 static tree cp_parser_condition
1452   (cp_parser *);
1453 static tree cp_parser_iteration_statement
1454   (cp_parser *);
1455 static void cp_parser_for_init_statement
1456   (cp_parser *);
1457 static tree cp_parser_jump_statement
1458   (cp_parser *);
1459 static void cp_parser_declaration_statement
1460   (cp_parser *);
1461
1462 static tree cp_parser_implicitly_scoped_statement
1463   (cp_parser *);
1464 static void cp_parser_already_scoped_statement
1465   (cp_parser *);
1466
1467 /* Declarations [gram.dcl.dcl] */
1468
1469 static void cp_parser_declaration_seq_opt
1470   (cp_parser *);
1471 static void cp_parser_declaration
1472   (cp_parser *);
1473 static void cp_parser_block_declaration
1474   (cp_parser *, bool);
1475 static void cp_parser_simple_declaration
1476   (cp_parser *, bool);
1477 static void cp_parser_decl_specifier_seq
1478   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1479 static tree cp_parser_storage_class_specifier_opt
1480   (cp_parser *);
1481 static tree cp_parser_function_specifier_opt
1482   (cp_parser *, cp_decl_specifier_seq *);
1483 static tree cp_parser_type_specifier
1484   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1485    int *, bool *);
1486 static tree cp_parser_simple_type_specifier
1487   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1488 static tree cp_parser_type_name
1489   (cp_parser *);
1490 static tree cp_parser_elaborated_type_specifier
1491   (cp_parser *, bool, bool);
1492 static tree cp_parser_enum_specifier
1493   (cp_parser *);
1494 static void cp_parser_enumerator_list
1495   (cp_parser *, tree);
1496 static void cp_parser_enumerator_definition
1497   (cp_parser *, tree);
1498 static tree cp_parser_namespace_name
1499   (cp_parser *);
1500 static void cp_parser_namespace_definition
1501   (cp_parser *);
1502 static void cp_parser_namespace_body
1503   (cp_parser *);
1504 static tree cp_parser_qualified_namespace_specifier
1505   (cp_parser *);
1506 static void cp_parser_namespace_alias_definition
1507   (cp_parser *);
1508 static void cp_parser_using_declaration
1509   (cp_parser *);
1510 static void cp_parser_using_directive
1511   (cp_parser *);
1512 static void cp_parser_asm_definition
1513   (cp_parser *);
1514 static void cp_parser_linkage_specification
1515   (cp_parser *);
1516
1517 /* Declarators [gram.dcl.decl] */
1518
1519 static tree cp_parser_init_declarator
1520   (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1521 static cp_declarator *cp_parser_declarator
1522   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1523 static cp_declarator *cp_parser_direct_declarator
1524   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1525 static enum tree_code cp_parser_ptr_operator
1526   (cp_parser *, tree *, cp_cv_quals *);
1527 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1528   (cp_parser *);
1529 static tree cp_parser_declarator_id
1530   (cp_parser *);
1531 static tree cp_parser_type_id
1532   (cp_parser *);
1533 static void cp_parser_type_specifier_seq
1534   (cp_parser *, bool, cp_decl_specifier_seq *);
1535 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1536   (cp_parser *);
1537 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1538   (cp_parser *, bool *);
1539 static cp_parameter_declarator *cp_parser_parameter_declaration
1540   (cp_parser *, bool, bool *);
1541 static void cp_parser_function_body
1542   (cp_parser *);
1543 static tree cp_parser_initializer
1544   (cp_parser *, bool *, bool *);
1545 static tree cp_parser_initializer_clause
1546   (cp_parser *, bool *);
1547 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1548   (cp_parser *, bool *);
1549
1550 static bool cp_parser_ctor_initializer_opt_and_function_body
1551   (cp_parser *);
1552
1553 /* Classes [gram.class] */
1554
1555 static tree cp_parser_class_name
1556   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1557 static tree cp_parser_class_specifier
1558   (cp_parser *);
1559 static tree cp_parser_class_head
1560   (cp_parser *, bool *, tree *);
1561 static enum tag_types cp_parser_class_key
1562   (cp_parser *);
1563 static void cp_parser_member_specification_opt
1564   (cp_parser *);
1565 static void cp_parser_member_declaration
1566   (cp_parser *);
1567 static tree cp_parser_pure_specifier
1568   (cp_parser *);
1569 static tree cp_parser_constant_initializer
1570   (cp_parser *);
1571
1572 /* Derived classes [gram.class.derived] */
1573
1574 static tree cp_parser_base_clause
1575   (cp_parser *);
1576 static tree cp_parser_base_specifier
1577   (cp_parser *);
1578
1579 /* Special member functions [gram.special] */
1580
1581 static tree cp_parser_conversion_function_id
1582   (cp_parser *);
1583 static tree cp_parser_conversion_type_id
1584   (cp_parser *);
1585 static cp_declarator *cp_parser_conversion_declarator_opt
1586   (cp_parser *);
1587 static bool cp_parser_ctor_initializer_opt
1588   (cp_parser *);
1589 static void cp_parser_mem_initializer_list
1590   (cp_parser *);
1591 static tree cp_parser_mem_initializer
1592   (cp_parser *);
1593 static tree cp_parser_mem_initializer_id
1594   (cp_parser *);
1595
1596 /* Overloading [gram.over] */
1597
1598 static tree cp_parser_operator_function_id
1599   (cp_parser *);
1600 static tree cp_parser_operator
1601   (cp_parser *);
1602
1603 /* Templates [gram.temp] */
1604
1605 static void cp_parser_template_declaration
1606   (cp_parser *, bool);
1607 static tree cp_parser_template_parameter_list
1608   (cp_parser *);
1609 static tree cp_parser_template_parameter
1610   (cp_parser *, bool *);
1611 static tree cp_parser_type_parameter
1612   (cp_parser *);
1613 static tree cp_parser_template_id
1614   (cp_parser *, bool, bool, bool);
1615 static tree cp_parser_template_name
1616   (cp_parser *, bool, bool, bool, bool *);
1617 static tree cp_parser_template_argument_list
1618   (cp_parser *);
1619 static tree cp_parser_template_argument
1620   (cp_parser *);
1621 static void cp_parser_explicit_instantiation
1622   (cp_parser *);
1623 static void cp_parser_explicit_specialization
1624   (cp_parser *);
1625
1626 /* Exception handling [gram.exception] */
1627
1628 static tree cp_parser_try_block
1629   (cp_parser *);
1630 static bool cp_parser_function_try_block
1631   (cp_parser *);
1632 static void cp_parser_handler_seq
1633   (cp_parser *);
1634 static void cp_parser_handler
1635   (cp_parser *);
1636 static tree cp_parser_exception_declaration
1637   (cp_parser *);
1638 static tree cp_parser_throw_expression
1639   (cp_parser *);
1640 static tree cp_parser_exception_specification_opt
1641   (cp_parser *);
1642 static tree cp_parser_type_id_list
1643   (cp_parser *);
1644
1645 /* GNU Extensions */
1646
1647 static tree cp_parser_asm_specification_opt
1648   (cp_parser *);
1649 static tree cp_parser_asm_operand_list
1650   (cp_parser *);
1651 static tree cp_parser_asm_clobber_list
1652   (cp_parser *);
1653 static tree cp_parser_attributes_opt
1654   (cp_parser *);
1655 static tree cp_parser_attribute_list
1656   (cp_parser *);
1657 static bool cp_parser_extension_opt
1658   (cp_parser *, int *);
1659 static void cp_parser_label_declaration
1660   (cp_parser *);
1661
1662 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1663 static bool cp_parser_pragma
1664   (cp_parser *, enum pragma_context);
1665
1666 /* Objective-C++ Productions */
1667
1668 static tree cp_parser_objc_message_receiver
1669   (cp_parser *);
1670 static tree cp_parser_objc_message_args
1671   (cp_parser *);
1672 static tree cp_parser_objc_message_expression
1673   (cp_parser *);
1674 static tree cp_parser_objc_encode_expression
1675   (cp_parser *);
1676 static tree cp_parser_objc_defs_expression
1677   (cp_parser *);
1678 static tree cp_parser_objc_protocol_expression
1679   (cp_parser *);
1680 static tree cp_parser_objc_selector_expression
1681   (cp_parser *);
1682 static tree cp_parser_objc_expression
1683   (cp_parser *);
1684 static bool cp_parser_objc_selector_p
1685   (enum cpp_ttype);
1686 static tree cp_parser_objc_selector
1687   (cp_parser *);
1688 static tree cp_parser_objc_protocol_refs_opt
1689   (cp_parser *);
1690 static void cp_parser_objc_declaration
1691   (cp_parser *);
1692 static tree cp_parser_objc_statement
1693   (cp_parser *);
1694
1695 /* Utility Routines */
1696
1697 static tree cp_parser_lookup_name
1698   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1699 static tree cp_parser_lookup_name_simple
1700   (cp_parser *, tree);
1701 static tree cp_parser_maybe_treat_template_as_class
1702   (tree, bool);
1703 static bool cp_parser_check_declarator_template_parameters
1704   (cp_parser *, cp_declarator *);
1705 static bool cp_parser_check_template_parameters
1706   (cp_parser *, unsigned);
1707 static tree cp_parser_simple_cast_expression
1708   (cp_parser *);
1709 static tree cp_parser_global_scope_opt
1710   (cp_parser *, bool);
1711 static bool cp_parser_constructor_declarator_p
1712   (cp_parser *, bool);
1713 static tree cp_parser_function_definition_from_specifiers_and_declarator
1714   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1715 static tree cp_parser_function_definition_after_declarator
1716   (cp_parser *, bool);
1717 static void cp_parser_template_declaration_after_export
1718   (cp_parser *, bool);
1719 static tree cp_parser_single_declaration
1720   (cp_parser *, bool, bool *);
1721 static tree cp_parser_functional_cast
1722   (cp_parser *, tree);
1723 static tree cp_parser_save_member_function_body
1724   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1725 static tree cp_parser_enclosed_template_argument_list
1726   (cp_parser *);
1727 static void cp_parser_save_default_args
1728   (cp_parser *, tree);
1729 static void cp_parser_late_parsing_for_member
1730   (cp_parser *, tree);
1731 static void cp_parser_late_parsing_default_args
1732   (cp_parser *, tree);
1733 static tree cp_parser_sizeof_operand
1734   (cp_parser *, enum rid);
1735 static bool cp_parser_declares_only_class_p
1736   (cp_parser *);
1737 static void cp_parser_set_storage_class
1738   (cp_decl_specifier_seq *, cp_storage_class);
1739 static void cp_parser_set_decl_spec_type
1740   (cp_decl_specifier_seq *, tree, bool);
1741 static bool cp_parser_friend_p
1742   (const cp_decl_specifier_seq *);
1743 static cp_token *cp_parser_require
1744   (cp_parser *, enum cpp_ttype, const char *);
1745 static cp_token *cp_parser_require_keyword
1746   (cp_parser *, enum rid, const char *);
1747 static bool cp_parser_token_starts_function_definition_p
1748   (cp_token *);
1749 static bool cp_parser_next_token_starts_class_definition_p
1750   (cp_parser *);
1751 static bool cp_parser_next_token_ends_template_argument_p
1752   (cp_parser *);
1753 static bool cp_parser_nth_token_starts_template_argument_list_p
1754   (cp_parser *, size_t);
1755 static enum tag_types cp_parser_token_is_class_key
1756   (cp_token *);
1757 static void cp_parser_check_class_key
1758   (enum tag_types, tree type);
1759 static void cp_parser_check_access_in_redeclaration
1760   (tree type);
1761 static bool cp_parser_optional_template_keyword
1762   (cp_parser *);
1763 static void cp_parser_pre_parsed_nested_name_specifier
1764   (cp_parser *);
1765 static void cp_parser_cache_group
1766   (cp_parser *, enum cpp_ttype, unsigned);
1767 static void cp_parser_parse_tentatively
1768   (cp_parser *);
1769 static void cp_parser_commit_to_tentative_parse
1770   (cp_parser *);
1771 static void cp_parser_abort_tentative_parse
1772   (cp_parser *);
1773 static bool cp_parser_parse_definitely
1774   (cp_parser *);
1775 static inline bool cp_parser_parsing_tentatively
1776   (cp_parser *);
1777 static bool cp_parser_uncommitted_to_tentative_parse_p
1778   (cp_parser *);
1779 static void cp_parser_error
1780   (cp_parser *, const char *);
1781 static void cp_parser_name_lookup_error
1782   (cp_parser *, tree, tree, const char *);
1783 static bool cp_parser_simulate_error
1784   (cp_parser *);
1785 static void cp_parser_check_type_definition
1786   (cp_parser *);
1787 static void cp_parser_check_for_definition_in_return_type
1788   (cp_declarator *, tree);
1789 static void cp_parser_check_for_invalid_template_id
1790   (cp_parser *, tree);
1791 static bool cp_parser_non_integral_constant_expression
1792   (cp_parser *, const char *);
1793 static void cp_parser_diagnose_invalid_type_name
1794   (cp_parser *, tree, tree);
1795 static bool cp_parser_parse_and_diagnose_invalid_type_name
1796   (cp_parser *);
1797 static int cp_parser_skip_to_closing_parenthesis
1798   (cp_parser *, bool, bool, bool);
1799 static void cp_parser_skip_to_end_of_statement
1800   (cp_parser *);
1801 static void cp_parser_consume_semicolon_at_end_of_statement
1802   (cp_parser *);
1803 static void cp_parser_skip_to_end_of_block_or_statement
1804   (cp_parser *);
1805 static void cp_parser_skip_to_closing_brace
1806   (cp_parser *);
1807 static void cp_parser_skip_until_found
1808   (cp_parser *, enum cpp_ttype, const char *);
1809 static void cp_parser_skip_to_pragma_eol
1810   (cp_parser*, cp_token *);
1811 static bool cp_parser_error_occurred
1812   (cp_parser *);
1813 static bool cp_parser_allow_gnu_extensions_p
1814   (cp_parser *);
1815 static bool cp_parser_is_string_literal
1816   (cp_token *);
1817 static bool cp_parser_is_keyword
1818   (cp_token *, enum rid);
1819 static tree cp_parser_make_typename_type
1820   (cp_parser *, tree, tree);
1821
1822 /* Returns nonzero if we are parsing tentatively.  */
1823
1824 static inline bool
1825 cp_parser_parsing_tentatively (cp_parser* parser)
1826 {
1827   return parser->context->next != NULL;
1828 }
1829
1830 /* Returns nonzero if TOKEN is a string literal.  */
1831
1832 static bool
1833 cp_parser_is_string_literal (cp_token* token)
1834 {
1835   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1836 }
1837
1838 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1839
1840 static bool
1841 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1842 {
1843   return token->keyword == keyword;
1844 }
1845
1846 /* A minimum or maximum operator has been seen.  As these are
1847    deprecated, issue a warning.  */
1848
1849 static inline void
1850 cp_parser_warn_min_max (void)
1851 {
1852   if (warn_deprecated && !in_system_header)
1853     warning (OPT_Wdeprecated, "minimum/maximum operators are deprecated");
1854 }
1855
1856 /* If not parsing tentatively, issue a diagnostic of the form
1857       FILE:LINE: MESSAGE before TOKEN
1858    where TOKEN is the next token in the input stream.  MESSAGE
1859    (specified by the caller) is usually of the form "expected
1860    OTHER-TOKEN".  */
1861
1862 static void
1863 cp_parser_error (cp_parser* parser, const char* message)
1864 {
1865   if (!cp_parser_simulate_error (parser))
1866     {
1867       cp_token *token = cp_lexer_peek_token (parser->lexer);
1868       /* This diagnostic makes more sense if it is tagged to the line
1869          of the token we just peeked at.  */
1870       cp_lexer_set_source_position_from_token (token);
1871
1872       if (token->type == CPP_PRAGMA)
1873         {
1874           error ("%<#pragma%> is not allowed here");
1875           cp_parser_skip_to_pragma_eol (parser, token);
1876           return;
1877         }
1878
1879       c_parse_error (message,
1880                      /* Because c_parser_error does not understand
1881                         CPP_KEYWORD, keywords are treated like
1882                         identifiers.  */
1883                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1884                      token->value);
1885     }
1886 }
1887
1888 /* Issue an error about name-lookup failing.  NAME is the
1889    IDENTIFIER_NODE DECL is the result of
1890    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1891    the thing that we hoped to find.  */
1892
1893 static void
1894 cp_parser_name_lookup_error (cp_parser* parser,
1895                              tree name,
1896                              tree decl,
1897                              const char* desired)
1898 {
1899   /* If name lookup completely failed, tell the user that NAME was not
1900      declared.  */
1901   if (decl == error_mark_node)
1902     {
1903       if (parser->scope && parser->scope != global_namespace)
1904         error ("%<%D::%D%> has not been declared",
1905                parser->scope, name);
1906       else if (parser->scope == global_namespace)
1907         error ("%<::%D%> has not been declared", name);
1908       else if (parser->object_scope
1909                && !CLASS_TYPE_P (parser->object_scope))
1910         error ("request for member %qD in non-class type %qT",
1911                name, parser->object_scope);
1912       else if (parser->object_scope)
1913         error ("%<%T::%D%> has not been declared",
1914                parser->object_scope, name);
1915       else
1916         error ("%qD has not been declared", name);
1917     }
1918   else if (parser->scope && parser->scope != global_namespace)
1919     error ("%<%D::%D%> %s", parser->scope, name, desired);
1920   else if (parser->scope == global_namespace)
1921     error ("%<::%D%> %s", name, desired);
1922   else
1923     error ("%qD %s", name, desired);
1924 }
1925
1926 /* If we are parsing tentatively, remember that an error has occurred
1927    during this tentative parse.  Returns true if the error was
1928    simulated; false if a message should be issued by the caller.  */
1929
1930 static bool
1931 cp_parser_simulate_error (cp_parser* parser)
1932 {
1933   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1934     {
1935       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1936       return true;
1937     }
1938   return false;
1939 }
1940
1941 /* This function is called when a type is defined.  If type
1942    definitions are forbidden at this point, an error message is
1943    issued.  */
1944
1945 static void
1946 cp_parser_check_type_definition (cp_parser* parser)
1947 {
1948   /* If types are forbidden here, issue a message.  */
1949   if (parser->type_definition_forbidden_message)
1950     /* Use `%s' to print the string in case there are any escape
1951        characters in the message.  */
1952     error ("%s", parser->type_definition_forbidden_message);
1953 }
1954
1955 /* This function is called when the DECLARATOR is processed.  The TYPE
1956    was a type defined in the decl-specifiers.  If it is invalid to
1957    define a type in the decl-specifiers for DECLARATOR, an error is
1958    issued.  */
1959
1960 static void
1961 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1962                                                tree type)
1963 {
1964   /* [dcl.fct] forbids type definitions in return types.
1965      Unfortunately, it's not easy to know whether or not we are
1966      processing a return type until after the fact.  */
1967   while (declarator
1968          && (declarator->kind == cdk_pointer
1969              || declarator->kind == cdk_reference
1970              || declarator->kind == cdk_ptrmem))
1971     declarator = declarator->declarator;
1972   if (declarator
1973       && declarator->kind == cdk_function)
1974     {
1975       error ("new types may not be defined in a return type");
1976       inform ("(perhaps a semicolon is missing after the definition of %qT)",
1977               type);
1978     }
1979 }
1980
1981 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1982    "<" in any valid C++ program.  If the next token is indeed "<",
1983    issue a message warning the user about what appears to be an
1984    invalid attempt to form a template-id.  */
1985
1986 static void
1987 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1988                                          tree type)
1989 {
1990   cp_token_position start = 0;
1991
1992   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1993     {
1994       if (TYPE_P (type))
1995         error ("%qT is not a template", type);
1996       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1997         error ("%qE is not a template", type);
1998       else
1999         error ("invalid template-id");
2000       /* Remember the location of the invalid "<".  */
2001       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2002         start = cp_lexer_token_position (parser->lexer, true);
2003       /* Consume the "<".  */
2004       cp_lexer_consume_token (parser->lexer);
2005       /* Parse the template arguments.  */
2006       cp_parser_enclosed_template_argument_list (parser);
2007       /* Permanently remove the invalid template arguments so that
2008          this error message is not issued again.  */
2009       if (start)
2010         cp_lexer_purge_tokens_after (parser->lexer, start);
2011     }
2012 }
2013
2014 /* If parsing an integral constant-expression, issue an error message
2015    about the fact that THING appeared and return true.  Otherwise,
2016    return false.  In either case, set
2017    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2018
2019 static bool
2020 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2021                                             const char *thing)
2022 {
2023   parser->non_integral_constant_expression_p = true;
2024   if (parser->integral_constant_expression_p)
2025     {
2026       if (!parser->allow_non_integral_constant_expression_p)
2027         {
2028           error ("%s cannot appear in a constant-expression", thing);
2029           return true;
2030         }
2031     }
2032   return false;
2033 }
2034
2035 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2036    qualifying scope (or NULL, if none) for ID.  This function commits
2037    to the current active tentative parse, if any.  (Otherwise, the
2038    problematic construct might be encountered again later, resulting
2039    in duplicate error messages.)  */
2040
2041 static void
2042 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2043 {
2044   tree decl, old_scope;
2045   /* Try to lookup the identifier.  */
2046   old_scope = parser->scope;
2047   parser->scope = scope;
2048   decl = cp_parser_lookup_name_simple (parser, id);
2049   parser->scope = old_scope;
2050   /* If the lookup found a template-name, it means that the user forgot
2051   to specify an argument list. Emit a useful error message.  */
2052   if (TREE_CODE (decl) == TEMPLATE_DECL)
2053     error ("invalid use of template-name %qE without an argument list",
2054       decl);
2055   else if (!parser->scope)
2056     {
2057       /* Issue an error message.  */
2058       error ("%qE does not name a type", id);
2059       /* If we're in a template class, it's possible that the user was
2060          referring to a type from a base class.  For example:
2061
2062            template <typename T> struct A { typedef T X; };
2063            template <typename T> struct B : public A<T> { X x; };
2064
2065          The user should have said "typename A<T>::X".  */
2066       if (processing_template_decl && current_class_type
2067           && TYPE_BINFO (current_class_type))
2068         {
2069           tree b;
2070
2071           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2072                b;
2073                b = TREE_CHAIN (b))
2074             {
2075               tree base_type = BINFO_TYPE (b);
2076               if (CLASS_TYPE_P (base_type)
2077                   && dependent_type_p (base_type))
2078                 {
2079                   tree field;
2080                   /* Go from a particular instantiation of the
2081                      template (which will have an empty TYPE_FIELDs),
2082                      to the main version.  */
2083                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2084                   for (field = TYPE_FIELDS (base_type);
2085                        field;
2086                        field = TREE_CHAIN (field))
2087                     if (TREE_CODE (field) == TYPE_DECL
2088                         && DECL_NAME (field) == id)
2089                       {
2090                         inform ("(perhaps %<typename %T::%E%> was intended)",
2091                                 BINFO_TYPE (b), id);
2092                         break;
2093                       }
2094                   if (field)
2095                     break;
2096                 }
2097             }
2098         }
2099     }
2100   /* Here we diagnose qualified-ids where the scope is actually correct,
2101      but the identifier does not resolve to a valid type name.  */
2102   else if (parser->scope != error_mark_node)
2103     {
2104       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2105         error ("%qE in namespace %qE does not name a type",
2106                id, parser->scope);
2107       else if (TYPE_P (parser->scope))
2108         error ("%qE in class %qT does not name a type", id, parser->scope);
2109       else
2110         gcc_unreachable ();
2111     }
2112   cp_parser_commit_to_tentative_parse (parser);
2113 }
2114
2115 /* Check for a common situation where a type-name should be present,
2116    but is not, and issue a sensible error message.  Returns true if an
2117    invalid type-name was detected.
2118
2119    The situation handled by this function are variable declarations of the
2120    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2121    Usually, `ID' should name a type, but if we got here it means that it
2122    does not. We try to emit the best possible error message depending on
2123    how exactly the id-expression looks like.
2124 */
2125
2126 static bool
2127 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2128 {
2129   tree id;
2130
2131   cp_parser_parse_tentatively (parser);
2132   id = cp_parser_id_expression (parser,
2133                                 /*template_keyword_p=*/false,
2134                                 /*check_dependency_p=*/true,
2135                                 /*template_p=*/NULL,
2136                                 /*declarator_p=*/true);
2137   /* After the id-expression, there should be a plain identifier,
2138      otherwise this is not a simple variable declaration. Also, if
2139      the scope is dependent, we cannot do much.  */
2140   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2141       || (parser->scope && TYPE_P (parser->scope)
2142           && dependent_type_p (parser->scope)))
2143     {
2144       cp_parser_abort_tentative_parse (parser);
2145       return false;
2146     }
2147   if (!cp_parser_parse_definitely (parser)
2148       || TREE_CODE (id) != IDENTIFIER_NODE)
2149     return false;
2150
2151   /* Emit a diagnostic for the invalid type.  */
2152   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2153   /* Skip to the end of the declaration; there's no point in
2154      trying to process it.  */
2155   cp_parser_skip_to_end_of_block_or_statement (parser);
2156   return true;
2157 }
2158
2159 /* Consume tokens up to, and including, the next non-nested closing `)'.
2160    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2161    are doing error recovery. Returns -1 if OR_COMMA is true and we
2162    found an unnested comma.  */
2163
2164 static int
2165 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2166                                        bool recovering,
2167                                        bool or_comma,
2168                                        bool consume_paren)
2169 {
2170   unsigned paren_depth = 0;
2171   unsigned brace_depth = 0;
2172
2173   if (recovering && !or_comma
2174       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2175     return 0;
2176
2177   while (true)
2178     {
2179       cp_token * token = cp_lexer_peek_token (parser->lexer);
2180
2181       switch (token->type)
2182         {
2183         case CPP_EOF:
2184         case CPP_PRAGMA_EOL:
2185           /* If we've run out of tokens, then there is no closing `)'.  */
2186           return 0;
2187
2188         case CPP_SEMICOLON:
2189           /* This matches the processing in skip_to_end_of_statement.  */
2190           if (!brace_depth)
2191             return 0;
2192           break;
2193
2194         case CPP_OPEN_BRACE:
2195           ++brace_depth;
2196           break;
2197         case CPP_CLOSE_BRACE:
2198           if (!brace_depth--)
2199             return 0;
2200           break;
2201
2202         case CPP_COMMA:
2203           if (recovering && or_comma && !brace_depth && !paren_depth)
2204             return -1;
2205           break;
2206
2207         case CPP_OPEN_PAREN:
2208           if (!brace_depth)
2209             ++paren_depth;
2210           break;
2211
2212         case CPP_CLOSE_PAREN:
2213           if (!brace_depth && !paren_depth--)
2214             {
2215               if (consume_paren)
2216                 cp_lexer_consume_token (parser->lexer);
2217               return 1;
2218             }
2219           break;
2220
2221         default:
2222           break;
2223         }
2224
2225       /* Consume the token.  */
2226       cp_lexer_consume_token (parser->lexer);
2227     }
2228 }
2229
2230 /* Consume tokens until we reach the end of the current statement.
2231    Normally, that will be just before consuming a `;'.  However, if a
2232    non-nested `}' comes first, then we stop before consuming that.  */
2233
2234 static void
2235 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2236 {
2237   unsigned nesting_depth = 0;
2238
2239   while (true)
2240     {
2241       cp_token *token = cp_lexer_peek_token (parser->lexer);
2242
2243       switch (token->type)
2244         {
2245         case CPP_EOF:
2246         case CPP_PRAGMA_EOL:
2247           /* If we've run out of tokens, stop.  */
2248           return;
2249
2250         case CPP_SEMICOLON:
2251           /* If the next token is a `;', we have reached the end of the
2252              statement.  */
2253           if (!nesting_depth)
2254             return;
2255           break;
2256
2257         case CPP_CLOSE_BRACE:
2258           /* If this is a non-nested '}', stop before consuming it.
2259              That way, when confronted with something like:
2260
2261                { 3 + }
2262
2263              we stop before consuming the closing '}', even though we
2264              have not yet reached a `;'.  */
2265           if (nesting_depth == 0)
2266             return;
2267
2268           /* If it is the closing '}' for a block that we have
2269              scanned, stop -- but only after consuming the token.
2270              That way given:
2271
2272                 void f g () { ... }
2273                 typedef int I;
2274
2275              we will stop after the body of the erroneously declared
2276              function, but before consuming the following `typedef'
2277              declaration.  */
2278           if (--nesting_depth == 0)
2279             {
2280               cp_lexer_consume_token (parser->lexer);
2281               return;
2282             }
2283
2284         case CPP_OPEN_BRACE:
2285           ++nesting_depth;
2286           break;
2287
2288         default:
2289           break;
2290         }
2291
2292       /* Consume the token.  */
2293       cp_lexer_consume_token (parser->lexer);
2294     }
2295 }
2296
2297 /* This function is called at the end of a statement or declaration.
2298    If the next token is a semicolon, it is consumed; otherwise, error
2299    recovery is attempted.  */
2300
2301 static void
2302 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2303 {
2304   /* Look for the trailing `;'.  */
2305   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2306     {
2307       /* If there is additional (erroneous) input, skip to the end of
2308          the statement.  */
2309       cp_parser_skip_to_end_of_statement (parser);
2310       /* If the next token is now a `;', consume it.  */
2311       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2312         cp_lexer_consume_token (parser->lexer);
2313     }
2314 }
2315
2316 /* Skip tokens until we have consumed an entire block, or until we
2317    have consumed a non-nested `;'.  */
2318
2319 static void
2320 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2321 {
2322   int nesting_depth = 0;
2323
2324   while (nesting_depth >= 0)
2325     {
2326       cp_token *token = cp_lexer_peek_token (parser->lexer);
2327
2328       switch (token->type)
2329         {
2330         case CPP_EOF:
2331         case CPP_PRAGMA_EOL:
2332           /* If we've run out of tokens, stop.  */
2333           return;
2334
2335         case CPP_SEMICOLON:
2336           /* Stop if this is an unnested ';'. */
2337           if (!nesting_depth)
2338             nesting_depth = -1;
2339           break;
2340
2341         case CPP_CLOSE_BRACE:
2342           /* Stop if this is an unnested '}', or closes the outermost
2343              nesting level.  */
2344           nesting_depth--;
2345           if (!nesting_depth)
2346             nesting_depth = -1;
2347           break;
2348
2349         case CPP_OPEN_BRACE:
2350           /* Nest. */
2351           nesting_depth++;
2352           break;
2353
2354         default:
2355           break;
2356         }
2357
2358       /* Consume the token.  */
2359       cp_lexer_consume_token (parser->lexer);
2360     }
2361 }
2362
2363 /* Skip tokens until a non-nested closing curly brace is the next
2364    token.  */
2365
2366 static void
2367 cp_parser_skip_to_closing_brace (cp_parser *parser)
2368 {
2369   unsigned nesting_depth = 0;
2370
2371   while (true)
2372     {
2373       cp_token *token = cp_lexer_peek_token (parser->lexer);
2374
2375       switch (token->type)
2376         {
2377         case CPP_EOF:
2378         case CPP_PRAGMA_EOL:
2379           /* If we've run out of tokens, stop.  */
2380           return;
2381
2382         case CPP_CLOSE_BRACE:
2383           /* If the next token is a non-nested `}', then we have reached
2384              the end of the current block.  */
2385           if (nesting_depth-- == 0)
2386             return;
2387           break;
2388
2389         case CPP_OPEN_BRACE:
2390           /* If it the next token is a `{', then we are entering a new
2391              block.  Consume the entire block.  */
2392           ++nesting_depth;
2393           break;
2394
2395         default:
2396           break;
2397         }
2398
2399       /* Consume the token.  */
2400       cp_lexer_consume_token (parser->lexer);
2401     }
2402 }
2403
2404 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2405    parameter is the PRAGMA token, allowing us to purge the entire pragma
2406    sequence.  */
2407
2408 static void
2409 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2410 {
2411   cp_token *token;
2412
2413   parser->lexer->in_pragma = false;
2414
2415   do
2416     token = cp_lexer_consume_token (parser->lexer);
2417   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2418
2419   /* Ensure that the pragma is not parsed again.  */
2420   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2421 }
2422
2423 /* This is a simple wrapper around make_typename_type. When the id is
2424    an unresolved identifier node, we can provide a superior diagnostic
2425    using cp_parser_diagnose_invalid_type_name.  */
2426
2427 static tree
2428 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2429 {
2430   tree result;
2431   if (TREE_CODE (id) == IDENTIFIER_NODE)
2432     {
2433       result = make_typename_type (scope, id, typename_type,
2434                                    /*complain=*/tf_none);
2435       if (result == error_mark_node)
2436         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2437       return result;
2438     }
2439   return make_typename_type (scope, id, typename_type, tf_error);
2440 }
2441
2442
2443 /* Create a new C++ parser.  */
2444
2445 static cp_parser *
2446 cp_parser_new (void)
2447 {
2448   cp_parser *parser;
2449   cp_lexer *lexer;
2450   unsigned i;
2451
2452   /* cp_lexer_new_main is called before calling ggc_alloc because
2453      cp_lexer_new_main might load a PCH file.  */
2454   lexer = cp_lexer_new_main ();
2455
2456   /* Initialize the binops_by_token so that we can get the tree
2457      directly from the token.  */
2458   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2459     binops_by_token[binops[i].token_type] = binops[i];
2460
2461   parser = GGC_CNEW (cp_parser);
2462   parser->lexer = lexer;
2463   parser->context = cp_parser_context_new (NULL);
2464
2465   /* For now, we always accept GNU extensions.  */
2466   parser->allow_gnu_extensions_p = 1;
2467
2468   /* The `>' token is a greater-than operator, not the end of a
2469      template-id.  */
2470   parser->greater_than_is_operator_p = true;
2471
2472   parser->default_arg_ok_p = true;
2473
2474   /* We are not parsing a constant-expression.  */
2475   parser->integral_constant_expression_p = false;
2476   parser->allow_non_integral_constant_expression_p = false;
2477   parser->non_integral_constant_expression_p = false;
2478
2479   /* Local variable names are not forbidden.  */
2480   parser->local_variables_forbidden_p = false;
2481
2482   /* We are not processing an `extern "C"' declaration.  */
2483   parser->in_unbraced_linkage_specification_p = false;
2484
2485   /* We are not processing a declarator.  */
2486   parser->in_declarator_p = false;
2487
2488   /* We are not processing a template-argument-list.  */
2489   parser->in_template_argument_list_p = false;
2490
2491   /* We are not in an iteration statement.  */
2492   parser->in_iteration_statement_p = false;
2493
2494   /* We are not in a switch statement.  */
2495   parser->in_switch_statement_p = false;
2496
2497   /* We are not parsing a type-id inside an expression.  */
2498   parser->in_type_id_in_expr_p = false;
2499
2500   /* Declarations aren't implicitly extern "C".  */
2501   parser->implicit_extern_c = false;
2502
2503   /* String literals should be translated to the execution character set.  */
2504   parser->translate_strings_p = true;
2505
2506   /* The unparsed function queue is empty.  */
2507   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2508
2509   /* There are no classes being defined.  */
2510   parser->num_classes_being_defined = 0;
2511
2512   /* No template parameters apply.  */
2513   parser->num_template_parameter_lists = 0;
2514
2515   return parser;
2516 }
2517
2518 /* Create a cp_lexer structure which will emit the tokens in CACHE
2519    and push it onto the parser's lexer stack.  This is used for delayed
2520    parsing of in-class method bodies and default arguments, and should
2521    not be confused with tentative parsing.  */
2522 static void
2523 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2524 {
2525   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2526   lexer->next = parser->lexer;
2527   parser->lexer = lexer;
2528
2529   /* Move the current source position to that of the first token in the
2530      new lexer.  */
2531   cp_lexer_set_source_position_from_token (lexer->next_token);
2532 }
2533
2534 /* Pop the top lexer off the parser stack.  This is never used for the
2535    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2536 static void
2537 cp_parser_pop_lexer (cp_parser *parser)
2538 {
2539   cp_lexer *lexer = parser->lexer;
2540   parser->lexer = lexer->next;
2541   cp_lexer_destroy (lexer);
2542
2543   /* Put the current source position back where it was before this
2544      lexer was pushed.  */
2545   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2546 }
2547
2548 /* Lexical conventions [gram.lex]  */
2549
2550 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2551    identifier.  */
2552
2553 static tree
2554 cp_parser_identifier (cp_parser* parser)
2555 {
2556   cp_token *token;
2557
2558   /* Look for the identifier.  */
2559   token = cp_parser_require (parser, CPP_NAME, "identifier");
2560   /* Return the value.  */
2561   return token ? token->value : error_mark_node;
2562 }
2563
2564 /* Parse a sequence of adjacent string constants.  Returns a
2565    TREE_STRING representing the combined, nul-terminated string
2566    constant.  If TRANSLATE is true, translate the string to the
2567    execution character set.  If WIDE_OK is true, a wide string is
2568    invalid here.
2569
2570    C++98 [lex.string] says that if a narrow string literal token is
2571    adjacent to a wide string literal token, the behavior is undefined.
2572    However, C99 6.4.5p4 says that this results in a wide string literal.
2573    We follow C99 here, for consistency with the C front end.
2574
2575    This code is largely lifted from lex_string() in c-lex.c.
2576
2577    FUTURE: ObjC++ will need to handle @-strings here.  */
2578 static tree
2579 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2580 {
2581   tree value;
2582   bool wide = false;
2583   size_t count;
2584   struct obstack str_ob;
2585   cpp_string str, istr, *strs;
2586   cp_token *tok;
2587
2588   tok = cp_lexer_peek_token (parser->lexer);
2589   if (!cp_parser_is_string_literal (tok))
2590     {
2591       cp_parser_error (parser, "expected string-literal");
2592       return error_mark_node;
2593     }
2594
2595   /* Try to avoid the overhead of creating and destroying an obstack
2596      for the common case of just one string.  */
2597   if (!cp_parser_is_string_literal
2598       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2599     {
2600       cp_lexer_consume_token (parser->lexer);
2601
2602       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2603       str.len = TREE_STRING_LENGTH (tok->value);
2604       count = 1;
2605       if (tok->type == CPP_WSTRING)
2606         wide = true;
2607
2608       strs = &str;
2609     }
2610   else
2611     {
2612       gcc_obstack_init (&str_ob);
2613       count = 0;
2614
2615       do
2616         {
2617           cp_lexer_consume_token (parser->lexer);
2618           count++;
2619           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2620           str.len = TREE_STRING_LENGTH (tok->value);
2621           if (tok->type == CPP_WSTRING)
2622             wide = true;
2623
2624           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2625
2626           tok = cp_lexer_peek_token (parser->lexer);
2627         }
2628       while (cp_parser_is_string_literal (tok));
2629
2630       strs = (cpp_string *) obstack_finish (&str_ob);
2631     }
2632
2633   if (wide && !wide_ok)
2634     {
2635       cp_parser_error (parser, "a wide string is invalid in this context");
2636       wide = false;
2637     }
2638
2639   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2640       (parse_in, strs, count, &istr, wide))
2641     {
2642       value = build_string (istr.len, (char *)istr.text);
2643       free ((void *)istr.text);
2644
2645       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2646       value = fix_string_type (value);
2647     }
2648   else
2649     /* cpp_interpret_string has issued an error.  */
2650     value = error_mark_node;
2651
2652   if (count > 1)
2653     obstack_free (&str_ob, 0);
2654
2655   return value;
2656 }
2657
2658
2659 /* Basic concepts [gram.basic]  */
2660
2661 /* Parse a translation-unit.
2662
2663    translation-unit:
2664      declaration-seq [opt]
2665
2666    Returns TRUE if all went well.  */
2667
2668 static bool
2669 cp_parser_translation_unit (cp_parser* parser)
2670 {
2671   /* The address of the first non-permanent object on the declarator
2672      obstack.  */
2673   static void *declarator_obstack_base;
2674
2675   bool success;
2676
2677   /* Create the declarator obstack, if necessary.  */
2678   if (!cp_error_declarator)
2679     {
2680       gcc_obstack_init (&declarator_obstack);
2681       /* Create the error declarator.  */
2682       cp_error_declarator = make_declarator (cdk_error);
2683       /* Create the empty parameter list.  */
2684       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2685       /* Remember where the base of the declarator obstack lies.  */
2686       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2687     }
2688
2689   cp_parser_declaration_seq_opt (parser);
2690   
2691   /* If there are no tokens left then all went well.  */
2692   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2693     {
2694       /* Get rid of the token array; we don't need it any more.  */
2695       cp_lexer_destroy (parser->lexer);
2696       parser->lexer = NULL;
2697       
2698       /* This file might have been a context that's implicitly extern
2699          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2700       if (parser->implicit_extern_c)
2701         {
2702           pop_lang_context ();
2703           parser->implicit_extern_c = false;
2704         }
2705       
2706       /* Finish up.  */
2707       finish_translation_unit ();
2708       
2709       success = true;
2710     }
2711   else
2712     {
2713       cp_parser_error (parser, "expected declaration");
2714       success = false;
2715     }
2716   
2717   /* Make sure the declarator obstack was fully cleaned up.  */
2718   gcc_assert (obstack_next_free (&declarator_obstack)
2719               == declarator_obstack_base);
2720
2721   /* All went well.  */
2722   return success;
2723 }
2724
2725 /* Expressions [gram.expr] */
2726
2727 /* Parse a primary-expression.
2728
2729    primary-expression:
2730      literal
2731      this
2732      ( expression )
2733      id-expression
2734
2735    GNU Extensions:
2736
2737    primary-expression:
2738      ( compound-statement )
2739      __builtin_va_arg ( assignment-expression , type-id )
2740      __builtin_offsetof ( type-id , offsetof-expression )
2741
2742    Objective-C++ Extension:
2743
2744    primary-expression:
2745      objc-expression
2746
2747    literal:
2748      __null
2749
2750    ADDRESS_P is true iff this expression was immediately preceded by
2751    "&" and therefore might denote a pointer-to-member.  CAST_P is true
2752    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
2753    true iff this expression is a template argument.
2754
2755    Returns a representation of the expression.  Upon return, *IDK
2756    indicates what kind of id-expression (if any) was present.  */
2757
2758 static tree
2759 cp_parser_primary_expression (cp_parser *parser,
2760                               bool address_p,
2761                               bool cast_p,
2762                               bool template_arg_p,
2763                               cp_id_kind *idk)
2764 {
2765   cp_token *token;
2766
2767   /* Assume the primary expression is not an id-expression.  */
2768   *idk = CP_ID_KIND_NONE;
2769
2770   /* Peek at the next token.  */
2771   token = cp_lexer_peek_token (parser->lexer);
2772   switch (token->type)
2773     {
2774       /* literal:
2775            integer-literal
2776            character-literal
2777            floating-literal
2778            string-literal
2779            boolean-literal  */
2780     case CPP_CHAR:
2781     case CPP_WCHAR:
2782     case CPP_NUMBER:
2783       token = cp_lexer_consume_token (parser->lexer);
2784       /* Floating-point literals are only allowed in an integral
2785          constant expression if they are cast to an integral or
2786          enumeration type.  */
2787       if (TREE_CODE (token->value) == REAL_CST
2788           && parser->integral_constant_expression_p
2789           && pedantic)
2790         {
2791           /* CAST_P will be set even in invalid code like "int(2.7 +
2792              ...)".   Therefore, we have to check that the next token
2793              is sure to end the cast.  */
2794           if (cast_p)
2795             {
2796               cp_token *next_token;
2797
2798               next_token = cp_lexer_peek_token (parser->lexer);
2799               if (/* The comma at the end of an
2800                      enumerator-definition.  */
2801                   next_token->type != CPP_COMMA
2802                   /* The curly brace at the end of an enum-specifier.  */
2803                   && next_token->type != CPP_CLOSE_BRACE
2804                   /* The end of a statement.  */
2805                   && next_token->type != CPP_SEMICOLON
2806                   /* The end of the cast-expression.  */
2807                   && next_token->type != CPP_CLOSE_PAREN
2808                   /* The end of an array bound.  */
2809                   && next_token->type != CPP_CLOSE_SQUARE
2810                   /* The closing ">" in a template-argument-list.  */
2811                   && (next_token->type != CPP_GREATER
2812                       || parser->greater_than_is_operator_p))
2813                 cast_p = false;
2814             }
2815
2816           /* If we are within a cast, then the constraint that the
2817              cast is to an integral or enumeration type will be
2818              checked at that point.  If we are not within a cast, then
2819              this code is invalid.  */
2820           if (!cast_p)
2821             cp_parser_non_integral_constant_expression
2822               (parser, "floating-point literal");
2823         }
2824       return token->value;
2825
2826     case CPP_STRING:
2827     case CPP_WSTRING:
2828       /* ??? Should wide strings be allowed when parser->translate_strings_p
2829          is false (i.e. in attributes)?  If not, we can kill the third
2830          argument to cp_parser_string_literal.  */
2831       return cp_parser_string_literal (parser,
2832                                        parser->translate_strings_p,
2833                                        true);
2834
2835     case CPP_OPEN_PAREN:
2836       {
2837         tree expr;
2838         bool saved_greater_than_is_operator_p;
2839
2840         /* Consume the `('.  */
2841         cp_lexer_consume_token (parser->lexer);
2842         /* Within a parenthesized expression, a `>' token is always
2843            the greater-than operator.  */
2844         saved_greater_than_is_operator_p
2845           = parser->greater_than_is_operator_p;
2846         parser->greater_than_is_operator_p = true;
2847         /* If we see `( { ' then we are looking at the beginning of
2848            a GNU statement-expression.  */
2849         if (cp_parser_allow_gnu_extensions_p (parser)
2850             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2851           {
2852             /* Statement-expressions are not allowed by the standard.  */
2853             if (pedantic)
2854               pedwarn ("ISO C++ forbids braced-groups within expressions");
2855
2856             /* And they're not allowed outside of a function-body; you
2857                cannot, for example, write:
2858
2859                  int i = ({ int j = 3; j + 1; });
2860
2861                at class or namespace scope.  */
2862             if (!at_function_scope_p ())
2863               error ("statement-expressions are allowed only inside functions");
2864             /* Start the statement-expression.  */
2865             expr = begin_stmt_expr ();
2866             /* Parse the compound-statement.  */
2867             cp_parser_compound_statement (parser, expr, false);
2868             /* Finish up.  */
2869             expr = finish_stmt_expr (expr, false);
2870           }
2871         else
2872           {
2873             /* Parse the parenthesized expression.  */
2874             expr = cp_parser_expression (parser, cast_p);
2875             /* Let the front end know that this expression was
2876                enclosed in parentheses. This matters in case, for
2877                example, the expression is of the form `A::B', since
2878                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2879                not.  */
2880             finish_parenthesized_expr (expr);
2881           }
2882         /* The `>' token might be the end of a template-id or
2883            template-parameter-list now.  */
2884         parser->greater_than_is_operator_p
2885           = saved_greater_than_is_operator_p;
2886         /* Consume the `)'.  */
2887         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2888           cp_parser_skip_to_end_of_statement (parser);
2889
2890         return expr;
2891       }
2892
2893     case CPP_KEYWORD:
2894       switch (token->keyword)
2895         {
2896           /* These two are the boolean literals.  */
2897         case RID_TRUE:
2898           cp_lexer_consume_token (parser->lexer);
2899           return boolean_true_node;
2900         case RID_FALSE:
2901           cp_lexer_consume_token (parser->lexer);
2902           return boolean_false_node;
2903
2904           /* The `__null' literal.  */
2905         case RID_NULL:
2906           cp_lexer_consume_token (parser->lexer);
2907           return null_node;
2908
2909           /* Recognize the `this' keyword.  */
2910         case RID_THIS:
2911           cp_lexer_consume_token (parser->lexer);
2912           if (parser->local_variables_forbidden_p)
2913             {
2914               error ("%<this%> may not be used in this context");
2915               return error_mark_node;
2916             }
2917           /* Pointers cannot appear in constant-expressions.  */
2918           if (cp_parser_non_integral_constant_expression (parser,
2919                                                           "`this'"))
2920             return error_mark_node;
2921           return finish_this_expr ();
2922
2923           /* The `operator' keyword can be the beginning of an
2924              id-expression.  */
2925         case RID_OPERATOR:
2926           goto id_expression;
2927
2928         case RID_FUNCTION_NAME:
2929         case RID_PRETTY_FUNCTION_NAME:
2930         case RID_C99_FUNCTION_NAME:
2931           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2932              __func__ are the names of variables -- but they are
2933              treated specially.  Therefore, they are handled here,
2934              rather than relying on the generic id-expression logic
2935              below.  Grammatically, these names are id-expressions.
2936
2937              Consume the token.  */
2938           token = cp_lexer_consume_token (parser->lexer);
2939           /* Look up the name.  */
2940           return finish_fname (token->value);
2941
2942         case RID_VA_ARG:
2943           {
2944             tree expression;
2945             tree type;
2946
2947             /* The `__builtin_va_arg' construct is used to handle
2948                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2949             cp_lexer_consume_token (parser->lexer);
2950             /* Look for the opening `('.  */
2951             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2952             /* Now, parse the assignment-expression.  */
2953             expression = cp_parser_assignment_expression (parser,
2954                                                           /*cast_p=*/false);
2955             /* Look for the `,'.  */
2956             cp_parser_require (parser, CPP_COMMA, "`,'");
2957             /* Parse the type-id.  */
2958             type = cp_parser_type_id (parser);
2959             /* Look for the closing `)'.  */
2960             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2961             /* Using `va_arg' in a constant-expression is not
2962                allowed.  */
2963             if (cp_parser_non_integral_constant_expression (parser,
2964                                                             "`va_arg'"))
2965               return error_mark_node;
2966             return build_x_va_arg (expression, type);
2967           }
2968
2969         case RID_OFFSETOF:
2970           return cp_parser_builtin_offsetof (parser);
2971
2972           /* Objective-C++ expressions.  */
2973         case RID_AT_ENCODE:
2974         case RID_AT_PROTOCOL:
2975         case RID_AT_SELECTOR:
2976           return cp_parser_objc_expression (parser);
2977
2978         default:
2979           cp_parser_error (parser, "expected primary-expression");
2980           return error_mark_node;
2981         }
2982
2983       /* An id-expression can start with either an identifier, a
2984          `::' as the beginning of a qualified-id, or the "operator"
2985          keyword.  */
2986     case CPP_NAME:
2987     case CPP_SCOPE:
2988     case CPP_TEMPLATE_ID:
2989     case CPP_NESTED_NAME_SPECIFIER:
2990       {
2991         tree id_expression;
2992         tree decl;
2993         const char *error_msg;
2994         bool template_p;
2995         bool done;
2996
2997       id_expression:
2998         /* Parse the id-expression.  */
2999         id_expression
3000           = cp_parser_id_expression (parser,
3001                                      /*template_keyword_p=*/false,
3002                                      /*check_dependency_p=*/true,
3003                                      &template_p,
3004                                      /*declarator_p=*/false);
3005         if (id_expression == error_mark_node)
3006           return error_mark_node;
3007         token = cp_lexer_peek_token (parser->lexer);
3008         done = (token->type != CPP_OPEN_SQUARE
3009                 && token->type != CPP_OPEN_PAREN
3010                 && token->type != CPP_DOT
3011                 && token->type != CPP_DEREF
3012                 && token->type != CPP_PLUS_PLUS
3013                 && token->type != CPP_MINUS_MINUS);
3014         /* If we have a template-id, then no further lookup is
3015            required.  If the template-id was for a template-class, we
3016            will sometimes have a TYPE_DECL at this point.  */
3017         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3018                  || TREE_CODE (id_expression) == TYPE_DECL)
3019           decl = id_expression;
3020         /* Look up the name.  */
3021         else
3022           {
3023             tree ambiguous_decls;
3024
3025             decl = cp_parser_lookup_name (parser, id_expression,
3026                                           none_type,
3027                                           template_p,
3028                                           /*is_namespace=*/false,
3029                                           /*check_dependency=*/true,
3030                                           &ambiguous_decls);
3031             /* If the lookup was ambiguous, an error will already have
3032                been issued.  */
3033             if (ambiguous_decls)
3034               return error_mark_node;
3035
3036             /* In Objective-C++, an instance variable (ivar) may be preferred
3037                to whatever cp_parser_lookup_name() found.  */
3038             decl = objc_lookup_ivar (decl, id_expression);
3039
3040             /* If name lookup gives us a SCOPE_REF, then the
3041                qualifying scope was dependent.  */
3042             if (TREE_CODE (decl) == SCOPE_REF)
3043               return decl;
3044             /* Check to see if DECL is a local variable in a context
3045                where that is forbidden.  */
3046             if (parser->local_variables_forbidden_p
3047                 && local_variable_p (decl))
3048               {
3049                 /* It might be that we only found DECL because we are
3050                    trying to be generous with pre-ISO scoping rules.
3051                    For example, consider:
3052
3053                      int i;
3054                      void g() {
3055                        for (int i = 0; i < 10; ++i) {}
3056                        extern void f(int j = i);
3057                      }
3058
3059                    Here, name look up will originally find the out
3060                    of scope `i'.  We need to issue a warning message,
3061                    but then use the global `i'.  */
3062                 decl = check_for_out_of_scope_variable (decl);
3063                 if (local_variable_p (decl))
3064                   {
3065                     error ("local variable %qD may not appear in this context",
3066                            decl);
3067                     return error_mark_node;
3068                   }
3069               }
3070           }
3071
3072         decl = (finish_id_expression 
3073                 (id_expression, decl, parser->scope,
3074                  idk,
3075                  parser->integral_constant_expression_p,
3076                  parser->allow_non_integral_constant_expression_p,
3077                  &parser->non_integral_constant_expression_p,
3078                  template_p, done, address_p,
3079                  template_arg_p,
3080                  &error_msg));
3081         if (error_msg)
3082           cp_parser_error (parser, error_msg);
3083         return decl;
3084       }
3085
3086       /* Anything else is an error.  */
3087     default:
3088       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3089       if (c_dialect_objc ()
3090           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3091         return cp_parser_objc_expression (parser);
3092
3093       cp_parser_error (parser, "expected primary-expression");
3094       return error_mark_node;
3095     }
3096 }
3097
3098 /* Parse an id-expression.
3099
3100    id-expression:
3101      unqualified-id
3102      qualified-id
3103
3104    qualified-id:
3105      :: [opt] nested-name-specifier template [opt] unqualified-id
3106      :: identifier
3107      :: operator-function-id
3108      :: template-id
3109
3110    Return a representation of the unqualified portion of the
3111    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3112    a `::' or nested-name-specifier.
3113
3114    Often, if the id-expression was a qualified-id, the caller will
3115    want to make a SCOPE_REF to represent the qualified-id.  This
3116    function does not do this in order to avoid wastefully creating
3117    SCOPE_REFs when they are not required.
3118
3119    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3120    `template' keyword.
3121
3122    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3123    uninstantiated templates.
3124
3125    If *TEMPLATE_P is non-NULL, it is set to true iff the
3126    `template' keyword is used to explicitly indicate that the entity
3127    named is a template.
3128
3129    If DECLARATOR_P is true, the id-expression is appearing as part of
3130    a declarator, rather than as part of an expression.  */
3131
3132 static tree
3133 cp_parser_id_expression (cp_parser *parser,
3134                          bool template_keyword_p,
3135                          bool check_dependency_p,
3136                          bool *template_p,
3137                          bool declarator_p)
3138 {
3139   bool global_scope_p;
3140   bool nested_name_specifier_p;
3141
3142   /* Assume the `template' keyword was not used.  */
3143   if (template_p)
3144     *template_p = template_keyword_p;
3145
3146   /* Look for the optional `::' operator.  */
3147   global_scope_p
3148     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3149        != NULL_TREE);
3150   /* Look for the optional nested-name-specifier.  */
3151   nested_name_specifier_p
3152     = (cp_parser_nested_name_specifier_opt (parser,
3153                                             /*typename_keyword_p=*/false,
3154                                             check_dependency_p,
3155                                             /*type_p=*/false,
3156                                             declarator_p)
3157        != NULL_TREE);
3158   /* If there is a nested-name-specifier, then we are looking at
3159      the first qualified-id production.  */
3160   if (nested_name_specifier_p)
3161     {
3162       tree saved_scope;
3163       tree saved_object_scope;
3164       tree saved_qualifying_scope;
3165       tree unqualified_id;
3166       bool is_template;
3167
3168       /* See if the next token is the `template' keyword.  */
3169       if (!template_p)
3170         template_p = &is_template;
3171       *template_p = cp_parser_optional_template_keyword (parser);
3172       /* Name lookup we do during the processing of the
3173          unqualified-id might obliterate SCOPE.  */
3174       saved_scope = parser->scope;
3175       saved_object_scope = parser->object_scope;
3176       saved_qualifying_scope = parser->qualifying_scope;
3177       /* Process the final unqualified-id.  */
3178       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3179                                                  check_dependency_p,
3180                                                  declarator_p);
3181       /* Restore the SAVED_SCOPE for our caller.  */
3182       parser->scope = saved_scope;
3183       parser->object_scope = saved_object_scope;
3184       parser->qualifying_scope = saved_qualifying_scope;
3185
3186       return unqualified_id;
3187     }
3188   /* Otherwise, if we are in global scope, then we are looking at one
3189      of the other qualified-id productions.  */
3190   else if (global_scope_p)
3191     {
3192       cp_token *token;
3193       tree id;
3194
3195       /* Peek at the next token.  */
3196       token = cp_lexer_peek_token (parser->lexer);
3197
3198       /* If it's an identifier, and the next token is not a "<", then
3199          we can avoid the template-id case.  This is an optimization
3200          for this common case.  */
3201       if (token->type == CPP_NAME
3202           && !cp_parser_nth_token_starts_template_argument_list_p
3203                (parser, 2))
3204         return cp_parser_identifier (parser);
3205
3206       cp_parser_parse_tentatively (parser);
3207       /* Try a template-id.  */
3208       id = cp_parser_template_id (parser,
3209                                   /*template_keyword_p=*/false,
3210                                   /*check_dependency_p=*/true,
3211                                   declarator_p);
3212       /* If that worked, we're done.  */
3213       if (cp_parser_parse_definitely (parser))
3214         return id;
3215
3216       /* Peek at the next token.  (Changes in the token buffer may
3217          have invalidated the pointer obtained above.)  */
3218       token = cp_lexer_peek_token (parser->lexer);
3219
3220       switch (token->type)
3221         {
3222         case CPP_NAME:
3223           return cp_parser_identifier (parser);
3224
3225         case CPP_KEYWORD:
3226           if (token->keyword == RID_OPERATOR)
3227             return cp_parser_operator_function_id (parser);
3228           /* Fall through.  */
3229
3230         default:
3231           cp_parser_error (parser, "expected id-expression");
3232           return error_mark_node;
3233         }
3234     }
3235   else
3236     return cp_parser_unqualified_id (parser, template_keyword_p,
3237                                      /*check_dependency_p=*/true,
3238                                      declarator_p);
3239 }
3240
3241 /* Parse an unqualified-id.
3242
3243    unqualified-id:
3244      identifier
3245      operator-function-id
3246      conversion-function-id
3247      ~ class-name
3248      template-id
3249
3250    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3251    keyword, in a construct like `A::template ...'.
3252
3253    Returns a representation of unqualified-id.  For the `identifier'
3254    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3255    production a BIT_NOT_EXPR is returned; the operand of the
3256    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3257    other productions, see the documentation accompanying the
3258    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3259    names are looked up in uninstantiated templates.  If DECLARATOR_P
3260    is true, the unqualified-id is appearing as part of a declarator,
3261    rather than as part of an expression.  */
3262
3263 static tree
3264 cp_parser_unqualified_id (cp_parser* parser,
3265                           bool template_keyword_p,
3266                           bool check_dependency_p,
3267                           bool declarator_p)
3268 {
3269   cp_token *token;
3270
3271   /* Peek at the next token.  */
3272   token = cp_lexer_peek_token (parser->lexer);
3273
3274   switch (token->type)
3275     {
3276     case CPP_NAME:
3277       {
3278         tree id;
3279
3280         /* We don't know yet whether or not this will be a
3281            template-id.  */
3282         cp_parser_parse_tentatively (parser);
3283         /* Try a template-id.  */
3284         id = cp_parser_template_id (parser, template_keyword_p,
3285                                     check_dependency_p,
3286                                     declarator_p);
3287         /* If it worked, we're done.  */
3288         if (cp_parser_parse_definitely (parser))
3289           return id;
3290         /* Otherwise, it's an ordinary identifier.  */
3291         return cp_parser_identifier (parser);
3292       }
3293
3294     case CPP_TEMPLATE_ID:
3295       return cp_parser_template_id (parser, template_keyword_p,
3296                                     check_dependency_p,
3297                                     declarator_p);
3298
3299     case CPP_COMPL:
3300       {
3301         tree type_decl;
3302         tree qualifying_scope;
3303         tree object_scope;
3304         tree scope;
3305         bool done;
3306
3307         /* Consume the `~' token.  */
3308         cp_lexer_consume_token (parser->lexer);
3309         /* Parse the class-name.  The standard, as written, seems to
3310            say that:
3311
3312              template <typename T> struct S { ~S (); };
3313              template <typename T> S<T>::~S() {}
3314
3315            is invalid, since `~' must be followed by a class-name, but
3316            `S<T>' is dependent, and so not known to be a class.
3317            That's not right; we need to look in uninstantiated
3318            templates.  A further complication arises from:
3319
3320              template <typename T> void f(T t) {
3321                t.T::~T();
3322              }
3323
3324            Here, it is not possible to look up `T' in the scope of `T'
3325            itself.  We must look in both the current scope, and the
3326            scope of the containing complete expression.
3327
3328            Yet another issue is:
3329
3330              struct S {
3331                int S;
3332                ~S();
3333              };
3334
3335              S::~S() {}
3336
3337            The standard does not seem to say that the `S' in `~S'
3338            should refer to the type `S' and not the data member
3339            `S::S'.  */
3340
3341         /* DR 244 says that we look up the name after the "~" in the
3342            same scope as we looked up the qualifying name.  That idea
3343            isn't fully worked out; it's more complicated than that.  */
3344         scope = parser->scope;
3345         object_scope = parser->object_scope;
3346         qualifying_scope = parser->qualifying_scope;
3347
3348         /* If the name is of the form "X::~X" it's OK.  */
3349         if (scope && TYPE_P (scope)
3350             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3351             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3352                 == CPP_OPEN_PAREN)
3353             && (cp_lexer_peek_token (parser->lexer)->value
3354                 == TYPE_IDENTIFIER (scope)))
3355           {
3356             cp_lexer_consume_token (parser->lexer);
3357             return build_nt (BIT_NOT_EXPR, scope);
3358           }
3359
3360         /* If there was an explicit qualification (S::~T), first look
3361            in the scope given by the qualification (i.e., S).  */
3362         done = false;
3363         type_decl = NULL_TREE;
3364         if (scope)
3365           {
3366             cp_parser_parse_tentatively (parser);
3367             type_decl = cp_parser_class_name (parser,
3368                                               /*typename_keyword_p=*/false,
3369                                               /*template_keyword_p=*/false,
3370                                               none_type,
3371                                               /*check_dependency=*/false,
3372                                               /*class_head_p=*/false,
3373                                               declarator_p);
3374             if (cp_parser_parse_definitely (parser))
3375               done = true;
3376           }
3377         /* In "N::S::~S", look in "N" as well.  */
3378         if (!done && scope && qualifying_scope)
3379           {
3380             cp_parser_parse_tentatively (parser);
3381             parser->scope = qualifying_scope;
3382             parser->object_scope = NULL_TREE;
3383             parser->qualifying_scope = NULL_TREE;
3384             type_decl
3385               = cp_parser_class_name (parser,
3386                                       /*typename_keyword_p=*/false,
3387                                       /*template_keyword_p=*/false,
3388                                       none_type,
3389                                       /*check_dependency=*/false,
3390                                       /*class_head_p=*/false,
3391                                       declarator_p);
3392             if (cp_parser_parse_definitely (parser))
3393               done = true;
3394           }
3395         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3396         else if (!done && object_scope)
3397           {
3398             cp_parser_parse_tentatively (parser);
3399             parser->scope = object_scope;
3400             parser->object_scope = NULL_TREE;
3401             parser->qualifying_scope = NULL_TREE;
3402             type_decl
3403               = cp_parser_class_name (parser,
3404                                       /*typename_keyword_p=*/false,
3405                                       /*template_keyword_p=*/false,
3406                                       none_type,
3407                                       /*check_dependency=*/false,
3408                                       /*class_head_p=*/false,
3409                                       declarator_p);
3410             if (cp_parser_parse_definitely (parser))
3411               done = true;
3412           }
3413         /* Look in the surrounding context.  */
3414         if (!done)
3415           {
3416             parser->scope = NULL_TREE;
3417             parser->object_scope = NULL_TREE;
3418             parser->qualifying_scope = NULL_TREE;
3419             type_decl
3420               = cp_parser_class_name (parser,
3421                                       /*typename_keyword_p=*/false,
3422                                       /*template_keyword_p=*/false,
3423                                       none_type,
3424                                       /*check_dependency=*/false,
3425                                       /*class_head_p=*/false,
3426                                       declarator_p);
3427           }
3428         /* If an error occurred, assume that the name of the
3429            destructor is the same as the name of the qualifying
3430            class.  That allows us to keep parsing after running
3431            into ill-formed destructor names.  */
3432         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3433           return build_nt (BIT_NOT_EXPR, scope);
3434         else if (type_decl == error_mark_node)
3435           return error_mark_node;
3436
3437         /* Check that destructor name and scope match.  */
3438         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3439           {
3440             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3441               error ("declaration of %<~%T%> as member of %qT",
3442                      type_decl, scope);
3443             return error_mark_node;
3444           }
3445
3446         /* [class.dtor]
3447
3448            A typedef-name that names a class shall not be used as the
3449            identifier in the declarator for a destructor declaration.  */
3450         if (declarator_p
3451             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3452             && !DECL_SELF_REFERENCE_P (type_decl)
3453             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3454           error ("typedef-name %qD used as destructor declarator",
3455                  type_decl);
3456
3457         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3458       }
3459
3460     case CPP_KEYWORD:
3461       if (token->keyword == RID_OPERATOR)
3462         {
3463           tree id;
3464
3465           /* This could be a template-id, so we try that first.  */
3466           cp_parser_parse_tentatively (parser);
3467           /* Try a template-id.  */
3468           id = cp_parser_template_id (parser, template_keyword_p,
3469                                       /*check_dependency_p=*/true,
3470                                       declarator_p);
3471           /* If that worked, we're done.  */
3472           if (cp_parser_parse_definitely (parser))
3473             return id;
3474           /* We still don't know whether we're looking at an
3475              operator-function-id or a conversion-function-id.  */
3476           cp_parser_parse_tentatively (parser);
3477           /* Try an operator-function-id.  */
3478           id = cp_parser_operator_function_id (parser);
3479           /* If that didn't work, try a conversion-function-id.  */
3480           if (!cp_parser_parse_definitely (parser))
3481             id = cp_parser_conversion_function_id (parser);
3482
3483           return id;
3484         }
3485       /* Fall through.  */
3486
3487     default:
3488       cp_parser_error (parser, "expected unqualified-id");
3489       return error_mark_node;
3490     }
3491 }
3492
3493 /* Parse an (optional) nested-name-specifier.
3494
3495    nested-name-specifier:
3496      class-or-namespace-name :: nested-name-specifier [opt]
3497      class-or-namespace-name :: template nested-name-specifier [opt]
3498
3499    PARSER->SCOPE should be set appropriately before this function is
3500    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3501    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3502    in name lookups.
3503
3504    Sets PARSER->SCOPE to the class (TYPE) or namespace
3505    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3506    it unchanged if there is no nested-name-specifier.  Returns the new
3507    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3508
3509    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3510    part of a declaration and/or decl-specifier.  */
3511
3512 static tree
3513 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3514                                      bool typename_keyword_p,
3515                                      bool check_dependency_p,
3516                                      bool type_p,
3517                                      bool is_declaration)
3518 {
3519   bool success = false;
3520   cp_token_position start = 0;
3521   cp_token *token;
3522
3523   /* If the next token corresponds to a nested name specifier, there
3524      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3525      false, it may have been true before, in which case something
3526      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3527      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3528      CHECK_DEPENDENCY_P is false, we have to fall through into the
3529      main loop.  */
3530   if (check_dependency_p
3531       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3532     {
3533       cp_parser_pre_parsed_nested_name_specifier (parser);
3534       return parser->scope;
3535     }
3536
3537   /* Remember where the nested-name-specifier starts.  */
3538   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3539     {
3540       start = cp_lexer_token_position (parser->lexer, false);
3541       push_deferring_access_checks (dk_deferred);
3542     }
3543
3544   while (true)
3545     {
3546       tree new_scope;
3547       tree old_scope;
3548       tree saved_qualifying_scope;
3549       bool template_keyword_p;
3550
3551       /* Spot cases that cannot be the beginning of a
3552          nested-name-specifier.  */
3553       token = cp_lexer_peek_token (parser->lexer);
3554
3555       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3556          the already parsed nested-name-specifier.  */
3557       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3558         {
3559           /* Grab the nested-name-specifier and continue the loop.  */
3560           cp_parser_pre_parsed_nested_name_specifier (parser);
3561           success = true;
3562           continue;
3563         }
3564
3565       /* Spot cases that cannot be the beginning of a
3566          nested-name-specifier.  On the second and subsequent times
3567          through the loop, we look for the `template' keyword.  */
3568       if (success && token->keyword == RID_TEMPLATE)
3569         ;
3570       /* A template-id can start a nested-name-specifier.  */
3571       else if (token->type == CPP_TEMPLATE_ID)
3572         ;
3573       else
3574         {
3575           /* If the next token is not an identifier, then it is
3576              definitely not a class-or-namespace-name.  */
3577           if (token->type != CPP_NAME)
3578             break;
3579           /* If the following token is neither a `<' (to begin a
3580              template-id), nor a `::', then we are not looking at a
3581              nested-name-specifier.  */
3582           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3583           if (token->type != CPP_SCOPE
3584               && !cp_parser_nth_token_starts_template_argument_list_p
3585                   (parser, 2))
3586             break;
3587         }
3588
3589       /* The nested-name-specifier is optional, so we parse
3590          tentatively.  */
3591       cp_parser_parse_tentatively (parser);
3592
3593       /* Look for the optional `template' keyword, if this isn't the
3594          first time through the loop.  */
3595       if (success)
3596         template_keyword_p = cp_parser_optional_template_keyword (parser);
3597       else
3598         template_keyword_p = false;
3599
3600       /* Save the old scope since the name lookup we are about to do
3601          might destroy it.  */
3602       old_scope = parser->scope;
3603       saved_qualifying_scope = parser->qualifying_scope;
3604       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3605          look up names in "X<T>::I" in order to determine that "Y" is
3606          a template.  So, if we have a typename at this point, we make
3607          an effort to look through it.  */
3608       if (is_declaration
3609           && !typename_keyword_p
3610           && parser->scope
3611           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3612         parser->scope = resolve_typename_type (parser->scope,
3613                                                /*only_current_p=*/false);
3614       /* Parse the qualifying entity.  */
3615       new_scope
3616         = cp_parser_class_or_namespace_name (parser,
3617                                              typename_keyword_p,
3618                                              template_keyword_p,
3619                                              check_dependency_p,
3620                                              type_p,
3621                                              is_declaration);
3622       /* Look for the `::' token.  */
3623       cp_parser_require (parser, CPP_SCOPE, "`::'");
3624
3625       /* If we found what we wanted, we keep going; otherwise, we're
3626          done.  */
3627       if (!cp_parser_parse_definitely (parser))
3628         {
3629           bool error_p = false;
3630
3631           /* Restore the OLD_SCOPE since it was valid before the
3632              failed attempt at finding the last
3633              class-or-namespace-name.  */
3634           parser->scope = old_scope;
3635           parser->qualifying_scope = saved_qualifying_scope;
3636           /* If the next token is an identifier, and the one after
3637              that is a `::', then any valid interpretation would have
3638              found a class-or-namespace-name.  */
3639           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3640                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3641                      == CPP_SCOPE)
3642                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3643                      != CPP_COMPL))
3644             {
3645               token = cp_lexer_consume_token (parser->lexer);
3646               if (!error_p)
3647                 {
3648                   if (!token->ambiguous_p)
3649                     {
3650                       tree decl;
3651                       tree ambiguous_decls;
3652
3653                       decl = cp_parser_lookup_name (parser, token->value,
3654                                                     none_type,
3655                                                     /*is_template=*/false,
3656                                                     /*is_namespace=*/false,
3657                                                     /*check_dependency=*/true,
3658                                                     &ambiguous_decls);
3659                       if (TREE_CODE (decl) == TEMPLATE_DECL)
3660                         error ("%qD used without template parameters", decl);
3661                       else if (ambiguous_decls)
3662                         {
3663                           error ("reference to %qD is ambiguous", 
3664                                  token->value);
3665                           print_candidates (ambiguous_decls);
3666                           decl = error_mark_node;
3667                         }
3668                       else
3669                         cp_parser_name_lookup_error
3670                           (parser, token->value, decl,
3671                            "is not a class or namespace");
3672                     }
3673                   parser->scope = error_mark_node;
3674                   error_p = true;
3675                   /* Treat this as a successful nested-name-specifier
3676                      due to:
3677
3678                      [basic.lookup.qual]
3679
3680                      If the name found is not a class-name (clause
3681                      _class_) or namespace-name (_namespace.def_), the
3682                      program is ill-formed.  */
3683                   success = true;
3684                 }
3685               cp_lexer_consume_token (parser->lexer);
3686             }
3687           break;
3688         }
3689       /* We've found one valid nested-name-specifier.  */
3690       success = true;
3691       /* Name lookup always gives us a DECL.  */
3692       if (TREE_CODE (new_scope) == TYPE_DECL)
3693         new_scope = TREE_TYPE (new_scope);
3694       /* Uses of "template" must be followed by actual templates.  */
3695       if (template_keyword_p
3696           && !(CLASS_TYPE_P (new_scope)
3697                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3698                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3699                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
3700           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3701                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3702                    == TEMPLATE_ID_EXPR)))
3703         pedwarn (TYPE_P (new_scope)
3704                  ? "%qT is not a template"
3705                  : "%qD is not a template",
3706                  new_scope);
3707       /* If it is a class scope, try to complete it; we are about to
3708          be looking up names inside the class.  */
3709       if (TYPE_P (new_scope)
3710           /* Since checking types for dependency can be expensive,
3711              avoid doing it if the type is already complete.  */
3712           && !COMPLETE_TYPE_P (new_scope)
3713           /* Do not try to complete dependent types.  */
3714           && !dependent_type_p (new_scope))
3715         new_scope = complete_type (new_scope);
3716       /* Make sure we look in the right scope the next time through
3717          the loop.  */
3718       parser->scope = new_scope;
3719     }
3720
3721   /* If parsing tentatively, replace the sequence of tokens that makes
3722      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3723      token.  That way, should we re-parse the token stream, we will
3724      not have to repeat the effort required to do the parse, nor will
3725      we issue duplicate error messages.  */
3726   if (success && start)
3727     {
3728       cp_token *token;
3729       tree access_checks;
3730
3731       token = cp_lexer_token_at (parser->lexer, start);
3732       /* Reset the contents of the START token.  */
3733       token->type = CPP_NESTED_NAME_SPECIFIER;
3734       /* Retrieve any deferred checks.  Do not pop this access checks yet
3735          so the memory will not be reclaimed during token replacing below.  */
3736       access_checks = get_deferred_access_checks ();
3737       token->value = build_tree_list (copy_list (access_checks),
3738                                       parser->scope);
3739       TREE_TYPE (token->value) = parser->qualifying_scope;
3740       token->keyword = RID_MAX;
3741
3742       /* Purge all subsequent tokens.  */
3743       cp_lexer_purge_tokens_after (parser->lexer, start);
3744     }
3745   
3746   if (start)
3747     pop_to_parent_deferring_access_checks ();
3748
3749   return success ? parser->scope : NULL_TREE;
3750 }
3751
3752 /* Parse a nested-name-specifier.  See
3753    cp_parser_nested_name_specifier_opt for details.  This function
3754    behaves identically, except that it will an issue an error if no
3755    nested-name-specifier is present.  */
3756
3757 static tree
3758 cp_parser_nested_name_specifier (cp_parser *parser,
3759                                  bool typename_keyword_p,
3760                                  bool check_dependency_p,
3761                                  bool type_p,
3762                                  bool is_declaration)
3763 {
3764   tree scope;
3765
3766   /* Look for the nested-name-specifier.  */
3767   scope = cp_parser_nested_name_specifier_opt (parser,
3768                                                typename_keyword_p,
3769                                                check_dependency_p,
3770                                                type_p,
3771                                                is_declaration);
3772   /* If it was not present, issue an error message.  */
3773   if (!scope)
3774     {
3775       cp_parser_error (parser, "expected nested-name-specifier");
3776       parser->scope = NULL_TREE;
3777     }
3778
3779   return scope;
3780 }
3781
3782 /* Parse a class-or-namespace-name.
3783
3784    class-or-namespace-name:
3785      class-name
3786      namespace-name
3787
3788    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3789    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3790    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3791    TYPE_P is TRUE iff the next name should be taken as a class-name,
3792    even the same name is declared to be another entity in the same
3793    scope.
3794
3795    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3796    specified by the class-or-namespace-name.  If neither is found the
3797    ERROR_MARK_NODE is returned.  */
3798
3799 static tree
3800 cp_parser_class_or_namespace_name (cp_parser *parser,
3801                                    bool typename_keyword_p,
3802                                    bool template_keyword_p,
3803                                    bool check_dependency_p,
3804                                    bool type_p,
3805                                    bool is_declaration)
3806 {
3807   tree saved_scope;
3808   tree saved_qualifying_scope;
3809   tree saved_object_scope;
3810   tree scope;
3811   bool only_class_p;
3812
3813   /* Before we try to parse the class-name, we must save away the
3814      current PARSER->SCOPE since cp_parser_class_name will destroy
3815      it.  */
3816   saved_scope = parser->scope;
3817   saved_qualifying_scope = parser->qualifying_scope;
3818   saved_object_scope = parser->object_scope;
3819   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3820      there is no need to look for a namespace-name.  */
3821   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3822   if (!only_class_p)
3823     cp_parser_parse_tentatively (parser);
3824   scope = cp_parser_class_name (parser,
3825                                 typename_keyword_p,
3826                                 template_keyword_p,
3827                                 type_p ? class_type : none_type,
3828                                 check_dependency_p,
3829                                 /*class_head_p=*/false,
3830                                 is_declaration);
3831   /* If that didn't work, try for a namespace-name.  */
3832   if (!only_class_p && !cp_parser_parse_definitely (parser))
3833     {
3834       /* Restore the saved scope.  */
3835       parser->scope = saved_scope;
3836       parser->qualifying_scope = saved_qualifying_scope;
3837       parser->object_scope = saved_object_scope;
3838       /* If we are not looking at an identifier followed by the scope
3839          resolution operator, then this is not part of a
3840          nested-name-specifier.  (Note that this function is only used
3841          to parse the components of a nested-name-specifier.)  */
3842       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3843           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3844         return error_mark_node;
3845       scope = cp_parser_namespace_name (parser);
3846     }
3847
3848   return scope;
3849 }
3850
3851 /* Parse a postfix-expression.
3852
3853    postfix-expression:
3854      primary-expression
3855      postfix-expression [ expression ]
3856      postfix-expression ( expression-list [opt] )
3857      simple-type-specifier ( expression-list [opt] )
3858      typename :: [opt] nested-name-specifier identifier
3859        ( expression-list [opt] )
3860      typename :: [opt] nested-name-specifier template [opt] template-id
3861        ( expression-list [opt] )
3862      postfix-expression . template [opt] id-expression
3863      postfix-expression -> template [opt] id-expression
3864      postfix-expression . pseudo-destructor-name
3865      postfix-expression -> pseudo-destructor-name
3866      postfix-expression ++
3867      postfix-expression --
3868      dynamic_cast < type-id > ( expression )
3869      static_cast < type-id > ( expression )
3870      reinterpret_cast < type-id > ( expression )
3871      const_cast < type-id > ( expression )
3872      typeid ( expression )
3873      typeid ( type-id )
3874
3875    GNU Extension:
3876
3877    postfix-expression:
3878      ( type-id ) { initializer-list , [opt] }
3879
3880    This extension is a GNU version of the C99 compound-literal
3881    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3882    but they are essentially the same concept.)
3883
3884    If ADDRESS_P is true, the postfix expression is the operand of the
3885    `&' operator.  CAST_P is true if this expression is the target of a
3886    cast.
3887
3888    Returns a representation of the expression.  */
3889
3890 static tree
3891 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3892 {
3893   cp_token *token;
3894   enum rid keyword;
3895   cp_id_kind idk = CP_ID_KIND_NONE;
3896   tree postfix_expression = NULL_TREE;
3897
3898   /* Peek at the next token.  */
3899   token = cp_lexer_peek_token (parser->lexer);
3900   /* Some of the productions are determined by keywords.  */
3901   keyword = token->keyword;
3902   switch (keyword)
3903     {
3904     case RID_DYNCAST:
3905     case RID_STATCAST:
3906     case RID_REINTCAST:
3907     case RID_CONSTCAST:
3908       {
3909         tree type;
3910         tree expression;
3911         const char *saved_message;
3912
3913         /* All of these can be handled in the same way from the point
3914            of view of parsing.  Begin by consuming the token
3915            identifying the cast.  */
3916         cp_lexer_consume_token (parser->lexer);
3917
3918         /* New types cannot be defined in the cast.  */
3919         saved_message = parser->type_definition_forbidden_message;
3920         parser->type_definition_forbidden_message
3921           = "types may not be defined in casts";
3922
3923         /* Look for the opening `<'.  */
3924         cp_parser_require (parser, CPP_LESS, "`<'");
3925         /* Parse the type to which we are casting.  */
3926         type = cp_parser_type_id (parser);
3927         /* Look for the closing `>'.  */
3928         cp_parser_require (parser, CPP_GREATER, "`>'");
3929         /* Restore the old message.  */
3930         parser->type_definition_forbidden_message = saved_message;
3931
3932         /* And the expression which is being cast.  */
3933         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3934         expression = cp_parser_expression (parser, /*cast_p=*/true);
3935         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3936
3937         /* Only type conversions to integral or enumeration types
3938            can be used in constant-expressions.  */
3939         if (parser->integral_constant_expression_p
3940             && !dependent_type_p (type)
3941             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3942             && (cp_parser_non_integral_constant_expression
3943                 (parser,
3944                  "a cast to a type other than an integral or "
3945                  "enumeration type")))
3946           return error_mark_node;
3947
3948         switch (keyword)
3949           {
3950           case RID_DYNCAST:
3951             postfix_expression
3952               = build_dynamic_cast (type, expression);
3953             break;
3954           case RID_STATCAST:
3955             postfix_expression
3956               = build_static_cast (type, expression);
3957             break;
3958           case RID_REINTCAST:
3959             postfix_expression
3960               = build_reinterpret_cast (type, expression);
3961             break;
3962           case RID_CONSTCAST:
3963             postfix_expression
3964               = build_const_cast (type, expression);
3965             break;
3966           default:
3967             gcc_unreachable ();
3968           }
3969       }
3970       break;
3971
3972     case RID_TYPEID:
3973       {
3974         tree type;
3975         const char *saved_message;
3976         bool saved_in_type_id_in_expr_p;
3977
3978         /* Consume the `typeid' token.  */
3979         cp_lexer_consume_token (parser->lexer);
3980         /* Look for the `(' token.  */
3981         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3982         /* Types cannot be defined in a `typeid' expression.  */
3983         saved_message = parser->type_definition_forbidden_message;
3984         parser->type_definition_forbidden_message
3985           = "types may not be defined in a `typeid\' expression";
3986         /* We can't be sure yet whether we're looking at a type-id or an
3987            expression.  */
3988         cp_parser_parse_tentatively (parser);
3989         /* Try a type-id first.  */
3990         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3991         parser->in_type_id_in_expr_p = true;
3992         type = cp_parser_type_id (parser);
3993         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3994         /* Look for the `)' token.  Otherwise, we can't be sure that
3995            we're not looking at an expression: consider `typeid (int
3996            (3))', for example.  */
3997         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3998         /* If all went well, simply lookup the type-id.  */
3999         if (cp_parser_parse_definitely (parser))
4000           postfix_expression = get_typeid (type);
4001         /* Otherwise, fall back to the expression variant.  */
4002         else
4003           {
4004             tree expression;
4005
4006             /* Look for an expression.  */
4007             expression = cp_parser_expression (parser, /*cast_p=*/false);
4008             /* Compute its typeid.  */
4009             postfix_expression = build_typeid (expression);
4010             /* Look for the `)' token.  */
4011             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4012           }
4013         /* `typeid' may not appear in an integral constant expression.  */
4014         if (cp_parser_non_integral_constant_expression(parser,
4015                                                        "`typeid' operator"))
4016           return error_mark_node;
4017         /* Restore the saved message.  */
4018         parser->type_definition_forbidden_message = saved_message;
4019       }
4020       break;
4021
4022     case RID_TYPENAME:
4023       {
4024         tree type;
4025         /* The syntax permitted here is the same permitted for an
4026            elaborated-type-specifier.  */
4027         type = cp_parser_elaborated_type_specifier (parser,
4028                                                     /*is_friend=*/false,
4029                                                     /*is_declaration=*/false);
4030         postfix_expression = cp_parser_functional_cast (parser, type);
4031       }
4032       break;
4033
4034     default:
4035       {
4036         tree type;
4037
4038         /* If the next thing is a simple-type-specifier, we may be
4039            looking at a functional cast.  We could also be looking at
4040            an id-expression.  So, we try the functional cast, and if
4041            that doesn't work we fall back to the primary-expression.  */
4042         cp_parser_parse_tentatively (parser);
4043         /* Look for the simple-type-specifier.  */
4044         type = cp_parser_simple_type_specifier (parser,
4045                                                 /*decl_specs=*/NULL,
4046                                                 CP_PARSER_FLAGS_NONE);
4047         /* Parse the cast itself.  */
4048         if (!cp_parser_error_occurred (parser))
4049           postfix_expression
4050             = cp_parser_functional_cast (parser, type);
4051         /* If that worked, we're done.  */
4052         if (cp_parser_parse_definitely (parser))
4053           break;
4054
4055         /* If the functional-cast didn't work out, try a
4056            compound-literal.  */
4057         if (cp_parser_allow_gnu_extensions_p (parser)
4058             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4059           {
4060             VEC(constructor_elt,gc) *initializer_list = NULL;
4061             bool saved_in_type_id_in_expr_p;
4062
4063             cp_parser_parse_tentatively (parser);
4064             /* Consume the `('.  */
4065             cp_lexer_consume_token (parser->lexer);
4066             /* Parse the type.  */
4067             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4068             parser->in_type_id_in_expr_p = true;
4069             type = cp_parser_type_id (parser);
4070             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4071             /* Look for the `)'.  */
4072             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4073             /* Look for the `{'.  */
4074             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4075             /* If things aren't going well, there's no need to
4076                keep going.  */
4077             if (!cp_parser_error_occurred (parser))
4078               {
4079                 bool non_constant_p;
4080                 /* Parse the initializer-list.  */
4081                 initializer_list
4082                   = cp_parser_initializer_list (parser, &non_constant_p);
4083                 /* Allow a trailing `,'.  */
4084                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4085                   cp_lexer_consume_token (parser->lexer);
4086                 /* Look for the final `}'.  */
4087                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4088               }
4089             /* If that worked, we're definitely looking at a
4090                compound-literal expression.  */
4091             if (cp_parser_parse_definitely (parser))
4092               {
4093                 /* Warn the user that a compound literal is not
4094                    allowed in standard C++.  */
4095                 if (pedantic)
4096                   pedwarn ("ISO C++ forbids compound-literals");
4097                 /* Form the representation of the compound-literal.  */
4098                 postfix_expression
4099                   = finish_compound_literal (type, initializer_list);
4100                 break;
4101               }
4102           }
4103
4104         /* It must be a primary-expression.  */
4105         postfix_expression 
4106           = cp_parser_primary_expression (parser, address_p, cast_p, 
4107                                           /*template_arg_p=*/false,
4108                                           &idk);
4109       }
4110       break;
4111     }
4112
4113   /* Keep looping until the postfix-expression is complete.  */
4114   while (true)
4115     {
4116       if (idk == CP_ID_KIND_UNQUALIFIED
4117           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4118           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4119         /* It is not a Koenig lookup function call.  */
4120         postfix_expression
4121           = unqualified_name_lookup_error (postfix_expression);
4122
4123       /* Peek at the next token.  */
4124       token = cp_lexer_peek_token (parser->lexer);
4125
4126       switch (token->type)
4127         {
4128         case CPP_OPEN_SQUARE:
4129           postfix_expression
4130             = cp_parser_postfix_open_square_expression (parser,
4131                                                         postfix_expression,
4132                                                         false);
4133           idk = CP_ID_KIND_NONE;
4134           break;
4135
4136         case CPP_OPEN_PAREN:
4137           /* postfix-expression ( expression-list [opt] ) */
4138           {
4139             bool koenig_p;
4140             bool is_builtin_constant_p;
4141             bool saved_integral_constant_expression_p = false;
4142             bool saved_non_integral_constant_expression_p = false;
4143             tree args;
4144
4145             is_builtin_constant_p
4146               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4147             if (is_builtin_constant_p)
4148               {
4149                 /* The whole point of __builtin_constant_p is to allow
4150                    non-constant expressions to appear as arguments.  */
4151                 saved_integral_constant_expression_p
4152                   = parser->integral_constant_expression_p;
4153                 saved_non_integral_constant_expression_p
4154                   = parser->non_integral_constant_expression_p;
4155                 parser->integral_constant_expression_p = false;
4156               }
4157             args = (cp_parser_parenthesized_expression_list
4158                     (parser, /*is_attribute_list=*/false,
4159                      /*cast_p=*/false,
4160                      /*non_constant_p=*/NULL));
4161             if (is_builtin_constant_p)
4162               {
4163                 parser->integral_constant_expression_p
4164                   = saved_integral_constant_expression_p;
4165                 parser->non_integral_constant_expression_p
4166                   = saved_non_integral_constant_expression_p;
4167               }
4168
4169             if (args == error_mark_node)
4170               {
4171                 postfix_expression = error_mark_node;
4172                 break;
4173               }
4174
4175             /* Function calls are not permitted in
4176                constant-expressions.  */
4177             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4178                 && cp_parser_non_integral_constant_expression (parser,
4179                                                                "a function call"))
4180               {
4181                 postfix_expression = error_mark_node;
4182                 break;
4183               }
4184
4185             koenig_p = false;
4186             if (idk == CP_ID_KIND_UNQUALIFIED)
4187               {
4188                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4189                   {
4190                     if (args)
4191                       {
4192                         koenig_p = true;
4193                         postfix_expression
4194                           = perform_koenig_lookup (postfix_expression, args);
4195                       }
4196                     else
4197                       postfix_expression
4198                         = unqualified_fn_lookup_error (postfix_expression);
4199                   }
4200                 /* We do not perform argument-dependent lookup if
4201                    normal lookup finds a non-function, in accordance
4202                    with the expected resolution of DR 218.  */
4203                 else if (args && is_overloaded_fn (postfix_expression))
4204                   {
4205                     tree fn = get_first_fn (postfix_expression);
4206
4207                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4208                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4209
4210                     /* Only do argument dependent lookup if regular
4211                        lookup does not find a set of member functions.
4212                        [basic.lookup.koenig]/2a  */
4213                     if (!DECL_FUNCTION_MEMBER_P (fn))
4214                       {
4215                         koenig_p = true;
4216                         postfix_expression
4217                           = perform_koenig_lookup (postfix_expression, args);
4218                       }
4219                   }
4220               }
4221
4222             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4223               {
4224                 tree instance = TREE_OPERAND (postfix_expression, 0);
4225                 tree fn = TREE_OPERAND (postfix_expression, 1);
4226
4227                 if (processing_template_decl
4228                     && (type_dependent_expression_p (instance)
4229                         || (!BASELINK_P (fn)
4230                             && TREE_CODE (fn) != FIELD_DECL)
4231                         || type_dependent_expression_p (fn)
4232                         || any_type_dependent_arguments_p (args)))
4233                   {
4234                     postfix_expression
4235                       = build_min_nt (CALL_EXPR, postfix_expression,
4236                                       args, NULL_TREE);
4237                     break;
4238                   }
4239
4240                 if (BASELINK_P (fn))
4241                   postfix_expression
4242                     = (build_new_method_call
4243                        (instance, fn, args, NULL_TREE,
4244                         (idk == CP_ID_KIND_QUALIFIED
4245                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4246                 else
4247                   postfix_expression
4248                     = finish_call_expr (postfix_expression, args,
4249                                         /*disallow_virtual=*/false,
4250                                         /*koenig_p=*/false);
4251               }
4252             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4253                      || TREE_CODE (postfix_expression) == MEMBER_REF
4254                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4255               postfix_expression = (build_offset_ref_call_from_tree
4256                                     (postfix_expression, args));
4257             else if (idk == CP_ID_KIND_QUALIFIED)
4258               /* A call to a static class member, or a namespace-scope
4259                  function.  */
4260               postfix_expression
4261                 = finish_call_expr (postfix_expression, args,
4262                                     /*disallow_virtual=*/true,
4263                                     koenig_p);
4264             else
4265               /* All other function calls.  */
4266               postfix_expression
4267                 = finish_call_expr (postfix_expression, args,
4268                                     /*disallow_virtual=*/false,
4269                                     koenig_p);
4270
4271             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4272             idk = CP_ID_KIND_NONE;
4273           }
4274           break;
4275
4276         case CPP_DOT:
4277         case CPP_DEREF:
4278           /* postfix-expression . template [opt] id-expression
4279              postfix-expression . pseudo-destructor-name
4280              postfix-expression -> template [opt] id-expression
4281              postfix-expression -> pseudo-destructor-name */
4282
4283           /* Consume the `.' or `->' operator.  */
4284           cp_lexer_consume_token (parser->lexer);
4285
4286           postfix_expression
4287             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4288                                                       postfix_expression,
4289                                                       false, &idk);
4290           break;
4291
4292         case CPP_PLUS_PLUS:
4293           /* postfix-expression ++  */
4294           /* Consume the `++' token.  */
4295           cp_lexer_consume_token (parser->lexer);
4296           /* Generate a representation for the complete expression.  */
4297           postfix_expression
4298             = finish_increment_expr (postfix_expression,
4299                                      POSTINCREMENT_EXPR);
4300           /* Increments may not appear in constant-expressions.  */
4301           if (cp_parser_non_integral_constant_expression (parser,
4302                                                           "an increment"))
4303             postfix_expression = error_mark_node;
4304           idk = CP_ID_KIND_NONE;
4305           break;
4306
4307         case CPP_MINUS_MINUS:
4308           /* postfix-expression -- */
4309           /* Consume the `--' token.  */
4310           cp_lexer_consume_token (parser->lexer);
4311           /* Generate a representation for the complete expression.  */
4312           postfix_expression
4313             = finish_increment_expr (postfix_expression,
4314                                      POSTDECREMENT_EXPR);
4315           /* Decrements may not appear in constant-expressions.  */
4316           if (cp_parser_non_integral_constant_expression (parser,
4317                                                           "a decrement"))
4318             postfix_expression = error_mark_node;
4319           idk = CP_ID_KIND_NONE;
4320           break;
4321
4322         default:
4323           return postfix_expression;
4324         }
4325     }
4326
4327   /* We should never get here.  */
4328   gcc_unreachable ();
4329   return error_mark_node;
4330 }
4331
4332 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4333    by cp_parser_builtin_offsetof.  We're looking for
4334
4335      postfix-expression [ expression ]
4336
4337    FOR_OFFSETOF is set if we're being called in that context, which
4338    changes how we deal with integer constant expressions.  */
4339
4340 static tree
4341 cp_parser_postfix_open_square_expression (cp_parser *parser,
4342                                           tree postfix_expression,
4343                                           bool for_offsetof)
4344 {
4345   tree index;
4346
4347   /* Consume the `[' token.  */
4348   cp_lexer_consume_token (parser->lexer);
4349
4350   /* Parse the index expression.  */
4351   /* ??? For offsetof, there is a question of what to allow here.  If
4352      offsetof is not being used in an integral constant expression context,
4353      then we *could* get the right answer by computing the value at runtime.
4354      If we are in an integral constant expression context, then we might
4355      could accept any constant expression; hard to say without analysis.
4356      Rather than open the barn door too wide right away, allow only integer
4357      constant expressions here.  */
4358   if (for_offsetof)
4359     index = cp_parser_constant_expression (parser, false, NULL);
4360   else
4361     index = cp_parser_expression (parser, /*cast_p=*/false);
4362
4363   /* Look for the closing `]'.  */
4364   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4365
4366   /* Build the ARRAY_REF.  */
4367   postfix_expression = grok_array_decl (postfix_expression, index);
4368
4369   /* When not doing offsetof, array references are not permitted in
4370      constant-expressions.  */
4371   if (!for_offsetof
4372       && (cp_parser_non_integral_constant_expression
4373           (parser, "an array reference")))
4374     postfix_expression = error_mark_node;
4375
4376   return postfix_expression;
4377 }
4378
4379 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4380    by cp_parser_builtin_offsetof.  We're looking for
4381
4382      postfix-expression . template [opt] id-expression
4383      postfix-expression . pseudo-destructor-name
4384      postfix-expression -> template [opt] id-expression
4385      postfix-expression -> pseudo-destructor-name
4386
4387    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4388    limits what of the above we'll actually accept, but nevermind.
4389    TOKEN_TYPE is the "." or "->" token, which will already have been
4390    removed from the stream.  */
4391
4392 static tree
4393 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4394                                         enum cpp_ttype token_type,
4395                                         tree postfix_expression,
4396                                         bool for_offsetof, cp_id_kind *idk)
4397 {
4398   tree name;
4399   bool dependent_p;
4400   bool pseudo_destructor_p;
4401   tree scope = NULL_TREE;
4402
4403   /* If this is a `->' operator, dereference the pointer.  */
4404   if (token_type == CPP_DEREF)
4405     postfix_expression = build_x_arrow (postfix_expression);
4406   /* Check to see whether or not the expression is type-dependent.  */
4407   dependent_p = type_dependent_expression_p (postfix_expression);
4408   /* The identifier following the `->' or `.' is not qualified.  */
4409   parser->scope = NULL_TREE;
4410   parser->qualifying_scope = NULL_TREE;
4411   parser->object_scope = NULL_TREE;
4412   *idk = CP_ID_KIND_NONE;
4413   /* Enter the scope corresponding to the type of the object
4414      given by the POSTFIX_EXPRESSION.  */
4415   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4416     {
4417       scope = TREE_TYPE (postfix_expression);
4418       /* According to the standard, no expression should ever have
4419          reference type.  Unfortunately, we do not currently match
4420          the standard in this respect in that our internal representation
4421          of an expression may have reference type even when the standard
4422          says it does not.  Therefore, we have to manually obtain the
4423          underlying type here.  */
4424       scope = non_reference (scope);
4425       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4426       if (scope == unknown_type_node)
4427         {
4428           error ("%qE does not have class type", postfix_expression);
4429           scope = NULL_TREE;
4430         }
4431       else
4432         scope = complete_type_or_else (scope, NULL_TREE);
4433       /* Let the name lookup machinery know that we are processing a
4434          class member access expression.  */
4435       parser->context->object_type = scope;
4436       /* If something went wrong, we want to be able to discern that case,
4437          as opposed to the case where there was no SCOPE due to the type
4438          of expression being dependent.  */
4439       if (!scope)
4440         scope = error_mark_node;
4441       /* If the SCOPE was erroneous, make the various semantic analysis
4442          functions exit quickly -- and without issuing additional error
4443          messages.  */
4444       if (scope == error_mark_node)
4445         postfix_expression = error_mark_node;
4446     }
4447
4448   /* Assume this expression is not a pseudo-destructor access.  */
4449   pseudo_destructor_p = false;
4450
4451   /* If the SCOPE is a scalar type, then, if this is a valid program,
4452      we must be looking at a pseudo-destructor-name.  */
4453   if (scope && SCALAR_TYPE_P (scope))
4454     {
4455       tree s;
4456       tree type;
4457
4458       cp_parser_parse_tentatively (parser);
4459       /* Parse the pseudo-destructor-name.  */
4460       s = NULL_TREE;
4461       cp_parser_pseudo_destructor_name (parser, &s, &type);
4462       if (cp_parser_parse_definitely (parser))
4463         {
4464           pseudo_destructor_p = true;
4465           postfix_expression
4466             = finish_pseudo_destructor_expr (postfix_expression,
4467                                              s, TREE_TYPE (type));
4468         }
4469     }
4470
4471   if (!pseudo_destructor_p)
4472     {
4473       /* If the SCOPE is not a scalar type, we are looking at an
4474          ordinary class member access expression, rather than a
4475          pseudo-destructor-name.  */
4476       bool template_p;
4477       /* Parse the id-expression.  */
4478       name = (cp_parser_id_expression 
4479               (parser, 
4480                cp_parser_optional_template_keyword (parser),
4481                /*check_dependency_p=*/true,
4482                &template_p,
4483                /*declarator_p=*/false));
4484       /* In general, build a SCOPE_REF if the member name is qualified.
4485          However, if the name was not dependent and has already been
4486          resolved; there is no need to build the SCOPE_REF.  For example;
4487
4488              struct X { void f(); };
4489              template <typename T> void f(T* t) { t->X::f(); }
4490
4491          Even though "t" is dependent, "X::f" is not and has been resolved
4492          to a BASELINK; there is no need to include scope information.  */
4493
4494       /* But we do need to remember that there was an explicit scope for
4495          virtual function calls.  */
4496       if (parser->scope)
4497         *idk = CP_ID_KIND_QUALIFIED;
4498
4499       /* If the name is a template-id that names a type, we will get a
4500          TYPE_DECL here.  That is invalid code.  */
4501       if (TREE_CODE (name) == TYPE_DECL)
4502         {
4503           error ("invalid use of %qD", name);
4504           postfix_expression = error_mark_node;
4505         }
4506       else
4507         {
4508           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4509             {
4510               name = build_qualified_name (/*type=*/NULL_TREE,
4511                                            parser->scope,
4512                                            name,
4513                                            template_p);
4514               parser->scope = NULL_TREE;
4515               parser->qualifying_scope = NULL_TREE;
4516               parser->object_scope = NULL_TREE;
4517             }
4518           if (scope && name && BASELINK_P (name))
4519             adjust_result_of_qualified_name_lookup
4520               (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4521           postfix_expression
4522             = finish_class_member_access_expr (postfix_expression, name,
4523                                                template_p);
4524         }
4525     }
4526
4527   /* We no longer need to look up names in the scope of the object on
4528      the left-hand side of the `.' or `->' operator.  */
4529   parser->context->object_type = NULL_TREE;
4530
4531   /* Outside of offsetof, these operators may not appear in
4532      constant-expressions.  */
4533   if (!for_offsetof
4534       && (cp_parser_non_integral_constant_expression
4535           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4536     postfix_expression = error_mark_node;
4537
4538   return postfix_expression;
4539 }
4540
4541 /* Parse a parenthesized expression-list.
4542
4543    expression-list:
4544      assignment-expression
4545      expression-list, assignment-expression
4546
4547    attribute-list:
4548      expression-list
4549      identifier
4550      identifier, expression-list
4551
4552    CAST_P is true if this expression is the target of a cast.
4553
4554    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4555    representation of an assignment-expression.  Note that a TREE_LIST
4556    is returned even if there is only a single expression in the list.
4557    error_mark_node is returned if the ( and or ) are
4558    missing. NULL_TREE is returned on no expressions. The parentheses
4559    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4560    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4561    indicates whether or not all of the expressions in the list were
4562    constant.  */
4563
4564 static tree
4565 cp_parser_parenthesized_expression_list (cp_parser* parser,
4566                                          bool is_attribute_list,
4567                                          bool cast_p,
4568                                          bool *non_constant_p)
4569 {
4570   tree expression_list = NULL_TREE;
4571   bool fold_expr_p = is_attribute_list;
4572   tree identifier = NULL_TREE;
4573
4574   /* Assume all the expressions will be constant.  */
4575   if (non_constant_p)
4576     *non_constant_p = false;
4577
4578   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4579     return error_mark_node;
4580
4581   /* Consume expressions until there are no more.  */
4582   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4583     while (true)
4584       {
4585         tree expr;
4586
4587         /* At the beginning of attribute lists, check to see if the
4588            next token is an identifier.  */
4589         if (is_attribute_list
4590             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4591           {
4592             cp_token *token;
4593
4594             /* Consume the identifier.  */
4595             token = cp_lexer_consume_token (parser->lexer);
4596             /* Save the identifier.  */
4597             identifier = token->value;
4598           }
4599         else
4600           {
4601             /* Parse the next assignment-expression.  */
4602             if (non_constant_p)
4603               {
4604                 bool expr_non_constant_p;
4605                 expr = (cp_parser_constant_expression
4606                         (parser, /*allow_non_constant_p=*/true,
4607                          &expr_non_constant_p));
4608                 if (expr_non_constant_p)
4609                   *non_constant_p = true;
4610               }
4611             else
4612               expr = cp_parser_assignment_expression (parser, cast_p);
4613
4614             if (fold_expr_p)
4615               expr = fold_non_dependent_expr (expr);
4616
4617              /* Add it to the list.  We add error_mark_node
4618                 expressions to the list, so that we can still tell if
4619                 the correct form for a parenthesized expression-list
4620                 is found. That gives better errors.  */
4621             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4622
4623             if (expr == error_mark_node)
4624               goto skip_comma;
4625           }
4626
4627         /* After the first item, attribute lists look the same as
4628            expression lists.  */
4629         is_attribute_list = false;
4630
4631       get_comma:;
4632         /* If the next token isn't a `,', then we are done.  */
4633         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4634           break;
4635
4636         /* Otherwise, consume the `,' and keep going.  */
4637         cp_lexer_consume_token (parser->lexer);
4638       }
4639
4640   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4641     {
4642       int ending;
4643
4644     skip_comma:;
4645       /* We try and resync to an unnested comma, as that will give the
4646          user better diagnostics.  */
4647       ending = cp_parser_skip_to_closing_parenthesis (parser,
4648                                                       /*recovering=*/true,
4649                                                       /*or_comma=*/true,
4650                                                       /*consume_paren=*/true);
4651       if (ending < 0)
4652         goto get_comma;
4653       if (!ending)
4654         return error_mark_node;
4655     }
4656
4657   /* We built up the list in reverse order so we must reverse it now.  */
4658   expression_list = nreverse (expression_list);
4659   if (identifier)
4660     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4661
4662   return expression_list;
4663 }
4664
4665 /* Parse a pseudo-destructor-name.
4666
4667    pseudo-destructor-name:
4668      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4669      :: [opt] nested-name-specifier template template-id :: ~ type-name
4670      :: [opt] nested-name-specifier [opt] ~ type-name
4671
4672    If either of the first two productions is used, sets *SCOPE to the
4673    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4674    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4675    or ERROR_MARK_NODE if the parse fails.  */
4676
4677 static void
4678 cp_parser_pseudo_destructor_name (cp_parser* parser,
4679                                   tree* scope,
4680                                   tree* type)
4681 {
4682   bool nested_name_specifier_p;
4683
4684   /* Assume that things will not work out.  */
4685   *type = error_mark_node;
4686
4687   /* Look for the optional `::' operator.  */
4688   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4689   /* Look for the optional nested-name-specifier.  */
4690   nested_name_specifier_p
4691     = (cp_parser_nested_name_specifier_opt (parser,
4692                                             /*typename_keyword_p=*/false,
4693                                             /*check_dependency_p=*/true,
4694                                             /*type_p=*/false,
4695                                             /*is_declaration=*/true)
4696        != NULL_TREE);
4697   /* Now, if we saw a nested-name-specifier, we might be doing the
4698      second production.  */
4699   if (nested_name_specifier_p
4700       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4701     {
4702       /* Consume the `template' keyword.  */
4703       cp_lexer_consume_token (parser->lexer);
4704       /* Parse the template-id.  */
4705       cp_parser_template_id (parser,
4706                              /*template_keyword_p=*/true,
4707                              /*check_dependency_p=*/false,
4708                              /*is_declaration=*/true);
4709       /* Look for the `::' token.  */
4710       cp_parser_require (parser, CPP_SCOPE, "`::'");
4711     }
4712   /* If the next token is not a `~', then there might be some
4713      additional qualification.  */
4714   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4715     {
4716       /* Look for the type-name.  */
4717       *scope = TREE_TYPE (cp_parser_type_name (parser));
4718
4719       if (*scope == error_mark_node)
4720         return;
4721
4722       /* If we don't have ::~, then something has gone wrong.  Since
4723          the only caller of this function is looking for something
4724          after `.' or `->' after a scalar type, most likely the
4725          program is trying to get a member of a non-aggregate
4726          type.  */
4727       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4728           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4729         {
4730           cp_parser_error (parser, "request for member of non-aggregate type");
4731           return;
4732         }
4733
4734       /* Look for the `::' token.  */
4735       cp_parser_require (parser, CPP_SCOPE, "`::'");
4736     }
4737   else
4738     *scope = NULL_TREE;
4739
4740   /* Look for the `~'.  */
4741   cp_parser_require (parser, CPP_COMPL, "`~'");
4742   /* Look for the type-name again.  We are not responsible for
4743      checking that it matches the first type-name.  */
4744   *type = cp_parser_type_name (parser);
4745 }
4746
4747 /* Parse a unary-expression.
4748
4749    unary-expression:
4750      postfix-expression
4751      ++ cast-expression
4752      -- cast-expression
4753      unary-operator cast-expression
4754      sizeof unary-expression
4755      sizeof ( type-id )
4756      new-expression
4757      delete-expression
4758
4759    GNU Extensions:
4760
4761    unary-expression:
4762      __extension__ cast-expression
4763      __alignof__ unary-expression
4764      __alignof__ ( type-id )
4765      __real__ cast-expression
4766      __imag__ cast-expression
4767      && identifier
4768
4769    ADDRESS_P is true iff the unary-expression is appearing as the
4770    operand of the `&' operator.   CAST_P is true if this expression is
4771    the target of a cast.
4772
4773    Returns a representation of the expression.  */
4774
4775 static tree
4776 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4777 {
4778   cp_token *token;
4779   enum tree_code unary_operator;
4780
4781   /* Peek at the next token.  */
4782   token = cp_lexer_peek_token (parser->lexer);
4783   /* Some keywords give away the kind of expression.  */
4784   if (token->type == CPP_KEYWORD)
4785     {
4786       enum rid keyword = token->keyword;
4787
4788       switch (keyword)
4789         {
4790         case RID_ALIGNOF:
4791         case RID_SIZEOF:
4792           {
4793             tree operand;
4794             enum tree_code op;
4795
4796             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4797             /* Consume the token.  */
4798             cp_lexer_consume_token (parser->lexer);
4799             /* Parse the operand.  */
4800             operand = cp_parser_sizeof_operand (parser, keyword);
4801
4802             if (TYPE_P (operand))
4803               return cxx_sizeof_or_alignof_type (operand, op, true);
4804             else
4805               return cxx_sizeof_or_alignof_expr (operand, op);
4806           }
4807
4808         case RID_NEW:
4809           return cp_parser_new_expression (parser);
4810
4811         case RID_DELETE:
4812           return cp_parser_delete_expression (parser);
4813
4814         case RID_EXTENSION:
4815           {
4816             /* The saved value of the PEDANTIC flag.  */
4817             int saved_pedantic;
4818             tree expr;
4819
4820             /* Save away the PEDANTIC flag.  */
4821             cp_parser_extension_opt (parser, &saved_pedantic);
4822             /* Parse the cast-expression.  */
4823             expr = cp_parser_simple_cast_expression (parser);
4824             /* Restore the PEDANTIC flag.  */
4825             pedantic = saved_pedantic;
4826
4827             return expr;
4828           }
4829
4830         case RID_REALPART:
4831         case RID_IMAGPART:
4832           {
4833             tree expression;
4834
4835             /* Consume the `__real__' or `__imag__' token.  */
4836             cp_lexer_consume_token (parser->lexer);
4837             /* Parse the cast-expression.  */
4838             expression = cp_parser_simple_cast_expression (parser);
4839             /* Create the complete representation.  */
4840             return build_x_unary_op ((keyword == RID_REALPART
4841                                       ? REALPART_EXPR : IMAGPART_EXPR),
4842                                      expression);
4843           }
4844           break;
4845
4846         default:
4847           break;
4848         }
4849     }
4850
4851   /* Look for the `:: new' and `:: delete', which also signal the
4852      beginning of a new-expression, or delete-expression,
4853      respectively.  If the next token is `::', then it might be one of
4854      these.  */
4855   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4856     {
4857       enum rid keyword;
4858
4859       /* See if the token after the `::' is one of the keywords in
4860          which we're interested.  */
4861       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4862       /* If it's `new', we have a new-expression.  */
4863       if (keyword == RID_NEW)
4864         return cp_parser_new_expression (parser);
4865       /* Similarly, for `delete'.  */
4866       else if (keyword == RID_DELETE)
4867         return cp_parser_delete_expression (parser);
4868     }
4869
4870   /* Look for a unary operator.  */
4871   unary_operator = cp_parser_unary_operator (token);
4872   /* The `++' and `--' operators can be handled similarly, even though
4873      they are not technically unary-operators in the grammar.  */
4874   if (unary_operator == ERROR_MARK)
4875     {
4876       if (token->type == CPP_PLUS_PLUS)
4877         unary_operator = PREINCREMENT_EXPR;
4878       else if (token->type == CPP_MINUS_MINUS)
4879         unary_operator = PREDECREMENT_EXPR;
4880       /* Handle the GNU address-of-label extension.  */
4881       else if (cp_parser_allow_gnu_extensions_p (parser)
4882                && token->type == CPP_AND_AND)
4883         {
4884           tree identifier;
4885
4886           /* Consume the '&&' token.  */
4887           cp_lexer_consume_token (parser->lexer);
4888           /* Look for the identifier.  */
4889           identifier = cp_parser_identifier (parser);
4890           /* Create an expression representing the address.  */
4891           return finish_label_address_expr (identifier);
4892         }
4893     }
4894   if (unary_operator != ERROR_MARK)
4895     {
4896       tree cast_expression;
4897       tree expression = error_mark_node;
4898       const char *non_constant_p = NULL;
4899
4900       /* Consume the operator token.  */
4901       token = cp_lexer_consume_token (parser->lexer);
4902       /* Parse the cast-expression.  */
4903       cast_expression
4904         = cp_parser_cast_expression (parser,
4905                                      unary_operator == ADDR_EXPR,
4906                                      /*cast_p=*/false);
4907       /* Now, build an appropriate representation.  */
4908       switch (unary_operator)
4909         {
4910         case INDIRECT_REF:
4911           non_constant_p = "`*'";
4912           expression = build_x_indirect_ref (cast_expression, "unary *");
4913           break;
4914
4915         case ADDR_EXPR:
4916           non_constant_p = "`&'";
4917           /* Fall through.  */
4918         case BIT_NOT_EXPR:
4919           expression = build_x_unary_op (unary_operator, cast_expression);
4920           break;
4921
4922         case PREINCREMENT_EXPR:
4923         case PREDECREMENT_EXPR:
4924           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4925                             ? "`++'" : "`--'");
4926           /* Fall through.  */
4927         case UNARY_PLUS_EXPR:
4928         case NEGATE_EXPR:
4929         case TRUTH_NOT_EXPR:
4930           expression = finish_unary_op_expr (unary_operator, cast_expression);
4931           break;
4932
4933         default:
4934           gcc_unreachable ();
4935         }
4936
4937       if (non_constant_p
4938           && cp_parser_non_integral_constant_expression (parser,
4939                                                          non_constant_p))
4940         expression = error_mark_node;
4941
4942       return expression;
4943     }
4944
4945   return cp_parser_postfix_expression (parser, address_p, cast_p);
4946 }
4947
4948 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4949    unary-operator, the corresponding tree code is returned.  */
4950
4951 static enum tree_code
4952 cp_parser_unary_operator (cp_token* token)
4953 {
4954   switch (token->type)
4955     {
4956     case CPP_MULT:
4957       return INDIRECT_REF;
4958
4959     case CPP_AND:
4960       return ADDR_EXPR;
4961
4962     case CPP_PLUS:
4963       return UNARY_PLUS_EXPR;
4964
4965     case CPP_MINUS:
4966       return NEGATE_EXPR;
4967
4968     case CPP_NOT:
4969       return TRUTH_NOT_EXPR;
4970
4971     case CPP_COMPL:
4972       return BIT_NOT_EXPR;
4973
4974     default:
4975       return ERROR_MARK;
4976     }
4977 }
4978
4979 /* Parse a new-expression.
4980
4981    new-expression:
4982      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4983      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4984
4985    Returns a representation of the expression.  */
4986
4987 static tree
4988 cp_parser_new_expression (cp_parser* parser)
4989 {
4990   bool global_scope_p;
4991   tree placement;
4992   tree type;
4993   tree initializer;
4994   tree nelts;
4995
4996   /* Look for the optional `::' operator.  */
4997   global_scope_p
4998     = (cp_parser_global_scope_opt (parser,
4999                                    /*current_scope_valid_p=*/false)
5000        != NULL_TREE);
5001   /* Look for the `new' operator.  */
5002   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5003   /* There's no easy way to tell a new-placement from the
5004      `( type-id )' construct.  */
5005   cp_parser_parse_tentatively (parser);
5006   /* Look for a new-placement.  */
5007   placement = cp_parser_new_placement (parser);
5008   /* If that didn't work out, there's no new-placement.  */
5009   if (!cp_parser_parse_definitely (parser))
5010     placement = NULL_TREE;
5011
5012   /* If the next token is a `(', then we have a parenthesized
5013      type-id.  */
5014   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5015     {
5016       /* Consume the `('.  */
5017       cp_lexer_consume_token (parser->lexer);
5018       /* Parse the type-id.  */
5019       type = cp_parser_type_id (parser);
5020       /* Look for the closing `)'.  */
5021       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5022       /* There should not be a direct-new-declarator in this production,
5023          but GCC used to allowed this, so we check and emit a sensible error
5024          message for this case.  */
5025       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5026         {
5027           error ("array bound forbidden after parenthesized type-id");
5028           inform ("try removing the parentheses around the type-id");
5029           cp_parser_direct_new_declarator (parser);
5030         }
5031       nelts = NULL_TREE;
5032     }
5033   /* Otherwise, there must be a new-type-id.  */
5034   else
5035     type = cp_parser_new_type_id (parser, &nelts);
5036
5037   /* If the next token is a `(', then we have a new-initializer.  */
5038   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5039     initializer = cp_parser_new_initializer (parser);
5040   else
5041     initializer = NULL_TREE;
5042
5043   /* A new-expression may not appear in an integral constant
5044      expression.  */
5045   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5046     return error_mark_node;
5047
5048   /* Create a representation of the new-expression.  */
5049   return build_new (placement, type, nelts, initializer, global_scope_p);
5050 }
5051
5052 /* Parse a new-placement.
5053
5054    new-placement:
5055      ( expression-list )
5056
5057    Returns the same representation as for an expression-list.  */
5058
5059 static tree
5060 cp_parser_new_placement (cp_parser* parser)
5061 {
5062   tree expression_list;
5063
5064   /* Parse the expression-list.  */
5065   expression_list = (cp_parser_parenthesized_expression_list
5066                      (parser, false, /*cast_p=*/false,
5067                       /*non_constant_p=*/NULL));
5068
5069   return expression_list;
5070 }
5071
5072 /* Parse a new-type-id.
5073
5074    new-type-id:
5075      type-specifier-seq new-declarator [opt]
5076
5077    Returns the TYPE allocated.  If the new-type-id indicates an array
5078    type, *NELTS is set to the number of elements in the last array
5079    bound; the TYPE will not include the last array bound.  */
5080
5081 static tree
5082 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5083 {
5084   cp_decl_specifier_seq type_specifier_seq;
5085   cp_declarator *new_declarator;
5086   cp_declarator *declarator;
5087   cp_declarator *outer_declarator;
5088   const char *saved_message;
5089   tree type;
5090
5091   /* The type-specifier sequence must not contain type definitions.
5092      (It cannot contain declarations of new types either, but if they
5093      are not definitions we will catch that because they are not
5094      complete.)  */
5095   saved_message = parser->type_definition_forbidden_message;
5096   parser->type_definition_forbidden_message
5097     = "types may not be defined in a new-type-id";
5098   /* Parse the type-specifier-seq.  */
5099   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5100                                 &type_specifier_seq);
5101   /* Restore the old message.  */
5102   parser->type_definition_forbidden_message = saved_message;
5103   /* Parse the new-declarator.  */
5104   new_declarator = cp_parser_new_declarator_opt (parser);
5105
5106   /* Determine the number of elements in the last array dimension, if
5107      any.  */
5108   *nelts = NULL_TREE;
5109   /* Skip down to the last array dimension.  */
5110   declarator = new_declarator;
5111   outer_declarator = NULL;
5112   while (declarator && (declarator->kind == cdk_pointer
5113                         || declarator->kind == cdk_ptrmem))
5114     {
5115       outer_declarator = declarator;
5116       declarator = declarator->declarator;
5117     }
5118   while (declarator
5119          && declarator->kind == cdk_array
5120          && declarator->declarator
5121          && declarator->declarator->kind == cdk_array)
5122     {
5123       outer_declarator = declarator;
5124       declarator = declarator->declarator;
5125     }
5126
5127   if (declarator && declarator->kind == cdk_array)
5128     {
5129       *nelts = declarator->u.array.bounds;
5130       if (*nelts == error_mark_node)
5131         *nelts = integer_one_node;
5132
5133       if (outer_declarator)
5134         outer_declarator->declarator = declarator->declarator;
5135       else
5136         new_declarator = NULL;
5137     }
5138
5139   type = groktypename (&type_specifier_seq, new_declarator);
5140   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5141     {
5142       *nelts = array_type_nelts_top (type);
5143       type = TREE_TYPE (type);
5144     }
5145   return type;
5146 }
5147
5148 /* Parse an (optional) new-declarator.
5149
5150    new-declarator:
5151      ptr-operator new-declarator [opt]
5152      direct-new-declarator
5153
5154    Returns the declarator.  */
5155
5156 static cp_declarator *
5157 cp_parser_new_declarator_opt (cp_parser* parser)
5158 {
5159   enum tree_code code;
5160   tree type;
5161   cp_cv_quals cv_quals;
5162
5163   /* We don't know if there's a ptr-operator next, or not.  */
5164   cp_parser_parse_tentatively (parser);
5165   /* Look for a ptr-operator.  */
5166   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5167   /* If that worked, look for more new-declarators.  */
5168   if (cp_parser_parse_definitely (parser))
5169     {
5170       cp_declarator *declarator;
5171
5172       /* Parse another optional declarator.  */
5173       declarator = cp_parser_new_declarator_opt (parser);
5174
5175       /* Create the representation of the declarator.  */
5176       if (type)
5177         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5178       else if (code == INDIRECT_REF)
5179         declarator = make_pointer_declarator (cv_quals, declarator);
5180       else
5181         declarator = make_reference_declarator (cv_quals, declarator);
5182
5183       return declarator;
5184     }
5185
5186   /* If the next token is a `[', there is a direct-new-declarator.  */
5187   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5188     return cp_parser_direct_new_declarator (parser);
5189
5190   return NULL;
5191 }
5192
5193 /* Parse a direct-new-declarator.
5194
5195    direct-new-declarator:
5196      [ expression ]
5197      direct-new-declarator [constant-expression]
5198
5199    */
5200
5201 static cp_declarator *
5202 cp_parser_direct_new_declarator (cp_parser* parser)
5203 {
5204   cp_declarator *declarator = NULL;
5205
5206   while (true)
5207     {
5208       tree expression;
5209
5210       /* Look for the opening `['.  */
5211       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5212       /* The first expression is not required to be constant.  */
5213       if (!declarator)
5214         {
5215           expression = cp_parser_expression (parser, /*cast_p=*/false);
5216           /* The standard requires that the expression have integral
5217              type.  DR 74 adds enumeration types.  We believe that the
5218              real intent is that these expressions be handled like the
5219              expression in a `switch' condition, which also allows
5220              classes with a single conversion to integral or
5221              enumeration type.  */
5222           if (!processing_template_decl)
5223             {
5224               expression
5225                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5226                                               expression,
5227                                               /*complain=*/true);
5228               if (!expression)
5229                 {
5230                   error ("expression in new-declarator must have integral "
5231                          "or enumeration type");
5232                   expression = error_mark_node;
5233                 }
5234             }
5235         }
5236       /* But all the other expressions must be.  */
5237       else
5238         expression
5239           = cp_parser_constant_expression (parser,
5240                                            /*allow_non_constant=*/false,
5241                                            NULL);
5242       /* Look for the closing `]'.  */
5243       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5244
5245       /* Add this bound to the declarator.  */
5246       declarator = make_array_declarator (declarator, expression);
5247
5248       /* If the next token is not a `[', then there are no more
5249          bounds.  */
5250       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5251         break;
5252     }
5253
5254   return declarator;
5255 }
5256
5257 /* Parse a new-initializer.
5258
5259    new-initializer:
5260      ( expression-list [opt] )
5261
5262    Returns a representation of the expression-list.  If there is no
5263    expression-list, VOID_ZERO_NODE is returned.  */
5264
5265 static tree
5266 cp_parser_new_initializer (cp_parser* parser)
5267 {
5268   tree expression_list;
5269
5270   expression_list = (cp_parser_parenthesized_expression_list
5271                      (parser, false, /*cast_p=*/false,
5272                       /*non_constant_p=*/NULL));
5273   if (!expression_list)
5274     expression_list = void_zero_node;
5275
5276   return expression_list;
5277 }
5278
5279 /* Parse a delete-expression.
5280
5281    delete-expression:
5282      :: [opt] delete cast-expression
5283      :: [opt] delete [ ] cast-expression
5284
5285    Returns a representation of the expression.  */
5286
5287 static tree
5288 cp_parser_delete_expression (cp_parser* parser)
5289 {
5290   bool global_scope_p;
5291   bool array_p;
5292   tree expression;
5293
5294   /* Look for the optional `::' operator.  */
5295   global_scope_p
5296     = (cp_parser_global_scope_opt (parser,
5297                                    /*current_scope_valid_p=*/false)
5298        != NULL_TREE);
5299   /* Look for the `delete' keyword.  */
5300   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5301   /* See if the array syntax is in use.  */
5302   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5303     {
5304       /* Consume the `[' token.  */
5305       cp_lexer_consume_token (parser->lexer);
5306       /* Look for the `]' token.  */
5307       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5308       /* Remember that this is the `[]' construct.  */
5309       array_p = true;
5310     }
5311   else
5312     array_p = false;
5313
5314   /* Parse the cast-expression.  */
5315   expression = cp_parser_simple_cast_expression (parser);
5316
5317   /* A delete-expression may not appear in an integral constant
5318      expression.  */
5319   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5320     return error_mark_node;
5321
5322   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5323 }
5324
5325 /* Parse a cast-expression.
5326
5327    cast-expression:
5328      unary-expression
5329      ( type-id ) cast-expression
5330
5331    ADDRESS_P is true iff the unary-expression is appearing as the
5332    operand of the `&' operator.   CAST_P is true if this expression is
5333    the target of a cast.
5334
5335    Returns a representation of the expression.  */
5336
5337 static tree
5338 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5339 {
5340   /* If it's a `(', then we might be looking at a cast.  */
5341   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5342     {
5343       tree type = NULL_TREE;
5344       tree expr = NULL_TREE;
5345       bool compound_literal_p;
5346       const char *saved_message;
5347
5348       /* There's no way to know yet whether or not this is a cast.
5349          For example, `(int (3))' is a unary-expression, while `(int)
5350          3' is a cast.  So, we resort to parsing tentatively.  */
5351       cp_parser_parse_tentatively (parser);
5352       /* Types may not be defined in a cast.  */
5353       saved_message = parser->type_definition_forbidden_message;
5354       parser->type_definition_forbidden_message
5355         = "types may not be defined in casts";
5356       /* Consume the `('.  */
5357       cp_lexer_consume_token (parser->lexer);
5358       /* A very tricky bit is that `(struct S) { 3 }' is a
5359          compound-literal (which we permit in C++ as an extension).
5360          But, that construct is not a cast-expression -- it is a
5361          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5362          is legal; if the compound-literal were a cast-expression,
5363          you'd need an extra set of parentheses.)  But, if we parse
5364          the type-id, and it happens to be a class-specifier, then we
5365          will commit to the parse at that point, because we cannot
5366          undo the action that is done when creating a new class.  So,
5367          then we cannot back up and do a postfix-expression.
5368
5369          Therefore, we scan ahead to the closing `)', and check to see
5370          if the token after the `)' is a `{'.  If so, we are not
5371          looking at a cast-expression.
5372
5373          Save tokens so that we can put them back.  */
5374       cp_lexer_save_tokens (parser->lexer);
5375       /* Skip tokens until the next token is a closing parenthesis.
5376          If we find the closing `)', and the next token is a `{', then
5377          we are looking at a compound-literal.  */
5378       compound_literal_p
5379         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5380                                                   /*consume_paren=*/true)
5381            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5382       /* Roll back the tokens we skipped.  */
5383       cp_lexer_rollback_tokens (parser->lexer);
5384       /* If we were looking at a compound-literal, simulate an error
5385          so that the call to cp_parser_parse_definitely below will
5386          fail.  */
5387       if (compound_literal_p)
5388         cp_parser_simulate_error (parser);
5389       else
5390         {
5391           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5392           parser->in_type_id_in_expr_p = true;
5393           /* Look for the type-id.  */
5394           type = cp_parser_type_id (parser);
5395           /* Look for the closing `)'.  */
5396           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5397           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5398         }
5399
5400       /* Restore the saved message.  */
5401       parser->type_definition_forbidden_message = saved_message;
5402
5403       /* If ok so far, parse the dependent expression. We cannot be
5404          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5405          ctor of T, but looks like a cast to function returning T
5406          without a dependent expression.  */
5407       if (!cp_parser_error_occurred (parser))
5408         expr = cp_parser_cast_expression (parser,
5409                                           /*address_p=*/false,
5410                                           /*cast_p=*/true);
5411
5412       if (cp_parser_parse_definitely (parser))
5413         {
5414           /* Warn about old-style casts, if so requested.  */
5415           if (warn_old_style_cast
5416               && !in_system_header
5417               && !VOID_TYPE_P (type)
5418               && current_lang_name != lang_name_c)
5419             warning (OPT_Wold_style_cast, "use of old-style cast");
5420
5421           /* Only type conversions to integral or enumeration types
5422              can be used in constant-expressions.  */
5423           if (parser->integral_constant_expression_p
5424               && !dependent_type_p (type)
5425               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5426               && (cp_parser_non_integral_constant_expression
5427                   (parser,
5428                    "a cast to a type other than an integral or "
5429                    "enumeration type")))
5430             return error_mark_node;
5431
5432           /* Perform the cast.  */
5433           expr = build_c_cast (type, expr);
5434           return expr;
5435         }
5436     }
5437
5438   /* If we get here, then it's not a cast, so it must be a
5439      unary-expression.  */
5440   return cp_parser_unary_expression (parser, address_p, cast_p);
5441 }
5442
5443 /* Parse a binary expression of the general form:
5444
5445    pm-expression:
5446      cast-expression
5447      pm-expression .* cast-expression
5448      pm-expression ->* cast-expression
5449
5450    multiplicative-expression:
5451      pm-expression
5452      multiplicative-expression * pm-expression
5453      multiplicative-expression / pm-expression
5454      multiplicative-expression % pm-expression
5455
5456    additive-expression:
5457      multiplicative-expression
5458      additive-expression + multiplicative-expression
5459      additive-expression - multiplicative-expression
5460
5461    shift-expression:
5462      additive-expression
5463      shift-expression << additive-expression
5464      shift-expression >> additive-expression
5465
5466    relational-expression:
5467      shift-expression
5468      relational-expression < shift-expression
5469      relational-expression > shift-expression
5470      relational-expression <= shift-expression
5471      relational-expression >= shift-expression
5472
5473   GNU Extension:
5474
5475    relational-expression:
5476      relational-expression <? shift-expression
5477      relational-expression >? shift-expression
5478
5479    equality-expression:
5480      relational-expression
5481      equality-expression == relational-expression
5482      equality-expression != relational-expression
5483
5484    and-expression:
5485      equality-expression
5486      and-expression & equality-expression
5487
5488    exclusive-or-expression:
5489      and-expression
5490      exclusive-or-expression ^ and-expression
5491
5492    inclusive-or-expression:
5493      exclusive-or-expression
5494      inclusive-or-expression | exclusive-or-expression
5495
5496    logical-and-expression:
5497      inclusive-or-expression
5498      logical-and-expression && inclusive-or-expression
5499
5500    logical-or-expression:
5501      logical-and-expression
5502      logical-or-expression || logical-and-expression
5503
5504    All these are implemented with a single function like:
5505
5506    binary-expression:
5507      simple-cast-expression
5508      binary-expression <token> binary-expression
5509
5510    CAST_P is true if this expression is the target of a cast.
5511
5512    The binops_by_token map is used to get the tree codes for each <token> type.
5513    binary-expressions are associated according to a precedence table.  */
5514
5515 #define TOKEN_PRECEDENCE(token) \
5516   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5517    ? PREC_NOT_OPERATOR \
5518    : binops_by_token[token->type].prec)
5519
5520 static tree
5521 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5522 {
5523   cp_parser_expression_stack stack;
5524   cp_parser_expression_stack_entry *sp = &stack[0];
5525   tree lhs, rhs;
5526   cp_token *token;
5527   enum tree_code tree_type;
5528   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5529   bool overloaded_p;
5530
5531   /* Parse the first expression.  */
5532   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5533
5534   for (;;)
5535     {
5536       /* Get an operator token.  */
5537       token = cp_lexer_peek_token (parser->lexer);
5538       if (token->type == CPP_MIN || token->type == CPP_MAX)
5539         cp_parser_warn_min_max ();
5540
5541       new_prec = TOKEN_PRECEDENCE (token);
5542
5543       /* Popping an entry off the stack means we completed a subexpression:
5544          - either we found a token which is not an operator (`>' where it is not
5545            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5546            will happen repeatedly;
5547          - or, we found an operator which has lower priority.  This is the case
5548            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5549            parsing `3 * 4'.  */
5550       if (new_prec <= prec)
5551         {
5552           if (sp == stack)
5553             break;
5554           else
5555             goto pop;
5556         }
5557
5558      get_rhs:
5559       tree_type = binops_by_token[token->type].tree_type;
5560
5561       /* We used the operator token.  */
5562       cp_lexer_consume_token (parser->lexer);
5563
5564       /* Extract another operand.  It may be the RHS of this expression
5565          or the LHS of a new, higher priority expression.  */
5566       rhs = cp_parser_simple_cast_expression (parser);
5567
5568       /* Get another operator token.  Look up its precedence to avoid
5569          building a useless (immediately popped) stack entry for common
5570          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5571       token = cp_lexer_peek_token (parser->lexer);
5572       lookahead_prec = TOKEN_PRECEDENCE (token);
5573       if (lookahead_prec > new_prec)
5574         {
5575           /* ... and prepare to parse the RHS of the new, higher priority
5576              expression.  Since precedence levels on the stack are
5577              monotonically increasing, we do not have to care about
5578              stack overflows.  */
5579           sp->prec = prec;
5580           sp->tree_type = tree_type;
5581           sp->lhs = lhs;
5582           sp++;
5583           lhs = rhs;
5584           prec = new_prec;
5585           new_prec = lookahead_prec;
5586           goto get_rhs;
5587
5588          pop:
5589           /* If the stack is not empty, we have parsed into LHS the right side
5590              (`4' in the example above) of an expression we had suspended.
5591              We can use the information on the stack to recover the LHS (`3')
5592              from the stack together with the tree code (`MULT_EXPR'), and
5593              the precedence of the higher level subexpression
5594              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5595              which will be used to actually build the additive expression.  */
5596           --sp;
5597           prec = sp->prec;
5598           tree_type = sp->tree_type;
5599           rhs = lhs;
5600           lhs = sp->lhs;
5601         }
5602
5603       overloaded_p = false;
5604       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5605
5606       /* If the binary operator required the use of an overloaded operator,
5607          then this expression cannot be an integral constant-expression.
5608          An overloaded operator can be used even if both operands are
5609          otherwise permissible in an integral constant-expression if at
5610          least one of the operands is of enumeration type.  */
5611
5612       if (overloaded_p
5613           && (cp_parser_non_integral_constant_expression
5614               (parser, "calls to overloaded operators")))
5615         return error_mark_node;
5616     }
5617
5618   return lhs;
5619 }
5620
5621
5622 /* Parse the `? expression : assignment-expression' part of a
5623    conditional-expression.  The LOGICAL_OR_EXPR is the
5624    logical-or-expression that started the conditional-expression.
5625    Returns a representation of the entire conditional-expression.
5626
5627    This routine is used by cp_parser_assignment_expression.
5628
5629      ? expression : assignment-expression
5630
5631    GNU Extensions:
5632
5633      ? : assignment-expression */
5634
5635 static tree
5636 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5637 {
5638   tree expr;
5639   tree assignment_expr;
5640
5641   /* Consume the `?' token.  */
5642   cp_lexer_consume_token (parser->lexer);
5643   if (cp_parser_allow_gnu_extensions_p (parser)
5644       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5645     /* Implicit true clause.  */
5646     expr = NULL_TREE;
5647   else
5648     /* Parse the expression.  */
5649     expr = cp_parser_expression (parser, /*cast_p=*/false);
5650
5651   /* The next token should be a `:'.  */
5652   cp_parser_require (parser, CPP_COLON, "`:'");
5653   /* Parse the assignment-expression.  */
5654   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5655
5656   /* Build the conditional-expression.  */
5657   return build_x_conditional_expr (logical_or_expr,
5658                                    expr,
5659                                    assignment_expr);
5660 }
5661
5662 /* Parse an assignment-expression.
5663
5664    assignment-expression:
5665      conditional-expression
5666      logical-or-expression assignment-operator assignment_expression
5667      throw-expression
5668
5669    CAST_P is true if this expression is the target of a cast.
5670
5671    Returns a representation for the expression.  */
5672
5673 static tree
5674 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5675 {
5676   tree expr;
5677
5678   /* If the next token is the `throw' keyword, then we're looking at
5679      a throw-expression.  */
5680   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5681     expr = cp_parser_throw_expression (parser);
5682   /* Otherwise, it must be that we are looking at a
5683      logical-or-expression.  */
5684   else
5685     {
5686       /* Parse the binary expressions (logical-or-expression).  */
5687       expr = cp_parser_binary_expression (parser, cast_p);
5688       /* If the next token is a `?' then we're actually looking at a
5689          conditional-expression.  */
5690       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5691         return cp_parser_question_colon_clause (parser, expr);
5692       else
5693         {
5694           enum tree_code assignment_operator;
5695
5696           /* If it's an assignment-operator, we're using the second
5697              production.  */
5698           assignment_operator
5699             = cp_parser_assignment_operator_opt (parser);
5700           if (assignment_operator != ERROR_MARK)
5701             {
5702               tree rhs;
5703
5704               /* Parse the right-hand side of the assignment.  */
5705               rhs = cp_parser_assignment_expression (parser, cast_p);
5706               /* An assignment may not appear in a
5707                  constant-expression.  */
5708               if (cp_parser_non_integral_constant_expression (parser,
5709                                                               "an assignment"))
5710                 return error_mark_node;
5711               /* Build the assignment expression.  */
5712               expr = build_x_modify_expr (expr,
5713                                           assignment_operator,
5714                                           rhs);
5715             }
5716         }
5717     }
5718
5719   return expr;
5720 }
5721
5722 /* Parse an (optional) assignment-operator.
5723
5724    assignment-operator: one of
5725      = *= /= %= += -= >>= <<= &= ^= |=
5726
5727    GNU Extension:
5728
5729    assignment-operator: one of
5730      <?= >?=
5731
5732    If the next token is an assignment operator, the corresponding tree
5733    code is returned, and the token is consumed.  For example, for
5734    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5735    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5736    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5737    operator, ERROR_MARK is returned.  */
5738
5739 static enum tree_code
5740 cp_parser_assignment_operator_opt (cp_parser* parser)
5741 {
5742   enum tree_code op;
5743   cp_token *token;
5744
5745   /* Peek at the next toen.  */
5746   token = cp_lexer_peek_token (parser->lexer);
5747
5748   switch (token->type)
5749     {
5750     case CPP_EQ:
5751       op = NOP_EXPR;
5752       break;
5753
5754     case CPP_MULT_EQ:
5755       op = MULT_EXPR;
5756       break;
5757
5758     case CPP_DIV_EQ:
5759       op = TRUNC_DIV_EXPR;
5760       break;
5761
5762     case CPP_MOD_EQ:
5763       op = TRUNC_MOD_EXPR;
5764       break;
5765
5766     case CPP_PLUS_EQ:
5767       op = PLUS_EXPR;
5768       break;
5769
5770     case CPP_MINUS_EQ:
5771       op = MINUS_EXPR;
5772       break;
5773
5774     case CPP_RSHIFT_EQ:
5775       op = RSHIFT_EXPR;
5776       break;
5777
5778     case CPP_LSHIFT_EQ:
5779       op = LSHIFT_EXPR;
5780       break;
5781
5782     case CPP_AND_EQ:
5783       op = BIT_AND_EXPR;
5784       break;
5785
5786     case CPP_XOR_EQ:
5787       op = BIT_XOR_EXPR;
5788       break;
5789
5790     case CPP_OR_EQ:
5791       op = BIT_IOR_EXPR;
5792       break;
5793
5794     case CPP_MIN_EQ:
5795       op = MIN_EXPR;
5796       cp_parser_warn_min_max ();
5797       break;
5798
5799     case CPP_MAX_EQ:
5800       op = MAX_EXPR;
5801       cp_parser_warn_min_max ();
5802       break;
5803
5804     default:
5805       /* Nothing else is an assignment operator.  */
5806       op = ERROR_MARK;
5807     }
5808
5809   /* If it was an assignment operator, consume it.  */
5810   if (op != ERROR_MARK)
5811     cp_lexer_consume_token (parser->lexer);
5812
5813   return op;
5814 }
5815
5816 /* Parse an expression.
5817
5818    expression:
5819      assignment-expression
5820      expression , assignment-expression
5821
5822    CAST_P is true if this expression is the target of a cast.
5823
5824    Returns a representation of the expression.  */
5825
5826 static tree
5827 cp_parser_expression (cp_parser* parser, bool cast_p)
5828 {
5829   tree expression = NULL_TREE;
5830
5831   while (true)
5832     {
5833       tree assignment_expression;
5834
5835       /* Parse the next assignment-expression.  */
5836       assignment_expression
5837         = cp_parser_assignment_expression (parser, cast_p);
5838       /* If this is the first assignment-expression, we can just
5839          save it away.  */
5840       if (!expression)
5841         expression = assignment_expression;
5842       else
5843         expression = build_x_compound_expr (expression,
5844                                             assignment_expression);
5845       /* If the next token is not a comma, then we are done with the
5846          expression.  */
5847       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5848         break;
5849       /* Consume the `,'.  */
5850       cp_lexer_consume_token (parser->lexer);
5851       /* A comma operator cannot appear in a constant-expression.  */
5852       if (cp_parser_non_integral_constant_expression (parser,
5853                                                       "a comma operator"))
5854         expression = error_mark_node;
5855     }
5856
5857   return expression;
5858 }
5859
5860 /* Parse a constant-expression.
5861
5862    constant-expression:
5863      conditional-expression
5864
5865   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5866   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5867   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5868   is false, NON_CONSTANT_P should be NULL.  */
5869
5870 static tree
5871 cp_parser_constant_expression (cp_parser* parser,
5872                                bool allow_non_constant_p,
5873                                bool *non_constant_p)
5874 {
5875   bool saved_integral_constant_expression_p;
5876   bool saved_allow_non_integral_constant_expression_p;
5877   bool saved_non_integral_constant_expression_p;
5878   tree expression;
5879
5880   /* It might seem that we could simply parse the
5881      conditional-expression, and then check to see if it were
5882      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5883      one that the compiler can figure out is constant, possibly after
5884      doing some simplifications or optimizations.  The standard has a
5885      precise definition of constant-expression, and we must honor
5886      that, even though it is somewhat more restrictive.
5887
5888      For example:
5889
5890        int i[(2, 3)];
5891
5892      is not a legal declaration, because `(2, 3)' is not a
5893      constant-expression.  The `,' operator is forbidden in a
5894      constant-expression.  However, GCC's constant-folding machinery
5895      will fold this operation to an INTEGER_CST for `3'.  */
5896
5897   /* Save the old settings.  */
5898   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5899   saved_allow_non_integral_constant_expression_p
5900     = parser->allow_non_integral_constant_expression_p;
5901   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5902   /* We are now parsing a constant-expression.  */
5903   parser->integral_constant_expression_p = true;
5904   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5905   parser->non_integral_constant_expression_p = false;
5906   /* Although the grammar says "conditional-expression", we parse an
5907      "assignment-expression", which also permits "throw-expression"
5908      and the use of assignment operators.  In the case that
5909      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5910      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5911      actually essential that we look for an assignment-expression.
5912      For example, cp_parser_initializer_clauses uses this function to
5913      determine whether a particular assignment-expression is in fact
5914      constant.  */
5915   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5916   /* Restore the old settings.  */
5917   parser->integral_constant_expression_p
5918     = saved_integral_constant_expression_p;
5919   parser->allow_non_integral_constant_expression_p
5920     = saved_allow_non_integral_constant_expression_p;
5921   if (allow_non_constant_p)
5922     *non_constant_p = parser->non_integral_constant_expression_p;
5923   else if (parser->non_integral_constant_expression_p)
5924     expression = error_mark_node;
5925   parser->non_integral_constant_expression_p
5926     = saved_non_integral_constant_expression_p;
5927
5928   return expression;
5929 }
5930
5931 /* Parse __builtin_offsetof.
5932
5933    offsetof-expression:
5934      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5935
5936    offsetof-member-designator:
5937      id-expression
5938      | offsetof-member-designator "." id-expression
5939      | offsetof-member-designator "[" expression "]"
5940 */
5941
5942 static tree
5943 cp_parser_builtin_offsetof (cp_parser *parser)
5944 {
5945   int save_ice_p, save_non_ice_p;
5946   tree type, expr;
5947   cp_id_kind dummy;
5948
5949   /* We're about to accept non-integral-constant things, but will
5950      definitely yield an integral constant expression.  Save and
5951      restore these values around our local parsing.  */
5952   save_ice_p = parser->integral_constant_expression_p;
5953   save_non_ice_p = parser->non_integral_constant_expression_p;
5954
5955   /* Consume the "__builtin_offsetof" token.  */
5956   cp_lexer_consume_token (parser->lexer);
5957   /* Consume the opening `('.  */
5958   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5959   /* Parse the type-id.  */
5960   type = cp_parser_type_id (parser);
5961   /* Look for the `,'.  */
5962   cp_parser_require (parser, CPP_COMMA, "`,'");
5963
5964   /* Build the (type *)null that begins the traditional offsetof macro.  */
5965   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5966
5967   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5968   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5969                                                  true, &dummy);
5970   while (true)
5971     {
5972       cp_token *token = cp_lexer_peek_token (parser->lexer);
5973       switch (token->type)
5974         {
5975         case CPP_OPEN_SQUARE:
5976           /* offsetof-member-designator "[" expression "]" */
5977           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5978           break;
5979
5980         case CPP_DOT:
5981           /* offsetof-member-designator "." identifier */
5982           cp_lexer_consume_token (parser->lexer);
5983           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5984                                                          true, &dummy);
5985           break;
5986
5987         case CPP_CLOSE_PAREN:
5988           /* Consume the ")" token.  */
5989           cp_lexer_consume_token (parser->lexer);
5990           goto success;
5991
5992         default:
5993           /* Error.  We know the following require will fail, but
5994              that gives the proper error message.  */
5995           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5996           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5997           expr = error_mark_node;
5998           goto failure;
5999         }
6000     }
6001
6002  success:
6003   /* If we're processing a template, we can't finish the semantics yet.
6004      Otherwise we can fold the entire expression now.  */
6005   if (processing_template_decl)
6006     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6007   else
6008     expr = fold_offsetof (expr);
6009
6010  failure:
6011   parser->integral_constant_expression_p = save_ice_p;
6012   parser->non_integral_constant_expression_p = save_non_ice_p;
6013
6014   return expr;
6015 }
6016
6017 /* Statements [gram.stmt.stmt]  */
6018
6019 /* Parse a statement.
6020
6021    statement:
6022      labeled-statement
6023      expression-statement
6024      compound-statement
6025      selection-statement
6026      iteration-statement
6027      jump-statement
6028      declaration-statement
6029      try-block
6030
6031   IN_COMPOUND is true when the statement is nested inside a 
6032   cp_parser_compound_statement; this matters for certain pragmas.  */
6033
6034 static void
6035 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6036                      bool in_compound)
6037 {
6038   tree statement;
6039   cp_token *token;
6040   location_t statement_location;
6041
6042  restart:
6043   /* There is no statement yet.  */
6044   statement = NULL_TREE;
6045   /* Peek at the next token.  */
6046   token = cp_lexer_peek_token (parser->lexer);
6047   /* Remember the location of the first token in the statement.  */
6048   statement_location = token->location;
6049   /* If this is a keyword, then that will often determine what kind of
6050      statement we have.  */
6051   if (token->type == CPP_KEYWORD)
6052     {
6053       enum rid keyword = token->keyword;
6054
6055       switch (keyword)
6056         {
6057         case RID_CASE:
6058         case RID_DEFAULT:
6059           statement = cp_parser_labeled_statement (parser, in_statement_expr,
6060                                                    in_compound);
6061           break;
6062
6063         case RID_IF:
6064         case RID_SWITCH:
6065           statement = cp_parser_selection_statement (parser);
6066           break;
6067
6068         case RID_WHILE:
6069         case RID_DO:
6070         case RID_FOR:
6071           statement = cp_parser_iteration_statement (parser);
6072           break;
6073
6074         case RID_BREAK:
6075         case RID_CONTINUE:
6076         case RID_RETURN:
6077         case RID_GOTO:
6078           statement = cp_parser_jump_statement (parser);
6079           break;
6080
6081           /* Objective-C++ exception-handling constructs.  */
6082         case RID_AT_TRY:
6083         case RID_AT_CATCH:
6084         case RID_AT_FINALLY:
6085         case RID_AT_SYNCHRONIZED:
6086         case RID_AT_THROW:
6087           statement = cp_parser_objc_statement (parser);
6088           break;
6089
6090         case RID_TRY:
6091           statement = cp_parser_try_block (parser);
6092           break;
6093
6094         default:
6095           /* It might be a keyword like `int' that can start a
6096              declaration-statement.  */
6097           break;
6098         }
6099     }
6100   else if (token->type == CPP_NAME)
6101     {
6102       /* If the next token is a `:', then we are looking at a
6103          labeled-statement.  */
6104       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6105       if (token->type == CPP_COLON)
6106         statement = cp_parser_labeled_statement (parser, in_statement_expr,
6107                                                  in_compound);
6108     }
6109   /* Anything that starts with a `{' must be a compound-statement.  */
6110   else if (token->type == CPP_OPEN_BRACE)
6111     statement = cp_parser_compound_statement (parser, NULL, false);
6112   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6113      a statement all its own.  */
6114   else if (token->type == CPP_PRAGMA)
6115     {
6116       /* Only certain OpenMP pragmas are attached to statements, and thus
6117          are considered statements themselves.  All others are not.  In
6118          the context of a compound, accept the pragma as a "statement" and
6119          return so that we can check for a close brace.  Otherwise we 
6120          require a real statement and must go back and read one.  */
6121       if (in_compound)
6122         cp_parser_pragma (parser, pragma_compound);
6123       else if (!cp_parser_pragma (parser, pragma_stmt))
6124         goto restart;
6125       return;
6126     }
6127   else if (token->type == CPP_EOF)
6128     {
6129       cp_parser_error (parser, "expected statement");
6130       return;
6131     }
6132
6133   /* Everything else must be a declaration-statement or an
6134      expression-statement.  Try for the declaration-statement
6135      first, unless we are looking at a `;', in which case we know that
6136      we have an expression-statement.  */
6137   if (!statement)
6138     {
6139       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6140         {
6141           cp_parser_parse_tentatively (parser);
6142           /* Try to parse the declaration-statement.  */
6143           cp_parser_declaration_statement (parser);
6144           /* If that worked, we're done.  */
6145           if (cp_parser_parse_definitely (parser))
6146             return;
6147         }
6148       /* Look for an expression-statement instead.  */
6149       statement = cp_parser_expression_statement (parser, in_statement_expr);
6150     }
6151
6152   /* Set the line number for the statement.  */
6153   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6154     SET_EXPR_LOCATION (statement, statement_location);
6155 }
6156
6157 /* Parse a labeled-statement.
6158
6159    labeled-statement:
6160      identifier : statement
6161      case constant-expression : statement
6162      default : statement
6163
6164    GNU Extension:
6165
6166    labeled-statement:
6167      case constant-expression ... constant-expression : statement
6168
6169    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6170    For an ordinary label, returns a LABEL_EXPR.
6171
6172    IN_COMPOUND is as for cp_parser_statement: true when we're nested
6173    inside a compound.  */
6174
6175 static tree
6176 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6177                              bool in_compound)
6178 {
6179   cp_token *token;
6180   tree statement = error_mark_node;
6181
6182   /* The next token should be an identifier.  */
6183   token = cp_lexer_peek_token (parser->lexer);
6184   if (token->type != CPP_NAME
6185       && token->type != CPP_KEYWORD)
6186     {
6187       cp_parser_error (parser, "expected labeled-statement");
6188       return error_mark_node;
6189     }
6190
6191   switch (token->keyword)
6192     {
6193     case RID_CASE:
6194       {
6195         tree expr, expr_hi;
6196         cp_token *ellipsis;
6197
6198         /* Consume the `case' token.  */
6199         cp_lexer_consume_token (parser->lexer);
6200         /* Parse the constant-expression.  */
6201         expr = cp_parser_constant_expression (parser,
6202                                               /*allow_non_constant_p=*/false,
6203                                               NULL);
6204
6205         ellipsis = cp_lexer_peek_token (parser->lexer);
6206         if (ellipsis->type == CPP_ELLIPSIS)
6207           {
6208             /* Consume the `...' token.  */
6209             cp_lexer_consume_token (parser->lexer);
6210             expr_hi =
6211               cp_parser_constant_expression (parser,
6212                                              /*allow_non_constant_p=*/false,
6213                                              NULL);
6214             /* We don't need to emit warnings here, as the common code
6215                will do this for us.  */
6216           }
6217         else
6218           expr_hi = NULL_TREE;
6219
6220         if (parser->in_switch_statement_p)
6221           statement = finish_case_label (expr, expr_hi);
6222         else
6223           error ("case label %qE not within a switch statement", expr);
6224       }
6225       break;
6226
6227     case RID_DEFAULT:
6228       /* Consume the `default' token.  */
6229       cp_lexer_consume_token (parser->lexer);
6230
6231       if (parser->in_switch_statement_p)
6232         statement = finish_case_label (NULL_TREE, NULL_TREE);
6233       else
6234         error ("case label not within a switch statement");
6235       break;
6236
6237     default:
6238       /* Anything else must be an ordinary label.  */
6239       statement = finish_label_stmt (cp_parser_identifier (parser));
6240       break;
6241     }
6242
6243   /* Require the `:' token.  */
6244   cp_parser_require (parser, CPP_COLON, "`:'");
6245   /* Parse the labeled statement.  */
6246   cp_parser_statement (parser, in_statement_expr, in_compound);
6247
6248   /* Return the label, in the case of a `case' or `default' label.  */
6249   return statement;
6250 }
6251
6252 /* Parse an expression-statement.
6253
6254    expression-statement:
6255      expression [opt] ;
6256
6257    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6258    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6259    indicates whether this expression-statement is part of an
6260    expression statement.  */
6261
6262 static tree
6263 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6264 {
6265   tree statement = NULL_TREE;
6266
6267   /* If the next token is a ';', then there is no expression
6268      statement.  */
6269   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6270     statement = cp_parser_expression (parser, /*cast_p=*/false);
6271
6272   /* Consume the final `;'.  */
6273   cp_parser_consume_semicolon_at_end_of_statement (parser);
6274
6275   if (in_statement_expr
6276       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6277     /* This is the final expression statement of a statement
6278        expression.  */
6279     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6280   else if (statement)
6281     statement = finish_expr_stmt (statement);
6282   else
6283     finish_stmt ();
6284
6285   return statement;
6286 }
6287
6288 /* Parse a compound-statement.
6289
6290    compound-statement:
6291      { statement-seq [opt] }
6292
6293    Returns a tree representing the statement.  */
6294
6295 static tree
6296 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6297                               bool in_try)
6298 {
6299   tree compound_stmt;
6300
6301   /* Consume the `{'.  */
6302   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6303     return error_mark_node;
6304   /* Begin the compound-statement.  */
6305   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6306   /* Parse an (optional) statement-seq.  */
6307   cp_parser_statement_seq_opt (parser, in_statement_expr);
6308   /* Finish the compound-statement.  */
6309   finish_compound_stmt (compound_stmt);
6310   /* Consume the `}'.  */
6311   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6312
6313   return compound_stmt;
6314 }
6315
6316 /* Parse an (optional) statement-seq.
6317
6318    statement-seq:
6319      statement
6320      statement-seq [opt] statement  */
6321
6322 static void
6323 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6324 {
6325   /* Scan statements until there aren't any more.  */
6326   while (true)
6327     {
6328       cp_token *token = cp_lexer_peek_token (parser->lexer);
6329
6330       /* If we're looking at a `}', then we've run out of statements.  */
6331       if (token->type == CPP_CLOSE_BRACE
6332           || token->type == CPP_EOF
6333           || token->type == CPP_PRAGMA_EOL)
6334         break;
6335
6336       /* Parse the statement.  */
6337       cp_parser_statement (parser, in_statement_expr, true);
6338     }
6339 }
6340
6341 /* Parse a selection-statement.
6342
6343    selection-statement:
6344      if ( condition ) statement
6345      if ( condition ) statement else statement
6346      switch ( condition ) statement
6347
6348    Returns the new IF_STMT or SWITCH_STMT.  */
6349
6350 static tree
6351 cp_parser_selection_statement (cp_parser* parser)
6352 {
6353   cp_token *token;
6354   enum rid keyword;
6355
6356   /* Peek at the next token.  */
6357   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6358
6359   /* See what kind of keyword it is.  */
6360   keyword = token->keyword;
6361   switch (keyword)
6362     {
6363     case RID_IF:
6364     case RID_SWITCH:
6365       {
6366         tree statement;
6367         tree condition;
6368
6369         /* Look for the `('.  */
6370         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6371           {
6372             cp_parser_skip_to_end_of_statement (parser);
6373             return error_mark_node;
6374           }
6375
6376         /* Begin the selection-statement.  */
6377         if (keyword == RID_IF)
6378           statement = begin_if_stmt ();
6379         else
6380           statement = begin_switch_stmt ();
6381
6382         /* Parse the condition.  */
6383         condition = cp_parser_condition (parser);
6384         /* Look for the `)'.  */
6385         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6386           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6387                                                  /*consume_paren=*/true);
6388
6389         if (keyword == RID_IF)
6390           {
6391             /* Add the condition.  */
6392             finish_if_stmt_cond (condition, statement);
6393
6394             /* Parse the then-clause.  */
6395             cp_parser_implicitly_scoped_statement (parser);
6396             finish_then_clause (statement);
6397
6398             /* If the next token is `else', parse the else-clause.  */
6399             if (cp_lexer_next_token_is_keyword (parser->lexer,
6400                                                 RID_ELSE))
6401               {
6402                 /* Consume the `else' keyword.  */
6403                 cp_lexer_consume_token (parser->lexer);
6404                 begin_else_clause (statement);
6405                 /* Parse the else-clause.  */
6406                 cp_parser_implicitly_scoped_statement (parser);
6407                 finish_else_clause (statement);
6408               }
6409
6410             /* Now we're all done with the if-statement.  */
6411             finish_if_stmt (statement);
6412           }
6413         else
6414           {
6415             bool in_switch_statement_p;
6416
6417             /* Add the condition.  */
6418             finish_switch_cond (condition, statement);
6419
6420             /* Parse the body of the switch-statement.  */
6421             in_switch_statement_p = parser->in_switch_statement_p;
6422             parser->in_switch_statement_p = true;
6423             cp_parser_implicitly_scoped_statement (parser);
6424             parser->in_switch_statement_p = in_switch_statement_p;
6425
6426             /* Now we're all done with the switch-statement.  */
6427             finish_switch_stmt (statement);
6428           }
6429
6430         return statement;
6431       }
6432       break;
6433
6434     default:
6435       cp_parser_error (parser, "expected selection-statement");
6436       return error_mark_node;
6437     }
6438 }
6439
6440 /* Parse a condition.
6441
6442    condition:
6443      expression
6444      type-specifier-seq declarator = assignment-expression
6445
6446    GNU Extension:
6447
6448    condition:
6449      type-specifier-seq declarator asm-specification [opt]
6450        attributes [opt] = assignment-expression
6451
6452    Returns the expression that should be tested.  */
6453
6454 static tree
6455 cp_parser_condition (cp_parser* parser)
6456 {
6457   cp_decl_specifier_seq type_specifiers;
6458   const char *saved_message;
6459
6460   /* Try the declaration first.  */
6461   cp_parser_parse_tentatively (parser);
6462   /* New types are not allowed in the type-specifier-seq for a
6463      condition.  */
6464   saved_message = parser->type_definition_forbidden_message;
6465   parser->type_definition_forbidden_message
6466     = "types may not be defined in conditions";
6467   /* Parse the type-specifier-seq.  */
6468   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6469                                 &type_specifiers);
6470   /* Restore the saved message.  */
6471   parser->type_definition_forbidden_message = saved_message;
6472   /* If all is well, we might be looking at a declaration.  */
6473   if (!cp_parser_error_occurred (parser))
6474     {
6475       tree decl;
6476       tree asm_specification;
6477       tree attributes;
6478       cp_declarator *declarator;
6479       tree initializer = NULL_TREE;
6480
6481       /* Parse the declarator.  */
6482       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6483                                          /*ctor_dtor_or_conv_p=*/NULL,
6484                                          /*parenthesized_p=*/NULL,
6485                                          /*member_p=*/false);
6486       /* Parse the attributes.  */
6487       attributes = cp_parser_attributes_opt (parser);
6488       /* Parse the asm-specification.  */
6489       asm_specification = cp_parser_asm_specification_opt (parser);
6490       /* If the next token is not an `=', then we might still be
6491          looking at an expression.  For example:
6492
6493            if (A(a).x)
6494
6495          looks like a decl-specifier-seq and a declarator -- but then
6496          there is no `=', so this is an expression.  */
6497       cp_parser_require (parser, CPP_EQ, "`='");
6498       /* If we did see an `=', then we are looking at a declaration
6499          for sure.  */
6500       if (cp_parser_parse_definitely (parser))
6501         {
6502           tree pushed_scope;
6503
6504           /* Create the declaration.  */
6505           decl = start_decl (declarator, &type_specifiers,
6506                              /*initialized_p=*/true,
6507                              attributes, /*prefix_attributes=*/NULL_TREE,
6508                              &pushed_scope);
6509           /* Parse the assignment-expression.  */
6510           initializer = cp_parser_assignment_expression (parser,
6511                                                          /*cast_p=*/false);
6512
6513           /* Process the initializer.  */
6514           cp_finish_decl (decl,
6515                           initializer,
6516                           asm_specification,
6517                           LOOKUP_ONLYCONVERTING);
6518
6519           if (pushed_scope)
6520             pop_scope (pushed_scope);
6521
6522           return convert_from_reference (decl);
6523         }
6524     }
6525   /* If we didn't even get past the declarator successfully, we are
6526      definitely not looking at a declaration.  */
6527   else
6528     cp_parser_abort_tentative_parse (parser);
6529
6530   /* Otherwise, we are looking at an expression.  */
6531   return cp_parser_expression (parser, /*cast_p=*/false);
6532 }
6533
6534 /* Parse an iteration-statement.
6535
6536    iteration-statement:
6537      while ( condition ) statement
6538      do statement while ( expression ) ;
6539      for ( for-init-statement condition [opt] ; expression [opt] )
6540        statement
6541
6542    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6543
6544 static tree
6545 cp_parser_iteration_statement (cp_parser* parser)
6546 {
6547   cp_token *token;
6548   enum rid keyword;
6549   tree statement;
6550   bool in_iteration_statement_p;
6551
6552
6553   /* Peek at the next token.  */
6554   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6555   if (!token)
6556     return error_mark_node;
6557
6558   /* Remember whether or not we are already within an iteration
6559      statement.  */
6560   in_iteration_statement_p = parser->in_iteration_statement_p;
6561
6562   /* See what kind of keyword it is.  */
6563   keyword = token->keyword;
6564   switch (keyword)
6565     {
6566     case RID_WHILE:
6567       {
6568         tree condition;
6569
6570         /* Begin the while-statement.  */
6571         statement = begin_while_stmt ();
6572         /* Look for the `('.  */
6573         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6574         /* Parse the condition.  */
6575         condition = cp_parser_condition (parser);
6576         finish_while_stmt_cond (condition, statement);
6577         /* Look for the `)'.  */
6578         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6579         /* Parse the dependent statement.  */
6580         parser->in_iteration_statement_p = true;
6581         cp_parser_already_scoped_statement (parser);
6582         parser->in_iteration_statement_p = in_iteration_statement_p;
6583         /* We're done with the while-statement.  */
6584         finish_while_stmt (statement);
6585       }
6586       break;
6587
6588     case RID_DO:
6589       {
6590         tree expression;
6591
6592         /* Begin the do-statement.  */
6593         statement = begin_do_stmt ();
6594         /* Parse the body of the do-statement.  */
6595         parser->in_iteration_statement_p = true;
6596         cp_parser_implicitly_scoped_statement (parser);
6597         parser->in_iteration_statement_p = in_iteration_statement_p;
6598         finish_do_body (statement);
6599         /* Look for the `while' keyword.  */
6600         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6601         /* Look for the `('.  */
6602         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6603         /* Parse the expression.  */
6604         expression = cp_parser_expression (parser, /*cast_p=*/false);
6605         /* We're done with the do-statement.  */
6606         finish_do_stmt (expression, statement);
6607         /* Look for the `)'.  */
6608         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6609         /* Look for the `;'.  */
6610         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6611       }
6612       break;
6613
6614     case RID_FOR:
6615       {
6616         tree condition = NULL_TREE;
6617         tree expression = NULL_TREE;
6618
6619         /* Begin the for-statement.  */
6620         statement = begin_for_stmt ();
6621         /* Look for the `('.  */
6622         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6623         /* Parse the initialization.  */
6624         cp_parser_for_init_statement (parser);
6625         finish_for_init_stmt (statement);
6626
6627         /* If there's a condition, process it.  */
6628         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6629           condition = cp_parser_condition (parser);
6630         finish_for_cond (condition, statement);
6631         /* Look for the `;'.  */
6632         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6633
6634         /* If there's an expression, process it.  */
6635         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6636           expression = cp_parser_expression (parser, /*cast_p=*/false);
6637         finish_for_expr (expression, statement);
6638         /* Look for the `)'.  */
6639         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6640
6641         /* Parse the body of the for-statement.  */
6642         parser->in_iteration_statement_p = true;
6643         cp_parser_already_scoped_statement (parser);
6644         parser->in_iteration_statement_p = in_iteration_statement_p;
6645
6646         /* We're done with the for-statement.  */
6647         finish_for_stmt (statement);
6648       }
6649       break;
6650
6651     default:
6652       cp_parser_error (parser, "expected iteration-statement");
6653       statement = error_mark_node;
6654       break;
6655     }
6656
6657   return statement;
6658 }
6659
6660 /* Parse a for-init-statement.
6661
6662    for-init-statement:
6663      expression-statement
6664      simple-declaration  */
6665
6666 static void
6667 cp_parser_for_init_statement (cp_parser* parser)
6668 {
6669   /* If the next token is a `;', then we have an empty
6670      expression-statement.  Grammatically, this is also a
6671      simple-declaration, but an invalid one, because it does not
6672      declare anything.  Therefore, if we did not handle this case
6673      specially, we would issue an error message about an invalid
6674      declaration.  */
6675   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6676     {
6677       /* We're going to speculatively look for a declaration, falling back
6678          to an expression, if necessary.  */
6679       cp_parser_parse_tentatively (parser);
6680       /* Parse the declaration.  */
6681       cp_parser_simple_declaration (parser,
6682                                     /*function_definition_allowed_p=*/false);
6683       /* If the tentative parse failed, then we shall need to look for an
6684          expression-statement.  */
6685       if (cp_parser_parse_definitely (parser))
6686         return;
6687     }
6688
6689   cp_parser_expression_statement (parser, false);
6690 }
6691
6692 /* Parse a jump-statement.
6693
6694    jump-statement:
6695      break ;
6696      continue ;
6697      return expression [opt] ;
6698      goto identifier ;
6699
6700    GNU extension:
6701
6702    jump-statement:
6703      goto * expression ;
6704
6705    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6706
6707 static tree
6708 cp_parser_jump_statement (cp_parser* parser)
6709 {
6710   tree statement = error_mark_node;
6711   cp_token *token;
6712   enum rid keyword;
6713
6714   /* Peek at the next token.  */
6715   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6716   if (!token)
6717     return error_mark_node;
6718
6719   /* See what kind of keyword it is.  */
6720   keyword = token->keyword;
6721   switch (keyword)
6722     {
6723     case RID_BREAK:
6724       if (!parser->in_switch_statement_p
6725           && !parser->in_iteration_statement_p)
6726         {
6727           error ("break statement not within loop or switch");
6728           statement = error_mark_node;
6729         }
6730       else
6731         statement = finish_break_stmt ();
6732       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6733       break;
6734
6735     case RID_CONTINUE:
6736       if (!parser->in_iteration_statement_p)
6737         {
6738           error ("continue statement not within a loop");
6739           statement = error_mark_node;
6740         }
6741       else
6742         statement = finish_continue_stmt ();
6743       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6744       break;
6745
6746     case RID_RETURN:
6747       {
6748         tree expr;
6749
6750         /* If the next token is a `;', then there is no
6751            expression.  */
6752         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6753           expr = cp_parser_expression (parser, /*cast_p=*/false);
6754         else
6755           expr = NULL_TREE;
6756         /* Build the return-statement.  */
6757         statement = finish_return_stmt (expr);
6758         /* Look for the final `;'.  */
6759         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6760       }
6761       break;
6762
6763     case RID_GOTO:
6764       /* Create the goto-statement.  */
6765       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6766         {
6767           /* Issue a warning about this use of a GNU extension.  */
6768           if (pedantic)
6769             pedwarn ("ISO C++ forbids computed gotos");
6770           /* Consume the '*' token.  */
6771           cp_lexer_consume_token (parser->lexer);
6772           /* Parse the dependent expression.  */
6773           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6774         }
6775       else
6776         finish_goto_stmt (cp_parser_identifier (parser));
6777       /* Look for the final `;'.  */
6778       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6779       break;
6780
6781     default:
6782       cp_parser_error (parser, "expected jump-statement");
6783       break;
6784     }
6785
6786   return statement;
6787 }
6788
6789 /* Parse a declaration-statement.
6790
6791    declaration-statement:
6792      block-declaration  */
6793
6794 static void
6795 cp_parser_declaration_statement (cp_parser* parser)
6796 {
6797   void *p;
6798
6799   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6800   p = obstack_alloc (&declarator_obstack, 0);
6801
6802  /* Parse the block-declaration.  */
6803   cp_parser_block_declaration (parser, /*statement_p=*/true);
6804
6805   /* Free any declarators allocated.  */
6806   obstack_free (&declarator_obstack, p);
6807
6808   /* Finish off the statement.  */
6809   finish_stmt ();
6810 }
6811
6812 /* Some dependent statements (like `if (cond) statement'), are
6813    implicitly in their own scope.  In other words, if the statement is
6814    a single statement (as opposed to a compound-statement), it is
6815    none-the-less treated as if it were enclosed in braces.  Any
6816    declarations appearing in the dependent statement are out of scope
6817    after control passes that point.  This function parses a statement,
6818    but ensures that is in its own scope, even if it is not a
6819    compound-statement.
6820
6821    Returns the new statement.  */
6822
6823 static tree
6824 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6825 {
6826   tree statement;
6827
6828   /* Mark if () ; with a special NOP_EXPR.  */
6829   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6830     {
6831       cp_lexer_consume_token (parser->lexer);
6832       statement = add_stmt (build_empty_stmt ());
6833     }
6834   /* if a compound is opened, we simply parse the statement directly.  */
6835   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6836     statement = cp_parser_compound_statement (parser, NULL, false);
6837   /* If the token is not a `{', then we must take special action.  */
6838   else
6839     {
6840       /* Create a compound-statement.  */
6841       statement = begin_compound_stmt (0);
6842       /* Parse the dependent-statement.  */
6843       cp_parser_statement (parser, NULL_TREE, false);
6844       /* Finish the dummy compound-statement.  */
6845       finish_compound_stmt (statement);
6846     }
6847
6848   /* Return the statement.  */
6849   return statement;
6850 }
6851
6852 /* For some dependent statements (like `while (cond) statement'), we
6853    have already created a scope.  Therefore, even if the dependent
6854    statement is a compound-statement, we do not want to create another
6855    scope.  */
6856
6857 static void
6858 cp_parser_already_scoped_statement (cp_parser* parser)
6859 {
6860   /* If the token is a `{', then we must take special action.  */
6861   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6862     cp_parser_statement (parser, NULL_TREE, false);
6863   else
6864     {
6865       /* Avoid calling cp_parser_compound_statement, so that we
6866          don't create a new scope.  Do everything else by hand.  */
6867       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6868       cp_parser_statement_seq_opt (parser, NULL_TREE);
6869       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6870     }
6871 }
6872
6873 /* Declarations [gram.dcl.dcl] */
6874
6875 /* Parse an optional declaration-sequence.
6876
6877    declaration-seq:
6878      declaration
6879      declaration-seq declaration  */
6880
6881 static void
6882 cp_parser_declaration_seq_opt (cp_parser* parser)
6883 {
6884   while (true)
6885     {
6886       cp_token *token;
6887
6888       token = cp_lexer_peek_token (parser->lexer);
6889
6890       if (token->type == CPP_CLOSE_BRACE
6891           || token->type == CPP_EOF
6892           || token->type == CPP_PRAGMA_EOL)
6893         break;
6894
6895       if (token->type == CPP_SEMICOLON)
6896         {
6897           /* A declaration consisting of a single semicolon is
6898              invalid.  Allow it unless we're being pedantic.  */
6899           cp_lexer_consume_token (parser->lexer);
6900           if (pedantic && !in_system_header)
6901             pedwarn ("extra %<;%>");
6902           continue;
6903         }
6904
6905       /* If we're entering or exiting a region that's implicitly
6906          extern "C", modify the lang context appropriately.  */
6907       if (!parser->implicit_extern_c && token->implicit_extern_c)
6908         {
6909           push_lang_context (lang_name_c);
6910           parser->implicit_extern_c = true;
6911         }
6912       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6913         {
6914           pop_lang_context ();
6915           parser->implicit_extern_c = false;
6916         }
6917
6918       if (token->type == CPP_PRAGMA)
6919         {
6920           /* A top-level declaration can consist solely of a #pragma.
6921              A nested declaration cannot, so this is done here and not
6922              in cp_parser_declaration.  (A #pragma at block scope is
6923              handled in cp_parser_statement.)  */
6924           cp_parser_pragma (parser, pragma_external);
6925           continue;
6926         }
6927
6928       /* Parse the declaration itself.  */
6929       cp_parser_declaration (parser);
6930     }
6931 }
6932
6933 /* Parse a declaration.
6934
6935    declaration:
6936      block-declaration
6937      function-definition
6938      template-declaration
6939      explicit-instantiation
6940      explicit-specialization
6941      linkage-specification
6942      namespace-definition
6943
6944    GNU extension:
6945
6946    declaration:
6947       __extension__ declaration */
6948
6949 static void
6950 cp_parser_declaration (cp_parser* parser)
6951 {
6952   cp_token token1;
6953   cp_token token2;
6954   int saved_pedantic;
6955   void *p;
6956
6957   /* Check for the `__extension__' keyword.  */
6958   if (cp_parser_extension_opt (parser, &saved_pedantic))
6959     {
6960       /* Parse the qualified declaration.  */
6961       cp_parser_declaration (parser);
6962       /* Restore the PEDANTIC flag.  */
6963       pedantic = saved_pedantic;
6964
6965       return;
6966     }
6967
6968   /* Try to figure out what kind of declaration is present.  */
6969   token1 = *cp_lexer_peek_token (parser->lexer);
6970
6971   if (token1.type != CPP_EOF)
6972     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6973   else
6974     {
6975       token2.type = CPP_EOF;
6976       token2.keyword = RID_MAX;
6977     }
6978
6979   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6980   p = obstack_alloc (&declarator_obstack, 0);
6981
6982   /* If the next token is `extern' and the following token is a string
6983      literal, then we have a linkage specification.  */
6984   if (token1.keyword == RID_EXTERN
6985       && cp_parser_is_string_literal (&token2))
6986     cp_parser_linkage_specification (parser);
6987   /* If the next token is `template', then we have either a template
6988      declaration, an explicit instantiation, or an explicit
6989      specialization.  */
6990   else if (token1.keyword == RID_TEMPLATE)
6991     {
6992       /* `template <>' indicates a template specialization.  */
6993       if (token2.type == CPP_LESS
6994           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6995         cp_parser_explicit_specialization (parser);
6996       /* `template <' indicates a template declaration.  */
6997       else if (token2.type == CPP_LESS)
6998         cp_parser_template_declaration (parser, /*member_p=*/false);
6999       /* Anything else must be an explicit instantiation.  */
7000       else
7001         cp_parser_explicit_instantiation (parser);
7002     }
7003   /* If the next token is `export', then we have a template
7004      declaration.  */
7005   else if (token1.keyword == RID_EXPORT)
7006     cp_parser_template_declaration (parser, /*member_p=*/false);
7007   /* If the next token is `extern', 'static' or 'inline' and the one
7008      after that is `template', we have a GNU extended explicit
7009      instantiation directive.  */
7010   else if (cp_parser_allow_gnu_extensions_p (parser)
7011            && (token1.keyword == RID_EXTERN
7012                || token1.keyword == RID_STATIC
7013                || token1.keyword == RID_INLINE)
7014            && token2.keyword == RID_TEMPLATE)
7015     cp_parser_explicit_instantiation (parser);
7016   /* If the next token is `namespace', check for a named or unnamed
7017      namespace definition.  */
7018   else if (token1.keyword == RID_NAMESPACE
7019            && (/* A named namespace definition.  */
7020                (token2.type == CPP_NAME
7021                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7022                     == CPP_OPEN_BRACE))
7023                /* An unnamed namespace definition.  */
7024                || token2.type == CPP_OPEN_BRACE))
7025     cp_parser_namespace_definition (parser);
7026   /* Objective-C++ declaration/definition.  */
7027   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7028     cp_parser_objc_declaration (parser);
7029   /* We must have either a block declaration or a function
7030      definition.  */
7031   else
7032     /* Try to parse a block-declaration, or a function-definition.  */
7033     cp_parser_block_declaration (parser, /*statement_p=*/false);
7034
7035   /* Free any declarators allocated.  */
7036   obstack_free (&declarator_obstack, p);
7037 }
7038
7039 /* Parse a block-declaration.
7040
7041    block-declaration:
7042      simple-declaration
7043      asm-definition
7044      namespace-alias-definition
7045      using-declaration
7046      using-directive
7047
7048    GNU Extension:
7049
7050    block-declaration:
7051      __extension__ block-declaration
7052      label-declaration
7053
7054    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7055    part of a declaration-statement.  */
7056
7057 static void
7058 cp_parser_block_declaration (cp_parser *parser,
7059                              bool      statement_p)
7060 {
7061   cp_token *token1;
7062   int saved_pedantic;
7063
7064   /* Check for the `__extension__' keyword.  */
7065   if (cp_parser_extension_opt (parser, &saved_pedantic))
7066     {
7067       /* Parse the qualified declaration.  */
7068       cp_parser_block_declaration (parser, statement_p);
7069       /* Restore the PEDANTIC flag.  */
7070       pedantic = saved_pedantic;
7071
7072       return;
7073     }
7074
7075   /* Peek at the next token to figure out which kind of declaration is
7076      present.  */
7077   token1 = cp_lexer_peek_token (parser->lexer);
7078
7079   /* If the next keyword is `asm', we have an asm-definition.  */
7080   if (token1->keyword == RID_ASM)
7081     {
7082       if (statement_p)
7083         cp_parser_commit_to_tentative_parse (parser);
7084       cp_parser_asm_definition (parser);
7085     }
7086   /* If the next keyword is `namespace', we have a
7087      namespace-alias-definition.  */
7088   else if (token1->keyword == RID_NAMESPACE)
7089     cp_parser_namespace_alias_definition (parser);
7090   /* If the next keyword is `using', we have either a
7091      using-declaration or a using-directive.  */
7092   else if (token1->keyword == RID_USING)
7093     {
7094       cp_token *token2;
7095
7096       if (statement_p)
7097         cp_parser_commit_to_tentative_parse (parser);
7098       /* If the token after `using' is `namespace', then we have a
7099          using-directive.  */
7100       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7101       if (token2->keyword == RID_NAMESPACE)
7102         cp_parser_using_directive (parser);
7103       /* Otherwise, it's a using-declaration.  */
7104       else
7105         cp_parser_using_declaration (parser);
7106     }
7107   /* If the next keyword is `__label__' we have a label declaration.  */
7108   else if (token1->keyword == RID_LABEL)
7109     {
7110       if (statement_p)
7111         cp_parser_commit_to_tentative_parse (parser);
7112       cp_parser_label_declaration (parser);
7113     }
7114   /* Anything else must be a simple-declaration.  */
7115   else
7116     cp_parser_simple_declaration (parser, !statement_p);
7117 }
7118
7119 /* Parse a simple-declaration.
7120
7121    simple-declaration:
7122      decl-specifier-seq [opt] init-declarator-list [opt] ;
7123
7124    init-declarator-list:
7125      init-declarator
7126      init-declarator-list , init-declarator
7127
7128    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7129    function-definition as a simple-declaration.  */
7130
7131 static void
7132 cp_parser_simple_declaration (cp_parser* parser,
7133                               bool function_definition_allowed_p)
7134 {
7135   cp_decl_specifier_seq decl_specifiers;
7136   int declares_class_or_enum;
7137   bool saw_declarator;
7138
7139   /* Defer access checks until we know what is being declared; the
7140      checks for names appearing in the decl-specifier-seq should be
7141      done as if we were in the scope of the thing being declared.  */
7142   push_deferring_access_checks (dk_deferred);
7143
7144   /* Parse the decl-specifier-seq.  We have to keep track of whether
7145      or not the decl-specifier-seq declares a named class or
7146      enumeration type, since that is the only case in which the
7147      init-declarator-list is allowed to be empty.
7148
7149      [dcl.dcl]
7150
7151      In a simple-declaration, the optional init-declarator-list can be
7152      omitted only when declaring a class or enumeration, that is when
7153      the decl-specifier-seq contains either a class-specifier, an
7154      elaborated-type-specifier, or an enum-specifier.  */
7155   cp_parser_decl_specifier_seq (parser,
7156                                 CP_PARSER_FLAGS_OPTIONAL,
7157                                 &decl_specifiers,
7158                                 &declares_class_or_enum);
7159   /* We no longer need to defer access checks.  */
7160   stop_deferring_access_checks ();
7161
7162   /* In a block scope, a valid declaration must always have a
7163      decl-specifier-seq.  By not trying to parse declarators, we can
7164      resolve the declaration/expression ambiguity more quickly.  */
7165   if (!function_definition_allowed_p
7166       && !decl_specifiers.any_specifiers_p)
7167     {
7168       cp_parser_error (parser, "expected declaration");
7169       goto done;
7170     }
7171
7172   /* If the next two tokens are both identifiers, the code is
7173      erroneous. The usual cause of this situation is code like:
7174
7175        T t;
7176
7177      where "T" should name a type -- but does not.  */
7178   if (!decl_specifiers.type
7179       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7180     {
7181       /* If parsing tentatively, we should commit; we really are
7182          looking at a declaration.  */
7183       cp_parser_commit_to_tentative_parse (parser);
7184       /* Give up.  */
7185       goto done;
7186     }
7187
7188   /* If we have seen at least one decl-specifier, and the next token
7189      is not a parenthesis, then we must be looking at a declaration.
7190      (After "int (" we might be looking at a functional cast.)  */
7191   if (decl_specifiers.any_specifiers_p
7192       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7193     cp_parser_commit_to_tentative_parse (parser);
7194
7195   /* Keep going until we hit the `;' at the end of the simple
7196      declaration.  */
7197   saw_declarator = false;
7198   while (cp_lexer_next_token_is_not (parser->lexer,
7199                                      CPP_SEMICOLON))
7200     {
7201       cp_token *token;
7202       bool function_definition_p;
7203       tree decl;
7204
7205       if (saw_declarator)
7206         {
7207           /* If we are processing next declarator, coma is expected */
7208           token = cp_lexer_peek_token (parser->lexer);
7209           gcc_assert (token->type == CPP_COMMA);
7210           cp_lexer_consume_token (parser->lexer);
7211         }
7212       else
7213         saw_declarator = true;
7214
7215       /* Parse the init-declarator.  */
7216       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7217                                         function_definition_allowed_p,
7218                                         /*member_p=*/false,
7219                                         declares_class_or_enum,
7220                                         &function_definition_p);
7221       /* If an error occurred while parsing tentatively, exit quickly.
7222          (That usually happens when in the body of a function; each
7223          statement is treated as a declaration-statement until proven
7224          otherwise.)  */
7225       if (cp_parser_error_occurred (parser))
7226         goto done;
7227       /* Handle function definitions specially.  */
7228       if (function_definition_p)
7229         {
7230           /* If the next token is a `,', then we are probably
7231              processing something like:
7232
7233                void f() {}, *p;
7234
7235              which is erroneous.  */
7236           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7237             error ("mixing declarations and function-definitions is forbidden");
7238           /* Otherwise, we're done with the list of declarators.  */
7239           else
7240             {
7241               pop_deferring_access_checks ();
7242               return;
7243             }
7244         }
7245       /* The next token should be either a `,' or a `;'.  */
7246       token = cp_lexer_peek_token (parser->lexer);
7247       /* If it's a `,', there are more declarators to come.  */
7248       if (token->type == CPP_COMMA)
7249         /* will be consumed next time around */;
7250       /* If it's a `;', we are done.  */
7251       else if (token->type == CPP_SEMICOLON)
7252         break;
7253       /* Anything else is an error.  */
7254       else
7255         {
7256           /* If we have already issued an error message we don't need
7257              to issue another one.  */
7258           if (decl != error_mark_node
7259               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7260             cp_parser_error (parser, "expected %<,%> or %<;%>");
7261           /* Skip tokens until we reach the end of the statement.  */
7262           cp_parser_skip_to_end_of_statement (parser);
7263           /* If the next token is now a `;', consume it.  */
7264           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7265             cp_lexer_consume_token (parser->lexer);
7266           goto done;
7267         }
7268       /* After the first time around, a function-definition is not
7269          allowed -- even if it was OK at first.  For example:
7270
7271            int i, f() {}
7272
7273          is not valid.  */
7274       function_definition_allowed_p = false;
7275     }
7276
7277   /* Issue an error message if no declarators are present, and the
7278      decl-specifier-seq does not itself declare a class or
7279      enumeration.  */
7280   if (!saw_declarator)
7281     {
7282       if (cp_parser_declares_only_class_p (parser))
7283         shadow_tag (&decl_specifiers);
7284       /* Perform any deferred access checks.  */
7285       perform_deferred_access_checks ();
7286     }
7287
7288   /* Consume the `;'.  */
7289   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7290
7291  done:
7292   pop_deferring_access_checks ();
7293 }
7294
7295 /* Parse a decl-specifier-seq.
7296
7297    decl-specifier-seq:
7298      decl-specifier-seq [opt] decl-specifier
7299
7300    decl-specifier:
7301      storage-class-specifier
7302      type-specifier
7303      function-specifier
7304      friend
7305      typedef
7306
7307    GNU Extension:
7308
7309    decl-specifier:
7310      attributes
7311
7312    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7313
7314    The parser flags FLAGS is used to control type-specifier parsing.
7315
7316    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7317    flags:
7318
7319      1: one of the decl-specifiers is an elaborated-type-specifier
7320         (i.e., a type declaration)
7321      2: one of the decl-specifiers is an enum-specifier or a
7322         class-specifier (i.e., a type definition)
7323
7324    */
7325
7326 static void
7327 cp_parser_decl_specifier_seq (cp_parser* parser,
7328                               cp_parser_flags flags,
7329                               cp_decl_specifier_seq *decl_specs,
7330                               int* declares_class_or_enum)
7331 {
7332   bool constructor_possible_p = !parser->in_declarator_p;
7333   cp_decl_spec ds;
7334
7335   /* Clear DECL_SPECS.  */
7336   clear_decl_specs (decl_specs);
7337
7338   /* Assume no class or enumeration type is declared.  */
7339   *declares_class_or_enum = 0;
7340
7341   /* Keep reading specifiers until there are no more to read.  */
7342   while (true)
7343     {
7344       bool constructor_p;
7345       bool found_decl_spec;
7346       cp_token *token;
7347
7348       /* Peek at the next token.  */
7349       token = cp_lexer_peek_token (parser->lexer);
7350       /* Handle attributes.  */
7351       if (token->keyword == RID_ATTRIBUTE)
7352         {
7353           /* Parse the attributes.  */
7354           decl_specs->attributes
7355             = chainon (decl_specs->attributes,
7356                        cp_parser_attributes_opt (parser));
7357           continue;
7358         }
7359       /* Assume we will find a decl-specifier keyword.  */
7360       found_decl_spec = true;
7361       /* If the next token is an appropriate keyword, we can simply
7362          add it to the list.  */
7363       switch (token->keyword)
7364         {
7365           /* decl-specifier:
7366                friend  */
7367         case RID_FRIEND:
7368           ++decl_specs->specs[(int) ds_friend];
7369           /* Consume the token.  */
7370           cp_lexer_consume_token (parser->lexer);
7371           break;
7372
7373           /* function-specifier:
7374                inline
7375                virtual
7376                explicit  */
7377         case RID_INLINE:
7378         case RID_VIRTUAL:
7379         case RID_EXPLICIT:
7380           cp_parser_function_specifier_opt (parser, decl_specs);
7381           break;
7382
7383           /* decl-specifier:
7384                typedef  */
7385         case RID_TYPEDEF:
7386           ++decl_specs->specs[(int) ds_typedef];
7387           /* Consume the token.  */
7388           cp_lexer_consume_token (parser->lexer);
7389           /* A constructor declarator cannot appear in a typedef.  */
7390           constructor_possible_p = false;
7391           /* The "typedef" keyword can only occur in a declaration; we
7392              may as well commit at this point.  */
7393           cp_parser_commit_to_tentative_parse (parser);
7394           break;
7395
7396           /* storage-class-specifier:
7397                auto
7398                register
7399                static
7400                extern
7401                mutable
7402
7403              GNU Extension:
7404                thread  */
7405         case RID_AUTO:
7406           /* Consume the token.  */
7407           cp_lexer_consume_token (parser->lexer);
7408           cp_parser_set_storage_class (decl_specs, sc_auto);
7409           break;
7410         case RID_REGISTER:
7411           /* Consume the token.  */
7412           cp_lexer_consume_token (parser->lexer);
7413           cp_parser_set_storage_class (decl_specs, sc_register);
7414           break;
7415         case RID_STATIC:
7416           /* Consume the token.  */
7417           cp_lexer_consume_token (parser->lexer);
7418           if (decl_specs->specs[(int) ds_thread])
7419             {
7420               error ("%<__thread%> before %<static%>");
7421               decl_specs->specs[(int) ds_thread] = 0;
7422             }
7423           cp_parser_set_storage_class (decl_specs, sc_static);
7424           break;
7425         case RID_EXTERN:
7426           /* Consume the token.  */
7427           cp_lexer_consume_token (parser->lexer);
7428           if (decl_specs->specs[(int) ds_thread])
7429             {
7430               error ("%<__thread%> before %<extern%>");
7431               decl_specs->specs[(int) ds_thread] = 0;
7432             }
7433           cp_parser_set_storage_class (decl_specs, sc_extern);
7434           break;
7435         case RID_MUTABLE:
7436           /* Consume the token.  */
7437           cp_lexer_consume_token (parser->lexer);
7438           cp_parser_set_storage_class (decl_specs, sc_mutable);
7439           break;
7440         case RID_THREAD:
7441           /* Consume the token.  */
7442           cp_lexer_consume_token (parser->lexer);
7443           ++decl_specs->specs[(int) ds_thread];
7444           break;
7445
7446         default:
7447           /* We did not yet find a decl-specifier yet.  */
7448           found_decl_spec = false;
7449           break;
7450         }
7451
7452       /* Constructors are a special case.  The `S' in `S()' is not a
7453          decl-specifier; it is the beginning of the declarator.  */
7454       constructor_p
7455         = (!found_decl_spec
7456            && constructor_possible_p
7457            && (cp_parser_constructor_declarator_p
7458                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7459
7460       /* If we don't have a DECL_SPEC yet, then we must be looking at
7461          a type-specifier.  */
7462       if (!found_decl_spec && !constructor_p)
7463         {
7464           int decl_spec_declares_class_or_enum;
7465           bool is_cv_qualifier;
7466           tree type_spec;
7467
7468           type_spec
7469             = cp_parser_type_specifier (parser, flags,
7470                                         decl_specs,
7471                                         /*is_declaration=*/true,
7472                                         &decl_spec_declares_class_or_enum,
7473                                         &is_cv_qualifier);
7474
7475           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7476
7477           /* If this type-specifier referenced a user-defined type
7478              (a typedef, class-name, etc.), then we can't allow any
7479              more such type-specifiers henceforth.
7480
7481              [dcl.spec]
7482
7483              The longest sequence of decl-specifiers that could
7484              possibly be a type name is taken as the
7485              decl-specifier-seq of a declaration.  The sequence shall
7486              be self-consistent as described below.
7487
7488              [dcl.type]
7489
7490              As a general rule, at most one type-specifier is allowed
7491              in the complete decl-specifier-seq of a declaration.  The
7492              only exceptions are the following:
7493
7494              -- const or volatile can be combined with any other
7495                 type-specifier.
7496
7497              -- signed or unsigned can be combined with char, long,
7498                 short, or int.
7499
7500              -- ..
7501
7502              Example:
7503
7504                typedef char* Pc;
7505                void g (const int Pc);
7506
7507              Here, Pc is *not* part of the decl-specifier seq; it's
7508              the declarator.  Therefore, once we see a type-specifier
7509              (other than a cv-qualifier), we forbid any additional
7510              user-defined types.  We *do* still allow things like `int
7511              int' to be considered a decl-specifier-seq, and issue the
7512              error message later.  */
7513           if (type_spec && !is_cv_qualifier)
7514             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7515           /* A constructor declarator cannot follow a type-specifier.  */
7516           if (type_spec)
7517             {
7518               constructor_possible_p = false;
7519               found_decl_spec = true;
7520             }
7521         }
7522
7523       /* If we still do not have a DECL_SPEC, then there are no more
7524          decl-specifiers.  */
7525       if (!found_decl_spec)
7526         break;
7527
7528       decl_specs->any_specifiers_p = true;
7529       /* After we see one decl-specifier, further decl-specifiers are
7530          always optional.  */
7531       flags |= CP_PARSER_FLAGS_OPTIONAL;
7532     }
7533
7534   /* Check for repeated decl-specifiers.  */
7535   for (ds = ds_first; ds != ds_last; ++ds)
7536     {
7537       unsigned count = decl_specs->specs[(int)ds];
7538       if (count < 2)
7539         continue;
7540       /* The "long" specifier is a special case because of "long long".  */
7541       if (ds == ds_long)
7542         {
7543           if (count > 2)
7544             error ("%<long long long%> is too long for GCC");
7545           else if (pedantic && !in_system_header && warn_long_long)
7546             pedwarn ("ISO C++ does not support %<long long%>");
7547         }
7548       else if (count > 1)
7549         {
7550           static const char *const decl_spec_names[] = {
7551             "signed",
7552             "unsigned",
7553             "short",
7554             "long",
7555             "const",
7556             "volatile",
7557             "restrict",
7558             "inline",
7559             "virtual",
7560             "explicit",
7561             "friend",
7562             "typedef",
7563             "__complex",
7564             "__thread"
7565           };
7566           error ("duplicate %qs", decl_spec_names[(int)ds]);
7567         }
7568     }
7569
7570   /* Don't allow a friend specifier with a class definition.  */
7571   if (decl_specs->specs[(int) ds_friend] != 0
7572       && (*declares_class_or_enum & 2))
7573     error ("class definition may not be declared a friend");
7574 }
7575
7576 /* Parse an (optional) storage-class-specifier.
7577
7578    storage-class-specifier:
7579      auto
7580      register
7581      static
7582      extern
7583      mutable
7584
7585    GNU Extension:
7586
7587    storage-class-specifier:
7588      thread
7589
7590    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7591
7592 static tree
7593 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7594 {
7595   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7596     {
7597     case RID_AUTO:
7598     case RID_REGISTER:
7599     case RID_STATIC:
7600     case RID_EXTERN:
7601     case RID_MUTABLE:
7602     case RID_THREAD:
7603       /* Consume the token.  */
7604       return cp_lexer_consume_token (parser->lexer)->value;
7605
7606     default:
7607       return NULL_TREE;
7608     }
7609 }
7610
7611 /* Parse an (optional) function-specifier.
7612
7613    function-specifier:
7614      inline
7615      virtual
7616      explicit
7617
7618    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7619    Updates DECL_SPECS, if it is non-NULL.  */
7620
7621 static tree
7622 cp_parser_function_specifier_opt (cp_parser* parser,
7623                                   cp_decl_specifier_seq *decl_specs)
7624 {
7625   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7626     {
7627     case RID_INLINE:
7628       if (decl_specs)
7629         ++decl_specs->specs[(int) ds_inline];
7630       break;
7631
7632     case RID_VIRTUAL:
7633       if (decl_specs)
7634         ++decl_specs->specs[(int) ds_virtual];
7635       break;
7636
7637     case RID_EXPLICIT:
7638       if (decl_specs)
7639         ++decl_specs->specs[(int) ds_explicit];
7640       break;
7641
7642     default:
7643       return NULL_TREE;
7644     }
7645
7646   /* Consume the token.  */
7647   return cp_lexer_consume_token (parser->lexer)->value;
7648 }
7649
7650 /* Parse a linkage-specification.
7651
7652    linkage-specification:
7653      extern string-literal { declaration-seq [opt] }
7654      extern string-literal declaration  */
7655
7656 static void
7657 cp_parser_linkage_specification (cp_parser* parser)
7658 {
7659   tree linkage;
7660
7661   /* Look for the `extern' keyword.  */
7662   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7663
7664   /* Look for the string-literal.  */
7665   linkage = cp_parser_string_literal (parser, false, false);
7666
7667   /* Transform the literal into an identifier.  If the literal is a
7668      wide-character string, or contains embedded NULs, then we can't
7669      handle it as the user wants.  */
7670   if (strlen (TREE_STRING_POINTER (linkage))
7671       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7672     {
7673       cp_parser_error (parser, "invalid linkage-specification");
7674       /* Assume C++ linkage.  */
7675       linkage = lang_name_cplusplus;
7676     }
7677   else
7678     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7679
7680   /* We're now using the new linkage.  */
7681   push_lang_context (linkage);
7682
7683   /* If the next token is a `{', then we're using the first
7684      production.  */
7685   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7686     {
7687       /* Consume the `{' token.  */
7688       cp_lexer_consume_token (parser->lexer);
7689       /* Parse the declarations.  */
7690       cp_parser_declaration_seq_opt (parser);
7691       /* Look for the closing `}'.  */
7692       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7693     }
7694   /* Otherwise, there's just one declaration.  */
7695   else
7696     {
7697       bool saved_in_unbraced_linkage_specification_p;
7698
7699       saved_in_unbraced_linkage_specification_p
7700         = parser->in_unbraced_linkage_specification_p;
7701       parser->in_unbraced_linkage_specification_p = true;
7702       have_extern_spec = true;
7703       cp_parser_declaration (parser);
7704       have_extern_spec = false;
7705       parser->in_unbraced_linkage_specification_p
7706         = saved_in_unbraced_linkage_specification_p;
7707     }
7708
7709   /* We're done with the linkage-specification.  */
7710   pop_lang_context ();
7711 }
7712
7713 /* Special member functions [gram.special] */
7714
7715 /* Parse a conversion-function-id.
7716
7717    conversion-function-id:
7718      operator conversion-type-id
7719
7720    Returns an IDENTIFIER_NODE representing the operator.  */
7721
7722 static tree
7723 cp_parser_conversion_function_id (cp_parser* parser)
7724 {
7725   tree type;
7726   tree saved_scope;
7727   tree saved_qualifying_scope;
7728   tree saved_object_scope;
7729   tree pushed_scope = NULL_TREE;
7730
7731   /* Look for the `operator' token.  */
7732   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7733     return error_mark_node;
7734   /* When we parse the conversion-type-id, the current scope will be
7735      reset.  However, we need that information in able to look up the
7736      conversion function later, so we save it here.  */
7737   saved_scope = parser->scope;
7738   saved_qualifying_scope = parser->qualifying_scope;
7739   saved_object_scope = parser->object_scope;
7740   /* We must enter the scope of the class so that the names of
7741      entities declared within the class are available in the
7742      conversion-type-id.  For example, consider:
7743
7744        struct S {
7745          typedef int I;
7746          operator I();
7747        };
7748
7749        S::operator I() { ... }
7750
7751      In order to see that `I' is a type-name in the definition, we
7752      must be in the scope of `S'.  */
7753   if (saved_scope)
7754     pushed_scope = push_scope (saved_scope);
7755   /* Parse the conversion-type-id.  */
7756   type = cp_parser_conversion_type_id (parser);
7757   /* Leave the scope of the class, if any.  */
7758   if (pushed_scope)
7759     pop_scope (pushed_scope);
7760   /* Restore the saved scope.  */
7761   parser->scope = saved_scope;
7762   parser->qualifying_scope = saved_qualifying_scope;
7763   parser->object_scope = saved_object_scope;
7764   /* If the TYPE is invalid, indicate failure.  */
7765   if (type == error_mark_node)
7766     return error_mark_node;
7767   return mangle_conv_op_name_for_type (type);
7768 }
7769
7770 /* Parse a conversion-type-id:
7771
7772    conversion-type-id:
7773      type-specifier-seq conversion-declarator [opt]
7774
7775    Returns the TYPE specified.  */
7776
7777 static tree
7778 cp_parser_conversion_type_id (cp_parser* parser)
7779 {
7780   tree attributes;
7781   cp_decl_specifier_seq type_specifiers;
7782   cp_declarator *declarator;
7783   tree type_specified;
7784
7785   /* Parse the attributes.  */
7786   attributes = cp_parser_attributes_opt (parser);
7787   /* Parse the type-specifiers.  */
7788   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7789                                 &type_specifiers);
7790   /* If that didn't work, stop.  */
7791   if (type_specifiers.type == error_mark_node)
7792     return error_mark_node;
7793   /* Parse the conversion-declarator.  */
7794   declarator = cp_parser_conversion_declarator_opt (parser);
7795
7796   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7797                                     /*initialized=*/0, &attributes);
7798   if (attributes)
7799     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7800   return type_specified;
7801 }
7802
7803 /* Parse an (optional) conversion-declarator.
7804
7805    conversion-declarator:
7806      ptr-operator conversion-declarator [opt]
7807
7808    */
7809
7810 static cp_declarator *
7811 cp_parser_conversion_declarator_opt (cp_parser* parser)
7812 {
7813   enum tree_code code;
7814   tree class_type;
7815   cp_cv_quals cv_quals;
7816
7817   /* We don't know if there's a ptr-operator next, or not.  */
7818   cp_parser_parse_tentatively (parser);
7819   /* Try the ptr-operator.  */
7820   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7821   /* If it worked, look for more conversion-declarators.  */
7822   if (cp_parser_parse_definitely (parser))
7823     {
7824       cp_declarator *declarator;
7825
7826       /* Parse another optional declarator.  */
7827       declarator = cp_parser_conversion_declarator_opt (parser);
7828
7829       /* Create the representation of the declarator.  */
7830       if (class_type)
7831         declarator = make_ptrmem_declarator (cv_quals, class_type,
7832                                              declarator);
7833       else if (code == INDIRECT_REF)
7834         declarator = make_pointer_declarator (cv_quals, declarator);
7835       else
7836         declarator = make_reference_declarator (cv_quals, declarator);
7837
7838       return declarator;
7839    }
7840
7841   return NULL;
7842 }
7843
7844 /* Parse an (optional) ctor-initializer.
7845
7846    ctor-initializer:
7847      : mem-initializer-list
7848
7849    Returns TRUE iff the ctor-initializer was actually present.  */
7850
7851 static bool
7852 cp_parser_ctor_initializer_opt (cp_parser* parser)
7853 {
7854   /* If the next token is not a `:', then there is no
7855      ctor-initializer.  */
7856   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7857     {
7858       /* Do default initialization of any bases and members.  */
7859       if (DECL_CONSTRUCTOR_P (current_function_decl))
7860         finish_mem_initializers (NULL_TREE);
7861
7862       return false;
7863     }
7864
7865   /* Consume the `:' token.  */
7866   cp_lexer_consume_token (parser->lexer);
7867   /* And the mem-initializer-list.  */
7868   cp_parser_mem_initializer_list (parser);
7869
7870   return true;
7871 }
7872
7873 /* Parse a mem-initializer-list.
7874
7875    mem-initializer-list:
7876      mem-initializer
7877      mem-initializer , mem-initializer-list  */
7878
7879 static void
7880 cp_parser_mem_initializer_list (cp_parser* parser)
7881 {
7882   tree mem_initializer_list = NULL_TREE;
7883
7884   /* Let the semantic analysis code know that we are starting the
7885      mem-initializer-list.  */
7886   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7887     error ("only constructors take base initializers");
7888
7889   /* Loop through the list.  */
7890   while (true)
7891     {
7892       tree mem_initializer;
7893
7894       /* Parse the mem-initializer.  */
7895       mem_initializer = cp_parser_mem_initializer (parser);
7896       /* Add it to the list, unless it was erroneous.  */
7897       if (mem_initializer != error_mark_node)
7898         {
7899           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7900           mem_initializer_list = mem_initializer;
7901         }
7902       /* If the next token is not a `,', we're done.  */
7903       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7904         break;
7905       /* Consume the `,' token.  */
7906       cp_lexer_consume_token (parser->lexer);
7907     }
7908
7909   /* Perform semantic analysis.  */
7910   if (DECL_CONSTRUCTOR_P (current_function_decl))
7911     finish_mem_initializers (mem_initializer_list);
7912 }
7913
7914 /* Parse a mem-initializer.
7915
7916    mem-initializer:
7917      mem-initializer-id ( expression-list [opt] )
7918
7919    GNU extension:
7920
7921    mem-initializer:
7922      ( expression-list [opt] )
7923
7924    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7925    class) or FIELD_DECL (for a non-static data member) to initialize;
7926    the TREE_VALUE is the expression-list.  An empty initialization
7927    list is represented by void_list_node.  */
7928
7929 static tree
7930 cp_parser_mem_initializer (cp_parser* parser)
7931 {
7932   tree mem_initializer_id;
7933   tree expression_list;
7934   tree member;
7935
7936   /* Find out what is being initialized.  */
7937   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7938     {
7939       pedwarn ("anachronistic old-style base class initializer");
7940       mem_initializer_id = NULL_TREE;
7941     }
7942   else
7943     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7944   member = expand_member_init (mem_initializer_id);
7945   if (member && !DECL_P (member))
7946     in_base_initializer = 1;
7947
7948   expression_list
7949     = cp_parser_parenthesized_expression_list (parser, false,
7950                                                /*cast_p=*/false,
7951                                                /*non_constant_p=*/NULL);
7952   if (expression_list == error_mark_node)
7953     return error_mark_node;
7954   if (!expression_list)
7955     expression_list = void_type_node;
7956
7957   in_base_initializer = 0;
7958
7959   return member ? build_tree_list (member, expression_list) : error_mark_node;
7960 }
7961
7962 /* Parse a mem-initializer-id.
7963
7964    mem-initializer-id:
7965      :: [opt] nested-name-specifier [opt] class-name
7966      identifier
7967
7968    Returns a TYPE indicating the class to be initializer for the first
7969    production.  Returns an IDENTIFIER_NODE indicating the data member
7970    to be initialized for the second production.  */
7971
7972 static tree
7973 cp_parser_mem_initializer_id (cp_parser* parser)
7974 {
7975   bool global_scope_p;
7976   bool nested_name_specifier_p;
7977   bool template_p = false;
7978   tree id;
7979
7980   /* `typename' is not allowed in this context ([temp.res]).  */
7981   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7982     {
7983       error ("keyword %<typename%> not allowed in this context (a qualified "
7984              "member initializer is implicitly a type)");
7985       cp_lexer_consume_token (parser->lexer);
7986     }
7987   /* Look for the optional `::' operator.  */
7988   global_scope_p
7989     = (cp_parser_global_scope_opt (parser,
7990                                    /*current_scope_valid_p=*/false)
7991        != NULL_TREE);
7992   /* Look for the optional nested-name-specifier.  The simplest way to
7993      implement:
7994
7995        [temp.res]
7996
7997        The keyword `typename' is not permitted in a base-specifier or
7998        mem-initializer; in these contexts a qualified name that
7999        depends on a template-parameter is implicitly assumed to be a
8000        type name.
8001
8002      is to assume that we have seen the `typename' keyword at this
8003      point.  */
8004   nested_name_specifier_p
8005     = (cp_parser_nested_name_specifier_opt (parser,
8006                                             /*typename_keyword_p=*/true,
8007                                             /*check_dependency_p=*/true,
8008                                             /*type_p=*/true,
8009                                             /*is_declaration=*/true)
8010        != NULL_TREE);
8011   if (nested_name_specifier_p)
8012     template_p = cp_parser_optional_template_keyword (parser);
8013   /* If there is a `::' operator or a nested-name-specifier, then we
8014      are definitely looking for a class-name.  */
8015   if (global_scope_p || nested_name_specifier_p)
8016     return cp_parser_class_name (parser,
8017                                  /*typename_keyword_p=*/true,
8018                                  /*template_keyword_p=*/template_p,
8019                                  none_type,
8020                                  /*check_dependency_p=*/true,
8021                                  /*class_head_p=*/false,
8022                                  /*is_declaration=*/true);
8023   /* Otherwise, we could also be looking for an ordinary identifier.  */
8024   cp_parser_parse_tentatively (parser);
8025   /* Try a class-name.  */
8026   id = cp_parser_class_name (parser,
8027                              /*typename_keyword_p=*/true,
8028                              /*template_keyword_p=*/false,
8029                              none_type,
8030                              /*check_dependency_p=*/true,
8031                              /*class_head_p=*/false,
8032                              /*is_declaration=*/true);
8033   /* If we found one, we're done.  */
8034   if (cp_parser_parse_definitely (parser))
8035     return id;
8036   /* Otherwise, look for an ordinary identifier.  */
8037   return cp_parser_identifier (parser);
8038 }
8039
8040 /* Overloading [gram.over] */
8041
8042 /* Parse an operator-function-id.
8043
8044    operator-function-id:
8045      operator operator
8046
8047    Returns an IDENTIFIER_NODE for the operator which is a
8048    human-readable spelling of the identifier, e.g., `operator +'.  */
8049
8050 static tree
8051 cp_parser_operator_function_id (cp_parser* parser)
8052 {
8053   /* Look for the `operator' keyword.  */
8054   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8055     return error_mark_node;
8056   /* And then the name of the operator itself.  */
8057   return cp_parser_operator (parser);
8058 }
8059
8060 /* Parse an operator.
8061
8062    operator:
8063      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8064      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8065      || ++ -- , ->* -> () []
8066
8067    GNU Extensions:
8068
8069    operator:
8070      <? >? <?= >?=
8071
8072    Returns an IDENTIFIER_NODE for the operator which is a
8073    human-readable spelling of the identifier, e.g., `operator +'.  */
8074
8075 static tree
8076 cp_parser_operator (cp_parser* parser)
8077 {
8078   tree id = NULL_TREE;
8079   cp_token *token;
8080
8081   /* Peek at the next token.  */
8082   token = cp_lexer_peek_token (parser->lexer);
8083   /* Figure out which operator we have.  */
8084   switch (token->type)
8085     {
8086     case CPP_KEYWORD:
8087       {
8088         enum tree_code op;
8089
8090         /* The keyword should be either `new' or `delete'.  */
8091         if (token->keyword == RID_NEW)
8092           op = NEW_EXPR;
8093         else if (token->keyword == RID_DELETE)
8094           op = DELETE_EXPR;
8095         else
8096           break;
8097
8098         /* Consume the `new' or `delete' token.  */
8099         cp_lexer_consume_token (parser->lexer);
8100
8101         /* Peek at the next token.  */
8102         token = cp_lexer_peek_token (parser->lexer);
8103         /* If it's a `[' token then this is the array variant of the
8104            operator.  */
8105         if (token->type == CPP_OPEN_SQUARE)
8106           {
8107             /* Consume the `[' token.  */
8108             cp_lexer_consume_token (parser->lexer);
8109             /* Look for the `]' token.  */
8110             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8111             id = ansi_opname (op == NEW_EXPR
8112                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8113           }
8114         /* Otherwise, we have the non-array variant.  */
8115         else
8116           id = ansi_opname (op);
8117
8118         return id;
8119       }
8120
8121     case CPP_PLUS:
8122       id = ansi_opname (PLUS_EXPR);
8123       break;
8124
8125     case CPP_MINUS:
8126       id = ansi_opname (MINUS_EXPR);
8127       break;
8128
8129     case CPP_MULT:
8130       id = ansi_opname (MULT_EXPR);
8131       break;
8132
8133     case CPP_DIV:
8134       id = ansi_opname (TRUNC_DIV_EXPR);
8135       break;
8136
8137     case CPP_MOD:
8138       id = ansi_opname (TRUNC_MOD_EXPR);
8139       break;
8140
8141     case CPP_XOR:
8142       id = ansi_opname (BIT_XOR_EXPR);
8143       break;
8144
8145     case CPP_AND:
8146       id = ansi_opname (BIT_AND_EXPR);
8147       break;
8148
8149     case CPP_OR:
8150       id = ansi_opname (BIT_IOR_EXPR);
8151       break;
8152
8153     case CPP_COMPL:
8154       id = ansi_opname (BIT_NOT_EXPR);
8155       break;
8156
8157     case CPP_NOT:
8158       id = ansi_opname (TRUTH_NOT_EXPR);
8159       break;
8160
8161     case CPP_EQ:
8162       id = ansi_assopname (NOP_EXPR);
8163       break;
8164
8165     case CPP_LESS:
8166       id = ansi_opname (LT_EXPR);
8167       break;
8168
8169     case CPP_GREATER:
8170       id = ansi_opname (GT_EXPR);
8171       break;
8172
8173     case CPP_PLUS_EQ:
8174       id = ansi_assopname (PLUS_EXPR);
8175       break;
8176
8177     case CPP_MINUS_EQ:
8178       id = ansi_assopname (MINUS_EXPR);
8179       break;
8180
8181     case CPP_MULT_EQ:
8182       id = ansi_assopname (MULT_EXPR);
8183       break;
8184
8185     case CPP_DIV_EQ:
8186       id = ansi_assopname (TRUNC_DIV_EXPR);
8187       break;
8188
8189     case CPP_MOD_EQ:
8190       id = ansi_assopname (TRUNC_MOD_EXPR);
8191       break;
8192
8193     case CPP_XOR_EQ:
8194       id = ansi_assopname (BIT_XOR_EXPR);
8195       break;
8196
8197     case CPP_AND_EQ:
8198       id = ansi_assopname (BIT_AND_EXPR);
8199       break;
8200
8201     case CPP_OR_EQ:
8202       id = ansi_assopname (BIT_IOR_EXPR);
8203       break;
8204
8205     case CPP_LSHIFT:
8206       id = ansi_opname (LSHIFT_EXPR);
8207       break;
8208
8209     case CPP_RSHIFT:
8210       id = ansi_opname (RSHIFT_EXPR);
8211       break;
8212
8213     case CPP_LSHIFT_EQ:
8214       id = ansi_assopname (LSHIFT_EXPR);
8215       break;
8216
8217     case CPP_RSHIFT_EQ:
8218       id = ansi_assopname (RSHIFT_EXPR);
8219       break;
8220
8221     case CPP_EQ_EQ:
8222       id = ansi_opname (EQ_EXPR);
8223       break;
8224
8225     case CPP_NOT_EQ:
8226       id = ansi_opname (NE_EXPR);
8227       break;
8228
8229     case CPP_LESS_EQ:
8230       id = ansi_opname (LE_EXPR);
8231       break;
8232
8233     case CPP_GREATER_EQ:
8234       id = ansi_opname (GE_EXPR);
8235       break;
8236
8237     case CPP_AND_AND:
8238       id = ansi_opname (TRUTH_ANDIF_EXPR);
8239       break;
8240
8241     case CPP_OR_OR:
8242       id = ansi_opname (TRUTH_ORIF_EXPR);
8243       break;
8244
8245     case CPP_PLUS_PLUS:
8246       id = ansi_opname (POSTINCREMENT_EXPR);
8247       break;
8248
8249     case CPP_MINUS_MINUS:
8250       id = ansi_opname (PREDECREMENT_EXPR);
8251       break;
8252
8253     case CPP_COMMA:
8254       id = ansi_opname (COMPOUND_EXPR);
8255       break;
8256
8257     case CPP_DEREF_STAR:
8258       id = ansi_opname (MEMBER_REF);
8259       break;
8260
8261     case CPP_DEREF:
8262       id = ansi_opname (COMPONENT_REF);
8263       break;
8264
8265     case CPP_OPEN_PAREN:
8266       /* Consume the `('.  */
8267       cp_lexer_consume_token (parser->lexer);
8268       /* Look for the matching `)'.  */
8269       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8270       return ansi_opname (CALL_EXPR);
8271
8272     case CPP_OPEN_SQUARE:
8273       /* Consume the `['.  */
8274       cp_lexer_consume_token (parser->lexer);
8275       /* Look for the matching `]'.  */
8276       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8277       return ansi_opname (ARRAY_REF);
8278
8279       /* Extensions.  */
8280     case CPP_MIN:
8281       id = ansi_opname (MIN_EXPR);
8282       cp_parser_warn_min_max ();
8283       break;
8284
8285     case CPP_MAX:
8286       id = ansi_opname (MAX_EXPR);
8287       cp_parser_warn_min_max ();
8288       break;
8289
8290     case CPP_MIN_EQ:
8291       id = ansi_assopname (MIN_EXPR);
8292       cp_parser_warn_min_max ();
8293       break;
8294
8295     case CPP_MAX_EQ:
8296       id = ansi_assopname (MAX_EXPR);
8297       cp_parser_warn_min_max ();
8298       break;
8299
8300     default:
8301       /* Anything else is an error.  */
8302       break;
8303     }
8304
8305   /* If we have selected an identifier, we need to consume the
8306      operator token.  */
8307   if (id)
8308     cp_lexer_consume_token (parser->lexer);
8309   /* Otherwise, no valid operator name was present.  */
8310   else
8311     {
8312       cp_parser_error (parser, "expected operator");
8313       id = error_mark_node;
8314     }
8315
8316   return id;
8317 }
8318
8319 /* Parse a template-declaration.
8320
8321    template-declaration:
8322      export [opt] template < template-parameter-list > declaration
8323
8324    If MEMBER_P is TRUE, this template-declaration occurs within a
8325    class-specifier.
8326
8327    The grammar rule given by the standard isn't correct.  What
8328    is really meant is:
8329
8330    template-declaration:
8331      export [opt] template-parameter-list-seq
8332        decl-specifier-seq [opt] init-declarator [opt] ;
8333      export [opt] template-parameter-list-seq
8334        function-definition
8335
8336    template-parameter-list-seq:
8337      template-parameter-list-seq [opt]
8338      template < template-parameter-list >  */
8339
8340 static void
8341 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8342 {
8343   /* Check for `export'.  */
8344   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8345     {
8346       /* Consume the `export' token.  */
8347       cp_lexer_consume_token (parser->lexer);
8348       /* Warn that we do not support `export'.  */
8349       warning (0, "keyword %<export%> not implemented, and will be ignored");
8350     }
8351
8352   cp_parser_template_declaration_after_export (parser, member_p);
8353 }
8354
8355 /* Parse a template-parameter-list.
8356
8357    template-parameter-list:
8358      template-parameter
8359      template-parameter-list , template-parameter
8360
8361    Returns a TREE_LIST.  Each node represents a template parameter.
8362    The nodes are connected via their TREE_CHAINs.  */
8363
8364 static tree
8365 cp_parser_template_parameter_list (cp_parser* parser)
8366 {
8367   tree parameter_list = NULL_TREE;
8368
8369   begin_template_parm_list ();
8370   while (true)
8371     {
8372       tree parameter;
8373       cp_token *token;
8374       bool is_non_type;
8375
8376       /* Parse the template-parameter.  */
8377       parameter = cp_parser_template_parameter (parser, &is_non_type);
8378       /* Add it to the list.  */
8379       if (parameter != error_mark_node)
8380         parameter_list = process_template_parm (parameter_list,
8381                                                 parameter,
8382                                                 is_non_type);
8383       /* Peek at the next token.  */
8384       token = cp_lexer_peek_token (parser->lexer);
8385       /* If it's not a `,', we're done.  */
8386       if (token->type != CPP_COMMA)
8387         break;
8388       /* Otherwise, consume the `,' token.  */
8389       cp_lexer_consume_token (parser->lexer);
8390     }
8391
8392   return end_template_parm_list (parameter_list);
8393 }
8394
8395 /* Parse a template-parameter.
8396
8397    template-parameter:
8398      type-parameter
8399      parameter-declaration
8400
8401    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8402    the parameter.  The TREE_PURPOSE is the default value, if any.
8403    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8404    iff this parameter is a non-type parameter.  */
8405
8406 static tree
8407 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8408 {
8409   cp_token *token;
8410   cp_parameter_declarator *parameter_declarator;
8411   tree parm;
8412
8413   /* Assume it is a type parameter or a template parameter.  */
8414   *is_non_type = false;
8415   /* Peek at the next token.  */
8416   token = cp_lexer_peek_token (parser->lexer);
8417   /* If it is `class' or `template', we have a type-parameter.  */
8418   if (token->keyword == RID_TEMPLATE)
8419     return cp_parser_type_parameter (parser);
8420   /* If it is `class' or `typename' we do not know yet whether it is a
8421      type parameter or a non-type parameter.  Consider:
8422
8423        template <typename T, typename T::X X> ...
8424
8425      or:
8426
8427        template <class C, class D*> ...
8428
8429      Here, the first parameter is a type parameter, and the second is
8430      a non-type parameter.  We can tell by looking at the token after
8431      the identifier -- if it is a `,', `=', or `>' then we have a type
8432      parameter.  */
8433   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8434     {
8435       /* Peek at the token after `class' or `typename'.  */
8436       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8437       /* If it's an identifier, skip it.  */
8438       if (token->type == CPP_NAME)
8439         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8440       /* Now, see if the token looks like the end of a template
8441          parameter.  */
8442       if (token->type == CPP_COMMA
8443           || token->type == CPP_EQ
8444           || token->type == CPP_GREATER)
8445         return cp_parser_type_parameter (parser);
8446     }
8447
8448   /* Otherwise, it is a non-type parameter.
8449
8450      [temp.param]
8451
8452      When parsing a default template-argument for a non-type
8453      template-parameter, the first non-nested `>' is taken as the end
8454      of the template parameter-list rather than a greater-than
8455      operator.  */
8456   *is_non_type = true;
8457   parameter_declarator
8458      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8459                                         /*parenthesized_p=*/NULL);
8460   parm = grokdeclarator (parameter_declarator->declarator,
8461                          &parameter_declarator->decl_specifiers,
8462                          PARM, /*initialized=*/0,
8463                          /*attrlist=*/NULL);
8464   if (parm == error_mark_node)
8465     return error_mark_node;
8466   return build_tree_list (parameter_declarator->default_argument, parm);
8467 }
8468
8469 /* Parse a type-parameter.
8470
8471    type-parameter:
8472      class identifier [opt]
8473      class identifier [opt] = type-id
8474      typename identifier [opt]
8475      typename identifier [opt] = type-id
8476      template < template-parameter-list > class identifier [opt]
8477      template < template-parameter-list > class identifier [opt]
8478        = id-expression
8479
8480    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8481    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8482    the declaration of the parameter.  */
8483
8484 static tree
8485 cp_parser_type_parameter (cp_parser* parser)
8486 {
8487   cp_token *token;
8488   tree parameter;
8489
8490   /* Look for a keyword to tell us what kind of parameter this is.  */
8491   token = cp_parser_require (parser, CPP_KEYWORD,
8492                              "`class', `typename', or `template'");
8493   if (!token)
8494     return error_mark_node;
8495
8496   switch (token->keyword)
8497     {
8498     case RID_CLASS:
8499     case RID_TYPENAME:
8500       {
8501         tree identifier;
8502         tree default_argument;
8503
8504         /* If the next token is an identifier, then it names the
8505            parameter.  */
8506         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8507           identifier = cp_parser_identifier (parser);
8508         else
8509           identifier = NULL_TREE;
8510
8511         /* Create the parameter.  */
8512         parameter = finish_template_type_parm (class_type_node, identifier);
8513
8514         /* If the next token is an `=', we have a default argument.  */
8515         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8516           {
8517             /* Consume the `=' token.  */
8518             cp_lexer_consume_token (parser->lexer);
8519             /* Parse the default-argument.  */
8520             default_argument = cp_parser_type_id (parser);
8521           }
8522         else
8523           default_argument = NULL_TREE;
8524
8525         /* Create the combined representation of the parameter and the
8526            default argument.  */
8527         parameter = build_tree_list (default_argument, parameter);
8528       }
8529       break;
8530
8531     case RID_TEMPLATE:
8532       {
8533         tree parameter_list;
8534         tree identifier;
8535         tree default_argument;
8536
8537         /* Look for the `<'.  */
8538         cp_parser_require (parser, CPP_LESS, "`<'");
8539         /* Parse the template-parameter-list.  */
8540         parameter_list = cp_parser_template_parameter_list (parser);
8541         /* Look for the `>'.  */
8542         cp_parser_require (parser, CPP_GREATER, "`>'");
8543         /* Look for the `class' keyword.  */
8544         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8545         /* If the next token is an `=', then there is a
8546            default-argument.  If the next token is a `>', we are at
8547            the end of the parameter-list.  If the next token is a `,',
8548            then we are at the end of this parameter.  */
8549         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8550             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8551             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8552           {
8553             identifier = cp_parser_identifier (parser);
8554             /* Treat invalid names as if the parameter were nameless.  */
8555             if (identifier == error_mark_node)
8556               identifier = NULL_TREE;
8557           }
8558         else
8559           identifier = NULL_TREE;
8560
8561         /* Create the template parameter.  */
8562         parameter = finish_template_template_parm (class_type_node,
8563                                                    identifier);
8564
8565         /* If the next token is an `=', then there is a
8566            default-argument.  */
8567         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8568           {
8569             bool is_template;
8570
8571             /* Consume the `='.  */
8572             cp_lexer_consume_token (parser->lexer);
8573             /* Parse the id-expression.  */
8574             default_argument
8575               = cp_parser_id_expression (parser,
8576                                          /*template_keyword_p=*/false,
8577                                          /*check_dependency_p=*/true,
8578                                          /*template_p=*/&is_template,
8579                                          /*declarator_p=*/false);
8580             if (TREE_CODE (default_argument) == TYPE_DECL)
8581               /* If the id-expression was a template-id that refers to
8582                  a template-class, we already have the declaration here,
8583                  so no further lookup is needed.  */
8584                  ;
8585             else
8586               /* Look up the name.  */
8587               default_argument
8588                 = cp_parser_lookup_name (parser, default_argument,
8589                                          none_type,
8590                                          /*is_template=*/is_template,
8591                                          /*is_namespace=*/false,
8592                                          /*check_dependency=*/true,
8593                                          /*ambiguous_decls=*/NULL);
8594             /* See if the default argument is valid.  */
8595             default_argument
8596               = check_template_template_default_arg (default_argument);
8597           }
8598         else
8599           default_argument = NULL_TREE;
8600
8601         /* Create the combined representation of the parameter and the
8602            default argument.  */
8603         parameter = build_tree_list (default_argument, parameter);
8604       }
8605       break;
8606
8607     default:
8608       gcc_unreachable ();
8609       break;
8610     }
8611
8612   return parameter;
8613 }
8614
8615 /* Parse a template-id.
8616
8617    template-id:
8618      template-name < template-argument-list [opt] >
8619
8620    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8621    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8622    returned.  Otherwise, if the template-name names a function, or set
8623    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8624    names a class, returns a TYPE_DECL for the specialization.
8625
8626    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8627    uninstantiated templates.  */
8628
8629 static tree
8630 cp_parser_template_id (cp_parser *parser,
8631                        bool template_keyword_p,
8632                        bool check_dependency_p,
8633                        bool is_declaration)
8634 {
8635   tree template;
8636   tree arguments;
8637   tree template_id;
8638   cp_token_position start_of_id = 0;
8639   tree access_check = NULL_TREE;
8640   cp_token *next_token, *next_token_2;
8641   bool is_identifier;
8642
8643   /* If the next token corresponds to a template-id, there is no need
8644      to reparse it.  */
8645   next_token = cp_lexer_peek_token (parser->lexer);
8646   if (next_token->type == CPP_TEMPLATE_ID)
8647     {
8648       tree value;
8649       tree check;
8650
8651       /* Get the stored value.  */
8652       value = cp_lexer_consume_token (parser->lexer)->value;
8653       /* Perform any access checks that were deferred.  */
8654       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8655         perform_or_defer_access_check (TREE_PURPOSE (check),
8656                                        TREE_VALUE (check));
8657       /* Return the stored value.  */
8658       return TREE_VALUE (value);
8659     }
8660
8661   /* Avoid performing name lookup if there is no possibility of
8662      finding a template-id.  */
8663   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8664       || (next_token->type == CPP_NAME
8665           && !cp_parser_nth_token_starts_template_argument_list_p
8666                (parser, 2)))
8667     {
8668       cp_parser_error (parser, "expected template-id");
8669       return error_mark_node;
8670     }
8671
8672   /* Remember where the template-id starts.  */
8673   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8674     start_of_id = cp_lexer_token_position (parser->lexer, false);
8675
8676   push_deferring_access_checks (dk_deferred);
8677
8678   /* Parse the template-name.  */
8679   is_identifier = false;
8680   template = cp_parser_template_name (parser, template_keyword_p,
8681                                       check_dependency_p,
8682                                       is_declaration,
8683                                       &is_identifier);
8684   if (template == error_mark_node || is_identifier)
8685     {
8686       pop_deferring_access_checks ();
8687       return template;
8688     }
8689
8690   /* If we find the sequence `[:' after a template-name, it's probably
8691      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8692      parse correctly the argument list.  */
8693   next_token = cp_lexer_peek_token (parser->lexer);
8694   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8695   if (next_token->type == CPP_OPEN_SQUARE
8696       && next_token->flags & DIGRAPH
8697       && next_token_2->type == CPP_COLON
8698       && !(next_token_2->flags & PREV_WHITE))
8699     {
8700       cp_parser_parse_tentatively (parser);
8701       /* Change `:' into `::'.  */
8702       next_token_2->type = CPP_SCOPE;
8703       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8704          CPP_LESS.  */
8705       cp_lexer_consume_token (parser->lexer);
8706       /* Parse the arguments.  */
8707       arguments = cp_parser_enclosed_template_argument_list (parser);
8708       if (!cp_parser_parse_definitely (parser))
8709         {
8710           /* If we couldn't parse an argument list, then we revert our changes
8711              and return simply an error. Maybe this is not a template-id
8712              after all.  */
8713           next_token_2->type = CPP_COLON;
8714           cp_parser_error (parser, "expected %<<%>");
8715           pop_deferring_access_checks ();
8716           return error_mark_node;
8717         }
8718       /* Otherwise, emit an error about the invalid digraph, but continue
8719          parsing because we got our argument list.  */
8720       pedwarn ("%<<::%> cannot begin a template-argument list");
8721       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8722               "between %<<%> and %<::%>");
8723       if (!flag_permissive)
8724         {
8725           static bool hint;
8726           if (!hint)
8727             {
8728               inform ("(if you use -fpermissive G++ will accept your code)");
8729               hint = true;
8730             }
8731         }
8732     }
8733   else
8734     {
8735       /* Look for the `<' that starts the template-argument-list.  */
8736       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8737         {
8738           pop_deferring_access_checks ();
8739           return error_mark_node;
8740         }
8741       /* Parse the arguments.  */
8742       arguments = cp_parser_enclosed_template_argument_list (parser);
8743     }
8744
8745   /* Build a representation of the specialization.  */
8746   if (TREE_CODE (template) == IDENTIFIER_NODE)
8747     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8748   else if (DECL_CLASS_TEMPLATE_P (template)
8749            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8750     template_id
8751       = finish_template_type (template, arguments,
8752                               cp_lexer_next_token_is (parser->lexer,
8753                                                       CPP_SCOPE));
8754   else
8755     {
8756       /* If it's not a class-template or a template-template, it should be
8757          a function-template.  */
8758       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8759                    || TREE_CODE (template) == OVERLOAD
8760                    || BASELINK_P (template)));
8761
8762       template_id = lookup_template_function (template, arguments);
8763     }
8764
8765   /* Retrieve any deferred checks.  Do not pop this access checks yet
8766      so the memory will not be reclaimed during token replacing below.  */
8767   access_check = get_deferred_access_checks ();
8768
8769   /* If parsing tentatively, replace the sequence of tokens that makes
8770      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8771      should we re-parse the token stream, we will not have to repeat
8772      the effort required to do the parse, nor will we issue duplicate
8773      error messages about problems during instantiation of the
8774      template.  */
8775   if (start_of_id)
8776     {
8777       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8778
8779       /* Reset the contents of the START_OF_ID token.  */
8780       token->type = CPP_TEMPLATE_ID;
8781       token->value = build_tree_list (access_check, template_id);
8782       token->keyword = RID_MAX;
8783
8784       /* Purge all subsequent tokens.  */
8785       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8786
8787       /* ??? Can we actually assume that, if template_id ==
8788          error_mark_node, we will have issued a diagnostic to the
8789          user, as opposed to simply marking the tentative parse as
8790          failed?  */
8791       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8792         error ("parse error in template argument list");
8793     }
8794
8795   pop_deferring_access_checks ();
8796   return template_id;
8797 }
8798
8799 /* Parse a template-name.
8800
8801    template-name:
8802      identifier
8803
8804    The standard should actually say:
8805
8806    template-name:
8807      identifier
8808      operator-function-id
8809
8810    A defect report has been filed about this issue.
8811
8812    A conversion-function-id cannot be a template name because they cannot
8813    be part of a template-id. In fact, looking at this code:
8814
8815    a.operator K<int>()
8816
8817    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8818    It is impossible to call a templated conversion-function-id with an
8819    explicit argument list, since the only allowed template parameter is
8820    the type to which it is converting.
8821
8822    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8823    `template' keyword, in a construction like:
8824
8825      T::template f<3>()
8826
8827    In that case `f' is taken to be a template-name, even though there
8828    is no way of knowing for sure.
8829
8830    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8831    name refers to a set of overloaded functions, at least one of which
8832    is a template, or an IDENTIFIER_NODE with the name of the template,
8833    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8834    names are looked up inside uninstantiated templates.  */
8835
8836 static tree
8837 cp_parser_template_name (cp_parser* parser,
8838                          bool template_keyword_p,
8839                          bool check_dependency_p,
8840                          bool is_declaration,
8841                          bool *is_identifier)
8842 {
8843   tree identifier;
8844   tree decl;
8845   tree fns;
8846
8847   /* If the next token is `operator', then we have either an
8848      operator-function-id or a conversion-function-id.  */
8849   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8850     {
8851       /* We don't know whether we're looking at an
8852          operator-function-id or a conversion-function-id.  */
8853       cp_parser_parse_tentatively (parser);
8854       /* Try an operator-function-id.  */
8855       identifier = cp_parser_operator_function_id (parser);
8856       /* If that didn't work, try a conversion-function-id.  */
8857       if (!cp_parser_parse_definitely (parser))
8858         {
8859           cp_parser_error (parser, "expected template-name");
8860           return error_mark_node;
8861         }
8862     }
8863   /* Look for the identifier.  */
8864   else
8865     identifier = cp_parser_identifier (parser);
8866
8867   /* If we didn't find an identifier, we don't have a template-id.  */
8868   if (identifier == error_mark_node)
8869     return error_mark_node;
8870
8871   /* If the name immediately followed the `template' keyword, then it
8872      is a template-name.  However, if the next token is not `<', then
8873      we do not treat it as a template-name, since it is not being used
8874      as part of a template-id.  This enables us to handle constructs
8875      like:
8876
8877        template <typename T> struct S { S(); };
8878        template <typename T> S<T>::S();
8879
8880      correctly.  We would treat `S' as a template -- if it were `S<T>'
8881      -- but we do not if there is no `<'.  */
8882
8883   if (processing_template_decl
8884       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8885     {
8886       /* In a declaration, in a dependent context, we pretend that the
8887          "template" keyword was present in order to improve error
8888          recovery.  For example, given:
8889
8890            template <typename T> void f(T::X<int>);
8891
8892          we want to treat "X<int>" as a template-id.  */
8893       if (is_declaration
8894           && !template_keyword_p
8895           && parser->scope && TYPE_P (parser->scope)
8896           && check_dependency_p
8897           && dependent_type_p (parser->scope)
8898           /* Do not do this for dtors (or ctors), since they never
8899              need the template keyword before their name.  */
8900           && !constructor_name_p (identifier, parser->scope))
8901         {
8902           cp_token_position start = 0;
8903
8904           /* Explain what went wrong.  */
8905           error ("non-template %qD used as template", identifier);
8906           inform ("use %<%T::template %D%> to indicate that it is a template",
8907                   parser->scope, identifier);
8908           /* If parsing tentatively, find the location of the "<" token.  */
8909           if (cp_parser_simulate_error (parser))
8910             start = cp_lexer_token_position (parser->lexer, true);
8911           /* Parse the template arguments so that we can issue error
8912              messages about them.  */
8913           cp_lexer_consume_token (parser->lexer);
8914           cp_parser_enclosed_template_argument_list (parser);
8915           /* Skip tokens until we find a good place from which to
8916              continue parsing.  */
8917           cp_parser_skip_to_closing_parenthesis (parser,
8918                                                  /*recovering=*/true,
8919                                                  /*or_comma=*/true,
8920                                                  /*consume_paren=*/false);
8921           /* If parsing tentatively, permanently remove the
8922              template argument list.  That will prevent duplicate
8923              error messages from being issued about the missing
8924              "template" keyword.  */
8925           if (start)
8926             cp_lexer_purge_tokens_after (parser->lexer, start);
8927           if (is_identifier)
8928             *is_identifier = true;
8929           return identifier;
8930         }
8931
8932       /* If the "template" keyword is present, then there is generally
8933          no point in doing name-lookup, so we just return IDENTIFIER.
8934          But, if the qualifying scope is non-dependent then we can
8935          (and must) do name-lookup normally.  */
8936       if (template_keyword_p
8937           && (!parser->scope
8938               || (TYPE_P (parser->scope)
8939                   && dependent_type_p (parser->scope))))
8940         return identifier;
8941     }
8942
8943   /* Look up the name.  */
8944   decl = cp_parser_lookup_name (parser, identifier,
8945                                 none_type,
8946                                 /*is_template=*/false,
8947                                 /*is_namespace=*/false,
8948                                 check_dependency_p,
8949                                 /*ambiguous_decls=*/NULL);
8950   decl = maybe_get_template_decl_from_type_decl (decl);
8951
8952   /* If DECL is a template, then the name was a template-name.  */
8953   if (TREE_CODE (decl) == TEMPLATE_DECL)
8954     ;
8955   else
8956     {
8957       tree fn = NULL_TREE;
8958
8959       /* The standard does not explicitly indicate whether a name that
8960          names a set of overloaded declarations, some of which are
8961          templates, is a template-name.  However, such a name should
8962          be a template-name; otherwise, there is no way to form a
8963          template-id for the overloaded templates.  */
8964       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8965       if (TREE_CODE (fns) == OVERLOAD)
8966         for (fn = fns; fn; fn = OVL_NEXT (fn))
8967           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8968             break;
8969
8970       if (!fn)
8971         {
8972           /* The name does not name a template.  */
8973           cp_parser_error (parser, "expected template-name");
8974           return error_mark_node;
8975         }
8976     }
8977
8978   /* If DECL is dependent, and refers to a function, then just return
8979      its name; we will look it up again during template instantiation.  */
8980   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8981     {
8982       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8983       if (TYPE_P (scope) && dependent_type_p (scope))
8984         return identifier;
8985     }
8986
8987   return decl;
8988 }
8989
8990 /* Parse a template-argument-list.
8991
8992    template-argument-list:
8993      template-argument
8994      template-argument-list , template-argument
8995
8996    Returns a TREE_VEC containing the arguments.  */
8997
8998 static tree
8999 cp_parser_template_argument_list (cp_parser* parser)
9000 {
9001   tree fixed_args[10];
9002   unsigned n_args = 0;
9003   unsigned alloced = 10;
9004   tree *arg_ary = fixed_args;
9005   tree vec;
9006   bool saved_in_template_argument_list_p;
9007   bool saved_ice_p;
9008   bool saved_non_ice_p;
9009
9010   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9011   parser->in_template_argument_list_p = true;
9012   /* Even if the template-id appears in an integral
9013      constant-expression, the contents of the argument list do 
9014      not.  */ 
9015   saved_ice_p = parser->integral_constant_expression_p;
9016   parser->integral_constant_expression_p = false;
9017   saved_non_ice_p = parser->non_integral_constant_expression_p;
9018   parser->non_integral_constant_expression_p = false;
9019   /* Parse the arguments.  */
9020   do
9021     {
9022       tree argument;
9023
9024       if (n_args)
9025         /* Consume the comma.  */
9026         cp_lexer_consume_token (parser->lexer);
9027
9028       /* Parse the template-argument.  */
9029       argument = cp_parser_template_argument (parser);
9030       if (n_args == alloced)
9031         {
9032           alloced *= 2;
9033
9034           if (arg_ary == fixed_args)
9035             {
9036               arg_ary = XNEWVEC (tree, alloced);
9037               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9038             }
9039           else
9040             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9041         }
9042       arg_ary[n_args++] = argument;
9043     }
9044   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9045
9046   vec = make_tree_vec (n_args);
9047
9048   while (n_args--)
9049     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9050
9051   if (arg_ary != fixed_args)
9052     free (arg_ary);
9053   parser->non_integral_constant_expression_p = saved_non_ice_p;
9054   parser->integral_constant_expression_p = saved_ice_p;
9055   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9056   return vec;
9057 }
9058
9059 /* Parse a template-argument.
9060
9061    template-argument:
9062      assignment-expression
9063      type-id
9064      id-expression
9065
9066    The representation is that of an assignment-expression, type-id, or
9067    id-expression -- except that the qualified id-expression is
9068    evaluated, so that the value returned is either a DECL or an
9069    OVERLOAD.
9070
9071    Although the standard says "assignment-expression", it forbids
9072    throw-expressions or assignments in the template argument.
9073    Therefore, we use "conditional-expression" instead.  */
9074
9075 static tree
9076 cp_parser_template_argument (cp_parser* parser)
9077 {
9078   tree argument;
9079   bool template_p;
9080   bool address_p;
9081   bool maybe_type_id = false;
9082   cp_token *token;
9083   cp_id_kind idk;
9084
9085   /* There's really no way to know what we're looking at, so we just
9086      try each alternative in order.
9087
9088        [temp.arg]
9089
9090        In a template-argument, an ambiguity between a type-id and an
9091        expression is resolved to a type-id, regardless of the form of
9092        the corresponding template-parameter.
9093
9094      Therefore, we try a type-id first.  */
9095   cp_parser_parse_tentatively (parser);
9096   argument = cp_parser_type_id (parser);
9097   /* If there was no error parsing the type-id but the next token is a '>>',
9098      we probably found a typo for '> >'. But there are type-id which are
9099      also valid expressions. For instance:
9100
9101      struct X { int operator >> (int); };
9102      template <int V> struct Foo {};
9103      Foo<X () >> 5> r;
9104
9105      Here 'X()' is a valid type-id of a function type, but the user just
9106      wanted to write the expression "X() >> 5". Thus, we remember that we
9107      found a valid type-id, but we still try to parse the argument as an
9108      expression to see what happens.  */
9109   if (!cp_parser_error_occurred (parser)
9110       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9111     {
9112       maybe_type_id = true;
9113       cp_parser_abort_tentative_parse (parser);
9114     }
9115   else
9116     {
9117       /* If the next token isn't a `,' or a `>', then this argument wasn't
9118       really finished. This means that the argument is not a valid
9119       type-id.  */
9120       if (!cp_parser_next_token_ends_template_argument_p (parser))
9121         cp_parser_error (parser, "expected template-argument");
9122       /* If that worked, we're done.  */
9123       if (cp_parser_parse_definitely (parser))
9124         return argument;
9125     }
9126   /* We're still not sure what the argument will be.  */
9127   cp_parser_parse_tentatively (parser);
9128   /* Try a template.  */
9129   argument = cp_parser_id_expression (parser,
9130                                       /*template_keyword_p=*/false,
9131                                       /*check_dependency_p=*/true,
9132                                       &template_p,
9133                                       /*declarator_p=*/false);
9134   /* If the next token isn't a `,' or a `>', then this argument wasn't
9135      really finished.  */
9136   if (!cp_parser_next_token_ends_template_argument_p (parser))
9137     cp_parser_error (parser, "expected template-argument");
9138   if (!cp_parser_error_occurred (parser))
9139     {
9140       /* Figure out what is being referred to.  If the id-expression
9141          was for a class template specialization, then we will have a
9142          TYPE_DECL at this point.  There is no need to do name lookup
9143          at this point in that case.  */
9144       if (TREE_CODE (argument) != TYPE_DECL)
9145         argument = cp_parser_lookup_name (parser, argument,
9146                                           none_type,
9147                                           /*is_template=*/template_p,
9148                                           /*is_namespace=*/false,
9149                                           /*check_dependency=*/true,
9150                                           /*ambiguous_decls=*/NULL);
9151       if (TREE_CODE (argument) != TEMPLATE_DECL
9152           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9153         cp_parser_error (parser, "expected template-name");
9154     }
9155   if (cp_parser_parse_definitely (parser))
9156     return argument;
9157   /* It must be a non-type argument.  There permitted cases are given
9158      in [temp.arg.nontype]:
9159
9160      -- an integral constant-expression of integral or enumeration
9161         type; or
9162
9163      -- the name of a non-type template-parameter; or
9164
9165      -- the name of an object or function with external linkage...
9166
9167      -- the address of an object or function with external linkage...
9168
9169      -- a pointer to member...  */
9170   /* Look for a non-type template parameter.  */
9171   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9172     {
9173       cp_parser_parse_tentatively (parser);
9174       argument = cp_parser_primary_expression (parser,
9175                                                /*adress_p=*/false,
9176                                                /*cast_p=*/false,
9177                                                /*template_arg_p=*/true,
9178                                                &idk);
9179       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9180           || !cp_parser_next_token_ends_template_argument_p (parser))
9181         cp_parser_simulate_error (parser);
9182       if (cp_parser_parse_definitely (parser))
9183         return argument;
9184     }
9185
9186   /* If the next token is "&", the argument must be the address of an
9187      object or function with external linkage.  */
9188   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9189   if (address_p)
9190     cp_lexer_consume_token (parser->lexer);
9191   /* See if we might have an id-expression.  */
9192   token = cp_lexer_peek_token (parser->lexer);
9193   if (token->type == CPP_NAME
9194       || token->keyword == RID_OPERATOR
9195       || token->type == CPP_SCOPE
9196       || token->type == CPP_TEMPLATE_ID
9197       || token->type == CPP_NESTED_NAME_SPECIFIER)
9198     {
9199       cp_parser_parse_tentatively (parser);
9200       argument = cp_parser_primary_expression (parser,
9201                                                address_p,
9202                                                /*cast_p=*/false,
9203                                                /*template_arg_p=*/true,
9204                                                &idk);
9205       if (cp_parser_error_occurred (parser)
9206           || !cp_parser_next_token_ends_template_argument_p (parser))
9207         cp_parser_abort_tentative_parse (parser);
9208       else
9209         {
9210           if (TREE_CODE (argument) == INDIRECT_REF)
9211             {
9212               gcc_assert (REFERENCE_REF_P (argument));
9213               argument = TREE_OPERAND (argument, 0);
9214             }
9215
9216           if (TREE_CODE (argument) == BASELINK)
9217             /* We don't need the information about what class was used
9218                to name the overloaded functions.  */  
9219             argument = BASELINK_FUNCTIONS (argument);
9220
9221           if (TREE_CODE (argument) == VAR_DECL)
9222             {
9223               /* A variable without external linkage might still be a
9224                  valid constant-expression, so no error is issued here
9225                  if the external-linkage check fails.  */
9226               if (!DECL_EXTERNAL_LINKAGE_P (argument))
9227                 cp_parser_simulate_error (parser);
9228             }
9229           else if (is_overloaded_fn (argument))
9230             /* All overloaded functions are allowed; if the external
9231                linkage test does not pass, an error will be issued
9232                later.  */
9233             ;
9234           else if (address_p
9235                    && (TREE_CODE (argument) == OFFSET_REF
9236                        || TREE_CODE (argument) == SCOPE_REF))
9237             /* A pointer-to-member.  */
9238             ;
9239           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9240             ;
9241           else
9242             cp_parser_simulate_error (parser);
9243
9244           if (cp_parser_parse_definitely (parser))
9245             {
9246               if (address_p)
9247                 argument = build_x_unary_op (ADDR_EXPR, argument);
9248               return argument;
9249             }
9250         }
9251     }
9252   /* If the argument started with "&", there are no other valid
9253      alternatives at this point.  */
9254   if (address_p)
9255     {
9256       cp_parser_error (parser, "invalid non-type template argument");
9257       return error_mark_node;
9258     }
9259
9260   /* If the argument wasn't successfully parsed as a type-id followed
9261      by '>>', the argument can only be a constant expression now.
9262      Otherwise, we try parsing the constant-expression tentatively,
9263      because the argument could really be a type-id.  */
9264   if (maybe_type_id)
9265     cp_parser_parse_tentatively (parser);
9266   argument = cp_parser_constant_expression (parser,
9267                                             /*allow_non_constant_p=*/false,
9268                                             /*non_constant_p=*/NULL);
9269   argument = fold_non_dependent_expr (argument);
9270   if (!maybe_type_id)
9271     return argument;
9272   if (!cp_parser_next_token_ends_template_argument_p (parser))
9273     cp_parser_error (parser, "expected template-argument");
9274   if (cp_parser_parse_definitely (parser))
9275     return argument;
9276   /* We did our best to parse the argument as a non type-id, but that
9277      was the only alternative that matched (albeit with a '>' after
9278      it). We can assume it's just a typo from the user, and a
9279      diagnostic will then be issued.  */
9280   return cp_parser_type_id (parser);
9281 }
9282
9283 /* Parse an explicit-instantiation.
9284
9285    explicit-instantiation:
9286      template declaration
9287
9288    Although the standard says `declaration', what it really means is:
9289
9290    explicit-instantiation:
9291      template decl-specifier-seq [opt] declarator [opt] ;
9292
9293    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9294    supposed to be allowed.  A defect report has been filed about this
9295    issue.
9296
9297    GNU Extension:
9298
9299    explicit-instantiation:
9300      storage-class-specifier template
9301        decl-specifier-seq [opt] declarator [opt] ;
9302      function-specifier template
9303        decl-specifier-seq [opt] declarator [opt] ;  */
9304
9305 static void
9306 cp_parser_explicit_instantiation (cp_parser* parser)
9307 {
9308   int declares_class_or_enum;
9309   cp_decl_specifier_seq decl_specifiers;
9310   tree extension_specifier = NULL_TREE;
9311
9312   /* Look for an (optional) storage-class-specifier or
9313      function-specifier.  */
9314   if (cp_parser_allow_gnu_extensions_p (parser))
9315     {
9316       extension_specifier
9317         = cp_parser_storage_class_specifier_opt (parser);
9318       if (!extension_specifier)
9319         extension_specifier
9320           = cp_parser_function_specifier_opt (parser,
9321                                               /*decl_specs=*/NULL);
9322     }
9323
9324   /* Look for the `template' keyword.  */
9325   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9326   /* Let the front end know that we are processing an explicit
9327      instantiation.  */
9328   begin_explicit_instantiation ();
9329   /* [temp.explicit] says that we are supposed to ignore access
9330      control while processing explicit instantiation directives.  */
9331   push_deferring_access_checks (dk_no_check);
9332   /* Parse a decl-specifier-seq.  */
9333   cp_parser_decl_specifier_seq (parser,
9334                                 CP_PARSER_FLAGS_OPTIONAL,
9335                                 &decl_specifiers,
9336                                 &declares_class_or_enum);
9337   /* If there was exactly one decl-specifier, and it declared a class,
9338      and there's no declarator, then we have an explicit type
9339      instantiation.  */
9340   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9341     {
9342       tree type;
9343
9344       type = check_tag_decl (&decl_specifiers);
9345       /* Turn access control back on for names used during
9346          template instantiation.  */
9347       pop_deferring_access_checks ();
9348       if (type)
9349         do_type_instantiation (type, extension_specifier,
9350                                /*complain=*/tf_error);
9351     }
9352   else
9353     {
9354       cp_declarator *declarator;
9355       tree decl;
9356
9357       /* Parse the declarator.  */
9358       declarator
9359         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9360                                 /*ctor_dtor_or_conv_p=*/NULL,
9361                                 /*parenthesized_p=*/NULL,
9362                                 /*member_p=*/false);
9363       if (declares_class_or_enum & 2)
9364         cp_parser_check_for_definition_in_return_type (declarator,
9365                                                        decl_specifiers.type);
9366       if (declarator != cp_error_declarator)
9367         {
9368           decl = grokdeclarator (declarator, &decl_specifiers,
9369                                  NORMAL, 0, NULL);
9370           /* Turn access control back on for names used during
9371              template instantiation.  */
9372           pop_deferring_access_checks ();
9373           /* Do the explicit instantiation.  */
9374           do_decl_instantiation (decl, extension_specifier);
9375         }
9376       else
9377         {
9378           pop_deferring_access_checks ();
9379           /* Skip the body of the explicit instantiation.  */
9380           cp_parser_skip_to_end_of_statement (parser);
9381         }
9382     }
9383   /* We're done with the instantiation.  */
9384   end_explicit_instantiation ();
9385
9386   cp_parser_consume_semicolon_at_end_of_statement (parser);
9387 }
9388
9389 /* Parse an explicit-specialization.
9390
9391    explicit-specialization:
9392      template < > declaration
9393
9394    Although the standard says `declaration', what it really means is:
9395
9396    explicit-specialization:
9397      template <> decl-specifier [opt] init-declarator [opt] ;
9398      template <> function-definition
9399      template <> explicit-specialization
9400      template <> template-declaration  */
9401
9402 static void
9403 cp_parser_explicit_specialization (cp_parser* parser)
9404 {
9405   bool need_lang_pop;
9406   /* Look for the `template' keyword.  */
9407   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9408   /* Look for the `<'.  */
9409   cp_parser_require (parser, CPP_LESS, "`<'");
9410   /* Look for the `>'.  */
9411   cp_parser_require (parser, CPP_GREATER, "`>'");
9412   /* We have processed another parameter list.  */
9413   ++parser->num_template_parameter_lists;
9414   /* [temp]
9415    
9416      A template ... explicit specialization ... shall not have C
9417      linkage.  */ 
9418   if (current_lang_name == lang_name_c)
9419     {
9420       error ("template specialization with C linkage");
9421       /* Give it C++ linkage to avoid confusing other parts of the
9422          front end.  */
9423       push_lang_context (lang_name_cplusplus);
9424       need_lang_pop = true;
9425     }
9426   else
9427     need_lang_pop = false;
9428   /* Let the front end know that we are beginning a specialization.  */
9429   begin_specialization ();
9430   /* If the next keyword is `template', we need to figure out whether
9431      or not we're looking a template-declaration.  */
9432   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9433     {
9434       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9435           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9436         cp_parser_template_declaration_after_export (parser,
9437                                                      /*member_p=*/false);
9438       else
9439         cp_parser_explicit_specialization (parser);
9440     }
9441   else
9442     /* Parse the dependent declaration.  */
9443     cp_parser_single_declaration (parser,
9444                                   /*member_p=*/false,
9445                                   /*friend_p=*/NULL);
9446   /* We're done with the specialization.  */
9447   end_specialization ();
9448   /* For the erroneous case of a template with C linkage, we pushed an
9449      implicit C++ linkage scope; exit that scope now.  */
9450   if (need_lang_pop)
9451     pop_lang_context ();
9452   /* We're done with this parameter list.  */
9453   --parser->num_template_parameter_lists;
9454 }
9455
9456 /* Parse a type-specifier.
9457
9458    type-specifier:
9459      simple-type-specifier
9460      class-specifier
9461      enum-specifier
9462      elaborated-type-specifier
9463      cv-qualifier
9464
9465    GNU Extension:
9466
9467    type-specifier:
9468      __complex__
9469
9470    Returns a representation of the type-specifier.  For a
9471    class-specifier, enum-specifier, or elaborated-type-specifier, a
9472    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9473
9474    The parser flags FLAGS is used to control type-specifier parsing.
9475
9476    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9477    in a decl-specifier-seq.
9478
9479    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9480    class-specifier, enum-specifier, or elaborated-type-specifier, then
9481    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9482    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9483    zero.
9484
9485    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9486    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9487    is set to FALSE.  */
9488
9489 static tree
9490 cp_parser_type_specifier (cp_parser* parser,
9491                           cp_parser_flags flags,
9492                           cp_decl_specifier_seq *decl_specs,
9493                           bool is_declaration,
9494                           int* declares_class_or_enum,
9495                           bool* is_cv_qualifier)
9496 {
9497   tree type_spec = NULL_TREE;
9498   cp_token *token;
9499   enum rid keyword;
9500   cp_decl_spec ds = ds_last;
9501
9502   /* Assume this type-specifier does not declare a new type.  */
9503   if (declares_class_or_enum)
9504     *declares_class_or_enum = 0;
9505   /* And that it does not specify a cv-qualifier.  */
9506   if (is_cv_qualifier)
9507     *is_cv_qualifier = false;
9508   /* Peek at the next token.  */
9509   token = cp_lexer_peek_token (parser->lexer);
9510
9511   /* If we're looking at a keyword, we can use that to guide the
9512      production we choose.  */
9513   keyword = token->keyword;
9514   switch (keyword)
9515     {
9516     case RID_ENUM:
9517       /* 'enum' [identifier] '{' introduces an enum-specifier;
9518          'enum' <anything else> introduces an elaborated-type-specifier.  */
9519       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9520           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9521               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9522                  == CPP_OPEN_BRACE))
9523         {
9524           if (parser->num_template_parameter_lists)
9525             {
9526               error ("template declaration of %qs", "enum");
9527               cp_parser_skip_to_end_of_block_or_statement (parser);
9528               type_spec = error_mark_node;
9529             }
9530           else
9531             type_spec = cp_parser_enum_specifier (parser);
9532
9533           if (declares_class_or_enum)
9534             *declares_class_or_enum = 2;
9535           if (decl_specs)
9536             cp_parser_set_decl_spec_type (decl_specs,
9537                                           type_spec,
9538                                           /*user_defined_p=*/true);
9539           return type_spec;
9540         }
9541       else
9542         goto elaborated_type_specifier;
9543
9544       /* Any of these indicate either a class-specifier, or an
9545          elaborated-type-specifier.  */
9546     case RID_CLASS:
9547     case RID_STRUCT:
9548     case RID_UNION:
9549       /* Parse tentatively so that we can back up if we don't find a
9550          class-specifier.  */
9551       cp_parser_parse_tentatively (parser);
9552       /* Look for the class-specifier.  */
9553       type_spec = cp_parser_class_specifier (parser);
9554       /* If that worked, we're done.  */
9555       if (cp_parser_parse_definitely (parser))
9556         {
9557           if (declares_class_or_enum)
9558             *declares_class_or_enum = 2;
9559           if (decl_specs)
9560             cp_parser_set_decl_spec_type (decl_specs,
9561                                           type_spec,
9562                                           /*user_defined_p=*/true);
9563           return type_spec;
9564         }
9565
9566       /* Fall through.  */
9567     elaborated_type_specifier:
9568       /* We're declaring (not defining) a class or enum.  */
9569       if (declares_class_or_enum)
9570         *declares_class_or_enum = 1;
9571
9572       /* Fall through.  */
9573     case RID_TYPENAME:
9574       /* Look for an elaborated-type-specifier.  */
9575       type_spec
9576         = (cp_parser_elaborated_type_specifier
9577            (parser,
9578             decl_specs && decl_specs->specs[(int) ds_friend],
9579             is_declaration));
9580       if (decl_specs)
9581         cp_parser_set_decl_spec_type (decl_specs,
9582                                       type_spec,
9583                                       /*user_defined_p=*/true);
9584       return type_spec;
9585
9586     case RID_CONST:
9587       ds = ds_const;
9588       if (is_cv_qualifier)
9589         *is_cv_qualifier = true;
9590       break;
9591
9592     case RID_VOLATILE:
9593       ds = ds_volatile;
9594       if (is_cv_qualifier)
9595         *is_cv_qualifier = true;
9596       break;
9597
9598     case RID_RESTRICT:
9599       ds = ds_restrict;
9600       if (is_cv_qualifier)
9601         *is_cv_qualifier = true;
9602       break;
9603
9604     case RID_COMPLEX:
9605       /* The `__complex__' keyword is a GNU extension.  */
9606       ds = ds_complex;
9607       break;
9608
9609     default:
9610       break;
9611     }
9612
9613   /* Handle simple keywords.  */
9614   if (ds != ds_last)
9615     {
9616       if (decl_specs)
9617         {
9618           ++decl_specs->specs[(int)ds];
9619           decl_specs->any_specifiers_p = true;
9620         }
9621       return cp_lexer_consume_token (parser->lexer)->value;
9622     }
9623
9624   /* If we do not already have a type-specifier, assume we are looking
9625      at a simple-type-specifier.  */
9626   type_spec = cp_parser_simple_type_specifier (parser,
9627                                                decl_specs,
9628                                                flags);
9629
9630   /* If we didn't find a type-specifier, and a type-specifier was not
9631      optional in this context, issue an error message.  */
9632   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9633     {
9634       cp_parser_error (parser, "expected type specifier");
9635       return error_mark_node;
9636     }
9637
9638   return type_spec;
9639 }
9640
9641 /* Parse a simple-type-specifier.
9642
9643    simple-type-specifier:
9644      :: [opt] nested-name-specifier [opt] type-name
9645      :: [opt] nested-name-specifier template template-id
9646      char
9647      wchar_t
9648      bool
9649      short
9650      int
9651      long
9652      signed
9653      unsigned
9654      float
9655      double
9656      void
9657
9658    GNU Extension:
9659
9660    simple-type-specifier:
9661      __typeof__ unary-expression
9662      __typeof__ ( type-id )
9663
9664    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9665    appropriately updated.  */
9666
9667 static tree
9668 cp_parser_simple_type_specifier (cp_parser* parser,
9669                                  cp_decl_specifier_seq *decl_specs,
9670                                  cp_parser_flags flags)
9671 {
9672   tree type = NULL_TREE;
9673   cp_token *token;
9674
9675   /* Peek at the next token.  */
9676   token = cp_lexer_peek_token (parser->lexer);
9677
9678   /* If we're looking at a keyword, things are easy.  */
9679   switch (token->keyword)
9680     {
9681     case RID_CHAR:
9682       if (decl_specs)
9683         decl_specs->explicit_char_p = true;
9684       type = char_type_node;
9685       break;
9686     case RID_WCHAR:
9687       type = wchar_type_node;
9688       break;
9689     case RID_BOOL:
9690       type = boolean_type_node;
9691       break;
9692     case RID_SHORT:
9693       if (decl_specs)
9694         ++decl_specs->specs[(int) ds_short];
9695       type = short_integer_type_node;
9696       break;
9697     case RID_INT:
9698       if (decl_specs)
9699         decl_specs->explicit_int_p = true;
9700       type = integer_type_node;
9701       break;
9702     case RID_LONG:
9703       if (decl_specs)
9704         ++decl_specs->specs[(int) ds_long];
9705       type = long_integer_type_node;
9706       break;
9707     case RID_SIGNED:
9708       if (decl_specs)
9709         ++decl_specs->specs[(int) ds_signed];
9710       type = integer_type_node;
9711       break;
9712     case RID_UNSIGNED:
9713       if (decl_specs)
9714         ++decl_specs->specs[(int) ds_unsigned];
9715       type = unsigned_type_node;
9716       break;
9717     case RID_FLOAT:
9718       type = float_type_node;
9719       break;
9720     case RID_DOUBLE:
9721       type = double_type_node;
9722       break;
9723     case RID_VOID:
9724       type = void_type_node;
9725       break;
9726
9727     case RID_TYPEOF:
9728       /* Consume the `typeof' token.  */
9729       cp_lexer_consume_token (parser->lexer);
9730       /* Parse the operand to `typeof'.  */
9731       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9732       /* If it is not already a TYPE, take its type.  */
9733       if (!TYPE_P (type))
9734         type = finish_typeof (type);
9735
9736       if (decl_specs)
9737         cp_parser_set_decl_spec_type (decl_specs, type,
9738                                       /*user_defined_p=*/true);
9739
9740       return type;
9741
9742     default:
9743       break;
9744     }
9745
9746   /* If the type-specifier was for a built-in type, we're done.  */
9747   if (type)
9748     {
9749       tree id;
9750
9751       /* Record the type.  */
9752       if (decl_specs
9753           && (token->keyword != RID_SIGNED
9754               && token->keyword != RID_UNSIGNED
9755               && token->keyword != RID_SHORT
9756               && token->keyword != RID_LONG))
9757         cp_parser_set_decl_spec_type (decl_specs,
9758                                       type,
9759                                       /*user_defined=*/false);
9760       if (decl_specs)
9761         decl_specs->any_specifiers_p = true;
9762
9763       /* Consume the token.  */
9764       id = cp_lexer_consume_token (parser->lexer)->value;
9765
9766       /* There is no valid C++ program where a non-template type is
9767          followed by a "<".  That usually indicates that the user thought
9768          that the type was a template.  */
9769       cp_parser_check_for_invalid_template_id (parser, type);
9770
9771       return TYPE_NAME (type);
9772     }
9773
9774   /* The type-specifier must be a user-defined type.  */
9775   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9776     {
9777       bool qualified_p;
9778       bool global_p;
9779
9780       /* Don't gobble tokens or issue error messages if this is an
9781          optional type-specifier.  */
9782       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9783         cp_parser_parse_tentatively (parser);
9784
9785       /* Look for the optional `::' operator.  */
9786       global_p
9787         = (cp_parser_global_scope_opt (parser,
9788                                        /*current_scope_valid_p=*/false)
9789            != NULL_TREE);
9790       /* Look for the nested-name specifier.  */
9791       qualified_p
9792         = (cp_parser_nested_name_specifier_opt (parser,
9793                                                 /*typename_keyword_p=*/false,
9794                                                 /*check_dependency_p=*/true,
9795                                                 /*type_p=*/false,
9796                                                 /*is_declaration=*/false)
9797            != NULL_TREE);
9798       /* If we have seen a nested-name-specifier, and the next token
9799          is `template', then we are using the template-id production.  */
9800       if (parser->scope
9801           && cp_parser_optional_template_keyword (parser))
9802         {
9803           /* Look for the template-id.  */
9804           type = cp_parser_template_id (parser,
9805                                         /*template_keyword_p=*/true,
9806                                         /*check_dependency_p=*/true,
9807                                         /*is_declaration=*/false);
9808           /* If the template-id did not name a type, we are out of
9809              luck.  */
9810           if (TREE_CODE (type) != TYPE_DECL)
9811             {
9812               cp_parser_error (parser, "expected template-id for type");
9813               type = NULL_TREE;
9814             }
9815         }
9816       /* Otherwise, look for a type-name.  */
9817       else
9818         type = cp_parser_type_name (parser);
9819       /* Keep track of all name-lookups performed in class scopes.  */
9820       if (type
9821           && !global_p
9822           && !qualified_p
9823           && TREE_CODE (type) == TYPE_DECL
9824           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9825         maybe_note_name_used_in_class (DECL_NAME (type), type);
9826       /* If it didn't work out, we don't have a TYPE.  */
9827       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9828           && !cp_parser_parse_definitely (parser))
9829         type = NULL_TREE;
9830       if (type && decl_specs)
9831         cp_parser_set_decl_spec_type (decl_specs, type,
9832                                       /*user_defined=*/true);
9833     }
9834
9835   /* If we didn't get a type-name, issue an error message.  */
9836   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9837     {
9838       cp_parser_error (parser, "expected type-name");
9839       return error_mark_node;
9840     }
9841
9842   /* There is no valid C++ program where a non-template type is
9843      followed by a "<".  That usually indicates that the user thought
9844      that the type was a template.  */
9845   if (type && type != error_mark_node)
9846     {
9847       /* As a last-ditch effort, see if TYPE is an Objective-C type.
9848          If it is, then the '<'...'>' enclose protocol names rather than
9849          template arguments, and so everything is fine.  */
9850       if (c_dialect_objc ()
9851           && (objc_is_id (type) || objc_is_class_name (type)))
9852         {
9853           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9854           tree qual_type = objc_get_protocol_qualified_type (type, protos);
9855
9856           /* Clobber the "unqualified" type previously entered into
9857              DECL_SPECS with the new, improved protocol-qualified version.  */
9858           if (decl_specs)
9859             decl_specs->type = qual_type;
9860
9861           return qual_type;
9862         }
9863
9864       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9865     }
9866
9867   return type;
9868 }
9869
9870 /* Parse a type-name.
9871
9872    type-name:
9873      class-name
9874      enum-name
9875      typedef-name
9876
9877    enum-name:
9878      identifier
9879
9880    typedef-name:
9881      identifier
9882
9883    Returns a TYPE_DECL for the type.  */
9884
9885 static tree
9886 cp_parser_type_name (cp_parser* parser)
9887 {
9888   tree type_decl;
9889   tree identifier;
9890
9891   /* We can't know yet whether it is a class-name or not.  */
9892   cp_parser_parse_tentatively (parser);
9893   /* Try a class-name.  */
9894   type_decl = cp_parser_class_name (parser,
9895                                     /*typename_keyword_p=*/false,
9896                                     /*template_keyword_p=*/false,
9897                                     none_type,
9898                                     /*check_dependency_p=*/true,
9899                                     /*class_head_p=*/false,
9900                                     /*is_declaration=*/false);
9901   /* If it's not a class-name, keep looking.  */
9902   if (!cp_parser_parse_definitely (parser))
9903     {
9904       /* It must be a typedef-name or an enum-name.  */
9905       identifier = cp_parser_identifier (parser);
9906       if (identifier == error_mark_node)
9907         return error_mark_node;
9908
9909       /* Look up the type-name.  */
9910       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9911
9912       if (TREE_CODE (type_decl) != TYPE_DECL
9913           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9914         {
9915           /* See if this is an Objective-C type.  */
9916           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9917           tree type = objc_get_protocol_qualified_type (identifier, protos);
9918           if (type)
9919             type_decl = TYPE_NAME (type);
9920         }
9921
9922       /* Issue an error if we did not find a type-name.  */
9923       if (TREE_CODE (type_decl) != TYPE_DECL)
9924         {
9925           if (!cp_parser_simulate_error (parser))
9926             cp_parser_name_lookup_error (parser, identifier, type_decl,
9927                                          "is not a type");
9928           type_decl = error_mark_node;
9929         }
9930       /* Remember that the name was used in the definition of the
9931          current class so that we can check later to see if the
9932          meaning would have been different after the class was
9933          entirely defined.  */
9934       else if (type_decl != error_mark_node
9935                && !parser->scope)
9936         maybe_note_name_used_in_class (identifier, type_decl);
9937     }
9938
9939   return type_decl;
9940 }
9941
9942
9943 /* Parse an elaborated-type-specifier.  Note that the grammar given
9944    here incorporates the resolution to DR68.
9945
9946    elaborated-type-specifier:
9947      class-key :: [opt] nested-name-specifier [opt] identifier
9948      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9949      enum :: [opt] nested-name-specifier [opt] identifier
9950      typename :: [opt] nested-name-specifier identifier
9951      typename :: [opt] nested-name-specifier template [opt]
9952        template-id
9953
9954    GNU extension:
9955
9956    elaborated-type-specifier:
9957      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9958      class-key attributes :: [opt] nested-name-specifier [opt]
9959                template [opt] template-id
9960      enum attributes :: [opt] nested-name-specifier [opt] identifier
9961
9962    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9963    declared `friend'.  If IS_DECLARATION is TRUE, then this
9964    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9965    something is being declared.
9966
9967    Returns the TYPE specified.  */
9968
9969 static tree
9970 cp_parser_elaborated_type_specifier (cp_parser* parser,
9971                                      bool is_friend,
9972                                      bool is_declaration)
9973 {
9974   enum tag_types tag_type;
9975   tree identifier;
9976   tree type = NULL_TREE;
9977   tree attributes = NULL_TREE;
9978
9979   /* See if we're looking at the `enum' keyword.  */
9980   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9981     {
9982       /* Consume the `enum' token.  */
9983       cp_lexer_consume_token (parser->lexer);
9984       /* Remember that it's an enumeration type.  */
9985       tag_type = enum_type;
9986       /* Parse the attributes.  */
9987       attributes = cp_parser_attributes_opt (parser);
9988     }
9989   /* Or, it might be `typename'.  */
9990   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9991                                            RID_TYPENAME))
9992     {
9993       /* Consume the `typename' token.  */
9994       cp_lexer_consume_token (parser->lexer);
9995       /* Remember that it's a `typename' type.  */
9996       tag_type = typename_type;
9997       /* The `typename' keyword is only allowed in templates.  */
9998       if (!processing_template_decl)
9999         pedwarn ("using %<typename%> outside of template");
10000     }
10001   /* Otherwise it must be a class-key.  */
10002   else
10003     {
10004       tag_type = cp_parser_class_key (parser);
10005       if (tag_type == none_type)
10006         return error_mark_node;
10007       /* Parse the attributes.  */
10008       attributes = cp_parser_attributes_opt (parser);
10009     }
10010
10011   /* Look for the `::' operator.  */
10012   cp_parser_global_scope_opt (parser,
10013                               /*current_scope_valid_p=*/false);
10014   /* Look for the nested-name-specifier.  */
10015   if (tag_type == typename_type)
10016     {
10017       if (!cp_parser_nested_name_specifier (parser,
10018                                            /*typename_keyword_p=*/true,
10019                                            /*check_dependency_p=*/true,
10020                                            /*type_p=*/true,
10021                                             is_declaration))
10022         return error_mark_node;
10023     }
10024   else
10025     /* Even though `typename' is not present, the proposed resolution
10026        to Core Issue 180 says that in `class A<T>::B', `B' should be
10027        considered a type-name, even if `A<T>' is dependent.  */
10028     cp_parser_nested_name_specifier_opt (parser,
10029                                          /*typename_keyword_p=*/true,
10030                                          /*check_dependency_p=*/true,
10031                                          /*type_p=*/true,
10032                                          is_declaration);
10033   /* For everything but enumeration types, consider a template-id.  */
10034   if (tag_type != enum_type)
10035     {
10036       bool template_p = false;
10037       tree decl;
10038
10039       /* Allow the `template' keyword.  */
10040       template_p = cp_parser_optional_template_keyword (parser);
10041       /* If we didn't see `template', we don't know if there's a
10042          template-id or not.  */
10043       if (!template_p)
10044         cp_parser_parse_tentatively (parser);
10045       /* Parse the template-id.  */
10046       decl = cp_parser_template_id (parser, template_p,
10047                                     /*check_dependency_p=*/true,
10048                                     is_declaration);
10049       /* If we didn't find a template-id, look for an ordinary
10050          identifier.  */
10051       if (!template_p && !cp_parser_parse_definitely (parser))
10052         ;
10053       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10054          in effect, then we must assume that, upon instantiation, the
10055          template will correspond to a class.  */
10056       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10057                && tag_type == typename_type)
10058         type = make_typename_type (parser->scope, decl,
10059                                    typename_type,
10060                                    /*complain=*/tf_error);
10061       else
10062         type = TREE_TYPE (decl);
10063     }
10064
10065   /* For an enumeration type, consider only a plain identifier.  */
10066   if (!type)
10067     {
10068       identifier = cp_parser_identifier (parser);
10069
10070       if (identifier == error_mark_node)
10071         {
10072           parser->scope = NULL_TREE;
10073           return error_mark_node;
10074         }
10075
10076       /* For a `typename', we needn't call xref_tag.  */
10077       if (tag_type == typename_type
10078           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10079         return cp_parser_make_typename_type (parser, parser->scope,
10080                                              identifier);
10081       /* Look up a qualified name in the usual way.  */
10082       if (parser->scope)
10083         {
10084           tree decl;
10085
10086           decl = cp_parser_lookup_name (parser, identifier,
10087                                         tag_type,
10088                                         /*is_template=*/false,
10089                                         /*is_namespace=*/false,
10090                                         /*check_dependency=*/true,
10091                                         /*ambiguous_decls=*/NULL);
10092
10093           /* If we are parsing friend declaration, DECL may be a
10094              TEMPLATE_DECL tree node here.  However, we need to check
10095              whether this TEMPLATE_DECL results in valid code.  Consider
10096              the following example:
10097
10098                namespace N {
10099                  template <class T> class C {};
10100                }
10101                class X {
10102                  template <class T> friend class N::C; // #1, valid code
10103                };
10104                template <class T> class Y {
10105                  friend class N::C;                    // #2, invalid code
10106                };
10107
10108              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10109              name lookup of `N::C'.  We see that friend declaration must
10110              be template for the code to be valid.  Note that
10111              processing_template_decl does not work here since it is
10112              always 1 for the above two cases.  */
10113
10114           decl = (cp_parser_maybe_treat_template_as_class
10115                   (decl, /*tag_name_p=*/is_friend
10116                          && parser->num_template_parameter_lists));
10117
10118           if (TREE_CODE (decl) != TYPE_DECL)
10119             {
10120               cp_parser_diagnose_invalid_type_name (parser,
10121                                                     parser->scope,
10122                                                     identifier);
10123               return error_mark_node;
10124             }
10125
10126           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10127             check_elaborated_type_specifier
10128               (tag_type, decl,
10129                (parser->num_template_parameter_lists
10130                 || DECL_SELF_REFERENCE_P (decl)));
10131
10132           type = TREE_TYPE (decl);
10133         }
10134       else
10135         {
10136           /* An elaborated-type-specifier sometimes introduces a new type and
10137              sometimes names an existing type.  Normally, the rule is that it
10138              introduces a new type only if there is not an existing type of
10139              the same name already in scope.  For example, given:
10140
10141                struct S {};
10142                void f() { struct S s; }
10143
10144              the `struct S' in the body of `f' is the same `struct S' as in
10145              the global scope; the existing definition is used.  However, if
10146              there were no global declaration, this would introduce a new
10147              local class named `S'.
10148
10149              An exception to this rule applies to the following code:
10150
10151                namespace N { struct S; }
10152
10153              Here, the elaborated-type-specifier names a new type
10154              unconditionally; even if there is already an `S' in the
10155              containing scope this declaration names a new type.
10156              This exception only applies if the elaborated-type-specifier
10157              forms the complete declaration:
10158
10159                [class.name]
10160
10161                A declaration consisting solely of `class-key identifier ;' is
10162                either a redeclaration of the name in the current scope or a
10163                forward declaration of the identifier as a class name.  It
10164                introduces the name into the current scope.
10165
10166              We are in this situation precisely when the next token is a `;'.
10167
10168              An exception to the exception is that a `friend' declaration does
10169              *not* name a new type; i.e., given:
10170
10171                struct S { friend struct T; };
10172
10173              `T' is not a new type in the scope of `S'.
10174
10175              Also, `new struct S' or `sizeof (struct S)' never results in the
10176              definition of a new type; a new type can only be declared in a
10177              declaration context.  */
10178
10179           tag_scope ts;
10180           bool template_p;
10181
10182           if (is_friend)
10183             /* Friends have special name lookup rules.  */
10184             ts = ts_within_enclosing_non_class;
10185           else if (is_declaration
10186                    && cp_lexer_next_token_is (parser->lexer,
10187                                               CPP_SEMICOLON))
10188             /* This is a `class-key identifier ;' */
10189             ts = ts_current;
10190           else
10191             ts = ts_global;
10192
10193           /* Warn about attributes. They are ignored.  */
10194           if (attributes)
10195             warning (OPT_Wattributes,
10196                      "type attributes are honored only at type definition");
10197
10198           template_p = 
10199             (parser->num_template_parameter_lists
10200              && (cp_parser_next_token_starts_class_definition_p (parser)
10201                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10202           /* An unqualified name was used to reference this type, so
10203              there were no qualifying templates.  */
10204           if (!cp_parser_check_template_parameters (parser, 
10205                                                     /*num_templates=*/0))
10206             return error_mark_node;
10207           type = xref_tag (tag_type, identifier, ts, template_p);
10208         }
10209     }
10210   if (tag_type != enum_type)
10211     cp_parser_check_class_key (tag_type, type);
10212
10213   /* A "<" cannot follow an elaborated type specifier.  If that
10214      happens, the user was probably trying to form a template-id.  */
10215   cp_parser_check_for_invalid_template_id (parser, type);
10216
10217   return type;
10218 }
10219
10220 /* Parse an enum-specifier.
10221
10222    enum-specifier:
10223      enum identifier [opt] { enumerator-list [opt] }
10224
10225    GNU Extensions:
10226      enum identifier [opt] { enumerator-list [opt] } attributes
10227
10228    Returns an ENUM_TYPE representing the enumeration.  */
10229
10230 static tree
10231 cp_parser_enum_specifier (cp_parser* parser)
10232 {
10233   tree identifier;
10234   tree type;
10235
10236   /* Caller guarantees that the current token is 'enum', an identifier
10237      possibly follows, and the token after that is an opening brace.
10238      If we don't have an identifier, fabricate an anonymous name for
10239      the enumeration being defined.  */
10240   cp_lexer_consume_token (parser->lexer);
10241
10242   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10243     identifier = cp_parser_identifier (parser);
10244   else
10245     identifier = make_anon_name ();
10246
10247   /* Issue an error message if type-definitions are forbidden here.  */
10248   cp_parser_check_type_definition (parser);
10249
10250   /* Create the new type.  We do this before consuming the opening brace
10251      so the enum will be recorded as being on the line of its tag (or the
10252      'enum' keyword, if there is no tag).  */
10253   type = start_enum (identifier);
10254
10255   /* Consume the opening brace.  */
10256   cp_lexer_consume_token (parser->lexer);
10257
10258   /* If the next token is not '}', then there are some enumerators.  */
10259   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10260     cp_parser_enumerator_list (parser, type);
10261
10262   /* Consume the final '}'.  */
10263   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10264
10265   /* Look for trailing attributes to apply to this enumeration, and
10266      apply them if appropriate.  */
10267   if (cp_parser_allow_gnu_extensions_p (parser))
10268     {
10269       tree trailing_attr = cp_parser_attributes_opt (parser);
10270       cplus_decl_attributes (&type,
10271                              trailing_attr,
10272                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10273     }
10274
10275   /* Finish up the enumeration.  */
10276   finish_enum (type);
10277
10278   return type;
10279 }
10280
10281 /* Parse an enumerator-list.  The enumerators all have the indicated
10282    TYPE.
10283
10284    enumerator-list:
10285      enumerator-definition
10286      enumerator-list , enumerator-definition  */
10287
10288 static void
10289 cp_parser_enumerator_list (cp_parser* parser, tree type)
10290 {
10291   while (true)
10292     {
10293       /* Parse an enumerator-definition.  */
10294       cp_parser_enumerator_definition (parser, type);
10295
10296       /* If the next token is not a ',', we've reached the end of
10297          the list.  */
10298       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10299         break;
10300       /* Otherwise, consume the `,' and keep going.  */
10301       cp_lexer_consume_token (parser->lexer);
10302       /* If the next token is a `}', there is a trailing comma.  */
10303       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10304         {
10305           if (pedantic && !in_system_header)
10306             pedwarn ("comma at end of enumerator list");
10307           break;
10308         }
10309     }
10310 }
10311
10312 /* Parse an enumerator-definition.  The enumerator has the indicated
10313    TYPE.
10314
10315    enumerator-definition:
10316      enumerator
10317      enumerator = constant-expression
10318
10319    enumerator:
10320      identifier  */
10321
10322 static void
10323 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10324 {
10325   tree identifier;
10326   tree value;
10327
10328   /* Look for the identifier.  */
10329   identifier = cp_parser_identifier (parser);
10330   if (identifier == error_mark_node)
10331     return;
10332
10333   /* If the next token is an '=', then there is an explicit value.  */
10334   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10335     {
10336       /* Consume the `=' token.  */
10337       cp_lexer_consume_token (parser->lexer);
10338       /* Parse the value.  */
10339       value = cp_parser_constant_expression (parser,
10340                                              /*allow_non_constant_p=*/false,
10341                                              NULL);
10342     }
10343   else
10344     value = NULL_TREE;
10345
10346   /* Create the enumerator.  */
10347   build_enumerator (identifier, value, type);
10348 }
10349
10350 /* Parse a namespace-name.
10351
10352    namespace-name:
10353      original-namespace-name
10354      namespace-alias
10355
10356    Returns the NAMESPACE_DECL for the namespace.  */
10357
10358 static tree
10359 cp_parser_namespace_name (cp_parser* parser)
10360 {
10361   tree identifier;
10362   tree namespace_decl;
10363
10364   /* Get the name of the namespace.  */
10365   identifier = cp_parser_identifier (parser);
10366   if (identifier == error_mark_node)
10367     return error_mark_node;
10368
10369   /* Look up the identifier in the currently active scope.  Look only
10370      for namespaces, due to:
10371
10372        [basic.lookup.udir]
10373
10374        When looking up a namespace-name in a using-directive or alias
10375        definition, only namespace names are considered.
10376
10377      And:
10378
10379        [basic.lookup.qual]
10380
10381        During the lookup of a name preceding the :: scope resolution
10382        operator, object, function, and enumerator names are ignored.
10383
10384      (Note that cp_parser_class_or_namespace_name only calls this
10385      function if the token after the name is the scope resolution
10386      operator.)  */
10387   namespace_decl = cp_parser_lookup_name (parser, identifier,
10388                                           none_type,
10389                                           /*is_template=*/false,
10390                                           /*is_namespace=*/true,
10391                                           /*check_dependency=*/true,
10392                                           /*ambiguous_decls=*/NULL);
10393   /* If it's not a namespace, issue an error.  */
10394   if (namespace_decl == error_mark_node
10395       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10396     {
10397       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10398         error ("%qD is not a namespace-name", identifier);
10399       cp_parser_error (parser, "expected namespace-name");
10400       namespace_decl = error_mark_node;
10401     }
10402
10403   return namespace_decl;
10404 }
10405
10406 /* Parse a namespace-definition.
10407
10408    namespace-definition:
10409      named-namespace-definition
10410      unnamed-namespace-definition
10411
10412    named-namespace-definition:
10413      original-namespace-definition
10414      extension-namespace-definition
10415
10416    original-namespace-definition:
10417      namespace identifier { namespace-body }
10418
10419    extension-namespace-definition:
10420      namespace original-namespace-name { namespace-body }
10421
10422    unnamed-namespace-definition:
10423      namespace { namespace-body } */
10424
10425 static void
10426 cp_parser_namespace_definition (cp_parser* parser)
10427 {
10428   tree identifier;
10429
10430   /* Look for the `namespace' keyword.  */
10431   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10432
10433   /* Get the name of the namespace.  We do not attempt to distinguish
10434      between an original-namespace-definition and an
10435      extension-namespace-definition at this point.  The semantic
10436      analysis routines are responsible for that.  */
10437   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10438     identifier = cp_parser_identifier (parser);
10439   else
10440     identifier = NULL_TREE;
10441
10442   /* Look for the `{' to start the namespace.  */
10443   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10444   /* Start the namespace.  */
10445   push_namespace (identifier);
10446   /* Parse the body of the namespace.  */
10447   cp_parser_namespace_body (parser);
10448   /* Finish the namespace.  */
10449   pop_namespace ();
10450   /* Look for the final `}'.  */
10451   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10452 }
10453
10454 /* Parse a namespace-body.
10455
10456    namespace-body:
10457      declaration-seq [opt]  */
10458
10459 static void
10460 cp_parser_namespace_body (cp_parser* parser)
10461 {
10462   cp_parser_declaration_seq_opt (parser);
10463 }
10464
10465 /* Parse a namespace-alias-definition.
10466
10467    namespace-alias-definition:
10468      namespace identifier = qualified-namespace-specifier ;  */
10469
10470 static void
10471 cp_parser_namespace_alias_definition (cp_parser* parser)
10472 {
10473   tree identifier;
10474   tree namespace_specifier;
10475
10476   /* Look for the `namespace' keyword.  */
10477   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10478   /* Look for the identifier.  */
10479   identifier = cp_parser_identifier (parser);
10480   if (identifier == error_mark_node)
10481     return;
10482   /* Look for the `=' token.  */
10483   cp_parser_require (parser, CPP_EQ, "`='");
10484   /* Look for the qualified-namespace-specifier.  */
10485   namespace_specifier
10486     = cp_parser_qualified_namespace_specifier (parser);
10487   /* Look for the `;' token.  */
10488   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10489
10490   /* Register the alias in the symbol table.  */
10491   do_namespace_alias (identifier, namespace_specifier);
10492 }
10493
10494 /* Parse a qualified-namespace-specifier.
10495
10496    qualified-namespace-specifier:
10497      :: [opt] nested-name-specifier [opt] namespace-name
10498
10499    Returns a NAMESPACE_DECL corresponding to the specified
10500    namespace.  */
10501
10502 static tree
10503 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10504 {
10505   /* Look for the optional `::'.  */
10506   cp_parser_global_scope_opt (parser,
10507                               /*current_scope_valid_p=*/false);
10508
10509   /* Look for the optional nested-name-specifier.  */
10510   cp_parser_nested_name_specifier_opt (parser,
10511                                        /*typename_keyword_p=*/false,
10512                                        /*check_dependency_p=*/true,
10513                                        /*type_p=*/false,
10514                                        /*is_declaration=*/true);
10515
10516   return cp_parser_namespace_name (parser);
10517 }
10518
10519 /* Parse a using-declaration.
10520
10521    using-declaration:
10522      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10523      using :: unqualified-id ;  */
10524
10525 static void
10526 cp_parser_using_declaration (cp_parser* parser)
10527 {
10528   cp_token *token;
10529   bool typename_p = false;
10530   bool global_scope_p;
10531   tree decl;
10532   tree identifier;
10533   tree qscope;
10534
10535   /* Look for the `using' keyword.  */
10536   cp_parser_require_keyword (parser, RID_USING, "`using'");
10537
10538   /* Peek at the next token.  */
10539   token = cp_lexer_peek_token (parser->lexer);
10540   /* See if it's `typename'.  */
10541   if (token->keyword == RID_TYPENAME)
10542     {
10543       /* Remember that we've seen it.  */
10544       typename_p = true;
10545       /* Consume the `typename' token.  */
10546       cp_lexer_consume_token (parser->lexer);
10547     }
10548
10549   /* Look for the optional global scope qualification.  */
10550   global_scope_p
10551     = (cp_parser_global_scope_opt (parser,
10552                                    /*current_scope_valid_p=*/false)
10553        != NULL_TREE);
10554
10555   /* If we saw `typename', or didn't see `::', then there must be a
10556      nested-name-specifier present.  */
10557   if (typename_p || !global_scope_p)
10558     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10559                                               /*check_dependency_p=*/true,
10560                                               /*type_p=*/false,
10561                                               /*is_declaration=*/true);
10562   /* Otherwise, we could be in either of the two productions.  In that
10563      case, treat the nested-name-specifier as optional.  */
10564   else
10565     qscope = cp_parser_nested_name_specifier_opt (parser,
10566                                                   /*typename_keyword_p=*/false,
10567                                                   /*check_dependency_p=*/true,
10568                                                   /*type_p=*/false,
10569                                                   /*is_declaration=*/true);
10570   if (!qscope)
10571     qscope = global_namespace;
10572
10573   /* Parse the unqualified-id.  */
10574   identifier = cp_parser_unqualified_id (parser,
10575                                          /*template_keyword_p=*/false,
10576                                          /*check_dependency_p=*/true,
10577                                          /*declarator_p=*/true);
10578
10579   /* The function we call to handle a using-declaration is different
10580      depending on what scope we are in.  */
10581   if (qscope == error_mark_node || identifier == error_mark_node)
10582     ;
10583   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10584            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10585     /* [namespace.udecl]
10586
10587        A using declaration shall not name a template-id.  */
10588     error ("a template-id may not appear in a using-declaration");
10589   else
10590     {
10591       if (at_class_scope_p ())
10592         {
10593           /* Create the USING_DECL.  */
10594           decl = do_class_using_decl (parser->scope, identifier);
10595           /* Add it to the list of members in this class.  */
10596           finish_member_declaration (decl);
10597         }
10598       else
10599         {
10600           decl = cp_parser_lookup_name_simple (parser, identifier);
10601           if (decl == error_mark_node)
10602             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10603           else if (!at_namespace_scope_p ())
10604             do_local_using_decl (decl, qscope, identifier);
10605           else
10606             do_toplevel_using_decl (decl, qscope, identifier);
10607         }
10608     }
10609
10610   /* Look for the final `;'.  */
10611   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10612 }
10613
10614 /* Parse a using-directive.
10615
10616    using-directive:
10617      using namespace :: [opt] nested-name-specifier [opt]
10618        namespace-name ;  */
10619
10620 static void
10621 cp_parser_using_directive (cp_parser* parser)
10622 {
10623   tree namespace_decl;
10624   tree attribs;
10625
10626   /* Look for the `using' keyword.  */
10627   cp_parser_require_keyword (parser, RID_USING, "`using'");
10628   /* And the `namespace' keyword.  */
10629   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10630   /* Look for the optional `::' operator.  */
10631   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10632   /* And the optional nested-name-specifier.  */
10633   cp_parser_nested_name_specifier_opt (parser,
10634                                        /*typename_keyword_p=*/false,
10635                                        /*check_dependency_p=*/true,
10636                                        /*type_p=*/false,
10637                                        /*is_declaration=*/true);
10638   /* Get the namespace being used.  */
10639   namespace_decl = cp_parser_namespace_name (parser);
10640   /* And any specified attributes.  */
10641   attribs = cp_parser_attributes_opt (parser);
10642   /* Update the symbol table.  */
10643   parse_using_directive (namespace_decl, attribs);
10644   /* Look for the final `;'.  */
10645   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10646 }
10647
10648 /* Parse an asm-definition.
10649
10650    asm-definition:
10651      asm ( string-literal ) ;
10652
10653    GNU Extension:
10654
10655    asm-definition:
10656      asm volatile [opt] ( string-literal ) ;
10657      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10658      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10659                           : asm-operand-list [opt] ) ;
10660      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10661                           : asm-operand-list [opt]
10662                           : asm-operand-list [opt] ) ;  */
10663
10664 static void
10665 cp_parser_asm_definition (cp_parser* parser)
10666 {
10667   tree string;
10668   tree outputs = NULL_TREE;
10669   tree inputs = NULL_TREE;
10670   tree clobbers = NULL_TREE;
10671   tree asm_stmt;
10672   bool volatile_p = false;
10673   bool extended_p = false;
10674
10675   /* Look for the `asm' keyword.  */
10676   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10677   /* See if the next token is `volatile'.  */
10678   if (cp_parser_allow_gnu_extensions_p (parser)
10679       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10680     {
10681       /* Remember that we saw the `volatile' keyword.  */
10682       volatile_p = true;
10683       /* Consume the token.  */
10684       cp_lexer_consume_token (parser->lexer);
10685     }
10686   /* Look for the opening `('.  */
10687   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10688     return;
10689   /* Look for the string.  */
10690   string = cp_parser_string_literal (parser, false, false);
10691   if (string == error_mark_node)
10692     {
10693       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10694                                              /*consume_paren=*/true);
10695       return;
10696     }
10697
10698   /* If we're allowing GNU extensions, check for the extended assembly
10699      syntax.  Unfortunately, the `:' tokens need not be separated by
10700      a space in C, and so, for compatibility, we tolerate that here
10701      too.  Doing that means that we have to treat the `::' operator as
10702      two `:' tokens.  */
10703   if (cp_parser_allow_gnu_extensions_p (parser)
10704       && at_function_scope_p ()
10705       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10706           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10707     {
10708       bool inputs_p = false;
10709       bool clobbers_p = false;
10710
10711       /* The extended syntax was used.  */
10712       extended_p = true;
10713
10714       /* Look for outputs.  */
10715       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10716         {
10717           /* Consume the `:'.  */
10718           cp_lexer_consume_token (parser->lexer);
10719           /* Parse the output-operands.  */
10720           if (cp_lexer_next_token_is_not (parser->lexer,
10721                                           CPP_COLON)
10722               && cp_lexer_next_token_is_not (parser->lexer,
10723                                              CPP_SCOPE)
10724               && cp_lexer_next_token_is_not (parser->lexer,
10725                                              CPP_CLOSE_PAREN))
10726             outputs = cp_parser_asm_operand_list (parser);
10727         }
10728       /* If the next token is `::', there are no outputs, and the
10729          next token is the beginning of the inputs.  */
10730       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10731         /* The inputs are coming next.  */
10732         inputs_p = true;
10733
10734       /* Look for inputs.  */
10735       if (inputs_p
10736           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10737         {
10738           /* Consume the `:' or `::'.  */
10739           cp_lexer_consume_token (parser->lexer);
10740           /* Parse the output-operands.  */
10741           if (cp_lexer_next_token_is_not (parser->lexer,
10742                                           CPP_COLON)
10743               && cp_lexer_next_token_is_not (parser->lexer,
10744                                              CPP_CLOSE_PAREN))
10745             inputs = cp_parser_asm_operand_list (parser);
10746         }
10747       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10748         /* The clobbers are coming next.  */
10749         clobbers_p = true;
10750
10751       /* Look for clobbers.  */
10752       if (clobbers_p
10753           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10754         {
10755           /* Consume the `:' or `::'.  */
10756           cp_lexer_consume_token (parser->lexer);
10757           /* Parse the clobbers.  */
10758           if (cp_lexer_next_token_is_not (parser->lexer,
10759                                           CPP_CLOSE_PAREN))
10760             clobbers = cp_parser_asm_clobber_list (parser);
10761         }
10762     }
10763   /* Look for the closing `)'.  */
10764   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10765     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10766                                            /*consume_paren=*/true);
10767   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10768
10769   /* Create the ASM_EXPR.  */
10770   if (at_function_scope_p ())
10771     {
10772       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10773                                   inputs, clobbers);
10774       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10775       if (!extended_p)
10776         {
10777           tree temp = asm_stmt;
10778           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10779             temp = TREE_OPERAND (temp, 0);
10780
10781           ASM_INPUT_P (temp) = 1;
10782         }
10783     }
10784   else
10785     cgraph_add_asm_node (string);
10786 }
10787
10788 /* Declarators [gram.dcl.decl] */
10789
10790 /* Parse an init-declarator.
10791
10792    init-declarator:
10793      declarator initializer [opt]
10794
10795    GNU Extension:
10796
10797    init-declarator:
10798      declarator asm-specification [opt] attributes [opt] initializer [opt]
10799
10800    function-definition:
10801      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10802        function-body
10803      decl-specifier-seq [opt] declarator function-try-block
10804
10805    GNU Extension:
10806
10807    function-definition:
10808      __extension__ function-definition
10809
10810    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10811    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10812    then this declarator appears in a class scope.  The new DECL created
10813    by this declarator is returned.
10814
10815    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10816    for a function-definition here as well.  If the declarator is a
10817    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10818    be TRUE upon return.  By that point, the function-definition will
10819    have been completely parsed.
10820
10821    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10822    is FALSE.  */
10823
10824 static tree
10825 cp_parser_init_declarator (cp_parser* parser,
10826                            cp_decl_specifier_seq *decl_specifiers,
10827                            bool function_definition_allowed_p,
10828                            bool member_p,
10829                            int declares_class_or_enum,
10830                            bool* function_definition_p)
10831 {
10832   cp_token *token;
10833   cp_declarator *declarator;
10834   tree prefix_attributes;
10835   tree attributes;
10836   tree asm_specification;
10837   tree initializer;
10838   tree decl = NULL_TREE;
10839   tree scope;
10840   bool is_initialized;
10841   bool is_parenthesized_init;
10842   bool is_non_constant_init;
10843   int ctor_dtor_or_conv_p;
10844   bool friend_p;
10845   tree pushed_scope = NULL;
10846
10847   /* Gather the attributes that were provided with the
10848      decl-specifiers.  */
10849   prefix_attributes = decl_specifiers->attributes;
10850
10851   /* Assume that this is not the declarator for a function
10852      definition.  */
10853   if (function_definition_p)
10854     *function_definition_p = false;
10855
10856   /* Defer access checks while parsing the declarator; we cannot know
10857      what names are accessible until we know what is being
10858      declared.  */
10859   resume_deferring_access_checks ();
10860
10861   /* Parse the declarator.  */
10862   declarator
10863     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10864                             &ctor_dtor_or_conv_p,
10865                             /*parenthesized_p=*/NULL,
10866                             /*member_p=*/false);
10867   /* Gather up the deferred checks.  */
10868   stop_deferring_access_checks ();
10869
10870   /* If the DECLARATOR was erroneous, there's no need to go
10871      further.  */
10872   if (declarator == cp_error_declarator)
10873     return error_mark_node;
10874
10875   if (declares_class_or_enum & 2)
10876     cp_parser_check_for_definition_in_return_type (declarator,
10877                                                    decl_specifiers->type);
10878
10879   /* Figure out what scope the entity declared by the DECLARATOR is
10880      located in.  `grokdeclarator' sometimes changes the scope, so
10881      we compute it now.  */
10882   scope = get_scope_of_declarator (declarator);
10883
10884   /* If we're allowing GNU extensions, look for an asm-specification
10885      and attributes.  */
10886   if (cp_parser_allow_gnu_extensions_p (parser))
10887     {
10888       /* Look for an asm-specification.  */
10889       asm_specification = cp_parser_asm_specification_opt (parser);
10890       /* And attributes.  */
10891       attributes = cp_parser_attributes_opt (parser);
10892     }
10893   else
10894     {
10895       asm_specification = NULL_TREE;
10896       attributes = NULL_TREE;
10897     }
10898
10899   /* Peek at the next token.  */
10900   token = cp_lexer_peek_token (parser->lexer);
10901   /* Check to see if the token indicates the start of a
10902      function-definition.  */
10903   if (cp_parser_token_starts_function_definition_p (token))
10904     {
10905       if (!function_definition_allowed_p)
10906         {
10907           /* If a function-definition should not appear here, issue an
10908              error message.  */
10909           cp_parser_error (parser,
10910                            "a function-definition is not allowed here");
10911           return error_mark_node;
10912         }
10913       else
10914         {
10915           /* Neither attributes nor an asm-specification are allowed
10916              on a function-definition.  */
10917           if (asm_specification)
10918             error ("an asm-specification is not allowed on a function-definition");
10919           if (attributes)
10920             error ("attributes are not allowed on a function-definition");
10921           /* This is a function-definition.  */
10922           *function_definition_p = true;
10923
10924           /* Parse the function definition.  */
10925           if (member_p)
10926             decl = cp_parser_save_member_function_body (parser,
10927                                                         decl_specifiers,
10928                                                         declarator,
10929                                                         prefix_attributes);
10930           else
10931             decl
10932               = (cp_parser_function_definition_from_specifiers_and_declarator
10933                  (parser, decl_specifiers, prefix_attributes, declarator));
10934
10935           return decl;
10936         }
10937     }
10938
10939   /* [dcl.dcl]
10940
10941      Only in function declarations for constructors, destructors, and
10942      type conversions can the decl-specifier-seq be omitted.
10943
10944      We explicitly postpone this check past the point where we handle
10945      function-definitions because we tolerate function-definitions
10946      that are missing their return types in some modes.  */
10947   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10948     {
10949       cp_parser_error (parser,
10950                        "expected constructor, destructor, or type conversion");
10951       return error_mark_node;
10952     }
10953
10954   /* An `=' or an `(' indicates an initializer.  */
10955   is_initialized = (token->type == CPP_EQ
10956                      || token->type == CPP_OPEN_PAREN);
10957   /* If the init-declarator isn't initialized and isn't followed by a
10958      `,' or `;', it's not a valid init-declarator.  */
10959   if (!is_initialized
10960       && token->type != CPP_COMMA
10961       && token->type != CPP_SEMICOLON)
10962     {
10963       cp_parser_error (parser, "expected initializer");
10964       return error_mark_node;
10965     }
10966
10967   /* Because start_decl has side-effects, we should only call it if we
10968      know we're going ahead.  By this point, we know that we cannot
10969      possibly be looking at any other construct.  */
10970   cp_parser_commit_to_tentative_parse (parser);
10971
10972   /* If the decl specifiers were bad, issue an error now that we're
10973      sure this was intended to be a declarator.  Then continue
10974      declaring the variable(s), as int, to try to cut down on further
10975      errors.  */
10976   if (decl_specifiers->any_specifiers_p
10977       && decl_specifiers->type == error_mark_node)
10978     {
10979       cp_parser_error (parser, "invalid type in declaration");
10980       decl_specifiers->type = integer_type_node;
10981     }
10982
10983   /* Check to see whether or not this declaration is a friend.  */
10984   friend_p = cp_parser_friend_p (decl_specifiers);
10985
10986   /* Check that the number of template-parameter-lists is OK.  */
10987   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10988     return error_mark_node;
10989
10990   /* Enter the newly declared entry in the symbol table.  If we're
10991      processing a declaration in a class-specifier, we wait until
10992      after processing the initializer.  */
10993   if (!member_p)
10994     {
10995       if (parser->in_unbraced_linkage_specification_p)
10996         {
10997           decl_specifiers->storage_class = sc_extern;
10998           have_extern_spec = false;
10999         }
11000       decl = start_decl (declarator, decl_specifiers,
11001                          is_initialized, attributes, prefix_attributes,
11002                          &pushed_scope);
11003     }
11004   else if (scope)
11005     /* Enter the SCOPE.  That way unqualified names appearing in the
11006        initializer will be looked up in SCOPE.  */
11007     pushed_scope = push_scope (scope);
11008
11009   /* Perform deferred access control checks, now that we know in which
11010      SCOPE the declared entity resides.  */
11011   if (!member_p && decl)
11012     {
11013       tree saved_current_function_decl = NULL_TREE;
11014
11015       /* If the entity being declared is a function, pretend that we
11016          are in its scope.  If it is a `friend', it may have access to
11017          things that would not otherwise be accessible.  */
11018       if (TREE_CODE (decl) == FUNCTION_DECL)
11019         {
11020           saved_current_function_decl = current_function_decl;
11021           current_function_decl = decl;
11022         }
11023
11024       /* Perform the access control checks for the declarator and the
11025          the decl-specifiers.  */
11026       perform_deferred_access_checks ();
11027
11028       /* Restore the saved value.  */
11029       if (TREE_CODE (decl) == FUNCTION_DECL)
11030         current_function_decl = saved_current_function_decl;
11031     }
11032
11033   /* Parse the initializer.  */
11034   if (is_initialized)
11035     initializer = cp_parser_initializer (parser,
11036                                          &is_parenthesized_init,
11037                                          &is_non_constant_init);
11038   else
11039     {
11040       initializer = NULL_TREE;
11041       is_parenthesized_init = false;
11042       is_non_constant_init = true;
11043     }
11044
11045   /* The old parser allows attributes to appear after a parenthesized
11046      initializer.  Mark Mitchell proposed removing this functionality
11047      on the GCC mailing lists on 2002-08-13.  This parser accepts the
11048      attributes -- but ignores them.  */
11049   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11050     if (cp_parser_attributes_opt (parser))
11051       warning (OPT_Wattributes,
11052                "attributes after parenthesized initializer ignored");
11053
11054   /* For an in-class declaration, use `grokfield' to create the
11055      declaration.  */
11056   if (member_p)
11057     {
11058       if (pushed_scope)
11059         {
11060           pop_scope (pushed_scope);
11061           pushed_scope = false;
11062         }
11063       decl = grokfield (declarator, decl_specifiers,
11064                         initializer, /*asmspec=*/NULL_TREE,
11065                         prefix_attributes);
11066       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11067         cp_parser_save_default_args (parser, decl);
11068     }
11069
11070   /* Finish processing the declaration.  But, skip friend
11071      declarations.  */
11072   if (!friend_p && decl && decl != error_mark_node)
11073     {
11074       cp_finish_decl (decl,
11075                       initializer,
11076                       asm_specification,
11077                       /* If the initializer is in parentheses, then this is
11078                          a direct-initialization, which means that an
11079                          `explicit' constructor is OK.  Otherwise, an
11080                          `explicit' constructor cannot be used.  */
11081                       ((is_parenthesized_init || !is_initialized)
11082                      ? 0 : LOOKUP_ONLYCONVERTING));
11083     }
11084   if (!friend_p && pushed_scope)
11085     pop_scope (pushed_scope);
11086
11087   /* Remember whether or not variables were initialized by
11088      constant-expressions.  */
11089   if (decl && TREE_CODE (decl) == VAR_DECL
11090       && is_initialized && !is_non_constant_init)
11091     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
11092
11093   return decl;
11094 }
11095
11096 /* Parse a declarator.
11097
11098    declarator:
11099      direct-declarator
11100      ptr-operator declarator
11101
11102    abstract-declarator:
11103      ptr-operator abstract-declarator [opt]
11104      direct-abstract-declarator
11105
11106    GNU Extensions:
11107
11108    declarator:
11109      attributes [opt] direct-declarator
11110      attributes [opt] ptr-operator declarator
11111
11112    abstract-declarator:
11113      attributes [opt] ptr-operator abstract-declarator [opt]
11114      attributes [opt] direct-abstract-declarator
11115
11116    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11117    detect constructor, destructor or conversion operators. It is set
11118    to -1 if the declarator is a name, and +1 if it is a
11119    function. Otherwise it is set to zero. Usually you just want to
11120    test for >0, but internally the negative value is used.
11121
11122    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11123    a decl-specifier-seq unless it declares a constructor, destructor,
11124    or conversion.  It might seem that we could check this condition in
11125    semantic analysis, rather than parsing, but that makes it difficult
11126    to handle something like `f()'.  We want to notice that there are
11127    no decl-specifiers, and therefore realize that this is an
11128    expression, not a declaration.)
11129
11130    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11131    the declarator is a direct-declarator of the form "(...)".
11132
11133    MEMBER_P is true iff this declarator is a member-declarator.  */
11134
11135 static cp_declarator *
11136 cp_parser_declarator (cp_parser* parser,
11137                       cp_parser_declarator_kind dcl_kind,
11138                       int* ctor_dtor_or_conv_p,
11139                       bool* parenthesized_p,
11140                       bool member_p)
11141 {
11142   cp_token *token;
11143   cp_declarator *declarator;
11144   enum tree_code code;
11145   cp_cv_quals cv_quals;
11146   tree class_type;
11147   tree attributes = NULL_TREE;
11148
11149   /* Assume this is not a constructor, destructor, or type-conversion
11150      operator.  */
11151   if (ctor_dtor_or_conv_p)
11152     *ctor_dtor_or_conv_p = 0;
11153
11154   if (cp_parser_allow_gnu_extensions_p (parser))
11155     attributes = cp_parser_attributes_opt (parser);
11156
11157   /* Peek at the next token.  */
11158   token = cp_lexer_peek_token (parser->lexer);
11159
11160   /* Check for the ptr-operator production.  */
11161   cp_parser_parse_tentatively (parser);
11162   /* Parse the ptr-operator.  */
11163   code = cp_parser_ptr_operator (parser,
11164                                  &class_type,
11165                                  &cv_quals);
11166   /* If that worked, then we have a ptr-operator.  */
11167   if (cp_parser_parse_definitely (parser))
11168     {
11169       /* If a ptr-operator was found, then this declarator was not
11170          parenthesized.  */
11171       if (parenthesized_p)
11172         *parenthesized_p = true;
11173       /* The dependent declarator is optional if we are parsing an
11174          abstract-declarator.  */
11175       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11176         cp_parser_parse_tentatively (parser);
11177
11178       /* Parse the dependent declarator.  */
11179       declarator = cp_parser_declarator (parser, dcl_kind,
11180                                          /*ctor_dtor_or_conv_p=*/NULL,
11181                                          /*parenthesized_p=*/NULL,
11182                                          /*member_p=*/false);
11183
11184       /* If we are parsing an abstract-declarator, we must handle the
11185          case where the dependent declarator is absent.  */
11186       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11187           && !cp_parser_parse_definitely (parser))
11188         declarator = NULL;
11189
11190       /* Build the representation of the ptr-operator.  */
11191       if (class_type)
11192         declarator = make_ptrmem_declarator (cv_quals,
11193                                              class_type,
11194                                              declarator);
11195       else if (code == INDIRECT_REF)
11196         declarator = make_pointer_declarator (cv_quals, declarator);
11197       else
11198         declarator = make_reference_declarator (cv_quals, declarator);
11199     }
11200   /* Everything else is a direct-declarator.  */
11201   else
11202     {
11203       if (parenthesized_p)
11204         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11205                                                    CPP_OPEN_PAREN);
11206       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11207                                                 ctor_dtor_or_conv_p,
11208                                                 member_p);
11209     }
11210
11211   if (attributes && declarator != cp_error_declarator)
11212     declarator->attributes = attributes;
11213
11214   return declarator;
11215 }
11216
11217 /* Parse a direct-declarator or direct-abstract-declarator.
11218
11219    direct-declarator:
11220      declarator-id
11221      direct-declarator ( parameter-declaration-clause )
11222        cv-qualifier-seq [opt]
11223        exception-specification [opt]
11224      direct-declarator [ constant-expression [opt] ]
11225      ( declarator )
11226
11227    direct-abstract-declarator:
11228      direct-abstract-declarator [opt]
11229        ( parameter-declaration-clause )
11230        cv-qualifier-seq [opt]
11231        exception-specification [opt]
11232      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11233      ( abstract-declarator )
11234
11235    Returns a representation of the declarator.  DCL_KIND is
11236    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11237    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11238    we are parsing a direct-declarator.  It is
11239    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11240    of ambiguity we prefer an abstract declarator, as per
11241    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11242    cp_parser_declarator.  */
11243
11244 static cp_declarator *
11245 cp_parser_direct_declarator (cp_parser* parser,
11246                              cp_parser_declarator_kind dcl_kind,
11247                              int* ctor_dtor_or_conv_p,
11248                              bool member_p)
11249 {
11250   cp_token *token;
11251   cp_declarator *declarator = NULL;
11252   tree scope = NULL_TREE;
11253   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11254   bool saved_in_declarator_p = parser->in_declarator_p;
11255   bool first = true;
11256   tree pushed_scope = NULL_TREE;
11257
11258   while (true)
11259     {
11260       /* Peek at the next token.  */
11261       token = cp_lexer_peek_token (parser->lexer);
11262       if (token->type == CPP_OPEN_PAREN)
11263         {
11264           /* This is either a parameter-declaration-clause, or a
11265              parenthesized declarator. When we know we are parsing a
11266              named declarator, it must be a parenthesized declarator
11267              if FIRST is true. For instance, `(int)' is a
11268              parameter-declaration-clause, with an omitted
11269              direct-abstract-declarator. But `((*))', is a
11270              parenthesized abstract declarator. Finally, when T is a
11271              template parameter `(T)' is a
11272              parameter-declaration-clause, and not a parenthesized
11273              named declarator.
11274
11275              We first try and parse a parameter-declaration-clause,
11276              and then try a nested declarator (if FIRST is true).
11277
11278              It is not an error for it not to be a
11279              parameter-declaration-clause, even when FIRST is
11280              false. Consider,
11281
11282                int i (int);
11283                int i (3);
11284
11285              The first is the declaration of a function while the
11286              second is a the definition of a variable, including its
11287              initializer.
11288
11289              Having seen only the parenthesis, we cannot know which of
11290              these two alternatives should be selected.  Even more
11291              complex are examples like:
11292
11293                int i (int (a));
11294                int i (int (3));
11295
11296              The former is a function-declaration; the latter is a
11297              variable initialization.
11298
11299              Thus again, we try a parameter-declaration-clause, and if
11300              that fails, we back out and return.  */
11301
11302           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11303             {
11304               cp_parameter_declarator *params;
11305               unsigned saved_num_template_parameter_lists;
11306
11307               /* In a member-declarator, the only valid interpretation
11308                  of a parenthesis is the start of a
11309                  parameter-declaration-clause.  (It is invalid to
11310                  initialize a static data member with a parenthesized
11311                  initializer; only the "=" form of initialization is
11312                  permitted.)  */
11313               if (!member_p)
11314                 cp_parser_parse_tentatively (parser);
11315
11316               /* Consume the `('.  */
11317               cp_lexer_consume_token (parser->lexer);
11318               if (first)
11319                 {
11320                   /* If this is going to be an abstract declarator, we're
11321                      in a declarator and we can't have default args.  */
11322                   parser->default_arg_ok_p = false;
11323                   parser->in_declarator_p = true;
11324                 }
11325
11326               /* Inside the function parameter list, surrounding
11327                  template-parameter-lists do not apply.  */
11328               saved_num_template_parameter_lists
11329                 = parser->num_template_parameter_lists;
11330               parser->num_template_parameter_lists = 0;
11331
11332               /* Parse the parameter-declaration-clause.  */
11333               params = cp_parser_parameter_declaration_clause (parser);
11334
11335               parser->num_template_parameter_lists
11336                 = saved_num_template_parameter_lists;
11337
11338               /* If all went well, parse the cv-qualifier-seq and the
11339                  exception-specification.  */
11340               if (member_p || cp_parser_parse_definitely (parser))
11341                 {
11342                   cp_cv_quals cv_quals;
11343                   tree exception_specification;
11344
11345                   if (ctor_dtor_or_conv_p)
11346                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11347                   first = false;
11348                   /* Consume the `)'.  */
11349                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11350
11351                   /* Parse the cv-qualifier-seq.  */
11352                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11353                   /* And the exception-specification.  */
11354                   exception_specification
11355                     = cp_parser_exception_specification_opt (parser);
11356
11357                   /* Create the function-declarator.  */
11358                   declarator = make_call_declarator (declarator,
11359                                                      params,
11360                                                      cv_quals,
11361                                                      exception_specification);
11362                   /* Any subsequent parameter lists are to do with
11363                      return type, so are not those of the declared
11364                      function.  */
11365                   parser->default_arg_ok_p = false;
11366
11367                   /* Repeat the main loop.  */
11368                   continue;
11369                 }
11370             }
11371
11372           /* If this is the first, we can try a parenthesized
11373              declarator.  */
11374           if (first)
11375             {
11376               bool saved_in_type_id_in_expr_p;
11377
11378               parser->default_arg_ok_p = saved_default_arg_ok_p;
11379               parser->in_declarator_p = saved_in_declarator_p;
11380
11381               /* Consume the `('.  */
11382               cp_lexer_consume_token (parser->lexer);
11383               /* Parse the nested declarator.  */
11384               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11385               parser->in_type_id_in_expr_p = true;
11386               declarator
11387                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11388                                         /*parenthesized_p=*/NULL,
11389                                         member_p);
11390               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11391               first = false;
11392               /* Expect a `)'.  */
11393               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11394                 declarator = cp_error_declarator;
11395               if (declarator == cp_error_declarator)
11396                 break;
11397
11398               goto handle_declarator;
11399             }
11400           /* Otherwise, we must be done.  */
11401           else
11402             break;
11403         }
11404       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11405                && token->type == CPP_OPEN_SQUARE)
11406         {
11407           /* Parse an array-declarator.  */
11408           tree bounds;
11409
11410           if (ctor_dtor_or_conv_p)
11411             *ctor_dtor_or_conv_p = 0;
11412
11413           first = false;
11414           parser->default_arg_ok_p = false;
11415           parser->in_declarator_p = true;
11416           /* Consume the `['.  */
11417           cp_lexer_consume_token (parser->lexer);
11418           /* Peek at the next token.  */
11419           token = cp_lexer_peek_token (parser->lexer);
11420           /* If the next token is `]', then there is no
11421              constant-expression.  */
11422           if (token->type != CPP_CLOSE_SQUARE)
11423             {
11424               bool non_constant_p;
11425
11426               bounds
11427                 = cp_parser_constant_expression (parser,
11428                                                  /*allow_non_constant=*/true,
11429                                                  &non_constant_p);
11430               if (!non_constant_p)
11431                 bounds = fold_non_dependent_expr (bounds);
11432               /* Normally, the array bound must be an integral constant
11433                  expression.  However, as an extension, we allow VLAs
11434                  in function scopes.  */
11435               else if (!at_function_scope_p ())
11436                 {
11437                   error ("array bound is not an integer constant");
11438                   bounds = error_mark_node;
11439                 }
11440             }
11441           else
11442             bounds = NULL_TREE;
11443           /* Look for the closing `]'.  */
11444           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11445             {
11446               declarator = cp_error_declarator;
11447               break;
11448             }
11449
11450           declarator = make_array_declarator (declarator, bounds);
11451         }
11452       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11453         {
11454           tree qualifying_scope;
11455           tree unqualified_name;
11456           special_function_kind sfk;
11457
11458           /* Parse a declarator-id */
11459           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11460             cp_parser_parse_tentatively (parser);
11461           unqualified_name = cp_parser_declarator_id (parser);
11462           qualifying_scope = parser->scope;
11463           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11464             {
11465               if (!cp_parser_parse_definitely (parser))
11466                 unqualified_name = error_mark_node;
11467               else if (qualifying_scope
11468                        || (TREE_CODE (unqualified_name)
11469                            != IDENTIFIER_NODE))
11470                 {
11471                   cp_parser_error (parser, "expected unqualified-id");
11472                   unqualified_name = error_mark_node;
11473                 }
11474             }
11475
11476           if (unqualified_name == error_mark_node)
11477             {
11478               declarator = cp_error_declarator;
11479               break;
11480             }
11481
11482           if (qualifying_scope && at_namespace_scope_p ()
11483               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11484             {
11485               /* In the declaration of a member of a template class
11486                  outside of the class itself, the SCOPE will sometimes
11487                  be a TYPENAME_TYPE.  For example, given:
11488
11489                  template <typename T>
11490                  int S<T>::R::i = 3;
11491
11492                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11493                  this context, we must resolve S<T>::R to an ordinary
11494                  type, rather than a typename type.
11495
11496                  The reason we normally avoid resolving TYPENAME_TYPEs
11497                  is that a specialization of `S' might render
11498                  `S<T>::R' not a type.  However, if `S' is
11499                  specialized, then this `i' will not be used, so there
11500                  is no harm in resolving the types here.  */
11501               tree type;
11502
11503               /* Resolve the TYPENAME_TYPE.  */
11504               type = resolve_typename_type (qualifying_scope,
11505                                             /*only_current_p=*/false);
11506               /* If that failed, the declarator is invalid.  */
11507               if (type == error_mark_node)
11508                 error ("%<%T::%D%> is not a type",
11509                        TYPE_CONTEXT (qualifying_scope),
11510                        TYPE_IDENTIFIER (qualifying_scope));
11511               qualifying_scope = type;
11512             }
11513
11514           sfk = sfk_none;
11515           if (unqualified_name)
11516             {
11517               tree class_type;
11518
11519               if (qualifying_scope
11520                   && CLASS_TYPE_P (qualifying_scope))
11521                 class_type = qualifying_scope;
11522               else
11523                 class_type = current_class_type;
11524
11525               if (TREE_CODE (unqualified_name) == TYPE_DECL)
11526                 {
11527                   tree name_type = TREE_TYPE (unqualified_name);
11528                   if (class_type && same_type_p (name_type, class_type))
11529                     {
11530                       if (qualifying_scope
11531                           && CLASSTYPE_USE_TEMPLATE (name_type))
11532                         {
11533                           error ("invalid use of constructor as a template");
11534                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11535                                   "name the constructor in a qualified name",
11536                                   class_type,
11537                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11538                                   class_type, name_type);
11539                           declarator = cp_error_declarator;
11540                           break;
11541                         }
11542                       else
11543                         unqualified_name = constructor_name (class_type);
11544                     }
11545                   else
11546                     {
11547                       /* We do not attempt to print the declarator
11548                          here because we do not have enough
11549                          information about its original syntactic
11550                          form.  */
11551                       cp_parser_error (parser, "invalid declarator");
11552                       declarator = cp_error_declarator;
11553                       break;
11554                     }
11555                 }
11556
11557               if (class_type)
11558                 {
11559                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11560                     sfk = sfk_destructor;
11561                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11562                     sfk = sfk_conversion;
11563                   else if (/* There's no way to declare a constructor
11564                               for an anonymous type, even if the type
11565                               got a name for linkage purposes.  */
11566                            !TYPE_WAS_ANONYMOUS (class_type)
11567                            && constructor_name_p (unqualified_name,
11568                                                   class_type))
11569                     {
11570                       unqualified_name = constructor_name (class_type);
11571                       sfk = sfk_constructor;
11572                     }
11573
11574                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
11575                     *ctor_dtor_or_conv_p = -1;
11576                 }
11577             }
11578           declarator = make_id_declarator (qualifying_scope, 
11579                                            unqualified_name,
11580                                            sfk);
11581           declarator->id_loc = token->location;
11582
11583         handle_declarator:;
11584           scope = get_scope_of_declarator (declarator);
11585           if (scope)
11586             /* Any names that appear after the declarator-id for a
11587                member are looked up in the containing scope.  */
11588             pushed_scope = push_scope (scope);
11589           parser->in_declarator_p = true;
11590           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11591               || (declarator && declarator->kind == cdk_id))
11592             /* Default args are only allowed on function
11593                declarations.  */
11594             parser->default_arg_ok_p = saved_default_arg_ok_p;
11595           else
11596             parser->default_arg_ok_p = false;
11597
11598           first = false;
11599         }
11600       /* We're done.  */
11601       else
11602         break;
11603     }
11604
11605   /* For an abstract declarator, we might wind up with nothing at this
11606      point.  That's an error; the declarator is not optional.  */
11607   if (!declarator)
11608     cp_parser_error (parser, "expected declarator");
11609
11610   /* If we entered a scope, we must exit it now.  */
11611   if (pushed_scope)
11612     pop_scope (pushed_scope);
11613
11614   parser->default_arg_ok_p = saved_default_arg_ok_p;
11615   parser->in_declarator_p = saved_in_declarator_p;
11616
11617   return declarator;
11618 }
11619
11620 /* Parse a ptr-operator.
11621
11622    ptr-operator:
11623      * cv-qualifier-seq [opt]
11624      &
11625      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11626
11627    GNU Extension:
11628
11629    ptr-operator:
11630      & cv-qualifier-seq [opt]
11631
11632    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11633    Returns ADDR_EXPR if a reference was used.  In the case of a
11634    pointer-to-member, *TYPE is filled in with the TYPE containing the
11635    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11636    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11637    ERROR_MARK if an error occurred.  */
11638
11639 static enum tree_code
11640 cp_parser_ptr_operator (cp_parser* parser,
11641                         tree* type,
11642                         cp_cv_quals *cv_quals)
11643 {
11644   enum tree_code code = ERROR_MARK;
11645   cp_token *token;
11646
11647   /* Assume that it's not a pointer-to-member.  */
11648   *type = NULL_TREE;
11649   /* And that there are no cv-qualifiers.  */
11650   *cv_quals = TYPE_UNQUALIFIED;
11651
11652   /* Peek at the next token.  */
11653   token = cp_lexer_peek_token (parser->lexer);
11654   /* If it's a `*' or `&' we have a pointer or reference.  */
11655   if (token->type == CPP_MULT || token->type == CPP_AND)
11656     {
11657       /* Remember which ptr-operator we were processing.  */
11658       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11659
11660       /* Consume the `*' or `&'.  */
11661       cp_lexer_consume_token (parser->lexer);
11662
11663       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11664          `&', if we are allowing GNU extensions.  (The only qualifier
11665          that can legally appear after `&' is `restrict', but that is
11666          enforced during semantic analysis.  */
11667       if (code == INDIRECT_REF
11668           || cp_parser_allow_gnu_extensions_p (parser))
11669         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11670     }
11671   else
11672     {
11673       /* Try the pointer-to-member case.  */
11674       cp_parser_parse_tentatively (parser);
11675       /* Look for the optional `::' operator.  */
11676       cp_parser_global_scope_opt (parser,
11677                                   /*current_scope_valid_p=*/false);
11678       /* Look for the nested-name specifier.  */
11679       cp_parser_nested_name_specifier (parser,
11680                                        /*typename_keyword_p=*/false,
11681                                        /*check_dependency_p=*/true,
11682                                        /*type_p=*/false,
11683                                        /*is_declaration=*/false);
11684       /* If we found it, and the next token is a `*', then we are
11685          indeed looking at a pointer-to-member operator.  */
11686       if (!cp_parser_error_occurred (parser)
11687           && cp_parser_require (parser, CPP_MULT, "`*'"))
11688         {
11689           /* The type of which the member is a member is given by the
11690              current SCOPE.  */
11691           *type = parser->scope;
11692           /* The next name will not be qualified.  */
11693           parser->scope = NULL_TREE;
11694           parser->qualifying_scope = NULL_TREE;
11695           parser->object_scope = NULL_TREE;
11696           /* Indicate that the `*' operator was used.  */
11697           code = INDIRECT_REF;
11698           /* Look for the optional cv-qualifier-seq.  */
11699           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11700         }
11701       /* If that didn't work we don't have a ptr-operator.  */
11702       if (!cp_parser_parse_definitely (parser))
11703         cp_parser_error (parser, "expected ptr-operator");
11704     }
11705
11706   return code;
11707 }
11708
11709 /* Parse an (optional) cv-qualifier-seq.
11710
11711    cv-qualifier-seq:
11712      cv-qualifier cv-qualifier-seq [opt]
11713
11714    cv-qualifier:
11715      const
11716      volatile
11717
11718    GNU Extension:
11719
11720    cv-qualifier:
11721      __restrict__
11722
11723    Returns a bitmask representing the cv-qualifiers.  */
11724
11725 static cp_cv_quals
11726 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11727 {
11728   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11729
11730   while (true)
11731     {
11732       cp_token *token;
11733       cp_cv_quals cv_qualifier;
11734
11735       /* Peek at the next token.  */
11736       token = cp_lexer_peek_token (parser->lexer);
11737       /* See if it's a cv-qualifier.  */
11738       switch (token->keyword)
11739         {
11740         case RID_CONST:
11741           cv_qualifier = TYPE_QUAL_CONST;
11742           break;
11743
11744         case RID_VOLATILE:
11745           cv_qualifier = TYPE_QUAL_VOLATILE;
11746           break;
11747
11748         case RID_RESTRICT:
11749           cv_qualifier = TYPE_QUAL_RESTRICT;
11750           break;
11751
11752         default:
11753           cv_qualifier = TYPE_UNQUALIFIED;
11754           break;
11755         }
11756
11757       if (!cv_qualifier)
11758         break;
11759
11760       if (cv_quals & cv_qualifier)
11761         {
11762           error ("duplicate cv-qualifier");
11763           cp_lexer_purge_token (parser->lexer);
11764         }
11765       else
11766         {
11767           cp_lexer_consume_token (parser->lexer);
11768           cv_quals |= cv_qualifier;
11769         }
11770     }
11771
11772   return cv_quals;
11773 }
11774
11775 /* Parse a declarator-id.
11776
11777    declarator-id:
11778      id-expression
11779      :: [opt] nested-name-specifier [opt] type-name
11780
11781    In the `id-expression' case, the value returned is as for
11782    cp_parser_id_expression if the id-expression was an unqualified-id.
11783    If the id-expression was a qualified-id, then a SCOPE_REF is
11784    returned.  The first operand is the scope (either a NAMESPACE_DECL
11785    or TREE_TYPE), but the second is still just a representation of an
11786    unqualified-id.  */
11787
11788 static tree
11789 cp_parser_declarator_id (cp_parser* parser)
11790 {
11791   tree id;
11792   /* The expression must be an id-expression.  Assume that qualified
11793      names are the names of types so that:
11794
11795        template <class T>
11796        int S<T>::R::i = 3;
11797
11798      will work; we must treat `S<T>::R' as the name of a type.
11799      Similarly, assume that qualified names are templates, where
11800      required, so that:
11801
11802        template <class T>
11803        int S<T>::R<T>::i = 3;
11804
11805      will work, too.  */
11806   id = cp_parser_id_expression (parser,
11807                                 /*template_keyword_p=*/false,
11808                                 /*check_dependency_p=*/false,
11809                                 /*template_p=*/NULL,
11810                                 /*declarator_p=*/true);
11811   if (BASELINK_P (id))
11812     id = BASELINK_FUNCTIONS (id);
11813   return id;
11814 }
11815
11816 /* Parse a type-id.
11817
11818    type-id:
11819      type-specifier-seq abstract-declarator [opt]
11820
11821    Returns the TYPE specified.  */
11822
11823 static tree
11824 cp_parser_type_id (cp_parser* parser)
11825 {
11826   cp_decl_specifier_seq type_specifier_seq;
11827   cp_declarator *abstract_declarator;
11828
11829   /* Parse the type-specifier-seq.  */
11830   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11831                                 &type_specifier_seq);
11832   if (type_specifier_seq.type == error_mark_node)
11833     return error_mark_node;
11834
11835   /* There might or might not be an abstract declarator.  */
11836   cp_parser_parse_tentatively (parser);
11837   /* Look for the declarator.  */
11838   abstract_declarator
11839     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11840                             /*parenthesized_p=*/NULL,
11841                             /*member_p=*/false);
11842   /* Check to see if there really was a declarator.  */
11843   if (!cp_parser_parse_definitely (parser))
11844     abstract_declarator = NULL;
11845
11846   return groktypename (&type_specifier_seq, abstract_declarator);
11847 }
11848
11849 /* Parse a type-specifier-seq.
11850
11851    type-specifier-seq:
11852      type-specifier type-specifier-seq [opt]
11853
11854    GNU extension:
11855
11856    type-specifier-seq:
11857      attributes type-specifier-seq [opt]
11858
11859    If IS_CONDITION is true, we are at the start of a "condition",
11860    e.g., we've just seen "if (".
11861
11862    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11863
11864 static void
11865 cp_parser_type_specifier_seq (cp_parser* parser,
11866                               bool is_condition,
11867                               cp_decl_specifier_seq *type_specifier_seq)
11868 {
11869   bool seen_type_specifier = false;
11870   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11871
11872   /* Clear the TYPE_SPECIFIER_SEQ.  */
11873   clear_decl_specs (type_specifier_seq);
11874
11875   /* Parse the type-specifiers and attributes.  */
11876   while (true)
11877     {
11878       tree type_specifier;
11879       bool is_cv_qualifier;
11880
11881       /* Check for attributes first.  */
11882       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11883         {
11884           type_specifier_seq->attributes =
11885             chainon (type_specifier_seq->attributes,
11886                      cp_parser_attributes_opt (parser));
11887           continue;
11888         }
11889
11890       /* Look for the type-specifier.  */
11891       type_specifier = cp_parser_type_specifier (parser,
11892                                                  flags,
11893                                                  type_specifier_seq,
11894                                                  /*is_declaration=*/false,
11895                                                  NULL,
11896                                                  &is_cv_qualifier);
11897       if (!type_specifier)
11898         {
11899           /* If the first type-specifier could not be found, this is not a
11900              type-specifier-seq at all.  */
11901           if (!seen_type_specifier)
11902             {
11903               cp_parser_error (parser, "expected type-specifier");
11904               type_specifier_seq->type = error_mark_node;
11905               return;
11906             }
11907           /* If subsequent type-specifiers could not be found, the
11908              type-specifier-seq is complete.  */
11909           break;
11910         }
11911
11912       seen_type_specifier = true;
11913       /* The standard says that a condition can be:
11914
11915             type-specifier-seq declarator = assignment-expression
11916
11917          However, given:
11918
11919            struct S {};
11920            if (int S = ...)
11921
11922          we should treat the "S" as a declarator, not as a
11923          type-specifier.  The standard doesn't say that explicitly for
11924          type-specifier-seq, but it does say that for
11925          decl-specifier-seq in an ordinary declaration.  Perhaps it
11926          would be clearer just to allow a decl-specifier-seq here, and
11927          then add a semantic restriction that if any decl-specifiers
11928          that are not type-specifiers appear, the program is invalid.  */
11929       if (is_condition && !is_cv_qualifier)
11930         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11931     }
11932 }
11933
11934 /* Parse a parameter-declaration-clause.
11935
11936    parameter-declaration-clause:
11937      parameter-declaration-list [opt] ... [opt]
11938      parameter-declaration-list , ...
11939
11940    Returns a representation for the parameter declarations.  A return
11941    value of NULL indicates a parameter-declaration-clause consisting
11942    only of an ellipsis.  */
11943
11944 static cp_parameter_declarator *
11945 cp_parser_parameter_declaration_clause (cp_parser* parser)
11946 {
11947   cp_parameter_declarator *parameters;
11948   cp_token *token;
11949   bool ellipsis_p;
11950   bool is_error;
11951
11952   /* Peek at the next token.  */
11953   token = cp_lexer_peek_token (parser->lexer);
11954   /* Check for trivial parameter-declaration-clauses.  */
11955   if (token->type == CPP_ELLIPSIS)
11956     {
11957       /* Consume the `...' token.  */
11958       cp_lexer_consume_token (parser->lexer);
11959       return NULL;
11960     }
11961   else if (token->type == CPP_CLOSE_PAREN)
11962     /* There are no parameters.  */
11963     {
11964 #ifndef NO_IMPLICIT_EXTERN_C
11965       if (in_system_header && current_class_type == NULL
11966           && current_lang_name == lang_name_c)
11967         return NULL;
11968       else
11969 #endif
11970         return no_parameters;
11971     }
11972   /* Check for `(void)', too, which is a special case.  */
11973   else if (token->keyword == RID_VOID
11974            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11975                == CPP_CLOSE_PAREN))
11976     {
11977       /* Consume the `void' token.  */
11978       cp_lexer_consume_token (parser->lexer);
11979       /* There are no parameters.  */
11980       return no_parameters;
11981     }
11982
11983   /* Parse the parameter-declaration-list.  */
11984   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11985   /* If a parse error occurred while parsing the
11986      parameter-declaration-list, then the entire
11987      parameter-declaration-clause is erroneous.  */
11988   if (is_error)
11989     return NULL;
11990
11991   /* Peek at the next token.  */
11992   token = cp_lexer_peek_token (parser->lexer);
11993   /* If it's a `,', the clause should terminate with an ellipsis.  */
11994   if (token->type == CPP_COMMA)
11995     {
11996       /* Consume the `,'.  */
11997       cp_lexer_consume_token (parser->lexer);
11998       /* Expect an ellipsis.  */
11999       ellipsis_p
12000         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12001     }
12002   /* It might also be `...' if the optional trailing `,' was
12003      omitted.  */
12004   else if (token->type == CPP_ELLIPSIS)
12005     {
12006       /* Consume the `...' token.  */
12007       cp_lexer_consume_token (parser->lexer);
12008       /* And remember that we saw it.  */
12009       ellipsis_p = true;
12010     }
12011   else
12012     ellipsis_p = false;
12013
12014   /* Finish the parameter list.  */
12015   if (parameters && ellipsis_p)
12016     parameters->ellipsis_p = true;
12017
12018   return parameters;
12019 }
12020
12021 /* Parse a parameter-declaration-list.
12022
12023    parameter-declaration-list:
12024      parameter-declaration
12025      parameter-declaration-list , parameter-declaration
12026
12027    Returns a representation of the parameter-declaration-list, as for
12028    cp_parser_parameter_declaration_clause.  However, the
12029    `void_list_node' is never appended to the list.  Upon return,
12030    *IS_ERROR will be true iff an error occurred.  */
12031
12032 static cp_parameter_declarator *
12033 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12034 {
12035   cp_parameter_declarator *parameters = NULL;
12036   cp_parameter_declarator **tail = &parameters;
12037
12038   /* Assume all will go well.  */
12039   *is_error = false;
12040
12041   /* Look for more parameters.  */
12042   while (true)
12043     {
12044       cp_parameter_declarator *parameter;
12045       bool parenthesized_p;
12046       /* Parse the parameter.  */
12047       parameter
12048         = cp_parser_parameter_declaration (parser,
12049                                            /*template_parm_p=*/false,
12050                                            &parenthesized_p);
12051
12052       /* If a parse error occurred parsing the parameter declaration,
12053          then the entire parameter-declaration-list is erroneous.  */
12054       if (!parameter)
12055         {
12056           *is_error = true;
12057           parameters = NULL;
12058           break;
12059         }
12060       /* Add the new parameter to the list.  */
12061       *tail = parameter;
12062       tail = &parameter->next;
12063
12064       /* Peek at the next token.  */
12065       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12066           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12067           /* These are for Objective-C++ */
12068           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12069           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12070         /* The parameter-declaration-list is complete.  */
12071         break;
12072       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12073         {
12074           cp_token *token;
12075
12076           /* Peek at the next token.  */
12077           token = cp_lexer_peek_nth_token (parser->lexer, 2);
12078           /* If it's an ellipsis, then the list is complete.  */
12079           if (token->type == CPP_ELLIPSIS)
12080             break;
12081           /* Otherwise, there must be more parameters.  Consume the
12082              `,'.  */
12083           cp_lexer_consume_token (parser->lexer);
12084           /* When parsing something like:
12085
12086                 int i(float f, double d)
12087
12088              we can tell after seeing the declaration for "f" that we
12089              are not looking at an initialization of a variable "i",
12090              but rather at the declaration of a function "i".
12091
12092              Due to the fact that the parsing of template arguments
12093              (as specified to a template-id) requires backtracking we
12094              cannot use this technique when inside a template argument
12095              list.  */
12096           if (!parser->in_template_argument_list_p
12097               && !parser->in_type_id_in_expr_p
12098               && cp_parser_uncommitted_to_tentative_parse_p (parser)
12099               /* However, a parameter-declaration of the form
12100                  "foat(f)" (which is a valid declaration of a
12101                  parameter "f") can also be interpreted as an
12102                  expression (the conversion of "f" to "float").  */
12103               && !parenthesized_p)
12104             cp_parser_commit_to_tentative_parse (parser);
12105         }
12106       else
12107         {
12108           cp_parser_error (parser, "expected %<,%> or %<...%>");
12109           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12110             cp_parser_skip_to_closing_parenthesis (parser,
12111                                                    /*recovering=*/true,
12112                                                    /*or_comma=*/false,
12113                                                    /*consume_paren=*/false);
12114           break;
12115         }
12116     }
12117
12118   return parameters;
12119 }
12120
12121 /* Parse a parameter declaration.
12122
12123    parameter-declaration:
12124      decl-specifier-seq declarator
12125      decl-specifier-seq declarator = assignment-expression
12126      decl-specifier-seq abstract-declarator [opt]
12127      decl-specifier-seq abstract-declarator [opt] = assignment-expression
12128
12129    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12130    declares a template parameter.  (In that case, a non-nested `>'
12131    token encountered during the parsing of the assignment-expression
12132    is not interpreted as a greater-than operator.)
12133
12134    Returns a representation of the parameter, or NULL if an error
12135    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12136    true iff the declarator is of the form "(p)".  */
12137
12138 static cp_parameter_declarator *
12139 cp_parser_parameter_declaration (cp_parser *parser,
12140                                  bool template_parm_p,
12141                                  bool *parenthesized_p)
12142 {
12143   int declares_class_or_enum;
12144   bool greater_than_is_operator_p;
12145   cp_decl_specifier_seq decl_specifiers;
12146   cp_declarator *declarator;
12147   tree default_argument;
12148   cp_token *token;
12149   const char *saved_message;
12150
12151   /* In a template parameter, `>' is not an operator.
12152
12153      [temp.param]
12154
12155      When parsing a default template-argument for a non-type
12156      template-parameter, the first non-nested `>' is taken as the end
12157      of the template parameter-list rather than a greater-than
12158      operator.  */
12159   greater_than_is_operator_p = !template_parm_p;
12160
12161   /* Type definitions may not appear in parameter types.  */
12162   saved_message = parser->type_definition_forbidden_message;
12163   parser->type_definition_forbidden_message
12164     = "types may not be defined in parameter types";
12165
12166   /* Parse the declaration-specifiers.  */
12167   cp_parser_decl_specifier_seq (parser,
12168                                 CP_PARSER_FLAGS_NONE,
12169                                 &decl_specifiers,
12170                                 &declares_class_or_enum);
12171   /* If an error occurred, there's no reason to attempt to parse the
12172      rest of the declaration.  */
12173   if (cp_parser_error_occurred (parser))
12174     {
12175       parser->type_definition_forbidden_message = saved_message;
12176       return NULL;
12177     }
12178
12179   /* Peek at the next token.  */
12180   token = cp_lexer_peek_token (parser->lexer);
12181   /* If the next token is a `)', `,', `=', `>', or `...', then there
12182      is no declarator.  */
12183   if (token->type == CPP_CLOSE_PAREN
12184       || token->type == CPP_COMMA
12185       || token->type == CPP_EQ
12186       || token->type == CPP_ELLIPSIS
12187       || token->type == CPP_GREATER)
12188     {
12189       declarator = NULL;
12190       if (parenthesized_p)
12191         *parenthesized_p = false;
12192     }
12193   /* Otherwise, there should be a declarator.  */
12194   else
12195     {
12196       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12197       parser->default_arg_ok_p = false;
12198
12199       /* After seeing a decl-specifier-seq, if the next token is not a
12200          "(", there is no possibility that the code is a valid
12201          expression.  Therefore, if parsing tentatively, we commit at
12202          this point.  */
12203       if (!parser->in_template_argument_list_p
12204           /* In an expression context, having seen:
12205
12206                (int((char ...
12207
12208              we cannot be sure whether we are looking at a
12209              function-type (taking a "char" as a parameter) or a cast
12210              of some object of type "char" to "int".  */
12211           && !parser->in_type_id_in_expr_p
12212           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12213           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12214         cp_parser_commit_to_tentative_parse (parser);
12215       /* Parse the declarator.  */
12216       declarator = cp_parser_declarator (parser,
12217                                          CP_PARSER_DECLARATOR_EITHER,
12218                                          /*ctor_dtor_or_conv_p=*/NULL,
12219                                          parenthesized_p,
12220                                          /*member_p=*/false);
12221       parser->default_arg_ok_p = saved_default_arg_ok_p;
12222       /* After the declarator, allow more attributes.  */
12223       decl_specifiers.attributes
12224         = chainon (decl_specifiers.attributes,
12225                    cp_parser_attributes_opt (parser));
12226     }
12227
12228   /* The restriction on defining new types applies only to the type
12229      of the parameter, not to the default argument.  */
12230   parser->type_definition_forbidden_message = saved_message;
12231
12232   /* If the next token is `=', then process a default argument.  */
12233   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12234     {
12235       bool saved_greater_than_is_operator_p;
12236       /* Consume the `='.  */
12237       cp_lexer_consume_token (parser->lexer);
12238
12239       /* If we are defining a class, then the tokens that make up the
12240          default argument must be saved and processed later.  */
12241       if (!template_parm_p && at_class_scope_p ()
12242           && TYPE_BEING_DEFINED (current_class_type))
12243         {
12244           unsigned depth = 0;
12245           cp_token *first_token;
12246           cp_token *token;
12247
12248           /* Add tokens until we have processed the entire default
12249              argument.  We add the range [first_token, token).  */
12250           first_token = cp_lexer_peek_token (parser->lexer);
12251           while (true)
12252             {
12253               bool done = false;
12254
12255               /* Peek at the next token.  */
12256               token = cp_lexer_peek_token (parser->lexer);
12257               /* What we do depends on what token we have.  */
12258               switch (token->type)
12259                 {
12260                   /* In valid code, a default argument must be
12261                      immediately followed by a `,' `)', or `...'.  */
12262                 case CPP_COMMA:
12263                 case CPP_CLOSE_PAREN:
12264                 case CPP_ELLIPSIS:
12265                   /* If we run into a non-nested `;', `}', or `]',
12266                      then the code is invalid -- but the default
12267                      argument is certainly over.  */
12268                 case CPP_SEMICOLON:
12269                 case CPP_CLOSE_BRACE:
12270                 case CPP_CLOSE_SQUARE:
12271                   if (depth == 0)
12272                     done = true;
12273                   /* Update DEPTH, if necessary.  */
12274                   else if (token->type == CPP_CLOSE_PAREN
12275                            || token->type == CPP_CLOSE_BRACE
12276                            || token->type == CPP_CLOSE_SQUARE)
12277                     --depth;
12278                   break;
12279
12280                 case CPP_OPEN_PAREN:
12281                 case CPP_OPEN_SQUARE:
12282                 case CPP_OPEN_BRACE:
12283                   ++depth;
12284                   break;
12285
12286                 case CPP_GREATER:
12287                   /* If we see a non-nested `>', and `>' is not an
12288                      operator, then it marks the end of the default
12289                      argument.  */
12290                   if (!depth && !greater_than_is_operator_p)
12291                     done = true;
12292                   break;
12293
12294                   /* If we run out of tokens, issue an error message.  */
12295                 case CPP_EOF:
12296                 case CPP_PRAGMA_EOL:
12297                   error ("file ends in default argument");
12298                   done = true;
12299                   break;
12300
12301                 case CPP_NAME:
12302                 case CPP_SCOPE:
12303                   /* In these cases, we should look for template-ids.
12304                      For example, if the default argument is
12305                      `X<int, double>()', we need to do name lookup to
12306                      figure out whether or not `X' is a template; if
12307                      so, the `,' does not end the default argument.
12308
12309                      That is not yet done.  */
12310                   break;
12311
12312                 default:
12313                   break;
12314                 }
12315
12316               /* If we've reached the end, stop.  */
12317               if (done)
12318                 break;
12319
12320               /* Add the token to the token block.  */
12321               token = cp_lexer_consume_token (parser->lexer);
12322             }
12323
12324           /* Create a DEFAULT_ARG to represented the unparsed default
12325              argument.  */
12326           default_argument = make_node (DEFAULT_ARG);
12327           DEFARG_TOKENS (default_argument)
12328             = cp_token_cache_new (first_token, token);
12329           DEFARG_INSTANTIATIONS (default_argument) = NULL;
12330         }
12331       /* Outside of a class definition, we can just parse the
12332          assignment-expression.  */
12333       else
12334         {
12335           bool saved_local_variables_forbidden_p;
12336
12337           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12338              set correctly.  */
12339           saved_greater_than_is_operator_p
12340             = parser->greater_than_is_operator_p;
12341           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12342           /* Local variable names (and the `this' keyword) may not
12343              appear in a default argument.  */
12344           saved_local_variables_forbidden_p
12345             = parser->local_variables_forbidden_p;
12346           parser->local_variables_forbidden_p = true;
12347           /* Parse the assignment-expression.  */
12348           default_argument
12349             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12350           /* Restore saved state.  */
12351           parser->greater_than_is_operator_p
12352             = saved_greater_than_is_operator_p;
12353           parser->local_variables_forbidden_p
12354             = saved_local_variables_forbidden_p;
12355         }
12356       if (!parser->default_arg_ok_p)
12357         {
12358           if (!flag_pedantic_errors)
12359             warning (0, "deprecated use of default argument for parameter of non-function");
12360           else
12361             {
12362               error ("default arguments are only permitted for function parameters");
12363               default_argument = NULL_TREE;
12364             }
12365         }
12366     }
12367   else
12368     default_argument = NULL_TREE;
12369
12370   return make_parameter_declarator (&decl_specifiers,
12371                                     declarator,
12372                                     default_argument);
12373 }
12374
12375 /* Parse a function-body.
12376
12377    function-body:
12378      compound_statement  */
12379
12380 static void
12381 cp_parser_function_body (cp_parser *parser)
12382 {
12383   cp_parser_compound_statement (parser, NULL, false);
12384 }
12385
12386 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12387    true if a ctor-initializer was present.  */
12388
12389 static bool
12390 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12391 {
12392   tree body;
12393   bool ctor_initializer_p;
12394
12395   /* Begin the function body.  */
12396   body = begin_function_body ();
12397   /* Parse the optional ctor-initializer.  */
12398   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12399   /* Parse the function-body.  */
12400   cp_parser_function_body (parser);
12401   /* Finish the function body.  */
12402   finish_function_body (body);
12403
12404   return ctor_initializer_p;
12405 }
12406
12407 /* Parse an initializer.
12408
12409    initializer:
12410      = initializer-clause
12411      ( expression-list )
12412
12413    Returns an expression representing the initializer.  If no
12414    initializer is present, NULL_TREE is returned.
12415
12416    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12417    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12418    set to FALSE if there is no initializer present.  If there is an
12419    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12420    is set to true; otherwise it is set to false.  */
12421
12422 static tree
12423 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12424                        bool* non_constant_p)
12425 {
12426   cp_token *token;
12427   tree init;
12428
12429   /* Peek at the next token.  */
12430   token = cp_lexer_peek_token (parser->lexer);
12431
12432   /* Let our caller know whether or not this initializer was
12433      parenthesized.  */
12434   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12435   /* Assume that the initializer is constant.  */
12436   *non_constant_p = false;
12437
12438   if (token->type == CPP_EQ)
12439     {
12440       /* Consume the `='.  */
12441       cp_lexer_consume_token (parser->lexer);
12442       /* Parse the initializer-clause.  */
12443       init = cp_parser_initializer_clause (parser, non_constant_p);
12444     }
12445   else if (token->type == CPP_OPEN_PAREN)
12446     init = cp_parser_parenthesized_expression_list (parser, false,
12447                                                     /*cast_p=*/false,
12448                                                     non_constant_p);
12449   else
12450     {
12451       /* Anything else is an error.  */
12452       cp_parser_error (parser, "expected initializer");
12453       init = error_mark_node;
12454     }
12455
12456   return init;
12457 }
12458
12459 /* Parse an initializer-clause.
12460
12461    initializer-clause:
12462      assignment-expression
12463      { initializer-list , [opt] }
12464      { }
12465
12466    Returns an expression representing the initializer.
12467
12468    If the `assignment-expression' production is used the value
12469    returned is simply a representation for the expression.
12470
12471    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12472    the elements of the initializer-list (or NULL, if the last
12473    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12474    NULL_TREE.  There is no way to detect whether or not the optional
12475    trailing `,' was provided.  NON_CONSTANT_P is as for
12476    cp_parser_initializer.  */
12477
12478 static tree
12479 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12480 {
12481   tree initializer;
12482
12483   /* Assume the expression is constant.  */
12484   *non_constant_p = false;
12485
12486   /* If it is not a `{', then we are looking at an
12487      assignment-expression.  */
12488   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12489     {
12490       initializer
12491         = cp_parser_constant_expression (parser,
12492                                         /*allow_non_constant_p=*/true,
12493                                         non_constant_p);
12494       if (!*non_constant_p)
12495         initializer = fold_non_dependent_expr (initializer);
12496     }
12497   else
12498     {
12499       /* Consume the `{' token.  */
12500       cp_lexer_consume_token (parser->lexer);
12501       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12502       initializer = make_node (CONSTRUCTOR);
12503       /* If it's not a `}', then there is a non-trivial initializer.  */
12504       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12505         {
12506           /* Parse the initializer list.  */
12507           CONSTRUCTOR_ELTS (initializer)
12508             = cp_parser_initializer_list (parser, non_constant_p);
12509           /* A trailing `,' token is allowed.  */
12510           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12511             cp_lexer_consume_token (parser->lexer);
12512         }
12513       /* Now, there should be a trailing `}'.  */
12514       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12515     }
12516
12517   return initializer;
12518 }
12519
12520 /* Parse an initializer-list.
12521
12522    initializer-list:
12523      initializer-clause
12524      initializer-list , initializer-clause
12525
12526    GNU Extension:
12527
12528    initializer-list:
12529      identifier : initializer-clause
12530      initializer-list, identifier : initializer-clause
12531
12532    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12533    for the initializer.  If the INDEX of the elt is non-NULL, it is the
12534    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12535    as for cp_parser_initializer.  */
12536
12537 static VEC(constructor_elt,gc) *
12538 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12539 {
12540   VEC(constructor_elt,gc) *v = NULL;
12541
12542   /* Assume all of the expressions are constant.  */
12543   *non_constant_p = false;
12544
12545   /* Parse the rest of the list.  */
12546   while (true)
12547     {
12548       cp_token *token;
12549       tree identifier;
12550       tree initializer;
12551       bool clause_non_constant_p;
12552
12553       /* If the next token is an identifier and the following one is a
12554          colon, we are looking at the GNU designated-initializer
12555          syntax.  */
12556       if (cp_parser_allow_gnu_extensions_p (parser)
12557           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12558           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12559         {
12560           /* Consume the identifier.  */
12561           identifier = cp_lexer_consume_token (parser->lexer)->value;
12562           /* Consume the `:'.  */
12563           cp_lexer_consume_token (parser->lexer);
12564         }
12565       else
12566         identifier = NULL_TREE;
12567
12568       /* Parse the initializer.  */
12569       initializer = cp_parser_initializer_clause (parser,
12570                                                   &clause_non_constant_p);
12571       /* If any clause is non-constant, so is the entire initializer.  */
12572       if (clause_non_constant_p)
12573         *non_constant_p = true;
12574
12575       /* Add it to the vector.  */
12576       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12577
12578       /* If the next token is not a comma, we have reached the end of
12579          the list.  */
12580       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12581         break;
12582
12583       /* Peek at the next token.  */
12584       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12585       /* If the next token is a `}', then we're still done.  An
12586          initializer-clause can have a trailing `,' after the
12587          initializer-list and before the closing `}'.  */
12588       if (token->type == CPP_CLOSE_BRACE)
12589         break;
12590
12591       /* Consume the `,' token.  */
12592       cp_lexer_consume_token (parser->lexer);
12593     }
12594
12595   return v;
12596 }
12597
12598 /* Classes [gram.class] */
12599
12600 /* Parse a class-name.
12601
12602    class-name:
12603      identifier
12604      template-id
12605
12606    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12607    to indicate that names looked up in dependent types should be
12608    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12609    keyword has been used to indicate that the name that appears next
12610    is a template.  TAG_TYPE indicates the explicit tag given before
12611    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12612    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12613    is the class being defined in a class-head.
12614
12615    Returns the TYPE_DECL representing the class.  */
12616
12617 static tree
12618 cp_parser_class_name (cp_parser *parser,
12619                       bool typename_keyword_p,
12620                       bool template_keyword_p,
12621                       enum tag_types tag_type,
12622                       bool check_dependency_p,
12623                       bool class_head_p,
12624                       bool is_declaration)
12625 {
12626   tree decl;
12627   tree scope;
12628   bool typename_p;
12629   cp_token *token;
12630
12631   /* All class-names start with an identifier.  */
12632   token = cp_lexer_peek_token (parser->lexer);
12633   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12634     {
12635       cp_parser_error (parser, "expected class-name");
12636       return error_mark_node;
12637     }
12638
12639   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12640      to a template-id, so we save it here.  */
12641   scope = parser->scope;
12642   if (scope == error_mark_node)
12643     return error_mark_node;
12644
12645   /* Any name names a type if we're following the `typename' keyword
12646      in a qualified name where the enclosing scope is type-dependent.  */
12647   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12648                 && dependent_type_p (scope));
12649   /* Handle the common case (an identifier, but not a template-id)
12650      efficiently.  */
12651   if (token->type == CPP_NAME
12652       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12653     {
12654       cp_token *identifier_token;
12655       tree identifier;
12656       bool ambiguous_p;
12657
12658       /* Look for the identifier.  */
12659       identifier_token = cp_lexer_peek_token (parser->lexer);
12660       ambiguous_p = identifier_token->ambiguous_p;
12661       identifier = cp_parser_identifier (parser);
12662       /* If the next token isn't an identifier, we are certainly not
12663          looking at a class-name.  */
12664       if (identifier == error_mark_node)
12665         decl = error_mark_node;
12666       /* If we know this is a type-name, there's no need to look it
12667          up.  */
12668       else if (typename_p)
12669         decl = identifier;
12670       else
12671         {
12672           tree ambiguous_decls;
12673           /* If we already know that this lookup is ambiguous, then
12674              we've already issued an error message; there's no reason
12675              to check again.  */
12676           if (ambiguous_p)
12677             {
12678               cp_parser_simulate_error (parser);
12679               return error_mark_node;
12680             }
12681           /* If the next token is a `::', then the name must be a type
12682              name.
12683
12684              [basic.lookup.qual]
12685
12686              During the lookup for a name preceding the :: scope
12687              resolution operator, object, function, and enumerator
12688              names are ignored.  */
12689           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12690             tag_type = typename_type;
12691           /* Look up the name.  */
12692           decl = cp_parser_lookup_name (parser, identifier,
12693                                         tag_type,
12694                                         /*is_template=*/false,
12695                                         /*is_namespace=*/false,
12696                                         check_dependency_p,
12697                                         &ambiguous_decls);
12698           if (ambiguous_decls)
12699             {
12700               error ("reference to %qD is ambiguous", identifier);
12701               print_candidates (ambiguous_decls);
12702               if (cp_parser_parsing_tentatively (parser))
12703                 {
12704                   identifier_token->ambiguous_p = true;
12705                   cp_parser_simulate_error (parser);
12706                 }
12707               return error_mark_node;
12708             }
12709         }
12710     }
12711   else
12712     {
12713       /* Try a template-id.  */
12714       decl = cp_parser_template_id (parser, template_keyword_p,
12715                                     check_dependency_p,
12716                                     is_declaration);
12717       if (decl == error_mark_node)
12718         return error_mark_node;
12719     }
12720
12721   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12722
12723   /* If this is a typename, create a TYPENAME_TYPE.  */
12724   if (typename_p && decl != error_mark_node)
12725     {
12726       decl = make_typename_type (scope, decl, typename_type,
12727                                  /*complain=*/tf_error);
12728       if (decl != error_mark_node)
12729         decl = TYPE_NAME (decl);
12730     }
12731
12732   /* Check to see that it is really the name of a class.  */
12733   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12734       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12735       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12736     /* Situations like this:
12737
12738          template <typename T> struct A {
12739            typename T::template X<int>::I i;
12740          };
12741
12742        are problematic.  Is `T::template X<int>' a class-name?  The
12743        standard does not seem to be definitive, but there is no other
12744        valid interpretation of the following `::'.  Therefore, those
12745        names are considered class-names.  */
12746     decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12747   else if (decl == error_mark_node
12748            || TREE_CODE (decl) != TYPE_DECL
12749            || TREE_TYPE (decl) == error_mark_node
12750            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12751     {
12752       cp_parser_error (parser, "expected class-name");
12753       return error_mark_node;
12754     }
12755
12756   return decl;
12757 }
12758
12759 /* Parse a class-specifier.
12760
12761    class-specifier:
12762      class-head { member-specification [opt] }
12763
12764    Returns the TREE_TYPE representing the class.  */
12765
12766 static tree
12767 cp_parser_class_specifier (cp_parser* parser)
12768 {
12769   cp_token *token;
12770   tree type;
12771   tree attributes = NULL_TREE;
12772   int has_trailing_semicolon;
12773   bool nested_name_specifier_p;
12774   unsigned saved_num_template_parameter_lists;
12775   tree old_scope = NULL_TREE;
12776   tree scope = NULL_TREE;
12777
12778   push_deferring_access_checks (dk_no_deferred);
12779
12780   /* Parse the class-head.  */
12781   type = cp_parser_class_head (parser,
12782                                &nested_name_specifier_p,
12783                                &attributes);
12784   /* If the class-head was a semantic disaster, skip the entire body
12785      of the class.  */
12786   if (!type)
12787     {
12788       cp_parser_skip_to_end_of_block_or_statement (parser);
12789       pop_deferring_access_checks ();
12790       return error_mark_node;
12791     }
12792
12793   /* Look for the `{'.  */
12794   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12795     {
12796       pop_deferring_access_checks ();
12797       return error_mark_node;
12798     }
12799
12800   /* Issue an error message if type-definitions are forbidden here.  */
12801   cp_parser_check_type_definition (parser);
12802   /* Remember that we are defining one more class.  */
12803   ++parser->num_classes_being_defined;
12804   /* Inside the class, surrounding template-parameter-lists do not
12805      apply.  */
12806   saved_num_template_parameter_lists
12807     = parser->num_template_parameter_lists;
12808   parser->num_template_parameter_lists = 0;
12809
12810   /* Start the class.  */
12811   if (nested_name_specifier_p)
12812     {
12813       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12814       old_scope = push_inner_scope (scope);
12815     }
12816   type = begin_class_definition (type);
12817
12818   if (type == error_mark_node)
12819     /* If the type is erroneous, skip the entire body of the class.  */
12820     cp_parser_skip_to_closing_brace (parser);
12821   else
12822     /* Parse the member-specification.  */
12823     cp_parser_member_specification_opt (parser);
12824
12825   /* Look for the trailing `}'.  */
12826   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12827   /* We get better error messages by noticing a common problem: a
12828      missing trailing `;'.  */
12829   token = cp_lexer_peek_token (parser->lexer);
12830   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12831   /* Look for trailing attributes to apply to this class.  */
12832   if (cp_parser_allow_gnu_extensions_p (parser))
12833     {
12834       tree sub_attr = cp_parser_attributes_opt (parser);
12835       attributes = chainon (attributes, sub_attr);
12836     }
12837   if (type != error_mark_node)
12838     type = finish_struct (type, attributes);
12839   if (nested_name_specifier_p)
12840     pop_inner_scope (old_scope, scope);
12841   /* If this class is not itself within the scope of another class,
12842      then we need to parse the bodies of all of the queued function
12843      definitions.  Note that the queued functions defined in a class
12844      are not always processed immediately following the
12845      class-specifier for that class.  Consider:
12846
12847        struct A {
12848          struct B { void f() { sizeof (A); } };
12849        };
12850
12851      If `f' were processed before the processing of `A' were
12852      completed, there would be no way to compute the size of `A'.
12853      Note that the nesting we are interested in here is lexical --
12854      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12855      for:
12856
12857        struct A { struct B; };
12858        struct A::B { void f() { } };
12859
12860      there is no need to delay the parsing of `A::B::f'.  */
12861   if (--parser->num_classes_being_defined == 0)
12862     {
12863       tree queue_entry;
12864       tree fn;
12865       tree class_type = NULL_TREE;
12866       tree pushed_scope = NULL_TREE;
12867  
12868       /* In a first pass, parse default arguments to the functions.
12869          Then, in a second pass, parse the bodies of the functions.
12870          This two-phased approach handles cases like:
12871
12872             struct S {
12873               void f() { g(); }
12874               void g(int i = 3);
12875             };
12876
12877          */
12878       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12879              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12880            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12881            TREE_PURPOSE (parser->unparsed_functions_queues)
12882              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12883         {
12884           fn = TREE_VALUE (queue_entry);
12885           /* If there are default arguments that have not yet been processed,
12886              take care of them now.  */
12887           if (class_type != TREE_PURPOSE (queue_entry))
12888             {
12889               if (pushed_scope)
12890                 pop_scope (pushed_scope);
12891               class_type = TREE_PURPOSE (queue_entry);
12892               pushed_scope = push_scope (class_type);
12893             }
12894           /* Make sure that any template parameters are in scope.  */
12895           maybe_begin_member_template_processing (fn);
12896           /* Parse the default argument expressions.  */
12897           cp_parser_late_parsing_default_args (parser, fn);
12898           /* Remove any template parameters from the symbol table.  */
12899           maybe_end_member_template_processing ();
12900         }
12901       if (pushed_scope)
12902         pop_scope (pushed_scope);
12903       /* Now parse the body of the functions.  */
12904       for (TREE_VALUE (parser->unparsed_functions_queues)
12905              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12906            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12907            TREE_VALUE (parser->unparsed_functions_queues)
12908              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12909         {
12910           /* Figure out which function we need to process.  */
12911           fn = TREE_VALUE (queue_entry);
12912           /* Parse the function.  */
12913           cp_parser_late_parsing_for_member (parser, fn);
12914         }
12915     }
12916
12917   /* Put back any saved access checks.  */
12918   pop_deferring_access_checks ();
12919
12920   /* Restore the count of active template-parameter-lists.  */
12921   parser->num_template_parameter_lists
12922     = saved_num_template_parameter_lists;
12923
12924   return type;
12925 }
12926
12927 /* Parse a class-head.
12928
12929    class-head:
12930      class-key identifier [opt] base-clause [opt]
12931      class-key nested-name-specifier identifier base-clause [opt]
12932      class-key nested-name-specifier [opt] template-id
12933        base-clause [opt]
12934
12935    GNU Extensions:
12936      class-key attributes identifier [opt] base-clause [opt]
12937      class-key attributes nested-name-specifier identifier base-clause [opt]
12938      class-key attributes nested-name-specifier [opt] template-id
12939        base-clause [opt]
12940
12941    Returns the TYPE of the indicated class.  Sets
12942    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12943    involving a nested-name-specifier was used, and FALSE otherwise.
12944
12945    Returns error_mark_node if this is not a class-head.
12946
12947    Returns NULL_TREE if the class-head is syntactically valid, but
12948    semantically invalid in a way that means we should skip the entire
12949    body of the class.  */
12950
12951 static tree
12952 cp_parser_class_head (cp_parser* parser,
12953                       bool* nested_name_specifier_p,
12954                       tree *attributes_p)
12955 {
12956   tree nested_name_specifier;
12957   enum tag_types class_key;
12958   tree id = NULL_TREE;
12959   tree type = NULL_TREE;
12960   tree attributes;
12961   bool template_id_p = false;
12962   bool qualified_p = false;
12963   bool invalid_nested_name_p = false;
12964   bool invalid_explicit_specialization_p = false;
12965   tree pushed_scope = NULL_TREE;
12966   unsigned num_templates;
12967   tree bases;
12968
12969   /* Assume no nested-name-specifier will be present.  */
12970   *nested_name_specifier_p = false;
12971   /* Assume no template parameter lists will be used in defining the
12972      type.  */
12973   num_templates = 0;
12974
12975   /* Look for the class-key.  */
12976   class_key = cp_parser_class_key (parser);
12977   if (class_key == none_type)
12978     return error_mark_node;
12979
12980   /* Parse the attributes.  */
12981   attributes = cp_parser_attributes_opt (parser);
12982
12983   /* If the next token is `::', that is invalid -- but sometimes
12984      people do try to write:
12985
12986        struct ::S {};
12987
12988      Handle this gracefully by accepting the extra qualifier, and then
12989      issuing an error about it later if this really is a
12990      class-head.  If it turns out just to be an elaborated type
12991      specifier, remain silent.  */
12992   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12993     qualified_p = true;
12994
12995   push_deferring_access_checks (dk_no_check);
12996
12997   /* Determine the name of the class.  Begin by looking for an
12998      optional nested-name-specifier.  */
12999   nested_name_specifier
13000     = cp_parser_nested_name_specifier_opt (parser,
13001                                            /*typename_keyword_p=*/false,
13002                                            /*check_dependency_p=*/false,
13003                                            /*type_p=*/false,
13004                                            /*is_declaration=*/false);
13005   /* If there was a nested-name-specifier, then there *must* be an
13006      identifier.  */
13007   if (nested_name_specifier)
13008     {
13009       /* Although the grammar says `identifier', it really means
13010          `class-name' or `template-name'.  You are only allowed to
13011          define a class that has already been declared with this
13012          syntax.
13013
13014          The proposed resolution for Core Issue 180 says that whever
13015          you see `class T::X' you should treat `X' as a type-name.
13016
13017          It is OK to define an inaccessible class; for example:
13018
13019            class A { class B; };
13020            class A::B {};
13021
13022          We do not know if we will see a class-name, or a
13023          template-name.  We look for a class-name first, in case the
13024          class-name is a template-id; if we looked for the
13025          template-name first we would stop after the template-name.  */
13026       cp_parser_parse_tentatively (parser);
13027       type = cp_parser_class_name (parser,
13028                                    /*typename_keyword_p=*/false,
13029                                    /*template_keyword_p=*/false,
13030                                    class_type,
13031                                    /*check_dependency_p=*/false,
13032                                    /*class_head_p=*/true,
13033                                    /*is_declaration=*/false);
13034       /* If that didn't work, ignore the nested-name-specifier.  */
13035       if (!cp_parser_parse_definitely (parser))
13036         {
13037           invalid_nested_name_p = true;
13038           id = cp_parser_identifier (parser);
13039           if (id == error_mark_node)
13040             id = NULL_TREE;
13041         }
13042       /* If we could not find a corresponding TYPE, treat this
13043          declaration like an unqualified declaration.  */
13044       if (type == error_mark_node)
13045         nested_name_specifier = NULL_TREE;
13046       /* Otherwise, count the number of templates used in TYPE and its
13047          containing scopes.  */
13048       else
13049         {
13050           tree scope;
13051
13052           for (scope = TREE_TYPE (type);
13053                scope && TREE_CODE (scope) != NAMESPACE_DECL;
13054                scope = (TYPE_P (scope)
13055                         ? TYPE_CONTEXT (scope)
13056                         : DECL_CONTEXT (scope)))
13057             if (TYPE_P (scope)
13058                 && CLASS_TYPE_P (scope)
13059                 && CLASSTYPE_TEMPLATE_INFO (scope)
13060                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13061                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13062               ++num_templates;
13063         }
13064     }
13065   /* Otherwise, the identifier is optional.  */
13066   else
13067     {
13068       /* We don't know whether what comes next is a template-id,
13069          an identifier, or nothing at all.  */
13070       cp_parser_parse_tentatively (parser);
13071       /* Check for a template-id.  */
13072       id = cp_parser_template_id (parser,
13073                                   /*template_keyword_p=*/false,
13074                                   /*check_dependency_p=*/true,
13075                                   /*is_declaration=*/true);
13076       /* If that didn't work, it could still be an identifier.  */
13077       if (!cp_parser_parse_definitely (parser))
13078         {
13079           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13080             id = cp_parser_identifier (parser);
13081           else
13082             id = NULL_TREE;
13083         }
13084       else
13085         {
13086           template_id_p = true;
13087           ++num_templates;
13088         }
13089     }
13090
13091   pop_deferring_access_checks ();
13092
13093   if (id)
13094     cp_parser_check_for_invalid_template_id (parser, id);
13095
13096   /* If it's not a `:' or a `{' then we can't really be looking at a
13097      class-head, since a class-head only appears as part of a
13098      class-specifier.  We have to detect this situation before calling
13099      xref_tag, since that has irreversible side-effects.  */
13100   if (!cp_parser_next_token_starts_class_definition_p (parser))
13101     {
13102       cp_parser_error (parser, "expected %<{%> or %<:%>");
13103       return error_mark_node;
13104     }
13105
13106   /* At this point, we're going ahead with the class-specifier, even
13107      if some other problem occurs.  */
13108   cp_parser_commit_to_tentative_parse (parser);
13109   /* Issue the error about the overly-qualified name now.  */
13110   if (qualified_p)
13111     cp_parser_error (parser,
13112                      "global qualification of class name is invalid");
13113   else if (invalid_nested_name_p)
13114     cp_parser_error (parser,
13115                      "qualified name does not name a class");
13116   else if (nested_name_specifier)
13117     {
13118       tree scope;
13119
13120       /* Reject typedef-names in class heads.  */
13121       if (!DECL_IMPLICIT_TYPEDEF_P (type))
13122         {
13123           error ("invalid class name in declaration of %qD", type);
13124           type = NULL_TREE;
13125           goto done;
13126         }
13127
13128       /* Figure out in what scope the declaration is being placed.  */
13129       scope = current_scope ();
13130       /* If that scope does not contain the scope in which the
13131          class was originally declared, the program is invalid.  */
13132       if (scope && !is_ancestor (scope, nested_name_specifier))
13133         {
13134           error ("declaration of %qD in %qD which does not enclose %qD",
13135                  type, scope, nested_name_specifier);
13136           type = NULL_TREE;
13137           goto done;
13138         }
13139       /* [dcl.meaning]
13140
13141          A declarator-id shall not be qualified exception of the
13142          definition of a ... nested class outside of its class
13143          ... [or] a the definition or explicit instantiation of a
13144          class member of a namespace outside of its namespace.  */
13145       if (scope == nested_name_specifier)
13146         {
13147           pedwarn ("extra qualification ignored");
13148           nested_name_specifier = NULL_TREE;
13149           num_templates = 0;
13150         }
13151     }
13152   /* An explicit-specialization must be preceded by "template <>".  If
13153      it is not, try to recover gracefully.  */
13154   if (at_namespace_scope_p ()
13155       && parser->num_template_parameter_lists == 0
13156       && template_id_p)
13157     {
13158       error ("an explicit specialization must be preceded by %<template <>%>");
13159       invalid_explicit_specialization_p = true;
13160       /* Take the same action that would have been taken by
13161          cp_parser_explicit_specialization.  */
13162       ++parser->num_template_parameter_lists;
13163       begin_specialization ();
13164     }
13165   /* There must be no "return" statements between this point and the
13166      end of this function; set "type "to the correct return value and
13167      use "goto done;" to return.  */
13168   /* Make sure that the right number of template parameters were
13169      present.  */
13170   if (!cp_parser_check_template_parameters (parser, num_templates))
13171     {
13172       /* If something went wrong, there is no point in even trying to
13173          process the class-definition.  */
13174       type = NULL_TREE;
13175       goto done;
13176     }
13177
13178   /* Look up the type.  */
13179   if (template_id_p)
13180     {
13181       type = TREE_TYPE (id);
13182       maybe_process_partial_specialization (type);
13183       if (nested_name_specifier)
13184         pushed_scope = push_scope (nested_name_specifier);
13185     }
13186   else if (nested_name_specifier)
13187     {
13188       tree class_type;
13189
13190       /* Given:
13191
13192             template <typename T> struct S { struct T };
13193             template <typename T> struct S<T>::T { };
13194
13195          we will get a TYPENAME_TYPE when processing the definition of
13196          `S::T'.  We need to resolve it to the actual type before we
13197          try to define it.  */
13198       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13199         {
13200           class_type = resolve_typename_type (TREE_TYPE (type),
13201                                               /*only_current_p=*/false);
13202           if (class_type != error_mark_node)
13203             type = TYPE_NAME (class_type);
13204           else
13205             {
13206               cp_parser_error (parser, "could not resolve typename type");
13207               type = error_mark_node;
13208             }
13209         }
13210
13211       maybe_process_partial_specialization (TREE_TYPE (type));
13212       class_type = current_class_type;
13213       /* Enter the scope indicated by the nested-name-specifier.  */
13214       pushed_scope = push_scope (nested_name_specifier);
13215       /* Get the canonical version of this type.  */
13216       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13217       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13218           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13219         {
13220           type = push_template_decl (type);
13221           if (type == error_mark_node)
13222             {
13223               type = NULL_TREE;
13224               goto done;
13225             }
13226         }
13227
13228       type = TREE_TYPE (type);
13229       *nested_name_specifier_p = true;
13230     }
13231   else      /* The name is not a nested name.  */
13232     {
13233       /* If the class was unnamed, create a dummy name.  */
13234       if (!id)
13235         id = make_anon_name ();
13236       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13237                        parser->num_template_parameter_lists);
13238     }
13239
13240   /* Indicate whether this class was declared as a `class' or as a
13241      `struct'.  */
13242   if (TREE_CODE (type) == RECORD_TYPE)
13243     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13244   cp_parser_check_class_key (class_key, type);
13245
13246   /* If this type was already complete, and we see another definition,
13247      that's an error.  */
13248   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13249     {
13250       error ("redefinition of %q#T", type);
13251       error ("previous definition of %q+#T", type);
13252       type = NULL_TREE;
13253       goto done;
13254     }
13255
13256   /* We will have entered the scope containing the class; the names of
13257      base classes should be looked up in that context.  For example:
13258
13259        struct A { struct B {}; struct C; };
13260        struct A::C : B {};
13261
13262      is valid.  */
13263   bases = NULL_TREE;
13264
13265   /* Get the list of base-classes, if there is one.  */
13266   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13267     bases = cp_parser_base_clause (parser);
13268
13269   /* Process the base classes.  */
13270   xref_basetypes (type, bases);
13271
13272  done:
13273   /* Leave the scope given by the nested-name-specifier.  We will
13274      enter the class scope itself while processing the members.  */
13275   if (pushed_scope)
13276     pop_scope (pushed_scope);
13277
13278   if (invalid_explicit_specialization_p)
13279     {
13280       end_specialization ();
13281       --parser->num_template_parameter_lists;
13282     }
13283   *attributes_p = attributes;
13284   return type;
13285 }
13286
13287 /* Parse a class-key.
13288
13289    class-key:
13290      class
13291      struct
13292      union
13293
13294    Returns the kind of class-key specified, or none_type to indicate
13295    error.  */
13296
13297 static enum tag_types
13298 cp_parser_class_key (cp_parser* parser)
13299 {
13300   cp_token *token;
13301   enum tag_types tag_type;
13302
13303   /* Look for the class-key.  */
13304   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13305   if (!token)
13306     return none_type;
13307
13308   /* Check to see if the TOKEN is a class-key.  */
13309   tag_type = cp_parser_token_is_class_key (token);
13310   if (!tag_type)
13311     cp_parser_error (parser, "expected class-key");
13312   return tag_type;
13313 }
13314
13315 /* Parse an (optional) member-specification.
13316
13317    member-specification:
13318      member-declaration member-specification [opt]
13319      access-specifier : member-specification [opt]  */
13320
13321 static void
13322 cp_parser_member_specification_opt (cp_parser* parser)
13323 {
13324   while (true)
13325     {
13326       cp_token *token;
13327       enum rid keyword;
13328
13329       /* Peek at the next token.  */
13330       token = cp_lexer_peek_token (parser->lexer);
13331       /* If it's a `}', or EOF then we've seen all the members.  */
13332       if (token->type == CPP_CLOSE_BRACE
13333           || token->type == CPP_EOF
13334           || token->type == CPP_PRAGMA_EOL)
13335         break;
13336
13337       /* See if this token is a keyword.  */
13338       keyword = token->keyword;
13339       switch (keyword)
13340         {
13341         case RID_PUBLIC:
13342         case RID_PROTECTED:
13343         case RID_PRIVATE:
13344           /* Consume the access-specifier.  */
13345           cp_lexer_consume_token (parser->lexer);
13346           /* Remember which access-specifier is active.  */
13347           current_access_specifier = token->value;
13348           /* Look for the `:'.  */
13349           cp_parser_require (parser, CPP_COLON, "`:'");
13350           break;
13351
13352         default:
13353           /* Accept #pragmas at class scope.  */
13354           if (token->type == CPP_PRAGMA)
13355             {
13356               cp_parser_pragma (parser, pragma_external);
13357               break;
13358             }
13359
13360           /* Otherwise, the next construction must be a
13361              member-declaration.  */
13362           cp_parser_member_declaration (parser);
13363         }
13364     }
13365 }
13366
13367 /* Parse a member-declaration.
13368
13369    member-declaration:
13370      decl-specifier-seq [opt] member-declarator-list [opt] ;
13371      function-definition ; [opt]
13372      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13373      using-declaration
13374      template-declaration
13375
13376    member-declarator-list:
13377      member-declarator
13378      member-declarator-list , member-declarator
13379
13380    member-declarator:
13381      declarator pure-specifier [opt]
13382      declarator constant-initializer [opt]
13383      identifier [opt] : constant-expression
13384
13385    GNU Extensions:
13386
13387    member-declaration:
13388      __extension__ member-declaration
13389
13390    member-declarator:
13391      declarator attributes [opt] pure-specifier [opt]
13392      declarator attributes [opt] constant-initializer [opt]
13393      identifier [opt] attributes [opt] : constant-expression  */
13394
13395 static void
13396 cp_parser_member_declaration (cp_parser* parser)
13397 {
13398   cp_decl_specifier_seq decl_specifiers;
13399   tree prefix_attributes;
13400   tree decl;
13401   int declares_class_or_enum;
13402   bool friend_p;
13403   cp_token *token;
13404   int saved_pedantic;
13405
13406   /* Check for the `__extension__' keyword.  */
13407   if (cp_parser_extension_opt (parser, &saved_pedantic))
13408     {
13409       /* Recurse.  */
13410       cp_parser_member_declaration (parser);
13411       /* Restore the old value of the PEDANTIC flag.  */
13412       pedantic = saved_pedantic;
13413
13414       return;
13415     }
13416
13417   /* Check for a template-declaration.  */
13418   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13419     {
13420       /* An explicit specialization here is an error condition, and we
13421          expect the specialization handler to detect and report this.  */
13422       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13423           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13424         cp_parser_explicit_specialization (parser);
13425       else
13426         cp_parser_template_declaration (parser, /*member_p=*/true);
13427
13428       return;
13429     }
13430
13431   /* Check for a using-declaration.  */
13432   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13433     {
13434       /* Parse the using-declaration.  */
13435       cp_parser_using_declaration (parser);
13436
13437       return;
13438     }
13439
13440   /* Check for @defs.  */
13441   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13442     {
13443       tree ivar, member;
13444       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13445       ivar = ivar_chains;
13446       while (ivar)
13447         {
13448           member = ivar;
13449           ivar = TREE_CHAIN (member);
13450           TREE_CHAIN (member) = NULL_TREE;
13451           finish_member_declaration (member);
13452         }
13453       return;
13454     }
13455
13456   /* Parse the decl-specifier-seq.  */
13457   cp_parser_decl_specifier_seq (parser,
13458                                 CP_PARSER_FLAGS_OPTIONAL,
13459                                 &decl_specifiers,
13460                                 &declares_class_or_enum);
13461   prefix_attributes = decl_specifiers.attributes;
13462   decl_specifiers.attributes = NULL_TREE;
13463   /* Check for an invalid type-name.  */
13464   if (!decl_specifiers.type
13465       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13466     return;
13467   /* If there is no declarator, then the decl-specifier-seq should
13468      specify a type.  */
13469   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13470     {
13471       /* If there was no decl-specifier-seq, and the next token is a
13472          `;', then we have something like:
13473
13474            struct S { ; };
13475
13476          [class.mem]
13477
13478          Each member-declaration shall declare at least one member
13479          name of the class.  */
13480       if (!decl_specifiers.any_specifiers_p)
13481         {
13482           cp_token *token = cp_lexer_peek_token (parser->lexer);
13483           if (pedantic && !token->in_system_header)
13484             pedwarn ("%Hextra %<;%>", &token->location);
13485         }
13486       else
13487         {
13488           tree type;
13489
13490           /* See if this declaration is a friend.  */
13491           friend_p = cp_parser_friend_p (&decl_specifiers);
13492           /* If there were decl-specifiers, check to see if there was
13493              a class-declaration.  */
13494           type = check_tag_decl (&decl_specifiers);
13495           /* Nested classes have already been added to the class, but
13496              a `friend' needs to be explicitly registered.  */
13497           if (friend_p)
13498             {
13499               /* If the `friend' keyword was present, the friend must
13500                  be introduced with a class-key.  */
13501                if (!declares_class_or_enum)
13502                  error ("a class-key must be used when declaring a friend");
13503                /* In this case:
13504
13505                     template <typename T> struct A {
13506                       friend struct A<T>::B;
13507                     };
13508
13509                   A<T>::B will be represented by a TYPENAME_TYPE, and
13510                   therefore not recognized by check_tag_decl.  */
13511                if (!type
13512                    && decl_specifiers.type
13513                    && TYPE_P (decl_specifiers.type))
13514                  type = decl_specifiers.type;
13515                if (!type || !TYPE_P (type))
13516                  error ("friend declaration does not name a class or "
13517                         "function");
13518                else
13519                  make_friend_class (current_class_type, type,
13520                                     /*complain=*/true);
13521             }
13522           /* If there is no TYPE, an error message will already have
13523              been issued.  */
13524           else if (!type || type == error_mark_node)
13525             ;
13526           /* An anonymous aggregate has to be handled specially; such
13527              a declaration really declares a data member (with a
13528              particular type), as opposed to a nested class.  */
13529           else if (ANON_AGGR_TYPE_P (type))
13530             {
13531               /* Remove constructors and such from TYPE, now that we
13532                  know it is an anonymous aggregate.  */
13533               fixup_anonymous_aggr (type);
13534               /* And make the corresponding data member.  */
13535               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13536               /* Add it to the class.  */
13537               finish_member_declaration (decl);
13538             }
13539           else
13540             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13541         }
13542     }
13543   else
13544     {
13545       /* See if these declarations will be friends.  */
13546       friend_p = cp_parser_friend_p (&decl_specifiers);
13547
13548       /* Keep going until we hit the `;' at the end of the
13549          declaration.  */
13550       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13551         {
13552           tree attributes = NULL_TREE;
13553           tree first_attribute;
13554
13555           /* Peek at the next token.  */
13556           token = cp_lexer_peek_token (parser->lexer);
13557
13558           /* Check for a bitfield declaration.  */
13559           if (token->type == CPP_COLON
13560               || (token->type == CPP_NAME
13561                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13562                   == CPP_COLON))
13563             {
13564               tree identifier;
13565               tree width;
13566
13567               /* Get the name of the bitfield.  Note that we cannot just
13568                  check TOKEN here because it may have been invalidated by
13569                  the call to cp_lexer_peek_nth_token above.  */
13570               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13571                 identifier = cp_parser_identifier (parser);
13572               else
13573                 identifier = NULL_TREE;
13574
13575               /* Consume the `:' token.  */
13576               cp_lexer_consume_token (parser->lexer);
13577               /* Get the width of the bitfield.  */
13578               width
13579                 = cp_parser_constant_expression (parser,
13580                                                  /*allow_non_constant=*/false,
13581                                                  NULL);
13582
13583               /* Look for attributes that apply to the bitfield.  */
13584               attributes = cp_parser_attributes_opt (parser);
13585               /* Remember which attributes are prefix attributes and
13586                  which are not.  */
13587               first_attribute = attributes;
13588               /* Combine the attributes.  */
13589               attributes = chainon (prefix_attributes, attributes);
13590
13591               /* Create the bitfield declaration.  */
13592               decl = grokbitfield (identifier
13593                                    ? make_id_declarator (NULL_TREE,
13594                                                          identifier,
13595                                                          sfk_none)
13596                                    : NULL,
13597                                    &decl_specifiers,
13598                                    width);
13599               /* Apply the attributes.  */
13600               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13601             }
13602           else
13603             {
13604               cp_declarator *declarator;
13605               tree initializer;
13606               tree asm_specification;
13607               int ctor_dtor_or_conv_p;
13608
13609               /* Parse the declarator.  */
13610               declarator
13611                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13612                                         &ctor_dtor_or_conv_p,
13613                                         /*parenthesized_p=*/NULL,
13614                                         /*member_p=*/true);
13615
13616               /* If something went wrong parsing the declarator, make sure
13617                  that we at least consume some tokens.  */
13618               if (declarator == cp_error_declarator)
13619                 {
13620                   /* Skip to the end of the statement.  */
13621                   cp_parser_skip_to_end_of_statement (parser);
13622                   /* If the next token is not a semicolon, that is
13623                      probably because we just skipped over the body of
13624                      a function.  So, we consume a semicolon if
13625                      present, but do not issue an error message if it
13626                      is not present.  */
13627                   if (cp_lexer_next_token_is (parser->lexer,
13628                                               CPP_SEMICOLON))
13629                     cp_lexer_consume_token (parser->lexer);
13630                   return;
13631                 }
13632
13633               if (declares_class_or_enum & 2)
13634                 cp_parser_check_for_definition_in_return_type
13635                   (declarator, decl_specifiers.type);
13636
13637               /* Look for an asm-specification.  */
13638               asm_specification = cp_parser_asm_specification_opt (parser);
13639               /* Look for attributes that apply to the declaration.  */
13640               attributes = cp_parser_attributes_opt (parser);
13641               /* Remember which attributes are prefix attributes and
13642                  which are not.  */
13643               first_attribute = attributes;
13644               /* Combine the attributes.  */
13645               attributes = chainon (prefix_attributes, attributes);
13646
13647               /* If it's an `=', then we have a constant-initializer or a
13648                  pure-specifier.  It is not correct to parse the
13649                  initializer before registering the member declaration
13650                  since the member declaration should be in scope while
13651                  its initializer is processed.  However, the rest of the
13652                  front end does not yet provide an interface that allows
13653                  us to handle this correctly.  */
13654               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13655                 {
13656                   /* In [class.mem]:
13657
13658                      A pure-specifier shall be used only in the declaration of
13659                      a virtual function.
13660
13661                      A member-declarator can contain a constant-initializer
13662                      only if it declares a static member of integral or
13663                      enumeration type.
13664
13665                      Therefore, if the DECLARATOR is for a function, we look
13666                      for a pure-specifier; otherwise, we look for a
13667                      constant-initializer.  When we call `grokfield', it will
13668                      perform more stringent semantics checks.  */
13669                   if (declarator->kind == cdk_function)
13670                     initializer = cp_parser_pure_specifier (parser);
13671                   else
13672                     /* Parse the initializer.  */
13673                     initializer = cp_parser_constant_initializer (parser);
13674                 }
13675               /* Otherwise, there is no initializer.  */
13676               else
13677                 initializer = NULL_TREE;
13678
13679               /* See if we are probably looking at a function
13680                  definition.  We are certainly not looking at a
13681                  member-declarator.  Calling `grokfield' has
13682                  side-effects, so we must not do it unless we are sure
13683                  that we are looking at a member-declarator.  */
13684               if (cp_parser_token_starts_function_definition_p
13685                   (cp_lexer_peek_token (parser->lexer)))
13686                 {
13687                   /* The grammar does not allow a pure-specifier to be
13688                      used when a member function is defined.  (It is
13689                      possible that this fact is an oversight in the
13690                      standard, since a pure function may be defined
13691                      outside of the class-specifier.  */
13692                   if (initializer)
13693                     error ("pure-specifier on function-definition");
13694                   decl = cp_parser_save_member_function_body (parser,
13695                                                               &decl_specifiers,
13696                                                               declarator,
13697                                                               attributes);
13698                   /* If the member was not a friend, declare it here.  */
13699                   if (!friend_p)
13700                     finish_member_declaration (decl);
13701                   /* Peek at the next token.  */
13702                   token = cp_lexer_peek_token (parser->lexer);
13703                   /* If the next token is a semicolon, consume it.  */
13704                   if (token->type == CPP_SEMICOLON)
13705                     cp_lexer_consume_token (parser->lexer);
13706                   return;
13707                 }
13708               else
13709                 {
13710                   /* Create the declaration.  */
13711                   decl = grokfield (declarator, &decl_specifiers,
13712                                     initializer, asm_specification,
13713                                     attributes);
13714                   /* Any initialization must have been from a
13715                      constant-expression.  */
13716                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13717                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13718                 }
13719             }
13720
13721           /* Reset PREFIX_ATTRIBUTES.  */
13722           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13723             attributes = TREE_CHAIN (attributes);
13724           if (attributes)
13725             TREE_CHAIN (attributes) = NULL_TREE;
13726
13727           /* If there is any qualification still in effect, clear it
13728              now; we will be starting fresh with the next declarator.  */
13729           parser->scope = NULL_TREE;
13730           parser->qualifying_scope = NULL_TREE;
13731           parser->object_scope = NULL_TREE;
13732           /* If it's a `,', then there are more declarators.  */
13733           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13734             cp_lexer_consume_token (parser->lexer);
13735           /* If the next token isn't a `;', then we have a parse error.  */
13736           else if (cp_lexer_next_token_is_not (parser->lexer,
13737                                                CPP_SEMICOLON))
13738             {
13739               cp_parser_error (parser, "expected %<;%>");
13740               /* Skip tokens until we find a `;'.  */
13741               cp_parser_skip_to_end_of_statement (parser);
13742
13743               break;
13744             }
13745
13746           if (decl)
13747             {
13748               /* Add DECL to the list of members.  */
13749               if (!friend_p)
13750                 finish_member_declaration (decl);
13751
13752               if (TREE_CODE (decl) == FUNCTION_DECL)
13753                 cp_parser_save_default_args (parser, decl);
13754             }
13755         }
13756     }
13757
13758   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13759 }
13760
13761 /* Parse a pure-specifier.
13762
13763    pure-specifier:
13764      = 0
13765
13766    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13767    Otherwise, ERROR_MARK_NODE is returned.  */
13768
13769 static tree
13770 cp_parser_pure_specifier (cp_parser* parser)
13771 {
13772   cp_token *token;
13773
13774   /* Look for the `=' token.  */
13775   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13776     return error_mark_node;
13777   /* Look for the `0' token.  */
13778   token = cp_lexer_consume_token (parser->lexer);
13779   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
13780   if (token->type == CPP_NUMBER && (token->flags & PURE_ZERO))
13781     return integer_zero_node;
13782
13783   cp_parser_error (parser, "invalid pure specifier (only `= 0' is allowed)");
13784   cp_parser_skip_to_end_of_statement (parser);
13785   return error_mark_node;
13786 }
13787
13788 /* Parse a constant-initializer.
13789
13790    constant-initializer:
13791      = constant-expression
13792
13793    Returns a representation of the constant-expression.  */
13794
13795 static tree
13796 cp_parser_constant_initializer (cp_parser* parser)
13797 {
13798   /* Look for the `=' token.  */
13799   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13800     return error_mark_node;
13801
13802   /* It is invalid to write:
13803
13804        struct S { static const int i = { 7 }; };
13805
13806      */
13807   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13808     {
13809       cp_parser_error (parser,
13810                        "a brace-enclosed initializer is not allowed here");
13811       /* Consume the opening brace.  */
13812       cp_lexer_consume_token (parser->lexer);
13813       /* Skip the initializer.  */
13814       cp_parser_skip_to_closing_brace (parser);
13815       /* Look for the trailing `}'.  */
13816       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13817
13818       return error_mark_node;
13819     }
13820
13821   return cp_parser_constant_expression (parser,
13822                                         /*allow_non_constant=*/false,
13823                                         NULL);
13824 }
13825
13826 /* Derived classes [gram.class.derived] */
13827
13828 /* Parse a base-clause.
13829
13830    base-clause:
13831      : base-specifier-list
13832
13833    base-specifier-list:
13834      base-specifier
13835      base-specifier-list , base-specifier
13836
13837    Returns a TREE_LIST representing the base-classes, in the order in
13838    which they were declared.  The representation of each node is as
13839    described by cp_parser_base_specifier.
13840
13841    In the case that no bases are specified, this function will return
13842    NULL_TREE, not ERROR_MARK_NODE.  */
13843
13844 static tree
13845 cp_parser_base_clause (cp_parser* parser)
13846 {
13847   tree bases = NULL_TREE;
13848
13849   /* Look for the `:' that begins the list.  */
13850   cp_parser_require (parser, CPP_COLON, "`:'");
13851
13852   /* Scan the base-specifier-list.  */
13853   while (true)
13854     {
13855       cp_token *token;
13856       tree base;
13857
13858       /* Look for the base-specifier.  */
13859       base = cp_parser_base_specifier (parser);
13860       /* Add BASE to the front of the list.  */
13861       if (base != error_mark_node)
13862         {
13863           TREE_CHAIN (base) = bases;
13864           bases = base;
13865         }
13866       /* Peek at the next token.  */
13867       token = cp_lexer_peek_token (parser->lexer);
13868       /* If it's not a comma, then the list is complete.  */
13869       if (token->type != CPP_COMMA)
13870         break;
13871       /* Consume the `,'.  */
13872       cp_lexer_consume_token (parser->lexer);
13873     }
13874
13875   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13876      base class had a qualified name.  However, the next name that
13877      appears is certainly not qualified.  */
13878   parser->scope = NULL_TREE;
13879   parser->qualifying_scope = NULL_TREE;
13880   parser->object_scope = NULL_TREE;
13881
13882   return nreverse (bases);
13883 }
13884
13885 /* Parse a base-specifier.
13886
13887    base-specifier:
13888      :: [opt] nested-name-specifier [opt] class-name
13889      virtual access-specifier [opt] :: [opt] nested-name-specifier
13890        [opt] class-name
13891      access-specifier virtual [opt] :: [opt] nested-name-specifier
13892        [opt] class-name
13893
13894    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13895    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13896    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13897    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13898
13899 static tree
13900 cp_parser_base_specifier (cp_parser* parser)
13901 {
13902   cp_token *token;
13903   bool done = false;
13904   bool virtual_p = false;
13905   bool duplicate_virtual_error_issued_p = false;
13906   bool duplicate_access_error_issued_p = false;
13907   bool class_scope_p, template_p;
13908   tree access = access_default_node;
13909   tree type;
13910
13911   /* Process the optional `virtual' and `access-specifier'.  */
13912   while (!done)
13913     {
13914       /* Peek at the next token.  */
13915       token = cp_lexer_peek_token (parser->lexer);
13916       /* Process `virtual'.  */
13917       switch (token->keyword)
13918         {
13919         case RID_VIRTUAL:
13920           /* If `virtual' appears more than once, issue an error.  */
13921           if (virtual_p && !duplicate_virtual_error_issued_p)
13922             {
13923               cp_parser_error (parser,
13924                                "%<virtual%> specified more than once in base-specified");
13925               duplicate_virtual_error_issued_p = true;
13926             }
13927
13928           virtual_p = true;
13929
13930           /* Consume the `virtual' token.  */
13931           cp_lexer_consume_token (parser->lexer);
13932
13933           break;
13934
13935         case RID_PUBLIC:
13936         case RID_PROTECTED:
13937         case RID_PRIVATE:
13938           /* If more than one access specifier appears, issue an
13939              error.  */
13940           if (access != access_default_node
13941               && !duplicate_access_error_issued_p)
13942             {
13943               cp_parser_error (parser,
13944                                "more than one access specifier in base-specified");
13945               duplicate_access_error_issued_p = true;
13946             }
13947
13948           access = ridpointers[(int) token->keyword];
13949
13950           /* Consume the access-specifier.  */
13951           cp_lexer_consume_token (parser->lexer);
13952
13953           break;
13954
13955         default:
13956           done = true;
13957           break;
13958         }
13959     }
13960   /* It is not uncommon to see programs mechanically, erroneously, use
13961      the 'typename' keyword to denote (dependent) qualified types
13962      as base classes.  */
13963   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13964     {
13965       if (!processing_template_decl)
13966         error ("keyword %<typename%> not allowed outside of templates");
13967       else
13968         error ("keyword %<typename%> not allowed in this context "
13969                "(the base class is implicitly a type)");
13970       cp_lexer_consume_token (parser->lexer);
13971     }
13972
13973   /* Look for the optional `::' operator.  */
13974   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13975   /* Look for the nested-name-specifier.  The simplest way to
13976      implement:
13977
13978        [temp.res]
13979
13980        The keyword `typename' is not permitted in a base-specifier or
13981        mem-initializer; in these contexts a qualified name that
13982        depends on a template-parameter is implicitly assumed to be a
13983        type name.
13984
13985      is to pretend that we have seen the `typename' keyword at this
13986      point.  */
13987   cp_parser_nested_name_specifier_opt (parser,
13988                                        /*typename_keyword_p=*/true,
13989                                        /*check_dependency_p=*/true,
13990                                        typename_type,
13991                                        /*is_declaration=*/true);
13992   /* If the base class is given by a qualified name, assume that names
13993      we see are type names or templates, as appropriate.  */
13994   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13995   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13996
13997   /* Finally, look for the class-name.  */
13998   type = cp_parser_class_name (parser,
13999                                class_scope_p,
14000                                template_p,
14001                                typename_type,
14002                                /*check_dependency_p=*/true,
14003                                /*class_head_p=*/false,
14004                                /*is_declaration=*/true);
14005
14006   if (type == error_mark_node)
14007     return error_mark_node;
14008
14009   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14010 }
14011
14012 /* Exception handling [gram.exception] */
14013
14014 /* Parse an (optional) exception-specification.
14015
14016    exception-specification:
14017      throw ( type-id-list [opt] )
14018
14019    Returns a TREE_LIST representing the exception-specification.  The
14020    TREE_VALUE of each node is a type.  */
14021
14022 static tree
14023 cp_parser_exception_specification_opt (cp_parser* parser)
14024 {
14025   cp_token *token;
14026   tree type_id_list;
14027
14028   /* Peek at the next token.  */
14029   token = cp_lexer_peek_token (parser->lexer);
14030   /* If it's not `throw', then there's no exception-specification.  */
14031   if (!cp_parser_is_keyword (token, RID_THROW))
14032     return NULL_TREE;
14033
14034   /* Consume the `throw'.  */
14035   cp_lexer_consume_token (parser->lexer);
14036
14037   /* Look for the `('.  */
14038   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14039
14040   /* Peek at the next token.  */
14041   token = cp_lexer_peek_token (parser->lexer);
14042   /* If it's not a `)', then there is a type-id-list.  */
14043   if (token->type != CPP_CLOSE_PAREN)
14044     {
14045       const char *saved_message;
14046
14047       /* Types may not be defined in an exception-specification.  */
14048       saved_message = parser->type_definition_forbidden_message;
14049       parser->type_definition_forbidden_message
14050         = "types may not be defined in an exception-specification";
14051       /* Parse the type-id-list.  */
14052       type_id_list = cp_parser_type_id_list (parser);
14053       /* Restore the saved message.  */
14054       parser->type_definition_forbidden_message = saved_message;
14055     }
14056   else
14057     type_id_list = empty_except_spec;
14058
14059   /* Look for the `)'.  */
14060   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14061
14062   return type_id_list;
14063 }
14064
14065 /* Parse an (optional) type-id-list.
14066
14067    type-id-list:
14068      type-id
14069      type-id-list , type-id
14070
14071    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14072    in the order that the types were presented.  */
14073
14074 static tree
14075 cp_parser_type_id_list (cp_parser* parser)
14076 {
14077   tree types = NULL_TREE;
14078
14079   while (true)
14080     {
14081       cp_token *token;
14082       tree type;
14083
14084       /* Get the next type-id.  */
14085       type = cp_parser_type_id (parser);
14086       /* Add it to the list.  */
14087       types = add_exception_specifier (types, type, /*complain=*/1);
14088       /* Peek at the next token.  */
14089       token = cp_lexer_peek_token (parser->lexer);
14090       /* If it is not a `,', we are done.  */
14091       if (token->type != CPP_COMMA)
14092         break;
14093       /* Consume the `,'.  */
14094       cp_lexer_consume_token (parser->lexer);
14095     }
14096
14097   return nreverse (types);
14098 }
14099
14100 /* Parse a try-block.
14101
14102    try-block:
14103      try compound-statement handler-seq  */
14104
14105 static tree
14106 cp_parser_try_block (cp_parser* parser)
14107 {
14108   tree try_block;
14109
14110   cp_parser_require_keyword (parser, RID_TRY, "`try'");
14111   try_block = begin_try_block ();
14112   cp_parser_compound_statement (parser, NULL, true);
14113   finish_try_block (try_block);
14114   cp_parser_handler_seq (parser);
14115   finish_handler_sequence (try_block);
14116
14117   return try_block;
14118 }
14119
14120 /* Parse a function-try-block.
14121
14122    function-try-block:
14123      try ctor-initializer [opt] function-body handler-seq  */
14124
14125 static bool
14126 cp_parser_function_try_block (cp_parser* parser)
14127 {
14128   tree try_block;
14129   bool ctor_initializer_p;
14130
14131   /* Look for the `try' keyword.  */
14132   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14133     return false;
14134   /* Let the rest of the front-end know where we are.  */
14135   try_block = begin_function_try_block ();
14136   /* Parse the function-body.  */
14137   ctor_initializer_p
14138     = cp_parser_ctor_initializer_opt_and_function_body (parser);
14139   /* We're done with the `try' part.  */
14140   finish_function_try_block (try_block);
14141   /* Parse the handlers.  */
14142   cp_parser_handler_seq (parser);
14143   /* We're done with the handlers.  */
14144   finish_function_handler_sequence (try_block);
14145
14146   return ctor_initializer_p;
14147 }
14148
14149 /* Parse a handler-seq.
14150
14151    handler-seq:
14152      handler handler-seq [opt]  */
14153
14154 static void
14155 cp_parser_handler_seq (cp_parser* parser)
14156 {
14157   while (true)
14158     {
14159       cp_token *token;
14160
14161       /* Parse the handler.  */
14162       cp_parser_handler (parser);
14163       /* Peek at the next token.  */
14164       token = cp_lexer_peek_token (parser->lexer);
14165       /* If it's not `catch' then there are no more handlers.  */
14166       if (!cp_parser_is_keyword (token, RID_CATCH))
14167         break;
14168     }
14169 }
14170
14171 /* Parse a handler.
14172
14173    handler:
14174      catch ( exception-declaration ) compound-statement  */
14175
14176 static void
14177 cp_parser_handler (cp_parser* parser)
14178 {
14179   tree handler;
14180   tree declaration;
14181
14182   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14183   handler = begin_handler ();
14184   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14185   declaration = cp_parser_exception_declaration (parser);
14186   finish_handler_parms (declaration, handler);
14187   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14188   cp_parser_compound_statement (parser, NULL, false);
14189   finish_handler (handler);
14190 }
14191
14192 /* Parse an exception-declaration.
14193
14194    exception-declaration:
14195      type-specifier-seq declarator
14196      type-specifier-seq abstract-declarator
14197      type-specifier-seq
14198      ...
14199
14200    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14201    ellipsis variant is used.  */
14202
14203 static tree
14204 cp_parser_exception_declaration (cp_parser* parser)
14205 {
14206   tree decl;
14207   cp_decl_specifier_seq type_specifiers;
14208   cp_declarator *declarator;
14209   const char *saved_message;
14210
14211   /* If it's an ellipsis, it's easy to handle.  */
14212   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14213     {
14214       /* Consume the `...' token.  */
14215       cp_lexer_consume_token (parser->lexer);
14216       return NULL_TREE;
14217     }
14218
14219   /* Types may not be defined in exception-declarations.  */
14220   saved_message = parser->type_definition_forbidden_message;
14221   parser->type_definition_forbidden_message
14222     = "types may not be defined in exception-declarations";
14223
14224   /* Parse the type-specifier-seq.  */
14225   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14226                                 &type_specifiers);
14227   /* If it's a `)', then there is no declarator.  */
14228   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14229     declarator = NULL;
14230   else
14231     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14232                                        /*ctor_dtor_or_conv_p=*/NULL,
14233                                        /*parenthesized_p=*/NULL,
14234                                        /*member_p=*/false);
14235
14236   /* Restore the saved message.  */
14237   parser->type_definition_forbidden_message = saved_message;
14238
14239   if (type_specifiers.any_specifiers_p)
14240     {
14241       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14242       if (decl == NULL_TREE)
14243         error ("invalid catch parameter");
14244     }
14245   else
14246     decl = NULL_TREE;
14247
14248   return decl;
14249 }
14250
14251 /* Parse a throw-expression.
14252
14253    throw-expression:
14254      throw assignment-expression [opt]
14255
14256    Returns a THROW_EXPR representing the throw-expression.  */
14257
14258 static tree
14259 cp_parser_throw_expression (cp_parser* parser)
14260 {
14261   tree expression;
14262   cp_token* token;
14263
14264   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14265   token = cp_lexer_peek_token (parser->lexer);
14266   /* Figure out whether or not there is an assignment-expression
14267      following the "throw" keyword.  */
14268   if (token->type == CPP_COMMA
14269       || token->type == CPP_SEMICOLON
14270       || token->type == CPP_CLOSE_PAREN
14271       || token->type == CPP_CLOSE_SQUARE
14272       || token->type == CPP_CLOSE_BRACE
14273       || token->type == CPP_COLON)
14274     expression = NULL_TREE;
14275   else
14276     expression = cp_parser_assignment_expression (parser,
14277                                                   /*cast_p=*/false);
14278
14279   return build_throw (expression);
14280 }
14281
14282 /* GNU Extensions */
14283
14284 /* Parse an (optional) asm-specification.
14285
14286    asm-specification:
14287      asm ( string-literal )
14288
14289    If the asm-specification is present, returns a STRING_CST
14290    corresponding to the string-literal.  Otherwise, returns
14291    NULL_TREE.  */
14292
14293 static tree
14294 cp_parser_asm_specification_opt (cp_parser* parser)
14295 {
14296   cp_token *token;
14297   tree asm_specification;
14298
14299   /* Peek at the next token.  */
14300   token = cp_lexer_peek_token (parser->lexer);
14301   /* If the next token isn't the `asm' keyword, then there's no
14302      asm-specification.  */
14303   if (!cp_parser_is_keyword (token, RID_ASM))
14304     return NULL_TREE;
14305
14306   /* Consume the `asm' token.  */
14307   cp_lexer_consume_token (parser->lexer);
14308   /* Look for the `('.  */
14309   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14310
14311   /* Look for the string-literal.  */
14312   asm_specification = cp_parser_string_literal (parser, false, false);
14313
14314   /* Look for the `)'.  */
14315   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14316
14317   return asm_specification;
14318 }
14319
14320 /* Parse an asm-operand-list.
14321
14322    asm-operand-list:
14323      asm-operand
14324      asm-operand-list , asm-operand
14325
14326    asm-operand:
14327      string-literal ( expression )
14328      [ string-literal ] string-literal ( expression )
14329
14330    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14331    each node is the expression.  The TREE_PURPOSE is itself a
14332    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14333    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14334    is a STRING_CST for the string literal before the parenthesis.  */
14335
14336 static tree
14337 cp_parser_asm_operand_list (cp_parser* parser)
14338 {
14339   tree asm_operands = NULL_TREE;
14340
14341   while (true)
14342     {
14343       tree string_literal;
14344       tree expression;
14345       tree name;
14346
14347       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14348         {
14349           /* Consume the `[' token.  */
14350           cp_lexer_consume_token (parser->lexer);
14351           /* Read the operand name.  */
14352           name = cp_parser_identifier (parser);
14353           if (name != error_mark_node)
14354             name = build_string (IDENTIFIER_LENGTH (name),
14355                                  IDENTIFIER_POINTER (name));
14356           /* Look for the closing `]'.  */
14357           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14358         }
14359       else
14360         name = NULL_TREE;
14361       /* Look for the string-literal.  */
14362       string_literal = cp_parser_string_literal (parser, false, false);
14363
14364       /* Look for the `('.  */
14365       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14366       /* Parse the expression.  */
14367       expression = cp_parser_expression (parser, /*cast_p=*/false);
14368       /* Look for the `)'.  */
14369       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14370
14371       /* Add this operand to the list.  */
14372       asm_operands = tree_cons (build_tree_list (name, string_literal),
14373                                 expression,
14374                                 asm_operands);
14375       /* If the next token is not a `,', there are no more
14376          operands.  */
14377       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14378         break;
14379       /* Consume the `,'.  */
14380       cp_lexer_consume_token (parser->lexer);
14381     }
14382
14383   return nreverse (asm_operands);
14384 }
14385
14386 /* Parse an asm-clobber-list.
14387
14388    asm-clobber-list:
14389      string-literal
14390      asm-clobber-list , string-literal
14391
14392    Returns a TREE_LIST, indicating the clobbers in the order that they
14393    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14394
14395 static tree
14396 cp_parser_asm_clobber_list (cp_parser* parser)
14397 {
14398   tree clobbers = NULL_TREE;
14399
14400   while (true)
14401     {
14402       tree string_literal;
14403
14404       /* Look for the string literal.  */
14405       string_literal = cp_parser_string_literal (parser, false, false);
14406       /* Add it to the list.  */
14407       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14408       /* If the next token is not a `,', then the list is
14409          complete.  */
14410       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14411         break;
14412       /* Consume the `,' token.  */
14413       cp_lexer_consume_token (parser->lexer);
14414     }
14415
14416   return clobbers;
14417 }
14418
14419 /* Parse an (optional) series of attributes.
14420
14421    attributes:
14422      attributes attribute
14423
14424    attribute:
14425      __attribute__ (( attribute-list [opt] ))
14426
14427    The return value is as for cp_parser_attribute_list.  */
14428
14429 static tree
14430 cp_parser_attributes_opt (cp_parser* parser)
14431 {
14432   tree attributes = NULL_TREE;
14433
14434   while (true)
14435     {
14436       cp_token *token;
14437       tree attribute_list;
14438
14439       /* Peek at the next token.  */
14440       token = cp_lexer_peek_token (parser->lexer);
14441       /* If it's not `__attribute__', then we're done.  */
14442       if (token->keyword != RID_ATTRIBUTE)
14443         break;
14444
14445       /* Consume the `__attribute__' keyword.  */
14446       cp_lexer_consume_token (parser->lexer);
14447       /* Look for the two `(' tokens.  */
14448       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14449       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14450
14451       /* Peek at the next token.  */
14452       token = cp_lexer_peek_token (parser->lexer);
14453       if (token->type != CPP_CLOSE_PAREN)
14454         /* Parse the attribute-list.  */
14455         attribute_list = cp_parser_attribute_list (parser);
14456       else
14457         /* If the next token is a `)', then there is no attribute
14458            list.  */
14459         attribute_list = NULL;
14460
14461       /* Look for the two `)' tokens.  */
14462       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14463       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14464
14465       /* Add these new attributes to the list.  */
14466       attributes = chainon (attributes, attribute_list);
14467     }
14468
14469   return attributes;
14470 }
14471
14472 /* Parse an attribute-list.
14473
14474    attribute-list:
14475      attribute
14476      attribute-list , attribute
14477
14478    attribute:
14479      identifier
14480      identifier ( identifier )
14481      identifier ( identifier , expression-list )
14482      identifier ( expression-list )
14483
14484    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14485    to an attribute.  The TREE_PURPOSE of each node is the identifier
14486    indicating which attribute is in use.  The TREE_VALUE represents
14487    the arguments, if any.  */
14488
14489 static tree
14490 cp_parser_attribute_list (cp_parser* parser)
14491 {
14492   tree attribute_list = NULL_TREE;
14493   bool save_translate_strings_p = parser->translate_strings_p;
14494
14495   parser->translate_strings_p = false;
14496   while (true)
14497     {
14498       cp_token *token;
14499       tree identifier;
14500       tree attribute;
14501
14502       /* Look for the identifier.  We also allow keywords here; for
14503          example `__attribute__ ((const))' is legal.  */
14504       token = cp_lexer_peek_token (parser->lexer);
14505       if (token->type == CPP_NAME
14506           || token->type == CPP_KEYWORD)
14507         {
14508           /* Consume the token.  */
14509           token = cp_lexer_consume_token (parser->lexer);
14510
14511           /* Save away the identifier that indicates which attribute
14512              this is.  */
14513           identifier = token->value;
14514           attribute = build_tree_list (identifier, NULL_TREE);
14515
14516           /* Peek at the next token.  */
14517           token = cp_lexer_peek_token (parser->lexer);
14518           /* If it's an `(', then parse the attribute arguments.  */
14519           if (token->type == CPP_OPEN_PAREN)
14520             {
14521               tree arguments;
14522
14523               arguments = (cp_parser_parenthesized_expression_list
14524                            (parser, true, /*cast_p=*/false,
14525                             /*non_constant_p=*/NULL));
14526               /* Save the identifier and arguments away.  */
14527               TREE_VALUE (attribute) = arguments;
14528             }
14529
14530           /* Add this attribute to the list.  */
14531           TREE_CHAIN (attribute) = attribute_list;
14532           attribute_list = attribute;
14533
14534           token = cp_lexer_peek_token (parser->lexer);
14535         }
14536       /* Now, look for more attributes.  If the next token isn't a
14537          `,', we're done.  */
14538       if (token->type != CPP_COMMA)
14539         break;
14540
14541       /* Consume the comma and keep going.  */
14542       cp_lexer_consume_token (parser->lexer);
14543     }
14544   parser->translate_strings_p = save_translate_strings_p;
14545
14546   /* We built up the list in reverse order.  */
14547   return nreverse (attribute_list);
14548 }
14549
14550 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14551    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14552    current value of the PEDANTIC flag, regardless of whether or not
14553    the `__extension__' keyword is present.  The caller is responsible
14554    for restoring the value of the PEDANTIC flag.  */
14555
14556 static bool
14557 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14558 {
14559   /* Save the old value of the PEDANTIC flag.  */
14560   *saved_pedantic = pedantic;
14561
14562   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14563     {
14564       /* Consume the `__extension__' token.  */
14565       cp_lexer_consume_token (parser->lexer);
14566       /* We're not being pedantic while the `__extension__' keyword is
14567          in effect.  */
14568       pedantic = 0;
14569
14570       return true;
14571     }
14572
14573   return false;
14574 }
14575
14576 /* Parse a label declaration.
14577
14578    label-declaration:
14579      __label__ label-declarator-seq ;
14580
14581    label-declarator-seq:
14582      identifier , label-declarator-seq
14583      identifier  */
14584
14585 static void
14586 cp_parser_label_declaration (cp_parser* parser)
14587 {
14588   /* Look for the `__label__' keyword.  */
14589   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14590
14591   while (true)
14592     {
14593       tree identifier;
14594
14595       /* Look for an identifier.  */
14596       identifier = cp_parser_identifier (parser);
14597       /* If we failed, stop.  */
14598       if (identifier == error_mark_node)
14599         break;
14600       /* Declare it as a label.  */
14601       finish_label_decl (identifier);
14602       /* If the next token is a `;', stop.  */
14603       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14604         break;
14605       /* Look for the `,' separating the label declarations.  */
14606       cp_parser_require (parser, CPP_COMMA, "`,'");
14607     }
14608
14609   /* Look for the final `;'.  */
14610   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14611 }
14612
14613 /* Support Functions */
14614
14615 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14616    NAME should have one of the representations used for an
14617    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14618    is returned.  If PARSER->SCOPE is a dependent type, then a
14619    SCOPE_REF is returned.
14620
14621    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14622    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14623    was formed.  Abstractly, such entities should not be passed to this
14624    function, because they do not need to be looked up, but it is
14625    simpler to check for this special case here, rather than at the
14626    call-sites.
14627
14628    In cases not explicitly covered above, this function returns a
14629    DECL, OVERLOAD, or baselink representing the result of the lookup.
14630    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14631    is returned.
14632
14633    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14634    (e.g., "struct") that was used.  In that case bindings that do not
14635    refer to types are ignored.
14636
14637    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14638    ignored.
14639
14640    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14641    are ignored.
14642
14643    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14644    types.
14645
14646    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14647    TREE_LIST of candidates if name-lookup results in an ambiguity, and
14648    NULL_TREE otherwise.  */ 
14649
14650 static tree
14651 cp_parser_lookup_name (cp_parser *parser, tree name,
14652                        enum tag_types tag_type,
14653                        bool is_template, 
14654                        bool is_namespace,
14655                        bool check_dependency,
14656                        tree *ambiguous_decls)
14657 {
14658   int flags = 0;
14659   tree decl;
14660   tree object_type = parser->context->object_type;
14661
14662   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14663     flags |= LOOKUP_COMPLAIN;
14664
14665   /* Assume that the lookup will be unambiguous.  */
14666   if (ambiguous_decls)
14667     *ambiguous_decls = NULL_TREE;
14668
14669   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14670      no longer valid.  Note that if we are parsing tentatively, and
14671      the parse fails, OBJECT_TYPE will be automatically restored.  */
14672   parser->context->object_type = NULL_TREE;
14673
14674   if (name == error_mark_node)
14675     return error_mark_node;
14676
14677   /* A template-id has already been resolved; there is no lookup to
14678      do.  */
14679   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14680     return name;
14681   if (BASELINK_P (name))
14682     {
14683       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14684                   == TEMPLATE_ID_EXPR);
14685       return name;
14686     }
14687
14688   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14689      it should already have been checked to make sure that the name
14690      used matches the type being destroyed.  */
14691   if (TREE_CODE (name) == BIT_NOT_EXPR)
14692     {
14693       tree type;
14694
14695       /* Figure out to which type this destructor applies.  */
14696       if (parser->scope)
14697         type = parser->scope;
14698       else if (object_type)
14699         type = object_type;
14700       else
14701         type = current_class_type;
14702       /* If that's not a class type, there is no destructor.  */
14703       if (!type || !CLASS_TYPE_P (type))
14704         return error_mark_node;
14705       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14706         lazily_declare_fn (sfk_destructor, type);
14707       if (!CLASSTYPE_DESTRUCTORS (type))
14708           return error_mark_node;
14709       /* If it was a class type, return the destructor.  */
14710       return CLASSTYPE_DESTRUCTORS (type);
14711     }
14712
14713   /* By this point, the NAME should be an ordinary identifier.  If
14714      the id-expression was a qualified name, the qualifying scope is
14715      stored in PARSER->SCOPE at this point.  */
14716   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14717
14718   /* Perform the lookup.  */
14719   if (parser->scope)
14720     {
14721       bool dependent_p;
14722
14723       if (parser->scope == error_mark_node)
14724         return error_mark_node;
14725
14726       /* If the SCOPE is dependent, the lookup must be deferred until
14727          the template is instantiated -- unless we are explicitly
14728          looking up names in uninstantiated templates.  Even then, we
14729          cannot look up the name if the scope is not a class type; it
14730          might, for example, be a template type parameter.  */
14731       dependent_p = (TYPE_P (parser->scope)
14732                      && !(parser->in_declarator_p
14733                           && currently_open_class (parser->scope))
14734                      && dependent_type_p (parser->scope));
14735       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14736            && dependent_p)
14737         {
14738           if (tag_type)
14739             {
14740               tree type;
14741
14742               /* The resolution to Core Issue 180 says that `struct
14743                  A::B' should be considered a type-name, even if `A'
14744                  is dependent.  */
14745               type = make_typename_type (parser->scope, name, tag_type,
14746                                          /*complain=*/tf_error);
14747               decl = TYPE_NAME (type);
14748             }
14749           else if (is_template
14750                    && (cp_parser_next_token_ends_template_argument_p (parser)
14751                        || cp_lexer_next_token_is (parser->lexer,
14752                                                   CPP_CLOSE_PAREN)))
14753             decl = make_unbound_class_template (parser->scope,
14754                                                 name, NULL_TREE,
14755                                                 /*complain=*/tf_error);
14756           else
14757             decl = build_qualified_name (/*type=*/NULL_TREE,
14758                                          parser->scope, name,
14759                                          is_template);
14760         }
14761       else
14762         {
14763           tree pushed_scope = NULL_TREE;
14764
14765           /* If PARSER->SCOPE is a dependent type, then it must be a
14766              class type, and we must not be checking dependencies;
14767              otherwise, we would have processed this lookup above.  So
14768              that PARSER->SCOPE is not considered a dependent base by
14769              lookup_member, we must enter the scope here.  */
14770           if (dependent_p)
14771             pushed_scope = push_scope (parser->scope);
14772           /* If the PARSER->SCOPE is a template specialization, it
14773              may be instantiated during name lookup.  In that case,
14774              errors may be issued.  Even if we rollback the current
14775              tentative parse, those errors are valid.  */
14776           decl = lookup_qualified_name (parser->scope, name,
14777                                         tag_type != none_type,
14778                                         /*complain=*/true);
14779           if (pushed_scope)
14780             pop_scope (pushed_scope);
14781         }
14782       parser->qualifying_scope = parser->scope;
14783       parser->object_scope = NULL_TREE;
14784     }
14785   else if (object_type)
14786     {
14787       tree object_decl = NULL_TREE;
14788       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14789          OBJECT_TYPE is not a class.  */
14790       if (CLASS_TYPE_P (object_type))
14791         /* If the OBJECT_TYPE is a template specialization, it may
14792            be instantiated during name lookup.  In that case, errors
14793            may be issued.  Even if we rollback the current tentative
14794            parse, those errors are valid.  */
14795         object_decl = lookup_member (object_type,
14796                                      name,
14797                                      /*protect=*/0,
14798                                      tag_type != none_type);
14799       /* Look it up in the enclosing context, too.  */
14800       decl = lookup_name_real (name, tag_type != none_type,
14801                                /*nonclass=*/0,
14802                                /*block_p=*/true, is_namespace, flags);
14803       parser->object_scope = object_type;
14804       parser->qualifying_scope = NULL_TREE;
14805       if (object_decl)
14806         decl = object_decl;
14807     }
14808   else
14809     {
14810       decl = lookup_name_real (name, tag_type != none_type,
14811                                /*nonclass=*/0,
14812                                /*block_p=*/true, is_namespace, flags);
14813       parser->qualifying_scope = NULL_TREE;
14814       parser->object_scope = NULL_TREE;
14815     }
14816
14817   /* If the lookup failed, let our caller know.  */
14818   if (!decl || decl == error_mark_node)
14819     return error_mark_node;
14820
14821   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14822   if (TREE_CODE (decl) == TREE_LIST)
14823     {
14824       if (ambiguous_decls)
14825         *ambiguous_decls = decl;
14826       /* The error message we have to print is too complicated for
14827          cp_parser_error, so we incorporate its actions directly.  */
14828       if (!cp_parser_simulate_error (parser))
14829         {
14830           error ("reference to %qD is ambiguous", name);
14831           print_candidates (decl);
14832         }
14833       return error_mark_node;
14834     }
14835
14836   gcc_assert (DECL_P (decl)
14837               || TREE_CODE (decl) == OVERLOAD
14838               || TREE_CODE (decl) == SCOPE_REF
14839               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14840               || BASELINK_P (decl));
14841
14842   /* If we have resolved the name of a member declaration, check to
14843      see if the declaration is accessible.  When the name resolves to
14844      set of overloaded functions, accessibility is checked when
14845      overload resolution is done.
14846
14847      During an explicit instantiation, access is not checked at all,
14848      as per [temp.explicit].  */
14849   if (DECL_P (decl))
14850     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14851
14852   return decl;
14853 }
14854
14855 /* Like cp_parser_lookup_name, but for use in the typical case where
14856    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14857    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14858
14859 static tree
14860 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14861 {
14862   return cp_parser_lookup_name (parser, name,
14863                                 none_type,
14864                                 /*is_template=*/false,
14865                                 /*is_namespace=*/false,
14866                                 /*check_dependency=*/true,
14867                                 /*ambiguous_decls=*/NULL);
14868 }
14869
14870 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14871    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14872    true, the DECL indicates the class being defined in a class-head,
14873    or declared in an elaborated-type-specifier.
14874
14875    Otherwise, return DECL.  */
14876
14877 static tree
14878 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14879 {
14880   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14881      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14882
14883        struct A {
14884          template <typename T> struct B;
14885        };
14886
14887        template <typename T> struct A::B {};
14888
14889      Similarly, in an elaborated-type-specifier:
14890
14891        namespace N { struct X{}; }
14892
14893        struct A {
14894          template <typename T> friend struct N::X;
14895        };
14896
14897      However, if the DECL refers to a class type, and we are in
14898      the scope of the class, then the name lookup automatically
14899      finds the TYPE_DECL created by build_self_reference rather
14900      than a TEMPLATE_DECL.  For example, in:
14901
14902        template <class T> struct S {
14903          S s;
14904        };
14905
14906      there is no need to handle such case.  */
14907
14908   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14909     return DECL_TEMPLATE_RESULT (decl);
14910
14911   return decl;
14912 }
14913
14914 /* If too many, or too few, template-parameter lists apply to the
14915    declarator, issue an error message.  Returns TRUE if all went well,
14916    and FALSE otherwise.  */
14917
14918 static bool
14919 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14920                                                 cp_declarator *declarator)
14921 {
14922   unsigned num_templates;
14923
14924   /* We haven't seen any classes that involve template parameters yet.  */
14925   num_templates = 0;
14926
14927   switch (declarator->kind)
14928     {
14929     case cdk_id:
14930       if (declarator->u.id.qualifying_scope)
14931         {
14932           tree scope;
14933           tree member;
14934
14935           scope = declarator->u.id.qualifying_scope;
14936           member = declarator->u.id.unqualified_name;
14937
14938           while (scope && CLASS_TYPE_P (scope))
14939             {
14940               /* You're supposed to have one `template <...>'
14941                  for every template class, but you don't need one
14942                  for a full specialization.  For example:
14943
14944                  template <class T> struct S{};
14945                  template <> struct S<int> { void f(); };
14946                  void S<int>::f () {}
14947
14948                  is correct; there shouldn't be a `template <>' for
14949                  the definition of `S<int>::f'.  */
14950               if (CLASSTYPE_TEMPLATE_INFO (scope)
14951                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14952                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14953                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14954                 ++num_templates;
14955
14956               scope = TYPE_CONTEXT (scope);
14957             }
14958         }
14959       else if (TREE_CODE (declarator->u.id.unqualified_name)
14960                == TEMPLATE_ID_EXPR)
14961         /* If the DECLARATOR has the form `X<y>' then it uses one
14962            additional level of template parameters.  */
14963         ++num_templates;
14964
14965       return cp_parser_check_template_parameters (parser,
14966                                                   num_templates);
14967
14968     case cdk_function:
14969     case cdk_array:
14970     case cdk_pointer:
14971     case cdk_reference:
14972     case cdk_ptrmem:
14973       return (cp_parser_check_declarator_template_parameters
14974               (parser, declarator->declarator));
14975
14976     case cdk_error:
14977       return true;
14978
14979     default:
14980       gcc_unreachable ();
14981     }
14982   return false;
14983 }
14984
14985 /* NUM_TEMPLATES were used in the current declaration.  If that is
14986    invalid, return FALSE and issue an error messages.  Otherwise,
14987    return TRUE.  */
14988
14989 static bool
14990 cp_parser_check_template_parameters (cp_parser* parser,
14991                                      unsigned num_templates)
14992 {
14993   /* If there are more template classes than parameter lists, we have
14994      something like:
14995
14996        template <class T> void S<T>::R<T>::f ();  */
14997   if (parser->num_template_parameter_lists < num_templates)
14998     {
14999       error ("too few template-parameter-lists");
15000       return false;
15001     }
15002   /* If there are the same number of template classes and parameter
15003      lists, that's OK.  */
15004   if (parser->num_template_parameter_lists == num_templates)
15005     return true;
15006   /* If there are more, but only one more, then we are referring to a
15007      member template.  That's OK too.  */
15008   if (parser->num_template_parameter_lists == num_templates + 1)
15009       return true;
15010   /* Otherwise, there are too many template parameter lists.  We have
15011      something like:
15012
15013      template <class T> template <class U> void S::f();  */
15014   error ("too many template-parameter-lists");
15015   return false;
15016 }
15017
15018 /* Parse an optional `::' token indicating that the following name is
15019    from the global namespace.  If so, PARSER->SCOPE is set to the
15020    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15021    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15022    Returns the new value of PARSER->SCOPE, if the `::' token is
15023    present, and NULL_TREE otherwise.  */
15024
15025 static tree
15026 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15027 {
15028   cp_token *token;
15029
15030   /* Peek at the next token.  */
15031   token = cp_lexer_peek_token (parser->lexer);
15032   /* If we're looking at a `::' token then we're starting from the
15033      global namespace, not our current location.  */
15034   if (token->type == CPP_SCOPE)
15035     {
15036       /* Consume the `::' token.  */
15037       cp_lexer_consume_token (parser->lexer);
15038       /* Set the SCOPE so that we know where to start the lookup.  */
15039       parser->scope = global_namespace;
15040       parser->qualifying_scope = global_namespace;
15041       parser->object_scope = NULL_TREE;
15042
15043       return parser->scope;
15044     }
15045   else if (!current_scope_valid_p)
15046     {
15047       parser->scope = NULL_TREE;
15048       parser->qualifying_scope = NULL_TREE;
15049       parser->object_scope = NULL_TREE;
15050     }
15051
15052   return NULL_TREE;
15053 }
15054
15055 /* Returns TRUE if the upcoming token sequence is the start of a
15056    constructor declarator.  If FRIEND_P is true, the declarator is
15057    preceded by the `friend' specifier.  */
15058
15059 static bool
15060 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15061 {
15062   bool constructor_p;
15063   tree type_decl = NULL_TREE;
15064   bool nested_name_p;
15065   cp_token *next_token;
15066
15067   /* The common case is that this is not a constructor declarator, so
15068      try to avoid doing lots of work if at all possible.  It's not
15069      valid declare a constructor at function scope.  */
15070   if (at_function_scope_p ())
15071     return false;
15072   /* And only certain tokens can begin a constructor declarator.  */
15073   next_token = cp_lexer_peek_token (parser->lexer);
15074   if (next_token->type != CPP_NAME
15075       && next_token->type != CPP_SCOPE
15076       && next_token->type != CPP_NESTED_NAME_SPECIFIER
15077       && next_token->type != CPP_TEMPLATE_ID)
15078     return false;
15079
15080   /* Parse tentatively; we are going to roll back all of the tokens
15081      consumed here.  */
15082   cp_parser_parse_tentatively (parser);
15083   /* Assume that we are looking at a constructor declarator.  */
15084   constructor_p = true;
15085
15086   /* Look for the optional `::' operator.  */
15087   cp_parser_global_scope_opt (parser,
15088                               /*current_scope_valid_p=*/false);
15089   /* Look for the nested-name-specifier.  */
15090   nested_name_p
15091     = (cp_parser_nested_name_specifier_opt (parser,
15092                                             /*typename_keyword_p=*/false,
15093                                             /*check_dependency_p=*/false,
15094                                             /*type_p=*/false,
15095                                             /*is_declaration=*/false)
15096        != NULL_TREE);
15097   /* Outside of a class-specifier, there must be a
15098      nested-name-specifier.  */
15099   if (!nested_name_p &&
15100       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15101        || friend_p))
15102     constructor_p = false;
15103   /* If we still think that this might be a constructor-declarator,
15104      look for a class-name.  */
15105   if (constructor_p)
15106     {
15107       /* If we have:
15108
15109            template <typename T> struct S { S(); };
15110            template <typename T> S<T>::S ();
15111
15112          we must recognize that the nested `S' names a class.
15113          Similarly, for:
15114
15115            template <typename T> S<T>::S<T> ();
15116
15117          we must recognize that the nested `S' names a template.  */
15118       type_decl = cp_parser_class_name (parser,
15119                                         /*typename_keyword_p=*/false,
15120                                         /*template_keyword_p=*/false,
15121                                         none_type,
15122                                         /*check_dependency_p=*/false,
15123                                         /*class_head_p=*/false,
15124                                         /*is_declaration=*/false);
15125       /* If there was no class-name, then this is not a constructor.  */
15126       constructor_p = !cp_parser_error_occurred (parser);
15127     }
15128
15129   /* If we're still considering a constructor, we have to see a `(',
15130      to begin the parameter-declaration-clause, followed by either a
15131      `)', an `...', or a decl-specifier.  We need to check for a
15132      type-specifier to avoid being fooled into thinking that:
15133
15134        S::S (f) (int);
15135
15136      is a constructor.  (It is actually a function named `f' that
15137      takes one parameter (of type `int') and returns a value of type
15138      `S::S'.  */
15139   if (constructor_p
15140       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15141     {
15142       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15143           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15144           /* A parameter declaration begins with a decl-specifier,
15145              which is either the "attribute" keyword, a storage class
15146              specifier, or (usually) a type-specifier.  */
15147           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15148           && !cp_parser_storage_class_specifier_opt (parser))
15149         {
15150           tree type;
15151           tree pushed_scope = NULL_TREE;
15152           unsigned saved_num_template_parameter_lists;
15153
15154           /* Names appearing in the type-specifier should be looked up
15155              in the scope of the class.  */
15156           if (current_class_type)
15157             type = NULL_TREE;
15158           else
15159             {
15160               type = TREE_TYPE (type_decl);
15161               if (TREE_CODE (type) == TYPENAME_TYPE)
15162                 {
15163                   type = resolve_typename_type (type,
15164                                                 /*only_current_p=*/false);
15165                   if (type == error_mark_node)
15166                     {
15167                       cp_parser_abort_tentative_parse (parser);
15168                       return false;
15169                     }
15170                 }
15171               pushed_scope = push_scope (type);
15172             }
15173
15174           /* Inside the constructor parameter list, surrounding
15175              template-parameter-lists do not apply.  */
15176           saved_num_template_parameter_lists
15177             = parser->num_template_parameter_lists;
15178           parser->num_template_parameter_lists = 0;
15179
15180           /* Look for the type-specifier.  */
15181           cp_parser_type_specifier (parser,
15182                                     CP_PARSER_FLAGS_NONE,
15183                                     /*decl_specs=*/NULL,
15184                                     /*is_declarator=*/true,
15185                                     /*declares_class_or_enum=*/NULL,
15186                                     /*is_cv_qualifier=*/NULL);
15187
15188           parser->num_template_parameter_lists
15189             = saved_num_template_parameter_lists;
15190
15191           /* Leave the scope of the class.  */
15192           if (pushed_scope)
15193             pop_scope (pushed_scope);
15194
15195           constructor_p = !cp_parser_error_occurred (parser);
15196         }
15197     }
15198   else
15199     constructor_p = false;
15200   /* We did not really want to consume any tokens.  */
15201   cp_parser_abort_tentative_parse (parser);
15202
15203   return constructor_p;
15204 }
15205
15206 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15207    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15208    they must be performed once we are in the scope of the function.
15209
15210    Returns the function defined.  */
15211
15212 static tree
15213 cp_parser_function_definition_from_specifiers_and_declarator
15214   (cp_parser* parser,
15215    cp_decl_specifier_seq *decl_specifiers,
15216    tree attributes,
15217    const cp_declarator *declarator)
15218 {
15219   tree fn;
15220   bool success_p;
15221
15222   /* Begin the function-definition.  */
15223   success_p = start_function (decl_specifiers, declarator, attributes);
15224
15225   /* The things we're about to see are not directly qualified by any
15226      template headers we've seen thus far.  */
15227   reset_specialization ();
15228
15229   /* If there were names looked up in the decl-specifier-seq that we
15230      did not check, check them now.  We must wait until we are in the
15231      scope of the function to perform the checks, since the function
15232      might be a friend.  */
15233   perform_deferred_access_checks ();
15234
15235   if (!success_p)
15236     {
15237       /* Skip the entire function.  */
15238       error ("invalid function declaration");
15239       cp_parser_skip_to_end_of_block_or_statement (parser);
15240       fn = error_mark_node;
15241     }
15242   else
15243     fn = cp_parser_function_definition_after_declarator (parser,
15244                                                          /*inline_p=*/false);
15245
15246   return fn;
15247 }
15248
15249 /* Parse the part of a function-definition that follows the
15250    declarator.  INLINE_P is TRUE iff this function is an inline
15251    function defined with a class-specifier.
15252
15253    Returns the function defined.  */
15254
15255 static tree
15256 cp_parser_function_definition_after_declarator (cp_parser* parser,
15257                                                 bool inline_p)
15258 {
15259   tree fn;
15260   bool ctor_initializer_p = false;
15261   bool saved_in_unbraced_linkage_specification_p;
15262   unsigned saved_num_template_parameter_lists;
15263
15264   /* If the next token is `return', then the code may be trying to
15265      make use of the "named return value" extension that G++ used to
15266      support.  */
15267   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15268     {
15269       /* Consume the `return' keyword.  */
15270       cp_lexer_consume_token (parser->lexer);
15271       /* Look for the identifier that indicates what value is to be
15272          returned.  */
15273       cp_parser_identifier (parser);
15274       /* Issue an error message.  */
15275       error ("named return values are no longer supported");
15276       /* Skip tokens until we reach the start of the function body.  */
15277       while (true)
15278         {
15279           cp_token *token = cp_lexer_peek_token (parser->lexer);
15280           if (token->type == CPP_OPEN_BRACE
15281               || token->type == CPP_EOF
15282               || token->type == CPP_PRAGMA_EOL)
15283             break;
15284           cp_lexer_consume_token (parser->lexer);
15285         }
15286     }
15287   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15288      anything declared inside `f'.  */
15289   saved_in_unbraced_linkage_specification_p
15290     = parser->in_unbraced_linkage_specification_p;
15291   parser->in_unbraced_linkage_specification_p = false;
15292   /* Inside the function, surrounding template-parameter-lists do not
15293      apply.  */
15294   saved_num_template_parameter_lists
15295     = parser->num_template_parameter_lists;
15296   parser->num_template_parameter_lists = 0;
15297   /* If the next token is `try', then we are looking at a
15298      function-try-block.  */
15299   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15300     ctor_initializer_p = cp_parser_function_try_block (parser);
15301   /* A function-try-block includes the function-body, so we only do
15302      this next part if we're not processing a function-try-block.  */
15303   else
15304     ctor_initializer_p
15305       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15306
15307   /* Finish the function.  */
15308   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15309                         (inline_p ? 2 : 0));
15310   /* Generate code for it, if necessary.  */
15311   expand_or_defer_fn (fn);
15312   /* Restore the saved values.  */
15313   parser->in_unbraced_linkage_specification_p
15314     = saved_in_unbraced_linkage_specification_p;
15315   parser->num_template_parameter_lists
15316     = saved_num_template_parameter_lists;
15317
15318   return fn;
15319 }
15320
15321 /* Parse a template-declaration, assuming that the `export' (and
15322    `extern') keywords, if present, has already been scanned.  MEMBER_P
15323    is as for cp_parser_template_declaration.  */
15324
15325 static void
15326 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15327 {
15328   tree decl = NULL_TREE;
15329   tree parameter_list;
15330   bool friend_p = false;
15331   bool need_lang_pop;
15332
15333   /* Look for the `template' keyword.  */
15334   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15335     return;
15336
15337   /* And the `<'.  */
15338   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15339     return;
15340   /* [temp]
15341    
15342      A template ... shall not have C linkage.  */
15343   if (current_lang_name == lang_name_c)
15344     {
15345       error ("template with C linkage");
15346       /* Give it C++ linkage to avoid confusing other parts of the
15347          front end.  */
15348       push_lang_context (lang_name_cplusplus);
15349       need_lang_pop = true;
15350     }
15351   else
15352     need_lang_pop = false;
15353   /* If the next token is `>', then we have an invalid
15354      specialization.  Rather than complain about an invalid template
15355      parameter, issue an error message here.  */
15356   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15357     {
15358       cp_parser_error (parser, "invalid explicit specialization");
15359       begin_specialization ();
15360       parameter_list = NULL_TREE;
15361     }
15362   else
15363     /* Parse the template parameters.  */
15364     parameter_list = cp_parser_template_parameter_list (parser);
15365
15366   /* Look for the `>'.  */
15367   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15368   /* We just processed one more parameter list.  */
15369   ++parser->num_template_parameter_lists;
15370   /* If the next token is `template', there are more template
15371      parameters.  */
15372   if (cp_lexer_next_token_is_keyword (parser->lexer,
15373                                       RID_TEMPLATE))
15374     cp_parser_template_declaration_after_export (parser, member_p);
15375   else
15376     {
15377       /* There are no access checks when parsing a template, as we do not
15378          know if a specialization will be a friend.  */
15379       push_deferring_access_checks (dk_no_check);
15380
15381       decl = cp_parser_single_declaration (parser,
15382                                            member_p,
15383                                            &friend_p);
15384
15385       pop_deferring_access_checks ();
15386
15387       /* If this is a member template declaration, let the front
15388          end know.  */
15389       if (member_p && !friend_p && decl)
15390         {
15391           if (TREE_CODE (decl) == TYPE_DECL)
15392             cp_parser_check_access_in_redeclaration (decl);
15393
15394           decl = finish_member_template_decl (decl);
15395         }
15396       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15397         make_friend_class (current_class_type, TREE_TYPE (decl),
15398                            /*complain=*/true);
15399     }
15400   /* We are done with the current parameter list.  */
15401   --parser->num_template_parameter_lists;
15402
15403   /* Finish up.  */
15404   finish_template_decl (parameter_list);
15405
15406   /* Register member declarations.  */
15407   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15408     finish_member_declaration (decl);
15409   /* For the erroneous case of a template with C linkage, we pushed an
15410      implicit C++ linkage scope; exit that scope now.  */
15411   if (need_lang_pop)
15412     pop_lang_context ();
15413   /* If DECL is a function template, we must return to parse it later.
15414      (Even though there is no definition, there might be default
15415      arguments that need handling.)  */
15416   if (member_p && decl
15417       && (TREE_CODE (decl) == FUNCTION_DECL
15418           || DECL_FUNCTION_TEMPLATE_P (decl)))
15419     TREE_VALUE (parser->unparsed_functions_queues)
15420       = tree_cons (NULL_TREE, decl,
15421                    TREE_VALUE (parser->unparsed_functions_queues));
15422 }
15423
15424 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15425    `function-definition' sequence.  MEMBER_P is true, this declaration
15426    appears in a class scope.
15427
15428    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15429    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15430
15431 static tree
15432 cp_parser_single_declaration (cp_parser* parser,
15433                               bool member_p,
15434                               bool* friend_p)
15435 {
15436   int declares_class_or_enum;
15437   tree decl = NULL_TREE;
15438   cp_decl_specifier_seq decl_specifiers;
15439   bool function_definition_p = false;
15440
15441   /* This function is only used when processing a template
15442      declaration.  */
15443   gcc_assert (innermost_scope_kind () == sk_template_parms
15444               || innermost_scope_kind () == sk_template_spec);
15445
15446   /* Defer access checks until we know what is being declared.  */
15447   push_deferring_access_checks (dk_deferred);
15448
15449   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15450      alternative.  */
15451   cp_parser_decl_specifier_seq (parser,
15452                                 CP_PARSER_FLAGS_OPTIONAL,
15453                                 &decl_specifiers,
15454                                 &declares_class_or_enum);
15455   if (friend_p)
15456     *friend_p = cp_parser_friend_p (&decl_specifiers);
15457
15458   /* There are no template typedefs.  */
15459   if (decl_specifiers.specs[(int) ds_typedef])
15460     {
15461       error ("template declaration of %qs", "typedef");
15462       decl = error_mark_node;
15463     }
15464
15465   /* Gather up the access checks that occurred the
15466      decl-specifier-seq.  */
15467   stop_deferring_access_checks ();
15468
15469   /* Check for the declaration of a template class.  */
15470   if (declares_class_or_enum)
15471     {
15472       if (cp_parser_declares_only_class_p (parser))
15473         {
15474           decl = shadow_tag (&decl_specifiers);
15475
15476           /* In this case:
15477
15478                struct C {
15479                  friend template <typename T> struct A<T>::B;
15480                };
15481
15482              A<T>::B will be represented by a TYPENAME_TYPE, and
15483              therefore not recognized by shadow_tag.  */
15484           if (friend_p && *friend_p
15485               && !decl
15486               && decl_specifiers.type
15487               && TYPE_P (decl_specifiers.type))
15488             decl = decl_specifiers.type;
15489
15490           if (decl && decl != error_mark_node)
15491             decl = TYPE_NAME (decl);
15492           else
15493             decl = error_mark_node;
15494         }
15495     }
15496   /* If it's not a template class, try for a template function.  If
15497      the next token is a `;', then this declaration does not declare
15498      anything.  But, if there were errors in the decl-specifiers, then
15499      the error might well have come from an attempted class-specifier.
15500      In that case, there's no need to warn about a missing declarator.  */
15501   if (!decl
15502       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15503           || decl_specifiers.type != error_mark_node))
15504     decl = cp_parser_init_declarator (parser,
15505                                       &decl_specifiers,
15506                                       /*function_definition_allowed_p=*/true,
15507                                       member_p,
15508                                       declares_class_or_enum,
15509                                       &function_definition_p);
15510
15511   pop_deferring_access_checks ();
15512
15513   /* Clear any current qualification; whatever comes next is the start
15514      of something new.  */
15515   parser->scope = NULL_TREE;
15516   parser->qualifying_scope = NULL_TREE;
15517   parser->object_scope = NULL_TREE;
15518   /* Look for a trailing `;' after the declaration.  */
15519   if (!function_definition_p
15520       && (decl == error_mark_node
15521           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15522     cp_parser_skip_to_end_of_block_or_statement (parser);
15523
15524   return decl;
15525 }
15526
15527 /* Parse a cast-expression that is not the operand of a unary "&".  */
15528
15529 static tree
15530 cp_parser_simple_cast_expression (cp_parser *parser)
15531 {
15532   return cp_parser_cast_expression (parser, /*address_p=*/false,
15533                                     /*cast_p=*/false);
15534 }
15535
15536 /* Parse a functional cast to TYPE.  Returns an expression
15537    representing the cast.  */
15538
15539 static tree
15540 cp_parser_functional_cast (cp_parser* parser, tree type)
15541 {
15542   tree expression_list;
15543   tree cast;
15544
15545   expression_list
15546     = cp_parser_parenthesized_expression_list (parser, false,
15547                                                /*cast_p=*/true,
15548                                                /*non_constant_p=*/NULL);
15549
15550   cast = build_functional_cast (type, expression_list);
15551   /* [expr.const]/1: In an integral constant expression "only type
15552      conversions to integral or enumeration type can be used".  */
15553   if (TREE_CODE (type) == TYPE_DECL)
15554     type = TREE_TYPE (type);
15555   if (cast != error_mark_node && !dependent_type_p (type)
15556       && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15557     {
15558       if (cp_parser_non_integral_constant_expression
15559           (parser, "a call to a constructor"))
15560         return error_mark_node;
15561     }
15562   return cast;
15563 }
15564
15565 /* Save the tokens that make up the body of a member function defined
15566    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15567    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15568    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15569    for the member function.  */
15570
15571 static tree
15572 cp_parser_save_member_function_body (cp_parser* parser,
15573                                      cp_decl_specifier_seq *decl_specifiers,
15574                                      cp_declarator *declarator,
15575                                      tree attributes)
15576 {
15577   cp_token *first;
15578   cp_token *last;
15579   tree fn;
15580
15581   /* Create the function-declaration.  */
15582   fn = start_method (decl_specifiers, declarator, attributes);
15583   /* If something went badly wrong, bail out now.  */
15584   if (fn == error_mark_node)
15585     {
15586       /* If there's a function-body, skip it.  */
15587       if (cp_parser_token_starts_function_definition_p
15588           (cp_lexer_peek_token (parser->lexer)))
15589         cp_parser_skip_to_end_of_block_or_statement (parser);
15590       return error_mark_node;
15591     }
15592
15593   /* Remember it, if there default args to post process.  */
15594   cp_parser_save_default_args (parser, fn);
15595
15596   /* Save away the tokens that make up the body of the
15597      function.  */
15598   first = parser->lexer->next_token;
15599   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15600   /* Handle function try blocks.  */
15601   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15602     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15603   last = parser->lexer->next_token;
15604
15605   /* Save away the inline definition; we will process it when the
15606      class is complete.  */
15607   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15608   DECL_PENDING_INLINE_P (fn) = 1;
15609
15610   /* We need to know that this was defined in the class, so that
15611      friend templates are handled correctly.  */
15612   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15613
15614   /* We're done with the inline definition.  */
15615   finish_method (fn);
15616
15617   /* Add FN to the queue of functions to be parsed later.  */
15618   TREE_VALUE (parser->unparsed_functions_queues)
15619     = tree_cons (NULL_TREE, fn,
15620                  TREE_VALUE (parser->unparsed_functions_queues));
15621
15622   return fn;
15623 }
15624
15625 /* Parse a template-argument-list, as well as the trailing ">" (but
15626    not the opening ">").  See cp_parser_template_argument_list for the
15627    return value.  */
15628
15629 static tree
15630 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15631 {
15632   tree arguments;
15633   tree saved_scope;
15634   tree saved_qualifying_scope;
15635   tree saved_object_scope;
15636   bool saved_greater_than_is_operator_p;
15637   bool saved_skip_evaluation;
15638
15639   /* [temp.names]
15640
15641      When parsing a template-id, the first non-nested `>' is taken as
15642      the end of the template-argument-list rather than a greater-than
15643      operator.  */
15644   saved_greater_than_is_operator_p
15645     = parser->greater_than_is_operator_p;
15646   parser->greater_than_is_operator_p = false;
15647   /* Parsing the argument list may modify SCOPE, so we save it
15648      here.  */
15649   saved_scope = parser->scope;
15650   saved_qualifying_scope = parser->qualifying_scope;
15651   saved_object_scope = parser->object_scope;
15652   /* We need to evaluate the template arguments, even though this
15653      template-id may be nested within a "sizeof".  */
15654   saved_skip_evaluation = skip_evaluation;
15655   skip_evaluation = false;
15656   /* Parse the template-argument-list itself.  */
15657   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15658     arguments = NULL_TREE;
15659   else
15660     arguments = cp_parser_template_argument_list (parser);
15661   /* Look for the `>' that ends the template-argument-list. If we find
15662      a '>>' instead, it's probably just a typo.  */
15663   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15664     {
15665       if (!saved_greater_than_is_operator_p)
15666         {
15667           /* If we're in a nested template argument list, the '>>' has
15668             to be a typo for '> >'. We emit the error message, but we
15669             continue parsing and we push a '>' as next token, so that
15670             the argument list will be parsed correctly.  Note that the
15671             global source location is still on the token before the
15672             '>>', so we need to say explicitly where we want it.  */
15673           cp_token *token = cp_lexer_peek_token (parser->lexer);
15674           error ("%H%<>>%> should be %<> >%> "
15675                  "within a nested template argument list",
15676                  &token->location);
15677
15678           /* ??? Proper recovery should terminate two levels of
15679              template argument list here.  */
15680           token->type = CPP_GREATER;
15681         }
15682       else
15683         {
15684           /* If this is not a nested template argument list, the '>>'
15685             is a typo for '>'. Emit an error message and continue.
15686             Same deal about the token location, but here we can get it
15687             right by consuming the '>>' before issuing the diagnostic.  */
15688           cp_lexer_consume_token (parser->lexer);
15689           error ("spurious %<>>%>, use %<>%> to terminate "
15690                  "a template argument list");
15691         }
15692     }
15693   else
15694     cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15695   /* The `>' token might be a greater-than operator again now.  */
15696   parser->greater_than_is_operator_p
15697     = saved_greater_than_is_operator_p;
15698   /* Restore the SAVED_SCOPE.  */
15699   parser->scope = saved_scope;
15700   parser->qualifying_scope = saved_qualifying_scope;
15701   parser->object_scope = saved_object_scope;
15702   skip_evaluation = saved_skip_evaluation;
15703
15704   return arguments;
15705 }
15706
15707 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15708    arguments, or the body of the function have not yet been parsed,
15709    parse them now.  */
15710
15711 static void
15712 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15713 {
15714   /* If this member is a template, get the underlying
15715      FUNCTION_DECL.  */
15716   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15717     member_function = DECL_TEMPLATE_RESULT (member_function);
15718
15719   /* There should not be any class definitions in progress at this
15720      point; the bodies of members are only parsed outside of all class
15721      definitions.  */
15722   gcc_assert (parser->num_classes_being_defined == 0);
15723   /* While we're parsing the member functions we might encounter more
15724      classes.  We want to handle them right away, but we don't want
15725      them getting mixed up with functions that are currently in the
15726      queue.  */
15727   parser->unparsed_functions_queues
15728     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15729
15730   /* Make sure that any template parameters are in scope.  */
15731   maybe_begin_member_template_processing (member_function);
15732
15733   /* If the body of the function has not yet been parsed, parse it
15734      now.  */
15735   if (DECL_PENDING_INLINE_P (member_function))
15736     {
15737       tree function_scope;
15738       cp_token_cache *tokens;
15739
15740       /* The function is no longer pending; we are processing it.  */
15741       tokens = DECL_PENDING_INLINE_INFO (member_function);
15742       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15743       DECL_PENDING_INLINE_P (member_function) = 0;
15744
15745       /* If this is a local class, enter the scope of the containing
15746          function.  */
15747       function_scope = current_function_decl;
15748       if (function_scope)
15749         push_function_context_to (function_scope);
15750
15751
15752       /* Push the body of the function onto the lexer stack.  */
15753       cp_parser_push_lexer_for_tokens (parser, tokens);
15754
15755       /* Let the front end know that we going to be defining this
15756          function.  */
15757       start_preparsed_function (member_function, NULL_TREE,
15758                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15759
15760       /* Don't do access checking if it is a templated function.  */
15761       if (processing_template_decl)
15762         push_deferring_access_checks (dk_no_check);
15763
15764       /* Now, parse the body of the function.  */
15765       cp_parser_function_definition_after_declarator (parser,
15766                                                       /*inline_p=*/true);
15767
15768       if (processing_template_decl)
15769         pop_deferring_access_checks ();
15770
15771       /* Leave the scope of the containing function.  */
15772       if (function_scope)
15773         pop_function_context_from (function_scope);
15774       cp_parser_pop_lexer (parser);
15775     }
15776
15777   /* Remove any template parameters from the symbol table.  */
15778   maybe_end_member_template_processing ();
15779
15780   /* Restore the queue.  */
15781   parser->unparsed_functions_queues
15782     = TREE_CHAIN (parser->unparsed_functions_queues);
15783 }
15784
15785 /* If DECL contains any default args, remember it on the unparsed
15786    functions queue.  */
15787
15788 static void
15789 cp_parser_save_default_args (cp_parser* parser, tree decl)
15790 {
15791   tree probe;
15792
15793   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15794        probe;
15795        probe = TREE_CHAIN (probe))
15796     if (TREE_PURPOSE (probe))
15797       {
15798         TREE_PURPOSE (parser->unparsed_functions_queues)
15799           = tree_cons (current_class_type, decl,
15800                        TREE_PURPOSE (parser->unparsed_functions_queues));
15801         break;
15802       }
15803 }
15804
15805 /* FN is a FUNCTION_DECL which may contains a parameter with an
15806    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15807    assumes that the current scope is the scope in which the default
15808    argument should be processed.  */
15809
15810 static void
15811 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15812 {
15813   bool saved_local_variables_forbidden_p;
15814   tree parm;
15815
15816   /* While we're parsing the default args, we might (due to the
15817      statement expression extension) encounter more classes.  We want
15818      to handle them right away, but we don't want them getting mixed
15819      up with default args that are currently in the queue.  */
15820   parser->unparsed_functions_queues
15821     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15822
15823   /* Local variable names (and the `this' keyword) may not appear
15824      in a default argument.  */
15825   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15826   parser->local_variables_forbidden_p = true;
15827
15828   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15829        parm;
15830        parm = TREE_CHAIN (parm))
15831     {
15832       cp_token_cache *tokens;
15833       tree default_arg = TREE_PURPOSE (parm);
15834       tree parsed_arg;
15835       VEC(tree,gc) *insts;
15836       tree copy;
15837       unsigned ix;
15838
15839       if (!default_arg)
15840         continue;
15841
15842       if (TREE_CODE (default_arg) != DEFAULT_ARG)
15843         /* This can happen for a friend declaration for a function
15844            already declared with default arguments.  */
15845         continue;
15846
15847        /* Push the saved tokens for the default argument onto the parser's
15848           lexer stack.  */
15849       tokens = DEFARG_TOKENS (default_arg);
15850       cp_parser_push_lexer_for_tokens (parser, tokens);
15851
15852       /* Parse the assignment-expression.  */
15853       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15854
15855       if (!processing_template_decl)
15856         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
15857       
15858       TREE_PURPOSE (parm) = parsed_arg;
15859
15860       /* Update any instantiations we've already created.  */
15861       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15862            VEC_iterate (tree, insts, ix, copy); ix++)
15863         TREE_PURPOSE (copy) = parsed_arg;
15864
15865       /* If the token stream has not been completely used up, then
15866          there was extra junk after the end of the default
15867          argument.  */
15868       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15869         cp_parser_error (parser, "expected %<,%>");
15870
15871       /* Revert to the main lexer.  */
15872       cp_parser_pop_lexer (parser);
15873     }
15874
15875   /* Make sure no default arg is missing.  */
15876   check_default_args (fn);
15877
15878   /* Restore the state of local_variables_forbidden_p.  */
15879   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15880
15881   /* Restore the queue.  */
15882   parser->unparsed_functions_queues
15883     = TREE_CHAIN (parser->unparsed_functions_queues);
15884 }
15885
15886 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15887    either a TYPE or an expression, depending on the form of the
15888    input.  The KEYWORD indicates which kind of expression we have
15889    encountered.  */
15890
15891 static tree
15892 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15893 {
15894   static const char *format;
15895   tree expr = NULL_TREE;
15896   const char *saved_message;
15897   bool saved_integral_constant_expression_p;
15898   bool saved_non_integral_constant_expression_p;
15899
15900   /* Initialize FORMAT the first time we get here.  */
15901   if (!format)
15902     format = "types may not be defined in '%s' expressions";
15903
15904   /* Types cannot be defined in a `sizeof' expression.  Save away the
15905      old message.  */
15906   saved_message = parser->type_definition_forbidden_message;
15907   /* And create the new one.  */
15908   parser->type_definition_forbidden_message
15909     = XNEWVEC (const char, strlen (format)
15910                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15911                + 1 /* `\0' */);
15912   sprintf ((char *) parser->type_definition_forbidden_message,
15913            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15914
15915   /* The restrictions on constant-expressions do not apply inside
15916      sizeof expressions.  */
15917   saved_integral_constant_expression_p
15918     = parser->integral_constant_expression_p;
15919   saved_non_integral_constant_expression_p
15920     = parser->non_integral_constant_expression_p;
15921   parser->integral_constant_expression_p = false;
15922
15923   /* Do not actually evaluate the expression.  */
15924   ++skip_evaluation;
15925   /* If it's a `(', then we might be looking at the type-id
15926      construction.  */
15927   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15928     {
15929       tree type;
15930       bool saved_in_type_id_in_expr_p;
15931
15932       /* We can't be sure yet whether we're looking at a type-id or an
15933          expression.  */
15934       cp_parser_parse_tentatively (parser);
15935       /* Consume the `('.  */
15936       cp_lexer_consume_token (parser->lexer);
15937       /* Parse the type-id.  */
15938       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15939       parser->in_type_id_in_expr_p = true;
15940       type = cp_parser_type_id (parser);
15941       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15942       /* Now, look for the trailing `)'.  */
15943       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15944       /* If all went well, then we're done.  */
15945       if (cp_parser_parse_definitely (parser))
15946         {
15947           cp_decl_specifier_seq decl_specs;
15948
15949           /* Build a trivial decl-specifier-seq.  */
15950           clear_decl_specs (&decl_specs);
15951           decl_specs.type = type;
15952
15953           /* Call grokdeclarator to figure out what type this is.  */
15954           expr = grokdeclarator (NULL,
15955                                  &decl_specs,
15956                                  TYPENAME,
15957                                  /*initialized=*/0,
15958                                  /*attrlist=*/NULL);
15959         }
15960     }
15961
15962   /* If the type-id production did not work out, then we must be
15963      looking at the unary-expression production.  */
15964   if (!expr)
15965     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15966                                        /*cast_p=*/false);
15967   /* Go back to evaluating expressions.  */
15968   --skip_evaluation;
15969
15970   /* Free the message we created.  */
15971   free ((char *) parser->type_definition_forbidden_message);
15972   /* And restore the old one.  */
15973   parser->type_definition_forbidden_message = saved_message;
15974   parser->integral_constant_expression_p
15975     = saved_integral_constant_expression_p;
15976   parser->non_integral_constant_expression_p
15977     = saved_non_integral_constant_expression_p;
15978
15979   return expr;
15980 }
15981
15982 /* If the current declaration has no declarator, return true.  */
15983
15984 static bool
15985 cp_parser_declares_only_class_p (cp_parser *parser)
15986 {
15987   /* If the next token is a `;' or a `,' then there is no
15988      declarator.  */
15989   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15990           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15991 }
15992
15993 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15994
15995 static void
15996 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15997                              cp_storage_class storage_class)
15998 {
15999   if (decl_specs->storage_class != sc_none)
16000     decl_specs->multiple_storage_classes_p = true;
16001   else
16002     decl_specs->storage_class = storage_class;
16003 }
16004
16005 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
16006    is true, the type is a user-defined type; otherwise it is a
16007    built-in type specified by a keyword.  */
16008
16009 static void
16010 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16011                               tree type_spec,
16012                               bool user_defined_p)
16013 {
16014   decl_specs->any_specifiers_p = true;
16015
16016   /* If the user tries to redeclare bool or wchar_t (with, for
16017      example, in "typedef int wchar_t;") we remember that this is what
16018      happened.  In system headers, we ignore these declarations so
16019      that G++ can work with system headers that are not C++-safe.  */
16020   if (decl_specs->specs[(int) ds_typedef]
16021       && !user_defined_p
16022       && (type_spec == boolean_type_node
16023           || type_spec == wchar_type_node)
16024       && (decl_specs->type
16025           || decl_specs->specs[(int) ds_long]
16026           || decl_specs->specs[(int) ds_short]
16027           || decl_specs->specs[(int) ds_unsigned]
16028           || decl_specs->specs[(int) ds_signed]))
16029     {
16030       decl_specs->redefined_builtin_type = type_spec;
16031       if (!decl_specs->type)
16032         {
16033           decl_specs->type = type_spec;
16034           decl_specs->user_defined_type_p = false;
16035         }
16036     }
16037   else if (decl_specs->type)
16038     decl_specs->multiple_types_p = true;
16039   else
16040     {
16041       decl_specs->type = type_spec;
16042       decl_specs->user_defined_type_p = user_defined_p;
16043       decl_specs->redefined_builtin_type = NULL_TREE;
16044     }
16045 }
16046
16047 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16048    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
16049
16050 static bool
16051 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16052 {
16053   return decl_specifiers->specs[(int) ds_friend] != 0;
16054 }
16055
16056 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
16057    issue an error message indicating that TOKEN_DESC was expected.
16058
16059    Returns the token consumed, if the token had the appropriate type.
16060    Otherwise, returns NULL.  */
16061
16062 static cp_token *
16063 cp_parser_require (cp_parser* parser,
16064                    enum cpp_ttype type,
16065                    const char* token_desc)
16066 {
16067   if (cp_lexer_next_token_is (parser->lexer, type))
16068     return cp_lexer_consume_token (parser->lexer);
16069   else
16070     {
16071       /* Output the MESSAGE -- unless we're parsing tentatively.  */
16072       if (!cp_parser_simulate_error (parser))
16073         {
16074           char *message = concat ("expected ", token_desc, NULL);
16075           cp_parser_error (parser, message);
16076           free (message);
16077         }
16078       return NULL;
16079     }
16080 }
16081
16082 /* Like cp_parser_require, except that tokens will be skipped until
16083    the desired token is found.  An error message is still produced if
16084    the next token is not as expected.  */
16085
16086 static void
16087 cp_parser_skip_until_found (cp_parser* parser,
16088                             enum cpp_ttype type,
16089                             const char* token_desc)
16090 {
16091   cp_token *token;
16092   unsigned nesting_depth = 0;
16093
16094   if (cp_parser_require (parser, type, token_desc))
16095     return;
16096
16097   /* Skip tokens until the desired token is found.  */
16098   while (true)
16099     {
16100       /* Peek at the next token.  */
16101       token = cp_lexer_peek_token (parser->lexer);
16102
16103       /* If we've reached the token we want, consume it and stop.  */
16104       if (token->type == type && !nesting_depth)
16105         {
16106           cp_lexer_consume_token (parser->lexer);
16107           return;
16108         }
16109
16110       switch (token->type)
16111         {
16112         case CPP_EOF:
16113         case CPP_PRAGMA_EOL:
16114           /* If we've run out of tokens, stop.  */
16115           return;
16116
16117         case CPP_OPEN_BRACE:
16118         case CPP_OPEN_PAREN:
16119         case CPP_OPEN_SQUARE:
16120           ++nesting_depth;
16121           break;
16122
16123         case CPP_CLOSE_BRACE:
16124         case CPP_CLOSE_PAREN:
16125         case CPP_CLOSE_SQUARE:
16126           if (nesting_depth-- == 0)
16127             return;
16128           break;
16129
16130         default:
16131           break;
16132         }
16133
16134       /* Consume this token.  */
16135       cp_lexer_consume_token (parser->lexer);
16136     }
16137 }
16138
16139 /* If the next token is the indicated keyword, consume it.  Otherwise,
16140    issue an error message indicating that TOKEN_DESC was expected.
16141
16142    Returns the token consumed, if the token had the appropriate type.
16143    Otherwise, returns NULL.  */
16144
16145 static cp_token *
16146 cp_parser_require_keyword (cp_parser* parser,
16147                            enum rid keyword,
16148                            const char* token_desc)
16149 {
16150   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16151
16152   if (token && token->keyword != keyword)
16153     {
16154       dyn_string_t error_msg;
16155
16156       /* Format the error message.  */
16157       error_msg = dyn_string_new (0);
16158       dyn_string_append_cstr (error_msg, "expected ");
16159       dyn_string_append_cstr (error_msg, token_desc);
16160       cp_parser_error (parser, error_msg->s);
16161       dyn_string_delete (error_msg);
16162       return NULL;
16163     }
16164
16165   return token;
16166 }
16167
16168 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16169    function-definition.  */
16170
16171 static bool
16172 cp_parser_token_starts_function_definition_p (cp_token* token)
16173 {
16174   return (/* An ordinary function-body begins with an `{'.  */
16175           token->type == CPP_OPEN_BRACE
16176           /* A ctor-initializer begins with a `:'.  */
16177           || token->type == CPP_COLON
16178           /* A function-try-block begins with `try'.  */
16179           || token->keyword == RID_TRY
16180           /* The named return value extension begins with `return'.  */
16181           || token->keyword == RID_RETURN);
16182 }
16183
16184 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16185    definition.  */
16186
16187 static bool
16188 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16189 {
16190   cp_token *token;
16191
16192   token = cp_lexer_peek_token (parser->lexer);
16193   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16194 }
16195
16196 /* Returns TRUE iff the next token is the "," or ">" ending a
16197    template-argument.  */
16198
16199 static bool
16200 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16201 {
16202   cp_token *token;
16203
16204   token = cp_lexer_peek_token (parser->lexer);
16205   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16206 }
16207
16208 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16209    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
16210
16211 static bool
16212 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16213                                                      size_t n)
16214 {
16215   cp_token *token;
16216
16217   token = cp_lexer_peek_nth_token (parser->lexer, n);
16218   if (token->type == CPP_LESS)
16219     return true;
16220   /* Check for the sequence `<::' in the original code. It would be lexed as
16221      `[:', where `[' is a digraph, and there is no whitespace before
16222      `:'.  */
16223   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16224     {
16225       cp_token *token2;
16226       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16227       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16228         return true;
16229     }
16230   return false;
16231 }
16232
16233 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16234    or none_type otherwise.  */
16235
16236 static enum tag_types
16237 cp_parser_token_is_class_key (cp_token* token)
16238 {
16239   switch (token->keyword)
16240     {
16241     case RID_CLASS:
16242       return class_type;
16243     case RID_STRUCT:
16244       return record_type;
16245     case RID_UNION:
16246       return union_type;
16247
16248     default:
16249       return none_type;
16250     }
16251 }
16252
16253 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16254
16255 static void
16256 cp_parser_check_class_key (enum tag_types class_key, tree type)
16257 {
16258   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16259     pedwarn ("%qs tag used in naming %q#T",
16260             class_key == union_type ? "union"
16261              : class_key == record_type ? "struct" : "class",
16262              type);
16263 }
16264
16265 /* Issue an error message if DECL is redeclared with different
16266    access than its original declaration [class.access.spec/3].
16267    This applies to nested classes and nested class templates.
16268    [class.mem/1].  */
16269
16270 static void
16271 cp_parser_check_access_in_redeclaration (tree decl)
16272 {
16273   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16274     return;
16275
16276   if ((TREE_PRIVATE (decl)
16277        != (current_access_specifier == access_private_node))
16278       || (TREE_PROTECTED (decl)
16279           != (current_access_specifier == access_protected_node)))
16280     error ("%qD redeclared with different access", decl);
16281 }
16282
16283 /* Look for the `template' keyword, as a syntactic disambiguator.
16284    Return TRUE iff it is present, in which case it will be
16285    consumed.  */
16286
16287 static bool
16288 cp_parser_optional_template_keyword (cp_parser *parser)
16289 {
16290   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16291     {
16292       /* The `template' keyword can only be used within templates;
16293          outside templates the parser can always figure out what is a
16294          template and what is not.  */
16295       if (!processing_template_decl)
16296         {
16297           error ("%<template%> (as a disambiguator) is only allowed "
16298                  "within templates");
16299           /* If this part of the token stream is rescanned, the same
16300              error message would be generated.  So, we purge the token
16301              from the stream.  */
16302           cp_lexer_purge_token (parser->lexer);
16303           return false;
16304         }
16305       else
16306         {
16307           /* Consume the `template' keyword.  */
16308           cp_lexer_consume_token (parser->lexer);
16309           return true;
16310         }
16311     }
16312
16313   return false;
16314 }
16315
16316 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16317    set PARSER->SCOPE, and perform other related actions.  */
16318
16319 static void
16320 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16321 {
16322   tree value;
16323   tree check;
16324
16325   /* Get the stored value.  */
16326   value = cp_lexer_consume_token (parser->lexer)->value;
16327   /* Perform any access checks that were deferred.  */
16328   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16329     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16330   /* Set the scope from the stored value.  */
16331   parser->scope = TREE_VALUE (value);
16332   parser->qualifying_scope = TREE_TYPE (value);
16333   parser->object_scope = NULL_TREE;
16334 }
16335
16336 /* Consume tokens up through a non-nested END token.  */
16337
16338 static void
16339 cp_parser_cache_group (cp_parser *parser,
16340                        enum cpp_ttype end,
16341                        unsigned depth)
16342 {
16343   while (true)
16344     {
16345       cp_token *token;
16346
16347       /* Abort a parenthesized expression if we encounter a brace.  */
16348       if ((end == CPP_CLOSE_PAREN || depth == 0)
16349           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16350         return;
16351       /* If we've reached the end of the file, stop.  */
16352       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16353           || (end != CPP_PRAGMA_EOL
16354               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16355         return;
16356       /* Consume the next token.  */
16357       token = cp_lexer_consume_token (parser->lexer);
16358       /* See if it starts a new group.  */
16359       if (token->type == CPP_OPEN_BRACE)
16360         {
16361           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16362           if (depth == 0)
16363             return;
16364         }
16365       else if (token->type == CPP_OPEN_PAREN)
16366         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16367       else if (token->type == CPP_PRAGMA)
16368         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16369       else if (token->type == end)
16370         return;
16371     }
16372 }
16373
16374 /* Begin parsing tentatively.  We always save tokens while parsing
16375    tentatively so that if the tentative parsing fails we can restore the
16376    tokens.  */
16377
16378 static void
16379 cp_parser_parse_tentatively (cp_parser* parser)
16380 {
16381   /* Enter a new parsing context.  */
16382   parser->context = cp_parser_context_new (parser->context);
16383   /* Begin saving tokens.  */
16384   cp_lexer_save_tokens (parser->lexer);
16385   /* In order to avoid repetitive access control error messages,
16386      access checks are queued up until we are no longer parsing
16387      tentatively.  */
16388   push_deferring_access_checks (dk_deferred);
16389 }
16390
16391 /* Commit to the currently active tentative parse.  */
16392
16393 static void
16394 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16395 {
16396   cp_parser_context *context;
16397   cp_lexer *lexer;
16398
16399   /* Mark all of the levels as committed.  */
16400   lexer = parser->lexer;
16401   for (context = parser->context; context->next; context = context->next)
16402     {
16403       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16404         break;
16405       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16406       while (!cp_lexer_saving_tokens (lexer))
16407         lexer = lexer->next;
16408       cp_lexer_commit_tokens (lexer);
16409     }
16410 }
16411
16412 /* Abort the currently active tentative parse.  All consumed tokens
16413    will be rolled back, and no diagnostics will be issued.  */
16414
16415 static void
16416 cp_parser_abort_tentative_parse (cp_parser* parser)
16417 {
16418   cp_parser_simulate_error (parser);
16419   /* Now, pretend that we want to see if the construct was
16420      successfully parsed.  */
16421   cp_parser_parse_definitely (parser);
16422 }
16423
16424 /* Stop parsing tentatively.  If a parse error has occurred, restore the
16425    token stream.  Otherwise, commit to the tokens we have consumed.
16426    Returns true if no error occurred; false otherwise.  */
16427
16428 static bool
16429 cp_parser_parse_definitely (cp_parser* parser)
16430 {
16431   bool error_occurred;
16432   cp_parser_context *context;
16433
16434   /* Remember whether or not an error occurred, since we are about to
16435      destroy that information.  */
16436   error_occurred = cp_parser_error_occurred (parser);
16437   /* Remove the topmost context from the stack.  */
16438   context = parser->context;
16439   parser->context = context->next;
16440   /* If no parse errors occurred, commit to the tentative parse.  */
16441   if (!error_occurred)
16442     {
16443       /* Commit to the tokens read tentatively, unless that was
16444          already done.  */
16445       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16446         cp_lexer_commit_tokens (parser->lexer);
16447
16448       pop_to_parent_deferring_access_checks ();
16449     }
16450   /* Otherwise, if errors occurred, roll back our state so that things
16451      are just as they were before we began the tentative parse.  */
16452   else
16453     {
16454       cp_lexer_rollback_tokens (parser->lexer);
16455       pop_deferring_access_checks ();
16456     }
16457   /* Add the context to the front of the free list.  */
16458   context->next = cp_parser_context_free_list;
16459   cp_parser_context_free_list = context;
16460
16461   return !error_occurred;
16462 }
16463
16464 /* Returns true if we are parsing tentatively and are not committed to
16465    this tentative parse.  */
16466
16467 static bool
16468 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16469 {
16470   return (cp_parser_parsing_tentatively (parser)
16471           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16472 }
16473
16474 /* Returns nonzero iff an error has occurred during the most recent
16475    tentative parse.  */
16476
16477 static bool
16478 cp_parser_error_occurred (cp_parser* parser)
16479 {
16480   return (cp_parser_parsing_tentatively (parser)
16481           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16482 }
16483
16484 /* Returns nonzero if GNU extensions are allowed.  */
16485
16486 static bool
16487 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16488 {
16489   return parser->allow_gnu_extensions_p;
16490 }
16491 \f
16492 /* Objective-C++ Productions */
16493
16494
16495 /* Parse an Objective-C expression, which feeds into a primary-expression
16496    above.
16497
16498    objc-expression:
16499      objc-message-expression
16500      objc-string-literal
16501      objc-encode-expression
16502      objc-protocol-expression
16503      objc-selector-expression
16504
16505   Returns a tree representation of the expression.  */
16506
16507 static tree
16508 cp_parser_objc_expression (cp_parser* parser)
16509 {
16510   /* Try to figure out what kind of declaration is present.  */
16511   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16512
16513   switch (kwd->type)
16514     {
16515     case CPP_OPEN_SQUARE:
16516       return cp_parser_objc_message_expression (parser);
16517
16518     case CPP_OBJC_STRING:
16519       kwd = cp_lexer_consume_token (parser->lexer);
16520       return objc_build_string_object (kwd->value);
16521
16522     case CPP_KEYWORD:
16523       switch (kwd->keyword)
16524         {
16525         case RID_AT_ENCODE:
16526           return cp_parser_objc_encode_expression (parser);
16527
16528         case RID_AT_PROTOCOL:
16529           return cp_parser_objc_protocol_expression (parser);
16530
16531         case RID_AT_SELECTOR:
16532           return cp_parser_objc_selector_expression (parser);
16533
16534         default:
16535           break;
16536         }
16537     default:
16538       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16539       cp_parser_skip_to_end_of_block_or_statement (parser);
16540     }
16541
16542   return error_mark_node;
16543 }
16544
16545 /* Parse an Objective-C message expression.
16546
16547    objc-message-expression:
16548      [ objc-message-receiver objc-message-args ]
16549
16550    Returns a representation of an Objective-C message.  */
16551
16552 static tree
16553 cp_parser_objc_message_expression (cp_parser* parser)
16554 {
16555   tree receiver, messageargs;
16556
16557   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
16558   receiver = cp_parser_objc_message_receiver (parser);
16559   messageargs = cp_parser_objc_message_args (parser);
16560   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16561
16562   return objc_build_message_expr (build_tree_list (receiver, messageargs));
16563 }
16564
16565 /* Parse an objc-message-receiver.
16566
16567    objc-message-receiver:
16568      expression
16569      simple-type-specifier
16570
16571   Returns a representation of the type or expression.  */
16572
16573 static tree
16574 cp_parser_objc_message_receiver (cp_parser* parser)
16575 {
16576   tree rcv;
16577
16578   /* An Objective-C message receiver may be either (1) a type
16579      or (2) an expression.  */
16580   cp_parser_parse_tentatively (parser);
16581   rcv = cp_parser_expression (parser, false);
16582
16583   if (cp_parser_parse_definitely (parser))
16584     return rcv;
16585
16586   rcv = cp_parser_simple_type_specifier (parser,
16587                                          /*decl_specs=*/NULL,
16588                                          CP_PARSER_FLAGS_NONE);
16589
16590   return objc_get_class_reference (rcv);
16591 }
16592
16593 /* Parse the arguments and selectors comprising an Objective-C message.
16594
16595    objc-message-args:
16596      objc-selector
16597      objc-selector-args
16598      objc-selector-args , objc-comma-args
16599
16600    objc-selector-args:
16601      objc-selector [opt] : assignment-expression
16602      objc-selector-args objc-selector [opt] : assignment-expression
16603
16604    objc-comma-args:
16605      assignment-expression
16606      objc-comma-args , assignment-expression
16607
16608    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16609    selector arguments and TREE_VALUE containing a list of comma
16610    arguments.  */
16611
16612 static tree
16613 cp_parser_objc_message_args (cp_parser* parser)
16614 {
16615   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16616   bool maybe_unary_selector_p = true;
16617   cp_token *token = cp_lexer_peek_token (parser->lexer);
16618
16619   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16620     {
16621       tree selector = NULL_TREE, arg;
16622
16623       if (token->type != CPP_COLON)
16624         selector = cp_parser_objc_selector (parser);
16625
16626       /* Detect if we have a unary selector.  */
16627       if (maybe_unary_selector_p
16628           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16629         return build_tree_list (selector, NULL_TREE);
16630
16631       maybe_unary_selector_p = false;
16632       cp_parser_require (parser, CPP_COLON, "`:'");
16633       arg = cp_parser_assignment_expression (parser, false);
16634
16635       sel_args
16636         = chainon (sel_args,
16637                    build_tree_list (selector, arg));
16638
16639       token = cp_lexer_peek_token (parser->lexer);
16640     }
16641
16642   /* Handle non-selector arguments, if any. */
16643   while (token->type == CPP_COMMA)
16644     {
16645       tree arg;
16646
16647       cp_lexer_consume_token (parser->lexer);
16648       arg = cp_parser_assignment_expression (parser, false);
16649
16650       addl_args
16651         = chainon (addl_args,
16652                    build_tree_list (NULL_TREE, arg));
16653
16654       token = cp_lexer_peek_token (parser->lexer);
16655     }
16656
16657   return build_tree_list (sel_args, addl_args);
16658 }
16659
16660 /* Parse an Objective-C encode expression.
16661
16662    objc-encode-expression:
16663      @encode objc-typename
16664
16665    Returns an encoded representation of the type argument.  */
16666
16667 static tree
16668 cp_parser_objc_encode_expression (cp_parser* parser)
16669 {
16670   tree type;
16671
16672   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
16673   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16674   type = complete_type (cp_parser_type_id (parser));
16675   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16676
16677   if (!type)
16678     {
16679       error ("%<@encode%> must specify a type as an argument");
16680       return error_mark_node;
16681     }
16682
16683   return objc_build_encode_expr (type);
16684 }
16685
16686 /* Parse an Objective-C @defs expression.  */
16687
16688 static tree
16689 cp_parser_objc_defs_expression (cp_parser *parser)
16690 {
16691   tree name;
16692
16693   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
16694   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16695   name = cp_parser_identifier (parser);
16696   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16697
16698   return objc_get_class_ivars (name);
16699 }
16700
16701 /* Parse an Objective-C protocol expression.
16702
16703   objc-protocol-expression:
16704     @protocol ( identifier )
16705
16706   Returns a representation of the protocol expression.  */
16707
16708 static tree
16709 cp_parser_objc_protocol_expression (cp_parser* parser)
16710 {
16711   tree proto;
16712
16713   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
16714   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16715   proto = cp_parser_identifier (parser);
16716   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16717
16718   return objc_build_protocol_expr (proto);
16719 }
16720
16721 /* Parse an Objective-C selector expression.
16722
16723    objc-selector-expression:
16724      @selector ( objc-method-signature )
16725
16726    objc-method-signature:
16727      objc-selector
16728      objc-selector-seq
16729
16730    objc-selector-seq:
16731      objc-selector :
16732      objc-selector-seq objc-selector :
16733
16734   Returns a representation of the method selector.  */
16735
16736 static tree
16737 cp_parser_objc_selector_expression (cp_parser* parser)
16738 {
16739   tree sel_seq = NULL_TREE;
16740   bool maybe_unary_selector_p = true;
16741   cp_token *token;
16742
16743   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
16744   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16745   token = cp_lexer_peek_token (parser->lexer);
16746
16747   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16748          || token->type == CPP_SCOPE)
16749     {
16750       tree selector = NULL_TREE;
16751
16752       if (token->type != CPP_COLON
16753           || token->type == CPP_SCOPE)
16754         selector = cp_parser_objc_selector (parser);
16755
16756       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16757           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16758         {
16759           /* Detect if we have a unary selector.  */
16760           if (maybe_unary_selector_p)
16761             {
16762               sel_seq = selector;
16763               goto finish_selector;
16764             }
16765           else
16766             {
16767               cp_parser_error (parser, "expected %<:%>");
16768             }
16769         }
16770       maybe_unary_selector_p = false;
16771       token = cp_lexer_consume_token (parser->lexer);
16772       
16773       if (token->type == CPP_SCOPE)
16774         {
16775           sel_seq
16776             = chainon (sel_seq,
16777                        build_tree_list (selector, NULL_TREE));
16778           sel_seq
16779             = chainon (sel_seq,
16780                        build_tree_list (NULL_TREE, NULL_TREE));
16781         }
16782       else
16783         sel_seq
16784           = chainon (sel_seq,
16785                      build_tree_list (selector, NULL_TREE));
16786
16787       token = cp_lexer_peek_token (parser->lexer);
16788     }
16789
16790  finish_selector:
16791   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16792
16793   return objc_build_selector_expr (sel_seq);
16794 }
16795
16796 /* Parse a list of identifiers.
16797
16798    objc-identifier-list:
16799      identifier
16800      objc-identifier-list , identifier
16801
16802    Returns a TREE_LIST of identifier nodes.  */
16803
16804 static tree
16805 cp_parser_objc_identifier_list (cp_parser* parser)
16806 {
16807   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16808   cp_token *sep = cp_lexer_peek_token (parser->lexer);
16809
16810   while (sep->type == CPP_COMMA)
16811     {
16812       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16813       list = chainon (list,
16814                       build_tree_list (NULL_TREE,
16815                                        cp_parser_identifier (parser)));
16816       sep = cp_lexer_peek_token (parser->lexer);
16817     }
16818
16819   return list;
16820 }
16821
16822 /* Parse an Objective-C alias declaration.
16823
16824    objc-alias-declaration:
16825      @compatibility_alias identifier identifier ;
16826
16827    This function registers the alias mapping with the Objective-C front-end.
16828    It returns nothing.  */
16829
16830 static void
16831 cp_parser_objc_alias_declaration (cp_parser* parser)
16832 {
16833   tree alias, orig;
16834
16835   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
16836   alias = cp_parser_identifier (parser);
16837   orig = cp_parser_identifier (parser);
16838   objc_declare_alias (alias, orig);
16839   cp_parser_consume_semicolon_at_end_of_statement (parser);
16840 }
16841
16842 /* Parse an Objective-C class forward-declaration.
16843
16844    objc-class-declaration:
16845      @class objc-identifier-list ;
16846
16847    The function registers the forward declarations with the Objective-C
16848    front-end.  It returns nothing.  */
16849
16850 static void
16851 cp_parser_objc_class_declaration (cp_parser* parser)
16852 {
16853   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
16854   objc_declare_class (cp_parser_objc_identifier_list (parser));
16855   cp_parser_consume_semicolon_at_end_of_statement (parser);
16856 }
16857
16858 /* Parse a list of Objective-C protocol references.
16859
16860    objc-protocol-refs-opt:
16861      objc-protocol-refs [opt]
16862
16863    objc-protocol-refs:
16864      < objc-identifier-list >
16865
16866    Returns a TREE_LIST of identifiers, if any.  */
16867
16868 static tree
16869 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16870 {
16871   tree protorefs = NULL_TREE;
16872
16873   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16874     {
16875       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
16876       protorefs = cp_parser_objc_identifier_list (parser);
16877       cp_parser_require (parser, CPP_GREATER, "`>'");
16878     }
16879
16880   return protorefs;
16881 }
16882
16883 /* Parse a Objective-C visibility specification.  */
16884
16885 static void
16886 cp_parser_objc_visibility_spec (cp_parser* parser)
16887 {
16888   cp_token *vis = cp_lexer_peek_token (parser->lexer);
16889
16890   switch (vis->keyword)
16891     {
16892     case RID_AT_PRIVATE:
16893       objc_set_visibility (2);
16894       break;
16895     case RID_AT_PROTECTED:
16896       objc_set_visibility (0);
16897       break;
16898     case RID_AT_PUBLIC:
16899       objc_set_visibility (1);
16900       break;
16901     default:
16902       return;
16903     }
16904
16905   /* Eat '@private'/'@protected'/'@public'.  */
16906   cp_lexer_consume_token (parser->lexer);
16907 }
16908
16909 /* Parse an Objective-C method type.  */
16910
16911 static void
16912 cp_parser_objc_method_type (cp_parser* parser)
16913 {
16914   objc_set_method_type
16915    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16916     ? PLUS_EXPR
16917     : MINUS_EXPR);
16918 }
16919
16920 /* Parse an Objective-C protocol qualifier.  */
16921
16922 static tree
16923 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16924 {
16925   tree quals = NULL_TREE, node;
16926   cp_token *token = cp_lexer_peek_token (parser->lexer);
16927
16928   node = token->value;
16929
16930   while (node && TREE_CODE (node) == IDENTIFIER_NODE
16931          && (node == ridpointers [(int) RID_IN]
16932              || node == ridpointers [(int) RID_OUT]
16933              || node == ridpointers [(int) RID_INOUT]
16934              || node == ridpointers [(int) RID_BYCOPY]
16935              || node == ridpointers [(int) RID_BYREF]
16936              || node == ridpointers [(int) RID_ONEWAY]))
16937     {
16938       quals = tree_cons (NULL_TREE, node, quals);
16939       cp_lexer_consume_token (parser->lexer);
16940       token = cp_lexer_peek_token (parser->lexer);
16941       node = token->value;
16942     }
16943
16944   return quals;
16945 }
16946
16947 /* Parse an Objective-C typename.  */
16948
16949 static tree
16950 cp_parser_objc_typename (cp_parser* parser)
16951 {
16952   tree typename = NULL_TREE;
16953
16954   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16955     {
16956       tree proto_quals, cp_type = NULL_TREE;
16957
16958       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
16959       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16960
16961       /* An ObjC type name may consist of just protocol qualifiers, in which
16962          case the type shall default to 'id'.  */
16963       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16964         cp_type = cp_parser_type_id (parser);
16965
16966       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16967       typename = build_tree_list (proto_quals, cp_type);
16968     }
16969
16970   return typename;
16971 }
16972
16973 /* Check to see if TYPE refers to an Objective-C selector name.  */
16974
16975 static bool
16976 cp_parser_objc_selector_p (enum cpp_ttype type)
16977 {
16978   return (type == CPP_NAME || type == CPP_KEYWORD
16979           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16980           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16981           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16982           || type == CPP_XOR || type == CPP_XOR_EQ);
16983 }
16984
16985 /* Parse an Objective-C selector.  */
16986
16987 static tree
16988 cp_parser_objc_selector (cp_parser* parser)
16989 {
16990   cp_token *token = cp_lexer_consume_token (parser->lexer);
16991
16992   if (!cp_parser_objc_selector_p (token->type))
16993     {
16994       error ("invalid Objective-C++ selector name");
16995       return error_mark_node;
16996     }
16997
16998   /* C++ operator names are allowed to appear in ObjC selectors.  */
16999   switch (token->type)
17000     {
17001     case CPP_AND_AND: return get_identifier ("and");
17002     case CPP_AND_EQ: return get_identifier ("and_eq");
17003     case CPP_AND: return get_identifier ("bitand");
17004     case CPP_OR: return get_identifier ("bitor");
17005     case CPP_COMPL: return get_identifier ("compl");
17006     case CPP_NOT: return get_identifier ("not");
17007     case CPP_NOT_EQ: return get_identifier ("not_eq");
17008     case CPP_OR_OR: return get_identifier ("or");
17009     case CPP_OR_EQ: return get_identifier ("or_eq");
17010     case CPP_XOR: return get_identifier ("xor");
17011     case CPP_XOR_EQ: return get_identifier ("xor_eq");
17012     default: return token->value;
17013     }
17014 }
17015
17016 /* Parse an Objective-C params list.  */
17017
17018 static tree
17019 cp_parser_objc_method_keyword_params (cp_parser* parser)
17020 {
17021   tree params = NULL_TREE;
17022   bool maybe_unary_selector_p = true;
17023   cp_token *token = cp_lexer_peek_token (parser->lexer);
17024
17025   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17026     {
17027       tree selector = NULL_TREE, typename, identifier;
17028
17029       if (token->type != CPP_COLON)
17030         selector = cp_parser_objc_selector (parser);
17031
17032       /* Detect if we have a unary selector.  */
17033       if (maybe_unary_selector_p
17034           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17035         return selector;
17036
17037       maybe_unary_selector_p = false;
17038       cp_parser_require (parser, CPP_COLON, "`:'");
17039       typename = cp_parser_objc_typename (parser);
17040       identifier = cp_parser_identifier (parser);
17041
17042       params
17043         = chainon (params,
17044                    objc_build_keyword_decl (selector,
17045                                             typename,
17046                                             identifier));
17047
17048       token = cp_lexer_peek_token (parser->lexer);
17049     }
17050
17051   return params;
17052 }
17053
17054 /* Parse the non-keyword Objective-C params.  */
17055
17056 static tree
17057 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17058 {
17059   tree params = make_node (TREE_LIST);
17060   cp_token *token = cp_lexer_peek_token (parser->lexer);
17061   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
17062
17063   while (token->type == CPP_COMMA)
17064     {
17065       cp_parameter_declarator *parmdecl;
17066       tree parm;
17067
17068       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17069       token = cp_lexer_peek_token (parser->lexer);
17070
17071       if (token->type == CPP_ELLIPSIS)
17072         {
17073           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
17074           *ellipsisp = true;
17075           break;
17076         }
17077
17078       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17079       parm = grokdeclarator (parmdecl->declarator,
17080                              &parmdecl->decl_specifiers,
17081                              PARM, /*initialized=*/0,
17082                              /*attrlist=*/NULL);
17083
17084       chainon (params, build_tree_list (NULL_TREE, parm));
17085       token = cp_lexer_peek_token (parser->lexer);
17086     }
17087
17088   return params;
17089 }
17090
17091 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
17092
17093 static void
17094 cp_parser_objc_interstitial_code (cp_parser* parser)
17095 {
17096   cp_token *token = cp_lexer_peek_token (parser->lexer);
17097
17098   /* If the next token is `extern' and the following token is a string
17099      literal, then we have a linkage specification.  */
17100   if (token->keyword == RID_EXTERN
17101       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17102     cp_parser_linkage_specification (parser);
17103   /* Handle #pragma, if any.  */
17104   else if (token->type == CPP_PRAGMA)
17105     cp_parser_pragma (parser, pragma_external);
17106   /* Allow stray semicolons.  */
17107   else if (token->type == CPP_SEMICOLON)
17108     cp_lexer_consume_token (parser->lexer);
17109   /* Finally, try to parse a block-declaration, or a function-definition.  */
17110   else
17111     cp_parser_block_declaration (parser, /*statement_p=*/false);
17112 }
17113
17114 /* Parse a method signature.  */
17115
17116 static tree
17117 cp_parser_objc_method_signature (cp_parser* parser)
17118 {
17119   tree rettype, kwdparms, optparms;
17120   bool ellipsis = false;
17121
17122   cp_parser_objc_method_type (parser);
17123   rettype = cp_parser_objc_typename (parser);
17124   kwdparms = cp_parser_objc_method_keyword_params (parser);
17125   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17126
17127   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17128 }
17129
17130 /* Pars an Objective-C method prototype list.  */
17131
17132 static void
17133 cp_parser_objc_method_prototype_list (cp_parser* parser)
17134 {
17135   cp_token *token = cp_lexer_peek_token (parser->lexer);
17136
17137   while (token->keyword != RID_AT_END)
17138     {
17139       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17140         {
17141           objc_add_method_declaration
17142            (cp_parser_objc_method_signature (parser));
17143           cp_parser_consume_semicolon_at_end_of_statement (parser);
17144         }
17145       else
17146         /* Allow for interspersed non-ObjC++ code.  */
17147         cp_parser_objc_interstitial_code (parser);
17148
17149       token = cp_lexer_peek_token (parser->lexer);
17150     }
17151
17152   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17153   objc_finish_interface ();
17154 }
17155
17156 /* Parse an Objective-C method definition list.  */
17157
17158 static void
17159 cp_parser_objc_method_definition_list (cp_parser* parser)
17160 {
17161   cp_token *token = cp_lexer_peek_token (parser->lexer);
17162
17163   while (token->keyword != RID_AT_END)
17164     {
17165       tree meth;
17166
17167       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17168         {
17169           push_deferring_access_checks (dk_deferred);
17170           objc_start_method_definition
17171            (cp_parser_objc_method_signature (parser));
17172
17173           /* For historical reasons, we accept an optional semicolon.  */
17174           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17175             cp_lexer_consume_token (parser->lexer);
17176
17177           perform_deferred_access_checks ();
17178           stop_deferring_access_checks ();
17179           meth = cp_parser_function_definition_after_declarator (parser,
17180                                                                  false);
17181           pop_deferring_access_checks ();
17182           objc_finish_method_definition (meth);
17183         }
17184       else
17185         /* Allow for interspersed non-ObjC++ code.  */
17186         cp_parser_objc_interstitial_code (parser);
17187
17188       token = cp_lexer_peek_token (parser->lexer);
17189     }
17190
17191   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17192   objc_finish_implementation ();
17193 }
17194
17195 /* Parse Objective-C ivars.  */
17196
17197 static void
17198 cp_parser_objc_class_ivars (cp_parser* parser)
17199 {
17200   cp_token *token = cp_lexer_peek_token (parser->lexer);
17201
17202   if (token->type != CPP_OPEN_BRACE)
17203     return;     /* No ivars specified.  */
17204
17205   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
17206   token = cp_lexer_peek_token (parser->lexer);
17207
17208   while (token->type != CPP_CLOSE_BRACE)
17209     {
17210       cp_decl_specifier_seq declspecs;
17211       int decl_class_or_enum_p;
17212       tree prefix_attributes;
17213
17214       cp_parser_objc_visibility_spec (parser);
17215
17216       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17217         break;
17218
17219       cp_parser_decl_specifier_seq (parser,
17220                                     CP_PARSER_FLAGS_OPTIONAL,
17221                                     &declspecs,
17222                                     &decl_class_or_enum_p);
17223       prefix_attributes = declspecs.attributes;
17224       declspecs.attributes = NULL_TREE;
17225
17226       /* Keep going until we hit the `;' at the end of the
17227          declaration.  */
17228       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17229         {
17230           tree width = NULL_TREE, attributes, first_attribute, decl;
17231           cp_declarator *declarator = NULL;
17232           int ctor_dtor_or_conv_p;
17233
17234           /* Check for a (possibly unnamed) bitfield declaration.  */
17235           token = cp_lexer_peek_token (parser->lexer);
17236           if (token->type == CPP_COLON)
17237             goto eat_colon;
17238
17239           if (token->type == CPP_NAME
17240               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17241                   == CPP_COLON))
17242             {
17243               /* Get the name of the bitfield.  */
17244               declarator = make_id_declarator (NULL_TREE,
17245                                                cp_parser_identifier (parser),
17246                                                sfk_none);
17247
17248              eat_colon:
17249               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17250               /* Get the width of the bitfield.  */
17251               width
17252                 = cp_parser_constant_expression (parser,
17253                                                  /*allow_non_constant=*/false,
17254                                                  NULL);
17255             }
17256           else
17257             {
17258               /* Parse the declarator.  */
17259               declarator
17260                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17261                                         &ctor_dtor_or_conv_p,
17262                                         /*parenthesized_p=*/NULL,
17263                                         /*member_p=*/false);
17264             }
17265
17266           /* Look for attributes that apply to the ivar.  */
17267           attributes = cp_parser_attributes_opt (parser);
17268           /* Remember which attributes are prefix attributes and
17269              which are not.  */
17270           first_attribute = attributes;
17271           /* Combine the attributes.  */
17272           attributes = chainon (prefix_attributes, attributes);
17273
17274           if (width)
17275             {
17276               /* Create the bitfield declaration.  */
17277               decl = grokbitfield (declarator, &declspecs, width);
17278               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17279             }
17280           else
17281             decl = grokfield (declarator, &declspecs, NULL_TREE,
17282                               NULL_TREE, attributes);
17283
17284           /* Add the instance variable.  */
17285           objc_add_instance_variable (decl);
17286
17287           /* Reset PREFIX_ATTRIBUTES.  */
17288           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17289             attributes = TREE_CHAIN (attributes);
17290           if (attributes)
17291             TREE_CHAIN (attributes) = NULL_TREE;
17292
17293           token = cp_lexer_peek_token (parser->lexer);
17294
17295           if (token->type == CPP_COMMA)
17296             {
17297               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17298               continue;
17299             }
17300           break;
17301         }
17302
17303       cp_parser_consume_semicolon_at_end_of_statement (parser);
17304       token = cp_lexer_peek_token (parser->lexer);
17305     }
17306
17307   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17308   /* For historical reasons, we accept an optional semicolon.  */
17309   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17310     cp_lexer_consume_token (parser->lexer);
17311 }
17312
17313 /* Parse an Objective-C protocol declaration.  */
17314
17315 static void
17316 cp_parser_objc_protocol_declaration (cp_parser* parser)
17317 {
17318   tree proto, protorefs;
17319   cp_token *tok;
17320
17321   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17322   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17323     {
17324       error ("identifier expected after %<@protocol%>");
17325       goto finish;
17326     }
17327
17328   /* See if we have a forward declaration or a definition.  */
17329   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17330
17331   /* Try a forward declaration first.  */
17332   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17333     {
17334       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17335      finish:
17336       cp_parser_consume_semicolon_at_end_of_statement (parser);
17337     }
17338
17339   /* Ok, we got a full-fledged definition (or at least should).  */
17340   else
17341     {
17342       proto = cp_parser_identifier (parser);
17343       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17344       objc_start_protocol (proto, protorefs);
17345       cp_parser_objc_method_prototype_list (parser);
17346     }
17347 }
17348
17349 /* Parse an Objective-C superclass or category.  */
17350
17351 static void
17352 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17353                                                           tree *categ)
17354 {
17355   cp_token *next = cp_lexer_peek_token (parser->lexer);
17356
17357   *super = *categ = NULL_TREE;
17358   if (next->type == CPP_COLON)
17359     {
17360       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17361       *super = cp_parser_identifier (parser);
17362     }
17363   else if (next->type == CPP_OPEN_PAREN)
17364     {
17365       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17366       *categ = cp_parser_identifier (parser);
17367       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17368     }
17369 }
17370
17371 /* Parse an Objective-C class interface.  */
17372
17373 static void
17374 cp_parser_objc_class_interface (cp_parser* parser)
17375 {
17376   tree name, super, categ, protos;
17377
17378   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17379   name = cp_parser_identifier (parser);
17380   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17381   protos = cp_parser_objc_protocol_refs_opt (parser);
17382
17383   /* We have either a class or a category on our hands.  */
17384   if (categ)
17385     objc_start_category_interface (name, categ, protos);
17386   else
17387     {
17388       objc_start_class_interface (name, super, protos);
17389       /* Handle instance variable declarations, if any.  */
17390       cp_parser_objc_class_ivars (parser);
17391       objc_continue_interface ();
17392     }
17393
17394   cp_parser_objc_method_prototype_list (parser);
17395 }
17396
17397 /* Parse an Objective-C class implementation.  */
17398
17399 static void
17400 cp_parser_objc_class_implementation (cp_parser* parser)
17401 {
17402   tree name, super, categ;
17403
17404   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17405   name = cp_parser_identifier (parser);
17406   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17407
17408   /* We have either a class or a category on our hands.  */
17409   if (categ)
17410     objc_start_category_implementation (name, categ);
17411   else
17412     {
17413       objc_start_class_implementation (name, super);
17414       /* Handle instance variable declarations, if any.  */
17415       cp_parser_objc_class_ivars (parser);
17416       objc_continue_implementation ();
17417     }
17418
17419   cp_parser_objc_method_definition_list (parser);
17420 }
17421
17422 /* Consume the @end token and finish off the implementation.  */
17423
17424 static void
17425 cp_parser_objc_end_implementation (cp_parser* parser)
17426 {
17427   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17428   objc_finish_implementation ();
17429 }
17430
17431 /* Parse an Objective-C declaration.  */
17432
17433 static void
17434 cp_parser_objc_declaration (cp_parser* parser)
17435 {
17436   /* Try to figure out what kind of declaration is present.  */
17437   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17438
17439   switch (kwd->keyword)
17440     {
17441     case RID_AT_ALIAS:
17442       cp_parser_objc_alias_declaration (parser);
17443       break;
17444     case RID_AT_CLASS:
17445       cp_parser_objc_class_declaration (parser);
17446       break;
17447     case RID_AT_PROTOCOL:
17448       cp_parser_objc_protocol_declaration (parser);
17449       break;
17450     case RID_AT_INTERFACE:
17451       cp_parser_objc_class_interface (parser);
17452       break;
17453     case RID_AT_IMPLEMENTATION:
17454       cp_parser_objc_class_implementation (parser);
17455       break;
17456     case RID_AT_END:
17457       cp_parser_objc_end_implementation (parser);
17458       break;
17459     default:
17460       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17461       cp_parser_skip_to_end_of_block_or_statement (parser);
17462     }
17463 }
17464
17465 /* Parse an Objective-C try-catch-finally statement.
17466
17467    objc-try-catch-finally-stmt:
17468      @try compound-statement objc-catch-clause-seq [opt]
17469        objc-finally-clause [opt]
17470
17471    objc-catch-clause-seq:
17472      objc-catch-clause objc-catch-clause-seq [opt]
17473
17474    objc-catch-clause:
17475      @catch ( exception-declaration ) compound-statement
17476
17477    objc-finally-clause
17478      @finally compound-statement
17479
17480    Returns NULL_TREE.  */
17481
17482 static tree
17483 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17484   location_t location;
17485   tree stmt;
17486
17487   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17488   location = cp_lexer_peek_token (parser->lexer)->location;
17489   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17490      node, lest it get absorbed into the surrounding block.  */
17491   stmt = push_stmt_list ();
17492   cp_parser_compound_statement (parser, NULL, false);
17493   objc_begin_try_stmt (location, pop_stmt_list (stmt));
17494
17495   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17496     {
17497       cp_parameter_declarator *parmdecl;
17498       tree parm;
17499
17500       cp_lexer_consume_token (parser->lexer);
17501       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17502       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17503       parm = grokdeclarator (parmdecl->declarator,
17504                              &parmdecl->decl_specifiers,
17505                              PARM, /*initialized=*/0,
17506                              /*attrlist=*/NULL);
17507       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17508       objc_begin_catch_clause (parm);
17509       cp_parser_compound_statement (parser, NULL, false);
17510       objc_finish_catch_clause ();
17511     }
17512
17513   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17514     {
17515       cp_lexer_consume_token (parser->lexer);
17516       location = cp_lexer_peek_token (parser->lexer)->location;
17517       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17518          node, lest it get absorbed into the surrounding block.  */
17519       stmt = push_stmt_list ();
17520       cp_parser_compound_statement (parser, NULL, false);
17521       objc_build_finally_clause (location, pop_stmt_list (stmt));
17522     }
17523
17524   return objc_finish_try_stmt ();
17525 }
17526
17527 /* Parse an Objective-C synchronized statement.
17528
17529    objc-synchronized-stmt:
17530      @synchronized ( expression ) compound-statement
17531
17532    Returns NULL_TREE.  */
17533
17534 static tree
17535 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17536   location_t location;
17537   tree lock, stmt;
17538
17539   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17540
17541   location = cp_lexer_peek_token (parser->lexer)->location;
17542   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17543   lock = cp_parser_expression (parser, false);
17544   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17545
17546   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17547      node, lest it get absorbed into the surrounding block.  */
17548   stmt = push_stmt_list ();
17549   cp_parser_compound_statement (parser, NULL, false);
17550
17551   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17552 }
17553
17554 /* Parse an Objective-C throw statement.
17555
17556    objc-throw-stmt:
17557      @throw assignment-expression [opt] ;
17558
17559    Returns a constructed '@throw' statement.  */
17560
17561 static tree
17562 cp_parser_objc_throw_statement (cp_parser *parser) {
17563   tree expr = NULL_TREE;
17564
17565   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17566
17567   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17568     expr = cp_parser_assignment_expression (parser, false);
17569
17570   cp_parser_consume_semicolon_at_end_of_statement (parser);
17571
17572   return objc_build_throw_stmt (expr);
17573 }
17574
17575 /* Parse an Objective-C statement.  */
17576
17577 static tree
17578 cp_parser_objc_statement (cp_parser * parser) {
17579   /* Try to figure out what kind of declaration is present.  */
17580   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17581
17582   switch (kwd->keyword)
17583     {
17584     case RID_AT_TRY:
17585       return cp_parser_objc_try_catch_finally_statement (parser);
17586     case RID_AT_SYNCHRONIZED:
17587       return cp_parser_objc_synchronized_statement (parser);
17588     case RID_AT_THROW:
17589       return cp_parser_objc_throw_statement (parser);
17590     default:
17591       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17592       cp_parser_skip_to_end_of_block_or_statement (parser);
17593     }
17594
17595   return error_mark_node;
17596 }
17597 /* The parser.  */
17598
17599 static GTY (()) cp_parser *the_parser;
17600
17601 \f
17602 /* Special handling for the first token or line in the file.  The first
17603    thing in the file might be #pragma GCC pch_preprocess, which loads a
17604    PCH file, which is a GC collection point.  So we need to handle this
17605    first pragma without benefit of an existing lexer structure.
17606
17607    Always returns one token to the caller in *FIRST_TOKEN.  This is 
17608    either the true first token of the file, or the first token after
17609    the initial pragma.  */
17610
17611 static void
17612 cp_parser_initial_pragma (cp_token *first_token)
17613 {
17614   tree name = NULL;
17615
17616   cp_lexer_get_preprocessor_token (NULL, first_token);
17617   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
17618     return;
17619
17620   cp_lexer_get_preprocessor_token (NULL, first_token);
17621   if (first_token->type == CPP_STRING)
17622     {
17623       name = first_token->value;
17624
17625       cp_lexer_get_preprocessor_token (NULL, first_token);
17626       if (first_token->type != CPP_PRAGMA_EOL)
17627         error ("junk at end of %<#pragma GCC pch_preprocess%>");
17628     }
17629   else
17630     error ("expected string literal");
17631
17632   /* Skip to the end of the pragma.  */
17633   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
17634     cp_lexer_get_preprocessor_token (NULL, first_token);
17635
17636   /* Read one more token to return to our caller.  */
17637   cp_lexer_get_preprocessor_token (NULL, first_token);
17638
17639   /* Now actually load the PCH file.  */
17640   if (name)
17641     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
17642 }
17643
17644 /* Normal parsing of a pragma token.  Here we can (and must) use the
17645    regular lexer.  */
17646
17647 static bool
17648 cp_parser_pragma (cp_parser *parser, enum pragma_context context ATTRIBUTE_UNUSED)
17649 {
17650   cp_token *pragma_tok;
17651   unsigned int id;
17652
17653   pragma_tok = cp_lexer_consume_token (parser->lexer);
17654   gcc_assert (pragma_tok->type == CPP_PRAGMA);
17655   parser->lexer->in_pragma = true;
17656
17657   id = pragma_tok->pragma_kind;
17658   switch (id)
17659     {
17660     case PRAGMA_GCC_PCH_PREPROCESS:
17661       error ("%<#pragma GCC pch_preprocess%> must be first");
17662       break;
17663
17664     default:
17665       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
17666       c_invoke_pragma_handler (id);
17667       break;
17668     }
17669
17670   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
17671   return false;
17672 }
17673
17674 /* The interface the pragma parsers have to the lexer.  */
17675
17676 enum cpp_ttype
17677 pragma_lex (tree *value)
17678 {
17679   cp_token *tok;
17680   enum cpp_ttype ret;
17681
17682   tok = cp_lexer_peek_token (the_parser->lexer);
17683
17684   ret = tok->type;
17685   *value = tok->value;
17686
17687   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
17688     ret = CPP_EOF;
17689   else if (ret == CPP_STRING)
17690     *value = cp_parser_string_literal (the_parser, false, false);
17691   else
17692     {
17693       cp_lexer_consume_token (the_parser->lexer);
17694       if (ret == CPP_KEYWORD)
17695         ret = CPP_NAME;
17696     }
17697
17698   return ret;
17699 }
17700
17701 \f
17702 /* External interface.  */
17703
17704 /* Parse one entire translation unit.  */
17705
17706 void
17707 c_parse_file (void)
17708 {
17709   bool error_occurred;
17710   static bool already_called = false;
17711
17712   if (already_called)
17713     {
17714       sorry ("inter-module optimizations not implemented for C++");
17715       return;
17716     }
17717   already_called = true;
17718
17719   the_parser = cp_parser_new ();
17720   push_deferring_access_checks (flag_access_control
17721                                 ? dk_no_deferred : dk_no_check);
17722   error_occurred = cp_parser_translation_unit (the_parser);
17723   the_parser = NULL;
17724 }
17725
17726 /* This variable must be provided by every front end.  */
17727
17728 int yydebug;
17729
17730 #include "gt-cp-parser.h"