OSDN Git Service

PR c++/26266
[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           bool non_constant_p;
6504
6505           /* Create the declaration.  */
6506           decl = start_decl (declarator, &type_specifiers,
6507                              /*initialized_p=*/true,
6508                              attributes, /*prefix_attributes=*/NULL_TREE,
6509                              &pushed_scope);
6510           /* Parse the assignment-expression.  */
6511           initializer 
6512             = cp_parser_constant_expression (parser,
6513                                              /*allow_non_constant_p=*/true,
6514                                              &non_constant_p);
6515           if (!non_constant_p)
6516             initializer = fold_non_dependent_expr (initializer);
6517
6518           /* Process the initializer.  */
6519           cp_finish_decl (decl,
6520                           initializer, !non_constant_p, 
6521                           asm_specification,
6522                           LOOKUP_ONLYCONVERTING);
6523
6524           if (pushed_scope)
6525             pop_scope (pushed_scope);
6526
6527           return convert_from_reference (decl);
6528         }
6529     }
6530   /* If we didn't even get past the declarator successfully, we are
6531      definitely not looking at a declaration.  */
6532   else
6533     cp_parser_abort_tentative_parse (parser);
6534
6535   /* Otherwise, we are looking at an expression.  */
6536   return cp_parser_expression (parser, /*cast_p=*/false);
6537 }
6538
6539 /* Parse an iteration-statement.
6540
6541    iteration-statement:
6542      while ( condition ) statement
6543      do statement while ( expression ) ;
6544      for ( for-init-statement condition [opt] ; expression [opt] )
6545        statement
6546
6547    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6548
6549 static tree
6550 cp_parser_iteration_statement (cp_parser* parser)
6551 {
6552   cp_token *token;
6553   enum rid keyword;
6554   tree statement;
6555   bool in_iteration_statement_p;
6556
6557
6558   /* Peek at the next token.  */
6559   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6560   if (!token)
6561     return error_mark_node;
6562
6563   /* Remember whether or not we are already within an iteration
6564      statement.  */
6565   in_iteration_statement_p = parser->in_iteration_statement_p;
6566
6567   /* See what kind of keyword it is.  */
6568   keyword = token->keyword;
6569   switch (keyword)
6570     {
6571     case RID_WHILE:
6572       {
6573         tree condition;
6574
6575         /* Begin the while-statement.  */
6576         statement = begin_while_stmt ();
6577         /* Look for the `('.  */
6578         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6579         /* Parse the condition.  */
6580         condition = cp_parser_condition (parser);
6581         finish_while_stmt_cond (condition, statement);
6582         /* Look for the `)'.  */
6583         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6584         /* Parse the dependent statement.  */
6585         parser->in_iteration_statement_p = true;
6586         cp_parser_already_scoped_statement (parser);
6587         parser->in_iteration_statement_p = in_iteration_statement_p;
6588         /* We're done with the while-statement.  */
6589         finish_while_stmt (statement);
6590       }
6591       break;
6592
6593     case RID_DO:
6594       {
6595         tree expression;
6596
6597         /* Begin the do-statement.  */
6598         statement = begin_do_stmt ();
6599         /* Parse the body of the do-statement.  */
6600         parser->in_iteration_statement_p = true;
6601         cp_parser_implicitly_scoped_statement (parser);
6602         parser->in_iteration_statement_p = in_iteration_statement_p;
6603         finish_do_body (statement);
6604         /* Look for the `while' keyword.  */
6605         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6606         /* Look for the `('.  */
6607         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6608         /* Parse the expression.  */
6609         expression = cp_parser_expression (parser, /*cast_p=*/false);
6610         /* We're done with the do-statement.  */
6611         finish_do_stmt (expression, statement);
6612         /* Look for the `)'.  */
6613         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6614         /* Look for the `;'.  */
6615         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6616       }
6617       break;
6618
6619     case RID_FOR:
6620       {
6621         tree condition = NULL_TREE;
6622         tree expression = NULL_TREE;
6623
6624         /* Begin the for-statement.  */
6625         statement = begin_for_stmt ();
6626         /* Look for the `('.  */
6627         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6628         /* Parse the initialization.  */
6629         cp_parser_for_init_statement (parser);
6630         finish_for_init_stmt (statement);
6631
6632         /* If there's a condition, process it.  */
6633         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6634           condition = cp_parser_condition (parser);
6635         finish_for_cond (condition, statement);
6636         /* Look for the `;'.  */
6637         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6638
6639         /* If there's an expression, process it.  */
6640         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6641           expression = cp_parser_expression (parser, /*cast_p=*/false);
6642         finish_for_expr (expression, statement);
6643         /* Look for the `)'.  */
6644         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6645
6646         /* Parse the body of the for-statement.  */
6647         parser->in_iteration_statement_p = true;
6648         cp_parser_already_scoped_statement (parser);
6649         parser->in_iteration_statement_p = in_iteration_statement_p;
6650
6651         /* We're done with the for-statement.  */
6652         finish_for_stmt (statement);
6653       }
6654       break;
6655
6656     default:
6657       cp_parser_error (parser, "expected iteration-statement");
6658       statement = error_mark_node;
6659       break;
6660     }
6661
6662   return statement;
6663 }
6664
6665 /* Parse a for-init-statement.
6666
6667    for-init-statement:
6668      expression-statement
6669      simple-declaration  */
6670
6671 static void
6672 cp_parser_for_init_statement (cp_parser* parser)
6673 {
6674   /* If the next token is a `;', then we have an empty
6675      expression-statement.  Grammatically, this is also a
6676      simple-declaration, but an invalid one, because it does not
6677      declare anything.  Therefore, if we did not handle this case
6678      specially, we would issue an error message about an invalid
6679      declaration.  */
6680   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6681     {
6682       /* We're going to speculatively look for a declaration, falling back
6683          to an expression, if necessary.  */
6684       cp_parser_parse_tentatively (parser);
6685       /* Parse the declaration.  */
6686       cp_parser_simple_declaration (parser,
6687                                     /*function_definition_allowed_p=*/false);
6688       /* If the tentative parse failed, then we shall need to look for an
6689          expression-statement.  */
6690       if (cp_parser_parse_definitely (parser))
6691         return;
6692     }
6693
6694   cp_parser_expression_statement (parser, false);
6695 }
6696
6697 /* Parse a jump-statement.
6698
6699    jump-statement:
6700      break ;
6701      continue ;
6702      return expression [opt] ;
6703      goto identifier ;
6704
6705    GNU extension:
6706
6707    jump-statement:
6708      goto * expression ;
6709
6710    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6711
6712 static tree
6713 cp_parser_jump_statement (cp_parser* parser)
6714 {
6715   tree statement = error_mark_node;
6716   cp_token *token;
6717   enum rid keyword;
6718
6719   /* Peek at the next token.  */
6720   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6721   if (!token)
6722     return error_mark_node;
6723
6724   /* See what kind of keyword it is.  */
6725   keyword = token->keyword;
6726   switch (keyword)
6727     {
6728     case RID_BREAK:
6729       if (!parser->in_switch_statement_p
6730           && !parser->in_iteration_statement_p)
6731         {
6732           error ("break statement not within loop or switch");
6733           statement = error_mark_node;
6734         }
6735       else
6736         statement = finish_break_stmt ();
6737       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6738       break;
6739
6740     case RID_CONTINUE:
6741       if (!parser->in_iteration_statement_p)
6742         {
6743           error ("continue statement not within a loop");
6744           statement = error_mark_node;
6745         }
6746       else
6747         statement = finish_continue_stmt ();
6748       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6749       break;
6750
6751     case RID_RETURN:
6752       {
6753         tree expr;
6754
6755         /* If the next token is a `;', then there is no
6756            expression.  */
6757         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6758           expr = cp_parser_expression (parser, /*cast_p=*/false);
6759         else
6760           expr = NULL_TREE;
6761         /* Build the return-statement.  */
6762         statement = finish_return_stmt (expr);
6763         /* Look for the final `;'.  */
6764         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6765       }
6766       break;
6767
6768     case RID_GOTO:
6769       /* Create the goto-statement.  */
6770       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6771         {
6772           /* Issue a warning about this use of a GNU extension.  */
6773           if (pedantic)
6774             pedwarn ("ISO C++ forbids computed gotos");
6775           /* Consume the '*' token.  */
6776           cp_lexer_consume_token (parser->lexer);
6777           /* Parse the dependent expression.  */
6778           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6779         }
6780       else
6781         finish_goto_stmt (cp_parser_identifier (parser));
6782       /* Look for the final `;'.  */
6783       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6784       break;
6785
6786     default:
6787       cp_parser_error (parser, "expected jump-statement");
6788       break;
6789     }
6790
6791   return statement;
6792 }
6793
6794 /* Parse a declaration-statement.
6795
6796    declaration-statement:
6797      block-declaration  */
6798
6799 static void
6800 cp_parser_declaration_statement (cp_parser* parser)
6801 {
6802   void *p;
6803
6804   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6805   p = obstack_alloc (&declarator_obstack, 0);
6806
6807  /* Parse the block-declaration.  */
6808   cp_parser_block_declaration (parser, /*statement_p=*/true);
6809
6810   /* Free any declarators allocated.  */
6811   obstack_free (&declarator_obstack, p);
6812
6813   /* Finish off the statement.  */
6814   finish_stmt ();
6815 }
6816
6817 /* Some dependent statements (like `if (cond) statement'), are
6818    implicitly in their own scope.  In other words, if the statement is
6819    a single statement (as opposed to a compound-statement), it is
6820    none-the-less treated as if it were enclosed in braces.  Any
6821    declarations appearing in the dependent statement are out of scope
6822    after control passes that point.  This function parses a statement,
6823    but ensures that is in its own scope, even if it is not a
6824    compound-statement.
6825
6826    Returns the new statement.  */
6827
6828 static tree
6829 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6830 {
6831   tree statement;
6832
6833   /* Mark if () ; with a special NOP_EXPR.  */
6834   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6835     {
6836       cp_lexer_consume_token (parser->lexer);
6837       statement = add_stmt (build_empty_stmt ());
6838     }
6839   /* if a compound is opened, we simply parse the statement directly.  */
6840   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6841     statement = cp_parser_compound_statement (parser, NULL, false);
6842   /* If the token is not a `{', then we must take special action.  */
6843   else
6844     {
6845       /* Create a compound-statement.  */
6846       statement = begin_compound_stmt (0);
6847       /* Parse the dependent-statement.  */
6848       cp_parser_statement (parser, NULL_TREE, false);
6849       /* Finish the dummy compound-statement.  */
6850       finish_compound_stmt (statement);
6851     }
6852
6853   /* Return the statement.  */
6854   return statement;
6855 }
6856
6857 /* For some dependent statements (like `while (cond) statement'), we
6858    have already created a scope.  Therefore, even if the dependent
6859    statement is a compound-statement, we do not want to create another
6860    scope.  */
6861
6862 static void
6863 cp_parser_already_scoped_statement (cp_parser* parser)
6864 {
6865   /* If the token is a `{', then we must take special action.  */
6866   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6867     cp_parser_statement (parser, NULL_TREE, false);
6868   else
6869     {
6870       /* Avoid calling cp_parser_compound_statement, so that we
6871          don't create a new scope.  Do everything else by hand.  */
6872       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6873       cp_parser_statement_seq_opt (parser, NULL_TREE);
6874       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6875     }
6876 }
6877
6878 /* Declarations [gram.dcl.dcl] */
6879
6880 /* Parse an optional declaration-sequence.
6881
6882    declaration-seq:
6883      declaration
6884      declaration-seq declaration  */
6885
6886 static void
6887 cp_parser_declaration_seq_opt (cp_parser* parser)
6888 {
6889   while (true)
6890     {
6891       cp_token *token;
6892
6893       token = cp_lexer_peek_token (parser->lexer);
6894
6895       if (token->type == CPP_CLOSE_BRACE
6896           || token->type == CPP_EOF
6897           || token->type == CPP_PRAGMA_EOL)
6898         break;
6899
6900       if (token->type == CPP_SEMICOLON)
6901         {
6902           /* A declaration consisting of a single semicolon is
6903              invalid.  Allow it unless we're being pedantic.  */
6904           cp_lexer_consume_token (parser->lexer);
6905           if (pedantic && !in_system_header)
6906             pedwarn ("extra %<;%>");
6907           continue;
6908         }
6909
6910       /* If we're entering or exiting a region that's implicitly
6911          extern "C", modify the lang context appropriately.  */
6912       if (!parser->implicit_extern_c && token->implicit_extern_c)
6913         {
6914           push_lang_context (lang_name_c);
6915           parser->implicit_extern_c = true;
6916         }
6917       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6918         {
6919           pop_lang_context ();
6920           parser->implicit_extern_c = false;
6921         }
6922
6923       if (token->type == CPP_PRAGMA)
6924         {
6925           /* A top-level declaration can consist solely of a #pragma.
6926              A nested declaration cannot, so this is done here and not
6927              in cp_parser_declaration.  (A #pragma at block scope is
6928              handled in cp_parser_statement.)  */
6929           cp_parser_pragma (parser, pragma_external);
6930           continue;
6931         }
6932
6933       /* Parse the declaration itself.  */
6934       cp_parser_declaration (parser);
6935     }
6936 }
6937
6938 /* Parse a declaration.
6939
6940    declaration:
6941      block-declaration
6942      function-definition
6943      template-declaration
6944      explicit-instantiation
6945      explicit-specialization
6946      linkage-specification
6947      namespace-definition
6948
6949    GNU extension:
6950
6951    declaration:
6952       __extension__ declaration */
6953
6954 static void
6955 cp_parser_declaration (cp_parser* parser)
6956 {
6957   cp_token token1;
6958   cp_token token2;
6959   int saved_pedantic;
6960   void *p;
6961
6962   /* Check for the `__extension__' keyword.  */
6963   if (cp_parser_extension_opt (parser, &saved_pedantic))
6964     {
6965       /* Parse the qualified declaration.  */
6966       cp_parser_declaration (parser);
6967       /* Restore the PEDANTIC flag.  */
6968       pedantic = saved_pedantic;
6969
6970       return;
6971     }
6972
6973   /* Try to figure out what kind of declaration is present.  */
6974   token1 = *cp_lexer_peek_token (parser->lexer);
6975
6976   if (token1.type != CPP_EOF)
6977     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6978   else
6979     {
6980       token2.type = CPP_EOF;
6981       token2.keyword = RID_MAX;
6982     }
6983
6984   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6985   p = obstack_alloc (&declarator_obstack, 0);
6986
6987   /* If the next token is `extern' and the following token is a string
6988      literal, then we have a linkage specification.  */
6989   if (token1.keyword == RID_EXTERN
6990       && cp_parser_is_string_literal (&token2))
6991     cp_parser_linkage_specification (parser);
6992   /* If the next token is `template', then we have either a template
6993      declaration, an explicit instantiation, or an explicit
6994      specialization.  */
6995   else if (token1.keyword == RID_TEMPLATE)
6996     {
6997       /* `template <>' indicates a template specialization.  */
6998       if (token2.type == CPP_LESS
6999           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7000         cp_parser_explicit_specialization (parser);
7001       /* `template <' indicates a template declaration.  */
7002       else if (token2.type == CPP_LESS)
7003         cp_parser_template_declaration (parser, /*member_p=*/false);
7004       /* Anything else must be an explicit instantiation.  */
7005       else
7006         cp_parser_explicit_instantiation (parser);
7007     }
7008   /* If the next token is `export', then we have a template
7009      declaration.  */
7010   else if (token1.keyword == RID_EXPORT)
7011     cp_parser_template_declaration (parser, /*member_p=*/false);
7012   /* If the next token is `extern', 'static' or 'inline' and the one
7013      after that is `template', we have a GNU extended explicit
7014      instantiation directive.  */
7015   else if (cp_parser_allow_gnu_extensions_p (parser)
7016            && (token1.keyword == RID_EXTERN
7017                || token1.keyword == RID_STATIC
7018                || token1.keyword == RID_INLINE)
7019            && token2.keyword == RID_TEMPLATE)
7020     cp_parser_explicit_instantiation (parser);
7021   /* If the next token is `namespace', check for a named or unnamed
7022      namespace definition.  */
7023   else if (token1.keyword == RID_NAMESPACE
7024            && (/* A named namespace definition.  */
7025                (token2.type == CPP_NAME
7026                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7027                     == CPP_OPEN_BRACE))
7028                /* An unnamed namespace definition.  */
7029                || token2.type == CPP_OPEN_BRACE))
7030     cp_parser_namespace_definition (parser);
7031   /* Objective-C++ declaration/definition.  */
7032   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7033     cp_parser_objc_declaration (parser);
7034   /* We must have either a block declaration or a function
7035      definition.  */
7036   else
7037     /* Try to parse a block-declaration, or a function-definition.  */
7038     cp_parser_block_declaration (parser, /*statement_p=*/false);
7039
7040   /* Free any declarators allocated.  */
7041   obstack_free (&declarator_obstack, p);
7042 }
7043
7044 /* Parse a block-declaration.
7045
7046    block-declaration:
7047      simple-declaration
7048      asm-definition
7049      namespace-alias-definition
7050      using-declaration
7051      using-directive
7052
7053    GNU Extension:
7054
7055    block-declaration:
7056      __extension__ block-declaration
7057      label-declaration
7058
7059    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7060    part of a declaration-statement.  */
7061
7062 static void
7063 cp_parser_block_declaration (cp_parser *parser,
7064                              bool      statement_p)
7065 {
7066   cp_token *token1;
7067   int saved_pedantic;
7068
7069   /* Check for the `__extension__' keyword.  */
7070   if (cp_parser_extension_opt (parser, &saved_pedantic))
7071     {
7072       /* Parse the qualified declaration.  */
7073       cp_parser_block_declaration (parser, statement_p);
7074       /* Restore the PEDANTIC flag.  */
7075       pedantic = saved_pedantic;
7076
7077       return;
7078     }
7079
7080   /* Peek at the next token to figure out which kind of declaration is
7081      present.  */
7082   token1 = cp_lexer_peek_token (parser->lexer);
7083
7084   /* If the next keyword is `asm', we have an asm-definition.  */
7085   if (token1->keyword == RID_ASM)
7086     {
7087       if (statement_p)
7088         cp_parser_commit_to_tentative_parse (parser);
7089       cp_parser_asm_definition (parser);
7090     }
7091   /* If the next keyword is `namespace', we have a
7092      namespace-alias-definition.  */
7093   else if (token1->keyword == RID_NAMESPACE)
7094     cp_parser_namespace_alias_definition (parser);
7095   /* If the next keyword is `using', we have either a
7096      using-declaration or a using-directive.  */
7097   else if (token1->keyword == RID_USING)
7098     {
7099       cp_token *token2;
7100
7101       if (statement_p)
7102         cp_parser_commit_to_tentative_parse (parser);
7103       /* If the token after `using' is `namespace', then we have a
7104          using-directive.  */
7105       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7106       if (token2->keyword == RID_NAMESPACE)
7107         cp_parser_using_directive (parser);
7108       /* Otherwise, it's a using-declaration.  */
7109       else
7110         cp_parser_using_declaration (parser);
7111     }
7112   /* If the next keyword is `__label__' we have a label declaration.  */
7113   else if (token1->keyword == RID_LABEL)
7114     {
7115       if (statement_p)
7116         cp_parser_commit_to_tentative_parse (parser);
7117       cp_parser_label_declaration (parser);
7118     }
7119   /* Anything else must be a simple-declaration.  */
7120   else
7121     cp_parser_simple_declaration (parser, !statement_p);
7122 }
7123
7124 /* Parse a simple-declaration.
7125
7126    simple-declaration:
7127      decl-specifier-seq [opt] init-declarator-list [opt] ;
7128
7129    init-declarator-list:
7130      init-declarator
7131      init-declarator-list , init-declarator
7132
7133    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7134    function-definition as a simple-declaration.  */
7135
7136 static void
7137 cp_parser_simple_declaration (cp_parser* parser,
7138                               bool function_definition_allowed_p)
7139 {
7140   cp_decl_specifier_seq decl_specifiers;
7141   int declares_class_or_enum;
7142   bool saw_declarator;
7143
7144   /* Defer access checks until we know what is being declared; the
7145      checks for names appearing in the decl-specifier-seq should be
7146      done as if we were in the scope of the thing being declared.  */
7147   push_deferring_access_checks (dk_deferred);
7148
7149   /* Parse the decl-specifier-seq.  We have to keep track of whether
7150      or not the decl-specifier-seq declares a named class or
7151      enumeration type, since that is the only case in which the
7152      init-declarator-list is allowed to be empty.
7153
7154      [dcl.dcl]
7155
7156      In a simple-declaration, the optional init-declarator-list can be
7157      omitted only when declaring a class or enumeration, that is when
7158      the decl-specifier-seq contains either a class-specifier, an
7159      elaborated-type-specifier, or an enum-specifier.  */
7160   cp_parser_decl_specifier_seq (parser,
7161                                 CP_PARSER_FLAGS_OPTIONAL,
7162                                 &decl_specifiers,
7163                                 &declares_class_or_enum);
7164   /* We no longer need to defer access checks.  */
7165   stop_deferring_access_checks ();
7166
7167   /* In a block scope, a valid declaration must always have a
7168      decl-specifier-seq.  By not trying to parse declarators, we can
7169      resolve the declaration/expression ambiguity more quickly.  */
7170   if (!function_definition_allowed_p
7171       && !decl_specifiers.any_specifiers_p)
7172     {
7173       cp_parser_error (parser, "expected declaration");
7174       goto done;
7175     }
7176
7177   /* If the next two tokens are both identifiers, the code is
7178      erroneous. The usual cause of this situation is code like:
7179
7180        T t;
7181
7182      where "T" should name a type -- but does not.  */
7183   if (!decl_specifiers.type
7184       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7185     {
7186       /* If parsing tentatively, we should commit; we really are
7187          looking at a declaration.  */
7188       cp_parser_commit_to_tentative_parse (parser);
7189       /* Give up.  */
7190       goto done;
7191     }
7192
7193   /* If we have seen at least one decl-specifier, and the next token
7194      is not a parenthesis, then we must be looking at a declaration.
7195      (After "int (" we might be looking at a functional cast.)  */
7196   if (decl_specifiers.any_specifiers_p
7197       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7198     cp_parser_commit_to_tentative_parse (parser);
7199
7200   /* Keep going until we hit the `;' at the end of the simple
7201      declaration.  */
7202   saw_declarator = false;
7203   while (cp_lexer_next_token_is_not (parser->lexer,
7204                                      CPP_SEMICOLON))
7205     {
7206       cp_token *token;
7207       bool function_definition_p;
7208       tree decl;
7209
7210       if (saw_declarator)
7211         {
7212           /* If we are processing next declarator, coma is expected */
7213           token = cp_lexer_peek_token (parser->lexer);
7214           gcc_assert (token->type == CPP_COMMA);
7215           cp_lexer_consume_token (parser->lexer);
7216         }
7217       else
7218         saw_declarator = true;
7219
7220       /* Parse the init-declarator.  */
7221       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7222                                         function_definition_allowed_p,
7223                                         /*member_p=*/false,
7224                                         declares_class_or_enum,
7225                                         &function_definition_p);
7226       /* If an error occurred while parsing tentatively, exit quickly.
7227          (That usually happens when in the body of a function; each
7228          statement is treated as a declaration-statement until proven
7229          otherwise.)  */
7230       if (cp_parser_error_occurred (parser))
7231         goto done;
7232       /* Handle function definitions specially.  */
7233       if (function_definition_p)
7234         {
7235           /* If the next token is a `,', then we are probably
7236              processing something like:
7237
7238                void f() {}, *p;
7239
7240              which is erroneous.  */
7241           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7242             error ("mixing declarations and function-definitions is forbidden");
7243           /* Otherwise, we're done with the list of declarators.  */
7244           else
7245             {
7246               pop_deferring_access_checks ();
7247               return;
7248             }
7249         }
7250       /* The next token should be either a `,' or a `;'.  */
7251       token = cp_lexer_peek_token (parser->lexer);
7252       /* If it's a `,', there are more declarators to come.  */
7253       if (token->type == CPP_COMMA)
7254         /* will be consumed next time around */;
7255       /* If it's a `;', we are done.  */
7256       else if (token->type == CPP_SEMICOLON)
7257         break;
7258       /* Anything else is an error.  */
7259       else
7260         {
7261           /* If we have already issued an error message we don't need
7262              to issue another one.  */
7263           if (decl != error_mark_node
7264               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7265             cp_parser_error (parser, "expected %<,%> or %<;%>");
7266           /* Skip tokens until we reach the end of the statement.  */
7267           cp_parser_skip_to_end_of_statement (parser);
7268           /* If the next token is now a `;', consume it.  */
7269           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7270             cp_lexer_consume_token (parser->lexer);
7271           goto done;
7272         }
7273       /* After the first time around, a function-definition is not
7274          allowed -- even if it was OK at first.  For example:
7275
7276            int i, f() {}
7277
7278          is not valid.  */
7279       function_definition_allowed_p = false;
7280     }
7281
7282   /* Issue an error message if no declarators are present, and the
7283      decl-specifier-seq does not itself declare a class or
7284      enumeration.  */
7285   if (!saw_declarator)
7286     {
7287       if (cp_parser_declares_only_class_p (parser))
7288         shadow_tag (&decl_specifiers);
7289       /* Perform any deferred access checks.  */
7290       perform_deferred_access_checks ();
7291     }
7292
7293   /* Consume the `;'.  */
7294   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7295
7296  done:
7297   pop_deferring_access_checks ();
7298 }
7299
7300 /* Parse a decl-specifier-seq.
7301
7302    decl-specifier-seq:
7303      decl-specifier-seq [opt] decl-specifier
7304
7305    decl-specifier:
7306      storage-class-specifier
7307      type-specifier
7308      function-specifier
7309      friend
7310      typedef
7311
7312    GNU Extension:
7313
7314    decl-specifier:
7315      attributes
7316
7317    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7318
7319    The parser flags FLAGS is used to control type-specifier parsing.
7320
7321    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7322    flags:
7323
7324      1: one of the decl-specifiers is an elaborated-type-specifier
7325         (i.e., a type declaration)
7326      2: one of the decl-specifiers is an enum-specifier or a
7327         class-specifier (i.e., a type definition)
7328
7329    */
7330
7331 static void
7332 cp_parser_decl_specifier_seq (cp_parser* parser,
7333                               cp_parser_flags flags,
7334                               cp_decl_specifier_seq *decl_specs,
7335                               int* declares_class_or_enum)
7336 {
7337   bool constructor_possible_p = !parser->in_declarator_p;
7338   cp_decl_spec ds;
7339
7340   /* Clear DECL_SPECS.  */
7341   clear_decl_specs (decl_specs);
7342
7343   /* Assume no class or enumeration type is declared.  */
7344   *declares_class_or_enum = 0;
7345
7346   /* Keep reading specifiers until there are no more to read.  */
7347   while (true)
7348     {
7349       bool constructor_p;
7350       bool found_decl_spec;
7351       cp_token *token;
7352
7353       /* Peek at the next token.  */
7354       token = cp_lexer_peek_token (parser->lexer);
7355       /* Handle attributes.  */
7356       if (token->keyword == RID_ATTRIBUTE)
7357         {
7358           /* Parse the attributes.  */
7359           decl_specs->attributes
7360             = chainon (decl_specs->attributes,
7361                        cp_parser_attributes_opt (parser));
7362           continue;
7363         }
7364       /* Assume we will find a decl-specifier keyword.  */
7365       found_decl_spec = true;
7366       /* If the next token is an appropriate keyword, we can simply
7367          add it to the list.  */
7368       switch (token->keyword)
7369         {
7370           /* decl-specifier:
7371                friend  */
7372         case RID_FRIEND:
7373           ++decl_specs->specs[(int) ds_friend];
7374           /* Consume the token.  */
7375           cp_lexer_consume_token (parser->lexer);
7376           break;
7377
7378           /* function-specifier:
7379                inline
7380                virtual
7381                explicit  */
7382         case RID_INLINE:
7383         case RID_VIRTUAL:
7384         case RID_EXPLICIT:
7385           cp_parser_function_specifier_opt (parser, decl_specs);
7386           break;
7387
7388           /* decl-specifier:
7389                typedef  */
7390         case RID_TYPEDEF:
7391           ++decl_specs->specs[(int) ds_typedef];
7392           /* Consume the token.  */
7393           cp_lexer_consume_token (parser->lexer);
7394           /* A constructor declarator cannot appear in a typedef.  */
7395           constructor_possible_p = false;
7396           /* The "typedef" keyword can only occur in a declaration; we
7397              may as well commit at this point.  */
7398           cp_parser_commit_to_tentative_parse (parser);
7399           break;
7400
7401           /* storage-class-specifier:
7402                auto
7403                register
7404                static
7405                extern
7406                mutable
7407
7408              GNU Extension:
7409                thread  */
7410         case RID_AUTO:
7411           /* Consume the token.  */
7412           cp_lexer_consume_token (parser->lexer);
7413           cp_parser_set_storage_class (decl_specs, sc_auto);
7414           break;
7415         case RID_REGISTER:
7416           /* Consume the token.  */
7417           cp_lexer_consume_token (parser->lexer);
7418           cp_parser_set_storage_class (decl_specs, sc_register);
7419           break;
7420         case RID_STATIC:
7421           /* Consume the token.  */
7422           cp_lexer_consume_token (parser->lexer);
7423           if (decl_specs->specs[(int) ds_thread])
7424             {
7425               error ("%<__thread%> before %<static%>");
7426               decl_specs->specs[(int) ds_thread] = 0;
7427             }
7428           cp_parser_set_storage_class (decl_specs, sc_static);
7429           break;
7430         case RID_EXTERN:
7431           /* Consume the token.  */
7432           cp_lexer_consume_token (parser->lexer);
7433           if (decl_specs->specs[(int) ds_thread])
7434             {
7435               error ("%<__thread%> before %<extern%>");
7436               decl_specs->specs[(int) ds_thread] = 0;
7437             }
7438           cp_parser_set_storage_class (decl_specs, sc_extern);
7439           break;
7440         case RID_MUTABLE:
7441           /* Consume the token.  */
7442           cp_lexer_consume_token (parser->lexer);
7443           cp_parser_set_storage_class (decl_specs, sc_mutable);
7444           break;
7445         case RID_THREAD:
7446           /* Consume the token.  */
7447           cp_lexer_consume_token (parser->lexer);
7448           ++decl_specs->specs[(int) ds_thread];
7449           break;
7450
7451         default:
7452           /* We did not yet find a decl-specifier yet.  */
7453           found_decl_spec = false;
7454           break;
7455         }
7456
7457       /* Constructors are a special case.  The `S' in `S()' is not a
7458          decl-specifier; it is the beginning of the declarator.  */
7459       constructor_p
7460         = (!found_decl_spec
7461            && constructor_possible_p
7462            && (cp_parser_constructor_declarator_p
7463                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7464
7465       /* If we don't have a DECL_SPEC yet, then we must be looking at
7466          a type-specifier.  */
7467       if (!found_decl_spec && !constructor_p)
7468         {
7469           int decl_spec_declares_class_or_enum;
7470           bool is_cv_qualifier;
7471           tree type_spec;
7472
7473           type_spec
7474             = cp_parser_type_specifier (parser, flags,
7475                                         decl_specs,
7476                                         /*is_declaration=*/true,
7477                                         &decl_spec_declares_class_or_enum,
7478                                         &is_cv_qualifier);
7479
7480           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7481
7482           /* If this type-specifier referenced a user-defined type
7483              (a typedef, class-name, etc.), then we can't allow any
7484              more such type-specifiers henceforth.
7485
7486              [dcl.spec]
7487
7488              The longest sequence of decl-specifiers that could
7489              possibly be a type name is taken as the
7490              decl-specifier-seq of a declaration.  The sequence shall
7491              be self-consistent as described below.
7492
7493              [dcl.type]
7494
7495              As a general rule, at most one type-specifier is allowed
7496              in the complete decl-specifier-seq of a declaration.  The
7497              only exceptions are the following:
7498
7499              -- const or volatile can be combined with any other
7500                 type-specifier.
7501
7502              -- signed or unsigned can be combined with char, long,
7503                 short, or int.
7504
7505              -- ..
7506
7507              Example:
7508
7509                typedef char* Pc;
7510                void g (const int Pc);
7511
7512              Here, Pc is *not* part of the decl-specifier seq; it's
7513              the declarator.  Therefore, once we see a type-specifier
7514              (other than a cv-qualifier), we forbid any additional
7515              user-defined types.  We *do* still allow things like `int
7516              int' to be considered a decl-specifier-seq, and issue the
7517              error message later.  */
7518           if (type_spec && !is_cv_qualifier)
7519             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7520           /* A constructor declarator cannot follow a type-specifier.  */
7521           if (type_spec)
7522             {
7523               constructor_possible_p = false;
7524               found_decl_spec = true;
7525             }
7526         }
7527
7528       /* If we still do not have a DECL_SPEC, then there are no more
7529          decl-specifiers.  */
7530       if (!found_decl_spec)
7531         break;
7532
7533       decl_specs->any_specifiers_p = true;
7534       /* After we see one decl-specifier, further decl-specifiers are
7535          always optional.  */
7536       flags |= CP_PARSER_FLAGS_OPTIONAL;
7537     }
7538
7539   /* Check for repeated decl-specifiers.  */
7540   for (ds = ds_first; ds != ds_last; ++ds)
7541     {
7542       unsigned count = decl_specs->specs[(int)ds];
7543       if (count < 2)
7544         continue;
7545       /* The "long" specifier is a special case because of "long long".  */
7546       if (ds == ds_long)
7547         {
7548           if (count > 2)
7549             error ("%<long long long%> is too long for GCC");
7550           else if (pedantic && !in_system_header && warn_long_long)
7551             pedwarn ("ISO C++ does not support %<long long%>");
7552         }
7553       else if (count > 1)
7554         {
7555           static const char *const decl_spec_names[] = {
7556             "signed",
7557             "unsigned",
7558             "short",
7559             "long",
7560             "const",
7561             "volatile",
7562             "restrict",
7563             "inline",
7564             "virtual",
7565             "explicit",
7566             "friend",
7567             "typedef",
7568             "__complex",
7569             "__thread"
7570           };
7571           error ("duplicate %qs", decl_spec_names[(int)ds]);
7572         }
7573     }
7574
7575   /* Don't allow a friend specifier with a class definition.  */
7576   if (decl_specs->specs[(int) ds_friend] != 0
7577       && (*declares_class_or_enum & 2))
7578     error ("class definition may not be declared a friend");
7579 }
7580
7581 /* Parse an (optional) storage-class-specifier.
7582
7583    storage-class-specifier:
7584      auto
7585      register
7586      static
7587      extern
7588      mutable
7589
7590    GNU Extension:
7591
7592    storage-class-specifier:
7593      thread
7594
7595    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7596
7597 static tree
7598 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7599 {
7600   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7601     {
7602     case RID_AUTO:
7603     case RID_REGISTER:
7604     case RID_STATIC:
7605     case RID_EXTERN:
7606     case RID_MUTABLE:
7607     case RID_THREAD:
7608       /* Consume the token.  */
7609       return cp_lexer_consume_token (parser->lexer)->value;
7610
7611     default:
7612       return NULL_TREE;
7613     }
7614 }
7615
7616 /* Parse an (optional) function-specifier.
7617
7618    function-specifier:
7619      inline
7620      virtual
7621      explicit
7622
7623    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7624    Updates DECL_SPECS, if it is non-NULL.  */
7625
7626 static tree
7627 cp_parser_function_specifier_opt (cp_parser* parser,
7628                                   cp_decl_specifier_seq *decl_specs)
7629 {
7630   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7631     {
7632     case RID_INLINE:
7633       if (decl_specs)
7634         ++decl_specs->specs[(int) ds_inline];
7635       break;
7636
7637     case RID_VIRTUAL:
7638       if (decl_specs)
7639         ++decl_specs->specs[(int) ds_virtual];
7640       break;
7641
7642     case RID_EXPLICIT:
7643       if (decl_specs)
7644         ++decl_specs->specs[(int) ds_explicit];
7645       break;
7646
7647     default:
7648       return NULL_TREE;
7649     }
7650
7651   /* Consume the token.  */
7652   return cp_lexer_consume_token (parser->lexer)->value;
7653 }
7654
7655 /* Parse a linkage-specification.
7656
7657    linkage-specification:
7658      extern string-literal { declaration-seq [opt] }
7659      extern string-literal declaration  */
7660
7661 static void
7662 cp_parser_linkage_specification (cp_parser* parser)
7663 {
7664   tree linkage;
7665
7666   /* Look for the `extern' keyword.  */
7667   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7668
7669   /* Look for the string-literal.  */
7670   linkage = cp_parser_string_literal (parser, false, false);
7671
7672   /* Transform the literal into an identifier.  If the literal is a
7673      wide-character string, or contains embedded NULs, then we can't
7674      handle it as the user wants.  */
7675   if (strlen (TREE_STRING_POINTER (linkage))
7676       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7677     {
7678       cp_parser_error (parser, "invalid linkage-specification");
7679       /* Assume C++ linkage.  */
7680       linkage = lang_name_cplusplus;
7681     }
7682   else
7683     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7684
7685   /* We're now using the new linkage.  */
7686   push_lang_context (linkage);
7687
7688   /* If the next token is a `{', then we're using the first
7689      production.  */
7690   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7691     {
7692       /* Consume the `{' token.  */
7693       cp_lexer_consume_token (parser->lexer);
7694       /* Parse the declarations.  */
7695       cp_parser_declaration_seq_opt (parser);
7696       /* Look for the closing `}'.  */
7697       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7698     }
7699   /* Otherwise, there's just one declaration.  */
7700   else
7701     {
7702       bool saved_in_unbraced_linkage_specification_p;
7703
7704       saved_in_unbraced_linkage_specification_p
7705         = parser->in_unbraced_linkage_specification_p;
7706       parser->in_unbraced_linkage_specification_p = true;
7707       have_extern_spec = true;
7708       cp_parser_declaration (parser);
7709       have_extern_spec = false;
7710       parser->in_unbraced_linkage_specification_p
7711         = saved_in_unbraced_linkage_specification_p;
7712     }
7713
7714   /* We're done with the linkage-specification.  */
7715   pop_lang_context ();
7716 }
7717
7718 /* Special member functions [gram.special] */
7719
7720 /* Parse a conversion-function-id.
7721
7722    conversion-function-id:
7723      operator conversion-type-id
7724
7725    Returns an IDENTIFIER_NODE representing the operator.  */
7726
7727 static tree
7728 cp_parser_conversion_function_id (cp_parser* parser)
7729 {
7730   tree type;
7731   tree saved_scope;
7732   tree saved_qualifying_scope;
7733   tree saved_object_scope;
7734   tree pushed_scope = NULL_TREE;
7735
7736   /* Look for the `operator' token.  */
7737   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7738     return error_mark_node;
7739   /* When we parse the conversion-type-id, the current scope will be
7740      reset.  However, we need that information in able to look up the
7741      conversion function later, so we save it here.  */
7742   saved_scope = parser->scope;
7743   saved_qualifying_scope = parser->qualifying_scope;
7744   saved_object_scope = parser->object_scope;
7745   /* We must enter the scope of the class so that the names of
7746      entities declared within the class are available in the
7747      conversion-type-id.  For example, consider:
7748
7749        struct S {
7750          typedef int I;
7751          operator I();
7752        };
7753
7754        S::operator I() { ... }
7755
7756      In order to see that `I' is a type-name in the definition, we
7757      must be in the scope of `S'.  */
7758   if (saved_scope)
7759     pushed_scope = push_scope (saved_scope);
7760   /* Parse the conversion-type-id.  */
7761   type = cp_parser_conversion_type_id (parser);
7762   /* Leave the scope of the class, if any.  */
7763   if (pushed_scope)
7764     pop_scope (pushed_scope);
7765   /* Restore the saved scope.  */
7766   parser->scope = saved_scope;
7767   parser->qualifying_scope = saved_qualifying_scope;
7768   parser->object_scope = saved_object_scope;
7769   /* If the TYPE is invalid, indicate failure.  */
7770   if (type == error_mark_node)
7771     return error_mark_node;
7772   return mangle_conv_op_name_for_type (type);
7773 }
7774
7775 /* Parse a conversion-type-id:
7776
7777    conversion-type-id:
7778      type-specifier-seq conversion-declarator [opt]
7779
7780    Returns the TYPE specified.  */
7781
7782 static tree
7783 cp_parser_conversion_type_id (cp_parser* parser)
7784 {
7785   tree attributes;
7786   cp_decl_specifier_seq type_specifiers;
7787   cp_declarator *declarator;
7788   tree type_specified;
7789
7790   /* Parse the attributes.  */
7791   attributes = cp_parser_attributes_opt (parser);
7792   /* Parse the type-specifiers.  */
7793   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7794                                 &type_specifiers);
7795   /* If that didn't work, stop.  */
7796   if (type_specifiers.type == error_mark_node)
7797     return error_mark_node;
7798   /* Parse the conversion-declarator.  */
7799   declarator = cp_parser_conversion_declarator_opt (parser);
7800
7801   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7802                                     /*initialized=*/0, &attributes);
7803   if (attributes)
7804     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7805   return type_specified;
7806 }
7807
7808 /* Parse an (optional) conversion-declarator.
7809
7810    conversion-declarator:
7811      ptr-operator conversion-declarator [opt]
7812
7813    */
7814
7815 static cp_declarator *
7816 cp_parser_conversion_declarator_opt (cp_parser* parser)
7817 {
7818   enum tree_code code;
7819   tree class_type;
7820   cp_cv_quals cv_quals;
7821
7822   /* We don't know if there's a ptr-operator next, or not.  */
7823   cp_parser_parse_tentatively (parser);
7824   /* Try the ptr-operator.  */
7825   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7826   /* If it worked, look for more conversion-declarators.  */
7827   if (cp_parser_parse_definitely (parser))
7828     {
7829       cp_declarator *declarator;
7830
7831       /* Parse another optional declarator.  */
7832       declarator = cp_parser_conversion_declarator_opt (parser);
7833
7834       /* Create the representation of the declarator.  */
7835       if (class_type)
7836         declarator = make_ptrmem_declarator (cv_quals, class_type,
7837                                              declarator);
7838       else if (code == INDIRECT_REF)
7839         declarator = make_pointer_declarator (cv_quals, declarator);
7840       else
7841         declarator = make_reference_declarator (cv_quals, declarator);
7842
7843       return declarator;
7844    }
7845
7846   return NULL;
7847 }
7848
7849 /* Parse an (optional) ctor-initializer.
7850
7851    ctor-initializer:
7852      : mem-initializer-list
7853
7854    Returns TRUE iff the ctor-initializer was actually present.  */
7855
7856 static bool
7857 cp_parser_ctor_initializer_opt (cp_parser* parser)
7858 {
7859   /* If the next token is not a `:', then there is no
7860      ctor-initializer.  */
7861   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7862     {
7863       /* Do default initialization of any bases and members.  */
7864       if (DECL_CONSTRUCTOR_P (current_function_decl))
7865         finish_mem_initializers (NULL_TREE);
7866
7867       return false;
7868     }
7869
7870   /* Consume the `:' token.  */
7871   cp_lexer_consume_token (parser->lexer);
7872   /* And the mem-initializer-list.  */
7873   cp_parser_mem_initializer_list (parser);
7874
7875   return true;
7876 }
7877
7878 /* Parse a mem-initializer-list.
7879
7880    mem-initializer-list:
7881      mem-initializer
7882      mem-initializer , mem-initializer-list  */
7883
7884 static void
7885 cp_parser_mem_initializer_list (cp_parser* parser)
7886 {
7887   tree mem_initializer_list = NULL_TREE;
7888
7889   /* Let the semantic analysis code know that we are starting the
7890      mem-initializer-list.  */
7891   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7892     error ("only constructors take base initializers");
7893
7894   /* Loop through the list.  */
7895   while (true)
7896     {
7897       tree mem_initializer;
7898
7899       /* Parse the mem-initializer.  */
7900       mem_initializer = cp_parser_mem_initializer (parser);
7901       /* Add it to the list, unless it was erroneous.  */
7902       if (mem_initializer != error_mark_node)
7903         {
7904           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7905           mem_initializer_list = mem_initializer;
7906         }
7907       /* If the next token is not a `,', we're done.  */
7908       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7909         break;
7910       /* Consume the `,' token.  */
7911       cp_lexer_consume_token (parser->lexer);
7912     }
7913
7914   /* Perform semantic analysis.  */
7915   if (DECL_CONSTRUCTOR_P (current_function_decl))
7916     finish_mem_initializers (mem_initializer_list);
7917 }
7918
7919 /* Parse a mem-initializer.
7920
7921    mem-initializer:
7922      mem-initializer-id ( expression-list [opt] )
7923
7924    GNU extension:
7925
7926    mem-initializer:
7927      ( expression-list [opt] )
7928
7929    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7930    class) or FIELD_DECL (for a non-static data member) to initialize;
7931    the TREE_VALUE is the expression-list.  An empty initialization
7932    list is represented by void_list_node.  */
7933
7934 static tree
7935 cp_parser_mem_initializer (cp_parser* parser)
7936 {
7937   tree mem_initializer_id;
7938   tree expression_list;
7939   tree member;
7940
7941   /* Find out what is being initialized.  */
7942   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7943     {
7944       pedwarn ("anachronistic old-style base class initializer");
7945       mem_initializer_id = NULL_TREE;
7946     }
7947   else
7948     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7949   member = expand_member_init (mem_initializer_id);
7950   if (member && !DECL_P (member))
7951     in_base_initializer = 1;
7952
7953   expression_list
7954     = cp_parser_parenthesized_expression_list (parser, false,
7955                                                /*cast_p=*/false,
7956                                                /*non_constant_p=*/NULL);
7957   if (expression_list == error_mark_node)
7958     return error_mark_node;
7959   if (!expression_list)
7960     expression_list = void_type_node;
7961
7962   in_base_initializer = 0;
7963
7964   return member ? build_tree_list (member, expression_list) : error_mark_node;
7965 }
7966
7967 /* Parse a mem-initializer-id.
7968
7969    mem-initializer-id:
7970      :: [opt] nested-name-specifier [opt] class-name
7971      identifier
7972
7973    Returns a TYPE indicating the class to be initializer for the first
7974    production.  Returns an IDENTIFIER_NODE indicating the data member
7975    to be initialized for the second production.  */
7976
7977 static tree
7978 cp_parser_mem_initializer_id (cp_parser* parser)
7979 {
7980   bool global_scope_p;
7981   bool nested_name_specifier_p;
7982   bool template_p = false;
7983   tree id;
7984
7985   /* `typename' is not allowed in this context ([temp.res]).  */
7986   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7987     {
7988       error ("keyword %<typename%> not allowed in this context (a qualified "
7989              "member initializer is implicitly a type)");
7990       cp_lexer_consume_token (parser->lexer);
7991     }
7992   /* Look for the optional `::' operator.  */
7993   global_scope_p
7994     = (cp_parser_global_scope_opt (parser,
7995                                    /*current_scope_valid_p=*/false)
7996        != NULL_TREE);
7997   /* Look for the optional nested-name-specifier.  The simplest way to
7998      implement:
7999
8000        [temp.res]
8001
8002        The keyword `typename' is not permitted in a base-specifier or
8003        mem-initializer; in these contexts a qualified name that
8004        depends on a template-parameter is implicitly assumed to be a
8005        type name.
8006
8007      is to assume that we have seen the `typename' keyword at this
8008      point.  */
8009   nested_name_specifier_p
8010     = (cp_parser_nested_name_specifier_opt (parser,
8011                                             /*typename_keyword_p=*/true,
8012                                             /*check_dependency_p=*/true,
8013                                             /*type_p=*/true,
8014                                             /*is_declaration=*/true)
8015        != NULL_TREE);
8016   if (nested_name_specifier_p)
8017     template_p = cp_parser_optional_template_keyword (parser);
8018   /* If there is a `::' operator or a nested-name-specifier, then we
8019      are definitely looking for a class-name.  */
8020   if (global_scope_p || nested_name_specifier_p)
8021     return cp_parser_class_name (parser,
8022                                  /*typename_keyword_p=*/true,
8023                                  /*template_keyword_p=*/template_p,
8024                                  none_type,
8025                                  /*check_dependency_p=*/true,
8026                                  /*class_head_p=*/false,
8027                                  /*is_declaration=*/true);
8028   /* Otherwise, we could also be looking for an ordinary identifier.  */
8029   cp_parser_parse_tentatively (parser);
8030   /* Try a class-name.  */
8031   id = cp_parser_class_name (parser,
8032                              /*typename_keyword_p=*/true,
8033                              /*template_keyword_p=*/false,
8034                              none_type,
8035                              /*check_dependency_p=*/true,
8036                              /*class_head_p=*/false,
8037                              /*is_declaration=*/true);
8038   /* If we found one, we're done.  */
8039   if (cp_parser_parse_definitely (parser))
8040     return id;
8041   /* Otherwise, look for an ordinary identifier.  */
8042   return cp_parser_identifier (parser);
8043 }
8044
8045 /* Overloading [gram.over] */
8046
8047 /* Parse an operator-function-id.
8048
8049    operator-function-id:
8050      operator operator
8051
8052    Returns an IDENTIFIER_NODE for the operator which is a
8053    human-readable spelling of the identifier, e.g., `operator +'.  */
8054
8055 static tree
8056 cp_parser_operator_function_id (cp_parser* parser)
8057 {
8058   /* Look for the `operator' keyword.  */
8059   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8060     return error_mark_node;
8061   /* And then the name of the operator itself.  */
8062   return cp_parser_operator (parser);
8063 }
8064
8065 /* Parse an operator.
8066
8067    operator:
8068      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8069      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8070      || ++ -- , ->* -> () []
8071
8072    GNU Extensions:
8073
8074    operator:
8075      <? >? <?= >?=
8076
8077    Returns an IDENTIFIER_NODE for the operator which is a
8078    human-readable spelling of the identifier, e.g., `operator +'.  */
8079
8080 static tree
8081 cp_parser_operator (cp_parser* parser)
8082 {
8083   tree id = NULL_TREE;
8084   cp_token *token;
8085
8086   /* Peek at the next token.  */
8087   token = cp_lexer_peek_token (parser->lexer);
8088   /* Figure out which operator we have.  */
8089   switch (token->type)
8090     {
8091     case CPP_KEYWORD:
8092       {
8093         enum tree_code op;
8094
8095         /* The keyword should be either `new' or `delete'.  */
8096         if (token->keyword == RID_NEW)
8097           op = NEW_EXPR;
8098         else if (token->keyword == RID_DELETE)
8099           op = DELETE_EXPR;
8100         else
8101           break;
8102
8103         /* Consume the `new' or `delete' token.  */
8104         cp_lexer_consume_token (parser->lexer);
8105
8106         /* Peek at the next token.  */
8107         token = cp_lexer_peek_token (parser->lexer);
8108         /* If it's a `[' token then this is the array variant of the
8109            operator.  */
8110         if (token->type == CPP_OPEN_SQUARE)
8111           {
8112             /* Consume the `[' token.  */
8113             cp_lexer_consume_token (parser->lexer);
8114             /* Look for the `]' token.  */
8115             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8116             id = ansi_opname (op == NEW_EXPR
8117                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8118           }
8119         /* Otherwise, we have the non-array variant.  */
8120         else
8121           id = ansi_opname (op);
8122
8123         return id;
8124       }
8125
8126     case CPP_PLUS:
8127       id = ansi_opname (PLUS_EXPR);
8128       break;
8129
8130     case CPP_MINUS:
8131       id = ansi_opname (MINUS_EXPR);
8132       break;
8133
8134     case CPP_MULT:
8135       id = ansi_opname (MULT_EXPR);
8136       break;
8137
8138     case CPP_DIV:
8139       id = ansi_opname (TRUNC_DIV_EXPR);
8140       break;
8141
8142     case CPP_MOD:
8143       id = ansi_opname (TRUNC_MOD_EXPR);
8144       break;
8145
8146     case CPP_XOR:
8147       id = ansi_opname (BIT_XOR_EXPR);
8148       break;
8149
8150     case CPP_AND:
8151       id = ansi_opname (BIT_AND_EXPR);
8152       break;
8153
8154     case CPP_OR:
8155       id = ansi_opname (BIT_IOR_EXPR);
8156       break;
8157
8158     case CPP_COMPL:
8159       id = ansi_opname (BIT_NOT_EXPR);
8160       break;
8161
8162     case CPP_NOT:
8163       id = ansi_opname (TRUTH_NOT_EXPR);
8164       break;
8165
8166     case CPP_EQ:
8167       id = ansi_assopname (NOP_EXPR);
8168       break;
8169
8170     case CPP_LESS:
8171       id = ansi_opname (LT_EXPR);
8172       break;
8173
8174     case CPP_GREATER:
8175       id = ansi_opname (GT_EXPR);
8176       break;
8177
8178     case CPP_PLUS_EQ:
8179       id = ansi_assopname (PLUS_EXPR);
8180       break;
8181
8182     case CPP_MINUS_EQ:
8183       id = ansi_assopname (MINUS_EXPR);
8184       break;
8185
8186     case CPP_MULT_EQ:
8187       id = ansi_assopname (MULT_EXPR);
8188       break;
8189
8190     case CPP_DIV_EQ:
8191       id = ansi_assopname (TRUNC_DIV_EXPR);
8192       break;
8193
8194     case CPP_MOD_EQ:
8195       id = ansi_assopname (TRUNC_MOD_EXPR);
8196       break;
8197
8198     case CPP_XOR_EQ:
8199       id = ansi_assopname (BIT_XOR_EXPR);
8200       break;
8201
8202     case CPP_AND_EQ:
8203       id = ansi_assopname (BIT_AND_EXPR);
8204       break;
8205
8206     case CPP_OR_EQ:
8207       id = ansi_assopname (BIT_IOR_EXPR);
8208       break;
8209
8210     case CPP_LSHIFT:
8211       id = ansi_opname (LSHIFT_EXPR);
8212       break;
8213
8214     case CPP_RSHIFT:
8215       id = ansi_opname (RSHIFT_EXPR);
8216       break;
8217
8218     case CPP_LSHIFT_EQ:
8219       id = ansi_assopname (LSHIFT_EXPR);
8220       break;
8221
8222     case CPP_RSHIFT_EQ:
8223       id = ansi_assopname (RSHIFT_EXPR);
8224       break;
8225
8226     case CPP_EQ_EQ:
8227       id = ansi_opname (EQ_EXPR);
8228       break;
8229
8230     case CPP_NOT_EQ:
8231       id = ansi_opname (NE_EXPR);
8232       break;
8233
8234     case CPP_LESS_EQ:
8235       id = ansi_opname (LE_EXPR);
8236       break;
8237
8238     case CPP_GREATER_EQ:
8239       id = ansi_opname (GE_EXPR);
8240       break;
8241
8242     case CPP_AND_AND:
8243       id = ansi_opname (TRUTH_ANDIF_EXPR);
8244       break;
8245
8246     case CPP_OR_OR:
8247       id = ansi_opname (TRUTH_ORIF_EXPR);
8248       break;
8249
8250     case CPP_PLUS_PLUS:
8251       id = ansi_opname (POSTINCREMENT_EXPR);
8252       break;
8253
8254     case CPP_MINUS_MINUS:
8255       id = ansi_opname (PREDECREMENT_EXPR);
8256       break;
8257
8258     case CPP_COMMA:
8259       id = ansi_opname (COMPOUND_EXPR);
8260       break;
8261
8262     case CPP_DEREF_STAR:
8263       id = ansi_opname (MEMBER_REF);
8264       break;
8265
8266     case CPP_DEREF:
8267       id = ansi_opname (COMPONENT_REF);
8268       break;
8269
8270     case CPP_OPEN_PAREN:
8271       /* Consume the `('.  */
8272       cp_lexer_consume_token (parser->lexer);
8273       /* Look for the matching `)'.  */
8274       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8275       return ansi_opname (CALL_EXPR);
8276
8277     case CPP_OPEN_SQUARE:
8278       /* Consume the `['.  */
8279       cp_lexer_consume_token (parser->lexer);
8280       /* Look for the matching `]'.  */
8281       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8282       return ansi_opname (ARRAY_REF);
8283
8284       /* Extensions.  */
8285     case CPP_MIN:
8286       id = ansi_opname (MIN_EXPR);
8287       cp_parser_warn_min_max ();
8288       break;
8289
8290     case CPP_MAX:
8291       id = ansi_opname (MAX_EXPR);
8292       cp_parser_warn_min_max ();
8293       break;
8294
8295     case CPP_MIN_EQ:
8296       id = ansi_assopname (MIN_EXPR);
8297       cp_parser_warn_min_max ();
8298       break;
8299
8300     case CPP_MAX_EQ:
8301       id = ansi_assopname (MAX_EXPR);
8302       cp_parser_warn_min_max ();
8303       break;
8304
8305     default:
8306       /* Anything else is an error.  */
8307       break;
8308     }
8309
8310   /* If we have selected an identifier, we need to consume the
8311      operator token.  */
8312   if (id)
8313     cp_lexer_consume_token (parser->lexer);
8314   /* Otherwise, no valid operator name was present.  */
8315   else
8316     {
8317       cp_parser_error (parser, "expected operator");
8318       id = error_mark_node;
8319     }
8320
8321   return id;
8322 }
8323
8324 /* Parse a template-declaration.
8325
8326    template-declaration:
8327      export [opt] template < template-parameter-list > declaration
8328
8329    If MEMBER_P is TRUE, this template-declaration occurs within a
8330    class-specifier.
8331
8332    The grammar rule given by the standard isn't correct.  What
8333    is really meant is:
8334
8335    template-declaration:
8336      export [opt] template-parameter-list-seq
8337        decl-specifier-seq [opt] init-declarator [opt] ;
8338      export [opt] template-parameter-list-seq
8339        function-definition
8340
8341    template-parameter-list-seq:
8342      template-parameter-list-seq [opt]
8343      template < template-parameter-list >  */
8344
8345 static void
8346 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8347 {
8348   /* Check for `export'.  */
8349   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8350     {
8351       /* Consume the `export' token.  */
8352       cp_lexer_consume_token (parser->lexer);
8353       /* Warn that we do not support `export'.  */
8354       warning (0, "keyword %<export%> not implemented, and will be ignored");
8355     }
8356
8357   cp_parser_template_declaration_after_export (parser, member_p);
8358 }
8359
8360 /* Parse a template-parameter-list.
8361
8362    template-parameter-list:
8363      template-parameter
8364      template-parameter-list , template-parameter
8365
8366    Returns a TREE_LIST.  Each node represents a template parameter.
8367    The nodes are connected via their TREE_CHAINs.  */
8368
8369 static tree
8370 cp_parser_template_parameter_list (cp_parser* parser)
8371 {
8372   tree parameter_list = NULL_TREE;
8373
8374   begin_template_parm_list ();
8375   while (true)
8376     {
8377       tree parameter;
8378       cp_token *token;
8379       bool is_non_type;
8380
8381       /* Parse the template-parameter.  */
8382       parameter = cp_parser_template_parameter (parser, &is_non_type);
8383       /* Add it to the list.  */
8384       if (parameter != error_mark_node)
8385         parameter_list = process_template_parm (parameter_list,
8386                                                 parameter,
8387                                                 is_non_type);
8388       /* Peek at the next token.  */
8389       token = cp_lexer_peek_token (parser->lexer);
8390       /* If it's not a `,', we're done.  */
8391       if (token->type != CPP_COMMA)
8392         break;
8393       /* Otherwise, consume the `,' token.  */
8394       cp_lexer_consume_token (parser->lexer);
8395     }
8396
8397   return end_template_parm_list (parameter_list);
8398 }
8399
8400 /* Parse a template-parameter.
8401
8402    template-parameter:
8403      type-parameter
8404      parameter-declaration
8405
8406    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8407    the parameter.  The TREE_PURPOSE is the default value, if any.
8408    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8409    iff this parameter is a non-type parameter.  */
8410
8411 static tree
8412 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8413 {
8414   cp_token *token;
8415   cp_parameter_declarator *parameter_declarator;
8416   tree parm;
8417
8418   /* Assume it is a type parameter or a template parameter.  */
8419   *is_non_type = false;
8420   /* Peek at the next token.  */
8421   token = cp_lexer_peek_token (parser->lexer);
8422   /* If it is `class' or `template', we have a type-parameter.  */
8423   if (token->keyword == RID_TEMPLATE)
8424     return cp_parser_type_parameter (parser);
8425   /* If it is `class' or `typename' we do not know yet whether it is a
8426      type parameter or a non-type parameter.  Consider:
8427
8428        template <typename T, typename T::X X> ...
8429
8430      or:
8431
8432        template <class C, class D*> ...
8433
8434      Here, the first parameter is a type parameter, and the second is
8435      a non-type parameter.  We can tell by looking at the token after
8436      the identifier -- if it is a `,', `=', or `>' then we have a type
8437      parameter.  */
8438   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8439     {
8440       /* Peek at the token after `class' or `typename'.  */
8441       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8442       /* If it's an identifier, skip it.  */
8443       if (token->type == CPP_NAME)
8444         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8445       /* Now, see if the token looks like the end of a template
8446          parameter.  */
8447       if (token->type == CPP_COMMA
8448           || token->type == CPP_EQ
8449           || token->type == CPP_GREATER)
8450         return cp_parser_type_parameter (parser);
8451     }
8452
8453   /* Otherwise, it is a non-type parameter.
8454
8455      [temp.param]
8456
8457      When parsing a default template-argument for a non-type
8458      template-parameter, the first non-nested `>' is taken as the end
8459      of the template parameter-list rather than a greater-than
8460      operator.  */
8461   *is_non_type = true;
8462   parameter_declarator
8463      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8464                                         /*parenthesized_p=*/NULL);
8465   parm = grokdeclarator (parameter_declarator->declarator,
8466                          &parameter_declarator->decl_specifiers,
8467                          PARM, /*initialized=*/0,
8468                          /*attrlist=*/NULL);
8469   if (parm == error_mark_node)
8470     return error_mark_node;
8471   return build_tree_list (parameter_declarator->default_argument, parm);
8472 }
8473
8474 /* Parse a type-parameter.
8475
8476    type-parameter:
8477      class identifier [opt]
8478      class identifier [opt] = type-id
8479      typename identifier [opt]
8480      typename identifier [opt] = type-id
8481      template < template-parameter-list > class identifier [opt]
8482      template < template-parameter-list > class identifier [opt]
8483        = id-expression
8484
8485    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8486    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8487    the declaration of the parameter.  */
8488
8489 static tree
8490 cp_parser_type_parameter (cp_parser* parser)
8491 {
8492   cp_token *token;
8493   tree parameter;
8494
8495   /* Look for a keyword to tell us what kind of parameter this is.  */
8496   token = cp_parser_require (parser, CPP_KEYWORD,
8497                              "`class', `typename', or `template'");
8498   if (!token)
8499     return error_mark_node;
8500
8501   switch (token->keyword)
8502     {
8503     case RID_CLASS:
8504     case RID_TYPENAME:
8505       {
8506         tree identifier;
8507         tree default_argument;
8508
8509         /* If the next token is an identifier, then it names the
8510            parameter.  */
8511         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8512           identifier = cp_parser_identifier (parser);
8513         else
8514           identifier = NULL_TREE;
8515
8516         /* Create the parameter.  */
8517         parameter = finish_template_type_parm (class_type_node, identifier);
8518
8519         /* If the next token is an `=', we have a default argument.  */
8520         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8521           {
8522             /* Consume the `=' token.  */
8523             cp_lexer_consume_token (parser->lexer);
8524             /* Parse the default-argument.  */
8525             default_argument = cp_parser_type_id (parser);
8526           }
8527         else
8528           default_argument = NULL_TREE;
8529
8530         /* Create the combined representation of the parameter and the
8531            default argument.  */
8532         parameter = build_tree_list (default_argument, parameter);
8533       }
8534       break;
8535
8536     case RID_TEMPLATE:
8537       {
8538         tree parameter_list;
8539         tree identifier;
8540         tree default_argument;
8541
8542         /* Look for the `<'.  */
8543         cp_parser_require (parser, CPP_LESS, "`<'");
8544         /* Parse the template-parameter-list.  */
8545         parameter_list = cp_parser_template_parameter_list (parser);
8546         /* Look for the `>'.  */
8547         cp_parser_require (parser, CPP_GREATER, "`>'");
8548         /* Look for the `class' keyword.  */
8549         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8550         /* If the next token is an `=', then there is a
8551            default-argument.  If the next token is a `>', we are at
8552            the end of the parameter-list.  If the next token is a `,',
8553            then we are at the end of this parameter.  */
8554         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8555             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8556             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8557           {
8558             identifier = cp_parser_identifier (parser);
8559             /* Treat invalid names as if the parameter were nameless.  */
8560             if (identifier == error_mark_node)
8561               identifier = NULL_TREE;
8562           }
8563         else
8564           identifier = NULL_TREE;
8565
8566         /* Create the template parameter.  */
8567         parameter = finish_template_template_parm (class_type_node,
8568                                                    identifier);
8569
8570         /* If the next token is an `=', then there is a
8571            default-argument.  */
8572         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8573           {
8574             bool is_template;
8575
8576             /* Consume the `='.  */
8577             cp_lexer_consume_token (parser->lexer);
8578             /* Parse the id-expression.  */
8579             default_argument
8580               = cp_parser_id_expression (parser,
8581                                          /*template_keyword_p=*/false,
8582                                          /*check_dependency_p=*/true,
8583                                          /*template_p=*/&is_template,
8584                                          /*declarator_p=*/false);
8585             if (TREE_CODE (default_argument) == TYPE_DECL)
8586               /* If the id-expression was a template-id that refers to
8587                  a template-class, we already have the declaration here,
8588                  so no further lookup is needed.  */
8589                  ;
8590             else
8591               /* Look up the name.  */
8592               default_argument
8593                 = cp_parser_lookup_name (parser, default_argument,
8594                                          none_type,
8595                                          /*is_template=*/is_template,
8596                                          /*is_namespace=*/false,
8597                                          /*check_dependency=*/true,
8598                                          /*ambiguous_decls=*/NULL);
8599             /* See if the default argument is valid.  */
8600             default_argument
8601               = check_template_template_default_arg (default_argument);
8602           }
8603         else
8604           default_argument = NULL_TREE;
8605
8606         /* Create the combined representation of the parameter and the
8607            default argument.  */
8608         parameter = build_tree_list (default_argument, parameter);
8609       }
8610       break;
8611
8612     default:
8613       gcc_unreachable ();
8614       break;
8615     }
8616
8617   return parameter;
8618 }
8619
8620 /* Parse a template-id.
8621
8622    template-id:
8623      template-name < template-argument-list [opt] >
8624
8625    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8626    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8627    returned.  Otherwise, if the template-name names a function, or set
8628    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8629    names a class, returns a TYPE_DECL for the specialization.
8630
8631    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8632    uninstantiated templates.  */
8633
8634 static tree
8635 cp_parser_template_id (cp_parser *parser,
8636                        bool template_keyword_p,
8637                        bool check_dependency_p,
8638                        bool is_declaration)
8639 {
8640   tree template;
8641   tree arguments;
8642   tree template_id;
8643   cp_token_position start_of_id = 0;
8644   tree access_check = NULL_TREE;
8645   cp_token *next_token, *next_token_2;
8646   bool is_identifier;
8647
8648   /* If the next token corresponds to a template-id, there is no need
8649      to reparse it.  */
8650   next_token = cp_lexer_peek_token (parser->lexer);
8651   if (next_token->type == CPP_TEMPLATE_ID)
8652     {
8653       tree value;
8654       tree check;
8655
8656       /* Get the stored value.  */
8657       value = cp_lexer_consume_token (parser->lexer)->value;
8658       /* Perform any access checks that were deferred.  */
8659       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8660         perform_or_defer_access_check (TREE_PURPOSE (check),
8661                                        TREE_VALUE (check));
8662       /* Return the stored value.  */
8663       return TREE_VALUE (value);
8664     }
8665
8666   /* Avoid performing name lookup if there is no possibility of
8667      finding a template-id.  */
8668   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8669       || (next_token->type == CPP_NAME
8670           && !cp_parser_nth_token_starts_template_argument_list_p
8671                (parser, 2)))
8672     {
8673       cp_parser_error (parser, "expected template-id");
8674       return error_mark_node;
8675     }
8676
8677   /* Remember where the template-id starts.  */
8678   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8679     start_of_id = cp_lexer_token_position (parser->lexer, false);
8680
8681   push_deferring_access_checks (dk_deferred);
8682
8683   /* Parse the template-name.  */
8684   is_identifier = false;
8685   template = cp_parser_template_name (parser, template_keyword_p,
8686                                       check_dependency_p,
8687                                       is_declaration,
8688                                       &is_identifier);
8689   if (template == error_mark_node || is_identifier)
8690     {
8691       pop_deferring_access_checks ();
8692       return template;
8693     }
8694
8695   /* If we find the sequence `[:' after a template-name, it's probably
8696      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8697      parse correctly the argument list.  */
8698   next_token = cp_lexer_peek_token (parser->lexer);
8699   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8700   if (next_token->type == CPP_OPEN_SQUARE
8701       && next_token->flags & DIGRAPH
8702       && next_token_2->type == CPP_COLON
8703       && !(next_token_2->flags & PREV_WHITE))
8704     {
8705       cp_parser_parse_tentatively (parser);
8706       /* Change `:' into `::'.  */
8707       next_token_2->type = CPP_SCOPE;
8708       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8709          CPP_LESS.  */
8710       cp_lexer_consume_token (parser->lexer);
8711       /* Parse the arguments.  */
8712       arguments = cp_parser_enclosed_template_argument_list (parser);
8713       if (!cp_parser_parse_definitely (parser))
8714         {
8715           /* If we couldn't parse an argument list, then we revert our changes
8716              and return simply an error. Maybe this is not a template-id
8717              after all.  */
8718           next_token_2->type = CPP_COLON;
8719           cp_parser_error (parser, "expected %<<%>");
8720           pop_deferring_access_checks ();
8721           return error_mark_node;
8722         }
8723       /* Otherwise, emit an error about the invalid digraph, but continue
8724          parsing because we got our argument list.  */
8725       pedwarn ("%<<::%> cannot begin a template-argument list");
8726       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8727               "between %<<%> and %<::%>");
8728       if (!flag_permissive)
8729         {
8730           static bool hint;
8731           if (!hint)
8732             {
8733               inform ("(if you use -fpermissive G++ will accept your code)");
8734               hint = true;
8735             }
8736         }
8737     }
8738   else
8739     {
8740       /* Look for the `<' that starts the template-argument-list.  */
8741       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8742         {
8743           pop_deferring_access_checks ();
8744           return error_mark_node;
8745         }
8746       /* Parse the arguments.  */
8747       arguments = cp_parser_enclosed_template_argument_list (parser);
8748     }
8749
8750   /* Build a representation of the specialization.  */
8751   if (TREE_CODE (template) == IDENTIFIER_NODE)
8752     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8753   else if (DECL_CLASS_TEMPLATE_P (template)
8754            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8755     template_id
8756       = finish_template_type (template, arguments,
8757                               cp_lexer_next_token_is (parser->lexer,
8758                                                       CPP_SCOPE));
8759   else
8760     {
8761       /* If it's not a class-template or a template-template, it should be
8762          a function-template.  */
8763       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8764                    || TREE_CODE (template) == OVERLOAD
8765                    || BASELINK_P (template)));
8766
8767       template_id = lookup_template_function (template, arguments);
8768     }
8769
8770   /* Retrieve any deferred checks.  Do not pop this access checks yet
8771      so the memory will not be reclaimed during token replacing below.  */
8772   access_check = get_deferred_access_checks ();
8773
8774   /* If parsing tentatively, replace the sequence of tokens that makes
8775      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8776      should we re-parse the token stream, we will not have to repeat
8777      the effort required to do the parse, nor will we issue duplicate
8778      error messages about problems during instantiation of the
8779      template.  */
8780   if (start_of_id)
8781     {
8782       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8783
8784       /* Reset the contents of the START_OF_ID token.  */
8785       token->type = CPP_TEMPLATE_ID;
8786       token->value = build_tree_list (access_check, template_id);
8787       token->keyword = RID_MAX;
8788
8789       /* Purge all subsequent tokens.  */
8790       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8791
8792       /* ??? Can we actually assume that, if template_id ==
8793          error_mark_node, we will have issued a diagnostic to the
8794          user, as opposed to simply marking the tentative parse as
8795          failed?  */
8796       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8797         error ("parse error in template argument list");
8798     }
8799
8800   pop_deferring_access_checks ();
8801   return template_id;
8802 }
8803
8804 /* Parse a template-name.
8805
8806    template-name:
8807      identifier
8808
8809    The standard should actually say:
8810
8811    template-name:
8812      identifier
8813      operator-function-id
8814
8815    A defect report has been filed about this issue.
8816
8817    A conversion-function-id cannot be a template name because they cannot
8818    be part of a template-id. In fact, looking at this code:
8819
8820    a.operator K<int>()
8821
8822    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8823    It is impossible to call a templated conversion-function-id with an
8824    explicit argument list, since the only allowed template parameter is
8825    the type to which it is converting.
8826
8827    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8828    `template' keyword, in a construction like:
8829
8830      T::template f<3>()
8831
8832    In that case `f' is taken to be a template-name, even though there
8833    is no way of knowing for sure.
8834
8835    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8836    name refers to a set of overloaded functions, at least one of which
8837    is a template, or an IDENTIFIER_NODE with the name of the template,
8838    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8839    names are looked up inside uninstantiated templates.  */
8840
8841 static tree
8842 cp_parser_template_name (cp_parser* parser,
8843                          bool template_keyword_p,
8844                          bool check_dependency_p,
8845                          bool is_declaration,
8846                          bool *is_identifier)
8847 {
8848   tree identifier;
8849   tree decl;
8850   tree fns;
8851
8852   /* If the next token is `operator', then we have either an
8853      operator-function-id or a conversion-function-id.  */
8854   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8855     {
8856       /* We don't know whether we're looking at an
8857          operator-function-id or a conversion-function-id.  */
8858       cp_parser_parse_tentatively (parser);
8859       /* Try an operator-function-id.  */
8860       identifier = cp_parser_operator_function_id (parser);
8861       /* If that didn't work, try a conversion-function-id.  */
8862       if (!cp_parser_parse_definitely (parser))
8863         {
8864           cp_parser_error (parser, "expected template-name");
8865           return error_mark_node;
8866         }
8867     }
8868   /* Look for the identifier.  */
8869   else
8870     identifier = cp_parser_identifier (parser);
8871
8872   /* If we didn't find an identifier, we don't have a template-id.  */
8873   if (identifier == error_mark_node)
8874     return error_mark_node;
8875
8876   /* If the name immediately followed the `template' keyword, then it
8877      is a template-name.  However, if the next token is not `<', then
8878      we do not treat it as a template-name, since it is not being used
8879      as part of a template-id.  This enables us to handle constructs
8880      like:
8881
8882        template <typename T> struct S { S(); };
8883        template <typename T> S<T>::S();
8884
8885      correctly.  We would treat `S' as a template -- if it were `S<T>'
8886      -- but we do not if there is no `<'.  */
8887
8888   if (processing_template_decl
8889       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8890     {
8891       /* In a declaration, in a dependent context, we pretend that the
8892          "template" keyword was present in order to improve error
8893          recovery.  For example, given:
8894
8895            template <typename T> void f(T::X<int>);
8896
8897          we want to treat "X<int>" as a template-id.  */
8898       if (is_declaration
8899           && !template_keyword_p
8900           && parser->scope && TYPE_P (parser->scope)
8901           && check_dependency_p
8902           && dependent_type_p (parser->scope)
8903           /* Do not do this for dtors (or ctors), since they never
8904              need the template keyword before their name.  */
8905           && !constructor_name_p (identifier, parser->scope))
8906         {
8907           cp_token_position start = 0;
8908
8909           /* Explain what went wrong.  */
8910           error ("non-template %qD used as template", identifier);
8911           inform ("use %<%T::template %D%> to indicate that it is a template",
8912                   parser->scope, identifier);
8913           /* If parsing tentatively, find the location of the "<" token.  */
8914           if (cp_parser_simulate_error (parser))
8915             start = cp_lexer_token_position (parser->lexer, true);
8916           /* Parse the template arguments so that we can issue error
8917              messages about them.  */
8918           cp_lexer_consume_token (parser->lexer);
8919           cp_parser_enclosed_template_argument_list (parser);
8920           /* Skip tokens until we find a good place from which to
8921              continue parsing.  */
8922           cp_parser_skip_to_closing_parenthesis (parser,
8923                                                  /*recovering=*/true,
8924                                                  /*or_comma=*/true,
8925                                                  /*consume_paren=*/false);
8926           /* If parsing tentatively, permanently remove the
8927              template argument list.  That will prevent duplicate
8928              error messages from being issued about the missing
8929              "template" keyword.  */
8930           if (start)
8931             cp_lexer_purge_tokens_after (parser->lexer, start);
8932           if (is_identifier)
8933             *is_identifier = true;
8934           return identifier;
8935         }
8936
8937       /* If the "template" keyword is present, then there is generally
8938          no point in doing name-lookup, so we just return IDENTIFIER.
8939          But, if the qualifying scope is non-dependent then we can
8940          (and must) do name-lookup normally.  */
8941       if (template_keyword_p
8942           && (!parser->scope
8943               || (TYPE_P (parser->scope)
8944                   && dependent_type_p (parser->scope))))
8945         return identifier;
8946     }
8947
8948   /* Look up the name.  */
8949   decl = cp_parser_lookup_name (parser, identifier,
8950                                 none_type,
8951                                 /*is_template=*/false,
8952                                 /*is_namespace=*/false,
8953                                 check_dependency_p,
8954                                 /*ambiguous_decls=*/NULL);
8955   decl = maybe_get_template_decl_from_type_decl (decl);
8956
8957   /* If DECL is a template, then the name was a template-name.  */
8958   if (TREE_CODE (decl) == TEMPLATE_DECL)
8959     ;
8960   else
8961     {
8962       tree fn = NULL_TREE;
8963
8964       /* The standard does not explicitly indicate whether a name that
8965          names a set of overloaded declarations, some of which are
8966          templates, is a template-name.  However, such a name should
8967          be a template-name; otherwise, there is no way to form a
8968          template-id for the overloaded templates.  */
8969       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8970       if (TREE_CODE (fns) == OVERLOAD)
8971         for (fn = fns; fn; fn = OVL_NEXT (fn))
8972           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8973             break;
8974
8975       if (!fn)
8976         {
8977           /* The name does not name a template.  */
8978           cp_parser_error (parser, "expected template-name");
8979           return error_mark_node;
8980         }
8981     }
8982
8983   /* If DECL is dependent, and refers to a function, then just return
8984      its name; we will look it up again during template instantiation.  */
8985   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8986     {
8987       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8988       if (TYPE_P (scope) && dependent_type_p (scope))
8989         return identifier;
8990     }
8991
8992   return decl;
8993 }
8994
8995 /* Parse a template-argument-list.
8996
8997    template-argument-list:
8998      template-argument
8999      template-argument-list , template-argument
9000
9001    Returns a TREE_VEC containing the arguments.  */
9002
9003 static tree
9004 cp_parser_template_argument_list (cp_parser* parser)
9005 {
9006   tree fixed_args[10];
9007   unsigned n_args = 0;
9008   unsigned alloced = 10;
9009   tree *arg_ary = fixed_args;
9010   tree vec;
9011   bool saved_in_template_argument_list_p;
9012   bool saved_ice_p;
9013   bool saved_non_ice_p;
9014
9015   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9016   parser->in_template_argument_list_p = true;
9017   /* Even if the template-id appears in an integral
9018      constant-expression, the contents of the argument list do 
9019      not.  */ 
9020   saved_ice_p = parser->integral_constant_expression_p;
9021   parser->integral_constant_expression_p = false;
9022   saved_non_ice_p = parser->non_integral_constant_expression_p;
9023   parser->non_integral_constant_expression_p = false;
9024   /* Parse the arguments.  */
9025   do
9026     {
9027       tree argument;
9028
9029       if (n_args)
9030         /* Consume the comma.  */
9031         cp_lexer_consume_token (parser->lexer);
9032
9033       /* Parse the template-argument.  */
9034       argument = cp_parser_template_argument (parser);
9035       if (n_args == alloced)
9036         {
9037           alloced *= 2;
9038
9039           if (arg_ary == fixed_args)
9040             {
9041               arg_ary = XNEWVEC (tree, alloced);
9042               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9043             }
9044           else
9045             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9046         }
9047       arg_ary[n_args++] = argument;
9048     }
9049   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9050
9051   vec = make_tree_vec (n_args);
9052
9053   while (n_args--)
9054     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9055
9056   if (arg_ary != fixed_args)
9057     free (arg_ary);
9058   parser->non_integral_constant_expression_p = saved_non_ice_p;
9059   parser->integral_constant_expression_p = saved_ice_p;
9060   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9061   return vec;
9062 }
9063
9064 /* Parse a template-argument.
9065
9066    template-argument:
9067      assignment-expression
9068      type-id
9069      id-expression
9070
9071    The representation is that of an assignment-expression, type-id, or
9072    id-expression -- except that the qualified id-expression is
9073    evaluated, so that the value returned is either a DECL or an
9074    OVERLOAD.
9075
9076    Although the standard says "assignment-expression", it forbids
9077    throw-expressions or assignments in the template argument.
9078    Therefore, we use "conditional-expression" instead.  */
9079
9080 static tree
9081 cp_parser_template_argument (cp_parser* parser)
9082 {
9083   tree argument;
9084   bool template_p;
9085   bool address_p;
9086   bool maybe_type_id = false;
9087   cp_token *token;
9088   cp_id_kind idk;
9089
9090   /* There's really no way to know what we're looking at, so we just
9091      try each alternative in order.
9092
9093        [temp.arg]
9094
9095        In a template-argument, an ambiguity between a type-id and an
9096        expression is resolved to a type-id, regardless of the form of
9097        the corresponding template-parameter.
9098
9099      Therefore, we try a type-id first.  */
9100   cp_parser_parse_tentatively (parser);
9101   argument = cp_parser_type_id (parser);
9102   /* If there was no error parsing the type-id but the next token is a '>>',
9103      we probably found a typo for '> >'. But there are type-id which are
9104      also valid expressions. For instance:
9105
9106      struct X { int operator >> (int); };
9107      template <int V> struct Foo {};
9108      Foo<X () >> 5> r;
9109
9110      Here 'X()' is a valid type-id of a function type, but the user just
9111      wanted to write the expression "X() >> 5". Thus, we remember that we
9112      found a valid type-id, but we still try to parse the argument as an
9113      expression to see what happens.  */
9114   if (!cp_parser_error_occurred (parser)
9115       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9116     {
9117       maybe_type_id = true;
9118       cp_parser_abort_tentative_parse (parser);
9119     }
9120   else
9121     {
9122       /* If the next token isn't a `,' or a `>', then this argument wasn't
9123       really finished. This means that the argument is not a valid
9124       type-id.  */
9125       if (!cp_parser_next_token_ends_template_argument_p (parser))
9126         cp_parser_error (parser, "expected template-argument");
9127       /* If that worked, we're done.  */
9128       if (cp_parser_parse_definitely (parser))
9129         return argument;
9130     }
9131   /* We're still not sure what the argument will be.  */
9132   cp_parser_parse_tentatively (parser);
9133   /* Try a template.  */
9134   argument = cp_parser_id_expression (parser,
9135                                       /*template_keyword_p=*/false,
9136                                       /*check_dependency_p=*/true,
9137                                       &template_p,
9138                                       /*declarator_p=*/false);
9139   /* If the next token isn't a `,' or a `>', then this argument wasn't
9140      really finished.  */
9141   if (!cp_parser_next_token_ends_template_argument_p (parser))
9142     cp_parser_error (parser, "expected template-argument");
9143   if (!cp_parser_error_occurred (parser))
9144     {
9145       /* Figure out what is being referred to.  If the id-expression
9146          was for a class template specialization, then we will have a
9147          TYPE_DECL at this point.  There is no need to do name lookup
9148          at this point in that case.  */
9149       if (TREE_CODE (argument) != TYPE_DECL)
9150         argument = cp_parser_lookup_name (parser, argument,
9151                                           none_type,
9152                                           /*is_template=*/template_p,
9153                                           /*is_namespace=*/false,
9154                                           /*check_dependency=*/true,
9155                                           /*ambiguous_decls=*/NULL);
9156       if (TREE_CODE (argument) != TEMPLATE_DECL
9157           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9158         cp_parser_error (parser, "expected template-name");
9159     }
9160   if (cp_parser_parse_definitely (parser))
9161     return argument;
9162   /* It must be a non-type argument.  There permitted cases are given
9163      in [temp.arg.nontype]:
9164
9165      -- an integral constant-expression of integral or enumeration
9166         type; or
9167
9168      -- the name of a non-type template-parameter; or
9169
9170      -- the name of an object or function with external linkage...
9171
9172      -- the address of an object or function with external linkage...
9173
9174      -- a pointer to member...  */
9175   /* Look for a non-type template parameter.  */
9176   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9177     {
9178       cp_parser_parse_tentatively (parser);
9179       argument = cp_parser_primary_expression (parser,
9180                                                /*adress_p=*/false,
9181                                                /*cast_p=*/false,
9182                                                /*template_arg_p=*/true,
9183                                                &idk);
9184       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9185           || !cp_parser_next_token_ends_template_argument_p (parser))
9186         cp_parser_simulate_error (parser);
9187       if (cp_parser_parse_definitely (parser))
9188         return argument;
9189     }
9190
9191   /* If the next token is "&", the argument must be the address of an
9192      object or function with external linkage.  */
9193   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9194   if (address_p)
9195     cp_lexer_consume_token (parser->lexer);
9196   /* See if we might have an id-expression.  */
9197   token = cp_lexer_peek_token (parser->lexer);
9198   if (token->type == CPP_NAME
9199       || token->keyword == RID_OPERATOR
9200       || token->type == CPP_SCOPE
9201       || token->type == CPP_TEMPLATE_ID
9202       || token->type == CPP_NESTED_NAME_SPECIFIER)
9203     {
9204       cp_parser_parse_tentatively (parser);
9205       argument = cp_parser_primary_expression (parser,
9206                                                address_p,
9207                                                /*cast_p=*/false,
9208                                                /*template_arg_p=*/true,
9209                                                &idk);
9210       if (cp_parser_error_occurred (parser)
9211           || !cp_parser_next_token_ends_template_argument_p (parser))
9212         cp_parser_abort_tentative_parse (parser);
9213       else
9214         {
9215           if (TREE_CODE (argument) == INDIRECT_REF)
9216             {
9217               gcc_assert (REFERENCE_REF_P (argument));
9218               argument = TREE_OPERAND (argument, 0);
9219             }
9220
9221           if (TREE_CODE (argument) == BASELINK)
9222             /* We don't need the information about what class was used
9223                to name the overloaded functions.  */  
9224             argument = BASELINK_FUNCTIONS (argument);
9225
9226           if (TREE_CODE (argument) == VAR_DECL)
9227             {
9228               /* A variable without external linkage might still be a
9229                  valid constant-expression, so no error is issued here
9230                  if the external-linkage check fails.  */
9231               if (!DECL_EXTERNAL_LINKAGE_P (argument))
9232                 cp_parser_simulate_error (parser);
9233             }
9234           else if (is_overloaded_fn (argument))
9235             /* All overloaded functions are allowed; if the external
9236                linkage test does not pass, an error will be issued
9237                later.  */
9238             ;
9239           else if (address_p
9240                    && (TREE_CODE (argument) == OFFSET_REF
9241                        || TREE_CODE (argument) == SCOPE_REF))
9242             /* A pointer-to-member.  */
9243             ;
9244           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9245             ;
9246           else
9247             cp_parser_simulate_error (parser);
9248
9249           if (cp_parser_parse_definitely (parser))
9250             {
9251               if (address_p)
9252                 argument = build_x_unary_op (ADDR_EXPR, argument);
9253               return argument;
9254             }
9255         }
9256     }
9257   /* If the argument started with "&", there are no other valid
9258      alternatives at this point.  */
9259   if (address_p)
9260     {
9261       cp_parser_error (parser, "invalid non-type template argument");
9262       return error_mark_node;
9263     }
9264
9265   /* If the argument wasn't successfully parsed as a type-id followed
9266      by '>>', the argument can only be a constant expression now.
9267      Otherwise, we try parsing the constant-expression tentatively,
9268      because the argument could really be a type-id.  */
9269   if (maybe_type_id)
9270     cp_parser_parse_tentatively (parser);
9271   argument = cp_parser_constant_expression (parser,
9272                                             /*allow_non_constant_p=*/false,
9273                                             /*non_constant_p=*/NULL);
9274   argument = fold_non_dependent_expr (argument);
9275   if (!maybe_type_id)
9276     return argument;
9277   if (!cp_parser_next_token_ends_template_argument_p (parser))
9278     cp_parser_error (parser, "expected template-argument");
9279   if (cp_parser_parse_definitely (parser))
9280     return argument;
9281   /* We did our best to parse the argument as a non type-id, but that
9282      was the only alternative that matched (albeit with a '>' after
9283      it). We can assume it's just a typo from the user, and a
9284      diagnostic will then be issued.  */
9285   return cp_parser_type_id (parser);
9286 }
9287
9288 /* Parse an explicit-instantiation.
9289
9290    explicit-instantiation:
9291      template declaration
9292
9293    Although the standard says `declaration', what it really means is:
9294
9295    explicit-instantiation:
9296      template decl-specifier-seq [opt] declarator [opt] ;
9297
9298    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9299    supposed to be allowed.  A defect report has been filed about this
9300    issue.
9301
9302    GNU Extension:
9303
9304    explicit-instantiation:
9305      storage-class-specifier template
9306        decl-specifier-seq [opt] declarator [opt] ;
9307      function-specifier template
9308        decl-specifier-seq [opt] declarator [opt] ;  */
9309
9310 static void
9311 cp_parser_explicit_instantiation (cp_parser* parser)
9312 {
9313   int declares_class_or_enum;
9314   cp_decl_specifier_seq decl_specifiers;
9315   tree extension_specifier = NULL_TREE;
9316
9317   /* Look for an (optional) storage-class-specifier or
9318      function-specifier.  */
9319   if (cp_parser_allow_gnu_extensions_p (parser))
9320     {
9321       extension_specifier
9322         = cp_parser_storage_class_specifier_opt (parser);
9323       if (!extension_specifier)
9324         extension_specifier
9325           = cp_parser_function_specifier_opt (parser,
9326                                               /*decl_specs=*/NULL);
9327     }
9328
9329   /* Look for the `template' keyword.  */
9330   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9331   /* Let the front end know that we are processing an explicit
9332      instantiation.  */
9333   begin_explicit_instantiation ();
9334   /* [temp.explicit] says that we are supposed to ignore access
9335      control while processing explicit instantiation directives.  */
9336   push_deferring_access_checks (dk_no_check);
9337   /* Parse a decl-specifier-seq.  */
9338   cp_parser_decl_specifier_seq (parser,
9339                                 CP_PARSER_FLAGS_OPTIONAL,
9340                                 &decl_specifiers,
9341                                 &declares_class_or_enum);
9342   /* If there was exactly one decl-specifier, and it declared a class,
9343      and there's no declarator, then we have an explicit type
9344      instantiation.  */
9345   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9346     {
9347       tree type;
9348
9349       type = check_tag_decl (&decl_specifiers);
9350       /* Turn access control back on for names used during
9351          template instantiation.  */
9352       pop_deferring_access_checks ();
9353       if (type)
9354         do_type_instantiation (type, extension_specifier,
9355                                /*complain=*/tf_error);
9356     }
9357   else
9358     {
9359       cp_declarator *declarator;
9360       tree decl;
9361
9362       /* Parse the declarator.  */
9363       declarator
9364         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9365                                 /*ctor_dtor_or_conv_p=*/NULL,
9366                                 /*parenthesized_p=*/NULL,
9367                                 /*member_p=*/false);
9368       if (declares_class_or_enum & 2)
9369         cp_parser_check_for_definition_in_return_type (declarator,
9370                                                        decl_specifiers.type);
9371       if (declarator != cp_error_declarator)
9372         {
9373           decl = grokdeclarator (declarator, &decl_specifiers,
9374                                  NORMAL, 0, NULL);
9375           /* Turn access control back on for names used during
9376              template instantiation.  */
9377           pop_deferring_access_checks ();
9378           /* Do the explicit instantiation.  */
9379           do_decl_instantiation (decl, extension_specifier);
9380         }
9381       else
9382         {
9383           pop_deferring_access_checks ();
9384           /* Skip the body of the explicit instantiation.  */
9385           cp_parser_skip_to_end_of_statement (parser);
9386         }
9387     }
9388   /* We're done with the instantiation.  */
9389   end_explicit_instantiation ();
9390
9391   cp_parser_consume_semicolon_at_end_of_statement (parser);
9392 }
9393
9394 /* Parse an explicit-specialization.
9395
9396    explicit-specialization:
9397      template < > declaration
9398
9399    Although the standard says `declaration', what it really means is:
9400
9401    explicit-specialization:
9402      template <> decl-specifier [opt] init-declarator [opt] ;
9403      template <> function-definition
9404      template <> explicit-specialization
9405      template <> template-declaration  */
9406
9407 static void
9408 cp_parser_explicit_specialization (cp_parser* parser)
9409 {
9410   bool need_lang_pop;
9411   /* Look for the `template' keyword.  */
9412   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9413   /* Look for the `<'.  */
9414   cp_parser_require (parser, CPP_LESS, "`<'");
9415   /* Look for the `>'.  */
9416   cp_parser_require (parser, CPP_GREATER, "`>'");
9417   /* We have processed another parameter list.  */
9418   ++parser->num_template_parameter_lists;
9419   /* [temp]
9420    
9421      A template ... explicit specialization ... shall not have C
9422      linkage.  */ 
9423   if (current_lang_name == lang_name_c)
9424     {
9425       error ("template specialization with C linkage");
9426       /* Give it C++ linkage to avoid confusing other parts of the
9427          front end.  */
9428       push_lang_context (lang_name_cplusplus);
9429       need_lang_pop = true;
9430     }
9431   else
9432     need_lang_pop = false;
9433   /* Let the front end know that we are beginning a specialization.  */
9434   begin_specialization ();
9435   /* If the next keyword is `template', we need to figure out whether
9436      or not we're looking a template-declaration.  */
9437   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9438     {
9439       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9440           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9441         cp_parser_template_declaration_after_export (parser,
9442                                                      /*member_p=*/false);
9443       else
9444         cp_parser_explicit_specialization (parser);
9445     }
9446   else
9447     /* Parse the dependent declaration.  */
9448     cp_parser_single_declaration (parser,
9449                                   /*member_p=*/false,
9450                                   /*friend_p=*/NULL);
9451   /* We're done with the specialization.  */
9452   end_specialization ();
9453   /* For the erroneous case of a template with C linkage, we pushed an
9454      implicit C++ linkage scope; exit that scope now.  */
9455   if (need_lang_pop)
9456     pop_lang_context ();
9457   /* We're done with this parameter list.  */
9458   --parser->num_template_parameter_lists;
9459 }
9460
9461 /* Parse a type-specifier.
9462
9463    type-specifier:
9464      simple-type-specifier
9465      class-specifier
9466      enum-specifier
9467      elaborated-type-specifier
9468      cv-qualifier
9469
9470    GNU Extension:
9471
9472    type-specifier:
9473      __complex__
9474
9475    Returns a representation of the type-specifier.  For a
9476    class-specifier, enum-specifier, or elaborated-type-specifier, a
9477    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9478
9479    The parser flags FLAGS is used to control type-specifier parsing.
9480
9481    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9482    in a decl-specifier-seq.
9483
9484    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9485    class-specifier, enum-specifier, or elaborated-type-specifier, then
9486    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9487    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9488    zero.
9489
9490    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9491    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9492    is set to FALSE.  */
9493
9494 static tree
9495 cp_parser_type_specifier (cp_parser* parser,
9496                           cp_parser_flags flags,
9497                           cp_decl_specifier_seq *decl_specs,
9498                           bool is_declaration,
9499                           int* declares_class_or_enum,
9500                           bool* is_cv_qualifier)
9501 {
9502   tree type_spec = NULL_TREE;
9503   cp_token *token;
9504   enum rid keyword;
9505   cp_decl_spec ds = ds_last;
9506
9507   /* Assume this type-specifier does not declare a new type.  */
9508   if (declares_class_or_enum)
9509     *declares_class_or_enum = 0;
9510   /* And that it does not specify a cv-qualifier.  */
9511   if (is_cv_qualifier)
9512     *is_cv_qualifier = false;
9513   /* Peek at the next token.  */
9514   token = cp_lexer_peek_token (parser->lexer);
9515
9516   /* If we're looking at a keyword, we can use that to guide the
9517      production we choose.  */
9518   keyword = token->keyword;
9519   switch (keyword)
9520     {
9521     case RID_ENUM:
9522       /* 'enum' [identifier] '{' introduces an enum-specifier;
9523          'enum' <anything else> introduces an elaborated-type-specifier.  */
9524       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9525           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9526               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9527                  == CPP_OPEN_BRACE))
9528         {
9529           if (parser->num_template_parameter_lists)
9530             {
9531               error ("template declaration of %qs", "enum");
9532               cp_parser_skip_to_end_of_block_or_statement (parser);
9533               type_spec = error_mark_node;
9534             }
9535           else
9536             type_spec = cp_parser_enum_specifier (parser);
9537
9538           if (declares_class_or_enum)
9539             *declares_class_or_enum = 2;
9540           if (decl_specs)
9541             cp_parser_set_decl_spec_type (decl_specs,
9542                                           type_spec,
9543                                           /*user_defined_p=*/true);
9544           return type_spec;
9545         }
9546       else
9547         goto elaborated_type_specifier;
9548
9549       /* Any of these indicate either a class-specifier, or an
9550          elaborated-type-specifier.  */
9551     case RID_CLASS:
9552     case RID_STRUCT:
9553     case RID_UNION:
9554       /* Parse tentatively so that we can back up if we don't find a
9555          class-specifier.  */
9556       cp_parser_parse_tentatively (parser);
9557       /* Look for the class-specifier.  */
9558       type_spec = cp_parser_class_specifier (parser);
9559       /* If that worked, we're done.  */
9560       if (cp_parser_parse_definitely (parser))
9561         {
9562           if (declares_class_or_enum)
9563             *declares_class_or_enum = 2;
9564           if (decl_specs)
9565             cp_parser_set_decl_spec_type (decl_specs,
9566                                           type_spec,
9567                                           /*user_defined_p=*/true);
9568           return type_spec;
9569         }
9570
9571       /* Fall through.  */
9572     elaborated_type_specifier:
9573       /* We're declaring (not defining) a class or enum.  */
9574       if (declares_class_or_enum)
9575         *declares_class_or_enum = 1;
9576
9577       /* Fall through.  */
9578     case RID_TYPENAME:
9579       /* Look for an elaborated-type-specifier.  */
9580       type_spec
9581         = (cp_parser_elaborated_type_specifier
9582            (parser,
9583             decl_specs && decl_specs->specs[(int) ds_friend],
9584             is_declaration));
9585       if (decl_specs)
9586         cp_parser_set_decl_spec_type (decl_specs,
9587                                       type_spec,
9588                                       /*user_defined_p=*/true);
9589       return type_spec;
9590
9591     case RID_CONST:
9592       ds = ds_const;
9593       if (is_cv_qualifier)
9594         *is_cv_qualifier = true;
9595       break;
9596
9597     case RID_VOLATILE:
9598       ds = ds_volatile;
9599       if (is_cv_qualifier)
9600         *is_cv_qualifier = true;
9601       break;
9602
9603     case RID_RESTRICT:
9604       ds = ds_restrict;
9605       if (is_cv_qualifier)
9606         *is_cv_qualifier = true;
9607       break;
9608
9609     case RID_COMPLEX:
9610       /* The `__complex__' keyword is a GNU extension.  */
9611       ds = ds_complex;
9612       break;
9613
9614     default:
9615       break;
9616     }
9617
9618   /* Handle simple keywords.  */
9619   if (ds != ds_last)
9620     {
9621       if (decl_specs)
9622         {
9623           ++decl_specs->specs[(int)ds];
9624           decl_specs->any_specifiers_p = true;
9625         }
9626       return cp_lexer_consume_token (parser->lexer)->value;
9627     }
9628
9629   /* If we do not already have a type-specifier, assume we are looking
9630      at a simple-type-specifier.  */
9631   type_spec = cp_parser_simple_type_specifier (parser,
9632                                                decl_specs,
9633                                                flags);
9634
9635   /* If we didn't find a type-specifier, and a type-specifier was not
9636      optional in this context, issue an error message.  */
9637   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9638     {
9639       cp_parser_error (parser, "expected type specifier");
9640       return error_mark_node;
9641     }
9642
9643   return type_spec;
9644 }
9645
9646 /* Parse a simple-type-specifier.
9647
9648    simple-type-specifier:
9649      :: [opt] nested-name-specifier [opt] type-name
9650      :: [opt] nested-name-specifier template template-id
9651      char
9652      wchar_t
9653      bool
9654      short
9655      int
9656      long
9657      signed
9658      unsigned
9659      float
9660      double
9661      void
9662
9663    GNU Extension:
9664
9665    simple-type-specifier:
9666      __typeof__ unary-expression
9667      __typeof__ ( type-id )
9668
9669    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9670    appropriately updated.  */
9671
9672 static tree
9673 cp_parser_simple_type_specifier (cp_parser* parser,
9674                                  cp_decl_specifier_seq *decl_specs,
9675                                  cp_parser_flags flags)
9676 {
9677   tree type = NULL_TREE;
9678   cp_token *token;
9679
9680   /* Peek at the next token.  */
9681   token = cp_lexer_peek_token (parser->lexer);
9682
9683   /* If we're looking at a keyword, things are easy.  */
9684   switch (token->keyword)
9685     {
9686     case RID_CHAR:
9687       if (decl_specs)
9688         decl_specs->explicit_char_p = true;
9689       type = char_type_node;
9690       break;
9691     case RID_WCHAR:
9692       type = wchar_type_node;
9693       break;
9694     case RID_BOOL:
9695       type = boolean_type_node;
9696       break;
9697     case RID_SHORT:
9698       if (decl_specs)
9699         ++decl_specs->specs[(int) ds_short];
9700       type = short_integer_type_node;
9701       break;
9702     case RID_INT:
9703       if (decl_specs)
9704         decl_specs->explicit_int_p = true;
9705       type = integer_type_node;
9706       break;
9707     case RID_LONG:
9708       if (decl_specs)
9709         ++decl_specs->specs[(int) ds_long];
9710       type = long_integer_type_node;
9711       break;
9712     case RID_SIGNED:
9713       if (decl_specs)
9714         ++decl_specs->specs[(int) ds_signed];
9715       type = integer_type_node;
9716       break;
9717     case RID_UNSIGNED:
9718       if (decl_specs)
9719         ++decl_specs->specs[(int) ds_unsigned];
9720       type = unsigned_type_node;
9721       break;
9722     case RID_FLOAT:
9723       type = float_type_node;
9724       break;
9725     case RID_DOUBLE:
9726       type = double_type_node;
9727       break;
9728     case RID_VOID:
9729       type = void_type_node;
9730       break;
9731
9732     case RID_TYPEOF:
9733       /* Consume the `typeof' token.  */
9734       cp_lexer_consume_token (parser->lexer);
9735       /* Parse the operand to `typeof'.  */
9736       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9737       /* If it is not already a TYPE, take its type.  */
9738       if (!TYPE_P (type))
9739         type = finish_typeof (type);
9740
9741       if (decl_specs)
9742         cp_parser_set_decl_spec_type (decl_specs, type,
9743                                       /*user_defined_p=*/true);
9744
9745       return type;
9746
9747     default:
9748       break;
9749     }
9750
9751   /* If the type-specifier was for a built-in type, we're done.  */
9752   if (type)
9753     {
9754       tree id;
9755
9756       /* Record the type.  */
9757       if (decl_specs
9758           && (token->keyword != RID_SIGNED
9759               && token->keyword != RID_UNSIGNED
9760               && token->keyword != RID_SHORT
9761               && token->keyword != RID_LONG))
9762         cp_parser_set_decl_spec_type (decl_specs,
9763                                       type,
9764                                       /*user_defined=*/false);
9765       if (decl_specs)
9766         decl_specs->any_specifiers_p = true;
9767
9768       /* Consume the token.  */
9769       id = cp_lexer_consume_token (parser->lexer)->value;
9770
9771       /* There is no valid C++ program where a non-template type is
9772          followed by a "<".  That usually indicates that the user thought
9773          that the type was a template.  */
9774       cp_parser_check_for_invalid_template_id (parser, type);
9775
9776       return TYPE_NAME (type);
9777     }
9778
9779   /* The type-specifier must be a user-defined type.  */
9780   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9781     {
9782       bool qualified_p;
9783       bool global_p;
9784
9785       /* Don't gobble tokens or issue error messages if this is an
9786          optional type-specifier.  */
9787       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9788         cp_parser_parse_tentatively (parser);
9789
9790       /* Look for the optional `::' operator.  */
9791       global_p
9792         = (cp_parser_global_scope_opt (parser,
9793                                        /*current_scope_valid_p=*/false)
9794            != NULL_TREE);
9795       /* Look for the nested-name specifier.  */
9796       qualified_p
9797         = (cp_parser_nested_name_specifier_opt (parser,
9798                                                 /*typename_keyword_p=*/false,
9799                                                 /*check_dependency_p=*/true,
9800                                                 /*type_p=*/false,
9801                                                 /*is_declaration=*/false)
9802            != NULL_TREE);
9803       /* If we have seen a nested-name-specifier, and the next token
9804          is `template', then we are using the template-id production.  */
9805       if (parser->scope
9806           && cp_parser_optional_template_keyword (parser))
9807         {
9808           /* Look for the template-id.  */
9809           type = cp_parser_template_id (parser,
9810                                         /*template_keyword_p=*/true,
9811                                         /*check_dependency_p=*/true,
9812                                         /*is_declaration=*/false);
9813           /* If the template-id did not name a type, we are out of
9814              luck.  */
9815           if (TREE_CODE (type) != TYPE_DECL)
9816             {
9817               cp_parser_error (parser, "expected template-id for type");
9818               type = NULL_TREE;
9819             }
9820         }
9821       /* Otherwise, look for a type-name.  */
9822       else
9823         type = cp_parser_type_name (parser);
9824       /* Keep track of all name-lookups performed in class scopes.  */
9825       if (type
9826           && !global_p
9827           && !qualified_p
9828           && TREE_CODE (type) == TYPE_DECL
9829           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9830         maybe_note_name_used_in_class (DECL_NAME (type), type);
9831       /* If it didn't work out, we don't have a TYPE.  */
9832       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9833           && !cp_parser_parse_definitely (parser))
9834         type = NULL_TREE;
9835       if (type && decl_specs)
9836         cp_parser_set_decl_spec_type (decl_specs, type,
9837                                       /*user_defined=*/true);
9838     }
9839
9840   /* If we didn't get a type-name, issue an error message.  */
9841   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9842     {
9843       cp_parser_error (parser, "expected type-name");
9844       return error_mark_node;
9845     }
9846
9847   /* There is no valid C++ program where a non-template type is
9848      followed by a "<".  That usually indicates that the user thought
9849      that the type was a template.  */
9850   if (type && type != error_mark_node)
9851     {
9852       /* As a last-ditch effort, see if TYPE is an Objective-C type.
9853          If it is, then the '<'...'>' enclose protocol names rather than
9854          template arguments, and so everything is fine.  */
9855       if (c_dialect_objc ()
9856           && (objc_is_id (type) || objc_is_class_name (type)))
9857         {
9858           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9859           tree qual_type = objc_get_protocol_qualified_type (type, protos);
9860
9861           /* Clobber the "unqualified" type previously entered into
9862              DECL_SPECS with the new, improved protocol-qualified version.  */
9863           if (decl_specs)
9864             decl_specs->type = qual_type;
9865
9866           return qual_type;
9867         }
9868
9869       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9870     }
9871
9872   return type;
9873 }
9874
9875 /* Parse a type-name.
9876
9877    type-name:
9878      class-name
9879      enum-name
9880      typedef-name
9881
9882    enum-name:
9883      identifier
9884
9885    typedef-name:
9886      identifier
9887
9888    Returns a TYPE_DECL for the type.  */
9889
9890 static tree
9891 cp_parser_type_name (cp_parser* parser)
9892 {
9893   tree type_decl;
9894   tree identifier;
9895
9896   /* We can't know yet whether it is a class-name or not.  */
9897   cp_parser_parse_tentatively (parser);
9898   /* Try a class-name.  */
9899   type_decl = cp_parser_class_name (parser,
9900                                     /*typename_keyword_p=*/false,
9901                                     /*template_keyword_p=*/false,
9902                                     none_type,
9903                                     /*check_dependency_p=*/true,
9904                                     /*class_head_p=*/false,
9905                                     /*is_declaration=*/false);
9906   /* If it's not a class-name, keep looking.  */
9907   if (!cp_parser_parse_definitely (parser))
9908     {
9909       /* It must be a typedef-name or an enum-name.  */
9910       identifier = cp_parser_identifier (parser);
9911       if (identifier == error_mark_node)
9912         return error_mark_node;
9913
9914       /* Look up the type-name.  */
9915       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9916
9917       if (TREE_CODE (type_decl) != TYPE_DECL
9918           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9919         {
9920           /* See if this is an Objective-C type.  */
9921           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9922           tree type = objc_get_protocol_qualified_type (identifier, protos);
9923           if (type)
9924             type_decl = TYPE_NAME (type);
9925         }
9926
9927       /* Issue an error if we did not find a type-name.  */
9928       if (TREE_CODE (type_decl) != TYPE_DECL)
9929         {
9930           if (!cp_parser_simulate_error (parser))
9931             cp_parser_name_lookup_error (parser, identifier, type_decl,
9932                                          "is not a type");
9933           type_decl = error_mark_node;
9934         }
9935       /* Remember that the name was used in the definition of the
9936          current class so that we can check later to see if the
9937          meaning would have been different after the class was
9938          entirely defined.  */
9939       else if (type_decl != error_mark_node
9940                && !parser->scope)
9941         maybe_note_name_used_in_class (identifier, type_decl);
9942     }
9943
9944   return type_decl;
9945 }
9946
9947
9948 /* Parse an elaborated-type-specifier.  Note that the grammar given
9949    here incorporates the resolution to DR68.
9950
9951    elaborated-type-specifier:
9952      class-key :: [opt] nested-name-specifier [opt] identifier
9953      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9954      enum :: [opt] nested-name-specifier [opt] identifier
9955      typename :: [opt] nested-name-specifier identifier
9956      typename :: [opt] nested-name-specifier template [opt]
9957        template-id
9958
9959    GNU extension:
9960
9961    elaborated-type-specifier:
9962      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9963      class-key attributes :: [opt] nested-name-specifier [opt]
9964                template [opt] template-id
9965      enum attributes :: [opt] nested-name-specifier [opt] identifier
9966
9967    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9968    declared `friend'.  If IS_DECLARATION is TRUE, then this
9969    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9970    something is being declared.
9971
9972    Returns the TYPE specified.  */
9973
9974 static tree
9975 cp_parser_elaborated_type_specifier (cp_parser* parser,
9976                                      bool is_friend,
9977                                      bool is_declaration)
9978 {
9979   enum tag_types tag_type;
9980   tree identifier;
9981   tree type = NULL_TREE;
9982   tree attributes = NULL_TREE;
9983
9984   /* See if we're looking at the `enum' keyword.  */
9985   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9986     {
9987       /* Consume the `enum' token.  */
9988       cp_lexer_consume_token (parser->lexer);
9989       /* Remember that it's an enumeration type.  */
9990       tag_type = enum_type;
9991       /* Parse the attributes.  */
9992       attributes = cp_parser_attributes_opt (parser);
9993     }
9994   /* Or, it might be `typename'.  */
9995   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9996                                            RID_TYPENAME))
9997     {
9998       /* Consume the `typename' token.  */
9999       cp_lexer_consume_token (parser->lexer);
10000       /* Remember that it's a `typename' type.  */
10001       tag_type = typename_type;
10002       /* The `typename' keyword is only allowed in templates.  */
10003       if (!processing_template_decl)
10004         pedwarn ("using %<typename%> outside of template");
10005     }
10006   /* Otherwise it must be a class-key.  */
10007   else
10008     {
10009       tag_type = cp_parser_class_key (parser);
10010       if (tag_type == none_type)
10011         return error_mark_node;
10012       /* Parse the attributes.  */
10013       attributes = cp_parser_attributes_opt (parser);
10014     }
10015
10016   /* Look for the `::' operator.  */
10017   cp_parser_global_scope_opt (parser,
10018                               /*current_scope_valid_p=*/false);
10019   /* Look for the nested-name-specifier.  */
10020   if (tag_type == typename_type)
10021     {
10022       if (!cp_parser_nested_name_specifier (parser,
10023                                            /*typename_keyword_p=*/true,
10024                                            /*check_dependency_p=*/true,
10025                                            /*type_p=*/true,
10026                                             is_declaration))
10027         return error_mark_node;
10028     }
10029   else
10030     /* Even though `typename' is not present, the proposed resolution
10031        to Core Issue 180 says that in `class A<T>::B', `B' should be
10032        considered a type-name, even if `A<T>' is dependent.  */
10033     cp_parser_nested_name_specifier_opt (parser,
10034                                          /*typename_keyword_p=*/true,
10035                                          /*check_dependency_p=*/true,
10036                                          /*type_p=*/true,
10037                                          is_declaration);
10038   /* For everything but enumeration types, consider a template-id.  */
10039   if (tag_type != enum_type)
10040     {
10041       bool template_p = false;
10042       tree decl;
10043
10044       /* Allow the `template' keyword.  */
10045       template_p = cp_parser_optional_template_keyword (parser);
10046       /* If we didn't see `template', we don't know if there's a
10047          template-id or not.  */
10048       if (!template_p)
10049         cp_parser_parse_tentatively (parser);
10050       /* Parse the template-id.  */
10051       decl = cp_parser_template_id (parser, template_p,
10052                                     /*check_dependency_p=*/true,
10053                                     is_declaration);
10054       /* If we didn't find a template-id, look for an ordinary
10055          identifier.  */
10056       if (!template_p && !cp_parser_parse_definitely (parser))
10057         ;
10058       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10059          in effect, then we must assume that, upon instantiation, the
10060          template will correspond to a class.  */
10061       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10062                && tag_type == typename_type)
10063         type = make_typename_type (parser->scope, decl,
10064                                    typename_type,
10065                                    /*complain=*/tf_error);
10066       else
10067         type = TREE_TYPE (decl);
10068     }
10069
10070   /* For an enumeration type, consider only a plain identifier.  */
10071   if (!type)
10072     {
10073       identifier = cp_parser_identifier (parser);
10074
10075       if (identifier == error_mark_node)
10076         {
10077           parser->scope = NULL_TREE;
10078           return error_mark_node;
10079         }
10080
10081       /* For a `typename', we needn't call xref_tag.  */
10082       if (tag_type == typename_type
10083           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10084         return cp_parser_make_typename_type (parser, parser->scope,
10085                                              identifier);
10086       /* Look up a qualified name in the usual way.  */
10087       if (parser->scope)
10088         {
10089           tree decl;
10090
10091           decl = cp_parser_lookup_name (parser, identifier,
10092                                         tag_type,
10093                                         /*is_template=*/false,
10094                                         /*is_namespace=*/false,
10095                                         /*check_dependency=*/true,
10096                                         /*ambiguous_decls=*/NULL);
10097
10098           /* If we are parsing friend declaration, DECL may be a
10099              TEMPLATE_DECL tree node here.  However, we need to check
10100              whether this TEMPLATE_DECL results in valid code.  Consider
10101              the following example:
10102
10103                namespace N {
10104                  template <class T> class C {};
10105                }
10106                class X {
10107                  template <class T> friend class N::C; // #1, valid code
10108                };
10109                template <class T> class Y {
10110                  friend class N::C;                    // #2, invalid code
10111                };
10112
10113              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10114              name lookup of `N::C'.  We see that friend declaration must
10115              be template for the code to be valid.  Note that
10116              processing_template_decl does not work here since it is
10117              always 1 for the above two cases.  */
10118
10119           decl = (cp_parser_maybe_treat_template_as_class
10120                   (decl, /*tag_name_p=*/is_friend
10121                          && parser->num_template_parameter_lists));
10122
10123           if (TREE_CODE (decl) != TYPE_DECL)
10124             {
10125               cp_parser_diagnose_invalid_type_name (parser,
10126                                                     parser->scope,
10127                                                     identifier);
10128               return error_mark_node;
10129             }
10130
10131           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10132             check_elaborated_type_specifier
10133               (tag_type, decl,
10134                (parser->num_template_parameter_lists
10135                 || DECL_SELF_REFERENCE_P (decl)));
10136
10137           type = TREE_TYPE (decl);
10138         }
10139       else
10140         {
10141           /* An elaborated-type-specifier sometimes introduces a new type and
10142              sometimes names an existing type.  Normally, the rule is that it
10143              introduces a new type only if there is not an existing type of
10144              the same name already in scope.  For example, given:
10145
10146                struct S {};
10147                void f() { struct S s; }
10148
10149              the `struct S' in the body of `f' is the same `struct S' as in
10150              the global scope; the existing definition is used.  However, if
10151              there were no global declaration, this would introduce a new
10152              local class named `S'.
10153
10154              An exception to this rule applies to the following code:
10155
10156                namespace N { struct S; }
10157
10158              Here, the elaborated-type-specifier names a new type
10159              unconditionally; even if there is already an `S' in the
10160              containing scope this declaration names a new type.
10161              This exception only applies if the elaborated-type-specifier
10162              forms the complete declaration:
10163
10164                [class.name]
10165
10166                A declaration consisting solely of `class-key identifier ;' is
10167                either a redeclaration of the name in the current scope or a
10168                forward declaration of the identifier as a class name.  It
10169                introduces the name into the current scope.
10170
10171              We are in this situation precisely when the next token is a `;'.
10172
10173              An exception to the exception is that a `friend' declaration does
10174              *not* name a new type; i.e., given:
10175
10176                struct S { friend struct T; };
10177
10178              `T' is not a new type in the scope of `S'.
10179
10180              Also, `new struct S' or `sizeof (struct S)' never results in the
10181              definition of a new type; a new type can only be declared in a
10182              declaration context.  */
10183
10184           tag_scope ts;
10185           bool template_p;
10186
10187           if (is_friend)
10188             /* Friends have special name lookup rules.  */
10189             ts = ts_within_enclosing_non_class;
10190           else if (is_declaration
10191                    && cp_lexer_next_token_is (parser->lexer,
10192                                               CPP_SEMICOLON))
10193             /* This is a `class-key identifier ;' */
10194             ts = ts_current;
10195           else
10196             ts = ts_global;
10197
10198           /* Warn about attributes. They are ignored.  */
10199           if (attributes)
10200             warning (OPT_Wattributes,
10201                      "type attributes are honored only at type definition");
10202
10203           template_p = 
10204             (parser->num_template_parameter_lists
10205              && (cp_parser_next_token_starts_class_definition_p (parser)
10206                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10207           /* An unqualified name was used to reference this type, so
10208              there were no qualifying templates.  */
10209           if (!cp_parser_check_template_parameters (parser, 
10210                                                     /*num_templates=*/0))
10211             return error_mark_node;
10212           type = xref_tag (tag_type, identifier, ts, template_p);
10213         }
10214     }
10215   if (tag_type != enum_type)
10216     cp_parser_check_class_key (tag_type, type);
10217
10218   /* A "<" cannot follow an elaborated type specifier.  If that
10219      happens, the user was probably trying to form a template-id.  */
10220   cp_parser_check_for_invalid_template_id (parser, type);
10221
10222   return type;
10223 }
10224
10225 /* Parse an enum-specifier.
10226
10227    enum-specifier:
10228      enum identifier [opt] { enumerator-list [opt] }
10229
10230    GNU Extensions:
10231      enum identifier [opt] { enumerator-list [opt] } attributes
10232
10233    Returns an ENUM_TYPE representing the enumeration.  */
10234
10235 static tree
10236 cp_parser_enum_specifier (cp_parser* parser)
10237 {
10238   tree identifier;
10239   tree type;
10240
10241   /* Caller guarantees that the current token is 'enum', an identifier
10242      possibly follows, and the token after that is an opening brace.
10243      If we don't have an identifier, fabricate an anonymous name for
10244      the enumeration being defined.  */
10245   cp_lexer_consume_token (parser->lexer);
10246
10247   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10248     identifier = cp_parser_identifier (parser);
10249   else
10250     identifier = make_anon_name ();
10251
10252   /* Issue an error message if type-definitions are forbidden here.  */
10253   cp_parser_check_type_definition (parser);
10254
10255   /* Create the new type.  We do this before consuming the opening brace
10256      so the enum will be recorded as being on the line of its tag (or the
10257      'enum' keyword, if there is no tag).  */
10258   type = start_enum (identifier);
10259
10260   /* Consume the opening brace.  */
10261   cp_lexer_consume_token (parser->lexer);
10262
10263   /* If the next token is not '}', then there are some enumerators.  */
10264   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10265     cp_parser_enumerator_list (parser, type);
10266
10267   /* Consume the final '}'.  */
10268   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10269
10270   /* Look for trailing attributes to apply to this enumeration, and
10271      apply them if appropriate.  */
10272   if (cp_parser_allow_gnu_extensions_p (parser))
10273     {
10274       tree trailing_attr = cp_parser_attributes_opt (parser);
10275       cplus_decl_attributes (&type,
10276                              trailing_attr,
10277                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10278     }
10279
10280   /* Finish up the enumeration.  */
10281   finish_enum (type);
10282
10283   return type;
10284 }
10285
10286 /* Parse an enumerator-list.  The enumerators all have the indicated
10287    TYPE.
10288
10289    enumerator-list:
10290      enumerator-definition
10291      enumerator-list , enumerator-definition  */
10292
10293 static void
10294 cp_parser_enumerator_list (cp_parser* parser, tree type)
10295 {
10296   while (true)
10297     {
10298       /* Parse an enumerator-definition.  */
10299       cp_parser_enumerator_definition (parser, type);
10300
10301       /* If the next token is not a ',', we've reached the end of
10302          the list.  */
10303       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10304         break;
10305       /* Otherwise, consume the `,' and keep going.  */
10306       cp_lexer_consume_token (parser->lexer);
10307       /* If the next token is a `}', there is a trailing comma.  */
10308       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10309         {
10310           if (pedantic && !in_system_header)
10311             pedwarn ("comma at end of enumerator list");
10312           break;
10313         }
10314     }
10315 }
10316
10317 /* Parse an enumerator-definition.  The enumerator has the indicated
10318    TYPE.
10319
10320    enumerator-definition:
10321      enumerator
10322      enumerator = constant-expression
10323
10324    enumerator:
10325      identifier  */
10326
10327 static void
10328 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10329 {
10330   tree identifier;
10331   tree value;
10332
10333   /* Look for the identifier.  */
10334   identifier = cp_parser_identifier (parser);
10335   if (identifier == error_mark_node)
10336     return;
10337
10338   /* If the next token is an '=', then there is an explicit value.  */
10339   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10340     {
10341       /* Consume the `=' token.  */
10342       cp_lexer_consume_token (parser->lexer);
10343       /* Parse the value.  */
10344       value = cp_parser_constant_expression (parser,
10345                                              /*allow_non_constant_p=*/false,
10346                                              NULL);
10347     }
10348   else
10349     value = NULL_TREE;
10350
10351   /* Create the enumerator.  */
10352   build_enumerator (identifier, value, type);
10353 }
10354
10355 /* Parse a namespace-name.
10356
10357    namespace-name:
10358      original-namespace-name
10359      namespace-alias
10360
10361    Returns the NAMESPACE_DECL for the namespace.  */
10362
10363 static tree
10364 cp_parser_namespace_name (cp_parser* parser)
10365 {
10366   tree identifier;
10367   tree namespace_decl;
10368
10369   /* Get the name of the namespace.  */
10370   identifier = cp_parser_identifier (parser);
10371   if (identifier == error_mark_node)
10372     return error_mark_node;
10373
10374   /* Look up the identifier in the currently active scope.  Look only
10375      for namespaces, due to:
10376
10377        [basic.lookup.udir]
10378
10379        When looking up a namespace-name in a using-directive or alias
10380        definition, only namespace names are considered.
10381
10382      And:
10383
10384        [basic.lookup.qual]
10385
10386        During the lookup of a name preceding the :: scope resolution
10387        operator, object, function, and enumerator names are ignored.
10388
10389      (Note that cp_parser_class_or_namespace_name only calls this
10390      function if the token after the name is the scope resolution
10391      operator.)  */
10392   namespace_decl = cp_parser_lookup_name (parser, identifier,
10393                                           none_type,
10394                                           /*is_template=*/false,
10395                                           /*is_namespace=*/true,
10396                                           /*check_dependency=*/true,
10397                                           /*ambiguous_decls=*/NULL);
10398   /* If it's not a namespace, issue an error.  */
10399   if (namespace_decl == error_mark_node
10400       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10401     {
10402       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10403         error ("%qD is not a namespace-name", identifier);
10404       cp_parser_error (parser, "expected namespace-name");
10405       namespace_decl = error_mark_node;
10406     }
10407
10408   return namespace_decl;
10409 }
10410
10411 /* Parse a namespace-definition.
10412
10413    namespace-definition:
10414      named-namespace-definition
10415      unnamed-namespace-definition
10416
10417    named-namespace-definition:
10418      original-namespace-definition
10419      extension-namespace-definition
10420
10421    original-namespace-definition:
10422      namespace identifier { namespace-body }
10423
10424    extension-namespace-definition:
10425      namespace original-namespace-name { namespace-body }
10426
10427    unnamed-namespace-definition:
10428      namespace { namespace-body } */
10429
10430 static void
10431 cp_parser_namespace_definition (cp_parser* parser)
10432 {
10433   tree identifier;
10434
10435   /* Look for the `namespace' keyword.  */
10436   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10437
10438   /* Get the name of the namespace.  We do not attempt to distinguish
10439      between an original-namespace-definition and an
10440      extension-namespace-definition at this point.  The semantic
10441      analysis routines are responsible for that.  */
10442   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10443     identifier = cp_parser_identifier (parser);
10444   else
10445     identifier = NULL_TREE;
10446
10447   /* Look for the `{' to start the namespace.  */
10448   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10449   /* Start the namespace.  */
10450   push_namespace (identifier);
10451   /* Parse the body of the namespace.  */
10452   cp_parser_namespace_body (parser);
10453   /* Finish the namespace.  */
10454   pop_namespace ();
10455   /* Look for the final `}'.  */
10456   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10457 }
10458
10459 /* Parse a namespace-body.
10460
10461    namespace-body:
10462      declaration-seq [opt]  */
10463
10464 static void
10465 cp_parser_namespace_body (cp_parser* parser)
10466 {
10467   cp_parser_declaration_seq_opt (parser);
10468 }
10469
10470 /* Parse a namespace-alias-definition.
10471
10472    namespace-alias-definition:
10473      namespace identifier = qualified-namespace-specifier ;  */
10474
10475 static void
10476 cp_parser_namespace_alias_definition (cp_parser* parser)
10477 {
10478   tree identifier;
10479   tree namespace_specifier;
10480
10481   /* Look for the `namespace' keyword.  */
10482   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10483   /* Look for the identifier.  */
10484   identifier = cp_parser_identifier (parser);
10485   if (identifier == error_mark_node)
10486     return;
10487   /* Look for the `=' token.  */
10488   cp_parser_require (parser, CPP_EQ, "`='");
10489   /* Look for the qualified-namespace-specifier.  */
10490   namespace_specifier
10491     = cp_parser_qualified_namespace_specifier (parser);
10492   /* Look for the `;' token.  */
10493   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10494
10495   /* Register the alias in the symbol table.  */
10496   do_namespace_alias (identifier, namespace_specifier);
10497 }
10498
10499 /* Parse a qualified-namespace-specifier.
10500
10501    qualified-namespace-specifier:
10502      :: [opt] nested-name-specifier [opt] namespace-name
10503
10504    Returns a NAMESPACE_DECL corresponding to the specified
10505    namespace.  */
10506
10507 static tree
10508 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10509 {
10510   /* Look for the optional `::'.  */
10511   cp_parser_global_scope_opt (parser,
10512                               /*current_scope_valid_p=*/false);
10513
10514   /* Look for the optional nested-name-specifier.  */
10515   cp_parser_nested_name_specifier_opt (parser,
10516                                        /*typename_keyword_p=*/false,
10517                                        /*check_dependency_p=*/true,
10518                                        /*type_p=*/false,
10519                                        /*is_declaration=*/true);
10520
10521   return cp_parser_namespace_name (parser);
10522 }
10523
10524 /* Parse a using-declaration.
10525
10526    using-declaration:
10527      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10528      using :: unqualified-id ;  */
10529
10530 static void
10531 cp_parser_using_declaration (cp_parser* parser)
10532 {
10533   cp_token *token;
10534   bool typename_p = false;
10535   bool global_scope_p;
10536   tree decl;
10537   tree identifier;
10538   tree qscope;
10539
10540   /* Look for the `using' keyword.  */
10541   cp_parser_require_keyword (parser, RID_USING, "`using'");
10542
10543   /* Peek at the next token.  */
10544   token = cp_lexer_peek_token (parser->lexer);
10545   /* See if it's `typename'.  */
10546   if (token->keyword == RID_TYPENAME)
10547     {
10548       /* Remember that we've seen it.  */
10549       typename_p = true;
10550       /* Consume the `typename' token.  */
10551       cp_lexer_consume_token (parser->lexer);
10552     }
10553
10554   /* Look for the optional global scope qualification.  */
10555   global_scope_p
10556     = (cp_parser_global_scope_opt (parser,
10557                                    /*current_scope_valid_p=*/false)
10558        != NULL_TREE);
10559
10560   /* If we saw `typename', or didn't see `::', then there must be a
10561      nested-name-specifier present.  */
10562   if (typename_p || !global_scope_p)
10563     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10564                                               /*check_dependency_p=*/true,
10565                                               /*type_p=*/false,
10566                                               /*is_declaration=*/true);
10567   /* Otherwise, we could be in either of the two productions.  In that
10568      case, treat the nested-name-specifier as optional.  */
10569   else
10570     qscope = cp_parser_nested_name_specifier_opt (parser,
10571                                                   /*typename_keyword_p=*/false,
10572                                                   /*check_dependency_p=*/true,
10573                                                   /*type_p=*/false,
10574                                                   /*is_declaration=*/true);
10575   if (!qscope)
10576     qscope = global_namespace;
10577
10578   /* Parse the unqualified-id.  */
10579   identifier = cp_parser_unqualified_id (parser,
10580                                          /*template_keyword_p=*/false,
10581                                          /*check_dependency_p=*/true,
10582                                          /*declarator_p=*/true);
10583
10584   /* The function we call to handle a using-declaration is different
10585      depending on what scope we are in.  */
10586   if (qscope == error_mark_node || identifier == error_mark_node)
10587     ;
10588   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10589            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10590     /* [namespace.udecl]
10591
10592        A using declaration shall not name a template-id.  */
10593     error ("a template-id may not appear in a using-declaration");
10594   else
10595     {
10596       if (at_class_scope_p ())
10597         {
10598           /* Create the USING_DECL.  */
10599           decl = do_class_using_decl (parser->scope, identifier);
10600           /* Add it to the list of members in this class.  */
10601           finish_member_declaration (decl);
10602         }
10603       else
10604         {
10605           decl = cp_parser_lookup_name_simple (parser, identifier);
10606           if (decl == error_mark_node)
10607             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10608           else if (!at_namespace_scope_p ())
10609             do_local_using_decl (decl, qscope, identifier);
10610           else
10611             do_toplevel_using_decl (decl, qscope, identifier);
10612         }
10613     }
10614
10615   /* Look for the final `;'.  */
10616   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10617 }
10618
10619 /* Parse a using-directive.
10620
10621    using-directive:
10622      using namespace :: [opt] nested-name-specifier [opt]
10623        namespace-name ;  */
10624
10625 static void
10626 cp_parser_using_directive (cp_parser* parser)
10627 {
10628   tree namespace_decl;
10629   tree attribs;
10630
10631   /* Look for the `using' keyword.  */
10632   cp_parser_require_keyword (parser, RID_USING, "`using'");
10633   /* And the `namespace' keyword.  */
10634   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10635   /* Look for the optional `::' operator.  */
10636   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10637   /* And the optional nested-name-specifier.  */
10638   cp_parser_nested_name_specifier_opt (parser,
10639                                        /*typename_keyword_p=*/false,
10640                                        /*check_dependency_p=*/true,
10641                                        /*type_p=*/false,
10642                                        /*is_declaration=*/true);
10643   /* Get the namespace being used.  */
10644   namespace_decl = cp_parser_namespace_name (parser);
10645   /* And any specified attributes.  */
10646   attribs = cp_parser_attributes_opt (parser);
10647   /* Update the symbol table.  */
10648   parse_using_directive (namespace_decl, attribs);
10649   /* Look for the final `;'.  */
10650   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10651 }
10652
10653 /* Parse an asm-definition.
10654
10655    asm-definition:
10656      asm ( string-literal ) ;
10657
10658    GNU Extension:
10659
10660    asm-definition:
10661      asm volatile [opt] ( string-literal ) ;
10662      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10663      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10664                           : asm-operand-list [opt] ) ;
10665      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10666                           : asm-operand-list [opt]
10667                           : asm-operand-list [opt] ) ;  */
10668
10669 static void
10670 cp_parser_asm_definition (cp_parser* parser)
10671 {
10672   tree string;
10673   tree outputs = NULL_TREE;
10674   tree inputs = NULL_TREE;
10675   tree clobbers = NULL_TREE;
10676   tree asm_stmt;
10677   bool volatile_p = false;
10678   bool extended_p = false;
10679
10680   /* Look for the `asm' keyword.  */
10681   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10682   /* See if the next token is `volatile'.  */
10683   if (cp_parser_allow_gnu_extensions_p (parser)
10684       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10685     {
10686       /* Remember that we saw the `volatile' keyword.  */
10687       volatile_p = true;
10688       /* Consume the token.  */
10689       cp_lexer_consume_token (parser->lexer);
10690     }
10691   /* Look for the opening `('.  */
10692   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10693     return;
10694   /* Look for the string.  */
10695   string = cp_parser_string_literal (parser, false, false);
10696   if (string == error_mark_node)
10697     {
10698       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10699                                              /*consume_paren=*/true);
10700       return;
10701     }
10702
10703   /* If we're allowing GNU extensions, check for the extended assembly
10704      syntax.  Unfortunately, the `:' tokens need not be separated by
10705      a space in C, and so, for compatibility, we tolerate that here
10706      too.  Doing that means that we have to treat the `::' operator as
10707      two `:' tokens.  */
10708   if (cp_parser_allow_gnu_extensions_p (parser)
10709       && at_function_scope_p ()
10710       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10711           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10712     {
10713       bool inputs_p = false;
10714       bool clobbers_p = false;
10715
10716       /* The extended syntax was used.  */
10717       extended_p = true;
10718
10719       /* Look for outputs.  */
10720       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10721         {
10722           /* Consume the `:'.  */
10723           cp_lexer_consume_token (parser->lexer);
10724           /* Parse the output-operands.  */
10725           if (cp_lexer_next_token_is_not (parser->lexer,
10726                                           CPP_COLON)
10727               && cp_lexer_next_token_is_not (parser->lexer,
10728                                              CPP_SCOPE)
10729               && cp_lexer_next_token_is_not (parser->lexer,
10730                                              CPP_CLOSE_PAREN))
10731             outputs = cp_parser_asm_operand_list (parser);
10732         }
10733       /* If the next token is `::', there are no outputs, and the
10734          next token is the beginning of the inputs.  */
10735       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10736         /* The inputs are coming next.  */
10737         inputs_p = true;
10738
10739       /* Look for inputs.  */
10740       if (inputs_p
10741           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10742         {
10743           /* Consume the `:' or `::'.  */
10744           cp_lexer_consume_token (parser->lexer);
10745           /* Parse the output-operands.  */
10746           if (cp_lexer_next_token_is_not (parser->lexer,
10747                                           CPP_COLON)
10748               && cp_lexer_next_token_is_not (parser->lexer,
10749                                              CPP_CLOSE_PAREN))
10750             inputs = cp_parser_asm_operand_list (parser);
10751         }
10752       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10753         /* The clobbers are coming next.  */
10754         clobbers_p = true;
10755
10756       /* Look for clobbers.  */
10757       if (clobbers_p
10758           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10759         {
10760           /* Consume the `:' or `::'.  */
10761           cp_lexer_consume_token (parser->lexer);
10762           /* Parse the clobbers.  */
10763           if (cp_lexer_next_token_is_not (parser->lexer,
10764                                           CPP_CLOSE_PAREN))
10765             clobbers = cp_parser_asm_clobber_list (parser);
10766         }
10767     }
10768   /* Look for the closing `)'.  */
10769   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10770     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10771                                            /*consume_paren=*/true);
10772   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10773
10774   /* Create the ASM_EXPR.  */
10775   if (at_function_scope_p ())
10776     {
10777       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10778                                   inputs, clobbers);
10779       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10780       if (!extended_p)
10781         {
10782           tree temp = asm_stmt;
10783           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10784             temp = TREE_OPERAND (temp, 0);
10785
10786           ASM_INPUT_P (temp) = 1;
10787         }
10788     }
10789   else
10790     cgraph_add_asm_node (string);
10791 }
10792
10793 /* Declarators [gram.dcl.decl] */
10794
10795 /* Parse an init-declarator.
10796
10797    init-declarator:
10798      declarator initializer [opt]
10799
10800    GNU Extension:
10801
10802    init-declarator:
10803      declarator asm-specification [opt] attributes [opt] initializer [opt]
10804
10805    function-definition:
10806      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10807        function-body
10808      decl-specifier-seq [opt] declarator function-try-block
10809
10810    GNU Extension:
10811
10812    function-definition:
10813      __extension__ function-definition
10814
10815    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10816    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10817    then this declarator appears in a class scope.  The new DECL created
10818    by this declarator is returned.
10819
10820    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10821    for a function-definition here as well.  If the declarator is a
10822    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10823    be TRUE upon return.  By that point, the function-definition will
10824    have been completely parsed.
10825
10826    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10827    is FALSE.  */
10828
10829 static tree
10830 cp_parser_init_declarator (cp_parser* parser,
10831                            cp_decl_specifier_seq *decl_specifiers,
10832                            bool function_definition_allowed_p,
10833                            bool member_p,
10834                            int declares_class_or_enum,
10835                            bool* function_definition_p)
10836 {
10837   cp_token *token;
10838   cp_declarator *declarator;
10839   tree prefix_attributes;
10840   tree attributes;
10841   tree asm_specification;
10842   tree initializer;
10843   tree decl = NULL_TREE;
10844   tree scope;
10845   bool is_initialized;
10846   bool is_parenthesized_init;
10847   bool is_non_constant_init;
10848   int ctor_dtor_or_conv_p;
10849   bool friend_p;
10850   tree pushed_scope = NULL;
10851
10852   /* Gather the attributes that were provided with the
10853      decl-specifiers.  */
10854   prefix_attributes = decl_specifiers->attributes;
10855
10856   /* Assume that this is not the declarator for a function
10857      definition.  */
10858   if (function_definition_p)
10859     *function_definition_p = false;
10860
10861   /* Defer access checks while parsing the declarator; we cannot know
10862      what names are accessible until we know what is being
10863      declared.  */
10864   resume_deferring_access_checks ();
10865
10866   /* Parse the declarator.  */
10867   declarator
10868     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10869                             &ctor_dtor_or_conv_p,
10870                             /*parenthesized_p=*/NULL,
10871                             /*member_p=*/false);
10872   /* Gather up the deferred checks.  */
10873   stop_deferring_access_checks ();
10874
10875   /* If the DECLARATOR was erroneous, there's no need to go
10876      further.  */
10877   if (declarator == cp_error_declarator)
10878     return error_mark_node;
10879
10880   if (declares_class_or_enum & 2)
10881     cp_parser_check_for_definition_in_return_type (declarator,
10882                                                    decl_specifiers->type);
10883
10884   /* Figure out what scope the entity declared by the DECLARATOR is
10885      located in.  `grokdeclarator' sometimes changes the scope, so
10886      we compute it now.  */
10887   scope = get_scope_of_declarator (declarator);
10888
10889   /* If we're allowing GNU extensions, look for an asm-specification
10890      and attributes.  */
10891   if (cp_parser_allow_gnu_extensions_p (parser))
10892     {
10893       /* Look for an asm-specification.  */
10894       asm_specification = cp_parser_asm_specification_opt (parser);
10895       /* And attributes.  */
10896       attributes = cp_parser_attributes_opt (parser);
10897     }
10898   else
10899     {
10900       asm_specification = NULL_TREE;
10901       attributes = NULL_TREE;
10902     }
10903
10904   /* Peek at the next token.  */
10905   token = cp_lexer_peek_token (parser->lexer);
10906   /* Check to see if the token indicates the start of a
10907      function-definition.  */
10908   if (cp_parser_token_starts_function_definition_p (token))
10909     {
10910       if (!function_definition_allowed_p)
10911         {
10912           /* If a function-definition should not appear here, issue an
10913              error message.  */
10914           cp_parser_error (parser,
10915                            "a function-definition is not allowed here");
10916           return error_mark_node;
10917         }
10918       else
10919         {
10920           /* Neither attributes nor an asm-specification are allowed
10921              on a function-definition.  */
10922           if (asm_specification)
10923             error ("an asm-specification is not allowed on a function-definition");
10924           if (attributes)
10925             error ("attributes are not allowed on a function-definition");
10926           /* This is a function-definition.  */
10927           *function_definition_p = true;
10928
10929           /* Parse the function definition.  */
10930           if (member_p)
10931             decl = cp_parser_save_member_function_body (parser,
10932                                                         decl_specifiers,
10933                                                         declarator,
10934                                                         prefix_attributes);
10935           else
10936             decl
10937               = (cp_parser_function_definition_from_specifiers_and_declarator
10938                  (parser, decl_specifiers, prefix_attributes, declarator));
10939
10940           return decl;
10941         }
10942     }
10943
10944   /* [dcl.dcl]
10945
10946      Only in function declarations for constructors, destructors, and
10947      type conversions can the decl-specifier-seq be omitted.
10948
10949      We explicitly postpone this check past the point where we handle
10950      function-definitions because we tolerate function-definitions
10951      that are missing their return types in some modes.  */
10952   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10953     {
10954       cp_parser_error (parser,
10955                        "expected constructor, destructor, or type conversion");
10956       return error_mark_node;
10957     }
10958
10959   /* An `=' or an `(' indicates an initializer.  */
10960   is_initialized = (token->type == CPP_EQ
10961                      || token->type == CPP_OPEN_PAREN);
10962   /* If the init-declarator isn't initialized and isn't followed by a
10963      `,' or `;', it's not a valid init-declarator.  */
10964   if (!is_initialized
10965       && token->type != CPP_COMMA
10966       && token->type != CPP_SEMICOLON)
10967     {
10968       cp_parser_error (parser, "expected initializer");
10969       return error_mark_node;
10970     }
10971
10972   /* Because start_decl has side-effects, we should only call it if we
10973      know we're going ahead.  By this point, we know that we cannot
10974      possibly be looking at any other construct.  */
10975   cp_parser_commit_to_tentative_parse (parser);
10976
10977   /* If the decl specifiers were bad, issue an error now that we're
10978      sure this was intended to be a declarator.  Then continue
10979      declaring the variable(s), as int, to try to cut down on further
10980      errors.  */
10981   if (decl_specifiers->any_specifiers_p
10982       && decl_specifiers->type == error_mark_node)
10983     {
10984       cp_parser_error (parser, "invalid type in declaration");
10985       decl_specifiers->type = integer_type_node;
10986     }
10987
10988   /* Check to see whether or not this declaration is a friend.  */
10989   friend_p = cp_parser_friend_p (decl_specifiers);
10990
10991   /* Check that the number of template-parameter-lists is OK.  */
10992   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10993     return error_mark_node;
10994
10995   /* Enter the newly declared entry in the symbol table.  If we're
10996      processing a declaration in a class-specifier, we wait until
10997      after processing the initializer.  */
10998   if (!member_p)
10999     {
11000       if (parser->in_unbraced_linkage_specification_p)
11001         {
11002           decl_specifiers->storage_class = sc_extern;
11003           have_extern_spec = false;
11004         }
11005       decl = start_decl (declarator, decl_specifiers,
11006                          is_initialized, attributes, prefix_attributes,
11007                          &pushed_scope);
11008     }
11009   else if (scope)
11010     /* Enter the SCOPE.  That way unqualified names appearing in the
11011        initializer will be looked up in SCOPE.  */
11012     pushed_scope = push_scope (scope);
11013
11014   /* Perform deferred access control checks, now that we know in which
11015      SCOPE the declared entity resides.  */
11016   if (!member_p && decl)
11017     {
11018       tree saved_current_function_decl = NULL_TREE;
11019
11020       /* If the entity being declared is a function, pretend that we
11021          are in its scope.  If it is a `friend', it may have access to
11022          things that would not otherwise be accessible.  */
11023       if (TREE_CODE (decl) == FUNCTION_DECL)
11024         {
11025           saved_current_function_decl = current_function_decl;
11026           current_function_decl = decl;
11027         }
11028
11029       /* Perform the access control checks for the declarator and the
11030          the decl-specifiers.  */
11031       perform_deferred_access_checks ();
11032
11033       /* Restore the saved value.  */
11034       if (TREE_CODE (decl) == FUNCTION_DECL)
11035         current_function_decl = saved_current_function_decl;
11036     }
11037
11038   /* Parse the initializer.  */
11039   if (is_initialized)
11040     initializer = cp_parser_initializer (parser,
11041                                          &is_parenthesized_init,
11042                                          &is_non_constant_init);
11043   else
11044     {
11045       initializer = NULL_TREE;
11046       is_parenthesized_init = false;
11047       is_non_constant_init = true;
11048     }
11049
11050   /* The old parser allows attributes to appear after a parenthesized
11051      initializer.  Mark Mitchell proposed removing this functionality
11052      on the GCC mailing lists on 2002-08-13.  This parser accepts the
11053      attributes -- but ignores them.  */
11054   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11055     if (cp_parser_attributes_opt (parser))
11056       warning (OPT_Wattributes,
11057                "attributes after parenthesized initializer ignored");
11058
11059   /* For an in-class declaration, use `grokfield' to create the
11060      declaration.  */
11061   if (member_p)
11062     {
11063       if (pushed_scope)
11064         {
11065           pop_scope (pushed_scope);
11066           pushed_scope = false;
11067         }
11068       decl = grokfield (declarator, decl_specifiers,
11069                         initializer, !is_non_constant_init,
11070                         /*asmspec=*/NULL_TREE,
11071                         prefix_attributes);
11072       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11073         cp_parser_save_default_args (parser, decl);
11074     }
11075
11076   /* Finish processing the declaration.  But, skip friend
11077      declarations.  */
11078   if (!friend_p && decl && decl != error_mark_node)
11079     {
11080       cp_finish_decl (decl,
11081                       initializer, !is_non_constant_init,
11082                       asm_specification,
11083                       /* If the initializer is in parentheses, then this is
11084                          a direct-initialization, which means that an
11085                          `explicit' constructor is OK.  Otherwise, an
11086                          `explicit' constructor cannot be used.  */
11087                       ((is_parenthesized_init || !is_initialized)
11088                      ? 0 : LOOKUP_ONLYCONVERTING));
11089     }
11090   if (!friend_p && pushed_scope)
11091     pop_scope (pushed_scope);
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                 /* Create the declaration.  */
13710                 decl = grokfield (declarator, &decl_specifiers,
13711                                   initializer, /*init_const_expr_p=*/true,
13712                                   asm_specification,
13713                                   attributes);
13714             }
13715
13716           /* Reset PREFIX_ATTRIBUTES.  */
13717           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13718             attributes = TREE_CHAIN (attributes);
13719           if (attributes)
13720             TREE_CHAIN (attributes) = NULL_TREE;
13721
13722           /* If there is any qualification still in effect, clear it
13723              now; we will be starting fresh with the next declarator.  */
13724           parser->scope = NULL_TREE;
13725           parser->qualifying_scope = NULL_TREE;
13726           parser->object_scope = NULL_TREE;
13727           /* If it's a `,', then there are more declarators.  */
13728           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13729             cp_lexer_consume_token (parser->lexer);
13730           /* If the next token isn't a `;', then we have a parse error.  */
13731           else if (cp_lexer_next_token_is_not (parser->lexer,
13732                                                CPP_SEMICOLON))
13733             {
13734               cp_parser_error (parser, "expected %<;%>");
13735               /* Skip tokens until we find a `;'.  */
13736               cp_parser_skip_to_end_of_statement (parser);
13737
13738               break;
13739             }
13740
13741           if (decl)
13742             {
13743               /* Add DECL to the list of members.  */
13744               if (!friend_p)
13745                 finish_member_declaration (decl);
13746
13747               if (TREE_CODE (decl) == FUNCTION_DECL)
13748                 cp_parser_save_default_args (parser, decl);
13749             }
13750         }
13751     }
13752
13753   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13754 }
13755
13756 /* Parse a pure-specifier.
13757
13758    pure-specifier:
13759      = 0
13760
13761    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13762    Otherwise, ERROR_MARK_NODE is returned.  */
13763
13764 static tree
13765 cp_parser_pure_specifier (cp_parser* parser)
13766 {
13767   cp_token *token;
13768
13769   /* Look for the `=' token.  */
13770   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13771     return error_mark_node;
13772   /* Look for the `0' token.  */
13773   token = cp_lexer_consume_token (parser->lexer);
13774   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
13775   if (token->type == CPP_NUMBER && (token->flags & PURE_ZERO))
13776     return integer_zero_node;
13777
13778   cp_parser_error (parser, "invalid pure specifier (only `= 0' is allowed)");
13779   cp_parser_skip_to_end_of_statement (parser);
13780   return error_mark_node;
13781 }
13782
13783 /* Parse a constant-initializer.
13784
13785    constant-initializer:
13786      = constant-expression
13787
13788    Returns a representation of the constant-expression.  */
13789
13790 static tree
13791 cp_parser_constant_initializer (cp_parser* parser)
13792 {
13793   /* Look for the `=' token.  */
13794   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13795     return error_mark_node;
13796
13797   /* It is invalid to write:
13798
13799        struct S { static const int i = { 7 }; };
13800
13801      */
13802   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13803     {
13804       cp_parser_error (parser,
13805                        "a brace-enclosed initializer is not allowed here");
13806       /* Consume the opening brace.  */
13807       cp_lexer_consume_token (parser->lexer);
13808       /* Skip the initializer.  */
13809       cp_parser_skip_to_closing_brace (parser);
13810       /* Look for the trailing `}'.  */
13811       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13812
13813       return error_mark_node;
13814     }
13815
13816   return cp_parser_constant_expression (parser,
13817                                         /*allow_non_constant=*/false,
13818                                         NULL);
13819 }
13820
13821 /* Derived classes [gram.class.derived] */
13822
13823 /* Parse a base-clause.
13824
13825    base-clause:
13826      : base-specifier-list
13827
13828    base-specifier-list:
13829      base-specifier
13830      base-specifier-list , base-specifier
13831
13832    Returns a TREE_LIST representing the base-classes, in the order in
13833    which they were declared.  The representation of each node is as
13834    described by cp_parser_base_specifier.
13835
13836    In the case that no bases are specified, this function will return
13837    NULL_TREE, not ERROR_MARK_NODE.  */
13838
13839 static tree
13840 cp_parser_base_clause (cp_parser* parser)
13841 {
13842   tree bases = NULL_TREE;
13843
13844   /* Look for the `:' that begins the list.  */
13845   cp_parser_require (parser, CPP_COLON, "`:'");
13846
13847   /* Scan the base-specifier-list.  */
13848   while (true)
13849     {
13850       cp_token *token;
13851       tree base;
13852
13853       /* Look for the base-specifier.  */
13854       base = cp_parser_base_specifier (parser);
13855       /* Add BASE to the front of the list.  */
13856       if (base != error_mark_node)
13857         {
13858           TREE_CHAIN (base) = bases;
13859           bases = base;
13860         }
13861       /* Peek at the next token.  */
13862       token = cp_lexer_peek_token (parser->lexer);
13863       /* If it's not a comma, then the list is complete.  */
13864       if (token->type != CPP_COMMA)
13865         break;
13866       /* Consume the `,'.  */
13867       cp_lexer_consume_token (parser->lexer);
13868     }
13869
13870   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13871      base class had a qualified name.  However, the next name that
13872      appears is certainly not qualified.  */
13873   parser->scope = NULL_TREE;
13874   parser->qualifying_scope = NULL_TREE;
13875   parser->object_scope = NULL_TREE;
13876
13877   return nreverse (bases);
13878 }
13879
13880 /* Parse a base-specifier.
13881
13882    base-specifier:
13883      :: [opt] nested-name-specifier [opt] class-name
13884      virtual access-specifier [opt] :: [opt] nested-name-specifier
13885        [opt] class-name
13886      access-specifier virtual [opt] :: [opt] nested-name-specifier
13887        [opt] class-name
13888
13889    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13890    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13891    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13892    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13893
13894 static tree
13895 cp_parser_base_specifier (cp_parser* parser)
13896 {
13897   cp_token *token;
13898   bool done = false;
13899   bool virtual_p = false;
13900   bool duplicate_virtual_error_issued_p = false;
13901   bool duplicate_access_error_issued_p = false;
13902   bool class_scope_p, template_p;
13903   tree access = access_default_node;
13904   tree type;
13905
13906   /* Process the optional `virtual' and `access-specifier'.  */
13907   while (!done)
13908     {
13909       /* Peek at the next token.  */
13910       token = cp_lexer_peek_token (parser->lexer);
13911       /* Process `virtual'.  */
13912       switch (token->keyword)
13913         {
13914         case RID_VIRTUAL:
13915           /* If `virtual' appears more than once, issue an error.  */
13916           if (virtual_p && !duplicate_virtual_error_issued_p)
13917             {
13918               cp_parser_error (parser,
13919                                "%<virtual%> specified more than once in base-specified");
13920               duplicate_virtual_error_issued_p = true;
13921             }
13922
13923           virtual_p = true;
13924
13925           /* Consume the `virtual' token.  */
13926           cp_lexer_consume_token (parser->lexer);
13927
13928           break;
13929
13930         case RID_PUBLIC:
13931         case RID_PROTECTED:
13932         case RID_PRIVATE:
13933           /* If more than one access specifier appears, issue an
13934              error.  */
13935           if (access != access_default_node
13936               && !duplicate_access_error_issued_p)
13937             {
13938               cp_parser_error (parser,
13939                                "more than one access specifier in base-specified");
13940               duplicate_access_error_issued_p = true;
13941             }
13942
13943           access = ridpointers[(int) token->keyword];
13944
13945           /* Consume the access-specifier.  */
13946           cp_lexer_consume_token (parser->lexer);
13947
13948           break;
13949
13950         default:
13951           done = true;
13952           break;
13953         }
13954     }
13955   /* It is not uncommon to see programs mechanically, erroneously, use
13956      the 'typename' keyword to denote (dependent) qualified types
13957      as base classes.  */
13958   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13959     {
13960       if (!processing_template_decl)
13961         error ("keyword %<typename%> not allowed outside of templates");
13962       else
13963         error ("keyword %<typename%> not allowed in this context "
13964                "(the base class is implicitly a type)");
13965       cp_lexer_consume_token (parser->lexer);
13966     }
13967
13968   /* Look for the optional `::' operator.  */
13969   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13970   /* Look for the nested-name-specifier.  The simplest way to
13971      implement:
13972
13973        [temp.res]
13974
13975        The keyword `typename' is not permitted in a base-specifier or
13976        mem-initializer; in these contexts a qualified name that
13977        depends on a template-parameter is implicitly assumed to be a
13978        type name.
13979
13980      is to pretend that we have seen the `typename' keyword at this
13981      point.  */
13982   cp_parser_nested_name_specifier_opt (parser,
13983                                        /*typename_keyword_p=*/true,
13984                                        /*check_dependency_p=*/true,
13985                                        typename_type,
13986                                        /*is_declaration=*/true);
13987   /* If the base class is given by a qualified name, assume that names
13988      we see are type names or templates, as appropriate.  */
13989   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13990   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13991
13992   /* Finally, look for the class-name.  */
13993   type = cp_parser_class_name (parser,
13994                                class_scope_p,
13995                                template_p,
13996                                typename_type,
13997                                /*check_dependency_p=*/true,
13998                                /*class_head_p=*/false,
13999                                /*is_declaration=*/true);
14000
14001   if (type == error_mark_node)
14002     return error_mark_node;
14003
14004   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14005 }
14006
14007 /* Exception handling [gram.exception] */
14008
14009 /* Parse an (optional) exception-specification.
14010
14011    exception-specification:
14012      throw ( type-id-list [opt] )
14013
14014    Returns a TREE_LIST representing the exception-specification.  The
14015    TREE_VALUE of each node is a type.  */
14016
14017 static tree
14018 cp_parser_exception_specification_opt (cp_parser* parser)
14019 {
14020   cp_token *token;
14021   tree type_id_list;
14022
14023   /* Peek at the next token.  */
14024   token = cp_lexer_peek_token (parser->lexer);
14025   /* If it's not `throw', then there's no exception-specification.  */
14026   if (!cp_parser_is_keyword (token, RID_THROW))
14027     return NULL_TREE;
14028
14029   /* Consume the `throw'.  */
14030   cp_lexer_consume_token (parser->lexer);
14031
14032   /* Look for the `('.  */
14033   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14034
14035   /* Peek at the next token.  */
14036   token = cp_lexer_peek_token (parser->lexer);
14037   /* If it's not a `)', then there is a type-id-list.  */
14038   if (token->type != CPP_CLOSE_PAREN)
14039     {
14040       const char *saved_message;
14041
14042       /* Types may not be defined in an exception-specification.  */
14043       saved_message = parser->type_definition_forbidden_message;
14044       parser->type_definition_forbidden_message
14045         = "types may not be defined in an exception-specification";
14046       /* Parse the type-id-list.  */
14047       type_id_list = cp_parser_type_id_list (parser);
14048       /* Restore the saved message.  */
14049       parser->type_definition_forbidden_message = saved_message;
14050     }
14051   else
14052     type_id_list = empty_except_spec;
14053
14054   /* Look for the `)'.  */
14055   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14056
14057   return type_id_list;
14058 }
14059
14060 /* Parse an (optional) type-id-list.
14061
14062    type-id-list:
14063      type-id
14064      type-id-list , type-id
14065
14066    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14067    in the order that the types were presented.  */
14068
14069 static tree
14070 cp_parser_type_id_list (cp_parser* parser)
14071 {
14072   tree types = NULL_TREE;
14073
14074   while (true)
14075     {
14076       cp_token *token;
14077       tree type;
14078
14079       /* Get the next type-id.  */
14080       type = cp_parser_type_id (parser);
14081       /* Add it to the list.  */
14082       types = add_exception_specifier (types, type, /*complain=*/1);
14083       /* Peek at the next token.  */
14084       token = cp_lexer_peek_token (parser->lexer);
14085       /* If it is not a `,', we are done.  */
14086       if (token->type != CPP_COMMA)
14087         break;
14088       /* Consume the `,'.  */
14089       cp_lexer_consume_token (parser->lexer);
14090     }
14091
14092   return nreverse (types);
14093 }
14094
14095 /* Parse a try-block.
14096
14097    try-block:
14098      try compound-statement handler-seq  */
14099
14100 static tree
14101 cp_parser_try_block (cp_parser* parser)
14102 {
14103   tree try_block;
14104
14105   cp_parser_require_keyword (parser, RID_TRY, "`try'");
14106   try_block = begin_try_block ();
14107   cp_parser_compound_statement (parser, NULL, true);
14108   finish_try_block (try_block);
14109   cp_parser_handler_seq (parser);
14110   finish_handler_sequence (try_block);
14111
14112   return try_block;
14113 }
14114
14115 /* Parse a function-try-block.
14116
14117    function-try-block:
14118      try ctor-initializer [opt] function-body handler-seq  */
14119
14120 static bool
14121 cp_parser_function_try_block (cp_parser* parser)
14122 {
14123   tree try_block;
14124   bool ctor_initializer_p;
14125
14126   /* Look for the `try' keyword.  */
14127   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14128     return false;
14129   /* Let the rest of the front-end know where we are.  */
14130   try_block = begin_function_try_block ();
14131   /* Parse the function-body.  */
14132   ctor_initializer_p
14133     = cp_parser_ctor_initializer_opt_and_function_body (parser);
14134   /* We're done with the `try' part.  */
14135   finish_function_try_block (try_block);
14136   /* Parse the handlers.  */
14137   cp_parser_handler_seq (parser);
14138   /* We're done with the handlers.  */
14139   finish_function_handler_sequence (try_block);
14140
14141   return ctor_initializer_p;
14142 }
14143
14144 /* Parse a handler-seq.
14145
14146    handler-seq:
14147      handler handler-seq [opt]  */
14148
14149 static void
14150 cp_parser_handler_seq (cp_parser* parser)
14151 {
14152   while (true)
14153     {
14154       cp_token *token;
14155
14156       /* Parse the handler.  */
14157       cp_parser_handler (parser);
14158       /* Peek at the next token.  */
14159       token = cp_lexer_peek_token (parser->lexer);
14160       /* If it's not `catch' then there are no more handlers.  */
14161       if (!cp_parser_is_keyword (token, RID_CATCH))
14162         break;
14163     }
14164 }
14165
14166 /* Parse a handler.
14167
14168    handler:
14169      catch ( exception-declaration ) compound-statement  */
14170
14171 static void
14172 cp_parser_handler (cp_parser* parser)
14173 {
14174   tree handler;
14175   tree declaration;
14176
14177   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14178   handler = begin_handler ();
14179   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14180   declaration = cp_parser_exception_declaration (parser);
14181   finish_handler_parms (declaration, handler);
14182   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14183   cp_parser_compound_statement (parser, NULL, false);
14184   finish_handler (handler);
14185 }
14186
14187 /* Parse an exception-declaration.
14188
14189    exception-declaration:
14190      type-specifier-seq declarator
14191      type-specifier-seq abstract-declarator
14192      type-specifier-seq
14193      ...
14194
14195    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14196    ellipsis variant is used.  */
14197
14198 static tree
14199 cp_parser_exception_declaration (cp_parser* parser)
14200 {
14201   tree decl;
14202   cp_decl_specifier_seq type_specifiers;
14203   cp_declarator *declarator;
14204   const char *saved_message;
14205
14206   /* If it's an ellipsis, it's easy to handle.  */
14207   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14208     {
14209       /* Consume the `...' token.  */
14210       cp_lexer_consume_token (parser->lexer);
14211       return NULL_TREE;
14212     }
14213
14214   /* Types may not be defined in exception-declarations.  */
14215   saved_message = parser->type_definition_forbidden_message;
14216   parser->type_definition_forbidden_message
14217     = "types may not be defined in exception-declarations";
14218
14219   /* Parse the type-specifier-seq.  */
14220   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14221                                 &type_specifiers);
14222   /* If it's a `)', then there is no declarator.  */
14223   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14224     declarator = NULL;
14225   else
14226     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14227                                        /*ctor_dtor_or_conv_p=*/NULL,
14228                                        /*parenthesized_p=*/NULL,
14229                                        /*member_p=*/false);
14230
14231   /* Restore the saved message.  */
14232   parser->type_definition_forbidden_message = saved_message;
14233
14234   if (type_specifiers.any_specifiers_p)
14235     {
14236       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14237       if (decl == NULL_TREE)
14238         error ("invalid catch parameter");
14239     }
14240   else
14241     decl = NULL_TREE;
14242
14243   return decl;
14244 }
14245
14246 /* Parse a throw-expression.
14247
14248    throw-expression:
14249      throw assignment-expression [opt]
14250
14251    Returns a THROW_EXPR representing the throw-expression.  */
14252
14253 static tree
14254 cp_parser_throw_expression (cp_parser* parser)
14255 {
14256   tree expression;
14257   cp_token* token;
14258
14259   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14260   token = cp_lexer_peek_token (parser->lexer);
14261   /* Figure out whether or not there is an assignment-expression
14262      following the "throw" keyword.  */
14263   if (token->type == CPP_COMMA
14264       || token->type == CPP_SEMICOLON
14265       || token->type == CPP_CLOSE_PAREN
14266       || token->type == CPP_CLOSE_SQUARE
14267       || token->type == CPP_CLOSE_BRACE
14268       || token->type == CPP_COLON)
14269     expression = NULL_TREE;
14270   else
14271     expression = cp_parser_assignment_expression (parser,
14272                                                   /*cast_p=*/false);
14273
14274   return build_throw (expression);
14275 }
14276
14277 /* GNU Extensions */
14278
14279 /* Parse an (optional) asm-specification.
14280
14281    asm-specification:
14282      asm ( string-literal )
14283
14284    If the asm-specification is present, returns a STRING_CST
14285    corresponding to the string-literal.  Otherwise, returns
14286    NULL_TREE.  */
14287
14288 static tree
14289 cp_parser_asm_specification_opt (cp_parser* parser)
14290 {
14291   cp_token *token;
14292   tree asm_specification;
14293
14294   /* Peek at the next token.  */
14295   token = cp_lexer_peek_token (parser->lexer);
14296   /* If the next token isn't the `asm' keyword, then there's no
14297      asm-specification.  */
14298   if (!cp_parser_is_keyword (token, RID_ASM))
14299     return NULL_TREE;
14300
14301   /* Consume the `asm' token.  */
14302   cp_lexer_consume_token (parser->lexer);
14303   /* Look for the `('.  */
14304   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14305
14306   /* Look for the string-literal.  */
14307   asm_specification = cp_parser_string_literal (parser, false, false);
14308
14309   /* Look for the `)'.  */
14310   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14311
14312   return asm_specification;
14313 }
14314
14315 /* Parse an asm-operand-list.
14316
14317    asm-operand-list:
14318      asm-operand
14319      asm-operand-list , asm-operand
14320
14321    asm-operand:
14322      string-literal ( expression )
14323      [ string-literal ] string-literal ( expression )
14324
14325    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14326    each node is the expression.  The TREE_PURPOSE is itself a
14327    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14328    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14329    is a STRING_CST for the string literal before the parenthesis.  */
14330
14331 static tree
14332 cp_parser_asm_operand_list (cp_parser* parser)
14333 {
14334   tree asm_operands = NULL_TREE;
14335
14336   while (true)
14337     {
14338       tree string_literal;
14339       tree expression;
14340       tree name;
14341
14342       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14343         {
14344           /* Consume the `[' token.  */
14345           cp_lexer_consume_token (parser->lexer);
14346           /* Read the operand name.  */
14347           name = cp_parser_identifier (parser);
14348           if (name != error_mark_node)
14349             name = build_string (IDENTIFIER_LENGTH (name),
14350                                  IDENTIFIER_POINTER (name));
14351           /* Look for the closing `]'.  */
14352           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14353         }
14354       else
14355         name = NULL_TREE;
14356       /* Look for the string-literal.  */
14357       string_literal = cp_parser_string_literal (parser, false, false);
14358
14359       /* Look for the `('.  */
14360       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14361       /* Parse the expression.  */
14362       expression = cp_parser_expression (parser, /*cast_p=*/false);
14363       /* Look for the `)'.  */
14364       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14365
14366       /* Add this operand to the list.  */
14367       asm_operands = tree_cons (build_tree_list (name, string_literal),
14368                                 expression,
14369                                 asm_operands);
14370       /* If the next token is not a `,', there are no more
14371          operands.  */
14372       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14373         break;
14374       /* Consume the `,'.  */
14375       cp_lexer_consume_token (parser->lexer);
14376     }
14377
14378   return nreverse (asm_operands);
14379 }
14380
14381 /* Parse an asm-clobber-list.
14382
14383    asm-clobber-list:
14384      string-literal
14385      asm-clobber-list , string-literal
14386
14387    Returns a TREE_LIST, indicating the clobbers in the order that they
14388    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14389
14390 static tree
14391 cp_parser_asm_clobber_list (cp_parser* parser)
14392 {
14393   tree clobbers = NULL_TREE;
14394
14395   while (true)
14396     {
14397       tree string_literal;
14398
14399       /* Look for the string literal.  */
14400       string_literal = cp_parser_string_literal (parser, false, false);
14401       /* Add it to the list.  */
14402       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14403       /* If the next token is not a `,', then the list is
14404          complete.  */
14405       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14406         break;
14407       /* Consume the `,' token.  */
14408       cp_lexer_consume_token (parser->lexer);
14409     }
14410
14411   return clobbers;
14412 }
14413
14414 /* Parse an (optional) series of attributes.
14415
14416    attributes:
14417      attributes attribute
14418
14419    attribute:
14420      __attribute__ (( attribute-list [opt] ))
14421
14422    The return value is as for cp_parser_attribute_list.  */
14423
14424 static tree
14425 cp_parser_attributes_opt (cp_parser* parser)
14426 {
14427   tree attributes = NULL_TREE;
14428
14429   while (true)
14430     {
14431       cp_token *token;
14432       tree attribute_list;
14433
14434       /* Peek at the next token.  */
14435       token = cp_lexer_peek_token (parser->lexer);
14436       /* If it's not `__attribute__', then we're done.  */
14437       if (token->keyword != RID_ATTRIBUTE)
14438         break;
14439
14440       /* Consume the `__attribute__' keyword.  */
14441       cp_lexer_consume_token (parser->lexer);
14442       /* Look for the two `(' tokens.  */
14443       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14444       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14445
14446       /* Peek at the next token.  */
14447       token = cp_lexer_peek_token (parser->lexer);
14448       if (token->type != CPP_CLOSE_PAREN)
14449         /* Parse the attribute-list.  */
14450         attribute_list = cp_parser_attribute_list (parser);
14451       else
14452         /* If the next token is a `)', then there is no attribute
14453            list.  */
14454         attribute_list = NULL;
14455
14456       /* Look for the two `)' tokens.  */
14457       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14458       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14459
14460       /* Add these new attributes to the list.  */
14461       attributes = chainon (attributes, attribute_list);
14462     }
14463
14464   return attributes;
14465 }
14466
14467 /* Parse an attribute-list.
14468
14469    attribute-list:
14470      attribute
14471      attribute-list , attribute
14472
14473    attribute:
14474      identifier
14475      identifier ( identifier )
14476      identifier ( identifier , expression-list )
14477      identifier ( expression-list )
14478
14479    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14480    to an attribute.  The TREE_PURPOSE of each node is the identifier
14481    indicating which attribute is in use.  The TREE_VALUE represents
14482    the arguments, if any.  */
14483
14484 static tree
14485 cp_parser_attribute_list (cp_parser* parser)
14486 {
14487   tree attribute_list = NULL_TREE;
14488   bool save_translate_strings_p = parser->translate_strings_p;
14489
14490   parser->translate_strings_p = false;
14491   while (true)
14492     {
14493       cp_token *token;
14494       tree identifier;
14495       tree attribute;
14496
14497       /* Look for the identifier.  We also allow keywords here; for
14498          example `__attribute__ ((const))' is legal.  */
14499       token = cp_lexer_peek_token (parser->lexer);
14500       if (token->type == CPP_NAME
14501           || token->type == CPP_KEYWORD)
14502         {
14503           /* Consume the token.  */
14504           token = cp_lexer_consume_token (parser->lexer);
14505
14506           /* Save away the identifier that indicates which attribute
14507              this is.  */
14508           identifier = token->value;
14509           attribute = build_tree_list (identifier, NULL_TREE);
14510
14511           /* Peek at the next token.  */
14512           token = cp_lexer_peek_token (parser->lexer);
14513           /* If it's an `(', then parse the attribute arguments.  */
14514           if (token->type == CPP_OPEN_PAREN)
14515             {
14516               tree arguments;
14517
14518               arguments = (cp_parser_parenthesized_expression_list
14519                            (parser, true, /*cast_p=*/false,
14520                             /*non_constant_p=*/NULL));
14521               /* Save the identifier and arguments away.  */
14522               TREE_VALUE (attribute) = arguments;
14523             }
14524
14525           /* Add this attribute to the list.  */
14526           TREE_CHAIN (attribute) = attribute_list;
14527           attribute_list = attribute;
14528
14529           token = cp_lexer_peek_token (parser->lexer);
14530         }
14531       /* Now, look for more attributes.  If the next token isn't a
14532          `,', we're done.  */
14533       if (token->type != CPP_COMMA)
14534         break;
14535
14536       /* Consume the comma and keep going.  */
14537       cp_lexer_consume_token (parser->lexer);
14538     }
14539   parser->translate_strings_p = save_translate_strings_p;
14540
14541   /* We built up the list in reverse order.  */
14542   return nreverse (attribute_list);
14543 }
14544
14545 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14546    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14547    current value of the PEDANTIC flag, regardless of whether or not
14548    the `__extension__' keyword is present.  The caller is responsible
14549    for restoring the value of the PEDANTIC flag.  */
14550
14551 static bool
14552 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14553 {
14554   /* Save the old value of the PEDANTIC flag.  */
14555   *saved_pedantic = pedantic;
14556
14557   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14558     {
14559       /* Consume the `__extension__' token.  */
14560       cp_lexer_consume_token (parser->lexer);
14561       /* We're not being pedantic while the `__extension__' keyword is
14562          in effect.  */
14563       pedantic = 0;
14564
14565       return true;
14566     }
14567
14568   return false;
14569 }
14570
14571 /* Parse a label declaration.
14572
14573    label-declaration:
14574      __label__ label-declarator-seq ;
14575
14576    label-declarator-seq:
14577      identifier , label-declarator-seq
14578      identifier  */
14579
14580 static void
14581 cp_parser_label_declaration (cp_parser* parser)
14582 {
14583   /* Look for the `__label__' keyword.  */
14584   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14585
14586   while (true)
14587     {
14588       tree identifier;
14589
14590       /* Look for an identifier.  */
14591       identifier = cp_parser_identifier (parser);
14592       /* If we failed, stop.  */
14593       if (identifier == error_mark_node)
14594         break;
14595       /* Declare it as a label.  */
14596       finish_label_decl (identifier);
14597       /* If the next token is a `;', stop.  */
14598       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14599         break;
14600       /* Look for the `,' separating the label declarations.  */
14601       cp_parser_require (parser, CPP_COMMA, "`,'");
14602     }
14603
14604   /* Look for the final `;'.  */
14605   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14606 }
14607
14608 /* Support Functions */
14609
14610 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14611    NAME should have one of the representations used for an
14612    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14613    is returned.  If PARSER->SCOPE is a dependent type, then a
14614    SCOPE_REF is returned.
14615
14616    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14617    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14618    was formed.  Abstractly, such entities should not be passed to this
14619    function, because they do not need to be looked up, but it is
14620    simpler to check for this special case here, rather than at the
14621    call-sites.
14622
14623    In cases not explicitly covered above, this function returns a
14624    DECL, OVERLOAD, or baselink representing the result of the lookup.
14625    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14626    is returned.
14627
14628    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14629    (e.g., "struct") that was used.  In that case bindings that do not
14630    refer to types are ignored.
14631
14632    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14633    ignored.
14634
14635    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14636    are ignored.
14637
14638    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14639    types.
14640
14641    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14642    TREE_LIST of candidates if name-lookup results in an ambiguity, and
14643    NULL_TREE otherwise.  */ 
14644
14645 static tree
14646 cp_parser_lookup_name (cp_parser *parser, tree name,
14647                        enum tag_types tag_type,
14648                        bool is_template, 
14649                        bool is_namespace,
14650                        bool check_dependency,
14651                        tree *ambiguous_decls)
14652 {
14653   int flags = 0;
14654   tree decl;
14655   tree object_type = parser->context->object_type;
14656
14657   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14658     flags |= LOOKUP_COMPLAIN;
14659
14660   /* Assume that the lookup will be unambiguous.  */
14661   if (ambiguous_decls)
14662     *ambiguous_decls = NULL_TREE;
14663
14664   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14665      no longer valid.  Note that if we are parsing tentatively, and
14666      the parse fails, OBJECT_TYPE will be automatically restored.  */
14667   parser->context->object_type = NULL_TREE;
14668
14669   if (name == error_mark_node)
14670     return error_mark_node;
14671
14672   /* A template-id has already been resolved; there is no lookup to
14673      do.  */
14674   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14675     return name;
14676   if (BASELINK_P (name))
14677     {
14678       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14679                   == TEMPLATE_ID_EXPR);
14680       return name;
14681     }
14682
14683   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14684      it should already have been checked to make sure that the name
14685      used matches the type being destroyed.  */
14686   if (TREE_CODE (name) == BIT_NOT_EXPR)
14687     {
14688       tree type;
14689
14690       /* Figure out to which type this destructor applies.  */
14691       if (parser->scope)
14692         type = parser->scope;
14693       else if (object_type)
14694         type = object_type;
14695       else
14696         type = current_class_type;
14697       /* If that's not a class type, there is no destructor.  */
14698       if (!type || !CLASS_TYPE_P (type))
14699         return error_mark_node;
14700       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14701         lazily_declare_fn (sfk_destructor, type);
14702       if (!CLASSTYPE_DESTRUCTORS (type))
14703           return error_mark_node;
14704       /* If it was a class type, return the destructor.  */
14705       return CLASSTYPE_DESTRUCTORS (type);
14706     }
14707
14708   /* By this point, the NAME should be an ordinary identifier.  If
14709      the id-expression was a qualified name, the qualifying scope is
14710      stored in PARSER->SCOPE at this point.  */
14711   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14712
14713   /* Perform the lookup.  */
14714   if (parser->scope)
14715     {
14716       bool dependent_p;
14717
14718       if (parser->scope == error_mark_node)
14719         return error_mark_node;
14720
14721       /* If the SCOPE is dependent, the lookup must be deferred until
14722          the template is instantiated -- unless we are explicitly
14723          looking up names in uninstantiated templates.  Even then, we
14724          cannot look up the name if the scope is not a class type; it
14725          might, for example, be a template type parameter.  */
14726       dependent_p = (TYPE_P (parser->scope)
14727                      && !(parser->in_declarator_p
14728                           && currently_open_class (parser->scope))
14729                      && dependent_type_p (parser->scope));
14730       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14731            && dependent_p)
14732         {
14733           if (tag_type)
14734             {
14735               tree type;
14736
14737               /* The resolution to Core Issue 180 says that `struct
14738                  A::B' should be considered a type-name, even if `A'
14739                  is dependent.  */
14740               type = make_typename_type (parser->scope, name, tag_type,
14741                                          /*complain=*/tf_error);
14742               decl = TYPE_NAME (type);
14743             }
14744           else if (is_template
14745                    && (cp_parser_next_token_ends_template_argument_p (parser)
14746                        || cp_lexer_next_token_is (parser->lexer,
14747                                                   CPP_CLOSE_PAREN)))
14748             decl = make_unbound_class_template (parser->scope,
14749                                                 name, NULL_TREE,
14750                                                 /*complain=*/tf_error);
14751           else
14752             decl = build_qualified_name (/*type=*/NULL_TREE,
14753                                          parser->scope, name,
14754                                          is_template);
14755         }
14756       else
14757         {
14758           tree pushed_scope = NULL_TREE;
14759
14760           /* If PARSER->SCOPE is a dependent type, then it must be a
14761              class type, and we must not be checking dependencies;
14762              otherwise, we would have processed this lookup above.  So
14763              that PARSER->SCOPE is not considered a dependent base by
14764              lookup_member, we must enter the scope here.  */
14765           if (dependent_p)
14766             pushed_scope = push_scope (parser->scope);
14767           /* If the PARSER->SCOPE is a template specialization, it
14768              may be instantiated during name lookup.  In that case,
14769              errors may be issued.  Even if we rollback the current
14770              tentative parse, those errors are valid.  */
14771           decl = lookup_qualified_name (parser->scope, name,
14772                                         tag_type != none_type,
14773                                         /*complain=*/true);
14774           if (pushed_scope)
14775             pop_scope (pushed_scope);
14776         }
14777       parser->qualifying_scope = parser->scope;
14778       parser->object_scope = NULL_TREE;
14779     }
14780   else if (object_type)
14781     {
14782       tree object_decl = NULL_TREE;
14783       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14784          OBJECT_TYPE is not a class.  */
14785       if (CLASS_TYPE_P (object_type))
14786         /* If the OBJECT_TYPE is a template specialization, it may
14787            be instantiated during name lookup.  In that case, errors
14788            may be issued.  Even if we rollback the current tentative
14789            parse, those errors are valid.  */
14790         object_decl = lookup_member (object_type,
14791                                      name,
14792                                      /*protect=*/0,
14793                                      tag_type != none_type);
14794       /* Look it up in the enclosing context, too.  */
14795       decl = lookup_name_real (name, tag_type != none_type,
14796                                /*nonclass=*/0,
14797                                /*block_p=*/true, is_namespace, flags);
14798       parser->object_scope = object_type;
14799       parser->qualifying_scope = NULL_TREE;
14800       if (object_decl)
14801         decl = object_decl;
14802     }
14803   else
14804     {
14805       decl = lookup_name_real (name, tag_type != none_type,
14806                                /*nonclass=*/0,
14807                                /*block_p=*/true, is_namespace, flags);
14808       parser->qualifying_scope = NULL_TREE;
14809       parser->object_scope = NULL_TREE;
14810     }
14811
14812   /* If the lookup failed, let our caller know.  */
14813   if (!decl || decl == error_mark_node)
14814     return error_mark_node;
14815
14816   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14817   if (TREE_CODE (decl) == TREE_LIST)
14818     {
14819       if (ambiguous_decls)
14820         *ambiguous_decls = decl;
14821       /* The error message we have to print is too complicated for
14822          cp_parser_error, so we incorporate its actions directly.  */
14823       if (!cp_parser_simulate_error (parser))
14824         {
14825           error ("reference to %qD is ambiguous", name);
14826           print_candidates (decl);
14827         }
14828       return error_mark_node;
14829     }
14830
14831   gcc_assert (DECL_P (decl)
14832               || TREE_CODE (decl) == OVERLOAD
14833               || TREE_CODE (decl) == SCOPE_REF
14834               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14835               || BASELINK_P (decl));
14836
14837   /* If we have resolved the name of a member declaration, check to
14838      see if the declaration is accessible.  When the name resolves to
14839      set of overloaded functions, accessibility is checked when
14840      overload resolution is done.
14841
14842      During an explicit instantiation, access is not checked at all,
14843      as per [temp.explicit].  */
14844   if (DECL_P (decl))
14845     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14846
14847   return decl;
14848 }
14849
14850 /* Like cp_parser_lookup_name, but for use in the typical case where
14851    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14852    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14853
14854 static tree
14855 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14856 {
14857   return cp_parser_lookup_name (parser, name,
14858                                 none_type,
14859                                 /*is_template=*/false,
14860                                 /*is_namespace=*/false,
14861                                 /*check_dependency=*/true,
14862                                 /*ambiguous_decls=*/NULL);
14863 }
14864
14865 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14866    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14867    true, the DECL indicates the class being defined in a class-head,
14868    or declared in an elaborated-type-specifier.
14869
14870    Otherwise, return DECL.  */
14871
14872 static tree
14873 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14874 {
14875   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14876      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14877
14878        struct A {
14879          template <typename T> struct B;
14880        };
14881
14882        template <typename T> struct A::B {};
14883
14884      Similarly, in an elaborated-type-specifier:
14885
14886        namespace N { struct X{}; }
14887
14888        struct A {
14889          template <typename T> friend struct N::X;
14890        };
14891
14892      However, if the DECL refers to a class type, and we are in
14893      the scope of the class, then the name lookup automatically
14894      finds the TYPE_DECL created by build_self_reference rather
14895      than a TEMPLATE_DECL.  For example, in:
14896
14897        template <class T> struct S {
14898          S s;
14899        };
14900
14901      there is no need to handle such case.  */
14902
14903   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14904     return DECL_TEMPLATE_RESULT (decl);
14905
14906   return decl;
14907 }
14908
14909 /* If too many, or too few, template-parameter lists apply to the
14910    declarator, issue an error message.  Returns TRUE if all went well,
14911    and FALSE otherwise.  */
14912
14913 static bool
14914 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14915                                                 cp_declarator *declarator)
14916 {
14917   unsigned num_templates;
14918
14919   /* We haven't seen any classes that involve template parameters yet.  */
14920   num_templates = 0;
14921
14922   switch (declarator->kind)
14923     {
14924     case cdk_id:
14925       if (declarator->u.id.qualifying_scope)
14926         {
14927           tree scope;
14928           tree member;
14929
14930           scope = declarator->u.id.qualifying_scope;
14931           member = declarator->u.id.unqualified_name;
14932
14933           while (scope && CLASS_TYPE_P (scope))
14934             {
14935               /* You're supposed to have one `template <...>'
14936                  for every template class, but you don't need one
14937                  for a full specialization.  For example:
14938
14939                  template <class T> struct S{};
14940                  template <> struct S<int> { void f(); };
14941                  void S<int>::f () {}
14942
14943                  is correct; there shouldn't be a `template <>' for
14944                  the definition of `S<int>::f'.  */
14945               if (CLASSTYPE_TEMPLATE_INFO (scope)
14946                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14947                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14948                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14949                 ++num_templates;
14950
14951               scope = TYPE_CONTEXT (scope);
14952             }
14953         }
14954       else if (TREE_CODE (declarator->u.id.unqualified_name)
14955                == TEMPLATE_ID_EXPR)
14956         /* If the DECLARATOR has the form `X<y>' then it uses one
14957            additional level of template parameters.  */
14958         ++num_templates;
14959
14960       return cp_parser_check_template_parameters (parser,
14961                                                   num_templates);
14962
14963     case cdk_function:
14964     case cdk_array:
14965     case cdk_pointer:
14966     case cdk_reference:
14967     case cdk_ptrmem:
14968       return (cp_parser_check_declarator_template_parameters
14969               (parser, declarator->declarator));
14970
14971     case cdk_error:
14972       return true;
14973
14974     default:
14975       gcc_unreachable ();
14976     }
14977   return false;
14978 }
14979
14980 /* NUM_TEMPLATES were used in the current declaration.  If that is
14981    invalid, return FALSE and issue an error messages.  Otherwise,
14982    return TRUE.  */
14983
14984 static bool
14985 cp_parser_check_template_parameters (cp_parser* parser,
14986                                      unsigned num_templates)
14987 {
14988   /* If there are more template classes than parameter lists, we have
14989      something like:
14990
14991        template <class T> void S<T>::R<T>::f ();  */
14992   if (parser->num_template_parameter_lists < num_templates)
14993     {
14994       error ("too few template-parameter-lists");
14995       return false;
14996     }
14997   /* If there are the same number of template classes and parameter
14998      lists, that's OK.  */
14999   if (parser->num_template_parameter_lists == num_templates)
15000     return true;
15001   /* If there are more, but only one more, then we are referring to a
15002      member template.  That's OK too.  */
15003   if (parser->num_template_parameter_lists == num_templates + 1)
15004       return true;
15005   /* Otherwise, there are too many template parameter lists.  We have
15006      something like:
15007
15008      template <class T> template <class U> void S::f();  */
15009   error ("too many template-parameter-lists");
15010   return false;
15011 }
15012
15013 /* Parse an optional `::' token indicating that the following name is
15014    from the global namespace.  If so, PARSER->SCOPE is set to the
15015    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15016    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15017    Returns the new value of PARSER->SCOPE, if the `::' token is
15018    present, and NULL_TREE otherwise.  */
15019
15020 static tree
15021 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15022 {
15023   cp_token *token;
15024
15025   /* Peek at the next token.  */
15026   token = cp_lexer_peek_token (parser->lexer);
15027   /* If we're looking at a `::' token then we're starting from the
15028      global namespace, not our current location.  */
15029   if (token->type == CPP_SCOPE)
15030     {
15031       /* Consume the `::' token.  */
15032       cp_lexer_consume_token (parser->lexer);
15033       /* Set the SCOPE so that we know where to start the lookup.  */
15034       parser->scope = global_namespace;
15035       parser->qualifying_scope = global_namespace;
15036       parser->object_scope = NULL_TREE;
15037
15038       return parser->scope;
15039     }
15040   else if (!current_scope_valid_p)
15041     {
15042       parser->scope = NULL_TREE;
15043       parser->qualifying_scope = NULL_TREE;
15044       parser->object_scope = NULL_TREE;
15045     }
15046
15047   return NULL_TREE;
15048 }
15049
15050 /* Returns TRUE if the upcoming token sequence is the start of a
15051    constructor declarator.  If FRIEND_P is true, the declarator is
15052    preceded by the `friend' specifier.  */
15053
15054 static bool
15055 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15056 {
15057   bool constructor_p;
15058   tree type_decl = NULL_TREE;
15059   bool nested_name_p;
15060   cp_token *next_token;
15061
15062   /* The common case is that this is not a constructor declarator, so
15063      try to avoid doing lots of work if at all possible.  It's not
15064      valid declare a constructor at function scope.  */
15065   if (at_function_scope_p ())
15066     return false;
15067   /* And only certain tokens can begin a constructor declarator.  */
15068   next_token = cp_lexer_peek_token (parser->lexer);
15069   if (next_token->type != CPP_NAME
15070       && next_token->type != CPP_SCOPE
15071       && next_token->type != CPP_NESTED_NAME_SPECIFIER
15072       && next_token->type != CPP_TEMPLATE_ID)
15073     return false;
15074
15075   /* Parse tentatively; we are going to roll back all of the tokens
15076      consumed here.  */
15077   cp_parser_parse_tentatively (parser);
15078   /* Assume that we are looking at a constructor declarator.  */
15079   constructor_p = true;
15080
15081   /* Look for the optional `::' operator.  */
15082   cp_parser_global_scope_opt (parser,
15083                               /*current_scope_valid_p=*/false);
15084   /* Look for the nested-name-specifier.  */
15085   nested_name_p
15086     = (cp_parser_nested_name_specifier_opt (parser,
15087                                             /*typename_keyword_p=*/false,
15088                                             /*check_dependency_p=*/false,
15089                                             /*type_p=*/false,
15090                                             /*is_declaration=*/false)
15091        != NULL_TREE);
15092   /* Outside of a class-specifier, there must be a
15093      nested-name-specifier.  */
15094   if (!nested_name_p &&
15095       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15096        || friend_p))
15097     constructor_p = false;
15098   /* If we still think that this might be a constructor-declarator,
15099      look for a class-name.  */
15100   if (constructor_p)
15101     {
15102       /* If we have:
15103
15104            template <typename T> struct S { S(); };
15105            template <typename T> S<T>::S ();
15106
15107          we must recognize that the nested `S' names a class.
15108          Similarly, for:
15109
15110            template <typename T> S<T>::S<T> ();
15111
15112          we must recognize that the nested `S' names a template.  */
15113       type_decl = cp_parser_class_name (parser,
15114                                         /*typename_keyword_p=*/false,
15115                                         /*template_keyword_p=*/false,
15116                                         none_type,
15117                                         /*check_dependency_p=*/false,
15118                                         /*class_head_p=*/false,
15119                                         /*is_declaration=*/false);
15120       /* If there was no class-name, then this is not a constructor.  */
15121       constructor_p = !cp_parser_error_occurred (parser);
15122     }
15123
15124   /* If we're still considering a constructor, we have to see a `(',
15125      to begin the parameter-declaration-clause, followed by either a
15126      `)', an `...', or a decl-specifier.  We need to check for a
15127      type-specifier to avoid being fooled into thinking that:
15128
15129        S::S (f) (int);
15130
15131      is a constructor.  (It is actually a function named `f' that
15132      takes one parameter (of type `int') and returns a value of type
15133      `S::S'.  */
15134   if (constructor_p
15135       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15136     {
15137       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15138           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15139           /* A parameter declaration begins with a decl-specifier,
15140              which is either the "attribute" keyword, a storage class
15141              specifier, or (usually) a type-specifier.  */
15142           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15143           && !cp_parser_storage_class_specifier_opt (parser))
15144         {
15145           tree type;
15146           tree pushed_scope = NULL_TREE;
15147           unsigned saved_num_template_parameter_lists;
15148
15149           /* Names appearing in the type-specifier should be looked up
15150              in the scope of the class.  */
15151           if (current_class_type)
15152             type = NULL_TREE;
15153           else
15154             {
15155               type = TREE_TYPE (type_decl);
15156               if (TREE_CODE (type) == TYPENAME_TYPE)
15157                 {
15158                   type = resolve_typename_type (type,
15159                                                 /*only_current_p=*/false);
15160                   if (type == error_mark_node)
15161                     {
15162                       cp_parser_abort_tentative_parse (parser);
15163                       return false;
15164                     }
15165                 }
15166               pushed_scope = push_scope (type);
15167             }
15168
15169           /* Inside the constructor parameter list, surrounding
15170              template-parameter-lists do not apply.  */
15171           saved_num_template_parameter_lists
15172             = parser->num_template_parameter_lists;
15173           parser->num_template_parameter_lists = 0;
15174
15175           /* Look for the type-specifier.  */
15176           cp_parser_type_specifier (parser,
15177                                     CP_PARSER_FLAGS_NONE,
15178                                     /*decl_specs=*/NULL,
15179                                     /*is_declarator=*/true,
15180                                     /*declares_class_or_enum=*/NULL,
15181                                     /*is_cv_qualifier=*/NULL);
15182
15183           parser->num_template_parameter_lists
15184             = saved_num_template_parameter_lists;
15185
15186           /* Leave the scope of the class.  */
15187           if (pushed_scope)
15188             pop_scope (pushed_scope);
15189
15190           constructor_p = !cp_parser_error_occurred (parser);
15191         }
15192     }
15193   else
15194     constructor_p = false;
15195   /* We did not really want to consume any tokens.  */
15196   cp_parser_abort_tentative_parse (parser);
15197
15198   return constructor_p;
15199 }
15200
15201 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15202    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15203    they must be performed once we are in the scope of the function.
15204
15205    Returns the function defined.  */
15206
15207 static tree
15208 cp_parser_function_definition_from_specifiers_and_declarator
15209   (cp_parser* parser,
15210    cp_decl_specifier_seq *decl_specifiers,
15211    tree attributes,
15212    const cp_declarator *declarator)
15213 {
15214   tree fn;
15215   bool success_p;
15216
15217   /* Begin the function-definition.  */
15218   success_p = start_function (decl_specifiers, declarator, attributes);
15219
15220   /* The things we're about to see are not directly qualified by any
15221      template headers we've seen thus far.  */
15222   reset_specialization ();
15223
15224   /* If there were names looked up in the decl-specifier-seq that we
15225      did not check, check them now.  We must wait until we are in the
15226      scope of the function to perform the checks, since the function
15227      might be a friend.  */
15228   perform_deferred_access_checks ();
15229
15230   if (!success_p)
15231     {
15232       /* Skip the entire function.  */
15233       error ("invalid function declaration");
15234       cp_parser_skip_to_end_of_block_or_statement (parser);
15235       fn = error_mark_node;
15236     }
15237   else
15238     fn = cp_parser_function_definition_after_declarator (parser,
15239                                                          /*inline_p=*/false);
15240
15241   return fn;
15242 }
15243
15244 /* Parse the part of a function-definition that follows the
15245    declarator.  INLINE_P is TRUE iff this function is an inline
15246    function defined with a class-specifier.
15247
15248    Returns the function defined.  */
15249
15250 static tree
15251 cp_parser_function_definition_after_declarator (cp_parser* parser,
15252                                                 bool inline_p)
15253 {
15254   tree fn;
15255   bool ctor_initializer_p = false;
15256   bool saved_in_unbraced_linkage_specification_p;
15257   unsigned saved_num_template_parameter_lists;
15258
15259   /* If the next token is `return', then the code may be trying to
15260      make use of the "named return value" extension that G++ used to
15261      support.  */
15262   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15263     {
15264       /* Consume the `return' keyword.  */
15265       cp_lexer_consume_token (parser->lexer);
15266       /* Look for the identifier that indicates what value is to be
15267          returned.  */
15268       cp_parser_identifier (parser);
15269       /* Issue an error message.  */
15270       error ("named return values are no longer supported");
15271       /* Skip tokens until we reach the start of the function body.  */
15272       while (true)
15273         {
15274           cp_token *token = cp_lexer_peek_token (parser->lexer);
15275           if (token->type == CPP_OPEN_BRACE
15276               || token->type == CPP_EOF
15277               || token->type == CPP_PRAGMA_EOL)
15278             break;
15279           cp_lexer_consume_token (parser->lexer);
15280         }
15281     }
15282   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15283      anything declared inside `f'.  */
15284   saved_in_unbraced_linkage_specification_p
15285     = parser->in_unbraced_linkage_specification_p;
15286   parser->in_unbraced_linkage_specification_p = false;
15287   /* Inside the function, surrounding template-parameter-lists do not
15288      apply.  */
15289   saved_num_template_parameter_lists
15290     = parser->num_template_parameter_lists;
15291   parser->num_template_parameter_lists = 0;
15292   /* If the next token is `try', then we are looking at a
15293      function-try-block.  */
15294   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15295     ctor_initializer_p = cp_parser_function_try_block (parser);
15296   /* A function-try-block includes the function-body, so we only do
15297      this next part if we're not processing a function-try-block.  */
15298   else
15299     ctor_initializer_p
15300       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15301
15302   /* Finish the function.  */
15303   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15304                         (inline_p ? 2 : 0));
15305   /* Generate code for it, if necessary.  */
15306   expand_or_defer_fn (fn);
15307   /* Restore the saved values.  */
15308   parser->in_unbraced_linkage_specification_p
15309     = saved_in_unbraced_linkage_specification_p;
15310   parser->num_template_parameter_lists
15311     = saved_num_template_parameter_lists;
15312
15313   return fn;
15314 }
15315
15316 /* Parse a template-declaration, assuming that the `export' (and
15317    `extern') keywords, if present, has already been scanned.  MEMBER_P
15318    is as for cp_parser_template_declaration.  */
15319
15320 static void
15321 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15322 {
15323   tree decl = NULL_TREE;
15324   tree parameter_list;
15325   bool friend_p = false;
15326   bool need_lang_pop;
15327
15328   /* Look for the `template' keyword.  */
15329   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15330     return;
15331
15332   /* And the `<'.  */
15333   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15334     return;
15335   /* [temp]
15336    
15337      A template ... shall not have C linkage.  */
15338   if (current_lang_name == lang_name_c)
15339     {
15340       error ("template with C linkage");
15341       /* Give it C++ linkage to avoid confusing other parts of the
15342          front end.  */
15343       push_lang_context (lang_name_cplusplus);
15344       need_lang_pop = true;
15345     }
15346   else
15347     need_lang_pop = false;
15348   /* If the next token is `>', then we have an invalid
15349      specialization.  Rather than complain about an invalid template
15350      parameter, issue an error message here.  */
15351   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15352     {
15353       cp_parser_error (parser, "invalid explicit specialization");
15354       begin_specialization ();
15355       parameter_list = NULL_TREE;
15356     }
15357   else
15358     /* Parse the template parameters.  */
15359     parameter_list = cp_parser_template_parameter_list (parser);
15360
15361   /* Look for the `>'.  */
15362   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15363   /* We just processed one more parameter list.  */
15364   ++parser->num_template_parameter_lists;
15365   /* If the next token is `template', there are more template
15366      parameters.  */
15367   if (cp_lexer_next_token_is_keyword (parser->lexer,
15368                                       RID_TEMPLATE))
15369     cp_parser_template_declaration_after_export (parser, member_p);
15370   else
15371     {
15372       /* There are no access checks when parsing a template, as we do not
15373          know if a specialization will be a friend.  */
15374       push_deferring_access_checks (dk_no_check);
15375
15376       decl = cp_parser_single_declaration (parser,
15377                                            member_p,
15378                                            &friend_p);
15379
15380       pop_deferring_access_checks ();
15381
15382       /* If this is a member template declaration, let the front
15383          end know.  */
15384       if (member_p && !friend_p && decl)
15385         {
15386           if (TREE_CODE (decl) == TYPE_DECL)
15387             cp_parser_check_access_in_redeclaration (decl);
15388
15389           decl = finish_member_template_decl (decl);
15390         }
15391       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15392         make_friend_class (current_class_type, TREE_TYPE (decl),
15393                            /*complain=*/true);
15394     }
15395   /* We are done with the current parameter list.  */
15396   --parser->num_template_parameter_lists;
15397
15398   /* Finish up.  */
15399   finish_template_decl (parameter_list);
15400
15401   /* Register member declarations.  */
15402   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15403     finish_member_declaration (decl);
15404   /* For the erroneous case of a template with C linkage, we pushed an
15405      implicit C++ linkage scope; exit that scope now.  */
15406   if (need_lang_pop)
15407     pop_lang_context ();
15408   /* If DECL is a function template, we must return to parse it later.
15409      (Even though there is no definition, there might be default
15410      arguments that need handling.)  */
15411   if (member_p && decl
15412       && (TREE_CODE (decl) == FUNCTION_DECL
15413           || DECL_FUNCTION_TEMPLATE_P (decl)))
15414     TREE_VALUE (parser->unparsed_functions_queues)
15415       = tree_cons (NULL_TREE, decl,
15416                    TREE_VALUE (parser->unparsed_functions_queues));
15417 }
15418
15419 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15420    `function-definition' sequence.  MEMBER_P is true, this declaration
15421    appears in a class scope.
15422
15423    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15424    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15425
15426 static tree
15427 cp_parser_single_declaration (cp_parser* parser,
15428                               bool member_p,
15429                               bool* friend_p)
15430 {
15431   int declares_class_or_enum;
15432   tree decl = NULL_TREE;
15433   cp_decl_specifier_seq decl_specifiers;
15434   bool function_definition_p = false;
15435
15436   /* This function is only used when processing a template
15437      declaration.  */
15438   gcc_assert (innermost_scope_kind () == sk_template_parms
15439               || innermost_scope_kind () == sk_template_spec);
15440
15441   /* Defer access checks until we know what is being declared.  */
15442   push_deferring_access_checks (dk_deferred);
15443
15444   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15445      alternative.  */
15446   cp_parser_decl_specifier_seq (parser,
15447                                 CP_PARSER_FLAGS_OPTIONAL,
15448                                 &decl_specifiers,
15449                                 &declares_class_or_enum);
15450   if (friend_p)
15451     *friend_p = cp_parser_friend_p (&decl_specifiers);
15452
15453   /* There are no template typedefs.  */
15454   if (decl_specifiers.specs[(int) ds_typedef])
15455     {
15456       error ("template declaration of %qs", "typedef");
15457       decl = error_mark_node;
15458     }
15459
15460   /* Gather up the access checks that occurred the
15461      decl-specifier-seq.  */
15462   stop_deferring_access_checks ();
15463
15464   /* Check for the declaration of a template class.  */
15465   if (declares_class_or_enum)
15466     {
15467       if (cp_parser_declares_only_class_p (parser))
15468         {
15469           decl = shadow_tag (&decl_specifiers);
15470
15471           /* In this case:
15472
15473                struct C {
15474                  friend template <typename T> struct A<T>::B;
15475                };
15476
15477              A<T>::B will be represented by a TYPENAME_TYPE, and
15478              therefore not recognized by shadow_tag.  */
15479           if (friend_p && *friend_p
15480               && !decl
15481               && decl_specifiers.type
15482               && TYPE_P (decl_specifiers.type))
15483             decl = decl_specifiers.type;
15484
15485           if (decl && decl != error_mark_node)
15486             decl = TYPE_NAME (decl);
15487           else
15488             decl = error_mark_node;
15489         }
15490     }
15491   /* If it's not a template class, try for a template function.  If
15492      the next token is a `;', then this declaration does not declare
15493      anything.  But, if there were errors in the decl-specifiers, then
15494      the error might well have come from an attempted class-specifier.
15495      In that case, there's no need to warn about a missing declarator.  */
15496   if (!decl
15497       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15498           || decl_specifiers.type != error_mark_node))
15499     decl = cp_parser_init_declarator (parser,
15500                                       &decl_specifiers,
15501                                       /*function_definition_allowed_p=*/true,
15502                                       member_p,
15503                                       declares_class_or_enum,
15504                                       &function_definition_p);
15505
15506   pop_deferring_access_checks ();
15507
15508   /* Clear any current qualification; whatever comes next is the start
15509      of something new.  */
15510   parser->scope = NULL_TREE;
15511   parser->qualifying_scope = NULL_TREE;
15512   parser->object_scope = NULL_TREE;
15513   /* Look for a trailing `;' after the declaration.  */
15514   if (!function_definition_p
15515       && (decl == error_mark_node
15516           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15517     cp_parser_skip_to_end_of_block_or_statement (parser);
15518
15519   return decl;
15520 }
15521
15522 /* Parse a cast-expression that is not the operand of a unary "&".  */
15523
15524 static tree
15525 cp_parser_simple_cast_expression (cp_parser *parser)
15526 {
15527   return cp_parser_cast_expression (parser, /*address_p=*/false,
15528                                     /*cast_p=*/false);
15529 }
15530
15531 /* Parse a functional cast to TYPE.  Returns an expression
15532    representing the cast.  */
15533
15534 static tree
15535 cp_parser_functional_cast (cp_parser* parser, tree type)
15536 {
15537   tree expression_list;
15538   tree cast;
15539
15540   expression_list
15541     = cp_parser_parenthesized_expression_list (parser, false,
15542                                                /*cast_p=*/true,
15543                                                /*non_constant_p=*/NULL);
15544
15545   cast = build_functional_cast (type, expression_list);
15546   /* [expr.const]/1: In an integral constant expression "only type
15547      conversions to integral or enumeration type can be used".  */
15548   if (TREE_CODE (type) == TYPE_DECL)
15549     type = TREE_TYPE (type);
15550   if (cast != error_mark_node && !dependent_type_p (type)
15551       && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15552     {
15553       if (cp_parser_non_integral_constant_expression
15554           (parser, "a call to a constructor"))
15555         return error_mark_node;
15556     }
15557   return cast;
15558 }
15559
15560 /* Save the tokens that make up the body of a member function defined
15561    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15562    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15563    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15564    for the member function.  */
15565
15566 static tree
15567 cp_parser_save_member_function_body (cp_parser* parser,
15568                                      cp_decl_specifier_seq *decl_specifiers,
15569                                      cp_declarator *declarator,
15570                                      tree attributes)
15571 {
15572   cp_token *first;
15573   cp_token *last;
15574   tree fn;
15575
15576   /* Create the function-declaration.  */
15577   fn = start_method (decl_specifiers, declarator, attributes);
15578   /* If something went badly wrong, bail out now.  */
15579   if (fn == error_mark_node)
15580     {
15581       /* If there's a function-body, skip it.  */
15582       if (cp_parser_token_starts_function_definition_p
15583           (cp_lexer_peek_token (parser->lexer)))
15584         cp_parser_skip_to_end_of_block_or_statement (parser);
15585       return error_mark_node;
15586     }
15587
15588   /* Remember it, if there default args to post process.  */
15589   cp_parser_save_default_args (parser, fn);
15590
15591   /* Save away the tokens that make up the body of the
15592      function.  */
15593   first = parser->lexer->next_token;
15594   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15595   /* Handle function try blocks.  */
15596   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15597     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15598   last = parser->lexer->next_token;
15599
15600   /* Save away the inline definition; we will process it when the
15601      class is complete.  */
15602   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15603   DECL_PENDING_INLINE_P (fn) = 1;
15604
15605   /* We need to know that this was defined in the class, so that
15606      friend templates are handled correctly.  */
15607   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15608
15609   /* We're done with the inline definition.  */
15610   finish_method (fn);
15611
15612   /* Add FN to the queue of functions to be parsed later.  */
15613   TREE_VALUE (parser->unparsed_functions_queues)
15614     = tree_cons (NULL_TREE, fn,
15615                  TREE_VALUE (parser->unparsed_functions_queues));
15616
15617   return fn;
15618 }
15619
15620 /* Parse a template-argument-list, as well as the trailing ">" (but
15621    not the opening ">").  See cp_parser_template_argument_list for the
15622    return value.  */
15623
15624 static tree
15625 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15626 {
15627   tree arguments;
15628   tree saved_scope;
15629   tree saved_qualifying_scope;
15630   tree saved_object_scope;
15631   bool saved_greater_than_is_operator_p;
15632   bool saved_skip_evaluation;
15633
15634   /* [temp.names]
15635
15636      When parsing a template-id, the first non-nested `>' is taken as
15637      the end of the template-argument-list rather than a greater-than
15638      operator.  */
15639   saved_greater_than_is_operator_p
15640     = parser->greater_than_is_operator_p;
15641   parser->greater_than_is_operator_p = false;
15642   /* Parsing the argument list may modify SCOPE, so we save it
15643      here.  */
15644   saved_scope = parser->scope;
15645   saved_qualifying_scope = parser->qualifying_scope;
15646   saved_object_scope = parser->object_scope;
15647   /* We need to evaluate the template arguments, even though this
15648      template-id may be nested within a "sizeof".  */
15649   saved_skip_evaluation = skip_evaluation;
15650   skip_evaluation = false;
15651   /* Parse the template-argument-list itself.  */
15652   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15653     arguments = NULL_TREE;
15654   else
15655     arguments = cp_parser_template_argument_list (parser);
15656   /* Look for the `>' that ends the template-argument-list. If we find
15657      a '>>' instead, it's probably just a typo.  */
15658   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15659     {
15660       if (!saved_greater_than_is_operator_p)
15661         {
15662           /* If we're in a nested template argument list, the '>>' has
15663             to be a typo for '> >'. We emit the error message, but we
15664             continue parsing and we push a '>' as next token, so that
15665             the argument list will be parsed correctly.  Note that the
15666             global source location is still on the token before the
15667             '>>', so we need to say explicitly where we want it.  */
15668           cp_token *token = cp_lexer_peek_token (parser->lexer);
15669           error ("%H%<>>%> should be %<> >%> "
15670                  "within a nested template argument list",
15671                  &token->location);
15672
15673           /* ??? Proper recovery should terminate two levels of
15674              template argument list here.  */
15675           token->type = CPP_GREATER;
15676         }
15677       else
15678         {
15679           /* If this is not a nested template argument list, the '>>'
15680             is a typo for '>'. Emit an error message and continue.
15681             Same deal about the token location, but here we can get it
15682             right by consuming the '>>' before issuing the diagnostic.  */
15683           cp_lexer_consume_token (parser->lexer);
15684           error ("spurious %<>>%>, use %<>%> to terminate "
15685                  "a template argument list");
15686         }
15687     }
15688   else
15689     cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15690   /* The `>' token might be a greater-than operator again now.  */
15691   parser->greater_than_is_operator_p
15692     = saved_greater_than_is_operator_p;
15693   /* Restore the SAVED_SCOPE.  */
15694   parser->scope = saved_scope;
15695   parser->qualifying_scope = saved_qualifying_scope;
15696   parser->object_scope = saved_object_scope;
15697   skip_evaluation = saved_skip_evaluation;
15698
15699   return arguments;
15700 }
15701
15702 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15703    arguments, or the body of the function have not yet been parsed,
15704    parse them now.  */
15705
15706 static void
15707 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15708 {
15709   /* If this member is a template, get the underlying
15710      FUNCTION_DECL.  */
15711   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15712     member_function = DECL_TEMPLATE_RESULT (member_function);
15713
15714   /* There should not be any class definitions in progress at this
15715      point; the bodies of members are only parsed outside of all class
15716      definitions.  */
15717   gcc_assert (parser->num_classes_being_defined == 0);
15718   /* While we're parsing the member functions we might encounter more
15719      classes.  We want to handle them right away, but we don't want
15720      them getting mixed up with functions that are currently in the
15721      queue.  */
15722   parser->unparsed_functions_queues
15723     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15724
15725   /* Make sure that any template parameters are in scope.  */
15726   maybe_begin_member_template_processing (member_function);
15727
15728   /* If the body of the function has not yet been parsed, parse it
15729      now.  */
15730   if (DECL_PENDING_INLINE_P (member_function))
15731     {
15732       tree function_scope;
15733       cp_token_cache *tokens;
15734
15735       /* The function is no longer pending; we are processing it.  */
15736       tokens = DECL_PENDING_INLINE_INFO (member_function);
15737       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15738       DECL_PENDING_INLINE_P (member_function) = 0;
15739
15740       /* If this is a local class, enter the scope of the containing
15741          function.  */
15742       function_scope = current_function_decl;
15743       if (function_scope)
15744         push_function_context_to (function_scope);
15745
15746
15747       /* Push the body of the function onto the lexer stack.  */
15748       cp_parser_push_lexer_for_tokens (parser, tokens);
15749
15750       /* Let the front end know that we going to be defining this
15751          function.  */
15752       start_preparsed_function (member_function, NULL_TREE,
15753                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15754
15755       /* Don't do access checking if it is a templated function.  */
15756       if (processing_template_decl)
15757         push_deferring_access_checks (dk_no_check);
15758
15759       /* Now, parse the body of the function.  */
15760       cp_parser_function_definition_after_declarator (parser,
15761                                                       /*inline_p=*/true);
15762
15763       if (processing_template_decl)
15764         pop_deferring_access_checks ();
15765
15766       /* Leave the scope of the containing function.  */
15767       if (function_scope)
15768         pop_function_context_from (function_scope);
15769       cp_parser_pop_lexer (parser);
15770     }
15771
15772   /* Remove any template parameters from the symbol table.  */
15773   maybe_end_member_template_processing ();
15774
15775   /* Restore the queue.  */
15776   parser->unparsed_functions_queues
15777     = TREE_CHAIN (parser->unparsed_functions_queues);
15778 }
15779
15780 /* If DECL contains any default args, remember it on the unparsed
15781    functions queue.  */
15782
15783 static void
15784 cp_parser_save_default_args (cp_parser* parser, tree decl)
15785 {
15786   tree probe;
15787
15788   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15789        probe;
15790        probe = TREE_CHAIN (probe))
15791     if (TREE_PURPOSE (probe))
15792       {
15793         TREE_PURPOSE (parser->unparsed_functions_queues)
15794           = tree_cons (current_class_type, decl,
15795                        TREE_PURPOSE (parser->unparsed_functions_queues));
15796         break;
15797       }
15798 }
15799
15800 /* FN is a FUNCTION_DECL which may contains a parameter with an
15801    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15802    assumes that the current scope is the scope in which the default
15803    argument should be processed.  */
15804
15805 static void
15806 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15807 {
15808   bool saved_local_variables_forbidden_p;
15809   tree parm;
15810
15811   /* While we're parsing the default args, we might (due to the
15812      statement expression extension) encounter more classes.  We want
15813      to handle them right away, but we don't want them getting mixed
15814      up with default args that are currently in the queue.  */
15815   parser->unparsed_functions_queues
15816     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15817
15818   /* Local variable names (and the `this' keyword) may not appear
15819      in a default argument.  */
15820   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15821   parser->local_variables_forbidden_p = true;
15822
15823   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15824        parm;
15825        parm = TREE_CHAIN (parm))
15826     {
15827       cp_token_cache *tokens;
15828       tree default_arg = TREE_PURPOSE (parm);
15829       tree parsed_arg;
15830       VEC(tree,gc) *insts;
15831       tree copy;
15832       unsigned ix;
15833
15834       if (!default_arg)
15835         continue;
15836
15837       if (TREE_CODE (default_arg) != DEFAULT_ARG)
15838         /* This can happen for a friend declaration for a function
15839            already declared with default arguments.  */
15840         continue;
15841
15842        /* Push the saved tokens for the default argument onto the parser's
15843           lexer stack.  */
15844       tokens = DEFARG_TOKENS (default_arg);
15845       cp_parser_push_lexer_for_tokens (parser, tokens);
15846
15847       /* Parse the assignment-expression.  */
15848       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15849
15850       if (!processing_template_decl)
15851         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
15852       
15853       TREE_PURPOSE (parm) = parsed_arg;
15854
15855       /* Update any instantiations we've already created.  */
15856       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15857            VEC_iterate (tree, insts, ix, copy); ix++)
15858         TREE_PURPOSE (copy) = parsed_arg;
15859
15860       /* If the token stream has not been completely used up, then
15861          there was extra junk after the end of the default
15862          argument.  */
15863       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15864         cp_parser_error (parser, "expected %<,%>");
15865
15866       /* Revert to the main lexer.  */
15867       cp_parser_pop_lexer (parser);
15868     }
15869
15870   /* Make sure no default arg is missing.  */
15871   check_default_args (fn);
15872
15873   /* Restore the state of local_variables_forbidden_p.  */
15874   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15875
15876   /* Restore the queue.  */
15877   parser->unparsed_functions_queues
15878     = TREE_CHAIN (parser->unparsed_functions_queues);
15879 }
15880
15881 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15882    either a TYPE or an expression, depending on the form of the
15883    input.  The KEYWORD indicates which kind of expression we have
15884    encountered.  */
15885
15886 static tree
15887 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15888 {
15889   static const char *format;
15890   tree expr = NULL_TREE;
15891   const char *saved_message;
15892   bool saved_integral_constant_expression_p;
15893   bool saved_non_integral_constant_expression_p;
15894
15895   /* Initialize FORMAT the first time we get here.  */
15896   if (!format)
15897     format = "types may not be defined in '%s' expressions";
15898
15899   /* Types cannot be defined in a `sizeof' expression.  Save away the
15900      old message.  */
15901   saved_message = parser->type_definition_forbidden_message;
15902   /* And create the new one.  */
15903   parser->type_definition_forbidden_message
15904     = XNEWVEC (const char, strlen (format)
15905                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15906                + 1 /* `\0' */);
15907   sprintf ((char *) parser->type_definition_forbidden_message,
15908            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15909
15910   /* The restrictions on constant-expressions do not apply inside
15911      sizeof expressions.  */
15912   saved_integral_constant_expression_p
15913     = parser->integral_constant_expression_p;
15914   saved_non_integral_constant_expression_p
15915     = parser->non_integral_constant_expression_p;
15916   parser->integral_constant_expression_p = false;
15917
15918   /* Do not actually evaluate the expression.  */
15919   ++skip_evaluation;
15920   /* If it's a `(', then we might be looking at the type-id
15921      construction.  */
15922   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15923     {
15924       tree type;
15925       bool saved_in_type_id_in_expr_p;
15926
15927       /* We can't be sure yet whether we're looking at a type-id or an
15928          expression.  */
15929       cp_parser_parse_tentatively (parser);
15930       /* Consume the `('.  */
15931       cp_lexer_consume_token (parser->lexer);
15932       /* Parse the type-id.  */
15933       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15934       parser->in_type_id_in_expr_p = true;
15935       type = cp_parser_type_id (parser);
15936       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15937       /* Now, look for the trailing `)'.  */
15938       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15939       /* If all went well, then we're done.  */
15940       if (cp_parser_parse_definitely (parser))
15941         {
15942           cp_decl_specifier_seq decl_specs;
15943
15944           /* Build a trivial decl-specifier-seq.  */
15945           clear_decl_specs (&decl_specs);
15946           decl_specs.type = type;
15947
15948           /* Call grokdeclarator to figure out what type this is.  */
15949           expr = grokdeclarator (NULL,
15950                                  &decl_specs,
15951                                  TYPENAME,
15952                                  /*initialized=*/0,
15953                                  /*attrlist=*/NULL);
15954         }
15955     }
15956
15957   /* If the type-id production did not work out, then we must be
15958      looking at the unary-expression production.  */
15959   if (!expr)
15960     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15961                                        /*cast_p=*/false);
15962   /* Go back to evaluating expressions.  */
15963   --skip_evaluation;
15964
15965   /* Free the message we created.  */
15966   free ((char *) parser->type_definition_forbidden_message);
15967   /* And restore the old one.  */
15968   parser->type_definition_forbidden_message = saved_message;
15969   parser->integral_constant_expression_p
15970     = saved_integral_constant_expression_p;
15971   parser->non_integral_constant_expression_p
15972     = saved_non_integral_constant_expression_p;
15973
15974   return expr;
15975 }
15976
15977 /* If the current declaration has no declarator, return true.  */
15978
15979 static bool
15980 cp_parser_declares_only_class_p (cp_parser *parser)
15981 {
15982   /* If the next token is a `;' or a `,' then there is no
15983      declarator.  */
15984   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15985           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15986 }
15987
15988 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15989
15990 static void
15991 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15992                              cp_storage_class storage_class)
15993 {
15994   if (decl_specs->storage_class != sc_none)
15995     decl_specs->multiple_storage_classes_p = true;
15996   else
15997     decl_specs->storage_class = storage_class;
15998 }
15999
16000 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
16001    is true, the type is a user-defined type; otherwise it is a
16002    built-in type specified by a keyword.  */
16003
16004 static void
16005 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16006                               tree type_spec,
16007                               bool user_defined_p)
16008 {
16009   decl_specs->any_specifiers_p = true;
16010
16011   /* If the user tries to redeclare bool or wchar_t (with, for
16012      example, in "typedef int wchar_t;") we remember that this is what
16013      happened.  In system headers, we ignore these declarations so
16014      that G++ can work with system headers that are not C++-safe.  */
16015   if (decl_specs->specs[(int) ds_typedef]
16016       && !user_defined_p
16017       && (type_spec == boolean_type_node
16018           || type_spec == wchar_type_node)
16019       && (decl_specs->type
16020           || decl_specs->specs[(int) ds_long]
16021           || decl_specs->specs[(int) ds_short]
16022           || decl_specs->specs[(int) ds_unsigned]
16023           || decl_specs->specs[(int) ds_signed]))
16024     {
16025       decl_specs->redefined_builtin_type = type_spec;
16026       if (!decl_specs->type)
16027         {
16028           decl_specs->type = type_spec;
16029           decl_specs->user_defined_type_p = false;
16030         }
16031     }
16032   else if (decl_specs->type)
16033     decl_specs->multiple_types_p = true;
16034   else
16035     {
16036       decl_specs->type = type_spec;
16037       decl_specs->user_defined_type_p = user_defined_p;
16038       decl_specs->redefined_builtin_type = NULL_TREE;
16039     }
16040 }
16041
16042 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16043    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
16044
16045 static bool
16046 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16047 {
16048   return decl_specifiers->specs[(int) ds_friend] != 0;
16049 }
16050
16051 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
16052    issue an error message indicating that TOKEN_DESC was expected.
16053
16054    Returns the token consumed, if the token had the appropriate type.
16055    Otherwise, returns NULL.  */
16056
16057 static cp_token *
16058 cp_parser_require (cp_parser* parser,
16059                    enum cpp_ttype type,
16060                    const char* token_desc)
16061 {
16062   if (cp_lexer_next_token_is (parser->lexer, type))
16063     return cp_lexer_consume_token (parser->lexer);
16064   else
16065     {
16066       /* Output the MESSAGE -- unless we're parsing tentatively.  */
16067       if (!cp_parser_simulate_error (parser))
16068         {
16069           char *message = concat ("expected ", token_desc, NULL);
16070           cp_parser_error (parser, message);
16071           free (message);
16072         }
16073       return NULL;
16074     }
16075 }
16076
16077 /* Like cp_parser_require, except that tokens will be skipped until
16078    the desired token is found.  An error message is still produced if
16079    the next token is not as expected.  */
16080
16081 static void
16082 cp_parser_skip_until_found (cp_parser* parser,
16083                             enum cpp_ttype type,
16084                             const char* token_desc)
16085 {
16086   cp_token *token;
16087   unsigned nesting_depth = 0;
16088
16089   if (cp_parser_require (parser, type, token_desc))
16090     return;
16091
16092   /* Skip tokens until the desired token is found.  */
16093   while (true)
16094     {
16095       /* Peek at the next token.  */
16096       token = cp_lexer_peek_token (parser->lexer);
16097
16098       /* If we've reached the token we want, consume it and stop.  */
16099       if (token->type == type && !nesting_depth)
16100         {
16101           cp_lexer_consume_token (parser->lexer);
16102           return;
16103         }
16104
16105       switch (token->type)
16106         {
16107         case CPP_EOF:
16108         case CPP_PRAGMA_EOL:
16109           /* If we've run out of tokens, stop.  */
16110           return;
16111
16112         case CPP_OPEN_BRACE:
16113         case CPP_OPEN_PAREN:
16114         case CPP_OPEN_SQUARE:
16115           ++nesting_depth;
16116           break;
16117
16118         case CPP_CLOSE_BRACE:
16119         case CPP_CLOSE_PAREN:
16120         case CPP_CLOSE_SQUARE:
16121           if (nesting_depth-- == 0)
16122             return;
16123           break;
16124
16125         default:
16126           break;
16127         }
16128
16129       /* Consume this token.  */
16130       cp_lexer_consume_token (parser->lexer);
16131     }
16132 }
16133
16134 /* If the next token is the indicated keyword, consume it.  Otherwise,
16135    issue an error message indicating that TOKEN_DESC was expected.
16136
16137    Returns the token consumed, if the token had the appropriate type.
16138    Otherwise, returns NULL.  */
16139
16140 static cp_token *
16141 cp_parser_require_keyword (cp_parser* parser,
16142                            enum rid keyword,
16143                            const char* token_desc)
16144 {
16145   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16146
16147   if (token && token->keyword != keyword)
16148     {
16149       dyn_string_t error_msg;
16150
16151       /* Format the error message.  */
16152       error_msg = dyn_string_new (0);
16153       dyn_string_append_cstr (error_msg, "expected ");
16154       dyn_string_append_cstr (error_msg, token_desc);
16155       cp_parser_error (parser, error_msg->s);
16156       dyn_string_delete (error_msg);
16157       return NULL;
16158     }
16159
16160   return token;
16161 }
16162
16163 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16164    function-definition.  */
16165
16166 static bool
16167 cp_parser_token_starts_function_definition_p (cp_token* token)
16168 {
16169   return (/* An ordinary function-body begins with an `{'.  */
16170           token->type == CPP_OPEN_BRACE
16171           /* A ctor-initializer begins with a `:'.  */
16172           || token->type == CPP_COLON
16173           /* A function-try-block begins with `try'.  */
16174           || token->keyword == RID_TRY
16175           /* The named return value extension begins with `return'.  */
16176           || token->keyword == RID_RETURN);
16177 }
16178
16179 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16180    definition.  */
16181
16182 static bool
16183 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16184 {
16185   cp_token *token;
16186
16187   token = cp_lexer_peek_token (parser->lexer);
16188   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16189 }
16190
16191 /* Returns TRUE iff the next token is the "," or ">" ending a
16192    template-argument.  */
16193
16194 static bool
16195 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16196 {
16197   cp_token *token;
16198
16199   token = cp_lexer_peek_token (parser->lexer);
16200   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16201 }
16202
16203 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16204    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
16205
16206 static bool
16207 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16208                                                      size_t n)
16209 {
16210   cp_token *token;
16211
16212   token = cp_lexer_peek_nth_token (parser->lexer, n);
16213   if (token->type == CPP_LESS)
16214     return true;
16215   /* Check for the sequence `<::' in the original code. It would be lexed as
16216      `[:', where `[' is a digraph, and there is no whitespace before
16217      `:'.  */
16218   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16219     {
16220       cp_token *token2;
16221       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16222       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16223         return true;
16224     }
16225   return false;
16226 }
16227
16228 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16229    or none_type otherwise.  */
16230
16231 static enum tag_types
16232 cp_parser_token_is_class_key (cp_token* token)
16233 {
16234   switch (token->keyword)
16235     {
16236     case RID_CLASS:
16237       return class_type;
16238     case RID_STRUCT:
16239       return record_type;
16240     case RID_UNION:
16241       return union_type;
16242
16243     default:
16244       return none_type;
16245     }
16246 }
16247
16248 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16249
16250 static void
16251 cp_parser_check_class_key (enum tag_types class_key, tree type)
16252 {
16253   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16254     pedwarn ("%qs tag used in naming %q#T",
16255             class_key == union_type ? "union"
16256              : class_key == record_type ? "struct" : "class",
16257              type);
16258 }
16259
16260 /* Issue an error message if DECL is redeclared with different
16261    access than its original declaration [class.access.spec/3].
16262    This applies to nested classes and nested class templates.
16263    [class.mem/1].  */
16264
16265 static void
16266 cp_parser_check_access_in_redeclaration (tree decl)
16267 {
16268   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16269     return;
16270
16271   if ((TREE_PRIVATE (decl)
16272        != (current_access_specifier == access_private_node))
16273       || (TREE_PROTECTED (decl)
16274           != (current_access_specifier == access_protected_node)))
16275     error ("%qD redeclared with different access", decl);
16276 }
16277
16278 /* Look for the `template' keyword, as a syntactic disambiguator.
16279    Return TRUE iff it is present, in which case it will be
16280    consumed.  */
16281
16282 static bool
16283 cp_parser_optional_template_keyword (cp_parser *parser)
16284 {
16285   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16286     {
16287       /* The `template' keyword can only be used within templates;
16288          outside templates the parser can always figure out what is a
16289          template and what is not.  */
16290       if (!processing_template_decl)
16291         {
16292           error ("%<template%> (as a disambiguator) is only allowed "
16293                  "within templates");
16294           /* If this part of the token stream is rescanned, the same
16295              error message would be generated.  So, we purge the token
16296              from the stream.  */
16297           cp_lexer_purge_token (parser->lexer);
16298           return false;
16299         }
16300       else
16301         {
16302           /* Consume the `template' keyword.  */
16303           cp_lexer_consume_token (parser->lexer);
16304           return true;
16305         }
16306     }
16307
16308   return false;
16309 }
16310
16311 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16312    set PARSER->SCOPE, and perform other related actions.  */
16313
16314 static void
16315 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16316 {
16317   tree value;
16318   tree check;
16319
16320   /* Get the stored value.  */
16321   value = cp_lexer_consume_token (parser->lexer)->value;
16322   /* Perform any access checks that were deferred.  */
16323   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16324     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16325   /* Set the scope from the stored value.  */
16326   parser->scope = TREE_VALUE (value);
16327   parser->qualifying_scope = TREE_TYPE (value);
16328   parser->object_scope = NULL_TREE;
16329 }
16330
16331 /* Consume tokens up through a non-nested END token.  */
16332
16333 static void
16334 cp_parser_cache_group (cp_parser *parser,
16335                        enum cpp_ttype end,
16336                        unsigned depth)
16337 {
16338   while (true)
16339     {
16340       cp_token *token;
16341
16342       /* Abort a parenthesized expression if we encounter a brace.  */
16343       if ((end == CPP_CLOSE_PAREN || depth == 0)
16344           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16345         return;
16346       /* If we've reached the end of the file, stop.  */
16347       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16348           || (end != CPP_PRAGMA_EOL
16349               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16350         return;
16351       /* Consume the next token.  */
16352       token = cp_lexer_consume_token (parser->lexer);
16353       /* See if it starts a new group.  */
16354       if (token->type == CPP_OPEN_BRACE)
16355         {
16356           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16357           if (depth == 0)
16358             return;
16359         }
16360       else if (token->type == CPP_OPEN_PAREN)
16361         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16362       else if (token->type == CPP_PRAGMA)
16363         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16364       else if (token->type == end)
16365         return;
16366     }
16367 }
16368
16369 /* Begin parsing tentatively.  We always save tokens while parsing
16370    tentatively so that if the tentative parsing fails we can restore the
16371    tokens.  */
16372
16373 static void
16374 cp_parser_parse_tentatively (cp_parser* parser)
16375 {
16376   /* Enter a new parsing context.  */
16377   parser->context = cp_parser_context_new (parser->context);
16378   /* Begin saving tokens.  */
16379   cp_lexer_save_tokens (parser->lexer);
16380   /* In order to avoid repetitive access control error messages,
16381      access checks are queued up until we are no longer parsing
16382      tentatively.  */
16383   push_deferring_access_checks (dk_deferred);
16384 }
16385
16386 /* Commit to the currently active tentative parse.  */
16387
16388 static void
16389 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16390 {
16391   cp_parser_context *context;
16392   cp_lexer *lexer;
16393
16394   /* Mark all of the levels as committed.  */
16395   lexer = parser->lexer;
16396   for (context = parser->context; context->next; context = context->next)
16397     {
16398       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16399         break;
16400       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16401       while (!cp_lexer_saving_tokens (lexer))
16402         lexer = lexer->next;
16403       cp_lexer_commit_tokens (lexer);
16404     }
16405 }
16406
16407 /* Abort the currently active tentative parse.  All consumed tokens
16408    will be rolled back, and no diagnostics will be issued.  */
16409
16410 static void
16411 cp_parser_abort_tentative_parse (cp_parser* parser)
16412 {
16413   cp_parser_simulate_error (parser);
16414   /* Now, pretend that we want to see if the construct was
16415      successfully parsed.  */
16416   cp_parser_parse_definitely (parser);
16417 }
16418
16419 /* Stop parsing tentatively.  If a parse error has occurred, restore the
16420    token stream.  Otherwise, commit to the tokens we have consumed.
16421    Returns true if no error occurred; false otherwise.  */
16422
16423 static bool
16424 cp_parser_parse_definitely (cp_parser* parser)
16425 {
16426   bool error_occurred;
16427   cp_parser_context *context;
16428
16429   /* Remember whether or not an error occurred, since we are about to
16430      destroy that information.  */
16431   error_occurred = cp_parser_error_occurred (parser);
16432   /* Remove the topmost context from the stack.  */
16433   context = parser->context;
16434   parser->context = context->next;
16435   /* If no parse errors occurred, commit to the tentative parse.  */
16436   if (!error_occurred)
16437     {
16438       /* Commit to the tokens read tentatively, unless that was
16439          already done.  */
16440       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16441         cp_lexer_commit_tokens (parser->lexer);
16442
16443       pop_to_parent_deferring_access_checks ();
16444     }
16445   /* Otherwise, if errors occurred, roll back our state so that things
16446      are just as they were before we began the tentative parse.  */
16447   else
16448     {
16449       cp_lexer_rollback_tokens (parser->lexer);
16450       pop_deferring_access_checks ();
16451     }
16452   /* Add the context to the front of the free list.  */
16453   context->next = cp_parser_context_free_list;
16454   cp_parser_context_free_list = context;
16455
16456   return !error_occurred;
16457 }
16458
16459 /* Returns true if we are parsing tentatively and are not committed to
16460    this tentative parse.  */
16461
16462 static bool
16463 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16464 {
16465   return (cp_parser_parsing_tentatively (parser)
16466           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16467 }
16468
16469 /* Returns nonzero iff an error has occurred during the most recent
16470    tentative parse.  */
16471
16472 static bool
16473 cp_parser_error_occurred (cp_parser* parser)
16474 {
16475   return (cp_parser_parsing_tentatively (parser)
16476           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16477 }
16478
16479 /* Returns nonzero if GNU extensions are allowed.  */
16480
16481 static bool
16482 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16483 {
16484   return parser->allow_gnu_extensions_p;
16485 }
16486 \f
16487 /* Objective-C++ Productions */
16488
16489
16490 /* Parse an Objective-C expression, which feeds into a primary-expression
16491    above.
16492
16493    objc-expression:
16494      objc-message-expression
16495      objc-string-literal
16496      objc-encode-expression
16497      objc-protocol-expression
16498      objc-selector-expression
16499
16500   Returns a tree representation of the expression.  */
16501
16502 static tree
16503 cp_parser_objc_expression (cp_parser* parser)
16504 {
16505   /* Try to figure out what kind of declaration is present.  */
16506   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16507
16508   switch (kwd->type)
16509     {
16510     case CPP_OPEN_SQUARE:
16511       return cp_parser_objc_message_expression (parser);
16512
16513     case CPP_OBJC_STRING:
16514       kwd = cp_lexer_consume_token (parser->lexer);
16515       return objc_build_string_object (kwd->value);
16516
16517     case CPP_KEYWORD:
16518       switch (kwd->keyword)
16519         {
16520         case RID_AT_ENCODE:
16521           return cp_parser_objc_encode_expression (parser);
16522
16523         case RID_AT_PROTOCOL:
16524           return cp_parser_objc_protocol_expression (parser);
16525
16526         case RID_AT_SELECTOR:
16527           return cp_parser_objc_selector_expression (parser);
16528
16529         default:
16530           break;
16531         }
16532     default:
16533       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16534       cp_parser_skip_to_end_of_block_or_statement (parser);
16535     }
16536
16537   return error_mark_node;
16538 }
16539
16540 /* Parse an Objective-C message expression.
16541
16542    objc-message-expression:
16543      [ objc-message-receiver objc-message-args ]
16544
16545    Returns a representation of an Objective-C message.  */
16546
16547 static tree
16548 cp_parser_objc_message_expression (cp_parser* parser)
16549 {
16550   tree receiver, messageargs;
16551
16552   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
16553   receiver = cp_parser_objc_message_receiver (parser);
16554   messageargs = cp_parser_objc_message_args (parser);
16555   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16556
16557   return objc_build_message_expr (build_tree_list (receiver, messageargs));
16558 }
16559
16560 /* Parse an objc-message-receiver.
16561
16562    objc-message-receiver:
16563      expression
16564      simple-type-specifier
16565
16566   Returns a representation of the type or expression.  */
16567
16568 static tree
16569 cp_parser_objc_message_receiver (cp_parser* parser)
16570 {
16571   tree rcv;
16572
16573   /* An Objective-C message receiver may be either (1) a type
16574      or (2) an expression.  */
16575   cp_parser_parse_tentatively (parser);
16576   rcv = cp_parser_expression (parser, false);
16577
16578   if (cp_parser_parse_definitely (parser))
16579     return rcv;
16580
16581   rcv = cp_parser_simple_type_specifier (parser,
16582                                          /*decl_specs=*/NULL,
16583                                          CP_PARSER_FLAGS_NONE);
16584
16585   return objc_get_class_reference (rcv);
16586 }
16587
16588 /* Parse the arguments and selectors comprising an Objective-C message.
16589
16590    objc-message-args:
16591      objc-selector
16592      objc-selector-args
16593      objc-selector-args , objc-comma-args
16594
16595    objc-selector-args:
16596      objc-selector [opt] : assignment-expression
16597      objc-selector-args objc-selector [opt] : assignment-expression
16598
16599    objc-comma-args:
16600      assignment-expression
16601      objc-comma-args , assignment-expression
16602
16603    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16604    selector arguments and TREE_VALUE containing a list of comma
16605    arguments.  */
16606
16607 static tree
16608 cp_parser_objc_message_args (cp_parser* parser)
16609 {
16610   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16611   bool maybe_unary_selector_p = true;
16612   cp_token *token = cp_lexer_peek_token (parser->lexer);
16613
16614   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16615     {
16616       tree selector = NULL_TREE, arg;
16617
16618       if (token->type != CPP_COLON)
16619         selector = cp_parser_objc_selector (parser);
16620
16621       /* Detect if we have a unary selector.  */
16622       if (maybe_unary_selector_p
16623           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16624         return build_tree_list (selector, NULL_TREE);
16625
16626       maybe_unary_selector_p = false;
16627       cp_parser_require (parser, CPP_COLON, "`:'");
16628       arg = cp_parser_assignment_expression (parser, false);
16629
16630       sel_args
16631         = chainon (sel_args,
16632                    build_tree_list (selector, arg));
16633
16634       token = cp_lexer_peek_token (parser->lexer);
16635     }
16636
16637   /* Handle non-selector arguments, if any. */
16638   while (token->type == CPP_COMMA)
16639     {
16640       tree arg;
16641
16642       cp_lexer_consume_token (parser->lexer);
16643       arg = cp_parser_assignment_expression (parser, false);
16644
16645       addl_args
16646         = chainon (addl_args,
16647                    build_tree_list (NULL_TREE, arg));
16648
16649       token = cp_lexer_peek_token (parser->lexer);
16650     }
16651
16652   return build_tree_list (sel_args, addl_args);
16653 }
16654
16655 /* Parse an Objective-C encode expression.
16656
16657    objc-encode-expression:
16658      @encode objc-typename
16659
16660    Returns an encoded representation of the type argument.  */
16661
16662 static tree
16663 cp_parser_objc_encode_expression (cp_parser* parser)
16664 {
16665   tree type;
16666
16667   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
16668   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16669   type = complete_type (cp_parser_type_id (parser));
16670   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16671
16672   if (!type)
16673     {
16674       error ("%<@encode%> must specify a type as an argument");
16675       return error_mark_node;
16676     }
16677
16678   return objc_build_encode_expr (type);
16679 }
16680
16681 /* Parse an Objective-C @defs expression.  */
16682
16683 static tree
16684 cp_parser_objc_defs_expression (cp_parser *parser)
16685 {
16686   tree name;
16687
16688   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
16689   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16690   name = cp_parser_identifier (parser);
16691   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16692
16693   return objc_get_class_ivars (name);
16694 }
16695
16696 /* Parse an Objective-C protocol expression.
16697
16698   objc-protocol-expression:
16699     @protocol ( identifier )
16700
16701   Returns a representation of the protocol expression.  */
16702
16703 static tree
16704 cp_parser_objc_protocol_expression (cp_parser* parser)
16705 {
16706   tree proto;
16707
16708   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
16709   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16710   proto = cp_parser_identifier (parser);
16711   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16712
16713   return objc_build_protocol_expr (proto);
16714 }
16715
16716 /* Parse an Objective-C selector expression.
16717
16718    objc-selector-expression:
16719      @selector ( objc-method-signature )
16720
16721    objc-method-signature:
16722      objc-selector
16723      objc-selector-seq
16724
16725    objc-selector-seq:
16726      objc-selector :
16727      objc-selector-seq objc-selector :
16728
16729   Returns a representation of the method selector.  */
16730
16731 static tree
16732 cp_parser_objc_selector_expression (cp_parser* parser)
16733 {
16734   tree sel_seq = NULL_TREE;
16735   bool maybe_unary_selector_p = true;
16736   cp_token *token;
16737
16738   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
16739   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16740   token = cp_lexer_peek_token (parser->lexer);
16741
16742   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16743          || token->type == CPP_SCOPE)
16744     {
16745       tree selector = NULL_TREE;
16746
16747       if (token->type != CPP_COLON
16748           || token->type == CPP_SCOPE)
16749         selector = cp_parser_objc_selector (parser);
16750
16751       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16752           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16753         {
16754           /* Detect if we have a unary selector.  */
16755           if (maybe_unary_selector_p)
16756             {
16757               sel_seq = selector;
16758               goto finish_selector;
16759             }
16760           else
16761             {
16762               cp_parser_error (parser, "expected %<:%>");
16763             }
16764         }
16765       maybe_unary_selector_p = false;
16766       token = cp_lexer_consume_token (parser->lexer);
16767       
16768       if (token->type == CPP_SCOPE)
16769         {
16770           sel_seq
16771             = chainon (sel_seq,
16772                        build_tree_list (selector, NULL_TREE));
16773           sel_seq
16774             = chainon (sel_seq,
16775                        build_tree_list (NULL_TREE, NULL_TREE));
16776         }
16777       else
16778         sel_seq
16779           = chainon (sel_seq,
16780                      build_tree_list (selector, NULL_TREE));
16781
16782       token = cp_lexer_peek_token (parser->lexer);
16783     }
16784
16785  finish_selector:
16786   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16787
16788   return objc_build_selector_expr (sel_seq);
16789 }
16790
16791 /* Parse a list of identifiers.
16792
16793    objc-identifier-list:
16794      identifier
16795      objc-identifier-list , identifier
16796
16797    Returns a TREE_LIST of identifier nodes.  */
16798
16799 static tree
16800 cp_parser_objc_identifier_list (cp_parser* parser)
16801 {
16802   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16803   cp_token *sep = cp_lexer_peek_token (parser->lexer);
16804
16805   while (sep->type == CPP_COMMA)
16806     {
16807       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16808       list = chainon (list,
16809                       build_tree_list (NULL_TREE,
16810                                        cp_parser_identifier (parser)));
16811       sep = cp_lexer_peek_token (parser->lexer);
16812     }
16813
16814   return list;
16815 }
16816
16817 /* Parse an Objective-C alias declaration.
16818
16819    objc-alias-declaration:
16820      @compatibility_alias identifier identifier ;
16821
16822    This function registers the alias mapping with the Objective-C front-end.
16823    It returns nothing.  */
16824
16825 static void
16826 cp_parser_objc_alias_declaration (cp_parser* parser)
16827 {
16828   tree alias, orig;
16829
16830   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
16831   alias = cp_parser_identifier (parser);
16832   orig = cp_parser_identifier (parser);
16833   objc_declare_alias (alias, orig);
16834   cp_parser_consume_semicolon_at_end_of_statement (parser);
16835 }
16836
16837 /* Parse an Objective-C class forward-declaration.
16838
16839    objc-class-declaration:
16840      @class objc-identifier-list ;
16841
16842    The function registers the forward declarations with the Objective-C
16843    front-end.  It returns nothing.  */
16844
16845 static void
16846 cp_parser_objc_class_declaration (cp_parser* parser)
16847 {
16848   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
16849   objc_declare_class (cp_parser_objc_identifier_list (parser));
16850   cp_parser_consume_semicolon_at_end_of_statement (parser);
16851 }
16852
16853 /* Parse a list of Objective-C protocol references.
16854
16855    objc-protocol-refs-opt:
16856      objc-protocol-refs [opt]
16857
16858    objc-protocol-refs:
16859      < objc-identifier-list >
16860
16861    Returns a TREE_LIST of identifiers, if any.  */
16862
16863 static tree
16864 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16865 {
16866   tree protorefs = NULL_TREE;
16867
16868   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16869     {
16870       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
16871       protorefs = cp_parser_objc_identifier_list (parser);
16872       cp_parser_require (parser, CPP_GREATER, "`>'");
16873     }
16874
16875   return protorefs;
16876 }
16877
16878 /* Parse a Objective-C visibility specification.  */
16879
16880 static void
16881 cp_parser_objc_visibility_spec (cp_parser* parser)
16882 {
16883   cp_token *vis = cp_lexer_peek_token (parser->lexer);
16884
16885   switch (vis->keyword)
16886     {
16887     case RID_AT_PRIVATE:
16888       objc_set_visibility (2);
16889       break;
16890     case RID_AT_PROTECTED:
16891       objc_set_visibility (0);
16892       break;
16893     case RID_AT_PUBLIC:
16894       objc_set_visibility (1);
16895       break;
16896     default:
16897       return;
16898     }
16899
16900   /* Eat '@private'/'@protected'/'@public'.  */
16901   cp_lexer_consume_token (parser->lexer);
16902 }
16903
16904 /* Parse an Objective-C method type.  */
16905
16906 static void
16907 cp_parser_objc_method_type (cp_parser* parser)
16908 {
16909   objc_set_method_type
16910    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16911     ? PLUS_EXPR
16912     : MINUS_EXPR);
16913 }
16914
16915 /* Parse an Objective-C protocol qualifier.  */
16916
16917 static tree
16918 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16919 {
16920   tree quals = NULL_TREE, node;
16921   cp_token *token = cp_lexer_peek_token (parser->lexer);
16922
16923   node = token->value;
16924
16925   while (node && TREE_CODE (node) == IDENTIFIER_NODE
16926          && (node == ridpointers [(int) RID_IN]
16927              || node == ridpointers [(int) RID_OUT]
16928              || node == ridpointers [(int) RID_INOUT]
16929              || node == ridpointers [(int) RID_BYCOPY]
16930              || node == ridpointers [(int) RID_BYREF]
16931              || node == ridpointers [(int) RID_ONEWAY]))
16932     {
16933       quals = tree_cons (NULL_TREE, node, quals);
16934       cp_lexer_consume_token (parser->lexer);
16935       token = cp_lexer_peek_token (parser->lexer);
16936       node = token->value;
16937     }
16938
16939   return quals;
16940 }
16941
16942 /* Parse an Objective-C typename.  */
16943
16944 static tree
16945 cp_parser_objc_typename (cp_parser* parser)
16946 {
16947   tree typename = NULL_TREE;
16948
16949   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16950     {
16951       tree proto_quals, cp_type = NULL_TREE;
16952
16953       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
16954       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16955
16956       /* An ObjC type name may consist of just protocol qualifiers, in which
16957          case the type shall default to 'id'.  */
16958       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16959         cp_type = cp_parser_type_id (parser);
16960
16961       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16962       typename = build_tree_list (proto_quals, cp_type);
16963     }
16964
16965   return typename;
16966 }
16967
16968 /* Check to see if TYPE refers to an Objective-C selector name.  */
16969
16970 static bool
16971 cp_parser_objc_selector_p (enum cpp_ttype type)
16972 {
16973   return (type == CPP_NAME || type == CPP_KEYWORD
16974           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16975           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16976           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16977           || type == CPP_XOR || type == CPP_XOR_EQ);
16978 }
16979
16980 /* Parse an Objective-C selector.  */
16981
16982 static tree
16983 cp_parser_objc_selector (cp_parser* parser)
16984 {
16985   cp_token *token = cp_lexer_consume_token (parser->lexer);
16986
16987   if (!cp_parser_objc_selector_p (token->type))
16988     {
16989       error ("invalid Objective-C++ selector name");
16990       return error_mark_node;
16991     }
16992
16993   /* C++ operator names are allowed to appear in ObjC selectors.  */
16994   switch (token->type)
16995     {
16996     case CPP_AND_AND: return get_identifier ("and");
16997     case CPP_AND_EQ: return get_identifier ("and_eq");
16998     case CPP_AND: return get_identifier ("bitand");
16999     case CPP_OR: return get_identifier ("bitor");
17000     case CPP_COMPL: return get_identifier ("compl");
17001     case CPP_NOT: return get_identifier ("not");
17002     case CPP_NOT_EQ: return get_identifier ("not_eq");
17003     case CPP_OR_OR: return get_identifier ("or");
17004     case CPP_OR_EQ: return get_identifier ("or_eq");
17005     case CPP_XOR: return get_identifier ("xor");
17006     case CPP_XOR_EQ: return get_identifier ("xor_eq");
17007     default: return token->value;
17008     }
17009 }
17010
17011 /* Parse an Objective-C params list.  */
17012
17013 static tree
17014 cp_parser_objc_method_keyword_params (cp_parser* parser)
17015 {
17016   tree params = NULL_TREE;
17017   bool maybe_unary_selector_p = true;
17018   cp_token *token = cp_lexer_peek_token (parser->lexer);
17019
17020   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17021     {
17022       tree selector = NULL_TREE, typename, identifier;
17023
17024       if (token->type != CPP_COLON)
17025         selector = cp_parser_objc_selector (parser);
17026
17027       /* Detect if we have a unary selector.  */
17028       if (maybe_unary_selector_p
17029           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17030         return selector;
17031
17032       maybe_unary_selector_p = false;
17033       cp_parser_require (parser, CPP_COLON, "`:'");
17034       typename = cp_parser_objc_typename (parser);
17035       identifier = cp_parser_identifier (parser);
17036
17037       params
17038         = chainon (params,
17039                    objc_build_keyword_decl (selector,
17040                                             typename,
17041                                             identifier));
17042
17043       token = cp_lexer_peek_token (parser->lexer);
17044     }
17045
17046   return params;
17047 }
17048
17049 /* Parse the non-keyword Objective-C params.  */
17050
17051 static tree
17052 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17053 {
17054   tree params = make_node (TREE_LIST);
17055   cp_token *token = cp_lexer_peek_token (parser->lexer);
17056   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
17057
17058   while (token->type == CPP_COMMA)
17059     {
17060       cp_parameter_declarator *parmdecl;
17061       tree parm;
17062
17063       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17064       token = cp_lexer_peek_token (parser->lexer);
17065
17066       if (token->type == CPP_ELLIPSIS)
17067         {
17068           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
17069           *ellipsisp = true;
17070           break;
17071         }
17072
17073       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17074       parm = grokdeclarator (parmdecl->declarator,
17075                              &parmdecl->decl_specifiers,
17076                              PARM, /*initialized=*/0,
17077                              /*attrlist=*/NULL);
17078
17079       chainon (params, build_tree_list (NULL_TREE, parm));
17080       token = cp_lexer_peek_token (parser->lexer);
17081     }
17082
17083   return params;
17084 }
17085
17086 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
17087
17088 static void
17089 cp_parser_objc_interstitial_code (cp_parser* parser)
17090 {
17091   cp_token *token = cp_lexer_peek_token (parser->lexer);
17092
17093   /* If the next token is `extern' and the following token is a string
17094      literal, then we have a linkage specification.  */
17095   if (token->keyword == RID_EXTERN
17096       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17097     cp_parser_linkage_specification (parser);
17098   /* Handle #pragma, if any.  */
17099   else if (token->type == CPP_PRAGMA)
17100     cp_parser_pragma (parser, pragma_external);
17101   /* Allow stray semicolons.  */
17102   else if (token->type == CPP_SEMICOLON)
17103     cp_lexer_consume_token (parser->lexer);
17104   /* Finally, try to parse a block-declaration, or a function-definition.  */
17105   else
17106     cp_parser_block_declaration (parser, /*statement_p=*/false);
17107 }
17108
17109 /* Parse a method signature.  */
17110
17111 static tree
17112 cp_parser_objc_method_signature (cp_parser* parser)
17113 {
17114   tree rettype, kwdparms, optparms;
17115   bool ellipsis = false;
17116
17117   cp_parser_objc_method_type (parser);
17118   rettype = cp_parser_objc_typename (parser);
17119   kwdparms = cp_parser_objc_method_keyword_params (parser);
17120   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17121
17122   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17123 }
17124
17125 /* Pars an Objective-C method prototype list.  */
17126
17127 static void
17128 cp_parser_objc_method_prototype_list (cp_parser* parser)
17129 {
17130   cp_token *token = cp_lexer_peek_token (parser->lexer);
17131
17132   while (token->keyword != RID_AT_END)
17133     {
17134       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17135         {
17136           objc_add_method_declaration
17137            (cp_parser_objc_method_signature (parser));
17138           cp_parser_consume_semicolon_at_end_of_statement (parser);
17139         }
17140       else
17141         /* Allow for interspersed non-ObjC++ code.  */
17142         cp_parser_objc_interstitial_code (parser);
17143
17144       token = cp_lexer_peek_token (parser->lexer);
17145     }
17146
17147   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17148   objc_finish_interface ();
17149 }
17150
17151 /* Parse an Objective-C method definition list.  */
17152
17153 static void
17154 cp_parser_objc_method_definition_list (cp_parser* parser)
17155 {
17156   cp_token *token = cp_lexer_peek_token (parser->lexer);
17157
17158   while (token->keyword != RID_AT_END)
17159     {
17160       tree meth;
17161
17162       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17163         {
17164           push_deferring_access_checks (dk_deferred);
17165           objc_start_method_definition
17166            (cp_parser_objc_method_signature (parser));
17167
17168           /* For historical reasons, we accept an optional semicolon.  */
17169           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17170             cp_lexer_consume_token (parser->lexer);
17171
17172           perform_deferred_access_checks ();
17173           stop_deferring_access_checks ();
17174           meth = cp_parser_function_definition_after_declarator (parser,
17175                                                                  false);
17176           pop_deferring_access_checks ();
17177           objc_finish_method_definition (meth);
17178         }
17179       else
17180         /* Allow for interspersed non-ObjC++ code.  */
17181         cp_parser_objc_interstitial_code (parser);
17182
17183       token = cp_lexer_peek_token (parser->lexer);
17184     }
17185
17186   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17187   objc_finish_implementation ();
17188 }
17189
17190 /* Parse Objective-C ivars.  */
17191
17192 static void
17193 cp_parser_objc_class_ivars (cp_parser* parser)
17194 {
17195   cp_token *token = cp_lexer_peek_token (parser->lexer);
17196
17197   if (token->type != CPP_OPEN_BRACE)
17198     return;     /* No ivars specified.  */
17199
17200   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
17201   token = cp_lexer_peek_token (parser->lexer);
17202
17203   while (token->type != CPP_CLOSE_BRACE)
17204     {
17205       cp_decl_specifier_seq declspecs;
17206       int decl_class_or_enum_p;
17207       tree prefix_attributes;
17208
17209       cp_parser_objc_visibility_spec (parser);
17210
17211       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17212         break;
17213
17214       cp_parser_decl_specifier_seq (parser,
17215                                     CP_PARSER_FLAGS_OPTIONAL,
17216                                     &declspecs,
17217                                     &decl_class_or_enum_p);
17218       prefix_attributes = declspecs.attributes;
17219       declspecs.attributes = NULL_TREE;
17220
17221       /* Keep going until we hit the `;' at the end of the
17222          declaration.  */
17223       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17224         {
17225           tree width = NULL_TREE, attributes, first_attribute, decl;
17226           cp_declarator *declarator = NULL;
17227           int ctor_dtor_or_conv_p;
17228
17229           /* Check for a (possibly unnamed) bitfield declaration.  */
17230           token = cp_lexer_peek_token (parser->lexer);
17231           if (token->type == CPP_COLON)
17232             goto eat_colon;
17233
17234           if (token->type == CPP_NAME
17235               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17236                   == CPP_COLON))
17237             {
17238               /* Get the name of the bitfield.  */
17239               declarator = make_id_declarator (NULL_TREE,
17240                                                cp_parser_identifier (parser),
17241                                                sfk_none);
17242
17243              eat_colon:
17244               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17245               /* Get the width of the bitfield.  */
17246               width
17247                 = cp_parser_constant_expression (parser,
17248                                                  /*allow_non_constant=*/false,
17249                                                  NULL);
17250             }
17251           else
17252             {
17253               /* Parse the declarator.  */
17254               declarator
17255                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17256                                         &ctor_dtor_or_conv_p,
17257                                         /*parenthesized_p=*/NULL,
17258                                         /*member_p=*/false);
17259             }
17260
17261           /* Look for attributes that apply to the ivar.  */
17262           attributes = cp_parser_attributes_opt (parser);
17263           /* Remember which attributes are prefix attributes and
17264              which are not.  */
17265           first_attribute = attributes;
17266           /* Combine the attributes.  */
17267           attributes = chainon (prefix_attributes, attributes);
17268
17269           if (width)
17270             {
17271               /* Create the bitfield declaration.  */
17272               decl = grokbitfield (declarator, &declspecs, width);
17273               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17274             }
17275           else
17276             decl = grokfield (declarator, &declspecs, 
17277                               NULL_TREE, /*init_const_expr_p=*/false,
17278                               NULL_TREE, attributes);
17279
17280           /* Add the instance variable.  */
17281           objc_add_instance_variable (decl);
17282
17283           /* Reset PREFIX_ATTRIBUTES.  */
17284           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17285             attributes = TREE_CHAIN (attributes);
17286           if (attributes)
17287             TREE_CHAIN (attributes) = NULL_TREE;
17288
17289           token = cp_lexer_peek_token (parser->lexer);
17290
17291           if (token->type == CPP_COMMA)
17292             {
17293               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17294               continue;
17295             }
17296           break;
17297         }
17298
17299       cp_parser_consume_semicolon_at_end_of_statement (parser);
17300       token = cp_lexer_peek_token (parser->lexer);
17301     }
17302
17303   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17304   /* For historical reasons, we accept an optional semicolon.  */
17305   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17306     cp_lexer_consume_token (parser->lexer);
17307 }
17308
17309 /* Parse an Objective-C protocol declaration.  */
17310
17311 static void
17312 cp_parser_objc_protocol_declaration (cp_parser* parser)
17313 {
17314   tree proto, protorefs;
17315   cp_token *tok;
17316
17317   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17318   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17319     {
17320       error ("identifier expected after %<@protocol%>");
17321       goto finish;
17322     }
17323
17324   /* See if we have a forward declaration or a definition.  */
17325   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17326
17327   /* Try a forward declaration first.  */
17328   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17329     {
17330       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17331      finish:
17332       cp_parser_consume_semicolon_at_end_of_statement (parser);
17333     }
17334
17335   /* Ok, we got a full-fledged definition (or at least should).  */
17336   else
17337     {
17338       proto = cp_parser_identifier (parser);
17339       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17340       objc_start_protocol (proto, protorefs);
17341       cp_parser_objc_method_prototype_list (parser);
17342     }
17343 }
17344
17345 /* Parse an Objective-C superclass or category.  */
17346
17347 static void
17348 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17349                                                           tree *categ)
17350 {
17351   cp_token *next = cp_lexer_peek_token (parser->lexer);
17352
17353   *super = *categ = NULL_TREE;
17354   if (next->type == CPP_COLON)
17355     {
17356       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17357       *super = cp_parser_identifier (parser);
17358     }
17359   else if (next->type == CPP_OPEN_PAREN)
17360     {
17361       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17362       *categ = cp_parser_identifier (parser);
17363       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17364     }
17365 }
17366
17367 /* Parse an Objective-C class interface.  */
17368
17369 static void
17370 cp_parser_objc_class_interface (cp_parser* parser)
17371 {
17372   tree name, super, categ, protos;
17373
17374   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17375   name = cp_parser_identifier (parser);
17376   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17377   protos = cp_parser_objc_protocol_refs_opt (parser);
17378
17379   /* We have either a class or a category on our hands.  */
17380   if (categ)
17381     objc_start_category_interface (name, categ, protos);
17382   else
17383     {
17384       objc_start_class_interface (name, super, protos);
17385       /* Handle instance variable declarations, if any.  */
17386       cp_parser_objc_class_ivars (parser);
17387       objc_continue_interface ();
17388     }
17389
17390   cp_parser_objc_method_prototype_list (parser);
17391 }
17392
17393 /* Parse an Objective-C class implementation.  */
17394
17395 static void
17396 cp_parser_objc_class_implementation (cp_parser* parser)
17397 {
17398   tree name, super, categ;
17399
17400   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17401   name = cp_parser_identifier (parser);
17402   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17403
17404   /* We have either a class or a category on our hands.  */
17405   if (categ)
17406     objc_start_category_implementation (name, categ);
17407   else
17408     {
17409       objc_start_class_implementation (name, super);
17410       /* Handle instance variable declarations, if any.  */
17411       cp_parser_objc_class_ivars (parser);
17412       objc_continue_implementation ();
17413     }
17414
17415   cp_parser_objc_method_definition_list (parser);
17416 }
17417
17418 /* Consume the @end token and finish off the implementation.  */
17419
17420 static void
17421 cp_parser_objc_end_implementation (cp_parser* parser)
17422 {
17423   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17424   objc_finish_implementation ();
17425 }
17426
17427 /* Parse an Objective-C declaration.  */
17428
17429 static void
17430 cp_parser_objc_declaration (cp_parser* parser)
17431 {
17432   /* Try to figure out what kind of declaration is present.  */
17433   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17434
17435   switch (kwd->keyword)
17436     {
17437     case RID_AT_ALIAS:
17438       cp_parser_objc_alias_declaration (parser);
17439       break;
17440     case RID_AT_CLASS:
17441       cp_parser_objc_class_declaration (parser);
17442       break;
17443     case RID_AT_PROTOCOL:
17444       cp_parser_objc_protocol_declaration (parser);
17445       break;
17446     case RID_AT_INTERFACE:
17447       cp_parser_objc_class_interface (parser);
17448       break;
17449     case RID_AT_IMPLEMENTATION:
17450       cp_parser_objc_class_implementation (parser);
17451       break;
17452     case RID_AT_END:
17453       cp_parser_objc_end_implementation (parser);
17454       break;
17455     default:
17456       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17457       cp_parser_skip_to_end_of_block_or_statement (parser);
17458     }
17459 }
17460
17461 /* Parse an Objective-C try-catch-finally statement.
17462
17463    objc-try-catch-finally-stmt:
17464      @try compound-statement objc-catch-clause-seq [opt]
17465        objc-finally-clause [opt]
17466
17467    objc-catch-clause-seq:
17468      objc-catch-clause objc-catch-clause-seq [opt]
17469
17470    objc-catch-clause:
17471      @catch ( exception-declaration ) compound-statement
17472
17473    objc-finally-clause
17474      @finally compound-statement
17475
17476    Returns NULL_TREE.  */
17477
17478 static tree
17479 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17480   location_t location;
17481   tree stmt;
17482
17483   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17484   location = cp_lexer_peek_token (parser->lexer)->location;
17485   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17486      node, lest it get absorbed into the surrounding block.  */
17487   stmt = push_stmt_list ();
17488   cp_parser_compound_statement (parser, NULL, false);
17489   objc_begin_try_stmt (location, pop_stmt_list (stmt));
17490
17491   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17492     {
17493       cp_parameter_declarator *parmdecl;
17494       tree parm;
17495
17496       cp_lexer_consume_token (parser->lexer);
17497       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17498       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17499       parm = grokdeclarator (parmdecl->declarator,
17500                              &parmdecl->decl_specifiers,
17501                              PARM, /*initialized=*/0,
17502                              /*attrlist=*/NULL);
17503       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17504       objc_begin_catch_clause (parm);
17505       cp_parser_compound_statement (parser, NULL, false);
17506       objc_finish_catch_clause ();
17507     }
17508
17509   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17510     {
17511       cp_lexer_consume_token (parser->lexer);
17512       location = cp_lexer_peek_token (parser->lexer)->location;
17513       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17514          node, lest it get absorbed into the surrounding block.  */
17515       stmt = push_stmt_list ();
17516       cp_parser_compound_statement (parser, NULL, false);
17517       objc_build_finally_clause (location, pop_stmt_list (stmt));
17518     }
17519
17520   return objc_finish_try_stmt ();
17521 }
17522
17523 /* Parse an Objective-C synchronized statement.
17524
17525    objc-synchronized-stmt:
17526      @synchronized ( expression ) compound-statement
17527
17528    Returns NULL_TREE.  */
17529
17530 static tree
17531 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17532   location_t location;
17533   tree lock, stmt;
17534
17535   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17536
17537   location = cp_lexer_peek_token (parser->lexer)->location;
17538   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17539   lock = cp_parser_expression (parser, false);
17540   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17541
17542   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17543      node, lest it get absorbed into the surrounding block.  */
17544   stmt = push_stmt_list ();
17545   cp_parser_compound_statement (parser, NULL, false);
17546
17547   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17548 }
17549
17550 /* Parse an Objective-C throw statement.
17551
17552    objc-throw-stmt:
17553      @throw assignment-expression [opt] ;
17554
17555    Returns a constructed '@throw' statement.  */
17556
17557 static tree
17558 cp_parser_objc_throw_statement (cp_parser *parser) {
17559   tree expr = NULL_TREE;
17560
17561   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17562
17563   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17564     expr = cp_parser_assignment_expression (parser, false);
17565
17566   cp_parser_consume_semicolon_at_end_of_statement (parser);
17567
17568   return objc_build_throw_stmt (expr);
17569 }
17570
17571 /* Parse an Objective-C statement.  */
17572
17573 static tree
17574 cp_parser_objc_statement (cp_parser * parser) {
17575   /* Try to figure out what kind of declaration is present.  */
17576   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17577
17578   switch (kwd->keyword)
17579     {
17580     case RID_AT_TRY:
17581       return cp_parser_objc_try_catch_finally_statement (parser);
17582     case RID_AT_SYNCHRONIZED:
17583       return cp_parser_objc_synchronized_statement (parser);
17584     case RID_AT_THROW:
17585       return cp_parser_objc_throw_statement (parser);
17586     default:
17587       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17588       cp_parser_skip_to_end_of_block_or_statement (parser);
17589     }
17590
17591   return error_mark_node;
17592 }
17593 /* The parser.  */
17594
17595 static GTY (()) cp_parser *the_parser;
17596
17597 \f
17598 /* Special handling for the first token or line in the file.  The first
17599    thing in the file might be #pragma GCC pch_preprocess, which loads a
17600    PCH file, which is a GC collection point.  So we need to handle this
17601    first pragma without benefit of an existing lexer structure.
17602
17603    Always returns one token to the caller in *FIRST_TOKEN.  This is 
17604    either the true first token of the file, or the first token after
17605    the initial pragma.  */
17606
17607 static void
17608 cp_parser_initial_pragma (cp_token *first_token)
17609 {
17610   tree name = NULL;
17611
17612   cp_lexer_get_preprocessor_token (NULL, first_token);
17613   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
17614     return;
17615
17616   cp_lexer_get_preprocessor_token (NULL, first_token);
17617   if (first_token->type == CPP_STRING)
17618     {
17619       name = first_token->value;
17620
17621       cp_lexer_get_preprocessor_token (NULL, first_token);
17622       if (first_token->type != CPP_PRAGMA_EOL)
17623         error ("junk at end of %<#pragma GCC pch_preprocess%>");
17624     }
17625   else
17626     error ("expected string literal");
17627
17628   /* Skip to the end of the pragma.  */
17629   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
17630     cp_lexer_get_preprocessor_token (NULL, first_token);
17631
17632   /* Read one more token to return to our caller.  */
17633   cp_lexer_get_preprocessor_token (NULL, first_token);
17634
17635   /* Now actually load the PCH file.  */
17636   if (name)
17637     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
17638 }
17639
17640 /* Normal parsing of a pragma token.  Here we can (and must) use the
17641    regular lexer.  */
17642
17643 static bool
17644 cp_parser_pragma (cp_parser *parser, enum pragma_context context ATTRIBUTE_UNUSED)
17645 {
17646   cp_token *pragma_tok;
17647   unsigned int id;
17648
17649   pragma_tok = cp_lexer_consume_token (parser->lexer);
17650   gcc_assert (pragma_tok->type == CPP_PRAGMA);
17651   parser->lexer->in_pragma = true;
17652
17653   id = pragma_tok->pragma_kind;
17654   switch (id)
17655     {
17656     case PRAGMA_GCC_PCH_PREPROCESS:
17657       error ("%<#pragma GCC pch_preprocess%> must be first");
17658       break;
17659
17660     default:
17661       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
17662       c_invoke_pragma_handler (id);
17663       break;
17664     }
17665
17666   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
17667   return false;
17668 }
17669
17670 /* The interface the pragma parsers have to the lexer.  */
17671
17672 enum cpp_ttype
17673 pragma_lex (tree *value)
17674 {
17675   cp_token *tok;
17676   enum cpp_ttype ret;
17677
17678   tok = cp_lexer_peek_token (the_parser->lexer);
17679
17680   ret = tok->type;
17681   *value = tok->value;
17682
17683   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
17684     ret = CPP_EOF;
17685   else if (ret == CPP_STRING)
17686     *value = cp_parser_string_literal (the_parser, false, false);
17687   else
17688     {
17689       cp_lexer_consume_token (the_parser->lexer);
17690       if (ret == CPP_KEYWORD)
17691         ret = CPP_NAME;
17692     }
17693
17694   return ret;
17695 }
17696
17697 \f
17698 /* External interface.  */
17699
17700 /* Parse one entire translation unit.  */
17701
17702 void
17703 c_parse_file (void)
17704 {
17705   bool error_occurred;
17706   static bool already_called = false;
17707
17708   if (already_called)
17709     {
17710       sorry ("inter-module optimizations not implemented for C++");
17711       return;
17712     }
17713   already_called = true;
17714
17715   the_parser = cp_parser_new ();
17716   push_deferring_access_checks (flag_access_control
17717                                 ? dk_no_deferred : dk_no_check);
17718   error_occurred = cp_parser_translation_unit (the_parser);
17719   the_parser = NULL;
17720 }
17721
17722 /* This variable must be provided by every front end.  */
17723
17724 int yydebug;
17725
17726 #include "gt-cp-parser.h"