OSDN Git Service

* call.c: Fix comment typo(s).
[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 "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A C++ token.  */
48
49 typedef struct cp_token GTY (())
50 {
51   /* The kind of token.  */
52   ENUM_BITFIELD (cpp_ttype) type : 8;
53   /* If this token is a keyword, this value indicates which keyword.
54      Otherwise, this value is RID_MAX.  */
55   ENUM_BITFIELD (rid) keyword : 8;
56   /* Token flags.  */
57   unsigned char flags;
58   /* True if this token is from a system header.  */
59   BOOL_BITFIELD in_system_header : 1;
60   /* True if this token is from a context where it is implicitly extern "C" */
61   BOOL_BITFIELD implicit_extern_c : 1;
62   /* The value associated with this token, if any.  */
63   tree value;
64   /* The location at which this token was found.  */
65   location_t location;
66 } cp_token;
67
68 /* We use a stack of token pointer for saving token sets.  */
69 typedef struct cp_token *cp_token_position;
70 DEF_VEC_P (cp_token_position);
71 DEF_VEC_ALLOC_P (cp_token_position,heap);
72
73 static const cp_token eof_token =
74 {
75   CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
76 #if USE_MAPPED_LOCATION
77   0
78 #else
79   {0, 0}
80 #endif
81 };
82
83 /* The cp_lexer structure represents the C++ lexer.  It is responsible
84    for managing the token stream from the preprocessor and supplying
85    it to the parser.  Tokens are never added to the cp_lexer after
86    it is created.  */
87
88 typedef struct cp_lexer GTY (())
89 {
90   /* The memory allocated for the buffer.  NULL if this lexer does not
91      own the token buffer.  */
92   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
93   /* If the lexer owns the buffer, this is the number of tokens in the
94      buffer.  */
95   size_t buffer_length;
96
97   /* A pointer just past the last available token.  The tokens
98      in this lexer are [buffer, last_token).  */
99   cp_token_position GTY ((skip)) last_token;
100
101   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
102      no more available tokens.  */
103   cp_token_position GTY ((skip)) next_token;
104
105   /* A stack indicating positions at which cp_lexer_save_tokens was
106      called.  The top entry is the most recent position at which we
107      began saving tokens.  If the stack is non-empty, we are saving
108      tokens.  */
109   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
110
111   /* True if we should output debugging information.  */
112   bool debugging_p;
113
114   /* The next lexer in a linked list of lexers.  */
115   struct cp_lexer *next;
116 } cp_lexer;
117
118 /* cp_token_cache is a range of tokens.  There is no need to represent
119    allocate heap memory for it, since tokens are never removed from the
120    lexer's array.  There is also no need for the GC to walk through
121    a cp_token_cache, since everything in here is referenced through
122    a lexer.  */
123
124 typedef struct cp_token_cache GTY(())
125 {
126   /* The beginning of the token range.  */
127   cp_token * GTY((skip)) first;
128
129   /* Points immediately after the last token in the range.  */
130   cp_token * GTY ((skip)) last;
131 } cp_token_cache;
132
133 /* Prototypes.  */
134
135 static cp_lexer *cp_lexer_new_main
136   (void);
137 static cp_lexer *cp_lexer_new_from_tokens
138   (cp_token_cache *tokens);
139 static void cp_lexer_destroy
140   (cp_lexer *);
141 static int cp_lexer_saving_tokens
142   (const cp_lexer *);
143 static cp_token_position cp_lexer_token_position
144   (cp_lexer *, bool);
145 static cp_token *cp_lexer_token_at
146   (cp_lexer *, cp_token_position);
147 static void cp_lexer_get_preprocessor_token
148   (cp_lexer *, cp_token *);
149 static inline cp_token *cp_lexer_peek_token
150   (cp_lexer *);
151 static cp_token *cp_lexer_peek_nth_token
152   (cp_lexer *, size_t);
153 static inline bool cp_lexer_next_token_is
154   (cp_lexer *, enum cpp_ttype);
155 static bool cp_lexer_next_token_is_not
156   (cp_lexer *, enum cpp_ttype);
157 static bool cp_lexer_next_token_is_keyword
158   (cp_lexer *, enum rid);
159 static cp_token *cp_lexer_consume_token
160   (cp_lexer *);
161 static void cp_lexer_purge_token
162   (cp_lexer *);
163 static void cp_lexer_purge_tokens_after
164   (cp_lexer *, cp_token_position);
165 static void cp_lexer_handle_pragma
166   (cp_lexer *);
167 static void cp_lexer_save_tokens
168   (cp_lexer *);
169 static void cp_lexer_commit_tokens
170   (cp_lexer *);
171 static void cp_lexer_rollback_tokens
172   (cp_lexer *);
173 #ifdef ENABLE_CHECKING
174 static void cp_lexer_print_token
175   (FILE *, cp_token *);
176 static inline bool cp_lexer_debugging_p
177   (cp_lexer *);
178 static void cp_lexer_start_debugging
179   (cp_lexer *) ATTRIBUTE_UNUSED;
180 static void cp_lexer_stop_debugging
181   (cp_lexer *) ATTRIBUTE_UNUSED;
182 #else
183 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
184    about passing NULL to functions that require non-NULL arguments
185    (fputs, fprintf).  It will never be used, so all we need is a value
186    of the right type that's guaranteed not to be NULL.  */
187 #define cp_lexer_debug_stream stdout
188 #define cp_lexer_print_token(str, tok) (void) 0
189 #define cp_lexer_debugging_p(lexer) 0
190 #endif /* ENABLE_CHECKING */
191
192 static cp_token_cache *cp_token_cache_new
193   (cp_token *, cp_token *);
194
195 /* Manifest constants.  */
196 #define CP_LEXER_BUFFER_SIZE 10000
197 #define CP_SAVED_TOKEN_STACK 5
198
199 /* A token type for keywords, as opposed to ordinary identifiers.  */
200 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
201
202 /* A token type for template-ids.  If a template-id is processed while
203    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
204    the value of the CPP_TEMPLATE_ID is whatever was returned by
205    cp_parser_template_id.  */
206 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
207
208 /* A token type for nested-name-specifiers.  If a
209    nested-name-specifier is processed while parsing tentatively, it is
210    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
211    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
212    cp_parser_nested_name_specifier_opt.  */
213 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
214
215 /* A token type for tokens that are not tokens at all; these are used
216    to represent slots in the array where there used to be a token
217    that has now been deleted.  */
218 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
219
220 /* The number of token types, including C++-specific ones.  */
221 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
222
223 /* Variables.  */
224
225 #ifdef ENABLE_CHECKING
226 /* The stream to which debugging output should be written.  */
227 static FILE *cp_lexer_debug_stream;
228 #endif /* ENABLE_CHECKING */
229
230 /* Create a new main C++ lexer, the lexer that gets tokens from the
231    preprocessor.  */
232
233 static cp_lexer *
234 cp_lexer_new_main (void)
235 {
236   cp_token first_token;
237   cp_lexer *lexer;
238   cp_token *pos;
239   size_t alloc;
240   size_t space;
241   cp_token *buffer;
242
243   /* It's possible that lexing the first token will load a PCH file,
244      which is a GC collection point.  So we have to grab the first
245      token before allocating any memory.  Pragmas must not be deferred
246      as -fpch-preprocess can generate a pragma to load the PCH file in
247      the preprocessed output used by -save-temps.  */
248   cp_lexer_get_preprocessor_token (NULL, &first_token);
249
250   /* Tell cpplib we want CPP_PRAGMA tokens.  */
251   cpp_get_options (parse_in)->defer_pragmas = true;
252
253   /* Tell c_lex not to merge string constants.  */
254   c_lex_return_raw_strings = true;
255
256   c_common_no_more_pch ();
257
258   /* Allocate the memory.  */
259   lexer = GGC_CNEW (cp_lexer);
260
261 #ifdef ENABLE_CHECKING
262   /* Initially we are not debugging.  */
263   lexer->debugging_p = false;
264 #endif /* ENABLE_CHECKING */
265   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
266                                    CP_SAVED_TOKEN_STACK);
267
268   /* Create the buffer.  */
269   alloc = CP_LEXER_BUFFER_SIZE;
270   buffer = ggc_alloc (alloc * sizeof (cp_token));
271
272   /* Put the first token in the buffer.  */
273   space = alloc;
274   pos = buffer;
275   *pos = first_token;
276
277   /* Get the remaining tokens from the preprocessor.  */
278   while (pos->type != CPP_EOF)
279     {
280       pos++;
281       if (!--space)
282         {
283           space = alloc;
284           alloc *= 2;
285           buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
286           pos = buffer + space;
287         }
288       cp_lexer_get_preprocessor_token (lexer, pos);
289     }
290   lexer->buffer = buffer;
291   lexer->buffer_length = alloc - space;
292   lexer->last_token = pos;
293   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
294
295   /* Pragma processing (via cpp_handle_deferred_pragma) may result in
296      direct calls to c_lex.  Those callers all expect c_lex to do
297      string constant concatenation.  */
298   c_lex_return_raw_strings = false;
299
300   gcc_assert (lexer->next_token->type != CPP_PURGED);
301   return lexer;
302 }
303
304 /* Create a new lexer whose token stream is primed with the tokens in
305    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
306
307 static cp_lexer *
308 cp_lexer_new_from_tokens (cp_token_cache *cache)
309 {
310   cp_token *first = cache->first;
311   cp_token *last = cache->last;
312   cp_lexer *lexer = GGC_CNEW (cp_lexer);
313
314   /* We do not own the buffer.  */
315   lexer->buffer = NULL;
316   lexer->buffer_length = 0;
317   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
318   lexer->last_token = last;
319
320   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
321                                    CP_SAVED_TOKEN_STACK);
322
323 #ifdef ENABLE_CHECKING
324   /* Initially we are not debugging.  */
325   lexer->debugging_p = false;
326 #endif
327
328   gcc_assert (lexer->next_token->type != CPP_PURGED);
329   return lexer;
330 }
331
332 /* Frees all resources associated with LEXER.  */
333
334 static void
335 cp_lexer_destroy (cp_lexer *lexer)
336 {
337   if (lexer->buffer)
338     ggc_free (lexer->buffer);
339   VEC_free (cp_token_position, heap, lexer->saved_tokens);
340   ggc_free (lexer);
341 }
342
343 /* Returns nonzero if debugging information should be output.  */
344
345 #ifdef ENABLE_CHECKING
346
347 static inline bool
348 cp_lexer_debugging_p (cp_lexer *lexer)
349 {
350   return lexer->debugging_p;
351 }
352
353 #endif /* ENABLE_CHECKING */
354
355 static inline cp_token_position
356 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
357 {
358   gcc_assert (!previous_p || lexer->next_token != &eof_token);
359
360   return lexer->next_token - previous_p;
361 }
362
363 static inline cp_token *
364 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
365 {
366   return pos;
367 }
368
369 /* nonzero if we are presently saving tokens.  */
370
371 static inline int
372 cp_lexer_saving_tokens (const cp_lexer* lexer)
373 {
374   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
375 }
376
377 /* Store the next token from the preprocessor in *TOKEN.  Return true
378    if we reach EOF.  */
379
380 static void
381 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
382                                  cp_token *token)
383 {
384   static int is_extern_c = 0;
385
386    /* Get a new token from the preprocessor.  */
387   token->type
388     = c_lex_with_flags (&token->value, &token->location, &token->flags);
389   token->in_system_header = in_system_header;
390
391   /* On some systems, some header files are surrounded by an
392      implicit extern "C" block.  Set a flag in the token if it
393      comes from such a header.  */
394   is_extern_c += pending_lang_change;
395   pending_lang_change = 0;
396   token->implicit_extern_c = is_extern_c > 0;
397
398   /* Check to see if this token is a keyword.  */
399   if (token->type == CPP_NAME
400       && C_IS_RESERVED_WORD (token->value))
401     {
402       /* Mark this token as a keyword.  */
403       token->type = CPP_KEYWORD;
404       /* Record which keyword.  */
405       token->keyword = C_RID_CODE (token->value);
406       /* Update the value.  Some keywords are mapped to particular
407          entities, rather than simply having the value of the
408          corresponding IDENTIFIER_NODE.  For example, `__const' is
409          mapped to `const'.  */
410       token->value = ridpointers[token->keyword];
411     }
412   /* Handle Objective-C++ keywords.  */
413   else if (token->type == CPP_AT_NAME)
414     {
415       token->type = CPP_KEYWORD;
416       switch (C_RID_CODE (token->value))
417         {
418         /* Map 'class' to '@class', 'private' to '@private', etc.  */
419         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
420         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
421         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
422         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
423         case RID_THROW: token->keyword = RID_AT_THROW; break;
424         case RID_TRY: token->keyword = RID_AT_TRY; break;
425         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
426         default: token->keyword = C_RID_CODE (token->value);
427         }
428     }
429   else
430     token->keyword = RID_MAX;
431 }
432
433 /* Update the globals input_location and in_system_header from TOKEN.  */
434 static inline void
435 cp_lexer_set_source_position_from_token (cp_token *token)
436 {
437   if (token->type != CPP_EOF)
438     {
439       input_location = token->location;
440       in_system_header = token->in_system_header;
441     }
442 }
443
444 /* Return a pointer to the next token in the token stream, but do not
445    consume it.  */
446
447 static inline cp_token *
448 cp_lexer_peek_token (cp_lexer *lexer)
449 {
450   if (cp_lexer_debugging_p (lexer))
451     {
452       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
453       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
454       putc ('\n', cp_lexer_debug_stream);
455     }
456   return lexer->next_token;
457 }
458
459 /* Return true if the next token has the indicated TYPE.  */
460
461 static inline bool
462 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
463 {
464   return cp_lexer_peek_token (lexer)->type == type;
465 }
466
467 /* Return true if the next token does not have the indicated TYPE.  */
468
469 static inline bool
470 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
471 {
472   return !cp_lexer_next_token_is (lexer, type);
473 }
474
475 /* Return true if the next token is the indicated KEYWORD.  */
476
477 static inline bool
478 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
479 {
480   cp_token *token;
481
482   /* Peek at the next token.  */
483   token = cp_lexer_peek_token (lexer);
484   /* Check to see if it is the indicated keyword.  */
485   return token->keyword == keyword;
486 }
487
488 /* Return a pointer to the Nth token in the token stream.  If N is 1,
489    then this is precisely equivalent to cp_lexer_peek_token (except
490    that it is not inline).  One would like to disallow that case, but
491    there is one case (cp_parser_nth_token_starts_template_id) where
492    the caller passes a variable for N and it might be 1.  */
493
494 static cp_token *
495 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
496 {
497   cp_token *token;
498
499   /* N is 1-based, not zero-based.  */
500   gcc_assert (n > 0 && lexer->next_token != &eof_token);
501
502   if (cp_lexer_debugging_p (lexer))
503     fprintf (cp_lexer_debug_stream,
504              "cp_lexer: peeking ahead %ld at token: ", (long)n);
505
506   --n;
507   token = lexer->next_token;
508   while (n != 0)
509     {
510       ++token;
511       if (token == lexer->last_token)
512         {
513           token = (cp_token *)&eof_token;
514           break;
515         }
516
517       if (token->type != CPP_PURGED)
518         --n;
519     }
520
521   if (cp_lexer_debugging_p (lexer))
522     {
523       cp_lexer_print_token (cp_lexer_debug_stream, token);
524       putc ('\n', cp_lexer_debug_stream);
525     }
526
527   return token;
528 }
529
530 /* Return the next token, and advance the lexer's next_token pointer
531    to point to the next non-purged token.  */
532
533 static cp_token *
534 cp_lexer_consume_token (cp_lexer* lexer)
535 {
536   cp_token *token = lexer->next_token;
537
538   gcc_assert (token != &eof_token);
539
540   do
541     {
542       lexer->next_token++;
543       if (lexer->next_token == lexer->last_token)
544         {
545           lexer->next_token = (cp_token *)&eof_token;
546           break;
547         }
548
549     }
550   while (lexer->next_token->type == CPP_PURGED);
551
552   cp_lexer_set_source_position_from_token (token);
553
554   /* Provide debugging output.  */
555   if (cp_lexer_debugging_p (lexer))
556     {
557       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
558       cp_lexer_print_token (cp_lexer_debug_stream, token);
559       putc ('\n', cp_lexer_debug_stream);
560     }
561
562   return token;
563 }
564
565 /* Permanently remove the next token from the token stream, and
566    advance the next_token pointer to refer to the next non-purged
567    token.  */
568
569 static void
570 cp_lexer_purge_token (cp_lexer *lexer)
571 {
572   cp_token *tok = lexer->next_token;
573
574   gcc_assert (tok != &eof_token);
575   tok->type = CPP_PURGED;
576   tok->location = UNKNOWN_LOCATION;
577   tok->value = NULL_TREE;
578   tok->keyword = RID_MAX;
579
580   do
581     {
582       tok++;
583       if (tok == lexer->last_token)
584         {
585           tok = (cp_token *)&eof_token;
586           break;
587         }
588     }
589   while (tok->type == CPP_PURGED);
590   lexer->next_token = tok;
591 }
592
593 /* Permanently remove all tokens after TOK, up to, but not
594    including, the token that will be returned next by
595    cp_lexer_peek_token.  */
596
597 static void
598 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
599 {
600   cp_token *peek = lexer->next_token;
601
602   if (peek == &eof_token)
603     peek = lexer->last_token;
604
605   gcc_assert (tok < peek);
606
607   for ( tok += 1; tok != peek; tok += 1)
608     {
609       tok->type = CPP_PURGED;
610       tok->location = UNKNOWN_LOCATION;
611       tok->value = NULL_TREE;
612       tok->keyword = RID_MAX;
613     }
614 }
615
616 /* Consume and handle a pragma token.  */
617 static void
618 cp_lexer_handle_pragma (cp_lexer *lexer)
619 {
620   cpp_string s;
621   cp_token *token = cp_lexer_consume_token (lexer);
622   gcc_assert (token->type == CPP_PRAGMA);
623   gcc_assert (token->value);
624
625   s.len = TREE_STRING_LENGTH (token->value);
626   s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
627
628   cpp_handle_deferred_pragma (parse_in, &s);
629
630   /* Clearing token->value here means that we will get an ICE if we
631      try to process this #pragma again (which should be impossible).  */
632   token->value = NULL;
633 }
634
635 /* Begin saving tokens.  All tokens consumed after this point will be
636    preserved.  */
637
638 static void
639 cp_lexer_save_tokens (cp_lexer* lexer)
640 {
641   /* Provide debugging output.  */
642   if (cp_lexer_debugging_p (lexer))
643     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
644
645   VEC_safe_push (cp_token_position, heap,
646                  lexer->saved_tokens, lexer->next_token);
647 }
648
649 /* Commit to the portion of the token stream most recently saved.  */
650
651 static void
652 cp_lexer_commit_tokens (cp_lexer* lexer)
653 {
654   /* Provide debugging output.  */
655   if (cp_lexer_debugging_p (lexer))
656     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
657
658   VEC_pop (cp_token_position, lexer->saved_tokens);
659 }
660
661 /* Return all tokens saved since the last call to cp_lexer_save_tokens
662    to the token stream.  Stop saving tokens.  */
663
664 static void
665 cp_lexer_rollback_tokens (cp_lexer* lexer)
666 {
667   /* Provide debugging output.  */
668   if (cp_lexer_debugging_p (lexer))
669     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
670
671   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
672 }
673
674 /* Print a representation of the TOKEN on the STREAM.  */
675
676 #ifdef ENABLE_CHECKING
677
678 static void
679 cp_lexer_print_token (FILE * stream, cp_token *token)
680 {
681   /* We don't use cpp_type2name here because the parser defines
682      a few tokens of its own.  */
683   static const char *const token_names[] = {
684     /* cpplib-defined token types */
685 #define OP(e, s) #e,
686 #define TK(e, s) #e,
687     TTYPE_TABLE
688 #undef OP
689 #undef TK
690     /* C++ parser token types - see "Manifest constants", above.  */
691     "KEYWORD",
692     "TEMPLATE_ID",
693     "NESTED_NAME_SPECIFIER",
694     "PURGED"
695   };
696
697   /* If we have a name for the token, print it out.  Otherwise, we
698      simply give the numeric code.  */
699   gcc_assert (token->type < ARRAY_SIZE(token_names));
700   fputs (token_names[token->type], stream);
701
702   /* For some tokens, print the associated data.  */
703   switch (token->type)
704     {
705     case CPP_KEYWORD:
706       /* Some keywords have a value that is not an IDENTIFIER_NODE.
707          For example, `struct' is mapped to an INTEGER_CST.  */
708       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
709         break;
710       /* else fall through */
711     case CPP_NAME:
712       fputs (IDENTIFIER_POINTER (token->value), stream);
713       break;
714
715     case CPP_STRING:
716     case CPP_WSTRING:
717     case CPP_PRAGMA:
718       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
719       break;
720
721     default:
722       break;
723     }
724 }
725
726 /* Start emitting debugging information.  */
727
728 static void
729 cp_lexer_start_debugging (cp_lexer* lexer)
730 {
731   lexer->debugging_p = true;
732 }
733
734 /* Stop emitting debugging information.  */
735
736 static void
737 cp_lexer_stop_debugging (cp_lexer* lexer)
738 {
739   lexer->debugging_p = false;
740 }
741
742 #endif /* ENABLE_CHECKING */
743
744 /* Create a new cp_token_cache, representing a range of tokens.  */
745
746 static cp_token_cache *
747 cp_token_cache_new (cp_token *first, cp_token *last)
748 {
749   cp_token_cache *cache = GGC_NEW (cp_token_cache);
750   cache->first = first;
751   cache->last = last;
752   return cache;
753 }
754
755 \f
756 /* Decl-specifiers.  */
757
758 static void clear_decl_specs
759   (cp_decl_specifier_seq *);
760
761 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
762
763 static void
764 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
765 {
766   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
767 }
768
769 /* Declarators.  */
770
771 /* Nothing other than the parser should be creating declarators;
772    declarators are a semi-syntactic representation of C++ entities.
773    Other parts of the front end that need to create entities (like
774    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
775
776 static cp_declarator *make_call_declarator
777   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
778 static cp_declarator *make_array_declarator
779   (cp_declarator *, tree);
780 static cp_declarator *make_pointer_declarator
781   (cp_cv_quals, cp_declarator *);
782 static cp_declarator *make_reference_declarator
783   (cp_cv_quals, cp_declarator *);
784 static cp_parameter_declarator *make_parameter_declarator
785   (cp_decl_specifier_seq *, cp_declarator *, tree);
786 static cp_declarator *make_ptrmem_declarator
787   (cp_cv_quals, tree, cp_declarator *);
788
789 cp_declarator *cp_error_declarator;
790
791 /* The obstack on which declarators and related data structures are
792    allocated.  */
793 static struct obstack declarator_obstack;
794
795 /* Alloc BYTES from the declarator memory pool.  */
796
797 static inline void *
798 alloc_declarator (size_t bytes)
799 {
800   return obstack_alloc (&declarator_obstack, bytes);
801 }
802
803 /* Allocate a declarator of the indicated KIND.  Clear fields that are
804    common to all declarators.  */
805
806 static cp_declarator *
807 make_declarator (cp_declarator_kind kind)
808 {
809   cp_declarator *declarator;
810
811   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
812   declarator->kind = kind;
813   declarator->attributes = NULL_TREE;
814   declarator->declarator = NULL;
815
816   return declarator;
817 }
818
819 /* Make a declarator for a generalized identifier.  If non-NULL, the
820    identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
821    just UNQUALIFIED_NAME.  */
822
823 static cp_declarator *
824 make_id_declarator (tree qualifying_scope, tree unqualified_name)
825 {
826   cp_declarator *declarator;
827
828   /* It is valid to write:
829
830        class C { void f(); };
831        typedef C D;
832        void D::f();
833
834      The standard is not clear about whether `typedef const C D' is
835      legal; as of 2002-09-15 the committee is considering that
836      question.  EDG 3.0 allows that syntax.  Therefore, we do as
837      well.  */
838   if (qualifying_scope && TYPE_P (qualifying_scope))
839     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
840
841   declarator = make_declarator (cdk_id);
842   declarator->u.id.qualifying_scope = qualifying_scope;
843   declarator->u.id.unqualified_name = unqualified_name;
844   declarator->u.id.sfk = sfk_none;
845
846   return declarator;
847 }
848
849 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
850    of modifiers such as const or volatile to apply to the pointer
851    type, represented as identifiers.  */
852
853 cp_declarator *
854 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
855 {
856   cp_declarator *declarator;
857
858   declarator = make_declarator (cdk_pointer);
859   declarator->declarator = target;
860   declarator->u.pointer.qualifiers = cv_qualifiers;
861   declarator->u.pointer.class_type = NULL_TREE;
862
863   return declarator;
864 }
865
866 /* Like make_pointer_declarator -- but for references.  */
867
868 cp_declarator *
869 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
870 {
871   cp_declarator *declarator;
872
873   declarator = make_declarator (cdk_reference);
874   declarator->declarator = target;
875   declarator->u.pointer.qualifiers = cv_qualifiers;
876   declarator->u.pointer.class_type = NULL_TREE;
877
878   return declarator;
879 }
880
881 /* Like make_pointer_declarator -- but for a pointer to a non-static
882    member of CLASS_TYPE.  */
883
884 cp_declarator *
885 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
886                         cp_declarator *pointee)
887 {
888   cp_declarator *declarator;
889
890   declarator = make_declarator (cdk_ptrmem);
891   declarator->declarator = pointee;
892   declarator->u.pointer.qualifiers = cv_qualifiers;
893   declarator->u.pointer.class_type = class_type;
894
895   return declarator;
896 }
897
898 /* Make a declarator for the function given by TARGET, with the
899    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
900    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
901    indicates what exceptions can be thrown.  */
902
903 cp_declarator *
904 make_call_declarator (cp_declarator *target,
905                       cp_parameter_declarator *parms,
906                       cp_cv_quals cv_qualifiers,
907                       tree exception_specification)
908 {
909   cp_declarator *declarator;
910
911   declarator = make_declarator (cdk_function);
912   declarator->declarator = target;
913   declarator->u.function.parameters = parms;
914   declarator->u.function.qualifiers = cv_qualifiers;
915   declarator->u.function.exception_specification = exception_specification;
916
917   return declarator;
918 }
919
920 /* Make a declarator for an array of BOUNDS elements, each of which is
921    defined by ELEMENT.  */
922
923 cp_declarator *
924 make_array_declarator (cp_declarator *element, tree bounds)
925 {
926   cp_declarator *declarator;
927
928   declarator = make_declarator (cdk_array);
929   declarator->declarator = element;
930   declarator->u.array.bounds = bounds;
931
932   return declarator;
933 }
934
935 cp_parameter_declarator *no_parameters;
936
937 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
938    DECLARATOR and DEFAULT_ARGUMENT.  */
939
940 cp_parameter_declarator *
941 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
942                            cp_declarator *declarator,
943                            tree default_argument)
944 {
945   cp_parameter_declarator *parameter;
946
947   parameter = ((cp_parameter_declarator *)
948                alloc_declarator (sizeof (cp_parameter_declarator)));
949   parameter->next = NULL;
950   if (decl_specifiers)
951     parameter->decl_specifiers = *decl_specifiers;
952   else
953     clear_decl_specs (&parameter->decl_specifiers);
954   parameter->declarator = declarator;
955   parameter->default_argument = default_argument;
956   parameter->ellipsis_p = false;
957
958   return parameter;
959 }
960
961 /* The parser.  */
962
963 /* Overview
964    --------
965
966    A cp_parser parses the token stream as specified by the C++
967    grammar.  Its job is purely parsing, not semantic analysis.  For
968    example, the parser breaks the token stream into declarators,
969    expressions, statements, and other similar syntactic constructs.
970    It does not check that the types of the expressions on either side
971    of an assignment-statement are compatible, or that a function is
972    not declared with a parameter of type `void'.
973
974    The parser invokes routines elsewhere in the compiler to perform
975    semantic analysis and to build up the abstract syntax tree for the
976    code processed.
977
978    The parser (and the template instantiation code, which is, in a
979    way, a close relative of parsing) are the only parts of the
980    compiler that should be calling push_scope and pop_scope, or
981    related functions.  The parser (and template instantiation code)
982    keeps track of what scope is presently active; everything else
983    should simply honor that.  (The code that generates static
984    initializers may also need to set the scope, in order to check
985    access control correctly when emitting the initializers.)
986
987    Methodology
988    -----------
989
990    The parser is of the standard recursive-descent variety.  Upcoming
991    tokens in the token stream are examined in order to determine which
992    production to use when parsing a non-terminal.  Some C++ constructs
993    require arbitrary look ahead to disambiguate.  For example, it is
994    impossible, in the general case, to tell whether a statement is an
995    expression or declaration without scanning the entire statement.
996    Therefore, the parser is capable of "parsing tentatively."  When the
997    parser is not sure what construct comes next, it enters this mode.
998    Then, while we attempt to parse the construct, the parser queues up
999    error messages, rather than issuing them immediately, and saves the
1000    tokens it consumes.  If the construct is parsed successfully, the
1001    parser "commits", i.e., it issues any queued error messages and
1002    the tokens that were being preserved are permanently discarded.
1003    If, however, the construct is not parsed successfully, the parser
1004    rolls back its state completely so that it can resume parsing using
1005    a different alternative.
1006
1007    Future Improvements
1008    -------------------
1009
1010    The performance of the parser could probably be improved substantially.
1011    We could often eliminate the need to parse tentatively by looking ahead
1012    a little bit.  In some places, this approach might not entirely eliminate
1013    the need to parse tentatively, but it might still speed up the average
1014    case.  */
1015
1016 /* Flags that are passed to some parsing functions.  These values can
1017    be bitwise-ored together.  */
1018
1019 typedef enum cp_parser_flags
1020 {
1021   /* No flags.  */
1022   CP_PARSER_FLAGS_NONE = 0x0,
1023   /* The construct is optional.  If it is not present, then no error
1024      should be issued.  */
1025   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1026   /* When parsing a type-specifier, do not allow user-defined types.  */
1027   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1028 } cp_parser_flags;
1029
1030 /* The different kinds of declarators we want to parse.  */
1031
1032 typedef enum cp_parser_declarator_kind
1033 {
1034   /* We want an abstract declarator.  */
1035   CP_PARSER_DECLARATOR_ABSTRACT,
1036   /* We want a named declarator.  */
1037   CP_PARSER_DECLARATOR_NAMED,
1038   /* We don't mind, but the name must be an unqualified-id.  */
1039   CP_PARSER_DECLARATOR_EITHER
1040 } cp_parser_declarator_kind;
1041
1042 /* The precedence values used to parse binary expressions.  The minimum value
1043    of PREC must be 1, because zero is reserved to quickly discriminate
1044    binary operators from other tokens.  */
1045
1046 enum cp_parser_prec
1047 {
1048   PREC_NOT_OPERATOR,
1049   PREC_LOGICAL_OR_EXPRESSION,
1050   PREC_LOGICAL_AND_EXPRESSION,
1051   PREC_INCLUSIVE_OR_EXPRESSION,
1052   PREC_EXCLUSIVE_OR_EXPRESSION,
1053   PREC_AND_EXPRESSION,
1054   PREC_EQUALITY_EXPRESSION,
1055   PREC_RELATIONAL_EXPRESSION,
1056   PREC_SHIFT_EXPRESSION,
1057   PREC_ADDITIVE_EXPRESSION,
1058   PREC_MULTIPLICATIVE_EXPRESSION,
1059   PREC_PM_EXPRESSION,
1060   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1061 };
1062
1063 /* A mapping from a token type to a corresponding tree node type, with a
1064    precedence value.  */
1065
1066 typedef struct cp_parser_binary_operations_map_node
1067 {
1068   /* The token type.  */
1069   enum cpp_ttype token_type;
1070   /* The corresponding tree code.  */
1071   enum tree_code tree_type;
1072   /* The precedence of this operator.  */
1073   enum cp_parser_prec prec;
1074 } cp_parser_binary_operations_map_node;
1075
1076 /* The status of a tentative parse.  */
1077
1078 typedef enum cp_parser_status_kind
1079 {
1080   /* No errors have occurred.  */
1081   CP_PARSER_STATUS_KIND_NO_ERROR,
1082   /* An error has occurred.  */
1083   CP_PARSER_STATUS_KIND_ERROR,
1084   /* We are committed to this tentative parse, whether or not an error
1085      has occurred.  */
1086   CP_PARSER_STATUS_KIND_COMMITTED
1087 } cp_parser_status_kind;
1088
1089 typedef struct cp_parser_expression_stack_entry
1090 {
1091   tree lhs;
1092   enum tree_code tree_type;
1093   int prec;
1094 } cp_parser_expression_stack_entry;
1095
1096 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1097    entries because precedence levels on the stack are monotonically
1098    increasing.  */
1099 typedef struct cp_parser_expression_stack_entry
1100   cp_parser_expression_stack[NUM_PREC_VALUES];
1101
1102 /* Context that is saved and restored when parsing tentatively.  */
1103 typedef struct cp_parser_context GTY (())
1104 {
1105   /* If this is a tentative parsing context, the status of the
1106      tentative parse.  */
1107   enum cp_parser_status_kind status;
1108   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1109      that are looked up in this context must be looked up both in the
1110      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1111      the context of the containing expression.  */
1112   tree object_type;
1113
1114   /* The next parsing context in the stack.  */
1115   struct cp_parser_context *next;
1116 } cp_parser_context;
1117
1118 /* Prototypes.  */
1119
1120 /* Constructors and destructors.  */
1121
1122 static cp_parser_context *cp_parser_context_new
1123   (cp_parser_context *);
1124
1125 /* Class variables.  */
1126
1127 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1128
1129 /* The operator-precedence table used by cp_parser_binary_expression.
1130    Transformed into an associative array (binops_by_token) by
1131    cp_parser_new.  */
1132
1133 static const cp_parser_binary_operations_map_node binops[] = {
1134   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1135   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1136
1137   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1138   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1139   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1140
1141   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1142   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1143
1144   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1145   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1146
1147   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1148   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1149   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1150   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1151   { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1152   { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1153
1154   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1155   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1156
1157   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1158
1159   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1160
1161   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1162
1163   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1164
1165   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1166 };
1167
1168 /* The same as binops, but initialized by cp_parser_new so that
1169    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1170    for speed.  */
1171 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1172
1173 /* Constructors and destructors.  */
1174
1175 /* Construct a new context.  The context below this one on the stack
1176    is given by NEXT.  */
1177
1178 static cp_parser_context *
1179 cp_parser_context_new (cp_parser_context* next)
1180 {
1181   cp_parser_context *context;
1182
1183   /* Allocate the storage.  */
1184   if (cp_parser_context_free_list != NULL)
1185     {
1186       /* Pull the first entry from the free list.  */
1187       context = cp_parser_context_free_list;
1188       cp_parser_context_free_list = context->next;
1189       memset (context, 0, sizeof (*context));
1190     }
1191   else
1192     context = GGC_CNEW (cp_parser_context);
1193
1194   /* No errors have occurred yet in this context.  */
1195   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1196   /* If this is not the bottomost context, copy information that we
1197      need from the previous context.  */
1198   if (next)
1199     {
1200       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1201          expression, then we are parsing one in this context, too.  */
1202       context->object_type = next->object_type;
1203       /* Thread the stack.  */
1204       context->next = next;
1205     }
1206
1207   return context;
1208 }
1209
1210 /* The cp_parser structure represents the C++ parser.  */
1211
1212 typedef struct cp_parser GTY(())
1213 {
1214   /* The lexer from which we are obtaining tokens.  */
1215   cp_lexer *lexer;
1216
1217   /* The scope in which names should be looked up.  If NULL_TREE, then
1218      we look up names in the scope that is currently open in the
1219      source program.  If non-NULL, this is either a TYPE or
1220      NAMESPACE_DECL for the scope in which we should look.  It can
1221      also be ERROR_MARK, when we've parsed a bogus scope.
1222
1223      This value is not cleared automatically after a name is looked
1224      up, so we must be careful to clear it before starting a new look
1225      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1226      will look up `Z' in the scope of `X', rather than the current
1227      scope.)  Unfortunately, it is difficult to tell when name lookup
1228      is complete, because we sometimes peek at a token, look it up,
1229      and then decide not to consume it.   */
1230   tree scope;
1231
1232   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1233      last lookup took place.  OBJECT_SCOPE is used if an expression
1234      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1235      respectively.  QUALIFYING_SCOPE is used for an expression of the
1236      form "X::Y"; it refers to X.  */
1237   tree object_scope;
1238   tree qualifying_scope;
1239
1240   /* A stack of parsing contexts.  All but the bottom entry on the
1241      stack will be tentative contexts.
1242
1243      We parse tentatively in order to determine which construct is in
1244      use in some situations.  For example, in order to determine
1245      whether a statement is an expression-statement or a
1246      declaration-statement we parse it tentatively as a
1247      declaration-statement.  If that fails, we then reparse the same
1248      token stream as an expression-statement.  */
1249   cp_parser_context *context;
1250
1251   /* True if we are parsing GNU C++.  If this flag is not set, then
1252      GNU extensions are not recognized.  */
1253   bool allow_gnu_extensions_p;
1254
1255   /* TRUE if the `>' token should be interpreted as the greater-than
1256      operator.  FALSE if it is the end of a template-id or
1257      template-parameter-list.  */
1258   bool greater_than_is_operator_p;
1259
1260   /* TRUE if default arguments are allowed within a parameter list
1261      that starts at this point. FALSE if only a gnu extension makes
1262      them permissible.  */
1263   bool default_arg_ok_p;
1264
1265   /* TRUE if we are parsing an integral constant-expression.  See
1266      [expr.const] for a precise definition.  */
1267   bool integral_constant_expression_p;
1268
1269   /* TRUE if we are parsing an integral constant-expression -- but a
1270      non-constant expression should be permitted as well.  This flag
1271      is used when parsing an array bound so that GNU variable-length
1272      arrays are tolerated.  */
1273   bool allow_non_integral_constant_expression_p;
1274
1275   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1276      been seen that makes the expression non-constant.  */
1277   bool non_integral_constant_expression_p;
1278
1279   /* TRUE if local variable names and `this' are forbidden in the
1280      current context.  */
1281   bool local_variables_forbidden_p;
1282
1283   /* TRUE if the declaration we are parsing is part of a
1284      linkage-specification of the form `extern string-literal
1285      declaration'.  */
1286   bool in_unbraced_linkage_specification_p;
1287
1288   /* TRUE if we are presently parsing a declarator, after the
1289      direct-declarator.  */
1290   bool in_declarator_p;
1291
1292   /* TRUE if we are presently parsing a template-argument-list.  */
1293   bool in_template_argument_list_p;
1294
1295   /* TRUE if we are presently parsing the body of an
1296      iteration-statement.  */
1297   bool in_iteration_statement_p;
1298
1299   /* TRUE if we are presently parsing the body of a switch
1300      statement.  */
1301   bool in_switch_statement_p;
1302
1303   /* TRUE if we are parsing a type-id in an expression context.  In
1304      such a situation, both "type (expr)" and "type (type)" are valid
1305      alternatives.  */
1306   bool in_type_id_in_expr_p;
1307
1308   /* TRUE if we are currently in a header file where declarations are
1309      implicitly extern "C".  */
1310   bool implicit_extern_c;
1311
1312   /* TRUE if strings in expressions should be translated to the execution
1313      character set.  */
1314   bool translate_strings_p;
1315
1316   /* If non-NULL, then we are parsing a construct where new type
1317      definitions are not permitted.  The string stored here will be
1318      issued as an error message if a type is defined.  */
1319   const char *type_definition_forbidden_message;
1320
1321   /* A list of lists. The outer list is a stack, used for member
1322      functions of local classes. At each level there are two sub-list,
1323      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1324      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1325      TREE_VALUE's. The functions are chained in reverse declaration
1326      order.
1327
1328      The TREE_PURPOSE sublist contains those functions with default
1329      arguments that need post processing, and the TREE_VALUE sublist
1330      contains those functions with definitions that need post
1331      processing.
1332
1333      These lists can only be processed once the outermost class being
1334      defined is complete.  */
1335   tree unparsed_functions_queues;
1336
1337   /* The number of classes whose definitions are currently in
1338      progress.  */
1339   unsigned num_classes_being_defined;
1340
1341   /* The number of template parameter lists that apply directly to the
1342      current declaration.  */
1343   unsigned num_template_parameter_lists;
1344 } cp_parser;
1345
1346 /* The type of a function that parses some kind of expression.  */
1347 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1348
1349 /* Prototypes.  */
1350
1351 /* Constructors and destructors.  */
1352
1353 static cp_parser *cp_parser_new
1354   (void);
1355
1356 /* Routines to parse various constructs.
1357
1358    Those that return `tree' will return the error_mark_node (rather
1359    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1360    Sometimes, they will return an ordinary node if error-recovery was
1361    attempted, even though a parse error occurred.  So, to check
1362    whether or not a parse error occurred, you should always use
1363    cp_parser_error_occurred.  If the construct is optional (indicated
1364    either by an `_opt' in the name of the function that does the
1365    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1366    the construct is not present.  */
1367
1368 /* Lexical conventions [gram.lex]  */
1369
1370 static tree cp_parser_identifier
1371   (cp_parser *);
1372 static tree cp_parser_string_literal
1373   (cp_parser *, bool, bool);
1374
1375 /* Basic concepts [gram.basic]  */
1376
1377 static bool cp_parser_translation_unit
1378   (cp_parser *);
1379
1380 /* Expressions [gram.expr]  */
1381
1382 static tree cp_parser_primary_expression
1383   (cp_parser *, bool, cp_id_kind *, tree *);
1384 static tree cp_parser_id_expression
1385   (cp_parser *, bool, bool, bool *, bool);
1386 static tree cp_parser_unqualified_id
1387   (cp_parser *, bool, bool, bool);
1388 static tree cp_parser_nested_name_specifier_opt
1389   (cp_parser *, bool, bool, bool, bool);
1390 static tree cp_parser_nested_name_specifier
1391   (cp_parser *, bool, bool, bool, bool);
1392 static tree cp_parser_class_or_namespace_name
1393   (cp_parser *, bool, bool, bool, bool, bool);
1394 static tree cp_parser_postfix_expression
1395   (cp_parser *, bool, bool);
1396 static tree cp_parser_postfix_open_square_expression
1397   (cp_parser *, tree, bool);
1398 static tree cp_parser_postfix_dot_deref_expression
1399   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1400 static tree cp_parser_parenthesized_expression_list
1401   (cp_parser *, bool, bool, bool *);
1402 static void cp_parser_pseudo_destructor_name
1403   (cp_parser *, tree *, tree *);
1404 static tree cp_parser_unary_expression
1405   (cp_parser *, bool, bool);
1406 static enum tree_code cp_parser_unary_operator
1407   (cp_token *);
1408 static tree cp_parser_new_expression
1409   (cp_parser *);
1410 static tree cp_parser_new_placement
1411   (cp_parser *);
1412 static tree cp_parser_new_type_id
1413   (cp_parser *, tree *);
1414 static cp_declarator *cp_parser_new_declarator_opt
1415   (cp_parser *);
1416 static cp_declarator *cp_parser_direct_new_declarator
1417   (cp_parser *);
1418 static tree cp_parser_new_initializer
1419   (cp_parser *);
1420 static tree cp_parser_delete_expression
1421   (cp_parser *);
1422 static tree cp_parser_cast_expression
1423   (cp_parser *, bool, bool);
1424 static tree cp_parser_binary_expression
1425   (cp_parser *, bool);
1426 static tree cp_parser_question_colon_clause
1427   (cp_parser *, tree);
1428 static tree cp_parser_assignment_expression
1429   (cp_parser *, bool);
1430 static enum tree_code cp_parser_assignment_operator_opt
1431   (cp_parser *);
1432 static tree cp_parser_expression
1433   (cp_parser *, bool);
1434 static tree cp_parser_constant_expression
1435   (cp_parser *, bool, bool *);
1436 static tree cp_parser_builtin_offsetof
1437   (cp_parser *);
1438
1439 /* Statements [gram.stmt.stmt]  */
1440
1441 static void cp_parser_statement
1442   (cp_parser *, tree);
1443 static tree cp_parser_labeled_statement
1444   (cp_parser *, tree);
1445 static tree cp_parser_expression_statement
1446   (cp_parser *, tree);
1447 static tree cp_parser_compound_statement
1448   (cp_parser *, tree, bool);
1449 static void cp_parser_statement_seq_opt
1450   (cp_parser *, tree);
1451 static tree cp_parser_selection_statement
1452   (cp_parser *);
1453 static tree cp_parser_condition
1454   (cp_parser *);
1455 static tree cp_parser_iteration_statement
1456   (cp_parser *);
1457 static void cp_parser_for_init_statement
1458   (cp_parser *);
1459 static tree cp_parser_jump_statement
1460   (cp_parser *);
1461 static void cp_parser_declaration_statement
1462   (cp_parser *);
1463
1464 static tree cp_parser_implicitly_scoped_statement
1465   (cp_parser *);
1466 static void cp_parser_already_scoped_statement
1467   (cp_parser *);
1468
1469 /* Declarations [gram.dcl.dcl] */
1470
1471 static void cp_parser_declaration_seq_opt
1472   (cp_parser *);
1473 static void cp_parser_declaration
1474   (cp_parser *);
1475 static void cp_parser_block_declaration
1476   (cp_parser *, bool);
1477 static void cp_parser_simple_declaration
1478   (cp_parser *, bool);
1479 static void cp_parser_decl_specifier_seq
1480   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1481 static tree cp_parser_storage_class_specifier_opt
1482   (cp_parser *);
1483 static tree cp_parser_function_specifier_opt
1484   (cp_parser *, cp_decl_specifier_seq *);
1485 static tree cp_parser_type_specifier
1486   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1487    int *, bool *);
1488 static tree cp_parser_simple_type_specifier
1489   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1490 static tree cp_parser_type_name
1491   (cp_parser *);
1492 static tree cp_parser_elaborated_type_specifier
1493   (cp_parser *, bool, bool);
1494 static tree cp_parser_enum_specifier
1495   (cp_parser *);
1496 static void cp_parser_enumerator_list
1497   (cp_parser *, tree);
1498 static void cp_parser_enumerator_definition
1499   (cp_parser *, tree);
1500 static tree cp_parser_namespace_name
1501   (cp_parser *);
1502 static void cp_parser_namespace_definition
1503   (cp_parser *);
1504 static void cp_parser_namespace_body
1505   (cp_parser *);
1506 static tree cp_parser_qualified_namespace_specifier
1507   (cp_parser *);
1508 static void cp_parser_namespace_alias_definition
1509   (cp_parser *);
1510 static void cp_parser_using_declaration
1511   (cp_parser *);
1512 static void cp_parser_using_directive
1513   (cp_parser *);
1514 static void cp_parser_asm_definition
1515   (cp_parser *);
1516 static void cp_parser_linkage_specification
1517   (cp_parser *);
1518
1519 /* Declarators [gram.dcl.decl] */
1520
1521 static tree cp_parser_init_declarator
1522   (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1523 static cp_declarator *cp_parser_declarator
1524   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1525 static cp_declarator *cp_parser_direct_declarator
1526   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1527 static enum tree_code cp_parser_ptr_operator
1528   (cp_parser *, tree *, cp_cv_quals *);
1529 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1530   (cp_parser *);
1531 static tree cp_parser_declarator_id
1532   (cp_parser *);
1533 static tree cp_parser_type_id
1534   (cp_parser *);
1535 static void cp_parser_type_specifier_seq
1536   (cp_parser *, bool, cp_decl_specifier_seq *);
1537 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1538   (cp_parser *);
1539 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1540   (cp_parser *, bool *);
1541 static cp_parameter_declarator *cp_parser_parameter_declaration
1542   (cp_parser *, bool, bool *);
1543 static void cp_parser_function_body
1544   (cp_parser *);
1545 static tree cp_parser_initializer
1546   (cp_parser *, bool *, bool *);
1547 static tree cp_parser_initializer_clause
1548   (cp_parser *, bool *);
1549 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1550   (cp_parser *, bool *);
1551
1552 static bool cp_parser_ctor_initializer_opt_and_function_body
1553   (cp_parser *);
1554
1555 /* Classes [gram.class] */
1556
1557 static tree cp_parser_class_name
1558   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1559 static tree cp_parser_class_specifier
1560   (cp_parser *);
1561 static tree cp_parser_class_head
1562   (cp_parser *, bool *, tree *);
1563 static enum tag_types cp_parser_class_key
1564   (cp_parser *);
1565 static void cp_parser_member_specification_opt
1566   (cp_parser *);
1567 static void cp_parser_member_declaration
1568   (cp_parser *);
1569 static tree cp_parser_pure_specifier
1570   (cp_parser *);
1571 static tree cp_parser_constant_initializer
1572   (cp_parser *);
1573
1574 /* Derived classes [gram.class.derived] */
1575
1576 static tree cp_parser_base_clause
1577   (cp_parser *);
1578 static tree cp_parser_base_specifier
1579   (cp_parser *);
1580
1581 /* Special member functions [gram.special] */
1582
1583 static tree cp_parser_conversion_function_id
1584   (cp_parser *);
1585 static tree cp_parser_conversion_type_id
1586   (cp_parser *);
1587 static cp_declarator *cp_parser_conversion_declarator_opt
1588   (cp_parser *);
1589 static bool cp_parser_ctor_initializer_opt
1590   (cp_parser *);
1591 static void cp_parser_mem_initializer_list
1592   (cp_parser *);
1593 static tree cp_parser_mem_initializer
1594   (cp_parser *);
1595 static tree cp_parser_mem_initializer_id
1596   (cp_parser *);
1597
1598 /* Overloading [gram.over] */
1599
1600 static tree cp_parser_operator_function_id
1601   (cp_parser *);
1602 static tree cp_parser_operator
1603   (cp_parser *);
1604
1605 /* Templates [gram.temp] */
1606
1607 static void cp_parser_template_declaration
1608   (cp_parser *, bool);
1609 static tree cp_parser_template_parameter_list
1610   (cp_parser *);
1611 static tree cp_parser_template_parameter
1612   (cp_parser *, bool *);
1613 static tree cp_parser_type_parameter
1614   (cp_parser *);
1615 static tree cp_parser_template_id
1616   (cp_parser *, bool, bool, bool);
1617 static tree cp_parser_template_name
1618   (cp_parser *, bool, bool, bool, bool *);
1619 static tree cp_parser_template_argument_list
1620   (cp_parser *);
1621 static tree cp_parser_template_argument
1622   (cp_parser *);
1623 static void cp_parser_explicit_instantiation
1624   (cp_parser *);
1625 static void cp_parser_explicit_specialization
1626   (cp_parser *);
1627
1628 /* Exception handling [gram.exception] */
1629
1630 static tree cp_parser_try_block
1631   (cp_parser *);
1632 static bool cp_parser_function_try_block
1633   (cp_parser *);
1634 static void cp_parser_handler_seq
1635   (cp_parser *);
1636 static void cp_parser_handler
1637   (cp_parser *);
1638 static tree cp_parser_exception_declaration
1639   (cp_parser *);
1640 static tree cp_parser_throw_expression
1641   (cp_parser *);
1642 static tree cp_parser_exception_specification_opt
1643   (cp_parser *);
1644 static tree cp_parser_type_id_list
1645   (cp_parser *);
1646
1647 /* GNU Extensions */
1648
1649 static tree cp_parser_asm_specification_opt
1650   (cp_parser *);
1651 static tree cp_parser_asm_operand_list
1652   (cp_parser *);
1653 static tree cp_parser_asm_clobber_list
1654   (cp_parser *);
1655 static tree cp_parser_attributes_opt
1656   (cp_parser *);
1657 static tree cp_parser_attribute_list
1658   (cp_parser *);
1659 static bool cp_parser_extension_opt
1660   (cp_parser *, int *);
1661 static void cp_parser_label_declaration
1662   (cp_parser *);
1663
1664 /* Objective-C++ Productions */
1665
1666 static tree cp_parser_objc_message_receiver
1667   (cp_parser *);
1668 static tree cp_parser_objc_message_args
1669   (cp_parser *);
1670 static tree cp_parser_objc_message_expression
1671   (cp_parser *);
1672 static tree cp_parser_objc_encode_expression
1673   (cp_parser *);
1674 static tree cp_parser_objc_defs_expression
1675   (cp_parser *);
1676 static tree cp_parser_objc_protocol_expression
1677   (cp_parser *);
1678 static tree cp_parser_objc_selector_expression
1679   (cp_parser *);
1680 static tree cp_parser_objc_expression
1681   (cp_parser *);
1682 static bool cp_parser_objc_selector_p
1683   (enum cpp_ttype);
1684 static tree cp_parser_objc_selector
1685   (cp_parser *);
1686 static tree cp_parser_objc_protocol_refs_opt
1687   (cp_parser *);
1688 static void cp_parser_objc_declaration
1689   (cp_parser *);
1690 static tree cp_parser_objc_statement
1691   (cp_parser *);
1692
1693 /* Utility Routines */
1694
1695 static tree cp_parser_lookup_name
1696   (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
1697 static tree cp_parser_lookup_name_simple
1698   (cp_parser *, tree);
1699 static tree cp_parser_maybe_treat_template_as_class
1700   (tree, bool);
1701 static bool cp_parser_check_declarator_template_parameters
1702   (cp_parser *, cp_declarator *);
1703 static bool cp_parser_check_template_parameters
1704   (cp_parser *, unsigned);
1705 static tree cp_parser_simple_cast_expression
1706   (cp_parser *);
1707 static tree cp_parser_global_scope_opt
1708   (cp_parser *, bool);
1709 static bool cp_parser_constructor_declarator_p
1710   (cp_parser *, bool);
1711 static tree cp_parser_function_definition_from_specifiers_and_declarator
1712   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1713 static tree cp_parser_function_definition_after_declarator
1714   (cp_parser *, bool);
1715 static void cp_parser_template_declaration_after_export
1716   (cp_parser *, bool);
1717 static tree cp_parser_single_declaration
1718   (cp_parser *, bool, bool *);
1719 static tree cp_parser_functional_cast
1720   (cp_parser *, tree);
1721 static tree cp_parser_save_member_function_body
1722   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1723 static tree cp_parser_enclosed_template_argument_list
1724   (cp_parser *);
1725 static void cp_parser_save_default_args
1726   (cp_parser *, tree);
1727 static void cp_parser_late_parsing_for_member
1728   (cp_parser *, tree);
1729 static void cp_parser_late_parsing_default_args
1730   (cp_parser *, tree);
1731 static tree cp_parser_sizeof_operand
1732   (cp_parser *, enum rid);
1733 static bool cp_parser_declares_only_class_p
1734   (cp_parser *);
1735 static void cp_parser_set_storage_class
1736   (cp_decl_specifier_seq *, cp_storage_class);
1737 static void cp_parser_set_decl_spec_type
1738   (cp_decl_specifier_seq *, tree, bool);
1739 static bool cp_parser_friend_p
1740   (const cp_decl_specifier_seq *);
1741 static cp_token *cp_parser_require
1742   (cp_parser *, enum cpp_ttype, const char *);
1743 static cp_token *cp_parser_require_keyword
1744   (cp_parser *, enum rid, const char *);
1745 static bool cp_parser_token_starts_function_definition_p
1746   (cp_token *);
1747 static bool cp_parser_next_token_starts_class_definition_p
1748   (cp_parser *);
1749 static bool cp_parser_next_token_ends_template_argument_p
1750   (cp_parser *);
1751 static bool cp_parser_nth_token_starts_template_argument_list_p
1752   (cp_parser *, size_t);
1753 static enum tag_types cp_parser_token_is_class_key
1754   (cp_token *);
1755 static void cp_parser_check_class_key
1756   (enum tag_types, tree type);
1757 static void cp_parser_check_access_in_redeclaration
1758   (tree type);
1759 static bool cp_parser_optional_template_keyword
1760   (cp_parser *);
1761 static void cp_parser_pre_parsed_nested_name_specifier
1762   (cp_parser *);
1763 static void cp_parser_cache_group
1764   (cp_parser *, enum cpp_ttype, unsigned);
1765 static void cp_parser_parse_tentatively
1766   (cp_parser *);
1767 static void cp_parser_commit_to_tentative_parse
1768   (cp_parser *);
1769 static void cp_parser_abort_tentative_parse
1770   (cp_parser *);
1771 static bool cp_parser_parse_definitely
1772   (cp_parser *);
1773 static inline bool cp_parser_parsing_tentatively
1774   (cp_parser *);
1775 static bool cp_parser_uncommitted_to_tentative_parse_p
1776   (cp_parser *);
1777 static void cp_parser_error
1778   (cp_parser *, const char *);
1779 static void cp_parser_name_lookup_error
1780   (cp_parser *, tree, tree, const char *);
1781 static bool cp_parser_simulate_error
1782   (cp_parser *);
1783 static void cp_parser_check_type_definition
1784   (cp_parser *);
1785 static void cp_parser_check_for_definition_in_return_type
1786   (cp_declarator *, tree);
1787 static void cp_parser_check_for_invalid_template_id
1788   (cp_parser *, tree);
1789 static bool cp_parser_non_integral_constant_expression
1790   (cp_parser *, const char *);
1791 static void cp_parser_diagnose_invalid_type_name
1792   (cp_parser *, tree, tree);
1793 static bool cp_parser_parse_and_diagnose_invalid_type_name
1794   (cp_parser *);
1795 static int cp_parser_skip_to_closing_parenthesis
1796   (cp_parser *, bool, bool, bool);
1797 static void cp_parser_skip_to_end_of_statement
1798   (cp_parser *);
1799 static void cp_parser_consume_semicolon_at_end_of_statement
1800   (cp_parser *);
1801 static void cp_parser_skip_to_end_of_block_or_statement
1802   (cp_parser *);
1803 static void cp_parser_skip_to_closing_brace
1804   (cp_parser *);
1805 static void cp_parser_skip_until_found
1806   (cp_parser *, enum cpp_ttype, const char *);
1807 static bool cp_parser_error_occurred
1808   (cp_parser *);
1809 static bool cp_parser_allow_gnu_extensions_p
1810   (cp_parser *);
1811 static bool cp_parser_is_string_literal
1812   (cp_token *);
1813 static bool cp_parser_is_keyword
1814   (cp_token *, enum rid);
1815 static tree cp_parser_make_typename_type
1816   (cp_parser *, tree, tree);
1817
1818 /* Returns nonzero if we are parsing tentatively.  */
1819
1820 static inline bool
1821 cp_parser_parsing_tentatively (cp_parser* parser)
1822 {
1823   return parser->context->next != NULL;
1824 }
1825
1826 /* Returns nonzero if TOKEN is a string literal.  */
1827
1828 static bool
1829 cp_parser_is_string_literal (cp_token* token)
1830 {
1831   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1832 }
1833
1834 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1835
1836 static bool
1837 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1838 {
1839   return token->keyword == keyword;
1840 }
1841
1842 /* A minimum or maximum operator has been seen.  As these are
1843    deprecated, issue a warning.  */
1844
1845 static inline void
1846 cp_parser_warn_min_max (void)
1847 {
1848   if (warn_deprecated && !in_system_header)
1849     warning (0, "minimum/maximum operators are deprecated");
1850 }
1851
1852 /* If not parsing tentatively, issue a diagnostic of the form
1853       FILE:LINE: MESSAGE before TOKEN
1854    where TOKEN is the next token in the input stream.  MESSAGE
1855    (specified by the caller) is usually of the form "expected
1856    OTHER-TOKEN".  */
1857
1858 static void
1859 cp_parser_error (cp_parser* parser, const char* message)
1860 {
1861   if (!cp_parser_simulate_error (parser))
1862     {
1863       cp_token *token = cp_lexer_peek_token (parser->lexer);
1864       /* This diagnostic makes more sense if it is tagged to the line
1865          of the token we just peeked at.  */
1866       cp_lexer_set_source_position_from_token (token);
1867       if (token->type == CPP_PRAGMA)
1868         {
1869           error ("%<#pragma%> is not allowed here");
1870           cp_lexer_purge_token (parser->lexer);
1871           return;
1872         }
1873       c_parse_error (message,
1874                      /* Because c_parser_error does not understand
1875                         CPP_KEYWORD, keywords are treated like
1876                         identifiers.  */
1877                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1878                      token->value);
1879     }
1880 }
1881
1882 /* Issue an error about name-lookup failing.  NAME is the
1883    IDENTIFIER_NODE DECL is the result of
1884    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1885    the thing that we hoped to find.  */
1886
1887 static void
1888 cp_parser_name_lookup_error (cp_parser* parser,
1889                              tree name,
1890                              tree decl,
1891                              const char* desired)
1892 {
1893   /* If name lookup completely failed, tell the user that NAME was not
1894      declared.  */
1895   if (decl == error_mark_node)
1896     {
1897       if (parser->scope && parser->scope != global_namespace)
1898         error ("%<%D::%D%> has not been declared",
1899                parser->scope, name);
1900       else if (parser->scope == global_namespace)
1901         error ("%<::%D%> has not been declared", name);
1902       else if (parser->object_scope
1903                && !CLASS_TYPE_P (parser->object_scope))
1904         error ("request for member %qD in non-class type %qT",
1905                name, parser->object_scope);
1906       else if (parser->object_scope)
1907         error ("%<%T::%D%> has not been declared",
1908                parser->object_scope, name);
1909       else
1910         error ("%qD has not been declared", name);
1911     }
1912   else if (parser->scope && parser->scope != global_namespace)
1913     error ("%<%D::%D%> %s", parser->scope, name, desired);
1914   else if (parser->scope == global_namespace)
1915     error ("%<::%D%> %s", name, desired);
1916   else
1917     error ("%qD %s", name, desired);
1918 }
1919
1920 /* If we are parsing tentatively, remember that an error has occurred
1921    during this tentative parse.  Returns true if the error was
1922    simulated; false if a message should be issued by the caller.  */
1923
1924 static bool
1925 cp_parser_simulate_error (cp_parser* parser)
1926 {
1927   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1928     {
1929       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1930       return true;
1931     }
1932   return false;
1933 }
1934
1935 /* This function is called when a type is defined.  If type
1936    definitions are forbidden at this point, an error message is
1937    issued.  */
1938
1939 static void
1940 cp_parser_check_type_definition (cp_parser* parser)
1941 {
1942   /* If types are forbidden here, issue a message.  */
1943   if (parser->type_definition_forbidden_message)
1944     /* Use `%s' to print the string in case there are any escape
1945        characters in the message.  */
1946     error ("%s", parser->type_definition_forbidden_message);
1947 }
1948
1949 /* This function is called when the DECLARATOR is processed.  The TYPE
1950    was a type defined in the decl-specifiers.  If it is invalid to
1951    define a type in the decl-specifiers for DECLARATOR, an error is
1952    issued.  */
1953
1954 static void
1955 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1956                                                tree type)
1957 {
1958   /* [dcl.fct] forbids type definitions in return types.
1959      Unfortunately, it's not easy to know whether or not we are
1960      processing a return type until after the fact.  */
1961   while (declarator
1962          && (declarator->kind == cdk_pointer
1963              || declarator->kind == cdk_reference
1964              || declarator->kind == cdk_ptrmem))
1965     declarator = declarator->declarator;
1966   if (declarator
1967       && declarator->kind == cdk_function)
1968     {
1969       error ("new types may not be defined in a return type");
1970       inform ("(perhaps a semicolon is missing after the definition of %qT)",
1971               type);
1972     }
1973 }
1974
1975 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1976    "<" in any valid C++ program.  If the next token is indeed "<",
1977    issue a message warning the user about what appears to be an
1978    invalid attempt to form a template-id.  */
1979
1980 static void
1981 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1982                                          tree type)
1983 {
1984   cp_token_position start = 0;
1985
1986   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1987     {
1988       if (TYPE_P (type))
1989         error ("%qT is not a template", type);
1990       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1991         error ("%qE is not a template", type);
1992       else
1993         error ("invalid template-id");
1994       /* Remember the location of the invalid "<".  */
1995       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1996         start = cp_lexer_token_position (parser->lexer, true);
1997       /* Consume the "<".  */
1998       cp_lexer_consume_token (parser->lexer);
1999       /* Parse the template arguments.  */
2000       cp_parser_enclosed_template_argument_list (parser);
2001       /* Permanently remove the invalid template arguments so that
2002          this error message is not issued again.  */
2003       if (start)
2004         cp_lexer_purge_tokens_after (parser->lexer, start);
2005     }
2006 }
2007
2008 /* If parsing an integral constant-expression, issue an error message
2009    about the fact that THING appeared and return true.  Otherwise,
2010    return false.  In either case, set
2011    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2012
2013 static bool
2014 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2015                                             const char *thing)
2016 {
2017   parser->non_integral_constant_expression_p = true;
2018   if (parser->integral_constant_expression_p)
2019     {
2020       if (!parser->allow_non_integral_constant_expression_p)
2021         {
2022           error ("%s cannot appear in a constant-expression", thing);
2023           return true;
2024         }
2025     }
2026   return false;
2027 }
2028
2029 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2030    qualifying scope (or NULL, if none) for ID.  This function commits
2031    to the current active tentative parse, if any.  (Otherwise, the
2032    problematic construct might be encountered again later, resulting
2033    in duplicate error messages.)  */
2034
2035 static void
2036 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2037 {
2038   tree decl, old_scope;
2039   /* Try to lookup the identifier.  */
2040   old_scope = parser->scope;
2041   parser->scope = scope;
2042   decl = cp_parser_lookup_name_simple (parser, id);
2043   parser->scope = old_scope;
2044   /* If the lookup found a template-name, it means that the user forgot
2045   to specify an argument list. Emit a useful error message.  */
2046   if (TREE_CODE (decl) == TEMPLATE_DECL)
2047     error ("invalid use of template-name %qE without an argument list",
2048       decl);
2049   else if (!parser->scope || parser->scope == error_mark_node)
2050     {
2051       /* Issue an error message.  */
2052       error ("%qE does not name a type", id);
2053       /* If we're in a template class, it's possible that the user was
2054          referring to a type from a base class.  For example:
2055
2056            template <typename T> struct A { typedef T X; };
2057            template <typename T> struct B : public A<T> { X x; };
2058
2059          The user should have said "typename A<T>::X".  */
2060       if (processing_template_decl && current_class_type
2061           && TYPE_BINFO (current_class_type))
2062         {
2063           tree b;
2064
2065           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2066                b;
2067                b = TREE_CHAIN (b))
2068             {
2069               tree base_type = BINFO_TYPE (b);
2070               if (CLASS_TYPE_P (base_type)
2071                   && dependent_type_p (base_type))
2072                 {
2073                   tree field;
2074                   /* Go from a particular instantiation of the
2075                      template (which will have an empty TYPE_FIELDs),
2076                      to the main version.  */
2077                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2078                   for (field = TYPE_FIELDS (base_type);
2079                        field;
2080                        field = TREE_CHAIN (field))
2081                     if (TREE_CODE (field) == TYPE_DECL
2082                         && DECL_NAME (field) == id)
2083                       {
2084                         inform ("(perhaps %<typename %T::%E%> was intended)",
2085                                 BINFO_TYPE (b), id);
2086                         break;
2087                       }
2088                   if (field)
2089                     break;
2090                 }
2091             }
2092         }
2093     }
2094   /* Here we diagnose qualified-ids where the scope is actually correct,
2095      but the identifier does not resolve to a valid type name.  */
2096   else
2097     {
2098       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2099         error ("%qE in namespace %qE does not name a type",
2100                id, parser->scope);
2101       else if (TYPE_P (parser->scope))
2102         error ("%qE in class %qT does not name a type", id, parser->scope);
2103       else
2104         gcc_unreachable ();
2105     }
2106   cp_parser_commit_to_tentative_parse (parser);
2107 }
2108
2109 /* Check for a common situation where a type-name should be present,
2110    but is not, and issue a sensible error message.  Returns true if an
2111    invalid type-name was detected.
2112
2113    The situation handled by this function are variable declarations of the
2114    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2115    Usually, `ID' should name a type, but if we got here it means that it
2116    does not. We try to emit the best possible error message depending on
2117    how exactly the id-expression looks like.
2118 */
2119
2120 static bool
2121 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2122 {
2123   tree id;
2124
2125   cp_parser_parse_tentatively (parser);
2126   id = cp_parser_id_expression (parser,
2127                                 /*template_keyword_p=*/false,
2128                                 /*check_dependency_p=*/true,
2129                                 /*template_p=*/NULL,
2130                                 /*declarator_p=*/true);
2131   /* After the id-expression, there should be a plain identifier,
2132      otherwise this is not a simple variable declaration. Also, if
2133      the scope is dependent, we cannot do much.  */
2134   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2135       || (parser->scope && TYPE_P (parser->scope)
2136           && dependent_type_p (parser->scope)))
2137     {
2138       cp_parser_abort_tentative_parse (parser);
2139       return false;
2140     }
2141   if (!cp_parser_parse_definitely (parser)
2142       || TREE_CODE (id) != IDENTIFIER_NODE)
2143     return false;
2144
2145   /* Emit a diagnostic for the invalid type.  */
2146   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2147   /* Skip to the end of the declaration; there's no point in
2148      trying to process it.  */
2149   cp_parser_skip_to_end_of_block_or_statement (parser);
2150   return true;
2151 }
2152
2153 /* Consume tokens up to, and including, the next non-nested closing `)'.
2154    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2155    are doing error recovery. Returns -1 if OR_COMMA is true and we
2156    found an unnested comma.  */
2157
2158 static int
2159 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2160                                        bool recovering,
2161                                        bool or_comma,
2162                                        bool consume_paren)
2163 {
2164   unsigned paren_depth = 0;
2165   unsigned brace_depth = 0;
2166   int result;
2167
2168   if (recovering && !or_comma
2169       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2170     return 0;
2171
2172   while (true)
2173     {
2174       cp_token *token;
2175
2176       /* If we've run out of tokens, then there is no closing `)'.  */
2177       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2178         {
2179           result = 0;
2180           break;
2181         }
2182
2183       token = cp_lexer_peek_token (parser->lexer);
2184
2185       /* This matches the processing in skip_to_end_of_statement.  */
2186       if (token->type == CPP_SEMICOLON && !brace_depth)
2187         {
2188           result = 0;
2189           break;
2190         }
2191       if (token->type == CPP_OPEN_BRACE)
2192         ++brace_depth;
2193       if (token->type == CPP_CLOSE_BRACE)
2194         {
2195           if (!brace_depth--)
2196             {
2197               result = 0;
2198               break;
2199             }
2200         }
2201       if (recovering && or_comma && token->type == CPP_COMMA
2202           && !brace_depth && !paren_depth)
2203         {
2204           result = -1;
2205           break;
2206         }
2207
2208       if (!brace_depth)
2209         {
2210           /* If it is an `(', we have entered another level of nesting.  */
2211           if (token->type == CPP_OPEN_PAREN)
2212             ++paren_depth;
2213           /* If it is a `)', then we might be done.  */
2214           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2215             {
2216               if (consume_paren)
2217                 cp_lexer_consume_token (parser->lexer);
2218               {
2219                 result = 1;
2220                 break;
2221               }
2222             }
2223         }
2224
2225       /* Consume the token.  */
2226       cp_lexer_consume_token (parser->lexer);
2227     }
2228
2229   return result;
2230 }
2231
2232 /* Consume tokens until we reach the end of the current statement.
2233    Normally, that will be just before consuming a `;'.  However, if a
2234    non-nested `}' comes first, then we stop before consuming that.  */
2235
2236 static void
2237 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2238 {
2239   unsigned nesting_depth = 0;
2240
2241   while (true)
2242     {
2243       cp_token *token;
2244
2245       /* Peek at the next token.  */
2246       token = cp_lexer_peek_token (parser->lexer);
2247       /* If we've run out of tokens, stop.  */
2248       if (token->type == CPP_EOF)
2249         break;
2250       /* If the next token is a `;', we have reached the end of the
2251          statement.  */
2252       if (token->type == CPP_SEMICOLON && !nesting_depth)
2253         break;
2254       /* If the next token is a non-nested `}', then we have reached
2255          the end of the current block.  */
2256       if (token->type == CPP_CLOSE_BRACE)
2257         {
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             break;
2267           /* If it is the closing `}' for a block that we have
2268              scanned, stop -- but only after consuming the token.
2269              That way given:
2270
2271                 void f g () { ... }
2272                 typedef int I;
2273
2274              we will stop after the body of the erroneously declared
2275              function, but before consuming the following `typedef'
2276              declaration.  */
2277           if (--nesting_depth == 0)
2278             {
2279               cp_lexer_consume_token (parser->lexer);
2280               break;
2281             }
2282         }
2283       /* If it the next token is a `{', then we are entering a new
2284          block.  Consume the entire block.  */
2285       else if (token->type == CPP_OPEN_BRACE)
2286         ++nesting_depth;
2287       /* Consume the token.  */
2288       cp_lexer_consume_token (parser->lexer);
2289     }
2290 }
2291
2292 /* This function is called at the end of a statement or declaration.
2293    If the next token is a semicolon, it is consumed; otherwise, error
2294    recovery is attempted.  */
2295
2296 static void
2297 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2298 {
2299   /* Look for the trailing `;'.  */
2300   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2301     {
2302       /* If there is additional (erroneous) input, skip to the end of
2303          the statement.  */
2304       cp_parser_skip_to_end_of_statement (parser);
2305       /* If the next token is now a `;', consume it.  */
2306       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2307         cp_lexer_consume_token (parser->lexer);
2308     }
2309 }
2310
2311 /* Skip tokens until we have consumed an entire block, or until we
2312    have consumed a non-nested `;'.  */
2313
2314 static void
2315 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2316 {
2317   int nesting_depth = 0;
2318
2319   while (nesting_depth >= 0)
2320     {
2321       cp_token *token = cp_lexer_peek_token (parser->lexer);
2322
2323       if (token->type == CPP_EOF)
2324         break;
2325
2326       switch (token->type)
2327         {
2328         case CPP_EOF:
2329           /* If we've run out of tokens, stop.  */
2330           nesting_depth = -1;
2331           continue;
2332
2333         case CPP_SEMICOLON:
2334           /* Stop if this is an unnested ';'. */
2335           if (!nesting_depth)
2336             nesting_depth = -1;
2337           break;
2338
2339         case CPP_CLOSE_BRACE:
2340           /* Stop if this is an unnested '}', or closes the outermost
2341              nesting level.  */
2342           nesting_depth--;
2343           if (!nesting_depth)
2344             nesting_depth = -1;
2345           break;
2346
2347         case CPP_OPEN_BRACE:
2348           /* Nest. */
2349           nesting_depth++;
2350           break;
2351
2352         default:
2353           break;
2354         }
2355
2356       /* Consume the token.  */
2357       cp_lexer_consume_token (parser->lexer);
2358
2359     }
2360 }
2361
2362 /* Skip tokens until a non-nested closing curly brace is the next
2363    token.  */
2364
2365 static void
2366 cp_parser_skip_to_closing_brace (cp_parser *parser)
2367 {
2368   unsigned nesting_depth = 0;
2369
2370   while (true)
2371     {
2372       cp_token *token;
2373
2374       /* Peek at the next token.  */
2375       token = cp_lexer_peek_token (parser->lexer);
2376       /* If we've run out of tokens, stop.  */
2377       if (token->type == CPP_EOF)
2378         break;
2379       /* If the next token is a non-nested `}', then we have reached
2380          the end of the current block.  */
2381       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2382         break;
2383       /* If it the next token is a `{', then we are entering a new
2384          block.  Consume the entire block.  */
2385       else if (token->type == CPP_OPEN_BRACE)
2386         ++nesting_depth;
2387       /* Consume the token.  */
2388       cp_lexer_consume_token (parser->lexer);
2389     }
2390 }
2391
2392 /* This is a simple wrapper around make_typename_type. When the id is
2393    an unresolved identifier node, we can provide a superior diagnostic
2394    using cp_parser_diagnose_invalid_type_name.  */
2395
2396 static tree
2397 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2398 {
2399   tree result;
2400   if (TREE_CODE (id) == IDENTIFIER_NODE)
2401     {
2402       result = make_typename_type (scope, id, typename_type,
2403                                    /*complain=*/0);
2404       if (result == error_mark_node)
2405         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2406       return result;
2407     }
2408   return make_typename_type (scope, id, typename_type, tf_error);
2409 }
2410
2411
2412 /* Create a new C++ parser.  */
2413
2414 static cp_parser *
2415 cp_parser_new (void)
2416 {
2417   cp_parser *parser;
2418   cp_lexer *lexer;
2419   unsigned i;
2420
2421   /* cp_lexer_new_main is called before calling ggc_alloc because
2422      cp_lexer_new_main might load a PCH file.  */
2423   lexer = cp_lexer_new_main ();
2424
2425   /* Initialize the binops_by_token so that we can get the tree
2426      directly from the token.  */
2427   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2428     binops_by_token[binops[i].token_type] = binops[i];
2429
2430   parser = GGC_CNEW (cp_parser);
2431   parser->lexer = lexer;
2432   parser->context = cp_parser_context_new (NULL);
2433
2434   /* For now, we always accept GNU extensions.  */
2435   parser->allow_gnu_extensions_p = 1;
2436
2437   /* The `>' token is a greater-than operator, not the end of a
2438      template-id.  */
2439   parser->greater_than_is_operator_p = true;
2440
2441   parser->default_arg_ok_p = true;
2442
2443   /* We are not parsing a constant-expression.  */
2444   parser->integral_constant_expression_p = false;
2445   parser->allow_non_integral_constant_expression_p = false;
2446   parser->non_integral_constant_expression_p = false;
2447
2448   /* Local variable names are not forbidden.  */
2449   parser->local_variables_forbidden_p = false;
2450
2451   /* We are not processing an `extern "C"' declaration.  */
2452   parser->in_unbraced_linkage_specification_p = false;
2453
2454   /* We are not processing a declarator.  */
2455   parser->in_declarator_p = false;
2456
2457   /* We are not processing a template-argument-list.  */
2458   parser->in_template_argument_list_p = false;
2459
2460   /* We are not in an iteration statement.  */
2461   parser->in_iteration_statement_p = false;
2462
2463   /* We are not in a switch statement.  */
2464   parser->in_switch_statement_p = false;
2465
2466   /* We are not parsing a type-id inside an expression.  */
2467   parser->in_type_id_in_expr_p = false;
2468
2469   /* Declarations aren't implicitly extern "C".  */
2470   parser->implicit_extern_c = false;
2471
2472   /* String literals should be translated to the execution character set.  */
2473   parser->translate_strings_p = true;
2474
2475   /* The unparsed function queue is empty.  */
2476   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2477
2478   /* There are no classes being defined.  */
2479   parser->num_classes_being_defined = 0;
2480
2481   /* No template parameters apply.  */
2482   parser->num_template_parameter_lists = 0;
2483
2484   return parser;
2485 }
2486
2487 /* Create a cp_lexer structure which will emit the tokens in CACHE
2488    and push it onto the parser's lexer stack.  This is used for delayed
2489    parsing of in-class method bodies and default arguments, and should
2490    not be confused with tentative parsing.  */
2491 static void
2492 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2493 {
2494   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2495   lexer->next = parser->lexer;
2496   parser->lexer = lexer;
2497
2498   /* Move the current source position to that of the first token in the
2499      new lexer.  */
2500   cp_lexer_set_source_position_from_token (lexer->next_token);
2501 }
2502
2503 /* Pop the top lexer off the parser stack.  This is never used for the
2504    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2505 static void
2506 cp_parser_pop_lexer (cp_parser *parser)
2507 {
2508   cp_lexer *lexer = parser->lexer;
2509   parser->lexer = lexer->next;
2510   cp_lexer_destroy (lexer);
2511
2512   /* Put the current source position back where it was before this
2513      lexer was pushed.  */
2514   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2515 }
2516
2517 /* Lexical conventions [gram.lex]  */
2518
2519 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2520    identifier.  */
2521
2522 static tree
2523 cp_parser_identifier (cp_parser* parser)
2524 {
2525   cp_token *token;
2526
2527   /* Look for the identifier.  */
2528   token = cp_parser_require (parser, CPP_NAME, "identifier");
2529   /* Return the value.  */
2530   return token ? token->value : error_mark_node;
2531 }
2532
2533 /* Parse a sequence of adjacent string constants.  Returns a
2534    TREE_STRING representing the combined, nul-terminated string
2535    constant.  If TRANSLATE is true, translate the string to the
2536    execution character set.  If WIDE_OK is true, a wide string is
2537    invalid here.
2538
2539    C++98 [lex.string] says that if a narrow string literal token is
2540    adjacent to a wide string literal token, the behavior is undefined.
2541    However, C99 6.4.5p4 says that this results in a wide string literal.
2542    We follow C99 here, for consistency with the C front end.
2543
2544    This code is largely lifted from lex_string() in c-lex.c.
2545
2546    FUTURE: ObjC++ will need to handle @-strings here.  */
2547 static tree
2548 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2549 {
2550   tree value;
2551   bool wide = false;
2552   size_t count;
2553   struct obstack str_ob;
2554   cpp_string str, istr, *strs;
2555   cp_token *tok;
2556
2557   tok = cp_lexer_peek_token (parser->lexer);
2558   if (!cp_parser_is_string_literal (tok))
2559     {
2560       cp_parser_error (parser, "expected string-literal");
2561       return error_mark_node;
2562     }
2563
2564   /* Try to avoid the overhead of creating and destroying an obstack
2565      for the common case of just one string.  */
2566   if (!cp_parser_is_string_literal
2567       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2568     {
2569       cp_lexer_consume_token (parser->lexer);
2570
2571       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2572       str.len = TREE_STRING_LENGTH (tok->value);
2573       count = 1;
2574       if (tok->type == CPP_WSTRING)
2575         wide = true;
2576
2577       strs = &str;
2578     }
2579   else
2580     {
2581       gcc_obstack_init (&str_ob);
2582       count = 0;
2583
2584       do
2585         {
2586           cp_lexer_consume_token (parser->lexer);
2587           count++;
2588           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2589           str.len = TREE_STRING_LENGTH (tok->value);
2590           if (tok->type == CPP_WSTRING)
2591             wide = true;
2592
2593           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2594
2595           tok = cp_lexer_peek_token (parser->lexer);
2596         }
2597       while (cp_parser_is_string_literal (tok));
2598
2599       strs = (cpp_string *) obstack_finish (&str_ob);
2600     }
2601
2602   if (wide && !wide_ok)
2603     {
2604       cp_parser_error (parser, "a wide string is invalid in this context");
2605       wide = false;
2606     }
2607
2608   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2609       (parse_in, strs, count, &istr, wide))
2610     {
2611       value = build_string (istr.len, (char *)istr.text);
2612       free ((void *)istr.text);
2613
2614       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2615       value = fix_string_type (value);
2616     }
2617   else
2618     /* cpp_interpret_string has issued an error.  */
2619     value = error_mark_node;
2620
2621   if (count > 1)
2622     obstack_free (&str_ob, 0);
2623
2624   return value;
2625 }
2626
2627
2628 /* Basic concepts [gram.basic]  */
2629
2630 /* Parse a translation-unit.
2631
2632    translation-unit:
2633      declaration-seq [opt]
2634
2635    Returns TRUE if all went well.  */
2636
2637 static bool
2638 cp_parser_translation_unit (cp_parser* parser)
2639 {
2640   /* The address of the first non-permanent object on the declarator
2641      obstack.  */
2642   static void *declarator_obstack_base;
2643
2644   bool success;
2645
2646   /* Create the declarator obstack, if necessary.  */
2647   if (!cp_error_declarator)
2648     {
2649       gcc_obstack_init (&declarator_obstack);
2650       /* Create the error declarator.  */
2651       cp_error_declarator = make_declarator (cdk_error);
2652       /* Create the empty parameter list.  */
2653       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2654       /* Remember where the base of the declarator obstack lies.  */
2655       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2656     }
2657
2658   while (true)
2659     {
2660       cp_parser_declaration_seq_opt (parser);
2661
2662       /* If there are no tokens left then all went well.  */
2663       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2664         {
2665           /* Get rid of the token array; we don't need it any more.  */
2666           cp_lexer_destroy (parser->lexer);
2667           parser->lexer = NULL;
2668
2669           /* This file might have been a context that's implicitly extern
2670              "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2671           if (parser->implicit_extern_c)
2672             {
2673               pop_lang_context ();
2674               parser->implicit_extern_c = false;
2675             }
2676
2677           /* Finish up.  */
2678           finish_translation_unit ();
2679
2680           success = true;
2681           break;
2682         }
2683       else
2684         {
2685           cp_parser_error (parser, "expected declaration");
2686           success = false;
2687           break;
2688         }
2689     }
2690
2691   /* Make sure the declarator obstack was fully cleaned up.  */
2692   gcc_assert (obstack_next_free (&declarator_obstack)
2693               == declarator_obstack_base);
2694
2695   /* All went well.  */
2696   return success;
2697 }
2698
2699 /* Expressions [gram.expr] */
2700
2701 /* Parse a primary-expression.
2702
2703    primary-expression:
2704      literal
2705      this
2706      ( expression )
2707      id-expression
2708
2709    GNU Extensions:
2710
2711    primary-expression:
2712      ( compound-statement )
2713      __builtin_va_arg ( assignment-expression , type-id )
2714
2715    Objective-C++ Extension:
2716
2717    primary-expression:
2718      objc-expression
2719
2720    literal:
2721      __null
2722
2723    CAST_P is true if this primary expression is the target of a cast.
2724
2725    Returns a representation of the expression.
2726
2727    *IDK indicates what kind of id-expression (if any) was present.
2728
2729    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2730    used as the operand of a pointer-to-member.  In that case,
2731    *QUALIFYING_CLASS gives the class that is used as the qualifying
2732    class in the pointer-to-member.  */
2733
2734 static tree
2735 cp_parser_primary_expression (cp_parser *parser,
2736                               bool cast_p,
2737                               cp_id_kind *idk,
2738                               tree *qualifying_class)
2739 {
2740   cp_token *token;
2741
2742   /* Assume the primary expression is not an id-expression.  */
2743   *idk = CP_ID_KIND_NONE;
2744   /* And that it cannot be used as pointer-to-member.  */
2745   *qualifying_class = NULL_TREE;
2746
2747   /* Peek at the next token.  */
2748   token = cp_lexer_peek_token (parser->lexer);
2749   switch (token->type)
2750     {
2751       /* literal:
2752            integer-literal
2753            character-literal
2754            floating-literal
2755            string-literal
2756            boolean-literal  */
2757     case CPP_CHAR:
2758     case CPP_WCHAR:
2759     case CPP_NUMBER:
2760       token = cp_lexer_consume_token (parser->lexer);
2761       /* Floating-point literals are only allowed in an integral
2762          constant expression if they are cast to an integral or
2763          enumeration type.  */
2764       if (TREE_CODE (token->value) == REAL_CST
2765           && parser->integral_constant_expression_p
2766           && pedantic)
2767         {
2768           /* CAST_P will be set even in invalid code like "int(2.7 +
2769              ...)".   Therefore, we have to check that the next token
2770              is sure to end the cast.  */
2771           if (cast_p)
2772             {
2773               cp_token *next_token;
2774
2775               next_token = cp_lexer_peek_token (parser->lexer);
2776               if (/* The comma at the end of an
2777                      enumerator-definition.  */
2778                   next_token->type != CPP_COMMA
2779                   /* The curly brace at the end of an enum-specifier.  */
2780                   && next_token->type != CPP_CLOSE_BRACE
2781                   /* The end of a statement.  */
2782                   && next_token->type != CPP_SEMICOLON
2783                   /* The end of the cast-expression.  */
2784                   && next_token->type != CPP_CLOSE_PAREN
2785                   /* The end of an array bound.  */
2786                   && next_token->type != CPP_CLOSE_SQUARE)
2787                 cast_p = false;
2788             }
2789
2790           /* If we are within a cast, then the constraint that the
2791              cast is to an integral or enumeration type will be
2792              checked at that point.  If we are not within a cast, then
2793              this code is invalid.  */
2794           if (!cast_p)
2795             cp_parser_non_integral_constant_expression
2796               (parser, "floating-point literal");
2797         }
2798       return token->value;
2799
2800     case CPP_STRING:
2801     case CPP_WSTRING:
2802       /* ??? Should wide strings be allowed when parser->translate_strings_p
2803          is false (i.e. in attributes)?  If not, we can kill the third
2804          argument to cp_parser_string_literal.  */
2805       return cp_parser_string_literal (parser,
2806                                        parser->translate_strings_p,
2807                                        true);
2808
2809     case CPP_OPEN_PAREN:
2810       {
2811         tree expr;
2812         bool saved_greater_than_is_operator_p;
2813
2814         /* Consume the `('.  */
2815         cp_lexer_consume_token (parser->lexer);
2816         /* Within a parenthesized expression, a `>' token is always
2817            the greater-than operator.  */
2818         saved_greater_than_is_operator_p
2819           = parser->greater_than_is_operator_p;
2820         parser->greater_than_is_operator_p = true;
2821         /* If we see `( { ' then we are looking at the beginning of
2822            a GNU statement-expression.  */
2823         if (cp_parser_allow_gnu_extensions_p (parser)
2824             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2825           {
2826             /* Statement-expressions are not allowed by the standard.  */
2827             if (pedantic)
2828               pedwarn ("ISO C++ forbids braced-groups within expressions");
2829
2830             /* And they're not allowed outside of a function-body; you
2831                cannot, for example, write:
2832
2833                  int i = ({ int j = 3; j + 1; });
2834
2835                at class or namespace scope.  */
2836             if (!at_function_scope_p ())
2837               error ("statement-expressions are allowed only inside functions");
2838             /* Start the statement-expression.  */
2839             expr = begin_stmt_expr ();
2840             /* Parse the compound-statement.  */
2841             cp_parser_compound_statement (parser, expr, false);
2842             /* Finish up.  */
2843             expr = finish_stmt_expr (expr, false);
2844           }
2845         else
2846           {
2847             /* Parse the parenthesized expression.  */
2848             expr = cp_parser_expression (parser, cast_p);
2849             /* Let the front end know that this expression was
2850                enclosed in parentheses. This matters in case, for
2851                example, the expression is of the form `A::B', since
2852                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2853                not.  */
2854             finish_parenthesized_expr (expr);
2855           }
2856         /* The `>' token might be the end of a template-id or
2857            template-parameter-list now.  */
2858         parser->greater_than_is_operator_p
2859           = saved_greater_than_is_operator_p;
2860         /* Consume the `)'.  */
2861         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2862           cp_parser_skip_to_end_of_statement (parser);
2863
2864         return expr;
2865       }
2866
2867     case CPP_KEYWORD:
2868       switch (token->keyword)
2869         {
2870           /* These two are the boolean literals.  */
2871         case RID_TRUE:
2872           cp_lexer_consume_token (parser->lexer);
2873           return boolean_true_node;
2874         case RID_FALSE:
2875           cp_lexer_consume_token (parser->lexer);
2876           return boolean_false_node;
2877
2878           /* The `__null' literal.  */
2879         case RID_NULL:
2880           cp_lexer_consume_token (parser->lexer);
2881           return null_node;
2882
2883           /* Recognize the `this' keyword.  */
2884         case RID_THIS:
2885           cp_lexer_consume_token (parser->lexer);
2886           if (parser->local_variables_forbidden_p)
2887             {
2888               error ("%<this%> may not be used in this context");
2889               return error_mark_node;
2890             }
2891           /* Pointers cannot appear in constant-expressions.  */
2892           if (cp_parser_non_integral_constant_expression (parser,
2893                                                           "`this'"))
2894             return error_mark_node;
2895           return finish_this_expr ();
2896
2897           /* The `operator' keyword can be the beginning of an
2898              id-expression.  */
2899         case RID_OPERATOR:
2900           goto id_expression;
2901
2902         case RID_FUNCTION_NAME:
2903         case RID_PRETTY_FUNCTION_NAME:
2904         case RID_C99_FUNCTION_NAME:
2905           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2906              __func__ are the names of variables -- but they are
2907              treated specially.  Therefore, they are handled here,
2908              rather than relying on the generic id-expression logic
2909              below.  Grammatically, these names are id-expressions.
2910
2911              Consume the token.  */
2912           token = cp_lexer_consume_token (parser->lexer);
2913           /* Look up the name.  */
2914           return finish_fname (token->value);
2915
2916         case RID_VA_ARG:
2917           {
2918             tree expression;
2919             tree type;
2920
2921             /* The `__builtin_va_arg' construct is used to handle
2922                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2923             cp_lexer_consume_token (parser->lexer);
2924             /* Look for the opening `('.  */
2925             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2926             /* Now, parse the assignment-expression.  */
2927             expression = cp_parser_assignment_expression (parser,
2928                                                           /*cast_p=*/false);
2929             /* Look for the `,'.  */
2930             cp_parser_require (parser, CPP_COMMA, "`,'");
2931             /* Parse the type-id.  */
2932             type = cp_parser_type_id (parser);
2933             /* Look for the closing `)'.  */
2934             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2935             /* Using `va_arg' in a constant-expression is not
2936                allowed.  */
2937             if (cp_parser_non_integral_constant_expression (parser,
2938                                                             "`va_arg'"))
2939               return error_mark_node;
2940             return build_x_va_arg (expression, type);
2941           }
2942
2943         case RID_OFFSETOF:
2944           return cp_parser_builtin_offsetof (parser);
2945
2946           /* Objective-C++ expressions.  */
2947         case RID_AT_ENCODE:
2948         case RID_AT_PROTOCOL:
2949         case RID_AT_SELECTOR:
2950           return cp_parser_objc_expression (parser);
2951
2952         default:
2953           cp_parser_error (parser, "expected primary-expression");
2954           return error_mark_node;
2955         }
2956
2957       /* An id-expression can start with either an identifier, a
2958          `::' as the beginning of a qualified-id, or the "operator"
2959          keyword.  */
2960     case CPP_NAME:
2961     case CPP_SCOPE:
2962     case CPP_TEMPLATE_ID:
2963     case CPP_NESTED_NAME_SPECIFIER:
2964       {
2965         tree id_expression;
2966         tree decl;
2967         const char *error_msg;
2968
2969       id_expression:
2970         /* Parse the id-expression.  */
2971         id_expression
2972           = cp_parser_id_expression (parser,
2973                                      /*template_keyword_p=*/false,
2974                                      /*check_dependency_p=*/true,
2975                                      /*template_p=*/NULL,
2976                                      /*declarator_p=*/false);
2977         if (id_expression == error_mark_node)
2978           return error_mark_node;
2979         /* If we have a template-id, then no further lookup is
2980            required.  If the template-id was for a template-class, we
2981            will sometimes have a TYPE_DECL at this point.  */
2982         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2983             || TREE_CODE (id_expression) == TYPE_DECL)
2984           decl = id_expression;
2985         /* Look up the name.  */
2986         else
2987           {
2988             bool ambiguous_p;
2989
2990             decl = cp_parser_lookup_name (parser, id_expression,
2991                                           none_type,
2992                                           /*is_template=*/false,
2993                                           /*is_namespace=*/false,
2994                                           /*check_dependency=*/true,
2995                                           &ambiguous_p);
2996             /* If the lookup was ambiguous, an error will already have
2997                been issued.  */
2998             if (ambiguous_p)
2999               return error_mark_node;
3000
3001             /* In Objective-C++, an instance variable (ivar) may be preferred
3002                to whatever cp_parser_lookup_name() found.  */
3003             decl = objc_lookup_ivar (decl, id_expression);
3004
3005             /* If name lookup gives us a SCOPE_REF, then the
3006                qualifying scope was dependent.  Just propagate the
3007                name.  */
3008             if (TREE_CODE (decl) == SCOPE_REF)
3009               {
3010                 if (TYPE_P (TREE_OPERAND (decl, 0)))
3011                   *qualifying_class = TREE_OPERAND (decl, 0);
3012                 return decl;
3013               }
3014             /* Check to see if DECL is a local variable in a context
3015                where that is forbidden.  */
3016             if (parser->local_variables_forbidden_p
3017                 && local_variable_p (decl))
3018               {
3019                 /* It might be that we only found DECL because we are
3020                    trying to be generous with pre-ISO scoping rules.
3021                    For example, consider:
3022
3023                      int i;
3024                      void g() {
3025                        for (int i = 0; i < 10; ++i) {}
3026                        extern void f(int j = i);
3027                      }
3028
3029                    Here, name look up will originally find the out
3030                    of scope `i'.  We need to issue a warning message,
3031                    but then use the global `i'.  */
3032                 decl = check_for_out_of_scope_variable (decl);
3033                 if (local_variable_p (decl))
3034                   {
3035                     error ("local variable %qD may not appear in this context",
3036                            decl);
3037                     return error_mark_node;
3038                   }
3039               }
3040           }
3041
3042         decl = finish_id_expression (id_expression, decl, parser->scope,
3043                                      idk, qualifying_class,
3044                                      parser->integral_constant_expression_p,
3045                                      parser->allow_non_integral_constant_expression_p,
3046                                      &parser->non_integral_constant_expression_p,
3047                                      &error_msg);
3048         if (error_msg)
3049           cp_parser_error (parser, error_msg);
3050         return decl;
3051       }
3052
3053       /* Anything else is an error.  */
3054     default:
3055       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3056       if (c_dialect_objc ()
3057           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3058         return cp_parser_objc_expression (parser);
3059
3060       cp_parser_error (parser, "expected primary-expression");
3061       return error_mark_node;
3062     }
3063 }
3064
3065 /* Parse an id-expression.
3066
3067    id-expression:
3068      unqualified-id
3069      qualified-id
3070
3071    qualified-id:
3072      :: [opt] nested-name-specifier template [opt] unqualified-id
3073      :: identifier
3074      :: operator-function-id
3075      :: template-id
3076
3077    Return a representation of the unqualified portion of the
3078    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3079    a `::' or nested-name-specifier.
3080
3081    Often, if the id-expression was a qualified-id, the caller will
3082    want to make a SCOPE_REF to represent the qualified-id.  This
3083    function does not do this in order to avoid wastefully creating
3084    SCOPE_REFs when they are not required.
3085
3086    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3087    `template' keyword.
3088
3089    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3090    uninstantiated templates.
3091
3092    If *TEMPLATE_P is non-NULL, it is set to true iff the
3093    `template' keyword is used to explicitly indicate that the entity
3094    named is a template.
3095
3096    If DECLARATOR_P is true, the id-expression is appearing as part of
3097    a declarator, rather than as part of an expression.  */
3098
3099 static tree
3100 cp_parser_id_expression (cp_parser *parser,
3101                          bool template_keyword_p,
3102                          bool check_dependency_p,
3103                          bool *template_p,
3104                          bool declarator_p)
3105 {
3106   bool global_scope_p;
3107   bool nested_name_specifier_p;
3108
3109   /* Assume the `template' keyword was not used.  */
3110   if (template_p)
3111     *template_p = false;
3112
3113   /* Look for the optional `::' operator.  */
3114   global_scope_p
3115     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3116        != NULL_TREE);
3117   /* Look for the optional nested-name-specifier.  */
3118   nested_name_specifier_p
3119     = (cp_parser_nested_name_specifier_opt (parser,
3120                                             /*typename_keyword_p=*/false,
3121                                             check_dependency_p,
3122                                             /*type_p=*/false,
3123                                             declarator_p)
3124        != NULL_TREE);
3125   /* If there is a nested-name-specifier, then we are looking at
3126      the first qualified-id production.  */
3127   if (nested_name_specifier_p)
3128     {
3129       tree saved_scope;
3130       tree saved_object_scope;
3131       tree saved_qualifying_scope;
3132       tree unqualified_id;
3133       bool is_template;
3134
3135       /* See if the next token is the `template' keyword.  */
3136       if (!template_p)
3137         template_p = &is_template;
3138       *template_p = cp_parser_optional_template_keyword (parser);
3139       /* Name lookup we do during the processing of the
3140          unqualified-id might obliterate SCOPE.  */
3141       saved_scope = parser->scope;
3142       saved_object_scope = parser->object_scope;
3143       saved_qualifying_scope = parser->qualifying_scope;
3144       /* Process the final unqualified-id.  */
3145       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3146                                                  check_dependency_p,
3147                                                  declarator_p);
3148       /* Restore the SAVED_SCOPE for our caller.  */
3149       parser->scope = saved_scope;
3150       parser->object_scope = saved_object_scope;
3151       parser->qualifying_scope = saved_qualifying_scope;
3152
3153       return unqualified_id;
3154     }
3155   /* Otherwise, if we are in global scope, then we are looking at one
3156      of the other qualified-id productions.  */
3157   else if (global_scope_p)
3158     {
3159       cp_token *token;
3160       tree id;
3161
3162       /* Peek at the next token.  */
3163       token = cp_lexer_peek_token (parser->lexer);
3164
3165       /* If it's an identifier, and the next token is not a "<", then
3166          we can avoid the template-id case.  This is an optimization
3167          for this common case.  */
3168       if (token->type == CPP_NAME
3169           && !cp_parser_nth_token_starts_template_argument_list_p
3170                (parser, 2))
3171         return cp_parser_identifier (parser);
3172
3173       cp_parser_parse_tentatively (parser);
3174       /* Try a template-id.  */
3175       id = cp_parser_template_id (parser,
3176                                   /*template_keyword_p=*/false,
3177                                   /*check_dependency_p=*/true,
3178                                   declarator_p);
3179       /* If that worked, we're done.  */
3180       if (cp_parser_parse_definitely (parser))
3181         return id;
3182
3183       /* Peek at the next token.  (Changes in the token buffer may
3184          have invalidated the pointer obtained above.)  */
3185       token = cp_lexer_peek_token (parser->lexer);
3186
3187       switch (token->type)
3188         {
3189         case CPP_NAME:
3190           return cp_parser_identifier (parser);
3191
3192         case CPP_KEYWORD:
3193           if (token->keyword == RID_OPERATOR)
3194             return cp_parser_operator_function_id (parser);
3195           /* Fall through.  */
3196
3197         default:
3198           cp_parser_error (parser, "expected id-expression");
3199           return error_mark_node;
3200         }
3201     }
3202   else
3203     return cp_parser_unqualified_id (parser, template_keyword_p,
3204                                      /*check_dependency_p=*/true,
3205                                      declarator_p);
3206 }
3207
3208 /* Parse an unqualified-id.
3209
3210    unqualified-id:
3211      identifier
3212      operator-function-id
3213      conversion-function-id
3214      ~ class-name
3215      template-id
3216
3217    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3218    keyword, in a construct like `A::template ...'.
3219
3220    Returns a representation of unqualified-id.  For the `identifier'
3221    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3222    production a BIT_NOT_EXPR is returned; the operand of the
3223    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3224    other productions, see the documentation accompanying the
3225    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3226    names are looked up in uninstantiated templates.  If DECLARATOR_P
3227    is true, the unqualified-id is appearing as part of a declarator,
3228    rather than as part of an expression.  */
3229
3230 static tree
3231 cp_parser_unqualified_id (cp_parser* parser,
3232                           bool template_keyword_p,
3233                           bool check_dependency_p,
3234                           bool declarator_p)
3235 {
3236   cp_token *token;
3237
3238   /* Peek at the next token.  */
3239   token = cp_lexer_peek_token (parser->lexer);
3240
3241   switch (token->type)
3242     {
3243     case CPP_NAME:
3244       {
3245         tree id;
3246
3247         /* We don't know yet whether or not this will be a
3248            template-id.  */
3249         cp_parser_parse_tentatively (parser);
3250         /* Try a template-id.  */
3251         id = cp_parser_template_id (parser, template_keyword_p,
3252                                     check_dependency_p,
3253                                     declarator_p);
3254         /* If it worked, we're done.  */
3255         if (cp_parser_parse_definitely (parser))
3256           return id;
3257         /* Otherwise, it's an ordinary identifier.  */
3258         return cp_parser_identifier (parser);
3259       }
3260
3261     case CPP_TEMPLATE_ID:
3262       return cp_parser_template_id (parser, template_keyword_p,
3263                                     check_dependency_p,
3264                                     declarator_p);
3265
3266     case CPP_COMPL:
3267       {
3268         tree type_decl;
3269         tree qualifying_scope;
3270         tree object_scope;
3271         tree scope;
3272         bool done;
3273
3274         /* Consume the `~' token.  */
3275         cp_lexer_consume_token (parser->lexer);
3276         /* Parse the class-name.  The standard, as written, seems to
3277            say that:
3278
3279              template <typename T> struct S { ~S (); };
3280              template <typename T> S<T>::~S() {}
3281
3282            is invalid, since `~' must be followed by a class-name, but
3283            `S<T>' is dependent, and so not known to be a class.
3284            That's not right; we need to look in uninstantiated
3285            templates.  A further complication arises from:
3286
3287              template <typename T> void f(T t) {
3288                t.T::~T();
3289              }
3290
3291            Here, it is not possible to look up `T' in the scope of `T'
3292            itself.  We must look in both the current scope, and the
3293            scope of the containing complete expression.
3294
3295            Yet another issue is:
3296
3297              struct S {
3298                int S;
3299                ~S();
3300              };
3301
3302              S::~S() {}
3303
3304            The standard does not seem to say that the `S' in `~S'
3305            should refer to the type `S' and not the data member
3306            `S::S'.  */
3307
3308         /* DR 244 says that we look up the name after the "~" in the
3309            same scope as we looked up the qualifying name.  That idea
3310            isn't fully worked out; it's more complicated than that.  */
3311         scope = parser->scope;
3312         object_scope = parser->object_scope;
3313         qualifying_scope = parser->qualifying_scope;
3314
3315         /* If the name is of the form "X::~X" it's OK.  */
3316         if (scope && TYPE_P (scope)
3317             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3318             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3319                 == CPP_OPEN_PAREN)
3320             && (cp_lexer_peek_token (parser->lexer)->value
3321                 == TYPE_IDENTIFIER (scope)))
3322           {
3323             cp_lexer_consume_token (parser->lexer);
3324             return build_nt (BIT_NOT_EXPR, scope);
3325           }
3326
3327         /* If there was an explicit qualification (S::~T), first look
3328            in the scope given by the qualification (i.e., S).  */
3329         done = false;
3330         type_decl = NULL_TREE;
3331         if (scope)
3332           {
3333             cp_parser_parse_tentatively (parser);
3334             type_decl = cp_parser_class_name (parser,
3335                                               /*typename_keyword_p=*/false,
3336                                               /*template_keyword_p=*/false,
3337                                               none_type,
3338                                               /*check_dependency=*/false,
3339                                               /*class_head_p=*/false,
3340                                               declarator_p);
3341             if (cp_parser_parse_definitely (parser))
3342               done = true;
3343           }
3344         /* In "N::S::~S", look in "N" as well.  */
3345         if (!done && scope && qualifying_scope)
3346           {
3347             cp_parser_parse_tentatively (parser);
3348             parser->scope = qualifying_scope;
3349             parser->object_scope = NULL_TREE;
3350             parser->qualifying_scope = NULL_TREE;
3351             type_decl
3352               = cp_parser_class_name (parser,
3353                                       /*typename_keyword_p=*/false,
3354                                       /*template_keyword_p=*/false,
3355                                       none_type,
3356                                       /*check_dependency=*/false,
3357                                       /*class_head_p=*/false,
3358                                       declarator_p);
3359             if (cp_parser_parse_definitely (parser))
3360               done = true;
3361           }
3362         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3363         else if (!done && object_scope)
3364           {
3365             cp_parser_parse_tentatively (parser);
3366             parser->scope = object_scope;
3367             parser->object_scope = NULL_TREE;
3368             parser->qualifying_scope = NULL_TREE;
3369             type_decl
3370               = cp_parser_class_name (parser,
3371                                       /*typename_keyword_p=*/false,
3372                                       /*template_keyword_p=*/false,
3373                                       none_type,
3374                                       /*check_dependency=*/false,
3375                                       /*class_head_p=*/false,
3376                                       declarator_p);
3377             if (cp_parser_parse_definitely (parser))
3378               done = true;
3379           }
3380         /* Look in the surrounding context.  */
3381         if (!done)
3382           {
3383             parser->scope = NULL_TREE;
3384             parser->object_scope = NULL_TREE;
3385             parser->qualifying_scope = NULL_TREE;
3386             type_decl
3387               = cp_parser_class_name (parser,
3388                                       /*typename_keyword_p=*/false,
3389                                       /*template_keyword_p=*/false,
3390                                       none_type,
3391                                       /*check_dependency=*/false,
3392                                       /*class_head_p=*/false,
3393                                       declarator_p);
3394           }
3395         /* If an error occurred, assume that the name of the
3396            destructor is the same as the name of the qualifying
3397            class.  That allows us to keep parsing after running
3398            into ill-formed destructor names.  */
3399         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3400           return build_nt (BIT_NOT_EXPR, scope);
3401         else if (type_decl == error_mark_node)
3402           return error_mark_node;
3403
3404         /* [class.dtor]
3405
3406            A typedef-name that names a class shall not be used as the
3407            identifier in the declarator for a destructor declaration.  */
3408         if (declarator_p
3409             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3410             && !DECL_SELF_REFERENCE_P (type_decl)
3411             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3412           error ("typedef-name %qD used as destructor declarator",
3413                  type_decl);
3414
3415         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3416       }
3417
3418     case CPP_KEYWORD:
3419       if (token->keyword == RID_OPERATOR)
3420         {
3421           tree id;
3422
3423           /* This could be a template-id, so we try that first.  */
3424           cp_parser_parse_tentatively (parser);
3425           /* Try a template-id.  */
3426           id = cp_parser_template_id (parser, template_keyword_p,
3427                                       /*check_dependency_p=*/true,
3428                                       declarator_p);
3429           /* If that worked, we're done.  */
3430           if (cp_parser_parse_definitely (parser))
3431             return id;
3432           /* We still don't know whether we're looking at an
3433              operator-function-id or a conversion-function-id.  */
3434           cp_parser_parse_tentatively (parser);
3435           /* Try an operator-function-id.  */
3436           id = cp_parser_operator_function_id (parser);
3437           /* If that didn't work, try a conversion-function-id.  */
3438           if (!cp_parser_parse_definitely (parser))
3439             id = cp_parser_conversion_function_id (parser);
3440
3441           return id;
3442         }
3443       /* Fall through.  */
3444
3445     default:
3446       cp_parser_error (parser, "expected unqualified-id");
3447       return error_mark_node;
3448     }
3449 }
3450
3451 /* Parse an (optional) nested-name-specifier.
3452
3453    nested-name-specifier:
3454      class-or-namespace-name :: nested-name-specifier [opt]
3455      class-or-namespace-name :: template nested-name-specifier [opt]
3456
3457    PARSER->SCOPE should be set appropriately before this function is
3458    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3459    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3460    in name lookups.
3461
3462    Sets PARSER->SCOPE to the class (TYPE) or namespace
3463    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3464    it unchanged if there is no nested-name-specifier.  Returns the new
3465    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3466
3467    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3468    part of a declaration and/or decl-specifier.  */
3469
3470 static tree
3471 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3472                                      bool typename_keyword_p,
3473                                      bool check_dependency_p,
3474                                      bool type_p,
3475                                      bool is_declaration)
3476 {
3477   bool success = false;
3478   tree access_check = NULL_TREE;
3479   cp_token_position start = 0;
3480   cp_token *token;
3481
3482   /* If the next token corresponds to a nested name specifier, there
3483      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3484      false, it may have been true before, in which case something
3485      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3486      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3487      CHECK_DEPENDENCY_P is false, we have to fall through into the
3488      main loop.  */
3489   if (check_dependency_p
3490       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3491     {
3492       cp_parser_pre_parsed_nested_name_specifier (parser);
3493       return parser->scope;
3494     }
3495
3496   /* Remember where the nested-name-specifier starts.  */
3497   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3498     start = cp_lexer_token_position (parser->lexer, false);
3499
3500   push_deferring_access_checks (dk_deferred);
3501
3502   while (true)
3503     {
3504       tree new_scope;
3505       tree old_scope;
3506       tree saved_qualifying_scope;
3507       bool template_keyword_p;
3508
3509       /* Spot cases that cannot be the beginning of a
3510          nested-name-specifier.  */
3511       token = cp_lexer_peek_token (parser->lexer);
3512
3513       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3514          the already parsed nested-name-specifier.  */
3515       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3516         {
3517           /* Grab the nested-name-specifier and continue the loop.  */
3518           cp_parser_pre_parsed_nested_name_specifier (parser);
3519           success = true;
3520           continue;
3521         }
3522
3523       /* Spot cases that cannot be the beginning of a
3524          nested-name-specifier.  On the second and subsequent times
3525          through the loop, we look for the `template' keyword.  */
3526       if (success && token->keyword == RID_TEMPLATE)
3527         ;
3528       /* A template-id can start a nested-name-specifier.  */
3529       else if (token->type == CPP_TEMPLATE_ID)
3530         ;
3531       else
3532         {
3533           /* If the next token is not an identifier, then it is
3534              definitely not a class-or-namespace-name.  */
3535           if (token->type != CPP_NAME)
3536             break;
3537           /* If the following token is neither a `<' (to begin a
3538              template-id), nor a `::', then we are not looking at a
3539              nested-name-specifier.  */
3540           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3541           if (token->type != CPP_SCOPE
3542               && !cp_parser_nth_token_starts_template_argument_list_p
3543                   (parser, 2))
3544             break;
3545         }
3546
3547       /* The nested-name-specifier is optional, so we parse
3548          tentatively.  */
3549       cp_parser_parse_tentatively (parser);
3550
3551       /* Look for the optional `template' keyword, if this isn't the
3552          first time through the loop.  */
3553       if (success)
3554         template_keyword_p = cp_parser_optional_template_keyword (parser);
3555       else
3556         template_keyword_p = false;
3557
3558       /* Save the old scope since the name lookup we are about to do
3559          might destroy it.  */
3560       old_scope = parser->scope;
3561       saved_qualifying_scope = parser->qualifying_scope;
3562       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3563          look up names in "X<T>::I" in order to determine that "Y" is
3564          a template.  So, if we have a typename at this point, we make
3565          an effort to look through it.  */
3566       if (is_declaration
3567           && !typename_keyword_p
3568           && parser->scope
3569           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3570         parser->scope = resolve_typename_type (parser->scope,
3571                                                /*only_current_p=*/false);
3572       /* Parse the qualifying entity.  */
3573       new_scope
3574         = cp_parser_class_or_namespace_name (parser,
3575                                              typename_keyword_p,
3576                                              template_keyword_p,
3577                                              check_dependency_p,
3578                                              type_p,
3579                                              is_declaration);
3580       /* Look for the `::' token.  */
3581       cp_parser_require (parser, CPP_SCOPE, "`::'");
3582
3583       /* If we found what we wanted, we keep going; otherwise, we're
3584          done.  */
3585       if (!cp_parser_parse_definitely (parser))
3586         {
3587           bool error_p = false;
3588
3589           /* Restore the OLD_SCOPE since it was valid before the
3590              failed attempt at finding the last
3591              class-or-namespace-name.  */
3592           parser->scope = old_scope;
3593           parser->qualifying_scope = saved_qualifying_scope;
3594           /* If the next token is an identifier, and the one after
3595              that is a `::', then any valid interpretation would have
3596              found a class-or-namespace-name.  */
3597           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3598                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3599                      == CPP_SCOPE)
3600                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3601                      != CPP_COMPL))
3602             {
3603               token = cp_lexer_consume_token (parser->lexer);
3604               if (!error_p)
3605                 {
3606                   tree decl;
3607
3608                   decl = cp_parser_lookup_name_simple (parser, token->value);
3609                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3610                     error ("%qD used without template parameters", decl);
3611                   else
3612                     cp_parser_name_lookup_error
3613                       (parser, token->value, decl,
3614                        "is not a class or namespace");
3615                   parser->scope = NULL_TREE;
3616                   error_p = true;
3617                   /* Treat this as a successful nested-name-specifier
3618                      due to:
3619
3620                      [basic.lookup.qual]
3621
3622                      If the name found is not a class-name (clause
3623                      _class_) or namespace-name (_namespace.def_), the
3624                      program is ill-formed.  */
3625                   success = true;
3626                 }
3627               cp_lexer_consume_token (parser->lexer);
3628             }
3629           break;
3630         }
3631
3632       /* We've found one valid nested-name-specifier.  */
3633       success = true;
3634       /* Make sure we look in the right scope the next time through
3635          the loop.  */
3636       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3637                        ? TREE_TYPE (new_scope)
3638                        : new_scope);
3639       /* If it is a class scope, try to complete it; we are about to
3640          be looking up names inside the class.  */
3641       if (TYPE_P (parser->scope)
3642           /* Since checking types for dependency can be expensive,
3643              avoid doing it if the type is already complete.  */
3644           && !COMPLETE_TYPE_P (parser->scope)
3645           /* Do not try to complete dependent types.  */
3646           && !dependent_type_p (parser->scope))
3647         complete_type (parser->scope);
3648     }
3649
3650   /* Retrieve any deferred checks.  Do not pop this access checks yet
3651      so the memory will not be reclaimed during token replacing below.  */
3652   access_check = get_deferred_access_checks ();
3653
3654   /* If parsing tentatively, replace the sequence of tokens that makes
3655      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3656      token.  That way, should we re-parse the token stream, we will
3657      not have to repeat the effort required to do the parse, nor will
3658      we issue duplicate error messages.  */
3659   if (success && start)
3660     {
3661       cp_token *token = cp_lexer_token_at (parser->lexer, start);
3662
3663       /* Reset the contents of the START token.  */
3664       token->type = CPP_NESTED_NAME_SPECIFIER;
3665       token->value = build_tree_list (access_check, parser->scope);
3666       TREE_TYPE (token->value) = parser->qualifying_scope;
3667       token->keyword = RID_MAX;
3668
3669       /* Purge all subsequent tokens.  */
3670       cp_lexer_purge_tokens_after (parser->lexer, start);
3671     }
3672
3673   pop_deferring_access_checks ();
3674   return success ? parser->scope : NULL_TREE;
3675 }
3676
3677 /* Parse a nested-name-specifier.  See
3678    cp_parser_nested_name_specifier_opt for details.  This function
3679    behaves identically, except that it will an issue an error if no
3680    nested-name-specifier is present.  */
3681
3682 static tree
3683 cp_parser_nested_name_specifier (cp_parser *parser,
3684                                  bool typename_keyword_p,
3685                                  bool check_dependency_p,
3686                                  bool type_p,
3687                                  bool is_declaration)
3688 {
3689   tree scope;
3690
3691   /* Look for the nested-name-specifier.  */
3692   scope = cp_parser_nested_name_specifier_opt (parser,
3693                                                typename_keyword_p,
3694                                                check_dependency_p,
3695                                                type_p,
3696                                                is_declaration);
3697   /* If it was not present, issue an error message.  */
3698   if (!scope)
3699     {
3700       cp_parser_error (parser, "expected nested-name-specifier");
3701       parser->scope = NULL_TREE;
3702     }
3703
3704   return scope;
3705 }
3706
3707 /* Parse a class-or-namespace-name.
3708
3709    class-or-namespace-name:
3710      class-name
3711      namespace-name
3712
3713    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3714    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3715    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3716    TYPE_P is TRUE iff the next name should be taken as a class-name,
3717    even the same name is declared to be another entity in the same
3718    scope.
3719
3720    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3721    specified by the class-or-namespace-name.  If neither is found the
3722    ERROR_MARK_NODE is returned.  */
3723
3724 static tree
3725 cp_parser_class_or_namespace_name (cp_parser *parser,
3726                                    bool typename_keyword_p,
3727                                    bool template_keyword_p,
3728                                    bool check_dependency_p,
3729                                    bool type_p,
3730                                    bool is_declaration)
3731 {
3732   tree saved_scope;
3733   tree saved_qualifying_scope;
3734   tree saved_object_scope;
3735   tree scope;
3736   bool only_class_p;
3737
3738   /* Before we try to parse the class-name, we must save away the
3739      current PARSER->SCOPE since cp_parser_class_name will destroy
3740      it.  */
3741   saved_scope = parser->scope;
3742   saved_qualifying_scope = parser->qualifying_scope;
3743   saved_object_scope = parser->object_scope;
3744   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3745      there is no need to look for a namespace-name.  */
3746   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3747   if (!only_class_p)
3748     cp_parser_parse_tentatively (parser);
3749   scope = cp_parser_class_name (parser,
3750                                 typename_keyword_p,
3751                                 template_keyword_p,
3752                                 type_p ? class_type : none_type,
3753                                 check_dependency_p,
3754                                 /*class_head_p=*/false,
3755                                 is_declaration);
3756   /* If that didn't work, try for a namespace-name.  */
3757   if (!only_class_p && !cp_parser_parse_definitely (parser))
3758     {
3759       /* Restore the saved scope.  */
3760       parser->scope = saved_scope;
3761       parser->qualifying_scope = saved_qualifying_scope;
3762       parser->object_scope = saved_object_scope;
3763       /* If we are not looking at an identifier followed by the scope
3764          resolution operator, then this is not part of a
3765          nested-name-specifier.  (Note that this function is only used
3766          to parse the components of a nested-name-specifier.)  */
3767       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3768           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3769         return error_mark_node;
3770       scope = cp_parser_namespace_name (parser);
3771     }
3772
3773   return scope;
3774 }
3775
3776 /* Parse a postfix-expression.
3777
3778    postfix-expression:
3779      primary-expression
3780      postfix-expression [ expression ]
3781      postfix-expression ( expression-list [opt] )
3782      simple-type-specifier ( expression-list [opt] )
3783      typename :: [opt] nested-name-specifier identifier
3784        ( expression-list [opt] )
3785      typename :: [opt] nested-name-specifier template [opt] template-id
3786        ( expression-list [opt] )
3787      postfix-expression . template [opt] id-expression
3788      postfix-expression -> template [opt] id-expression
3789      postfix-expression . pseudo-destructor-name
3790      postfix-expression -> pseudo-destructor-name
3791      postfix-expression ++
3792      postfix-expression --
3793      dynamic_cast < type-id > ( expression )
3794      static_cast < type-id > ( expression )
3795      reinterpret_cast < type-id > ( expression )
3796      const_cast < type-id > ( expression )
3797      typeid ( expression )
3798      typeid ( type-id )
3799
3800    GNU Extension:
3801
3802    postfix-expression:
3803      ( type-id ) { initializer-list , [opt] }
3804
3805    This extension is a GNU version of the C99 compound-literal
3806    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3807    but they are essentially the same concept.)
3808
3809    If ADDRESS_P is true, the postfix expression is the operand of the
3810    `&' operator.  CAST_P is true if this expression is the target of a
3811    cast.
3812
3813    Returns a representation of the expression.  */
3814
3815 static tree
3816 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3817 {
3818   cp_token *token;
3819   enum rid keyword;
3820   cp_id_kind idk = CP_ID_KIND_NONE;
3821   tree postfix_expression = NULL_TREE;
3822   /* Non-NULL only if the current postfix-expression can be used to
3823      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3824      class used to qualify the member.  */
3825   tree qualifying_class = NULL_TREE;
3826
3827   /* Peek at the next token.  */
3828   token = cp_lexer_peek_token (parser->lexer);
3829   /* Some of the productions are determined by keywords.  */
3830   keyword = token->keyword;
3831   switch (keyword)
3832     {
3833     case RID_DYNCAST:
3834     case RID_STATCAST:
3835     case RID_REINTCAST:
3836     case RID_CONSTCAST:
3837       {
3838         tree type;
3839         tree expression;
3840         const char *saved_message;
3841
3842         /* All of these can be handled in the same way from the point
3843            of view of parsing.  Begin by consuming the token
3844            identifying the cast.  */
3845         cp_lexer_consume_token (parser->lexer);
3846
3847         /* New types cannot be defined in the cast.  */
3848         saved_message = parser->type_definition_forbidden_message;
3849         parser->type_definition_forbidden_message
3850           = "types may not be defined in casts";
3851
3852         /* Look for the opening `<'.  */
3853         cp_parser_require (parser, CPP_LESS, "`<'");
3854         /* Parse the type to which we are casting.  */
3855         type = cp_parser_type_id (parser);
3856         /* Look for the closing `>'.  */
3857         cp_parser_require (parser, CPP_GREATER, "`>'");
3858         /* Restore the old message.  */
3859         parser->type_definition_forbidden_message = saved_message;
3860
3861         /* And the expression which is being cast.  */
3862         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3863         expression = cp_parser_expression (parser, /*cast_p=*/true);
3864         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3865
3866         /* Only type conversions to integral or enumeration types
3867            can be used in constant-expressions.  */
3868         if (parser->integral_constant_expression_p
3869             && !dependent_type_p (type)
3870             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3871             && (cp_parser_non_integral_constant_expression
3872                 (parser,
3873                  "a cast to a type other than an integral or "
3874                  "enumeration type")))
3875           return error_mark_node;
3876
3877         switch (keyword)
3878           {
3879           case RID_DYNCAST:
3880             postfix_expression
3881               = build_dynamic_cast (type, expression);
3882             break;
3883           case RID_STATCAST:
3884             postfix_expression
3885               = build_static_cast (type, expression);
3886             break;
3887           case RID_REINTCAST:
3888             postfix_expression
3889               = build_reinterpret_cast (type, expression);
3890             break;
3891           case RID_CONSTCAST:
3892             postfix_expression
3893               = build_const_cast (type, expression);
3894             break;
3895           default:
3896             gcc_unreachable ();
3897           }
3898       }
3899       break;
3900
3901     case RID_TYPEID:
3902       {
3903         tree type;
3904         const char *saved_message;
3905         bool saved_in_type_id_in_expr_p;
3906
3907         /* Consume the `typeid' token.  */
3908         cp_lexer_consume_token (parser->lexer);
3909         /* Look for the `(' token.  */
3910         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3911         /* Types cannot be defined in a `typeid' expression.  */
3912         saved_message = parser->type_definition_forbidden_message;
3913         parser->type_definition_forbidden_message
3914           = "types may not be defined in a `typeid\' expression";
3915         /* We can't be sure yet whether we're looking at a type-id or an
3916            expression.  */
3917         cp_parser_parse_tentatively (parser);
3918         /* Try a type-id first.  */
3919         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3920         parser->in_type_id_in_expr_p = true;
3921         type = cp_parser_type_id (parser);
3922         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3923         /* Look for the `)' token.  Otherwise, we can't be sure that
3924            we're not looking at an expression: consider `typeid (int
3925            (3))', for example.  */
3926         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3927         /* If all went well, simply lookup the type-id.  */
3928         if (cp_parser_parse_definitely (parser))
3929           postfix_expression = get_typeid (type);
3930         /* Otherwise, fall back to the expression variant.  */
3931         else
3932           {
3933             tree expression;
3934
3935             /* Look for an expression.  */
3936             expression = cp_parser_expression (parser, /*cast_p=*/false);
3937             /* Compute its typeid.  */
3938             postfix_expression = build_typeid (expression);
3939             /* Look for the `)' token.  */
3940             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3941           }
3942         /* `typeid' may not appear in an integral constant expression.  */
3943         if (cp_parser_non_integral_constant_expression(parser,
3944                                                        "`typeid' operator"))
3945           return error_mark_node;
3946         /* Restore the saved message.  */
3947         parser->type_definition_forbidden_message = saved_message;
3948       }
3949       break;
3950
3951     case RID_TYPENAME:
3952       {
3953         bool template_p = false;
3954         tree id;
3955         tree type;
3956         tree scope;
3957
3958         /* Consume the `typename' token.  */
3959         cp_lexer_consume_token (parser->lexer);
3960         /* Look for the optional `::' operator.  */
3961         cp_parser_global_scope_opt (parser,
3962                                     /*current_scope_valid_p=*/false);
3963         /* Look for the nested-name-specifier.  In case of error here,
3964            consume the trailing id to avoid subsequent error messages
3965            for usual cases.  */
3966         scope = cp_parser_nested_name_specifier (parser,
3967                                                  /*typename_keyword_p=*/true,
3968                                                  /*check_dependency_p=*/true,
3969                                                  /*type_p=*/true,
3970                                                  /*is_declaration=*/true);
3971
3972         /* Look for the optional `template' keyword.  */
3973         template_p = cp_parser_optional_template_keyword (parser);
3974         /* We don't know whether we're looking at a template-id or an
3975            identifier.  */
3976         cp_parser_parse_tentatively (parser);
3977         /* Try a template-id.  */
3978         id = cp_parser_template_id (parser, template_p,
3979                                     /*check_dependency_p=*/true,
3980                                     /*is_declaration=*/true);
3981         /* If that didn't work, try an identifier.  */
3982         if (!cp_parser_parse_definitely (parser))
3983           id = cp_parser_identifier (parser);
3984
3985         /* Don't process id if nested name specifier is invalid.  */
3986         if (!scope || scope == error_mark_node)
3987           return error_mark_node;
3988         /* If we look up a template-id in a non-dependent qualifying
3989            scope, there's no need to create a dependent type.  */
3990         else if (TREE_CODE (id) == TYPE_DECL
3991             && !dependent_type_p (parser->scope))
3992           type = TREE_TYPE (id);
3993         /* Create a TYPENAME_TYPE to represent the type to which the
3994            functional cast is being performed.  */
3995         else
3996           type = make_typename_type (parser->scope, id,
3997                                      typename_type,
3998                                      /*complain=*/1);
3999
4000         postfix_expression = cp_parser_functional_cast (parser, type);
4001       }
4002       break;
4003
4004     default:
4005       {
4006         tree type;
4007
4008         /* If the next thing is a simple-type-specifier, we may be
4009            looking at a functional cast.  We could also be looking at
4010            an id-expression.  So, we try the functional cast, and if
4011            that doesn't work we fall back to the primary-expression.  */
4012         cp_parser_parse_tentatively (parser);
4013         /* Look for the simple-type-specifier.  */
4014         type = cp_parser_simple_type_specifier (parser,
4015                                                 /*decl_specs=*/NULL,
4016                                                 CP_PARSER_FLAGS_NONE);
4017         /* Parse the cast itself.  */
4018         if (!cp_parser_error_occurred (parser))
4019           postfix_expression
4020             = cp_parser_functional_cast (parser, type);
4021         /* If that worked, we're done.  */
4022         if (cp_parser_parse_definitely (parser))
4023           break;
4024
4025         /* If the functional-cast didn't work out, try a
4026            compound-literal.  */
4027         if (cp_parser_allow_gnu_extensions_p (parser)
4028             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4029           {
4030             VEC(constructor_elt,gc) *initializer_list = NULL;
4031             bool saved_in_type_id_in_expr_p;
4032
4033             cp_parser_parse_tentatively (parser);
4034             /* Consume the `('.  */
4035             cp_lexer_consume_token (parser->lexer);
4036             /* Parse the type.  */
4037             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4038             parser->in_type_id_in_expr_p = true;
4039             type = cp_parser_type_id (parser);
4040             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4041             /* Look for the `)'.  */
4042             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4043             /* Look for the `{'.  */
4044             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4045             /* If things aren't going well, there's no need to
4046                keep going.  */
4047             if (!cp_parser_error_occurred (parser))
4048               {
4049                 bool non_constant_p;
4050                 /* Parse the initializer-list.  */
4051                 initializer_list
4052                   = cp_parser_initializer_list (parser, &non_constant_p);
4053                 /* Allow a trailing `,'.  */
4054                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4055                   cp_lexer_consume_token (parser->lexer);
4056                 /* Look for the final `}'.  */
4057                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4058               }
4059             /* If that worked, we're definitely looking at a
4060                compound-literal expression.  */
4061             if (cp_parser_parse_definitely (parser))
4062               {
4063                 /* Warn the user that a compound literal is not
4064                    allowed in standard C++.  */
4065                 if (pedantic)
4066                   pedwarn ("ISO C++ forbids compound-literals");
4067                 /* Form the representation of the compound-literal.  */
4068                 postfix_expression
4069                   = finish_compound_literal (type, initializer_list);
4070                 break;
4071               }
4072           }
4073
4074         /* It must be a primary-expression.  */
4075         postfix_expression = cp_parser_primary_expression (parser,
4076                                                            cast_p,
4077                                                            &idk,
4078                                                            &qualifying_class);
4079       }
4080       break;
4081     }
4082
4083   /* If we were avoiding committing to the processing of a
4084      qualified-id until we knew whether or not we had a
4085      pointer-to-member, we now know.  */
4086   if (qualifying_class)
4087     {
4088       bool done;
4089
4090       /* Peek at the next token.  */
4091       token = cp_lexer_peek_token (parser->lexer);
4092       done = (token->type != CPP_OPEN_SQUARE
4093               && token->type != CPP_OPEN_PAREN
4094               && token->type != CPP_DOT
4095               && token->type != CPP_DEREF
4096               && token->type != CPP_PLUS_PLUS
4097               && token->type != CPP_MINUS_MINUS);
4098
4099       postfix_expression = finish_qualified_id_expr (qualifying_class,
4100                                                      postfix_expression,
4101                                                      done,
4102                                                      address_p);
4103       if (done)
4104         return postfix_expression;
4105     }
4106
4107   /* Keep looping until the postfix-expression is complete.  */
4108   while (true)
4109     {
4110       if (idk == CP_ID_KIND_UNQUALIFIED
4111           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4112           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4113         /* It is not a Koenig lookup function call.  */
4114         postfix_expression
4115           = unqualified_name_lookup_error (postfix_expression);
4116
4117       /* Peek at the next token.  */
4118       token = cp_lexer_peek_token (parser->lexer);
4119
4120       switch (token->type)
4121         {
4122         case CPP_OPEN_SQUARE:
4123           postfix_expression
4124             = cp_parser_postfix_open_square_expression (parser,
4125                                                         postfix_expression,
4126                                                         false);
4127           idk = CP_ID_KIND_NONE;
4128           break;
4129
4130         case CPP_OPEN_PAREN:
4131           /* postfix-expression ( expression-list [opt] ) */
4132           {
4133             bool koenig_p;
4134             bool is_builtin_constant_p;
4135             bool saved_integral_constant_expression_p = false;
4136             bool saved_non_integral_constant_expression_p = false;
4137             tree args;
4138
4139             is_builtin_constant_p
4140               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4141             if (is_builtin_constant_p)
4142               {
4143                 /* The whole point of __builtin_constant_p is to allow
4144                    non-constant expressions to appear as arguments.  */
4145                 saved_integral_constant_expression_p
4146                   = parser->integral_constant_expression_p;
4147                 saved_non_integral_constant_expression_p
4148                   = parser->non_integral_constant_expression_p;
4149                 parser->integral_constant_expression_p = false;
4150               }
4151             args = (cp_parser_parenthesized_expression_list
4152                     (parser, /*is_attribute_list=*/false,
4153                      /*cast_p=*/false,
4154                      /*non_constant_p=*/NULL));
4155             if (is_builtin_constant_p)
4156               {
4157                 parser->integral_constant_expression_p
4158                   = saved_integral_constant_expression_p;
4159                 parser->non_integral_constant_expression_p
4160                   = saved_non_integral_constant_expression_p;
4161               }
4162
4163             if (args == error_mark_node)
4164               {
4165                 postfix_expression = error_mark_node;
4166                 break;
4167               }
4168
4169             /* Function calls are not permitted in
4170                constant-expressions.  */
4171             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4172                 && cp_parser_non_integral_constant_expression (parser,
4173                                                                "a function call"))
4174               {
4175                 postfix_expression = error_mark_node;
4176                 break;
4177               }
4178
4179             koenig_p = false;
4180             if (idk == CP_ID_KIND_UNQUALIFIED)
4181               {
4182                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4183                   {
4184                     if (args)
4185                       {
4186                         koenig_p = true;
4187                         postfix_expression
4188                           = perform_koenig_lookup (postfix_expression, args);
4189                       }
4190                     else
4191                       postfix_expression
4192                         = unqualified_fn_lookup_error (postfix_expression);
4193                   }
4194                 /* We do not perform argument-dependent lookup if
4195                    normal lookup finds a non-function, in accordance
4196                    with the expected resolution of DR 218.  */
4197                 else if (args && is_overloaded_fn (postfix_expression))
4198                   {
4199                     tree fn = get_first_fn (postfix_expression);
4200
4201                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4202                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4203
4204                     /* Only do argument dependent lookup if regular
4205                        lookup does not find a set of member functions.
4206                        [basic.lookup.koenig]/2a  */
4207                     if (!DECL_FUNCTION_MEMBER_P (fn))
4208                       {
4209                         koenig_p = true;
4210                         postfix_expression
4211                           = perform_koenig_lookup (postfix_expression, args);
4212                       }
4213                   }
4214               }
4215
4216             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4217               {
4218                 tree instance = TREE_OPERAND (postfix_expression, 0);
4219                 tree fn = TREE_OPERAND (postfix_expression, 1);
4220
4221                 if (processing_template_decl
4222                     && (type_dependent_expression_p (instance)
4223                         || (!BASELINK_P (fn)
4224                             && TREE_CODE (fn) != FIELD_DECL)
4225                         || type_dependent_expression_p (fn)
4226                         || any_type_dependent_arguments_p (args)))
4227                   {
4228                     postfix_expression
4229                       = build_min_nt (CALL_EXPR, postfix_expression,
4230                                       args, NULL_TREE);
4231                     break;
4232                   }
4233
4234                 if (BASELINK_P (fn))
4235                   postfix_expression
4236                     = (build_new_method_call
4237                        (instance, fn, args, NULL_TREE,
4238                         (idk == CP_ID_KIND_QUALIFIED
4239                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4240                 else
4241                   postfix_expression
4242                     = finish_call_expr (postfix_expression, args,
4243                                         /*disallow_virtual=*/false,
4244                                         /*koenig_p=*/false);
4245               }
4246             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4247                      || TREE_CODE (postfix_expression) == MEMBER_REF
4248                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4249               postfix_expression = (build_offset_ref_call_from_tree
4250                                     (postfix_expression, args));
4251             else if (idk == CP_ID_KIND_QUALIFIED)
4252               /* A call to a static class member, or a namespace-scope
4253                  function.  */
4254               postfix_expression
4255                 = finish_call_expr (postfix_expression, args,
4256                                     /*disallow_virtual=*/true,
4257                                     koenig_p);
4258             else
4259               /* All other function calls.  */
4260               postfix_expression
4261                 = finish_call_expr (postfix_expression, args,
4262                                     /*disallow_virtual=*/false,
4263                                     koenig_p);
4264
4265             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4266             idk = CP_ID_KIND_NONE;
4267           }
4268           break;
4269
4270         case CPP_DOT:
4271         case CPP_DEREF:
4272           /* postfix-expression . template [opt] id-expression
4273              postfix-expression . pseudo-destructor-name
4274              postfix-expression -> template [opt] id-expression
4275              postfix-expression -> pseudo-destructor-name */
4276
4277           /* Consume the `.' or `->' operator.  */
4278           cp_lexer_consume_token (parser->lexer);
4279
4280           postfix_expression
4281             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4282                                                       postfix_expression,
4283                                                       false, &idk);
4284           break;
4285
4286         case CPP_PLUS_PLUS:
4287           /* postfix-expression ++  */
4288           /* Consume the `++' token.  */
4289           cp_lexer_consume_token (parser->lexer);
4290           /* Generate a representation for the complete expression.  */
4291           postfix_expression
4292             = finish_increment_expr (postfix_expression,
4293                                      POSTINCREMENT_EXPR);
4294           /* Increments may not appear in constant-expressions.  */
4295           if (cp_parser_non_integral_constant_expression (parser,
4296                                                           "an increment"))
4297             postfix_expression = error_mark_node;
4298           idk = CP_ID_KIND_NONE;
4299           break;
4300
4301         case CPP_MINUS_MINUS:
4302           /* postfix-expression -- */
4303           /* Consume the `--' token.  */
4304           cp_lexer_consume_token (parser->lexer);
4305           /* Generate a representation for the complete expression.  */
4306           postfix_expression
4307             = finish_increment_expr (postfix_expression,
4308                                      POSTDECREMENT_EXPR);
4309           /* Decrements may not appear in constant-expressions.  */
4310           if (cp_parser_non_integral_constant_expression (parser,
4311                                                           "a decrement"))
4312             postfix_expression = error_mark_node;
4313           idk = CP_ID_KIND_NONE;
4314           break;
4315
4316         default:
4317           return postfix_expression;
4318         }
4319     }
4320
4321   /* We should never get here.  */
4322   gcc_unreachable ();
4323   return error_mark_node;
4324 }
4325
4326 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4327    by cp_parser_builtin_offsetof.  We're looking for
4328
4329      postfix-expression [ expression ]
4330
4331    FOR_OFFSETOF is set if we're being called in that context, which
4332    changes how we deal with integer constant expressions.  */
4333
4334 static tree
4335 cp_parser_postfix_open_square_expression (cp_parser *parser,
4336                                           tree postfix_expression,
4337                                           bool for_offsetof)
4338 {
4339   tree index;
4340
4341   /* Consume the `[' token.  */
4342   cp_lexer_consume_token (parser->lexer);
4343
4344   /* Parse the index expression.  */
4345   /* ??? For offsetof, there is a question of what to allow here.  If
4346      offsetof is not being used in an integral constant expression context,
4347      then we *could* get the right answer by computing the value at runtime.
4348      If we are in an integral constant expression context, then we might
4349      could accept any constant expression; hard to say without analysis.
4350      Rather than open the barn door too wide right away, allow only integer
4351      constant expressions here.  */
4352   if (for_offsetof)
4353     index = cp_parser_constant_expression (parser, false, NULL);
4354   else
4355     index = cp_parser_expression (parser, /*cast_p=*/false);
4356
4357   /* Look for the closing `]'.  */
4358   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4359
4360   /* Build the ARRAY_REF.  */
4361   postfix_expression = grok_array_decl (postfix_expression, index);
4362
4363   /* When not doing offsetof, array references are not permitted in
4364      constant-expressions.  */
4365   if (!for_offsetof
4366       && (cp_parser_non_integral_constant_expression
4367           (parser, "an array reference")))
4368     postfix_expression = error_mark_node;
4369
4370   return postfix_expression;
4371 }
4372
4373 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4374    by cp_parser_builtin_offsetof.  We're looking for
4375
4376      postfix-expression . template [opt] id-expression
4377      postfix-expression . pseudo-destructor-name
4378      postfix-expression -> template [opt] id-expression
4379      postfix-expression -> pseudo-destructor-name
4380
4381    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4382    limits what of the above we'll actually accept, but nevermind.
4383    TOKEN_TYPE is the "." or "->" token, which will already have been
4384    removed from the stream.  */
4385
4386 static tree
4387 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4388                                         enum cpp_ttype token_type,
4389                                         tree postfix_expression,
4390                                         bool for_offsetof, cp_id_kind *idk)
4391 {
4392   tree name;
4393   bool dependent_p;
4394   bool template_p;
4395   bool pseudo_destructor_p;
4396   tree scope = NULL_TREE;
4397
4398   /* If this is a `->' operator, dereference the pointer.  */
4399   if (token_type == CPP_DEREF)
4400     postfix_expression = build_x_arrow (postfix_expression);
4401   /* Check to see whether or not the expression is type-dependent.  */
4402   dependent_p = type_dependent_expression_p (postfix_expression);
4403   /* The identifier following the `->' or `.' is not qualified.  */
4404   parser->scope = NULL_TREE;
4405   parser->qualifying_scope = NULL_TREE;
4406   parser->object_scope = NULL_TREE;
4407   *idk = CP_ID_KIND_NONE;
4408   /* Enter the scope corresponding to the type of the object
4409      given by the POSTFIX_EXPRESSION.  */
4410   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4411     {
4412       scope = TREE_TYPE (postfix_expression);
4413       /* According to the standard, no expression should ever have
4414          reference type.  Unfortunately, we do not currently match
4415          the standard in this respect in that our internal representation
4416          of an expression may have reference type even when the standard
4417          says it does not.  Therefore, we have to manually obtain the
4418          underlying type here.  */
4419       scope = non_reference (scope);
4420       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4421       scope = complete_type_or_else (scope, NULL_TREE);
4422       /* Let the name lookup machinery know that we are processing a
4423          class member access expression.  */
4424       parser->context->object_type = scope;
4425       /* If something went wrong, we want to be able to discern that case,
4426          as opposed to the case where there was no SCOPE due to the type
4427          of expression being dependent.  */
4428       if (!scope)
4429         scope = error_mark_node;
4430       /* If the SCOPE was erroneous, make the various semantic analysis
4431          functions exit quickly -- and without issuing additional error
4432          messages.  */
4433       if (scope == error_mark_node)
4434         postfix_expression = error_mark_node;
4435     }
4436
4437   /* Assume this expression is not a pseudo-destructor access.  */
4438   pseudo_destructor_p = false;
4439
4440   /* If the SCOPE is a scalar type, then, if this is a valid program,
4441      we must be looking at a pseudo-destructor-name.  */
4442   if (scope && SCALAR_TYPE_P (scope))
4443     {
4444       tree s;
4445       tree type;
4446
4447       cp_parser_parse_tentatively (parser);
4448       /* Parse the pseudo-destructor-name.  */
4449       s = NULL_TREE;
4450       cp_parser_pseudo_destructor_name (parser, &s, &type);
4451       if (cp_parser_parse_definitely (parser))
4452         {
4453           pseudo_destructor_p = true;
4454           postfix_expression
4455             = finish_pseudo_destructor_expr (postfix_expression,
4456                                              s, TREE_TYPE (type));
4457         }
4458     }
4459
4460   if (!pseudo_destructor_p)
4461     {
4462       /* If the SCOPE is not a scalar type, we are looking at an
4463          ordinary class member access expression, rather than a
4464          pseudo-destructor-name.  */
4465       template_p = cp_parser_optional_template_keyword (parser);
4466       /* Parse the id-expression.  */
4467       name = cp_parser_id_expression (parser, template_p,
4468                                       /*check_dependency_p=*/true,
4469                                       /*template_p=*/NULL,
4470                                       /*declarator_p=*/false);
4471       /* In general, build a SCOPE_REF if the member name is qualified.
4472          However, if the name was not dependent and has already been
4473          resolved; there is no need to build the SCOPE_REF.  For example;
4474
4475              struct X { void f(); };
4476              template <typename T> void f(T* t) { t->X::f(); }
4477
4478          Even though "t" is dependent, "X::f" is not and has been resolved
4479          to a BASELINK; there is no need to include scope information.  */
4480
4481       /* But we do need to remember that there was an explicit scope for
4482          virtual function calls.  */
4483       if (parser->scope)
4484         *idk = CP_ID_KIND_QUALIFIED;
4485
4486       /* If the name is a template-id that names a type, we will get a
4487          TYPE_DECL here.  That is invalid code.  */
4488       if (TREE_CODE (name) == TYPE_DECL)
4489         {
4490           error ("invalid use of %qD", name);
4491           postfix_expression = error_mark_node;
4492         }
4493       else
4494         {
4495           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4496             {
4497               name = build_nt (SCOPE_REF, parser->scope, name);
4498               parser->scope = NULL_TREE;
4499               parser->qualifying_scope = NULL_TREE;
4500               parser->object_scope = NULL_TREE;
4501             }
4502           if (scope && name && BASELINK_P (name))
4503             adjust_result_of_qualified_name_lookup
4504               (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4505           postfix_expression
4506             = finish_class_member_access_expr (postfix_expression, name);
4507         }
4508     }
4509
4510   /* We no longer need to look up names in the scope of the object on
4511      the left-hand side of the `.' or `->' operator.  */
4512   parser->context->object_type = NULL_TREE;
4513
4514   /* Outside of offsetof, these operators may not appear in
4515      constant-expressions.  */
4516   if (!for_offsetof
4517       && (cp_parser_non_integral_constant_expression
4518           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4519     postfix_expression = error_mark_node;
4520
4521   return postfix_expression;
4522 }
4523
4524 /* Parse a parenthesized expression-list.
4525
4526    expression-list:
4527      assignment-expression
4528      expression-list, assignment-expression
4529
4530    attribute-list:
4531      expression-list
4532      identifier
4533      identifier, expression-list
4534
4535    CAST_P is true if this expression is the target of a cast.
4536
4537    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4538    representation of an assignment-expression.  Note that a TREE_LIST
4539    is returned even if there is only a single expression in the list.
4540    error_mark_node is returned if the ( and or ) are
4541    missing. NULL_TREE is returned on no expressions. The parentheses
4542    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4543    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4544    indicates whether or not all of the expressions in the list were
4545    constant.  */
4546
4547 static tree
4548 cp_parser_parenthesized_expression_list (cp_parser* parser,
4549                                          bool is_attribute_list,
4550                                          bool cast_p,
4551                                          bool *non_constant_p)
4552 {
4553   tree expression_list = NULL_TREE;
4554   bool fold_expr_p = is_attribute_list;
4555   tree identifier = NULL_TREE;
4556
4557   /* Assume all the expressions will be constant.  */
4558   if (non_constant_p)
4559     *non_constant_p = false;
4560
4561   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4562     return error_mark_node;
4563
4564   /* Consume expressions until there are no more.  */
4565   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4566     while (true)
4567       {
4568         tree expr;
4569
4570         /* At the beginning of attribute lists, check to see if the
4571            next token is an identifier.  */
4572         if (is_attribute_list
4573             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4574           {
4575             cp_token *token;
4576
4577             /* Consume the identifier.  */
4578             token = cp_lexer_consume_token (parser->lexer);
4579             /* Save the identifier.  */
4580             identifier = token->value;
4581           }
4582         else
4583           {
4584             /* Parse the next assignment-expression.  */
4585             if (non_constant_p)
4586               {
4587                 bool expr_non_constant_p;
4588                 expr = (cp_parser_constant_expression
4589                         (parser, /*allow_non_constant_p=*/true,
4590                          &expr_non_constant_p));
4591                 if (expr_non_constant_p)
4592                   *non_constant_p = true;
4593               }
4594             else
4595               expr = cp_parser_assignment_expression (parser, cast_p);
4596
4597             if (fold_expr_p)
4598               expr = fold_non_dependent_expr (expr);
4599
4600              /* Add it to the list.  We add error_mark_node
4601                 expressions to the list, so that we can still tell if
4602                 the correct form for a parenthesized expression-list
4603                 is found. That gives better errors.  */
4604             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4605
4606             if (expr == error_mark_node)
4607               goto skip_comma;
4608           }
4609
4610         /* After the first item, attribute lists look the same as
4611            expression lists.  */
4612         is_attribute_list = false;
4613
4614       get_comma:;
4615         /* If the next token isn't a `,', then we are done.  */
4616         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4617           break;
4618
4619         /* Otherwise, consume the `,' and keep going.  */
4620         cp_lexer_consume_token (parser->lexer);
4621       }
4622
4623   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4624     {
4625       int ending;
4626
4627     skip_comma:;
4628       /* We try and resync to an unnested comma, as that will give the
4629          user better diagnostics.  */
4630       ending = cp_parser_skip_to_closing_parenthesis (parser,
4631                                                       /*recovering=*/true,
4632                                                       /*or_comma=*/true,
4633                                                       /*consume_paren=*/true);
4634       if (ending < 0)
4635         goto get_comma;
4636       if (!ending)
4637         return error_mark_node;
4638     }
4639
4640   /* We built up the list in reverse order so we must reverse it now.  */
4641   expression_list = nreverse (expression_list);
4642   if (identifier)
4643     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4644
4645   return expression_list;
4646 }
4647
4648 /* Parse a pseudo-destructor-name.
4649
4650    pseudo-destructor-name:
4651      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4652      :: [opt] nested-name-specifier template template-id :: ~ type-name
4653      :: [opt] nested-name-specifier [opt] ~ type-name
4654
4655    If either of the first two productions is used, sets *SCOPE to the
4656    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4657    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4658    or ERROR_MARK_NODE if the parse fails.  */
4659
4660 static void
4661 cp_parser_pseudo_destructor_name (cp_parser* parser,
4662                                   tree* scope,
4663                                   tree* type)
4664 {
4665   bool nested_name_specifier_p;
4666
4667   /* Assume that things will not work out.  */
4668   *type = error_mark_node;
4669
4670   /* Look for the optional `::' operator.  */
4671   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4672   /* Look for the optional nested-name-specifier.  */
4673   nested_name_specifier_p
4674     = (cp_parser_nested_name_specifier_opt (parser,
4675                                             /*typename_keyword_p=*/false,
4676                                             /*check_dependency_p=*/true,
4677                                             /*type_p=*/false,
4678                                             /*is_declaration=*/true)
4679        != NULL_TREE);
4680   /* Now, if we saw a nested-name-specifier, we might be doing the
4681      second production.  */
4682   if (nested_name_specifier_p
4683       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4684     {
4685       /* Consume the `template' keyword.  */
4686       cp_lexer_consume_token (parser->lexer);
4687       /* Parse the template-id.  */
4688       cp_parser_template_id (parser,
4689                              /*template_keyword_p=*/true,
4690                              /*check_dependency_p=*/false,
4691                              /*is_declaration=*/true);
4692       /* Look for the `::' token.  */
4693       cp_parser_require (parser, CPP_SCOPE, "`::'");
4694     }
4695   /* If the next token is not a `~', then there might be some
4696      additional qualification.  */
4697   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4698     {
4699       /* Look for the type-name.  */
4700       *scope = TREE_TYPE (cp_parser_type_name (parser));
4701
4702       if (*scope == error_mark_node)
4703         return;
4704
4705       /* If we don't have ::~, then something has gone wrong.  Since
4706          the only caller of this function is looking for something
4707          after `.' or `->' after a scalar type, most likely the
4708          program is trying to get a member of a non-aggregate
4709          type.  */
4710       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4711           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4712         {
4713           cp_parser_error (parser, "request for member of non-aggregate type");
4714           return;
4715         }
4716
4717       /* Look for the `::' token.  */
4718       cp_parser_require (parser, CPP_SCOPE, "`::'");
4719     }
4720   else
4721     *scope = NULL_TREE;
4722
4723   /* Look for the `~'.  */
4724   cp_parser_require (parser, CPP_COMPL, "`~'");
4725   /* Look for the type-name again.  We are not responsible for
4726      checking that it matches the first type-name.  */
4727   *type = cp_parser_type_name (parser);
4728 }
4729
4730 /* Parse a unary-expression.
4731
4732    unary-expression:
4733      postfix-expression
4734      ++ cast-expression
4735      -- cast-expression
4736      unary-operator cast-expression
4737      sizeof unary-expression
4738      sizeof ( type-id )
4739      new-expression
4740      delete-expression
4741
4742    GNU Extensions:
4743
4744    unary-expression:
4745      __extension__ cast-expression
4746      __alignof__ unary-expression
4747      __alignof__ ( type-id )
4748      __real__ cast-expression
4749      __imag__ cast-expression
4750      && identifier
4751
4752    ADDRESS_P is true iff the unary-expression is appearing as the
4753    operand of the `&' operator.   CAST_P is true if this expression is
4754    the target of a cast.
4755
4756    Returns a representation of the expression.  */
4757
4758 static tree
4759 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4760 {
4761   cp_token *token;
4762   enum tree_code unary_operator;
4763
4764   /* Peek at the next token.  */
4765   token = cp_lexer_peek_token (parser->lexer);
4766   /* Some keywords give away the kind of expression.  */
4767   if (token->type == CPP_KEYWORD)
4768     {
4769       enum rid keyword = token->keyword;
4770
4771       switch (keyword)
4772         {
4773         case RID_ALIGNOF:
4774         case RID_SIZEOF:
4775           {
4776             tree operand;
4777             enum tree_code op;
4778
4779             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4780             /* Consume the token.  */
4781             cp_lexer_consume_token (parser->lexer);
4782             /* Parse the operand.  */
4783             operand = cp_parser_sizeof_operand (parser, keyword);
4784
4785             if (TYPE_P (operand))
4786               return cxx_sizeof_or_alignof_type (operand, op, true);
4787             else
4788               return cxx_sizeof_or_alignof_expr (operand, op);
4789           }
4790
4791         case RID_NEW:
4792           return cp_parser_new_expression (parser);
4793
4794         case RID_DELETE:
4795           return cp_parser_delete_expression (parser);
4796
4797         case RID_EXTENSION:
4798           {
4799             /* The saved value of the PEDANTIC flag.  */
4800             int saved_pedantic;
4801             tree expr;
4802
4803             /* Save away the PEDANTIC flag.  */
4804             cp_parser_extension_opt (parser, &saved_pedantic);
4805             /* Parse the cast-expression.  */
4806             expr = cp_parser_simple_cast_expression (parser);
4807             /* Restore the PEDANTIC flag.  */
4808             pedantic = saved_pedantic;
4809
4810             return expr;
4811           }
4812
4813         case RID_REALPART:
4814         case RID_IMAGPART:
4815           {
4816             tree expression;
4817
4818             /* Consume the `__real__' or `__imag__' token.  */
4819             cp_lexer_consume_token (parser->lexer);
4820             /* Parse the cast-expression.  */
4821             expression = cp_parser_simple_cast_expression (parser);
4822             /* Create the complete representation.  */
4823             return build_x_unary_op ((keyword == RID_REALPART
4824                                       ? REALPART_EXPR : IMAGPART_EXPR),
4825                                      expression);
4826           }
4827           break;
4828
4829         default:
4830           break;
4831         }
4832     }
4833
4834   /* Look for the `:: new' and `:: delete', which also signal the
4835      beginning of a new-expression, or delete-expression,
4836      respectively.  If the next token is `::', then it might be one of
4837      these.  */
4838   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4839     {
4840       enum rid keyword;
4841
4842       /* See if the token after the `::' is one of the keywords in
4843          which we're interested.  */
4844       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4845       /* If it's `new', we have a new-expression.  */
4846       if (keyword == RID_NEW)
4847         return cp_parser_new_expression (parser);
4848       /* Similarly, for `delete'.  */
4849       else if (keyword == RID_DELETE)
4850         return cp_parser_delete_expression (parser);
4851     }
4852
4853   /* Look for a unary operator.  */
4854   unary_operator = cp_parser_unary_operator (token);
4855   /* The `++' and `--' operators can be handled similarly, even though
4856      they are not technically unary-operators in the grammar.  */
4857   if (unary_operator == ERROR_MARK)
4858     {
4859       if (token->type == CPP_PLUS_PLUS)
4860         unary_operator = PREINCREMENT_EXPR;
4861       else if (token->type == CPP_MINUS_MINUS)
4862         unary_operator = PREDECREMENT_EXPR;
4863       /* Handle the GNU address-of-label extension.  */
4864       else if (cp_parser_allow_gnu_extensions_p (parser)
4865                && token->type == CPP_AND_AND)
4866         {
4867           tree identifier;
4868
4869           /* Consume the '&&' token.  */
4870           cp_lexer_consume_token (parser->lexer);
4871           /* Look for the identifier.  */
4872           identifier = cp_parser_identifier (parser);
4873           /* Create an expression representing the address.  */
4874           return finish_label_address_expr (identifier);
4875         }
4876     }
4877   if (unary_operator != ERROR_MARK)
4878     {
4879       tree cast_expression;
4880       tree expression = error_mark_node;
4881       const char *non_constant_p = NULL;
4882
4883       /* Consume the operator token.  */
4884       token = cp_lexer_consume_token (parser->lexer);
4885       /* Parse the cast-expression.  */
4886       cast_expression
4887         = cp_parser_cast_expression (parser,
4888                                      unary_operator == ADDR_EXPR,
4889                                      /*cast_p=*/false);
4890       /* Now, build an appropriate representation.  */
4891       switch (unary_operator)
4892         {
4893         case INDIRECT_REF:
4894           non_constant_p = "`*'";
4895           expression = build_x_indirect_ref (cast_expression, "unary *");
4896           break;
4897
4898         case ADDR_EXPR:
4899           non_constant_p = "`&'";
4900           /* Fall through.  */
4901         case BIT_NOT_EXPR:
4902           expression = build_x_unary_op (unary_operator, cast_expression);
4903           break;
4904
4905         case PREINCREMENT_EXPR:
4906         case PREDECREMENT_EXPR:
4907           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4908                             ? "`++'" : "`--'");
4909           /* Fall through.  */
4910         case UNARY_PLUS_EXPR:
4911         case NEGATE_EXPR:
4912         case TRUTH_NOT_EXPR:
4913           expression = finish_unary_op_expr (unary_operator, cast_expression);
4914           break;
4915
4916         default:
4917           gcc_unreachable ();
4918         }
4919
4920       if (non_constant_p
4921           && cp_parser_non_integral_constant_expression (parser,
4922                                                          non_constant_p))
4923         expression = error_mark_node;
4924
4925       return expression;
4926     }
4927
4928   return cp_parser_postfix_expression (parser, address_p, cast_p);
4929 }
4930
4931 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4932    unary-operator, the corresponding tree code is returned.  */
4933
4934 static enum tree_code
4935 cp_parser_unary_operator (cp_token* token)
4936 {
4937   switch (token->type)
4938     {
4939     case CPP_MULT:
4940       return INDIRECT_REF;
4941
4942     case CPP_AND:
4943       return ADDR_EXPR;
4944
4945     case CPP_PLUS:
4946       return UNARY_PLUS_EXPR;
4947
4948     case CPP_MINUS:
4949       return NEGATE_EXPR;
4950
4951     case CPP_NOT:
4952       return TRUTH_NOT_EXPR;
4953
4954     case CPP_COMPL:
4955       return BIT_NOT_EXPR;
4956
4957     default:
4958       return ERROR_MARK;
4959     }
4960 }
4961
4962 /* Parse a new-expression.
4963
4964    new-expression:
4965      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4966      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4967
4968    Returns a representation of the expression.  */
4969
4970 static tree
4971 cp_parser_new_expression (cp_parser* parser)
4972 {
4973   bool global_scope_p;
4974   tree placement;
4975   tree type;
4976   tree initializer;
4977   tree nelts;
4978
4979   /* Look for the optional `::' operator.  */
4980   global_scope_p
4981     = (cp_parser_global_scope_opt (parser,
4982                                    /*current_scope_valid_p=*/false)
4983        != NULL_TREE);
4984   /* Look for the `new' operator.  */
4985   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4986   /* There's no easy way to tell a new-placement from the
4987      `( type-id )' construct.  */
4988   cp_parser_parse_tentatively (parser);
4989   /* Look for a new-placement.  */
4990   placement = cp_parser_new_placement (parser);
4991   /* If that didn't work out, there's no new-placement.  */
4992   if (!cp_parser_parse_definitely (parser))
4993     placement = NULL_TREE;
4994
4995   /* If the next token is a `(', then we have a parenthesized
4996      type-id.  */
4997   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4998     {
4999       /* Consume the `('.  */
5000       cp_lexer_consume_token (parser->lexer);
5001       /* Parse the type-id.  */
5002       type = cp_parser_type_id (parser);
5003       /* Look for the closing `)'.  */
5004       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5005       /* There should not be a direct-new-declarator in this production,
5006          but GCC used to allowed this, so we check and emit a sensible error
5007          message for this case.  */
5008       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5009         {
5010           error ("array bound forbidden after parenthesized type-id");
5011           inform ("try removing the parentheses around the type-id");
5012           cp_parser_direct_new_declarator (parser);
5013         }
5014       nelts = NULL_TREE;
5015     }
5016   /* Otherwise, there must be a new-type-id.  */
5017   else
5018     type = cp_parser_new_type_id (parser, &nelts);
5019
5020   /* If the next token is a `(', then we have a new-initializer.  */
5021   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5022     initializer = cp_parser_new_initializer (parser);
5023   else
5024     initializer = NULL_TREE;
5025
5026   /* A new-expression may not appear in an integral constant
5027      expression.  */
5028   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5029     return error_mark_node;
5030
5031   /* Create a representation of the new-expression.  */
5032   return build_new (placement, type, nelts, initializer, global_scope_p);
5033 }
5034
5035 /* Parse a new-placement.
5036
5037    new-placement:
5038      ( expression-list )
5039
5040    Returns the same representation as for an expression-list.  */
5041
5042 static tree
5043 cp_parser_new_placement (cp_parser* parser)
5044 {
5045   tree expression_list;
5046
5047   /* Parse the expression-list.  */
5048   expression_list = (cp_parser_parenthesized_expression_list
5049                      (parser, false, /*cast_p=*/false,
5050                       /*non_constant_p=*/NULL));
5051
5052   return expression_list;
5053 }
5054
5055 /* Parse a new-type-id.
5056
5057    new-type-id:
5058      type-specifier-seq new-declarator [opt]
5059
5060    Returns the TYPE allocated.  If the new-type-id indicates an array
5061    type, *NELTS is set to the number of elements in the last array
5062    bound; the TYPE will not include the last array bound.  */
5063
5064 static tree
5065 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5066 {
5067   cp_decl_specifier_seq type_specifier_seq;
5068   cp_declarator *new_declarator;
5069   cp_declarator *declarator;
5070   cp_declarator *outer_declarator;
5071   const char *saved_message;
5072   tree type;
5073
5074   /* The type-specifier sequence must not contain type definitions.
5075      (It cannot contain declarations of new types either, but if they
5076      are not definitions we will catch that because they are not
5077      complete.)  */
5078   saved_message = parser->type_definition_forbidden_message;
5079   parser->type_definition_forbidden_message
5080     = "types may not be defined in a new-type-id";
5081   /* Parse the type-specifier-seq.  */
5082   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5083                                 &type_specifier_seq);
5084   /* Restore the old message.  */
5085   parser->type_definition_forbidden_message = saved_message;
5086   /* Parse the new-declarator.  */
5087   new_declarator = cp_parser_new_declarator_opt (parser);
5088
5089   /* Determine the number of elements in the last array dimension, if
5090      any.  */
5091   *nelts = NULL_TREE;
5092   /* Skip down to the last array dimension.  */
5093   declarator = new_declarator;
5094   outer_declarator = NULL;
5095   while (declarator && (declarator->kind == cdk_pointer
5096                         || declarator->kind == cdk_ptrmem))
5097     {
5098       outer_declarator = declarator;
5099       declarator = declarator->declarator;
5100     }
5101   while (declarator
5102          && declarator->kind == cdk_array
5103          && declarator->declarator
5104          && declarator->declarator->kind == cdk_array)
5105     {
5106       outer_declarator = declarator;
5107       declarator = declarator->declarator;
5108     }
5109
5110   if (declarator && declarator->kind == cdk_array)
5111     {
5112       *nelts = declarator->u.array.bounds;
5113       if (*nelts == error_mark_node)
5114         *nelts = integer_one_node;
5115
5116       if (outer_declarator)
5117         outer_declarator->declarator = declarator->declarator;
5118       else
5119         new_declarator = NULL;
5120     }
5121
5122   type = groktypename (&type_specifier_seq, new_declarator);
5123   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5124     {
5125       *nelts = array_type_nelts_top (type);
5126       type = TREE_TYPE (type);
5127     }
5128   return type;
5129 }
5130
5131 /* Parse an (optional) new-declarator.
5132
5133    new-declarator:
5134      ptr-operator new-declarator [opt]
5135      direct-new-declarator
5136
5137    Returns the declarator.  */
5138
5139 static cp_declarator *
5140 cp_parser_new_declarator_opt (cp_parser* parser)
5141 {
5142   enum tree_code code;
5143   tree type;
5144   cp_cv_quals cv_quals;
5145
5146   /* We don't know if there's a ptr-operator next, or not.  */
5147   cp_parser_parse_tentatively (parser);
5148   /* Look for a ptr-operator.  */
5149   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5150   /* If that worked, look for more new-declarators.  */
5151   if (cp_parser_parse_definitely (parser))
5152     {
5153       cp_declarator *declarator;
5154
5155       /* Parse another optional declarator.  */
5156       declarator = cp_parser_new_declarator_opt (parser);
5157
5158       /* Create the representation of the declarator.  */
5159       if (type)
5160         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5161       else if (code == INDIRECT_REF)
5162         declarator = make_pointer_declarator (cv_quals, declarator);
5163       else
5164         declarator = make_reference_declarator (cv_quals, declarator);
5165
5166       return declarator;
5167     }
5168
5169   /* If the next token is a `[', there is a direct-new-declarator.  */
5170   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5171     return cp_parser_direct_new_declarator (parser);
5172
5173   return NULL;
5174 }
5175
5176 /* Parse a direct-new-declarator.
5177
5178    direct-new-declarator:
5179      [ expression ]
5180      direct-new-declarator [constant-expression]
5181
5182    */
5183
5184 static cp_declarator *
5185 cp_parser_direct_new_declarator (cp_parser* parser)
5186 {
5187   cp_declarator *declarator = NULL;
5188
5189   while (true)
5190     {
5191       tree expression;
5192
5193       /* Look for the opening `['.  */
5194       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5195       /* The first expression is not required to be constant.  */
5196       if (!declarator)
5197         {
5198           expression = cp_parser_expression (parser, /*cast_p=*/false);
5199           /* The standard requires that the expression have integral
5200              type.  DR 74 adds enumeration types.  We believe that the
5201              real intent is that these expressions be handled like the
5202              expression in a `switch' condition, which also allows
5203              classes with a single conversion to integral or
5204              enumeration type.  */
5205           if (!processing_template_decl)
5206             {
5207               expression
5208                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5209                                               expression,
5210                                               /*complain=*/true);
5211               if (!expression)
5212                 {
5213                   error ("expression in new-declarator must have integral "
5214                          "or enumeration type");
5215                   expression = error_mark_node;
5216                 }
5217             }
5218         }
5219       /* But all the other expressions must be.  */
5220       else
5221         expression
5222           = cp_parser_constant_expression (parser,
5223                                            /*allow_non_constant=*/false,
5224                                            NULL);
5225       /* Look for the closing `]'.  */
5226       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5227
5228       /* Add this bound to the declarator.  */
5229       declarator = make_array_declarator (declarator, expression);
5230
5231       /* If the next token is not a `[', then there are no more
5232          bounds.  */
5233       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5234         break;
5235     }
5236
5237   return declarator;
5238 }
5239
5240 /* Parse a new-initializer.
5241
5242    new-initializer:
5243      ( expression-list [opt] )
5244
5245    Returns a representation of the expression-list.  If there is no
5246    expression-list, VOID_ZERO_NODE is returned.  */
5247
5248 static tree
5249 cp_parser_new_initializer (cp_parser* parser)
5250 {
5251   tree expression_list;
5252
5253   expression_list = (cp_parser_parenthesized_expression_list
5254                      (parser, false, /*cast_p=*/false,
5255                       /*non_constant_p=*/NULL));
5256   if (!expression_list)
5257     expression_list = void_zero_node;
5258
5259   return expression_list;
5260 }
5261
5262 /* Parse a delete-expression.
5263
5264    delete-expression:
5265      :: [opt] delete cast-expression
5266      :: [opt] delete [ ] cast-expression
5267
5268    Returns a representation of the expression.  */
5269
5270 static tree
5271 cp_parser_delete_expression (cp_parser* parser)
5272 {
5273   bool global_scope_p;
5274   bool array_p;
5275   tree expression;
5276
5277   /* Look for the optional `::' operator.  */
5278   global_scope_p
5279     = (cp_parser_global_scope_opt (parser,
5280                                    /*current_scope_valid_p=*/false)
5281        != NULL_TREE);
5282   /* Look for the `delete' keyword.  */
5283   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5284   /* See if the array syntax is in use.  */
5285   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5286     {
5287       /* Consume the `[' token.  */
5288       cp_lexer_consume_token (parser->lexer);
5289       /* Look for the `]' token.  */
5290       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5291       /* Remember that this is the `[]' construct.  */
5292       array_p = true;
5293     }
5294   else
5295     array_p = false;
5296
5297   /* Parse the cast-expression.  */
5298   expression = cp_parser_simple_cast_expression (parser);
5299
5300   /* A delete-expression may not appear in an integral constant
5301      expression.  */
5302   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5303     return error_mark_node;
5304
5305   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5306 }
5307
5308 /* Parse a cast-expression.
5309
5310    cast-expression:
5311      unary-expression
5312      ( type-id ) cast-expression
5313
5314    ADDRESS_P is true iff the unary-expression is appearing as the
5315    operand of the `&' operator.   CAST_P is true if this expression is
5316    the target of a cast.
5317
5318    Returns a representation of the expression.  */
5319
5320 static tree
5321 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5322 {
5323   /* If it's a `(', then we might be looking at a cast.  */
5324   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5325     {
5326       tree type = NULL_TREE;
5327       tree expr = NULL_TREE;
5328       bool compound_literal_p;
5329       const char *saved_message;
5330
5331       /* There's no way to know yet whether or not this is a cast.
5332          For example, `(int (3))' is a unary-expression, while `(int)
5333          3' is a cast.  So, we resort to parsing tentatively.  */
5334       cp_parser_parse_tentatively (parser);
5335       /* Types may not be defined in a cast.  */
5336       saved_message = parser->type_definition_forbidden_message;
5337       parser->type_definition_forbidden_message
5338         = "types may not be defined in casts";
5339       /* Consume the `('.  */
5340       cp_lexer_consume_token (parser->lexer);
5341       /* A very tricky bit is that `(struct S) { 3 }' is a
5342          compound-literal (which we permit in C++ as an extension).
5343          But, that construct is not a cast-expression -- it is a
5344          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5345          is legal; if the compound-literal were a cast-expression,
5346          you'd need an extra set of parentheses.)  But, if we parse
5347          the type-id, and it happens to be a class-specifier, then we
5348          will commit to the parse at that point, because we cannot
5349          undo the action that is done when creating a new class.  So,
5350          then we cannot back up and do a postfix-expression.
5351
5352          Therefore, we scan ahead to the closing `)', and check to see
5353          if the token after the `)' is a `{'.  If so, we are not
5354          looking at a cast-expression.
5355
5356          Save tokens so that we can put them back.  */
5357       cp_lexer_save_tokens (parser->lexer);
5358       /* Skip tokens until the next token is a closing parenthesis.
5359          If we find the closing `)', and the next token is a `{', then
5360          we are looking at a compound-literal.  */
5361       compound_literal_p
5362         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5363                                                   /*consume_paren=*/true)
5364            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5365       /* Roll back the tokens we skipped.  */
5366       cp_lexer_rollback_tokens (parser->lexer);
5367       /* If we were looking at a compound-literal, simulate an error
5368          so that the call to cp_parser_parse_definitely below will
5369          fail.  */
5370       if (compound_literal_p)
5371         cp_parser_simulate_error (parser);
5372       else
5373         {
5374           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5375           parser->in_type_id_in_expr_p = true;
5376           /* Look for the type-id.  */
5377           type = cp_parser_type_id (parser);
5378           /* Look for the closing `)'.  */
5379           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5380           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5381         }
5382
5383       /* Restore the saved message.  */
5384       parser->type_definition_forbidden_message = saved_message;
5385
5386       /* If ok so far, parse the dependent expression. We cannot be
5387          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5388          ctor of T, but looks like a cast to function returning T
5389          without a dependent expression.  */
5390       if (!cp_parser_error_occurred (parser))
5391         expr = cp_parser_cast_expression (parser,
5392                                           /*address_p=*/false,
5393                                           /*cast_p=*/true);
5394
5395       if (cp_parser_parse_definitely (parser))
5396         {
5397           /* Warn about old-style casts, if so requested.  */
5398           if (warn_old_style_cast
5399               && !in_system_header
5400               && !VOID_TYPE_P (type)
5401               && current_lang_name != lang_name_c)
5402             warning (0, "use of old-style cast");
5403
5404           /* Only type conversions to integral or enumeration types
5405              can be used in constant-expressions.  */
5406           if (parser->integral_constant_expression_p
5407               && !dependent_type_p (type)
5408               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5409               && (cp_parser_non_integral_constant_expression
5410                   (parser,
5411                    "a cast to a type other than an integral or "
5412                    "enumeration type")))
5413             return error_mark_node;
5414
5415           /* Perform the cast.  */
5416           expr = build_c_cast (type, expr);
5417           return expr;
5418         }
5419     }
5420
5421   /* If we get here, then it's not a cast, so it must be a
5422      unary-expression.  */
5423   return cp_parser_unary_expression (parser, address_p, cast_p);
5424 }
5425
5426 /* Parse a binary expression of the general form:
5427
5428    pm-expression:
5429      cast-expression
5430      pm-expression .* cast-expression
5431      pm-expression ->* cast-expression
5432
5433    multiplicative-expression:
5434      pm-expression
5435      multiplicative-expression * pm-expression
5436      multiplicative-expression / pm-expression
5437      multiplicative-expression % pm-expression
5438
5439    additive-expression:
5440      multiplicative-expression
5441      additive-expression + multiplicative-expression
5442      additive-expression - multiplicative-expression
5443
5444    shift-expression:
5445      additive-expression
5446      shift-expression << additive-expression
5447      shift-expression >> additive-expression
5448
5449    relational-expression:
5450      shift-expression
5451      relational-expression < shift-expression
5452      relational-expression > shift-expression
5453      relational-expression <= shift-expression
5454      relational-expression >= shift-expression
5455
5456   GNU Extension:
5457
5458    relational-expression:
5459      relational-expression <? shift-expression
5460      relational-expression >? shift-expression
5461
5462    equality-expression:
5463      relational-expression
5464      equality-expression == relational-expression
5465      equality-expression != relational-expression
5466
5467    and-expression:
5468      equality-expression
5469      and-expression & equality-expression
5470
5471    exclusive-or-expression:
5472      and-expression
5473      exclusive-or-expression ^ and-expression
5474
5475    inclusive-or-expression:
5476      exclusive-or-expression
5477      inclusive-or-expression | exclusive-or-expression
5478
5479    logical-and-expression:
5480      inclusive-or-expression
5481      logical-and-expression && inclusive-or-expression
5482
5483    logical-or-expression:
5484      logical-and-expression
5485      logical-or-expression || logical-and-expression
5486
5487    All these are implemented with a single function like:
5488
5489    binary-expression:
5490      simple-cast-expression
5491      binary-expression <token> binary-expression
5492
5493    CAST_P is true if this expression is the target of a cast.
5494
5495    The binops_by_token map is used to get the tree codes for each <token> type.
5496    binary-expressions are associated according to a precedence table.  */
5497
5498 #define TOKEN_PRECEDENCE(token) \
5499   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5500    ? PREC_NOT_OPERATOR \
5501    : binops_by_token[token->type].prec)
5502
5503 static tree
5504 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5505 {
5506   cp_parser_expression_stack stack;
5507   cp_parser_expression_stack_entry *sp = &stack[0];
5508   tree lhs, rhs;
5509   cp_token *token;
5510   enum tree_code tree_type;
5511   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5512   bool overloaded_p;
5513
5514   /* Parse the first expression.  */
5515   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5516
5517   for (;;)
5518     {
5519       /* Get an operator token.  */
5520       token = cp_lexer_peek_token (parser->lexer);
5521       if (token->type == CPP_MIN || token->type == CPP_MAX)
5522         cp_parser_warn_min_max ();
5523
5524       new_prec = TOKEN_PRECEDENCE (token);
5525
5526       /* Popping an entry off the stack means we completed a subexpression:
5527          - either we found a token which is not an operator (`>' where it is not
5528            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5529            will happen repeatedly;
5530          - or, we found an operator which has lower priority.  This is the case
5531            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5532            parsing `3 * 4'.  */
5533       if (new_prec <= prec)
5534         {
5535           if (sp == stack)
5536             break;
5537           else
5538             goto pop;
5539         }
5540
5541      get_rhs:
5542       tree_type = binops_by_token[token->type].tree_type;
5543
5544       /* We used the operator token.  */
5545       cp_lexer_consume_token (parser->lexer);
5546
5547       /* Extract another operand.  It may be the RHS of this expression
5548          or the LHS of a new, higher priority expression.  */
5549       rhs = cp_parser_simple_cast_expression (parser);
5550
5551       /* Get another operator token.  Look up its precedence to avoid
5552          building a useless (immediately popped) stack entry for common
5553          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5554       token = cp_lexer_peek_token (parser->lexer);
5555       lookahead_prec = TOKEN_PRECEDENCE (token);
5556       if (lookahead_prec > new_prec)
5557         {
5558           /* ... and prepare to parse the RHS of the new, higher priority
5559              expression.  Since precedence levels on the stack are
5560              monotonically increasing, we do not have to care about
5561              stack overflows.  */
5562           sp->prec = prec;
5563           sp->tree_type = tree_type;
5564           sp->lhs = lhs;
5565           sp++;
5566           lhs = rhs;
5567           prec = new_prec;
5568           new_prec = lookahead_prec;
5569           goto get_rhs;
5570
5571          pop:
5572           /* If the stack is not empty, we have parsed into LHS the right side
5573              (`4' in the example above) of an expression we had suspended.
5574              We can use the information on the stack to recover the LHS (`3')
5575              from the stack together with the tree code (`MULT_EXPR'), and
5576              the precedence of the higher level subexpression
5577              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5578              which will be used to actually build the additive expression.  */
5579           --sp;
5580           prec = sp->prec;
5581           tree_type = sp->tree_type;
5582           rhs = lhs;
5583           lhs = sp->lhs;
5584         }
5585
5586       overloaded_p = false;
5587       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5588
5589       /* If the binary operator required the use of an overloaded operator,
5590          then this expression cannot be an integral constant-expression.
5591          An overloaded operator can be used even if both operands are
5592          otherwise permissible in an integral constant-expression if at
5593          least one of the operands is of enumeration type.  */
5594
5595       if (overloaded_p
5596           && (cp_parser_non_integral_constant_expression
5597               (parser, "calls to overloaded operators")))
5598         return error_mark_node;
5599     }
5600
5601   return lhs;
5602 }
5603
5604
5605 /* Parse the `? expression : assignment-expression' part of a
5606    conditional-expression.  The LOGICAL_OR_EXPR is the
5607    logical-or-expression that started the conditional-expression.
5608    Returns a representation of the entire conditional-expression.
5609
5610    This routine is used by cp_parser_assignment_expression.
5611
5612      ? expression : assignment-expression
5613
5614    GNU Extensions:
5615
5616      ? : assignment-expression */
5617
5618 static tree
5619 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5620 {
5621   tree expr;
5622   tree assignment_expr;
5623
5624   /* Consume the `?' token.  */
5625   cp_lexer_consume_token (parser->lexer);
5626   if (cp_parser_allow_gnu_extensions_p (parser)
5627       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5628     /* Implicit true clause.  */
5629     expr = NULL_TREE;
5630   else
5631     /* Parse the expression.  */
5632     expr = cp_parser_expression (parser, /*cast_p=*/false);
5633
5634   /* The next token should be a `:'.  */
5635   cp_parser_require (parser, CPP_COLON, "`:'");
5636   /* Parse the assignment-expression.  */
5637   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5638
5639   /* Build the conditional-expression.  */
5640   return build_x_conditional_expr (logical_or_expr,
5641                                    expr,
5642                                    assignment_expr);
5643 }
5644
5645 /* Parse an assignment-expression.
5646
5647    assignment-expression:
5648      conditional-expression
5649      logical-or-expression assignment-operator assignment_expression
5650      throw-expression
5651
5652    CAST_P is true if this expression is the target of a cast.
5653
5654    Returns a representation for the expression.  */
5655
5656 static tree
5657 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5658 {
5659   tree expr;
5660
5661   /* If the next token is the `throw' keyword, then we're looking at
5662      a throw-expression.  */
5663   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5664     expr = cp_parser_throw_expression (parser);
5665   /* Otherwise, it must be that we are looking at a
5666      logical-or-expression.  */
5667   else
5668     {
5669       /* Parse the binary expressions (logical-or-expression).  */
5670       expr = cp_parser_binary_expression (parser, cast_p);
5671       /* If the next token is a `?' then we're actually looking at a
5672          conditional-expression.  */
5673       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5674         return cp_parser_question_colon_clause (parser, expr);
5675       else
5676         {
5677           enum tree_code assignment_operator;
5678
5679           /* If it's an assignment-operator, we're using the second
5680              production.  */
5681           assignment_operator
5682             = cp_parser_assignment_operator_opt (parser);
5683           if (assignment_operator != ERROR_MARK)
5684             {
5685               tree rhs;
5686
5687               /* Parse the right-hand side of the assignment.  */
5688               rhs = cp_parser_assignment_expression (parser, cast_p);
5689               /* An assignment may not appear in a
5690                  constant-expression.  */
5691               if (cp_parser_non_integral_constant_expression (parser,
5692                                                               "an assignment"))
5693                 return error_mark_node;
5694               /* Build the assignment expression.  */
5695               expr = build_x_modify_expr (expr,
5696                                           assignment_operator,
5697                                           rhs);
5698             }
5699         }
5700     }
5701
5702   return expr;
5703 }
5704
5705 /* Parse an (optional) assignment-operator.
5706
5707    assignment-operator: one of
5708      = *= /= %= += -= >>= <<= &= ^= |=
5709
5710    GNU Extension:
5711
5712    assignment-operator: one of
5713      <?= >?=
5714
5715    If the next token is an assignment operator, the corresponding tree
5716    code is returned, and the token is consumed.  For example, for
5717    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5718    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5719    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5720    operator, ERROR_MARK is returned.  */
5721
5722 static enum tree_code
5723 cp_parser_assignment_operator_opt (cp_parser* parser)
5724 {
5725   enum tree_code op;
5726   cp_token *token;
5727
5728   /* Peek at the next toen.  */
5729   token = cp_lexer_peek_token (parser->lexer);
5730
5731   switch (token->type)
5732     {
5733     case CPP_EQ:
5734       op = NOP_EXPR;
5735       break;
5736
5737     case CPP_MULT_EQ:
5738       op = MULT_EXPR;
5739       break;
5740
5741     case CPP_DIV_EQ:
5742       op = TRUNC_DIV_EXPR;
5743       break;
5744
5745     case CPP_MOD_EQ:
5746       op = TRUNC_MOD_EXPR;
5747       break;
5748
5749     case CPP_PLUS_EQ:
5750       op = PLUS_EXPR;
5751       break;
5752
5753     case CPP_MINUS_EQ:
5754       op = MINUS_EXPR;
5755       break;
5756
5757     case CPP_RSHIFT_EQ:
5758       op = RSHIFT_EXPR;
5759       break;
5760
5761     case CPP_LSHIFT_EQ:
5762       op = LSHIFT_EXPR;
5763       break;
5764
5765     case CPP_AND_EQ:
5766       op = BIT_AND_EXPR;
5767       break;
5768
5769     case CPP_XOR_EQ:
5770       op = BIT_XOR_EXPR;
5771       break;
5772
5773     case CPP_OR_EQ:
5774       op = BIT_IOR_EXPR;
5775       break;
5776
5777     case CPP_MIN_EQ:
5778       op = MIN_EXPR;
5779       cp_parser_warn_min_max ();
5780       break;
5781
5782     case CPP_MAX_EQ:
5783       op = MAX_EXPR;
5784       cp_parser_warn_min_max ();
5785       break;
5786
5787     default:
5788       /* Nothing else is an assignment operator.  */
5789       op = ERROR_MARK;
5790     }
5791
5792   /* If it was an assignment operator, consume it.  */
5793   if (op != ERROR_MARK)
5794     cp_lexer_consume_token (parser->lexer);
5795
5796   return op;
5797 }
5798
5799 /* Parse an expression.
5800
5801    expression:
5802      assignment-expression
5803      expression , assignment-expression
5804
5805    CAST_P is true if this expression is the target of a cast.
5806
5807    Returns a representation of the expression.  */
5808
5809 static tree
5810 cp_parser_expression (cp_parser* parser, bool cast_p)
5811 {
5812   tree expression = NULL_TREE;
5813
5814   while (true)
5815     {
5816       tree assignment_expression;
5817
5818       /* Parse the next assignment-expression.  */
5819       assignment_expression
5820         = cp_parser_assignment_expression (parser, cast_p);
5821       /* If this is the first assignment-expression, we can just
5822          save it away.  */
5823       if (!expression)
5824         expression = assignment_expression;
5825       else
5826         expression = build_x_compound_expr (expression,
5827                                             assignment_expression);
5828       /* If the next token is not a comma, then we are done with the
5829          expression.  */
5830       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5831         break;
5832       /* Consume the `,'.  */
5833       cp_lexer_consume_token (parser->lexer);
5834       /* A comma operator cannot appear in a constant-expression.  */
5835       if (cp_parser_non_integral_constant_expression (parser,
5836                                                       "a comma operator"))
5837         expression = error_mark_node;
5838     }
5839
5840   return expression;
5841 }
5842
5843 /* Parse a constant-expression.
5844
5845    constant-expression:
5846      conditional-expression
5847
5848   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5849   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5850   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5851   is false, NON_CONSTANT_P should be NULL.  */
5852
5853 static tree
5854 cp_parser_constant_expression (cp_parser* parser,
5855                                bool allow_non_constant_p,
5856                                bool *non_constant_p)
5857 {
5858   bool saved_integral_constant_expression_p;
5859   bool saved_allow_non_integral_constant_expression_p;
5860   bool saved_non_integral_constant_expression_p;
5861   tree expression;
5862
5863   /* It might seem that we could simply parse the
5864      conditional-expression, and then check to see if it were
5865      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5866      one that the compiler can figure out is constant, possibly after
5867      doing some simplifications or optimizations.  The standard has a
5868      precise definition of constant-expression, and we must honor
5869      that, even though it is somewhat more restrictive.
5870
5871      For example:
5872
5873        int i[(2, 3)];
5874
5875      is not a legal declaration, because `(2, 3)' is not a
5876      constant-expression.  The `,' operator is forbidden in a
5877      constant-expression.  However, GCC's constant-folding machinery
5878      will fold this operation to an INTEGER_CST for `3'.  */
5879
5880   /* Save the old settings.  */
5881   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5882   saved_allow_non_integral_constant_expression_p
5883     = parser->allow_non_integral_constant_expression_p;
5884   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5885   /* We are now parsing a constant-expression.  */
5886   parser->integral_constant_expression_p = true;
5887   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5888   parser->non_integral_constant_expression_p = false;
5889   /* Although the grammar says "conditional-expression", we parse an
5890      "assignment-expression", which also permits "throw-expression"
5891      and the use of assignment operators.  In the case that
5892      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5893      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5894      actually essential that we look for an assignment-expression.
5895      For example, cp_parser_initializer_clauses uses this function to
5896      determine whether a particular assignment-expression is in fact
5897      constant.  */
5898   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5899   /* Restore the old settings.  */
5900   parser->integral_constant_expression_p
5901     = saved_integral_constant_expression_p;
5902   parser->allow_non_integral_constant_expression_p
5903     = saved_allow_non_integral_constant_expression_p;
5904   if (allow_non_constant_p)
5905     *non_constant_p = parser->non_integral_constant_expression_p;
5906   else if (parser->non_integral_constant_expression_p)
5907     expression = error_mark_node;
5908   parser->non_integral_constant_expression_p
5909     = saved_non_integral_constant_expression_p;
5910
5911   return expression;
5912 }
5913
5914 /* Parse __builtin_offsetof.
5915
5916    offsetof-expression:
5917      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5918
5919    offsetof-member-designator:
5920      id-expression
5921      | offsetof-member-designator "." id-expression
5922      | offsetof-member-designator "[" expression "]"
5923 */
5924
5925 static tree
5926 cp_parser_builtin_offsetof (cp_parser *parser)
5927 {
5928   int save_ice_p, save_non_ice_p;
5929   tree type, expr;
5930   cp_id_kind dummy;
5931
5932   /* We're about to accept non-integral-constant things, but will
5933      definitely yield an integral constant expression.  Save and
5934      restore these values around our local parsing.  */
5935   save_ice_p = parser->integral_constant_expression_p;
5936   save_non_ice_p = parser->non_integral_constant_expression_p;
5937
5938   /* Consume the "__builtin_offsetof" token.  */
5939   cp_lexer_consume_token (parser->lexer);
5940   /* Consume the opening `('.  */
5941   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5942   /* Parse the type-id.  */
5943   type = cp_parser_type_id (parser);
5944   /* Look for the `,'.  */
5945   cp_parser_require (parser, CPP_COMMA, "`,'");
5946
5947   /* Build the (type *)null that begins the traditional offsetof macro.  */
5948   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5949
5950   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5951   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5952                                                  true, &dummy);
5953   while (true)
5954     {
5955       cp_token *token = cp_lexer_peek_token (parser->lexer);
5956       switch (token->type)
5957         {
5958         case CPP_OPEN_SQUARE:
5959           /* offsetof-member-designator "[" expression "]" */
5960           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5961           break;
5962
5963         case CPP_DOT:
5964           /* offsetof-member-designator "." identifier */
5965           cp_lexer_consume_token (parser->lexer);
5966           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5967                                                          true, &dummy);
5968           break;
5969
5970         case CPP_CLOSE_PAREN:
5971           /* Consume the ")" token.  */
5972           cp_lexer_consume_token (parser->lexer);
5973           goto success;
5974
5975         default:
5976           /* Error.  We know the following require will fail, but
5977              that gives the proper error message.  */
5978           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5979           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5980           expr = error_mark_node;
5981           goto failure;
5982         }
5983     }
5984
5985  success:
5986   /* If we're processing a template, we can't finish the semantics yet.
5987      Otherwise we can fold the entire expression now.  */
5988   if (processing_template_decl)
5989     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5990   else
5991     expr = fold_offsetof (expr);
5992
5993  failure:
5994   parser->integral_constant_expression_p = save_ice_p;
5995   parser->non_integral_constant_expression_p = save_non_ice_p;
5996
5997   return expr;
5998 }
5999
6000 /* Statements [gram.stmt.stmt]  */
6001
6002 /* Parse a statement.
6003
6004    statement:
6005      labeled-statement
6006      expression-statement
6007      compound-statement
6008      selection-statement
6009      iteration-statement
6010      jump-statement
6011      declaration-statement
6012      try-block  */
6013
6014 static void
6015 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
6016 {
6017   tree statement;
6018   cp_token *token;
6019   location_t statement_location;
6020
6021   /* There is no statement yet.  */
6022   statement = NULL_TREE;
6023   /* Peek at the next token.  */
6024   token = cp_lexer_peek_token (parser->lexer);
6025   /* Remember the location of the first token in the statement.  */
6026   statement_location = token->location;
6027   /* If this is a keyword, then that will often determine what kind of
6028      statement we have.  */
6029   if (token->type == CPP_KEYWORD)
6030     {
6031       enum rid keyword = token->keyword;
6032
6033       switch (keyword)
6034         {
6035         case RID_CASE:
6036         case RID_DEFAULT:
6037           statement = cp_parser_labeled_statement (parser,
6038                                                    in_statement_expr);
6039           break;
6040
6041         case RID_IF:
6042         case RID_SWITCH:
6043           statement = cp_parser_selection_statement (parser);
6044           break;
6045
6046         case RID_WHILE:
6047         case RID_DO:
6048         case RID_FOR:
6049           statement = cp_parser_iteration_statement (parser);
6050           break;
6051
6052         case RID_BREAK:
6053         case RID_CONTINUE:
6054         case RID_RETURN:
6055         case RID_GOTO:
6056           statement = cp_parser_jump_statement (parser);
6057           break;
6058
6059           /* Objective-C++ exception-handling constructs.  */
6060         case RID_AT_TRY:
6061         case RID_AT_CATCH:
6062         case RID_AT_FINALLY:
6063         case RID_AT_SYNCHRONIZED:
6064         case RID_AT_THROW:
6065           statement = cp_parser_objc_statement (parser);
6066           break;
6067
6068         case RID_TRY:
6069           statement = cp_parser_try_block (parser);
6070           break;
6071
6072         default:
6073           /* It might be a keyword like `int' that can start a
6074              declaration-statement.  */
6075           break;
6076         }
6077     }
6078   else if (token->type == CPP_NAME)
6079     {
6080       /* If the next token is a `:', then we are looking at a
6081          labeled-statement.  */
6082       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6083       if (token->type == CPP_COLON)
6084         statement = cp_parser_labeled_statement (parser, in_statement_expr);
6085     }
6086   /* Anything that starts with a `{' must be a compound-statement.  */
6087   else if (token->type == CPP_OPEN_BRACE)
6088     statement = cp_parser_compound_statement (parser, NULL, false);
6089   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6090      a statement all its own.  */
6091   else if (token->type == CPP_PRAGMA)
6092     {
6093       cp_lexer_handle_pragma (parser->lexer);
6094       return;
6095     }
6096
6097   /* Everything else must be a declaration-statement or an
6098      expression-statement.  Try for the declaration-statement
6099      first, unless we are looking at a `;', in which case we know that
6100      we have an expression-statement.  */
6101   if (!statement)
6102     {
6103       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6104         {
6105           cp_parser_parse_tentatively (parser);
6106           /* Try to parse the declaration-statement.  */
6107           cp_parser_declaration_statement (parser);
6108           /* If that worked, we're done.  */
6109           if (cp_parser_parse_definitely (parser))
6110             return;
6111         }
6112       /* Look for an expression-statement instead.  */
6113       statement = cp_parser_expression_statement (parser, in_statement_expr);
6114     }
6115
6116   /* Set the line number for the statement.  */
6117   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6118     SET_EXPR_LOCATION (statement, statement_location);
6119 }
6120
6121 /* Parse a labeled-statement.
6122
6123    labeled-statement:
6124      identifier : statement
6125      case constant-expression : statement
6126      default : statement
6127
6128    GNU Extension:
6129
6130    labeled-statement:
6131      case constant-expression ... constant-expression : statement
6132
6133    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6134    For an ordinary label, returns a LABEL_EXPR.  */
6135
6136 static tree
6137 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6138 {
6139   cp_token *token;
6140   tree statement = error_mark_node;
6141
6142   /* The next token should be an identifier.  */
6143   token = cp_lexer_peek_token (parser->lexer);
6144   if (token->type != CPP_NAME
6145       && token->type != CPP_KEYWORD)
6146     {
6147       cp_parser_error (parser, "expected labeled-statement");
6148       return error_mark_node;
6149     }
6150
6151   switch (token->keyword)
6152     {
6153     case RID_CASE:
6154       {
6155         tree expr, expr_hi;
6156         cp_token *ellipsis;
6157
6158         /* Consume the `case' token.  */
6159         cp_lexer_consume_token (parser->lexer);
6160         /* Parse the constant-expression.  */
6161         expr = cp_parser_constant_expression (parser,
6162                                               /*allow_non_constant_p=*/false,
6163                                               NULL);
6164
6165         ellipsis = cp_lexer_peek_token (parser->lexer);
6166         if (ellipsis->type == CPP_ELLIPSIS)
6167           {
6168             /* Consume the `...' token.  */
6169             cp_lexer_consume_token (parser->lexer);
6170             expr_hi =
6171               cp_parser_constant_expression (parser,
6172                                              /*allow_non_constant_p=*/false,
6173                                              NULL);
6174             /* We don't need to emit warnings here, as the common code
6175                will do this for us.  */
6176           }
6177         else
6178           expr_hi = NULL_TREE;
6179
6180         if (!parser->in_switch_statement_p)
6181           error ("case label %qE not within a switch statement", expr);
6182         else
6183           statement = finish_case_label (expr, expr_hi);
6184       }
6185       break;
6186
6187     case RID_DEFAULT:
6188       /* Consume the `default' token.  */
6189       cp_lexer_consume_token (parser->lexer);
6190       if (!parser->in_switch_statement_p)
6191         error ("case label not within a switch statement");
6192       else
6193         statement = finish_case_label (NULL_TREE, NULL_TREE);
6194       break;
6195
6196     default:
6197       /* Anything else must be an ordinary label.  */
6198       statement = finish_label_stmt (cp_parser_identifier (parser));
6199       break;
6200     }
6201
6202   /* Require the `:' token.  */
6203   cp_parser_require (parser, CPP_COLON, "`:'");
6204   /* Parse the labeled statement.  */
6205   cp_parser_statement (parser, in_statement_expr);
6206
6207   /* Return the label, in the case of a `case' or `default' label.  */
6208   return statement;
6209 }
6210
6211 /* Parse an expression-statement.
6212
6213    expression-statement:
6214      expression [opt] ;
6215
6216    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6217    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6218    indicates whether this expression-statement is part of an
6219    expression statement.  */
6220
6221 static tree
6222 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6223 {
6224   tree statement = NULL_TREE;
6225
6226   /* If the next token is a ';', then there is no expression
6227      statement.  */
6228   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6229     statement = cp_parser_expression (parser, /*cast_p=*/false);
6230
6231   /* Consume the final `;'.  */
6232   cp_parser_consume_semicolon_at_end_of_statement (parser);
6233
6234   if (in_statement_expr
6235       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6236     /* This is the final expression statement of a statement
6237        expression.  */
6238     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6239   else if (statement)
6240     statement = finish_expr_stmt (statement);
6241   else
6242     finish_stmt ();
6243
6244   return statement;
6245 }
6246
6247 /* Parse a compound-statement.
6248
6249    compound-statement:
6250      { statement-seq [opt] }
6251
6252    Returns a tree representing the statement.  */
6253
6254 static tree
6255 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6256                               bool in_try)
6257 {
6258   tree compound_stmt;
6259
6260   /* Consume the `{'.  */
6261   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6262     return error_mark_node;
6263   /* Begin the compound-statement.  */
6264   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6265   /* Parse an (optional) statement-seq.  */
6266   cp_parser_statement_seq_opt (parser, in_statement_expr);
6267   /* Finish the compound-statement.  */
6268   finish_compound_stmt (compound_stmt);
6269   /* Consume the `}'.  */
6270   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6271
6272   return compound_stmt;
6273 }
6274
6275 /* Parse an (optional) statement-seq.
6276
6277    statement-seq:
6278      statement
6279      statement-seq [opt] statement  */
6280
6281 static void
6282 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6283 {
6284   /* Scan statements until there aren't any more.  */
6285   while (true)
6286     {
6287       /* If we're looking at a `}', then we've run out of statements.  */
6288       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6289           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6290         break;
6291
6292       /* Parse the statement.  */
6293       cp_parser_statement (parser, in_statement_expr);
6294     }
6295 }
6296
6297 /* Parse a selection-statement.
6298
6299    selection-statement:
6300      if ( condition ) statement
6301      if ( condition ) statement else statement
6302      switch ( condition ) statement
6303
6304    Returns the new IF_STMT or SWITCH_STMT.  */
6305
6306 static tree
6307 cp_parser_selection_statement (cp_parser* parser)
6308 {
6309   cp_token *token;
6310   enum rid keyword;
6311
6312   /* Peek at the next token.  */
6313   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6314
6315   /* See what kind of keyword it is.  */
6316   keyword = token->keyword;
6317   switch (keyword)
6318     {
6319     case RID_IF:
6320     case RID_SWITCH:
6321       {
6322         tree statement;
6323         tree condition;
6324
6325         /* Look for the `('.  */
6326         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6327           {
6328             cp_parser_skip_to_end_of_statement (parser);
6329             return error_mark_node;
6330           }
6331
6332         /* Begin the selection-statement.  */
6333         if (keyword == RID_IF)
6334           statement = begin_if_stmt ();
6335         else
6336           statement = begin_switch_stmt ();
6337
6338         /* Parse the condition.  */
6339         condition = cp_parser_condition (parser);
6340         /* Look for the `)'.  */
6341         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6342           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6343                                                  /*consume_paren=*/true);
6344
6345         if (keyword == RID_IF)
6346           {
6347             /* Add the condition.  */
6348             finish_if_stmt_cond (condition, statement);
6349
6350             /* Parse the then-clause.  */
6351             cp_parser_implicitly_scoped_statement (parser);
6352             finish_then_clause (statement);
6353
6354             /* If the next token is `else', parse the else-clause.  */
6355             if (cp_lexer_next_token_is_keyword (parser->lexer,
6356                                                 RID_ELSE))
6357               {
6358                 /* Consume the `else' keyword.  */
6359                 cp_lexer_consume_token (parser->lexer);
6360                 begin_else_clause (statement);
6361                 /* Parse the else-clause.  */
6362                 cp_parser_implicitly_scoped_statement (parser);
6363                 finish_else_clause (statement);
6364               }
6365
6366             /* Now we're all done with the if-statement.  */
6367             finish_if_stmt (statement);
6368           }
6369         else
6370           {
6371             bool in_switch_statement_p;
6372
6373             /* Add the condition.  */
6374             finish_switch_cond (condition, statement);
6375
6376             /* Parse the body of the switch-statement.  */
6377             in_switch_statement_p = parser->in_switch_statement_p;
6378             parser->in_switch_statement_p = true;
6379             cp_parser_implicitly_scoped_statement (parser);
6380             parser->in_switch_statement_p = in_switch_statement_p;
6381
6382             /* Now we're all done with the switch-statement.  */
6383             finish_switch_stmt (statement);
6384           }
6385
6386         return statement;
6387       }
6388       break;
6389
6390     default:
6391       cp_parser_error (parser, "expected selection-statement");
6392       return error_mark_node;
6393     }
6394 }
6395
6396 /* Parse a condition.
6397
6398    condition:
6399      expression
6400      type-specifier-seq declarator = assignment-expression
6401
6402    GNU Extension:
6403
6404    condition:
6405      type-specifier-seq declarator asm-specification [opt]
6406        attributes [opt] = assignment-expression
6407
6408    Returns the expression that should be tested.  */
6409
6410 static tree
6411 cp_parser_condition (cp_parser* parser)
6412 {
6413   cp_decl_specifier_seq type_specifiers;
6414   const char *saved_message;
6415
6416   /* Try the declaration first.  */
6417   cp_parser_parse_tentatively (parser);
6418   /* New types are not allowed in the type-specifier-seq for a
6419      condition.  */
6420   saved_message = parser->type_definition_forbidden_message;
6421   parser->type_definition_forbidden_message
6422     = "types may not be defined in conditions";
6423   /* Parse the type-specifier-seq.  */
6424   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6425                                 &type_specifiers);
6426   /* Restore the saved message.  */
6427   parser->type_definition_forbidden_message = saved_message;
6428   /* If all is well, we might be looking at a declaration.  */
6429   if (!cp_parser_error_occurred (parser))
6430     {
6431       tree decl;
6432       tree asm_specification;
6433       tree attributes;
6434       cp_declarator *declarator;
6435       tree initializer = NULL_TREE;
6436
6437       /* Parse the declarator.  */
6438       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6439                                          /*ctor_dtor_or_conv_p=*/NULL,
6440                                          /*parenthesized_p=*/NULL,
6441                                          /*member_p=*/false);
6442       /* Parse the attributes.  */
6443       attributes = cp_parser_attributes_opt (parser);
6444       /* Parse the asm-specification.  */
6445       asm_specification = cp_parser_asm_specification_opt (parser);
6446       /* If the next token is not an `=', then we might still be
6447          looking at an expression.  For example:
6448
6449            if (A(a).x)
6450
6451          looks like a decl-specifier-seq and a declarator -- but then
6452          there is no `=', so this is an expression.  */
6453       cp_parser_require (parser, CPP_EQ, "`='");
6454       /* If we did see an `=', then we are looking at a declaration
6455          for sure.  */
6456       if (cp_parser_parse_definitely (parser))
6457         {
6458           tree pushed_scope;
6459
6460           /* Create the declaration.  */
6461           decl = start_decl (declarator, &type_specifiers,
6462                              /*initialized_p=*/true,
6463                              attributes, /*prefix_attributes=*/NULL_TREE,
6464                              &pushed_scope);
6465           /* Parse the assignment-expression.  */
6466           initializer = cp_parser_assignment_expression (parser,
6467                                                          /*cast_p=*/false);
6468
6469           /* Process the initializer.  */
6470           cp_finish_decl (decl,
6471                           initializer,
6472                           asm_specification,
6473                           LOOKUP_ONLYCONVERTING);
6474
6475           if (pushed_scope)
6476             pop_scope (pushed_scope);
6477
6478           return convert_from_reference (decl);
6479         }
6480     }
6481   /* If we didn't even get past the declarator successfully, we are
6482      definitely not looking at a declaration.  */
6483   else
6484     cp_parser_abort_tentative_parse (parser);
6485
6486   /* Otherwise, we are looking at an expression.  */
6487   return cp_parser_expression (parser, /*cast_p=*/false);
6488 }
6489
6490 /* Parse an iteration-statement.
6491
6492    iteration-statement:
6493      while ( condition ) statement
6494      do statement while ( expression ) ;
6495      for ( for-init-statement condition [opt] ; expression [opt] )
6496        statement
6497
6498    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6499
6500 static tree
6501 cp_parser_iteration_statement (cp_parser* parser)
6502 {
6503   cp_token *token;
6504   enum rid keyword;
6505   tree statement;
6506   bool in_iteration_statement_p;
6507
6508
6509   /* Peek at the next token.  */
6510   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6511   if (!token)
6512     return error_mark_node;
6513
6514   /* Remember whether or not we are already within an iteration
6515      statement.  */
6516   in_iteration_statement_p = parser->in_iteration_statement_p;
6517
6518   /* See what kind of keyword it is.  */
6519   keyword = token->keyword;
6520   switch (keyword)
6521     {
6522     case RID_WHILE:
6523       {
6524         tree condition;
6525
6526         /* Begin the while-statement.  */
6527         statement = begin_while_stmt ();
6528         /* Look for the `('.  */
6529         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6530         /* Parse the condition.  */
6531         condition = cp_parser_condition (parser);
6532         finish_while_stmt_cond (condition, statement);
6533         /* Look for the `)'.  */
6534         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6535         /* Parse the dependent statement.  */
6536         parser->in_iteration_statement_p = true;
6537         cp_parser_already_scoped_statement (parser);
6538         parser->in_iteration_statement_p = in_iteration_statement_p;
6539         /* We're done with the while-statement.  */
6540         finish_while_stmt (statement);
6541       }
6542       break;
6543
6544     case RID_DO:
6545       {
6546         tree expression;
6547
6548         /* Begin the do-statement.  */
6549         statement = begin_do_stmt ();
6550         /* Parse the body of the do-statement.  */
6551         parser->in_iteration_statement_p = true;
6552         cp_parser_implicitly_scoped_statement (parser);
6553         parser->in_iteration_statement_p = in_iteration_statement_p;
6554         finish_do_body (statement);
6555         /* Look for the `while' keyword.  */
6556         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6557         /* Look for the `('.  */
6558         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6559         /* Parse the expression.  */
6560         expression = cp_parser_expression (parser, /*cast_p=*/false);
6561         /* We're done with the do-statement.  */
6562         finish_do_stmt (expression, statement);
6563         /* Look for the `)'.  */
6564         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6565         /* Look for the `;'.  */
6566         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6567       }
6568       break;
6569
6570     case RID_FOR:
6571       {
6572         tree condition = NULL_TREE;
6573         tree expression = NULL_TREE;
6574
6575         /* Begin the for-statement.  */
6576         statement = begin_for_stmt ();
6577         /* Look for the `('.  */
6578         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6579         /* Parse the initialization.  */
6580         cp_parser_for_init_statement (parser);
6581         finish_for_init_stmt (statement);
6582
6583         /* If there's a condition, process it.  */
6584         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6585           condition = cp_parser_condition (parser);
6586         finish_for_cond (condition, statement);
6587         /* Look for the `;'.  */
6588         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6589
6590         /* If there's an expression, process it.  */
6591         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6592           expression = cp_parser_expression (parser, /*cast_p=*/false);
6593         finish_for_expr (expression, statement);
6594         /* Look for the `)'.  */
6595         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6596
6597         /* Parse the body of the for-statement.  */
6598         parser->in_iteration_statement_p = true;
6599         cp_parser_already_scoped_statement (parser);
6600         parser->in_iteration_statement_p = in_iteration_statement_p;
6601
6602         /* We're done with the for-statement.  */
6603         finish_for_stmt (statement);
6604       }
6605       break;
6606
6607     default:
6608       cp_parser_error (parser, "expected iteration-statement");
6609       statement = error_mark_node;
6610       break;
6611     }
6612
6613   return statement;
6614 }
6615
6616 /* Parse a for-init-statement.
6617
6618    for-init-statement:
6619      expression-statement
6620      simple-declaration  */
6621
6622 static void
6623 cp_parser_for_init_statement (cp_parser* parser)
6624 {
6625   /* If the next token is a `;', then we have an empty
6626      expression-statement.  Grammatically, this is also a
6627      simple-declaration, but an invalid one, because it does not
6628      declare anything.  Therefore, if we did not handle this case
6629      specially, we would issue an error message about an invalid
6630      declaration.  */
6631   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6632     {
6633       /* We're going to speculatively look for a declaration, falling back
6634          to an expression, if necessary.  */
6635       cp_parser_parse_tentatively (parser);
6636       /* Parse the declaration.  */
6637       cp_parser_simple_declaration (parser,
6638                                     /*function_definition_allowed_p=*/false);
6639       /* If the tentative parse failed, then we shall need to look for an
6640          expression-statement.  */
6641       if (cp_parser_parse_definitely (parser))
6642         return;
6643     }
6644
6645   cp_parser_expression_statement (parser, false);
6646 }
6647
6648 /* Parse a jump-statement.
6649
6650    jump-statement:
6651      break ;
6652      continue ;
6653      return expression [opt] ;
6654      goto identifier ;
6655
6656    GNU extension:
6657
6658    jump-statement:
6659      goto * expression ;
6660
6661    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6662
6663 static tree
6664 cp_parser_jump_statement (cp_parser* parser)
6665 {
6666   tree statement = error_mark_node;
6667   cp_token *token;
6668   enum rid keyword;
6669
6670   /* Peek at the next token.  */
6671   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6672   if (!token)
6673     return error_mark_node;
6674
6675   /* See what kind of keyword it is.  */
6676   keyword = token->keyword;
6677   switch (keyword)
6678     {
6679     case RID_BREAK:
6680       if (!parser->in_switch_statement_p
6681           && !parser->in_iteration_statement_p)
6682         {
6683           error ("break statement not within loop or switch");
6684           statement = error_mark_node;
6685         }
6686       else
6687         statement = finish_break_stmt ();
6688       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6689       break;
6690
6691     case RID_CONTINUE:
6692       if (!parser->in_iteration_statement_p)
6693         {
6694           error ("continue statement not within a loop");
6695           statement = error_mark_node;
6696         }
6697       else
6698         statement = finish_continue_stmt ();
6699       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6700       break;
6701
6702     case RID_RETURN:
6703       {
6704         tree expr;
6705
6706         /* If the next token is a `;', then there is no
6707            expression.  */
6708         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6709           expr = cp_parser_expression (parser, /*cast_p=*/false);
6710         else
6711           expr = NULL_TREE;
6712         /* Build the return-statement.  */
6713         statement = finish_return_stmt (expr);
6714         /* Look for the final `;'.  */
6715         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6716       }
6717       break;
6718
6719     case RID_GOTO:
6720       /* Create the goto-statement.  */
6721       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6722         {
6723           /* Issue a warning about this use of a GNU extension.  */
6724           if (pedantic)
6725             pedwarn ("ISO C++ forbids computed gotos");
6726           /* Consume the '*' token.  */
6727           cp_lexer_consume_token (parser->lexer);
6728           /* Parse the dependent expression.  */
6729           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6730         }
6731       else
6732         finish_goto_stmt (cp_parser_identifier (parser));
6733       /* Look for the final `;'.  */
6734       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6735       break;
6736
6737     default:
6738       cp_parser_error (parser, "expected jump-statement");
6739       break;
6740     }
6741
6742   return statement;
6743 }
6744
6745 /* Parse a declaration-statement.
6746
6747    declaration-statement:
6748      block-declaration  */
6749
6750 static void
6751 cp_parser_declaration_statement (cp_parser* parser)
6752 {
6753   void *p;
6754
6755   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6756   p = obstack_alloc (&declarator_obstack, 0);
6757
6758  /* Parse the block-declaration.  */
6759   cp_parser_block_declaration (parser, /*statement_p=*/true);
6760
6761   /* Free any declarators allocated.  */
6762   obstack_free (&declarator_obstack, p);
6763
6764   /* Finish off the statement.  */
6765   finish_stmt ();
6766 }
6767
6768 /* Some dependent statements (like `if (cond) statement'), are
6769    implicitly in their own scope.  In other words, if the statement is
6770    a single statement (as opposed to a compound-statement), it is
6771    none-the-less treated as if it were enclosed in braces.  Any
6772    declarations appearing in the dependent statement are out of scope
6773    after control passes that point.  This function parses a statement,
6774    but ensures that is in its own scope, even if it is not a
6775    compound-statement.
6776
6777    Returns the new statement.  */
6778
6779 static tree
6780 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6781 {
6782   tree statement;
6783
6784   /* If the token is not a `{', then we must take special action.  */
6785   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6786     {
6787       /* Create a compound-statement.  */
6788       statement = begin_compound_stmt (0);
6789       /* Parse the dependent-statement.  */
6790       cp_parser_statement (parser, false);
6791       /* Finish the dummy compound-statement.  */
6792       finish_compound_stmt (statement);
6793     }
6794   /* Otherwise, we simply parse the statement directly.  */
6795   else
6796     statement = cp_parser_compound_statement (parser, NULL, false);
6797
6798   /* Return the statement.  */
6799   return statement;
6800 }
6801
6802 /* For some dependent statements (like `while (cond) statement'), we
6803    have already created a scope.  Therefore, even if the dependent
6804    statement is a compound-statement, we do not want to create another
6805    scope.  */
6806
6807 static void
6808 cp_parser_already_scoped_statement (cp_parser* parser)
6809 {
6810   /* If the token is a `{', then we must take special action.  */
6811   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6812     cp_parser_statement (parser, false);
6813   else
6814     {
6815       /* Avoid calling cp_parser_compound_statement, so that we
6816          don't create a new scope.  Do everything else by hand.  */
6817       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6818       cp_parser_statement_seq_opt (parser, false);
6819       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6820     }
6821 }
6822
6823 /* Declarations [gram.dcl.dcl] */
6824
6825 /* Parse an optional declaration-sequence.
6826
6827    declaration-seq:
6828      declaration
6829      declaration-seq declaration  */
6830
6831 static void
6832 cp_parser_declaration_seq_opt (cp_parser* parser)
6833 {
6834   while (true)
6835     {
6836       cp_token *token;
6837
6838       token = cp_lexer_peek_token (parser->lexer);
6839
6840       if (token->type == CPP_CLOSE_BRACE
6841           || token->type == CPP_EOF)
6842         break;
6843
6844       if (token->type == CPP_SEMICOLON)
6845         {
6846           /* A declaration consisting of a single semicolon is
6847              invalid.  Allow it unless we're being pedantic.  */
6848           cp_lexer_consume_token (parser->lexer);
6849           if (pedantic && !in_system_header)
6850             pedwarn ("extra %<;%>");
6851           continue;
6852         }
6853
6854       /* If we're entering or exiting a region that's implicitly
6855          extern "C", modify the lang context appropriately.  */
6856       if (!parser->implicit_extern_c && token->implicit_extern_c)
6857         {
6858           push_lang_context (lang_name_c);
6859           parser->implicit_extern_c = true;
6860         }
6861       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6862         {
6863           pop_lang_context ();
6864           parser->implicit_extern_c = false;
6865         }
6866
6867       if (token->type == CPP_PRAGMA)
6868         {
6869           /* A top-level declaration can consist solely of a #pragma.
6870              A nested declaration cannot, so this is done here and not
6871              in cp_parser_declaration.  (A #pragma at block scope is
6872              handled in cp_parser_statement.)  */
6873           cp_lexer_handle_pragma (parser->lexer);
6874           continue;
6875         }
6876
6877       /* Parse the declaration itself.  */
6878       cp_parser_declaration (parser);
6879     }
6880 }
6881
6882 /* Parse a declaration.
6883
6884    declaration:
6885      block-declaration
6886      function-definition
6887      template-declaration
6888      explicit-instantiation
6889      explicit-specialization
6890      linkage-specification
6891      namespace-definition
6892
6893    GNU extension:
6894
6895    declaration:
6896       __extension__ declaration */
6897
6898 static void
6899 cp_parser_declaration (cp_parser* parser)
6900 {
6901   cp_token token1;
6902   cp_token token2;
6903   int saved_pedantic;
6904   void *p;
6905
6906   /* Check for the `__extension__' keyword.  */
6907   if (cp_parser_extension_opt (parser, &saved_pedantic))
6908     {
6909       /* Parse the qualified declaration.  */
6910       cp_parser_declaration (parser);
6911       /* Restore the PEDANTIC flag.  */
6912       pedantic = saved_pedantic;
6913
6914       return;
6915     }
6916
6917   /* Try to figure out what kind of declaration is present.  */
6918   token1 = *cp_lexer_peek_token (parser->lexer);
6919
6920   if (token1.type != CPP_EOF)
6921     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6922
6923   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6924   p = obstack_alloc (&declarator_obstack, 0);
6925
6926   /* If the next token is `extern' and the following token is a string
6927      literal, then we have a linkage specification.  */
6928   if (token1.keyword == RID_EXTERN
6929       && cp_parser_is_string_literal (&token2))
6930     cp_parser_linkage_specification (parser);
6931   /* If the next token is `template', then we have either a template
6932      declaration, an explicit instantiation, or an explicit
6933      specialization.  */
6934   else if (token1.keyword == RID_TEMPLATE)
6935     {
6936       /* `template <>' indicates a template specialization.  */
6937       if (token2.type == CPP_LESS
6938           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6939         cp_parser_explicit_specialization (parser);
6940       /* `template <' indicates a template declaration.  */
6941       else if (token2.type == CPP_LESS)
6942         cp_parser_template_declaration (parser, /*member_p=*/false);
6943       /* Anything else must be an explicit instantiation.  */
6944       else
6945         cp_parser_explicit_instantiation (parser);
6946     }
6947   /* If the next token is `export', then we have a template
6948      declaration.  */
6949   else if (token1.keyword == RID_EXPORT)
6950     cp_parser_template_declaration (parser, /*member_p=*/false);
6951   /* If the next token is `extern', 'static' or 'inline' and the one
6952      after that is `template', we have a GNU extended explicit
6953      instantiation directive.  */
6954   else if (cp_parser_allow_gnu_extensions_p (parser)
6955            && (token1.keyword == RID_EXTERN
6956                || token1.keyword == RID_STATIC
6957                || token1.keyword == RID_INLINE)
6958            && token2.keyword == RID_TEMPLATE)
6959     cp_parser_explicit_instantiation (parser);
6960   /* If the next token is `namespace', check for a named or unnamed
6961      namespace definition.  */
6962   else if (token1.keyword == RID_NAMESPACE
6963            && (/* A named namespace definition.  */
6964                (token2.type == CPP_NAME
6965                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6966                     == CPP_OPEN_BRACE))
6967                /* An unnamed namespace definition.  */
6968                || token2.type == CPP_OPEN_BRACE))
6969     cp_parser_namespace_definition (parser);
6970   /* Objective-C++ declaration/definition.  */
6971   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
6972     cp_parser_objc_declaration (parser);
6973   /* We must have either a block declaration or a function
6974      definition.  */
6975   else
6976     /* Try to parse a block-declaration, or a function-definition.  */
6977     cp_parser_block_declaration (parser, /*statement_p=*/false);
6978
6979   /* Free any declarators allocated.  */
6980   obstack_free (&declarator_obstack, p);
6981 }
6982
6983 /* Parse a block-declaration.
6984
6985    block-declaration:
6986      simple-declaration
6987      asm-definition
6988      namespace-alias-definition
6989      using-declaration
6990      using-directive
6991
6992    GNU Extension:
6993
6994    block-declaration:
6995      __extension__ block-declaration
6996      label-declaration
6997
6998    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6999    part of a declaration-statement.  */
7000
7001 static void
7002 cp_parser_block_declaration (cp_parser *parser,
7003                              bool      statement_p)
7004 {
7005   cp_token *token1;
7006   int saved_pedantic;
7007
7008   /* Check for the `__extension__' keyword.  */
7009   if (cp_parser_extension_opt (parser, &saved_pedantic))
7010     {
7011       /* Parse the qualified declaration.  */
7012       cp_parser_block_declaration (parser, statement_p);
7013       /* Restore the PEDANTIC flag.  */
7014       pedantic = saved_pedantic;
7015
7016       return;
7017     }
7018
7019   /* Peek at the next token to figure out which kind of declaration is
7020      present.  */
7021   token1 = cp_lexer_peek_token (parser->lexer);
7022
7023   /* If the next keyword is `asm', we have an asm-definition.  */
7024   if (token1->keyword == RID_ASM)
7025     {
7026       if (statement_p)
7027         cp_parser_commit_to_tentative_parse (parser);
7028       cp_parser_asm_definition (parser);
7029     }
7030   /* If the next keyword is `namespace', we have a
7031      namespace-alias-definition.  */
7032   else if (token1->keyword == RID_NAMESPACE)
7033     cp_parser_namespace_alias_definition (parser);
7034   /* If the next keyword is `using', we have either a
7035      using-declaration or a using-directive.  */
7036   else if (token1->keyword == RID_USING)
7037     {
7038       cp_token *token2;
7039
7040       if (statement_p)
7041         cp_parser_commit_to_tentative_parse (parser);
7042       /* If the token after `using' is `namespace', then we have a
7043          using-directive.  */
7044       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7045       if (token2->keyword == RID_NAMESPACE)
7046         cp_parser_using_directive (parser);
7047       /* Otherwise, it's a using-declaration.  */
7048       else
7049         cp_parser_using_declaration (parser);
7050     }
7051   /* If the next keyword is `__label__' we have a label declaration.  */
7052   else if (token1->keyword == RID_LABEL)
7053     {
7054       if (statement_p)
7055         cp_parser_commit_to_tentative_parse (parser);
7056       cp_parser_label_declaration (parser);
7057     }
7058   /* Anything else must be a simple-declaration.  */
7059   else
7060     cp_parser_simple_declaration (parser, !statement_p);
7061 }
7062
7063 /* Parse a simple-declaration.
7064
7065    simple-declaration:
7066      decl-specifier-seq [opt] init-declarator-list [opt] ;
7067
7068    init-declarator-list:
7069      init-declarator
7070      init-declarator-list , init-declarator
7071
7072    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7073    function-definition as a simple-declaration.  */
7074
7075 static void
7076 cp_parser_simple_declaration (cp_parser* parser,
7077                               bool function_definition_allowed_p)
7078 {
7079   cp_decl_specifier_seq decl_specifiers;
7080   int declares_class_or_enum;
7081   bool saw_declarator;
7082
7083   /* Defer access checks until we know what is being declared; the
7084      checks for names appearing in the decl-specifier-seq should be
7085      done as if we were in the scope of the thing being declared.  */
7086   push_deferring_access_checks (dk_deferred);
7087
7088   /* Parse the decl-specifier-seq.  We have to keep track of whether
7089      or not the decl-specifier-seq declares a named class or
7090      enumeration type, since that is the only case in which the
7091      init-declarator-list is allowed to be empty.
7092
7093      [dcl.dcl]
7094
7095      In a simple-declaration, the optional init-declarator-list can be
7096      omitted only when declaring a class or enumeration, that is when
7097      the decl-specifier-seq contains either a class-specifier, an
7098      elaborated-type-specifier, or an enum-specifier.  */
7099   cp_parser_decl_specifier_seq (parser,
7100                                 CP_PARSER_FLAGS_OPTIONAL,
7101                                 &decl_specifiers,
7102                                 &declares_class_or_enum);
7103   /* We no longer need to defer access checks.  */
7104   stop_deferring_access_checks ();
7105
7106   /* In a block scope, a valid declaration must always have a
7107      decl-specifier-seq.  By not trying to parse declarators, we can
7108      resolve the declaration/expression ambiguity more quickly.  */
7109   if (!function_definition_allowed_p
7110       && !decl_specifiers.any_specifiers_p)
7111     {
7112       cp_parser_error (parser, "expected declaration");
7113       goto done;
7114     }
7115
7116   /* If the next two tokens are both identifiers, the code is
7117      erroneous. The usual cause of this situation is code like:
7118
7119        T t;
7120
7121      where "T" should name a type -- but does not.  */
7122   if (!decl_specifiers.type
7123       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7124     {
7125       /* If parsing tentatively, we should commit; we really are
7126          looking at a declaration.  */
7127       cp_parser_commit_to_tentative_parse (parser);
7128       /* Give up.  */
7129       goto done;
7130     }
7131
7132   /* If we have seen at least one decl-specifier, and the next token
7133      is not a parenthesis, then we must be looking at a declaration.
7134      (After "int (" we might be looking at a functional cast.)  */
7135   if (decl_specifiers.any_specifiers_p
7136       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7137     cp_parser_commit_to_tentative_parse (parser);
7138
7139   /* Keep going until we hit the `;' at the end of the simple
7140      declaration.  */
7141   saw_declarator = false;
7142   while (cp_lexer_next_token_is_not (parser->lexer,
7143                                      CPP_SEMICOLON))
7144     {
7145       cp_token *token;
7146       bool function_definition_p;
7147       tree decl;
7148
7149       saw_declarator = true;
7150       /* Parse the init-declarator.  */
7151       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7152                                         function_definition_allowed_p,
7153                                         /*member_p=*/false,
7154                                         declares_class_or_enum,
7155                                         &function_definition_p);
7156       /* If an error occurred while parsing tentatively, exit quickly.
7157          (That usually happens when in the body of a function; each
7158          statement is treated as a declaration-statement until proven
7159          otherwise.)  */
7160       if (cp_parser_error_occurred (parser))
7161         goto done;
7162       /* Handle function definitions specially.  */
7163       if (function_definition_p)
7164         {
7165           /* If the next token is a `,', then we are probably
7166              processing something like:
7167
7168                void f() {}, *p;
7169
7170              which is erroneous.  */
7171           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7172             error ("mixing declarations and function-definitions is forbidden");
7173           /* Otherwise, we're done with the list of declarators.  */
7174           else
7175             {
7176               pop_deferring_access_checks ();
7177               return;
7178             }
7179         }
7180       /* The next token should be either a `,' or a `;'.  */
7181       token = cp_lexer_peek_token (parser->lexer);
7182       /* If it's a `,', there are more declarators to come.  */
7183       if (token->type == CPP_COMMA)
7184         cp_lexer_consume_token (parser->lexer);
7185       /* If it's a `;', we are done.  */
7186       else if (token->type == CPP_SEMICOLON)
7187         break;
7188       /* Anything else is an error.  */
7189       else
7190         {
7191           /* If we have already issued an error message we don't need
7192              to issue another one.  */
7193           if (decl != error_mark_node
7194               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7195             cp_parser_error (parser, "expected %<,%> or %<;%>");
7196           /* Skip tokens until we reach the end of the statement.  */
7197           cp_parser_skip_to_end_of_statement (parser);
7198           /* If the next token is now a `;', consume it.  */
7199           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7200             cp_lexer_consume_token (parser->lexer);
7201           goto done;
7202         }
7203       /* After the first time around, a function-definition is not
7204          allowed -- even if it was OK at first.  For example:
7205
7206            int i, f() {}
7207
7208          is not valid.  */
7209       function_definition_allowed_p = false;
7210     }
7211
7212   /* Issue an error message if no declarators are present, and the
7213      decl-specifier-seq does not itself declare a class or
7214      enumeration.  */
7215   if (!saw_declarator)
7216     {
7217       if (cp_parser_declares_only_class_p (parser))
7218         shadow_tag (&decl_specifiers);
7219       /* Perform any deferred access checks.  */
7220       perform_deferred_access_checks ();
7221     }
7222
7223   /* Consume the `;'.  */
7224   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7225
7226  done:
7227   pop_deferring_access_checks ();
7228 }
7229
7230 /* Parse a decl-specifier-seq.
7231
7232    decl-specifier-seq:
7233      decl-specifier-seq [opt] decl-specifier
7234
7235    decl-specifier:
7236      storage-class-specifier
7237      type-specifier
7238      function-specifier
7239      friend
7240      typedef
7241
7242    GNU Extension:
7243
7244    decl-specifier:
7245      attributes
7246
7247    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7248
7249    The parser flags FLAGS is used to control type-specifier parsing.
7250
7251    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7252    flags:
7253
7254      1: one of the decl-specifiers is an elaborated-type-specifier
7255         (i.e., a type declaration)
7256      2: one of the decl-specifiers is an enum-specifier or a
7257         class-specifier (i.e., a type definition)
7258
7259    */
7260
7261 static void
7262 cp_parser_decl_specifier_seq (cp_parser* parser,
7263                               cp_parser_flags flags,
7264                               cp_decl_specifier_seq *decl_specs,
7265                               int* declares_class_or_enum)
7266 {
7267   bool constructor_possible_p = !parser->in_declarator_p;
7268
7269   /* Clear DECL_SPECS.  */
7270   clear_decl_specs (decl_specs);
7271
7272   /* Assume no class or enumeration type is declared.  */
7273   *declares_class_or_enum = 0;
7274
7275   /* Keep reading specifiers until there are no more to read.  */
7276   while (true)
7277     {
7278       bool constructor_p;
7279       bool found_decl_spec;
7280       cp_token *token;
7281
7282       /* Peek at the next token.  */
7283       token = cp_lexer_peek_token (parser->lexer);
7284       /* Handle attributes.  */
7285       if (token->keyword == RID_ATTRIBUTE)
7286         {
7287           /* Parse the attributes.  */
7288           decl_specs->attributes
7289             = chainon (decl_specs->attributes,
7290                        cp_parser_attributes_opt (parser));
7291           continue;
7292         }
7293       /* Assume we will find a decl-specifier keyword.  */
7294       found_decl_spec = true;
7295       /* If the next token is an appropriate keyword, we can simply
7296          add it to the list.  */
7297       switch (token->keyword)
7298         {
7299           /* decl-specifier:
7300                friend  */
7301         case RID_FRIEND:
7302           if (decl_specs->specs[(int) ds_friend]++)
7303             error ("duplicate %<friend%>");
7304           /* Consume the token.  */
7305           cp_lexer_consume_token (parser->lexer);
7306           break;
7307
7308           /* function-specifier:
7309                inline
7310                virtual
7311                explicit  */
7312         case RID_INLINE:
7313         case RID_VIRTUAL:
7314         case RID_EXPLICIT:
7315           cp_parser_function_specifier_opt (parser, decl_specs);
7316           break;
7317
7318           /* decl-specifier:
7319                typedef  */
7320         case RID_TYPEDEF:
7321           ++decl_specs->specs[(int) ds_typedef];
7322           /* Consume the token.  */
7323           cp_lexer_consume_token (parser->lexer);
7324           /* A constructor declarator cannot appear in a typedef.  */
7325           constructor_possible_p = false;
7326           /* The "typedef" keyword can only occur in a declaration; we
7327              may as well commit at this point.  */
7328           cp_parser_commit_to_tentative_parse (parser);
7329           break;
7330
7331           /* storage-class-specifier:
7332                auto
7333                register
7334                static
7335                extern
7336                mutable
7337
7338              GNU Extension:
7339                thread  */
7340         case RID_AUTO:
7341           /* Consume the token.  */
7342           cp_lexer_consume_token (parser->lexer);
7343           cp_parser_set_storage_class (decl_specs, sc_auto);
7344           break;
7345         case RID_REGISTER:
7346           /* Consume the token.  */
7347           cp_lexer_consume_token (parser->lexer);
7348           cp_parser_set_storage_class (decl_specs, sc_register);
7349           break;
7350         case RID_STATIC:
7351           /* Consume the token.  */
7352           cp_lexer_consume_token (parser->lexer);
7353           if (decl_specs->specs[(int) ds_thread])
7354             {
7355               error ("%<__thread%> before %<static%>");
7356               decl_specs->specs[(int) ds_thread] = 0;
7357             }
7358           cp_parser_set_storage_class (decl_specs, sc_static);
7359           break;
7360         case RID_EXTERN:
7361           /* Consume the token.  */
7362           cp_lexer_consume_token (parser->lexer);
7363           if (decl_specs->specs[(int) ds_thread])
7364             {
7365               error ("%<__thread%> before %<extern%>");
7366               decl_specs->specs[(int) ds_thread] = 0;
7367             }
7368           cp_parser_set_storage_class (decl_specs, sc_extern);
7369           break;
7370         case RID_MUTABLE:
7371           /* Consume the token.  */
7372           cp_lexer_consume_token (parser->lexer);
7373           cp_parser_set_storage_class (decl_specs, sc_mutable);
7374           break;
7375         case RID_THREAD:
7376           /* Consume the token.  */
7377           cp_lexer_consume_token (parser->lexer);
7378           ++decl_specs->specs[(int) ds_thread];
7379           break;
7380
7381         default:
7382           /* We did not yet find a decl-specifier yet.  */
7383           found_decl_spec = false;
7384           break;
7385         }
7386
7387       /* Constructors are a special case.  The `S' in `S()' is not a
7388          decl-specifier; it is the beginning of the declarator.  */
7389       constructor_p
7390         = (!found_decl_spec
7391            && constructor_possible_p
7392            && (cp_parser_constructor_declarator_p
7393                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7394
7395       /* If we don't have a DECL_SPEC yet, then we must be looking at
7396          a type-specifier.  */
7397       if (!found_decl_spec && !constructor_p)
7398         {
7399           int decl_spec_declares_class_or_enum;
7400           bool is_cv_qualifier;
7401           tree type_spec;
7402
7403           type_spec
7404             = cp_parser_type_specifier (parser, flags,
7405                                         decl_specs,
7406                                         /*is_declaration=*/true,
7407                                         &decl_spec_declares_class_or_enum,
7408                                         &is_cv_qualifier);
7409
7410           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7411
7412           /* If this type-specifier referenced a user-defined type
7413              (a typedef, class-name, etc.), then we can't allow any
7414              more such type-specifiers henceforth.
7415
7416              [dcl.spec]
7417
7418              The longest sequence of decl-specifiers that could
7419              possibly be a type name is taken as the
7420              decl-specifier-seq of a declaration.  The sequence shall
7421              be self-consistent as described below.
7422
7423              [dcl.type]
7424
7425              As a general rule, at most one type-specifier is allowed
7426              in the complete decl-specifier-seq of a declaration.  The
7427              only exceptions are the following:
7428
7429              -- const or volatile can be combined with any other
7430                 type-specifier.
7431
7432              -- signed or unsigned can be combined with char, long,
7433                 short, or int.
7434
7435              -- ..
7436
7437              Example:
7438
7439                typedef char* Pc;
7440                void g (const int Pc);
7441
7442              Here, Pc is *not* part of the decl-specifier seq; it's
7443              the declarator.  Therefore, once we see a type-specifier
7444              (other than a cv-qualifier), we forbid any additional
7445              user-defined types.  We *do* still allow things like `int
7446              int' to be considered a decl-specifier-seq, and issue the
7447              error message later.  */
7448           if (type_spec && !is_cv_qualifier)
7449             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7450           /* A constructor declarator cannot follow a type-specifier.  */
7451           if (type_spec)
7452             {
7453               constructor_possible_p = false;
7454               found_decl_spec = true;
7455             }
7456         }
7457
7458       /* If we still do not have a DECL_SPEC, then there are no more
7459          decl-specifiers.  */
7460       if (!found_decl_spec)
7461         break;
7462
7463       decl_specs->any_specifiers_p = true;
7464       /* After we see one decl-specifier, further decl-specifiers are
7465          always optional.  */
7466       flags |= CP_PARSER_FLAGS_OPTIONAL;
7467     }
7468
7469   /* Don't allow a friend specifier with a class definition.  */
7470   if (decl_specs->specs[(int) ds_friend] != 0
7471       && (*declares_class_or_enum & 2))
7472     error ("class definition may not be declared a friend");
7473 }
7474
7475 /* Parse an (optional) storage-class-specifier.
7476
7477    storage-class-specifier:
7478      auto
7479      register
7480      static
7481      extern
7482      mutable
7483
7484    GNU Extension:
7485
7486    storage-class-specifier:
7487      thread
7488
7489    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7490
7491 static tree
7492 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7493 {
7494   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7495     {
7496     case RID_AUTO:
7497     case RID_REGISTER:
7498     case RID_STATIC:
7499     case RID_EXTERN:
7500     case RID_MUTABLE:
7501     case RID_THREAD:
7502       /* Consume the token.  */
7503       return cp_lexer_consume_token (parser->lexer)->value;
7504
7505     default:
7506       return NULL_TREE;
7507     }
7508 }
7509
7510 /* Parse an (optional) function-specifier.
7511
7512    function-specifier:
7513      inline
7514      virtual
7515      explicit
7516
7517    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7518    Updates DECL_SPECS, if it is non-NULL.  */
7519
7520 static tree
7521 cp_parser_function_specifier_opt (cp_parser* parser,
7522                                   cp_decl_specifier_seq *decl_specs)
7523 {
7524   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7525     {
7526     case RID_INLINE:
7527       if (decl_specs)
7528         ++decl_specs->specs[(int) ds_inline];
7529       break;
7530
7531     case RID_VIRTUAL:
7532       if (decl_specs)
7533         ++decl_specs->specs[(int) ds_virtual];
7534       break;
7535
7536     case RID_EXPLICIT:
7537       if (decl_specs)
7538         ++decl_specs->specs[(int) ds_explicit];
7539       break;
7540
7541     default:
7542       return NULL_TREE;
7543     }
7544
7545   /* Consume the token.  */
7546   return cp_lexer_consume_token (parser->lexer)->value;
7547 }
7548
7549 /* Parse a linkage-specification.
7550
7551    linkage-specification:
7552      extern string-literal { declaration-seq [opt] }
7553      extern string-literal declaration  */
7554
7555 static void
7556 cp_parser_linkage_specification (cp_parser* parser)
7557 {
7558   tree linkage;
7559
7560   /* Look for the `extern' keyword.  */
7561   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7562
7563   /* Look for the string-literal.  */
7564   linkage = cp_parser_string_literal (parser, false, false);
7565
7566   /* Transform the literal into an identifier.  If the literal is a
7567      wide-character string, or contains embedded NULs, then we can't
7568      handle it as the user wants.  */
7569   if (strlen (TREE_STRING_POINTER (linkage))
7570       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7571     {
7572       cp_parser_error (parser, "invalid linkage-specification");
7573       /* Assume C++ linkage.  */
7574       linkage = lang_name_cplusplus;
7575     }
7576   else
7577     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7578
7579   /* We're now using the new linkage.  */
7580   push_lang_context (linkage);
7581
7582   /* If the next token is a `{', then we're using the first
7583      production.  */
7584   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7585     {
7586       /* Consume the `{' token.  */
7587       cp_lexer_consume_token (parser->lexer);
7588       /* Parse the declarations.  */
7589       cp_parser_declaration_seq_opt (parser);
7590       /* Look for the closing `}'.  */
7591       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7592     }
7593   /* Otherwise, there's just one declaration.  */
7594   else
7595     {
7596       bool saved_in_unbraced_linkage_specification_p;
7597
7598       saved_in_unbraced_linkage_specification_p
7599         = parser->in_unbraced_linkage_specification_p;
7600       parser->in_unbraced_linkage_specification_p = true;
7601       have_extern_spec = true;
7602       cp_parser_declaration (parser);
7603       have_extern_spec = false;
7604       parser->in_unbraced_linkage_specification_p
7605         = saved_in_unbraced_linkage_specification_p;
7606     }
7607
7608   /* We're done with the linkage-specification.  */
7609   pop_lang_context ();
7610 }
7611
7612 /* Special member functions [gram.special] */
7613
7614 /* Parse a conversion-function-id.
7615
7616    conversion-function-id:
7617      operator conversion-type-id
7618
7619    Returns an IDENTIFIER_NODE representing the operator.  */
7620
7621 static tree
7622 cp_parser_conversion_function_id (cp_parser* parser)
7623 {
7624   tree type;
7625   tree saved_scope;
7626   tree saved_qualifying_scope;
7627   tree saved_object_scope;
7628   tree pushed_scope = NULL_TREE;
7629
7630   /* Look for the `operator' token.  */
7631   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7632     return error_mark_node;
7633   /* When we parse the conversion-type-id, the current scope will be
7634      reset.  However, we need that information in able to look up the
7635      conversion function later, so we save it here.  */
7636   saved_scope = parser->scope;
7637   saved_qualifying_scope = parser->qualifying_scope;
7638   saved_object_scope = parser->object_scope;
7639   /* We must enter the scope of the class so that the names of
7640      entities declared within the class are available in the
7641      conversion-type-id.  For example, consider:
7642
7643        struct S {
7644          typedef int I;
7645          operator I();
7646        };
7647
7648        S::operator I() { ... }
7649
7650      In order to see that `I' is a type-name in the definition, we
7651      must be in the scope of `S'.  */
7652   if (saved_scope)
7653     pushed_scope = push_scope (saved_scope);
7654   /* Parse the conversion-type-id.  */
7655   type = cp_parser_conversion_type_id (parser);
7656   /* Leave the scope of the class, if any.  */
7657   if (pushed_scope)
7658     pop_scope (pushed_scope);
7659   /* Restore the saved scope.  */
7660   parser->scope = saved_scope;
7661   parser->qualifying_scope = saved_qualifying_scope;
7662   parser->object_scope = saved_object_scope;
7663   /* If the TYPE is invalid, indicate failure.  */
7664   if (type == error_mark_node)
7665     return error_mark_node;
7666   return mangle_conv_op_name_for_type (type);
7667 }
7668
7669 /* Parse a conversion-type-id:
7670
7671    conversion-type-id:
7672      type-specifier-seq conversion-declarator [opt]
7673
7674    Returns the TYPE specified.  */
7675
7676 static tree
7677 cp_parser_conversion_type_id (cp_parser* parser)
7678 {
7679   tree attributes;
7680   cp_decl_specifier_seq type_specifiers;
7681   cp_declarator *declarator;
7682   tree type_specified;
7683
7684   /* Parse the attributes.  */
7685   attributes = cp_parser_attributes_opt (parser);
7686   /* Parse the type-specifiers.  */
7687   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7688                                 &type_specifiers);
7689   /* If that didn't work, stop.  */
7690   if (type_specifiers.type == error_mark_node)
7691     return error_mark_node;
7692   /* Parse the conversion-declarator.  */
7693   declarator = cp_parser_conversion_declarator_opt (parser);
7694
7695   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7696                                     /*initialized=*/0, &attributes);
7697   if (attributes)
7698     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7699   return type_specified;
7700 }
7701
7702 /* Parse an (optional) conversion-declarator.
7703
7704    conversion-declarator:
7705      ptr-operator conversion-declarator [opt]
7706
7707    */
7708
7709 static cp_declarator *
7710 cp_parser_conversion_declarator_opt (cp_parser* parser)
7711 {
7712   enum tree_code code;
7713   tree class_type;
7714   cp_cv_quals cv_quals;
7715
7716   /* We don't know if there's a ptr-operator next, or not.  */
7717   cp_parser_parse_tentatively (parser);
7718   /* Try the ptr-operator.  */
7719   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7720   /* If it worked, look for more conversion-declarators.  */
7721   if (cp_parser_parse_definitely (parser))
7722     {
7723       cp_declarator *declarator;
7724
7725       /* Parse another optional declarator.  */
7726       declarator = cp_parser_conversion_declarator_opt (parser);
7727
7728       /* Create the representation of the declarator.  */
7729       if (class_type)
7730         declarator = make_ptrmem_declarator (cv_quals, class_type,
7731                                              declarator);
7732       else if (code == INDIRECT_REF)
7733         declarator = make_pointer_declarator (cv_quals, declarator);
7734       else
7735         declarator = make_reference_declarator (cv_quals, declarator);
7736
7737       return declarator;
7738    }
7739
7740   return NULL;
7741 }
7742
7743 /* Parse an (optional) ctor-initializer.
7744
7745    ctor-initializer:
7746      : mem-initializer-list
7747
7748    Returns TRUE iff the ctor-initializer was actually present.  */
7749
7750 static bool
7751 cp_parser_ctor_initializer_opt (cp_parser* parser)
7752 {
7753   /* If the next token is not a `:', then there is no
7754      ctor-initializer.  */
7755   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7756     {
7757       /* Do default initialization of any bases and members.  */
7758       if (DECL_CONSTRUCTOR_P (current_function_decl))
7759         finish_mem_initializers (NULL_TREE);
7760
7761       return false;
7762     }
7763
7764   /* Consume the `:' token.  */
7765   cp_lexer_consume_token (parser->lexer);
7766   /* And the mem-initializer-list.  */
7767   cp_parser_mem_initializer_list (parser);
7768
7769   return true;
7770 }
7771
7772 /* Parse a mem-initializer-list.
7773
7774    mem-initializer-list:
7775      mem-initializer
7776      mem-initializer , mem-initializer-list  */
7777
7778 static void
7779 cp_parser_mem_initializer_list (cp_parser* parser)
7780 {
7781   tree mem_initializer_list = NULL_TREE;
7782
7783   /* Let the semantic analysis code know that we are starting the
7784      mem-initializer-list.  */
7785   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7786     error ("only constructors take base initializers");
7787
7788   /* Loop through the list.  */
7789   while (true)
7790     {
7791       tree mem_initializer;
7792
7793       /* Parse the mem-initializer.  */
7794       mem_initializer = cp_parser_mem_initializer (parser);
7795       /* Add it to the list, unless it was erroneous.  */
7796       if (mem_initializer)
7797         {
7798           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7799           mem_initializer_list = mem_initializer;
7800         }
7801       /* If the next token is not a `,', we're done.  */
7802       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7803         break;
7804       /* Consume the `,' token.  */
7805       cp_lexer_consume_token (parser->lexer);
7806     }
7807
7808   /* Perform semantic analysis.  */
7809   if (DECL_CONSTRUCTOR_P (current_function_decl))
7810     finish_mem_initializers (mem_initializer_list);
7811 }
7812
7813 /* Parse a mem-initializer.
7814
7815    mem-initializer:
7816      mem-initializer-id ( expression-list [opt] )
7817
7818    GNU extension:
7819
7820    mem-initializer:
7821      ( expression-list [opt] )
7822
7823    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7824    class) or FIELD_DECL (for a non-static data member) to initialize;
7825    the TREE_VALUE is the expression-list.  */
7826
7827 static tree
7828 cp_parser_mem_initializer (cp_parser* parser)
7829 {
7830   tree mem_initializer_id;
7831   tree expression_list;
7832   tree member;
7833
7834   /* Find out what is being initialized.  */
7835   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7836     {
7837       pedwarn ("anachronistic old-style base class initializer");
7838       mem_initializer_id = NULL_TREE;
7839     }
7840   else
7841     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7842   member = expand_member_init (mem_initializer_id);
7843   if (member && !DECL_P (member))
7844     in_base_initializer = 1;
7845
7846   expression_list
7847     = cp_parser_parenthesized_expression_list (parser, false,
7848                                                /*cast_p=*/false,
7849                                                /*non_constant_p=*/NULL);
7850   if (!expression_list)
7851     expression_list = void_type_node;
7852
7853   in_base_initializer = 0;
7854
7855   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7856 }
7857
7858 /* Parse a mem-initializer-id.
7859
7860    mem-initializer-id:
7861      :: [opt] nested-name-specifier [opt] class-name
7862      identifier
7863
7864    Returns a TYPE indicating the class to be initializer for the first
7865    production.  Returns an IDENTIFIER_NODE indicating the data member
7866    to be initialized for the second production.  */
7867
7868 static tree
7869 cp_parser_mem_initializer_id (cp_parser* parser)
7870 {
7871   bool global_scope_p;
7872   bool nested_name_specifier_p;
7873   bool template_p = false;
7874   tree id;
7875
7876   /* `typename' is not allowed in this context ([temp.res]).  */
7877   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7878     {
7879       error ("keyword %<typename%> not allowed in this context (a qualified "
7880              "member initializer is implicitly a type)");
7881       cp_lexer_consume_token (parser->lexer);
7882     }
7883   /* Look for the optional `::' operator.  */
7884   global_scope_p
7885     = (cp_parser_global_scope_opt (parser,
7886                                    /*current_scope_valid_p=*/false)
7887        != NULL_TREE);
7888   /* Look for the optional nested-name-specifier.  The simplest way to
7889      implement:
7890
7891        [temp.res]
7892
7893        The keyword `typename' is not permitted in a base-specifier or
7894        mem-initializer; in these contexts a qualified name that
7895        depends on a template-parameter is implicitly assumed to be a
7896        type name.
7897
7898      is to assume that we have seen the `typename' keyword at this
7899      point.  */
7900   nested_name_specifier_p
7901     = (cp_parser_nested_name_specifier_opt (parser,
7902                                             /*typename_keyword_p=*/true,
7903                                             /*check_dependency_p=*/true,
7904                                             /*type_p=*/true,
7905                                             /*is_declaration=*/true)
7906        != NULL_TREE);
7907   if (nested_name_specifier_p)
7908     template_p = cp_parser_optional_template_keyword (parser);
7909   /* If there is a `::' operator or a nested-name-specifier, then we
7910      are definitely looking for a class-name.  */
7911   if (global_scope_p || nested_name_specifier_p)
7912     return cp_parser_class_name (parser,
7913                                  /*typename_keyword_p=*/true,
7914                                  /*template_keyword_p=*/template_p,
7915                                  none_type,
7916                                  /*check_dependency_p=*/true,
7917                                  /*class_head_p=*/false,
7918                                  /*is_declaration=*/true);
7919   /* Otherwise, we could also be looking for an ordinary identifier.  */
7920   cp_parser_parse_tentatively (parser);
7921   /* Try a class-name.  */
7922   id = cp_parser_class_name (parser,
7923                              /*typename_keyword_p=*/true,
7924                              /*template_keyword_p=*/false,
7925                              none_type,
7926                              /*check_dependency_p=*/true,
7927                              /*class_head_p=*/false,
7928                              /*is_declaration=*/true);
7929   /* If we found one, we're done.  */
7930   if (cp_parser_parse_definitely (parser))
7931     return id;
7932   /* Otherwise, look for an ordinary identifier.  */
7933   return cp_parser_identifier (parser);
7934 }
7935
7936 /* Overloading [gram.over] */
7937
7938 /* Parse an operator-function-id.
7939
7940    operator-function-id:
7941      operator operator
7942
7943    Returns an IDENTIFIER_NODE for the operator which is a
7944    human-readable spelling of the identifier, e.g., `operator +'.  */
7945
7946 static tree
7947 cp_parser_operator_function_id (cp_parser* parser)
7948 {
7949   /* Look for the `operator' keyword.  */
7950   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7951     return error_mark_node;
7952   /* And then the name of the operator itself.  */
7953   return cp_parser_operator (parser);
7954 }
7955
7956 /* Parse an operator.
7957
7958    operator:
7959      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7960      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7961      || ++ -- , ->* -> () []
7962
7963    GNU Extensions:
7964
7965    operator:
7966      <? >? <?= >?=
7967
7968    Returns an IDENTIFIER_NODE for the operator which is a
7969    human-readable spelling of the identifier, e.g., `operator +'.  */
7970
7971 static tree
7972 cp_parser_operator (cp_parser* parser)
7973 {
7974   tree id = NULL_TREE;
7975   cp_token *token;
7976
7977   /* Peek at the next token.  */
7978   token = cp_lexer_peek_token (parser->lexer);
7979   /* Figure out which operator we have.  */
7980   switch (token->type)
7981     {
7982     case CPP_KEYWORD:
7983       {
7984         enum tree_code op;
7985
7986         /* The keyword should be either `new' or `delete'.  */
7987         if (token->keyword == RID_NEW)
7988           op = NEW_EXPR;
7989         else if (token->keyword == RID_DELETE)
7990           op = DELETE_EXPR;
7991         else
7992           break;
7993
7994         /* Consume the `new' or `delete' token.  */
7995         cp_lexer_consume_token (parser->lexer);
7996
7997         /* Peek at the next token.  */
7998         token = cp_lexer_peek_token (parser->lexer);
7999         /* If it's a `[' token then this is the array variant of the
8000            operator.  */
8001         if (token->type == CPP_OPEN_SQUARE)
8002           {
8003             /* Consume the `[' token.  */
8004             cp_lexer_consume_token (parser->lexer);
8005             /* Look for the `]' token.  */
8006             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8007             id = ansi_opname (op == NEW_EXPR
8008                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8009           }
8010         /* Otherwise, we have the non-array variant.  */
8011         else
8012           id = ansi_opname (op);
8013
8014         return id;
8015       }
8016
8017     case CPP_PLUS:
8018       id = ansi_opname (PLUS_EXPR);
8019       break;
8020
8021     case CPP_MINUS:
8022       id = ansi_opname (MINUS_EXPR);
8023       break;
8024
8025     case CPP_MULT:
8026       id = ansi_opname (MULT_EXPR);
8027       break;
8028
8029     case CPP_DIV:
8030       id = ansi_opname (TRUNC_DIV_EXPR);
8031       break;
8032
8033     case CPP_MOD:
8034       id = ansi_opname (TRUNC_MOD_EXPR);
8035       break;
8036
8037     case CPP_XOR:
8038       id = ansi_opname (BIT_XOR_EXPR);
8039       break;
8040
8041     case CPP_AND:
8042       id = ansi_opname (BIT_AND_EXPR);
8043       break;
8044
8045     case CPP_OR:
8046       id = ansi_opname (BIT_IOR_EXPR);
8047       break;
8048
8049     case CPP_COMPL:
8050       id = ansi_opname (BIT_NOT_EXPR);
8051       break;
8052
8053     case CPP_NOT:
8054       id = ansi_opname (TRUTH_NOT_EXPR);
8055       break;
8056
8057     case CPP_EQ:
8058       id = ansi_assopname (NOP_EXPR);
8059       break;
8060
8061     case CPP_LESS:
8062       id = ansi_opname (LT_EXPR);
8063       break;
8064
8065     case CPP_GREATER:
8066       id = ansi_opname (GT_EXPR);
8067       break;
8068
8069     case CPP_PLUS_EQ:
8070       id = ansi_assopname (PLUS_EXPR);
8071       break;
8072
8073     case CPP_MINUS_EQ:
8074       id = ansi_assopname (MINUS_EXPR);
8075       break;
8076
8077     case CPP_MULT_EQ:
8078       id = ansi_assopname (MULT_EXPR);
8079       break;
8080
8081     case CPP_DIV_EQ:
8082       id = ansi_assopname (TRUNC_DIV_EXPR);
8083       break;
8084
8085     case CPP_MOD_EQ:
8086       id = ansi_assopname (TRUNC_MOD_EXPR);
8087       break;
8088
8089     case CPP_XOR_EQ:
8090       id = ansi_assopname (BIT_XOR_EXPR);
8091       break;
8092
8093     case CPP_AND_EQ:
8094       id = ansi_assopname (BIT_AND_EXPR);
8095       break;
8096
8097     case CPP_OR_EQ:
8098       id = ansi_assopname (BIT_IOR_EXPR);
8099       break;
8100
8101     case CPP_LSHIFT:
8102       id = ansi_opname (LSHIFT_EXPR);
8103       break;
8104
8105     case CPP_RSHIFT:
8106       id = ansi_opname (RSHIFT_EXPR);
8107       break;
8108
8109     case CPP_LSHIFT_EQ:
8110       id = ansi_assopname (LSHIFT_EXPR);
8111       break;
8112
8113     case CPP_RSHIFT_EQ:
8114       id = ansi_assopname (RSHIFT_EXPR);
8115       break;
8116
8117     case CPP_EQ_EQ:
8118       id = ansi_opname (EQ_EXPR);
8119       break;
8120
8121     case CPP_NOT_EQ:
8122       id = ansi_opname (NE_EXPR);
8123       break;
8124
8125     case CPP_LESS_EQ:
8126       id = ansi_opname (LE_EXPR);
8127       break;
8128
8129     case CPP_GREATER_EQ:
8130       id = ansi_opname (GE_EXPR);
8131       break;
8132
8133     case CPP_AND_AND:
8134       id = ansi_opname (TRUTH_ANDIF_EXPR);
8135       break;
8136
8137     case CPP_OR_OR:
8138       id = ansi_opname (TRUTH_ORIF_EXPR);
8139       break;
8140
8141     case CPP_PLUS_PLUS:
8142       id = ansi_opname (POSTINCREMENT_EXPR);
8143       break;
8144
8145     case CPP_MINUS_MINUS:
8146       id = ansi_opname (PREDECREMENT_EXPR);
8147       break;
8148
8149     case CPP_COMMA:
8150       id = ansi_opname (COMPOUND_EXPR);
8151       break;
8152
8153     case CPP_DEREF_STAR:
8154       id = ansi_opname (MEMBER_REF);
8155       break;
8156
8157     case CPP_DEREF:
8158       id = ansi_opname (COMPONENT_REF);
8159       break;
8160
8161     case CPP_OPEN_PAREN:
8162       /* Consume the `('.  */
8163       cp_lexer_consume_token (parser->lexer);
8164       /* Look for the matching `)'.  */
8165       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8166       return ansi_opname (CALL_EXPR);
8167
8168     case CPP_OPEN_SQUARE:
8169       /* Consume the `['.  */
8170       cp_lexer_consume_token (parser->lexer);
8171       /* Look for the matching `]'.  */
8172       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8173       return ansi_opname (ARRAY_REF);
8174
8175       /* Extensions.  */
8176     case CPP_MIN:
8177       id = ansi_opname (MIN_EXPR);
8178       cp_parser_warn_min_max ();
8179       break;
8180
8181     case CPP_MAX:
8182       id = ansi_opname (MAX_EXPR);
8183       cp_parser_warn_min_max ();
8184       break;
8185
8186     case CPP_MIN_EQ:
8187       id = ansi_assopname (MIN_EXPR);
8188       cp_parser_warn_min_max ();
8189       break;
8190
8191     case CPP_MAX_EQ:
8192       id = ansi_assopname (MAX_EXPR);
8193       cp_parser_warn_min_max ();
8194       break;
8195
8196     default:
8197       /* Anything else is an error.  */
8198       break;
8199     }
8200
8201   /* If we have selected an identifier, we need to consume the
8202      operator token.  */
8203   if (id)
8204     cp_lexer_consume_token (parser->lexer);
8205   /* Otherwise, no valid operator name was present.  */
8206   else
8207     {
8208       cp_parser_error (parser, "expected operator");
8209       id = error_mark_node;
8210     }
8211
8212   return id;
8213 }
8214
8215 /* Parse a template-declaration.
8216
8217    template-declaration:
8218      export [opt] template < template-parameter-list > declaration
8219
8220    If MEMBER_P is TRUE, this template-declaration occurs within a
8221    class-specifier.
8222
8223    The grammar rule given by the standard isn't correct.  What
8224    is really meant is:
8225
8226    template-declaration:
8227      export [opt] template-parameter-list-seq
8228        decl-specifier-seq [opt] init-declarator [opt] ;
8229      export [opt] template-parameter-list-seq
8230        function-definition
8231
8232    template-parameter-list-seq:
8233      template-parameter-list-seq [opt]
8234      template < template-parameter-list >  */
8235
8236 static void
8237 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8238 {
8239   /* Check for `export'.  */
8240   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8241     {
8242       /* Consume the `export' token.  */
8243       cp_lexer_consume_token (parser->lexer);
8244       /* Warn that we do not support `export'.  */
8245       warning (0, "keyword %<export%> not implemented, and will be ignored");
8246     }
8247
8248   cp_parser_template_declaration_after_export (parser, member_p);
8249 }
8250
8251 /* Parse a template-parameter-list.
8252
8253    template-parameter-list:
8254      template-parameter
8255      template-parameter-list , template-parameter
8256
8257    Returns a TREE_LIST.  Each node represents a template parameter.
8258    The nodes are connected via their TREE_CHAINs.  */
8259
8260 static tree
8261 cp_parser_template_parameter_list (cp_parser* parser)
8262 {
8263   tree parameter_list = NULL_TREE;
8264
8265   while (true)
8266     {
8267       tree parameter;
8268       cp_token *token;
8269       bool is_non_type;
8270
8271       /* Parse the template-parameter.  */
8272       parameter = cp_parser_template_parameter (parser, &is_non_type);
8273       /* Add it to the list.  */
8274       if (parameter != error_mark_node)
8275         parameter_list = process_template_parm (parameter_list,
8276                                                 parameter,
8277                                                 is_non_type);
8278       /* Peek at the next token.  */
8279       token = cp_lexer_peek_token (parser->lexer);
8280       /* If it's not a `,', we're done.  */
8281       if (token->type != CPP_COMMA)
8282         break;
8283       /* Otherwise, consume the `,' token.  */
8284       cp_lexer_consume_token (parser->lexer);
8285     }
8286
8287   return parameter_list;
8288 }
8289
8290 /* Parse a template-parameter.
8291
8292    template-parameter:
8293      type-parameter
8294      parameter-declaration
8295
8296    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8297    the parameter.  The TREE_PURPOSE is the default value, if any.
8298    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8299    iff this parameter is a non-type parameter.  */
8300
8301 static tree
8302 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8303 {
8304   cp_token *token;
8305   cp_parameter_declarator *parameter_declarator;
8306   tree parm;
8307
8308   /* Assume it is a type parameter or a template parameter.  */
8309   *is_non_type = false;
8310   /* Peek at the next token.  */
8311   token = cp_lexer_peek_token (parser->lexer);
8312   /* If it is `class' or `template', we have a type-parameter.  */
8313   if (token->keyword == RID_TEMPLATE)
8314     return cp_parser_type_parameter (parser);
8315   /* If it is `class' or `typename' we do not know yet whether it is a
8316      type parameter or a non-type parameter.  Consider:
8317
8318        template <typename T, typename T::X X> ...
8319
8320      or:
8321
8322        template <class C, class D*> ...
8323
8324      Here, the first parameter is a type parameter, and the second is
8325      a non-type parameter.  We can tell by looking at the token after
8326      the identifier -- if it is a `,', `=', or `>' then we have a type
8327      parameter.  */
8328   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8329     {
8330       /* Peek at the token after `class' or `typename'.  */
8331       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8332       /* If it's an identifier, skip it.  */
8333       if (token->type == CPP_NAME)
8334         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8335       /* Now, see if the token looks like the end of a template
8336          parameter.  */
8337       if (token->type == CPP_COMMA
8338           || token->type == CPP_EQ
8339           || token->type == CPP_GREATER)
8340         return cp_parser_type_parameter (parser);
8341     }
8342
8343   /* Otherwise, it is a non-type parameter.
8344
8345      [temp.param]
8346
8347      When parsing a default template-argument for a non-type
8348      template-parameter, the first non-nested `>' is taken as the end
8349      of the template parameter-list rather than a greater-than
8350      operator.  */
8351   *is_non_type = true;
8352   parameter_declarator
8353      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8354                                         /*parenthesized_p=*/NULL);
8355   parm = grokdeclarator (parameter_declarator->declarator,
8356                          &parameter_declarator->decl_specifiers,
8357                          PARM, /*initialized=*/0,
8358                          /*attrlist=*/NULL);
8359   if (parm == error_mark_node)
8360     return error_mark_node;
8361   return build_tree_list (parameter_declarator->default_argument, parm);
8362 }
8363
8364 /* Parse a type-parameter.
8365
8366    type-parameter:
8367      class identifier [opt]
8368      class identifier [opt] = type-id
8369      typename identifier [opt]
8370      typename identifier [opt] = type-id
8371      template < template-parameter-list > class identifier [opt]
8372      template < template-parameter-list > class identifier [opt]
8373        = id-expression
8374
8375    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8376    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8377    the declaration of the parameter.  */
8378
8379 static tree
8380 cp_parser_type_parameter (cp_parser* parser)
8381 {
8382   cp_token *token;
8383   tree parameter;
8384
8385   /* Look for a keyword to tell us what kind of parameter this is.  */
8386   token = cp_parser_require (parser, CPP_KEYWORD,
8387                              "`class', `typename', or `template'");
8388   if (!token)
8389     return error_mark_node;
8390
8391   switch (token->keyword)
8392     {
8393     case RID_CLASS:
8394     case RID_TYPENAME:
8395       {
8396         tree identifier;
8397         tree default_argument;
8398
8399         /* If the next token is an identifier, then it names the
8400            parameter.  */
8401         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8402           identifier = cp_parser_identifier (parser);
8403         else
8404           identifier = NULL_TREE;
8405
8406         /* Create the parameter.  */
8407         parameter = finish_template_type_parm (class_type_node, identifier);
8408
8409         /* If the next token is an `=', we have a default argument.  */
8410         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8411           {
8412             /* Consume the `=' token.  */
8413             cp_lexer_consume_token (parser->lexer);
8414             /* Parse the default-argument.  */
8415             default_argument = cp_parser_type_id (parser);
8416           }
8417         else
8418           default_argument = NULL_TREE;
8419
8420         /* Create the combined representation of the parameter and the
8421            default argument.  */
8422         parameter = build_tree_list (default_argument, parameter);
8423       }
8424       break;
8425
8426     case RID_TEMPLATE:
8427       {
8428         tree parameter_list;
8429         tree identifier;
8430         tree default_argument;
8431
8432         /* Look for the `<'.  */
8433         cp_parser_require (parser, CPP_LESS, "`<'");
8434         /* Parse the template-parameter-list.  */
8435         begin_template_parm_list ();
8436         parameter_list
8437           = cp_parser_template_parameter_list (parser);
8438         parameter_list = end_template_parm_list (parameter_list);
8439         /* Look for the `>'.  */
8440         cp_parser_require (parser, CPP_GREATER, "`>'");
8441         /* Look for the `class' keyword.  */
8442         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8443         /* If the next token is an `=', then there is a
8444            default-argument.  If the next token is a `>', we are at
8445            the end of the parameter-list.  If the next token is a `,',
8446            then we are at the end of this parameter.  */
8447         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8448             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8449             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8450           {
8451             identifier = cp_parser_identifier (parser);
8452             /* Treat invalid names as if the parameter were nameless.  */
8453             if (identifier == error_mark_node)
8454               identifier = NULL_TREE;
8455           }
8456         else
8457           identifier = NULL_TREE;
8458
8459         /* Create the template parameter.  */
8460         parameter = finish_template_template_parm (class_type_node,
8461                                                    identifier);
8462
8463         /* If the next token is an `=', then there is a
8464            default-argument.  */
8465         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8466           {
8467             bool is_template;
8468
8469             /* Consume the `='.  */
8470             cp_lexer_consume_token (parser->lexer);
8471             /* Parse the id-expression.  */
8472             default_argument
8473               = cp_parser_id_expression (parser,
8474                                          /*template_keyword_p=*/false,
8475                                          /*check_dependency_p=*/true,
8476                                          /*template_p=*/&is_template,
8477                                          /*declarator_p=*/false);
8478             if (TREE_CODE (default_argument) == TYPE_DECL)
8479               /* If the id-expression was a template-id that refers to
8480                  a template-class, we already have the declaration here,
8481                  so no further lookup is needed.  */
8482                  ;
8483             else
8484               /* Look up the name.  */
8485               default_argument
8486                 = cp_parser_lookup_name (parser, default_argument,
8487                                          none_type,
8488                                          /*is_template=*/is_template,
8489                                          /*is_namespace=*/false,
8490                                          /*check_dependency=*/true,
8491                                          /*ambiguous_p=*/NULL);
8492             /* See if the default argument is valid.  */
8493             default_argument
8494               = check_template_template_default_arg (default_argument);
8495           }
8496         else
8497           default_argument = NULL_TREE;
8498
8499         /* Create the combined representation of the parameter and the
8500            default argument.  */
8501         parameter = build_tree_list (default_argument, parameter);
8502       }
8503       break;
8504
8505     default:
8506       gcc_unreachable ();
8507       break;
8508     }
8509
8510   return parameter;
8511 }
8512
8513 /* Parse a template-id.
8514
8515    template-id:
8516      template-name < template-argument-list [opt] >
8517
8518    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8519    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8520    returned.  Otherwise, if the template-name names a function, or set
8521    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8522    names a class, returns a TYPE_DECL for the specialization.
8523
8524    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8525    uninstantiated templates.  */
8526
8527 static tree
8528 cp_parser_template_id (cp_parser *parser,
8529                        bool template_keyword_p,
8530                        bool check_dependency_p,
8531                        bool is_declaration)
8532 {
8533   tree template;
8534   tree arguments;
8535   tree template_id;
8536   cp_token_position start_of_id = 0;
8537   tree access_check = NULL_TREE;
8538   cp_token *next_token, *next_token_2;
8539   bool is_identifier;
8540
8541   /* If the next token corresponds to a template-id, there is no need
8542      to reparse it.  */
8543   next_token = cp_lexer_peek_token (parser->lexer);
8544   if (next_token->type == CPP_TEMPLATE_ID)
8545     {
8546       tree value;
8547       tree check;
8548
8549       /* Get the stored value.  */
8550       value = cp_lexer_consume_token (parser->lexer)->value;
8551       /* Perform any access checks that were deferred.  */
8552       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8553         perform_or_defer_access_check (TREE_PURPOSE (check),
8554                                        TREE_VALUE (check));
8555       /* Return the stored value.  */
8556       return TREE_VALUE (value);
8557     }
8558
8559   /* Avoid performing name lookup if there is no possibility of
8560      finding a template-id.  */
8561   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8562       || (next_token->type == CPP_NAME
8563           && !cp_parser_nth_token_starts_template_argument_list_p
8564                (parser, 2)))
8565     {
8566       cp_parser_error (parser, "expected template-id");
8567       return error_mark_node;
8568     }
8569
8570   /* Remember where the template-id starts.  */
8571   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8572     start_of_id = cp_lexer_token_position (parser->lexer, false);
8573
8574   push_deferring_access_checks (dk_deferred);
8575
8576   /* Parse the template-name.  */
8577   is_identifier = false;
8578   template = cp_parser_template_name (parser, template_keyword_p,
8579                                       check_dependency_p,
8580                                       is_declaration,
8581                                       &is_identifier);
8582   if (template == error_mark_node || is_identifier)
8583     {
8584       pop_deferring_access_checks ();
8585       return template;
8586     }
8587
8588   /* If we find the sequence `[:' after a template-name, it's probably
8589      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8590      parse correctly the argument list.  */
8591   next_token = cp_lexer_peek_token (parser->lexer);
8592   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8593   if (next_token->type == CPP_OPEN_SQUARE
8594       && next_token->flags & DIGRAPH
8595       && next_token_2->type == CPP_COLON
8596       && !(next_token_2->flags & PREV_WHITE))
8597     {
8598       cp_parser_parse_tentatively (parser);
8599       /* Change `:' into `::'.  */
8600       next_token_2->type = CPP_SCOPE;
8601       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8602          CPP_LESS.  */
8603       cp_lexer_consume_token (parser->lexer);
8604       /* Parse the arguments.  */
8605       arguments = cp_parser_enclosed_template_argument_list (parser);
8606       if (!cp_parser_parse_definitely (parser))
8607         {
8608           /* If we couldn't parse an argument list, then we revert our changes
8609              and return simply an error. Maybe this is not a template-id
8610              after all.  */
8611           next_token_2->type = CPP_COLON;
8612           cp_parser_error (parser, "expected %<<%>");
8613           pop_deferring_access_checks ();
8614           return error_mark_node;
8615         }
8616       /* Otherwise, emit an error about the invalid digraph, but continue
8617          parsing because we got our argument list.  */
8618       pedwarn ("%<<::%> cannot begin a template-argument list");
8619       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8620               "between %<<%> and %<::%>");
8621       if (!flag_permissive)
8622         {
8623           static bool hint;
8624           if (!hint)
8625             {
8626               inform ("(if you use -fpermissive G++ will accept your code)");
8627               hint = true;
8628             }
8629         }
8630     }
8631   else
8632     {
8633       /* Look for the `<' that starts the template-argument-list.  */
8634       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8635         {
8636           pop_deferring_access_checks ();
8637           return error_mark_node;
8638         }
8639       /* Parse the arguments.  */
8640       arguments = cp_parser_enclosed_template_argument_list (parser);
8641     }
8642
8643   /* Build a representation of the specialization.  */
8644   if (TREE_CODE (template) == IDENTIFIER_NODE)
8645     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8646   else if (DECL_CLASS_TEMPLATE_P (template)
8647            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8648     template_id
8649       = finish_template_type (template, arguments,
8650                               cp_lexer_next_token_is (parser->lexer,
8651                                                       CPP_SCOPE));
8652   else
8653     {
8654       /* If it's not a class-template or a template-template, it should be
8655          a function-template.  */
8656       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8657                    || TREE_CODE (template) == OVERLOAD
8658                    || BASELINK_P (template)));
8659
8660       template_id = lookup_template_function (template, arguments);
8661     }
8662
8663   /* Retrieve any deferred checks.  Do not pop this access checks yet
8664      so the memory will not be reclaimed during token replacing below.  */
8665   access_check = get_deferred_access_checks ();
8666
8667   /* If parsing tentatively, replace the sequence of tokens that makes
8668      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8669      should we re-parse the token stream, we will not have to repeat
8670      the effort required to do the parse, nor will we issue duplicate
8671      error messages about problems during instantiation of the
8672      template.  */
8673   if (start_of_id)
8674     {
8675       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8676
8677       /* Reset the contents of the START_OF_ID token.  */
8678       token->type = CPP_TEMPLATE_ID;
8679       token->value = build_tree_list (access_check, template_id);
8680       token->keyword = RID_MAX;
8681
8682       /* Purge all subsequent tokens.  */
8683       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8684
8685       /* ??? Can we actually assume that, if template_id ==
8686          error_mark_node, we will have issued a diagnostic to the
8687          user, as opposed to simply marking the tentative parse as
8688          failed?  */
8689       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8690         error ("parse error in template argument list");
8691     }
8692
8693   pop_deferring_access_checks ();
8694   return template_id;
8695 }
8696
8697 /* Parse a template-name.
8698
8699    template-name:
8700      identifier
8701
8702    The standard should actually say:
8703
8704    template-name:
8705      identifier
8706      operator-function-id
8707
8708    A defect report has been filed about this issue.
8709
8710    A conversion-function-id cannot be a template name because they cannot
8711    be part of a template-id. In fact, looking at this code:
8712
8713    a.operator K<int>()
8714
8715    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8716    It is impossible to call a templated conversion-function-id with an
8717    explicit argument list, since the only allowed template parameter is
8718    the type to which it is converting.
8719
8720    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8721    `template' keyword, in a construction like:
8722
8723      T::template f<3>()
8724
8725    In that case `f' is taken to be a template-name, even though there
8726    is no way of knowing for sure.
8727
8728    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8729    name refers to a set of overloaded functions, at least one of which
8730    is a template, or an IDENTIFIER_NODE with the name of the template,
8731    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8732    names are looked up inside uninstantiated templates.  */
8733
8734 static tree
8735 cp_parser_template_name (cp_parser* parser,
8736                          bool template_keyword_p,
8737                          bool check_dependency_p,
8738                          bool is_declaration,
8739                          bool *is_identifier)
8740 {
8741   tree identifier;
8742   tree decl;
8743   tree fns;
8744
8745   /* If the next token is `operator', then we have either an
8746      operator-function-id or a conversion-function-id.  */
8747   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8748     {
8749       /* We don't know whether we're looking at an
8750          operator-function-id or a conversion-function-id.  */
8751       cp_parser_parse_tentatively (parser);
8752       /* Try an operator-function-id.  */
8753       identifier = cp_parser_operator_function_id (parser);
8754       /* If that didn't work, try a conversion-function-id.  */
8755       if (!cp_parser_parse_definitely (parser))
8756         {
8757           cp_parser_error (parser, "expected template-name");
8758           return error_mark_node;
8759         }
8760     }
8761   /* Look for the identifier.  */
8762   else
8763     identifier = cp_parser_identifier (parser);
8764
8765   /* If we didn't find an identifier, we don't have a template-id.  */
8766   if (identifier == error_mark_node)
8767     return error_mark_node;
8768
8769   /* If the name immediately followed the `template' keyword, then it
8770      is a template-name.  However, if the next token is not `<', then
8771      we do not treat it as a template-name, since it is not being used
8772      as part of a template-id.  This enables us to handle constructs
8773      like:
8774
8775        template <typename T> struct S { S(); };
8776        template <typename T> S<T>::S();
8777
8778      correctly.  We would treat `S' as a template -- if it were `S<T>'
8779      -- but we do not if there is no `<'.  */
8780
8781   if (processing_template_decl
8782       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8783     {
8784       /* In a declaration, in a dependent context, we pretend that the
8785          "template" keyword was present in order to improve error
8786          recovery.  For example, given:
8787
8788            template <typename T> void f(T::X<int>);
8789
8790          we want to treat "X<int>" as a template-id.  */
8791       if (is_declaration
8792           && !template_keyword_p
8793           && parser->scope && TYPE_P (parser->scope)
8794           && check_dependency_p
8795           && dependent_type_p (parser->scope)
8796           /* Do not do this for dtors (or ctors), since they never
8797              need the template keyword before their name.  */
8798           && !constructor_name_p (identifier, parser->scope))
8799         {
8800           cp_token_position start = 0;
8801
8802           /* Explain what went wrong.  */
8803           error ("non-template %qD used as template", identifier);
8804           inform ("use %<%T::template %D%> to indicate that it is a template",
8805                   parser->scope, identifier);
8806           /* If parsing tentatively, find the location of the "<" token.  */
8807           if (cp_parser_simulate_error (parser))
8808             start = cp_lexer_token_position (parser->lexer, true);
8809           /* Parse the template arguments so that we can issue error
8810              messages about them.  */
8811           cp_lexer_consume_token (parser->lexer);
8812           cp_parser_enclosed_template_argument_list (parser);
8813           /* Skip tokens until we find a good place from which to
8814              continue parsing.  */
8815           cp_parser_skip_to_closing_parenthesis (parser,
8816                                                  /*recovering=*/true,
8817                                                  /*or_comma=*/true,
8818                                                  /*consume_paren=*/false);
8819           /* If parsing tentatively, permanently remove the
8820              template argument list.  That will prevent duplicate
8821              error messages from being issued about the missing
8822              "template" keyword.  */
8823           if (start)
8824             cp_lexer_purge_tokens_after (parser->lexer, start);
8825           if (is_identifier)
8826             *is_identifier = true;
8827           return identifier;
8828         }
8829
8830       /* If the "template" keyword is present, then there is generally
8831          no point in doing name-lookup, so we just return IDENTIFIER.
8832          But, if the qualifying scope is non-dependent then we can
8833          (and must) do name-lookup normally.  */
8834       if (template_keyword_p
8835           && (!parser->scope
8836               || (TYPE_P (parser->scope)
8837                   && dependent_type_p (parser->scope))))
8838         return identifier;
8839     }
8840
8841   /* Look up the name.  */
8842   decl = cp_parser_lookup_name (parser, identifier,
8843                                 none_type,
8844                                 /*is_template=*/false,
8845                                 /*is_namespace=*/false,
8846                                 check_dependency_p,
8847                                 /*ambiguous_p=*/NULL);
8848   decl = maybe_get_template_decl_from_type_decl (decl);
8849
8850   /* If DECL is a template, then the name was a template-name.  */
8851   if (TREE_CODE (decl) == TEMPLATE_DECL)
8852     ;
8853   else
8854     {
8855       tree fn = NULL_TREE;
8856
8857       /* The standard does not explicitly indicate whether a name that
8858          names a set of overloaded declarations, some of which are
8859          templates, is a template-name.  However, such a name should
8860          be a template-name; otherwise, there is no way to form a
8861          template-id for the overloaded templates.  */
8862       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8863       if (TREE_CODE (fns) == OVERLOAD)
8864         for (fn = fns; fn; fn = OVL_NEXT (fn))
8865           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8866             break;
8867
8868       if (!fn)
8869         {
8870           /* The name does not name a template.  */
8871           cp_parser_error (parser, "expected template-name");
8872           return error_mark_node;
8873         }
8874     }
8875
8876   /* If DECL is dependent, and refers to a function, then just return
8877      its name; we will look it up again during template instantiation.  */
8878   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8879     {
8880       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8881       if (TYPE_P (scope) && dependent_type_p (scope))
8882         return identifier;
8883     }
8884
8885   return decl;
8886 }
8887
8888 /* Parse a template-argument-list.
8889
8890    template-argument-list:
8891      template-argument
8892      template-argument-list , template-argument
8893
8894    Returns a TREE_VEC containing the arguments.  */
8895
8896 static tree
8897 cp_parser_template_argument_list (cp_parser* parser)
8898 {
8899   tree fixed_args[10];
8900   unsigned n_args = 0;
8901   unsigned alloced = 10;
8902   tree *arg_ary = fixed_args;
8903   tree vec;
8904   bool saved_in_template_argument_list_p;
8905
8906   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8907   parser->in_template_argument_list_p = true;
8908   do
8909     {
8910       tree argument;
8911
8912       if (n_args)
8913         /* Consume the comma.  */
8914         cp_lexer_consume_token (parser->lexer);
8915
8916       /* Parse the template-argument.  */
8917       argument = cp_parser_template_argument (parser);
8918       if (n_args == alloced)
8919         {
8920           alloced *= 2;
8921
8922           if (arg_ary == fixed_args)
8923             {
8924               arg_ary = xmalloc (sizeof (tree) * alloced);
8925               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8926             }
8927           else
8928             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8929         }
8930       arg_ary[n_args++] = argument;
8931     }
8932   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8933
8934   vec = make_tree_vec (n_args);
8935
8936   while (n_args--)
8937     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8938
8939   if (arg_ary != fixed_args)
8940     free (arg_ary);
8941   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8942   return vec;
8943 }
8944
8945 /* Parse a template-argument.
8946
8947    template-argument:
8948      assignment-expression
8949      type-id
8950      id-expression
8951
8952    The representation is that of an assignment-expression, type-id, or
8953    id-expression -- except that the qualified id-expression is
8954    evaluated, so that the value returned is either a DECL or an
8955    OVERLOAD.
8956
8957    Although the standard says "assignment-expression", it forbids
8958    throw-expressions or assignments in the template argument.
8959    Therefore, we use "conditional-expression" instead.  */
8960
8961 static tree
8962 cp_parser_template_argument (cp_parser* parser)
8963 {
8964   tree argument;
8965   bool template_p;
8966   bool address_p;
8967   bool maybe_type_id = false;
8968   cp_token *token;
8969   cp_id_kind idk;
8970   tree qualifying_class;
8971
8972   /* There's really no way to know what we're looking at, so we just
8973      try each alternative in order.
8974
8975        [temp.arg]
8976
8977        In a template-argument, an ambiguity between a type-id and an
8978        expression is resolved to a type-id, regardless of the form of
8979        the corresponding template-parameter.
8980
8981      Therefore, we try a type-id first.  */
8982   cp_parser_parse_tentatively (parser);
8983   argument = cp_parser_type_id (parser);
8984   /* If there was no error parsing the type-id but the next token is a '>>',
8985      we probably found a typo for '> >'. But there are type-id which are
8986      also valid expressions. For instance:
8987
8988      struct X { int operator >> (int); };
8989      template <int V> struct Foo {};
8990      Foo<X () >> 5> r;
8991
8992      Here 'X()' is a valid type-id of a function type, but the user just
8993      wanted to write the expression "X() >> 5". Thus, we remember that we
8994      found a valid type-id, but we still try to parse the argument as an
8995      expression to see what happens.  */
8996   if (!cp_parser_error_occurred (parser)
8997       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8998     {
8999       maybe_type_id = true;
9000       cp_parser_abort_tentative_parse (parser);
9001     }
9002   else
9003     {
9004       /* If the next token isn't a `,' or a `>', then this argument wasn't
9005       really finished. This means that the argument is not a valid
9006       type-id.  */
9007       if (!cp_parser_next_token_ends_template_argument_p (parser))
9008         cp_parser_error (parser, "expected template-argument");
9009       /* If that worked, we're done.  */
9010       if (cp_parser_parse_definitely (parser))
9011         return argument;
9012     }
9013   /* We're still not sure what the argument will be.  */
9014   cp_parser_parse_tentatively (parser);
9015   /* Try a template.  */
9016   argument = cp_parser_id_expression (parser,
9017                                       /*template_keyword_p=*/false,
9018                                       /*check_dependency_p=*/true,
9019                                       &template_p,
9020                                       /*declarator_p=*/false);
9021   /* If the next token isn't a `,' or a `>', then this argument wasn't
9022      really finished.  */
9023   if (!cp_parser_next_token_ends_template_argument_p (parser))
9024     cp_parser_error (parser, "expected template-argument");
9025   if (!cp_parser_error_occurred (parser))
9026     {
9027       /* Figure out what is being referred to.  If the id-expression
9028          was for a class template specialization, then we will have a
9029          TYPE_DECL at this point.  There is no need to do name lookup
9030          at this point in that case.  */
9031       if (TREE_CODE (argument) != TYPE_DECL)
9032         argument = cp_parser_lookup_name (parser, argument,
9033                                           none_type,
9034                                           /*is_template=*/template_p,
9035                                           /*is_namespace=*/false,
9036                                           /*check_dependency=*/true,
9037                                           /*ambiguous_p=*/NULL);
9038       if (TREE_CODE (argument) != TEMPLATE_DECL
9039           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9040         cp_parser_error (parser, "expected template-name");
9041     }
9042   if (cp_parser_parse_definitely (parser))
9043     return argument;
9044   /* It must be a non-type argument.  There permitted cases are given
9045      in [temp.arg.nontype]:
9046
9047      -- an integral constant-expression of integral or enumeration
9048         type; or
9049
9050      -- the name of a non-type template-parameter; or
9051
9052      -- the name of an object or function with external linkage...
9053
9054      -- the address of an object or function with external linkage...
9055
9056      -- a pointer to member...  */
9057   /* Look for a non-type template parameter.  */
9058   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9059     {
9060       cp_parser_parse_tentatively (parser);
9061       argument = cp_parser_primary_expression (parser,
9062                                                /*cast_p=*/false,
9063                                                &idk,
9064                                                &qualifying_class);
9065       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9066           || !cp_parser_next_token_ends_template_argument_p (parser))
9067         cp_parser_simulate_error (parser);
9068       if (cp_parser_parse_definitely (parser))
9069         return argument;
9070     }
9071
9072   /* If the next token is "&", the argument must be the address of an
9073      object or function with external linkage.  */
9074   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9075   if (address_p)
9076     cp_lexer_consume_token (parser->lexer);
9077   /* See if we might have an id-expression.  */
9078   token = cp_lexer_peek_token (parser->lexer);
9079   if (token->type == CPP_NAME
9080       || token->keyword == RID_OPERATOR
9081       || token->type == CPP_SCOPE
9082       || token->type == CPP_TEMPLATE_ID
9083       || token->type == CPP_NESTED_NAME_SPECIFIER)
9084     {
9085       cp_parser_parse_tentatively (parser);
9086       argument = cp_parser_primary_expression (parser,
9087                                                /*cast_p=*/false,
9088                                                &idk,
9089                                                &qualifying_class);
9090       if (cp_parser_error_occurred (parser)
9091           || !cp_parser_next_token_ends_template_argument_p (parser))
9092         cp_parser_abort_tentative_parse (parser);
9093       else
9094         {
9095           if (TREE_CODE (argument) == INDIRECT_REF)
9096             {
9097               gcc_assert (REFERENCE_REF_P (argument));
9098               argument = TREE_OPERAND (argument, 0);
9099             }
9100
9101           if (qualifying_class)
9102             argument = finish_qualified_id_expr (qualifying_class,
9103                                                  argument,
9104                                                  /*done=*/true,
9105                                                  address_p);
9106           if (TREE_CODE (argument) == VAR_DECL)
9107             {
9108               /* A variable without external linkage might still be a
9109                  valid constant-expression, so no error is issued here
9110                  if the external-linkage check fails.  */
9111               if (!DECL_EXTERNAL_LINKAGE_P (argument))
9112                 cp_parser_simulate_error (parser);
9113             }
9114           else if (is_overloaded_fn (argument))
9115             /* All overloaded functions are allowed; if the external
9116                linkage test does not pass, an error will be issued
9117                later.  */
9118             ;
9119           else if (address_p
9120                    && (TREE_CODE (argument) == OFFSET_REF
9121                        || TREE_CODE (argument) == SCOPE_REF))
9122             /* A pointer-to-member.  */
9123             ;
9124           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9125             ;
9126           else
9127             cp_parser_simulate_error (parser);
9128
9129           if (cp_parser_parse_definitely (parser))
9130             {
9131               if (address_p)
9132                 argument = build_x_unary_op (ADDR_EXPR, argument);
9133               return argument;
9134             }
9135         }
9136     }
9137   /* If the argument started with "&", there are no other valid
9138      alternatives at this point.  */
9139   if (address_p)
9140     {
9141       cp_parser_error (parser, "invalid non-type template argument");
9142       return error_mark_node;
9143     }
9144
9145   /* If the argument wasn't successfully parsed as a type-id followed
9146      by '>>', the argument can only be a constant expression now.
9147      Otherwise, we try parsing the constant-expression tentatively,
9148      because the argument could really be a type-id.  */
9149   if (maybe_type_id)
9150     cp_parser_parse_tentatively (parser);
9151   argument = cp_parser_constant_expression (parser,
9152                                             /*allow_non_constant_p=*/false,
9153                                             /*non_constant_p=*/NULL);
9154   argument = fold_non_dependent_expr (argument);
9155   if (!maybe_type_id)
9156     return argument;
9157   if (!cp_parser_next_token_ends_template_argument_p (parser))
9158     cp_parser_error (parser, "expected template-argument");
9159   if (cp_parser_parse_definitely (parser))
9160     return argument;
9161   /* We did our best to parse the argument as a non type-id, but that
9162      was the only alternative that matched (albeit with a '>' after
9163      it). We can assume it's just a typo from the user, and a
9164      diagnostic will then be issued.  */
9165   return cp_parser_type_id (parser);
9166 }
9167
9168 /* Parse an explicit-instantiation.
9169
9170    explicit-instantiation:
9171      template declaration
9172
9173    Although the standard says `declaration', what it really means is:
9174
9175    explicit-instantiation:
9176      template decl-specifier-seq [opt] declarator [opt] ;
9177
9178    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9179    supposed to be allowed.  A defect report has been filed about this
9180    issue.
9181
9182    GNU Extension:
9183
9184    explicit-instantiation:
9185      storage-class-specifier template
9186        decl-specifier-seq [opt] declarator [opt] ;
9187      function-specifier template
9188        decl-specifier-seq [opt] declarator [opt] ;  */
9189
9190 static void
9191 cp_parser_explicit_instantiation (cp_parser* parser)
9192 {
9193   int declares_class_or_enum;
9194   cp_decl_specifier_seq decl_specifiers;
9195   tree extension_specifier = NULL_TREE;
9196
9197   /* Look for an (optional) storage-class-specifier or
9198      function-specifier.  */
9199   if (cp_parser_allow_gnu_extensions_p (parser))
9200     {
9201       extension_specifier
9202         = cp_parser_storage_class_specifier_opt (parser);
9203       if (!extension_specifier)
9204         extension_specifier
9205           = cp_parser_function_specifier_opt (parser,
9206                                               /*decl_specs=*/NULL);
9207     }
9208
9209   /* Look for the `template' keyword.  */
9210   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9211   /* Let the front end know that we are processing an explicit
9212      instantiation.  */
9213   begin_explicit_instantiation ();
9214   /* [temp.explicit] says that we are supposed to ignore access
9215      control while processing explicit instantiation directives.  */
9216   push_deferring_access_checks (dk_no_check);
9217   /* Parse a decl-specifier-seq.  */
9218   cp_parser_decl_specifier_seq (parser,
9219                                 CP_PARSER_FLAGS_OPTIONAL,
9220                                 &decl_specifiers,
9221                                 &declares_class_or_enum);
9222   /* If there was exactly one decl-specifier, and it declared a class,
9223      and there's no declarator, then we have an explicit type
9224      instantiation.  */
9225   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9226     {
9227       tree type;
9228
9229       type = check_tag_decl (&decl_specifiers);
9230       /* Turn access control back on for names used during
9231          template instantiation.  */
9232       pop_deferring_access_checks ();
9233       if (type)
9234         do_type_instantiation (type, extension_specifier, /*complain=*/1);
9235     }
9236   else
9237     {
9238       cp_declarator *declarator;
9239       tree decl;
9240
9241       /* Parse the declarator.  */
9242       declarator
9243         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9244                                 /*ctor_dtor_or_conv_p=*/NULL,
9245                                 /*parenthesized_p=*/NULL,
9246                                 /*member_p=*/false);
9247       if (declares_class_or_enum & 2)
9248         cp_parser_check_for_definition_in_return_type (declarator,
9249                                                        decl_specifiers.type);
9250       if (declarator != cp_error_declarator)
9251         {
9252           decl = grokdeclarator (declarator, &decl_specifiers,
9253                                  NORMAL, 0, NULL);
9254           /* Turn access control back on for names used during
9255              template instantiation.  */
9256           pop_deferring_access_checks ();
9257           /* Do the explicit instantiation.  */
9258           do_decl_instantiation (decl, extension_specifier);
9259         }
9260       else
9261         {
9262           pop_deferring_access_checks ();
9263           /* Skip the body of the explicit instantiation.  */
9264           cp_parser_skip_to_end_of_statement (parser);
9265         }
9266     }
9267   /* We're done with the instantiation.  */
9268   end_explicit_instantiation ();
9269
9270   cp_parser_consume_semicolon_at_end_of_statement (parser);
9271 }
9272
9273 /* Parse an explicit-specialization.
9274
9275    explicit-specialization:
9276      template < > declaration
9277
9278    Although the standard says `declaration', what it really means is:
9279
9280    explicit-specialization:
9281      template <> decl-specifier [opt] init-declarator [opt] ;
9282      template <> function-definition
9283      template <> explicit-specialization
9284      template <> template-declaration  */
9285
9286 static void
9287 cp_parser_explicit_specialization (cp_parser* parser)
9288 {
9289   /* Look for the `template' keyword.  */
9290   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9291   /* Look for the `<'.  */
9292   cp_parser_require (parser, CPP_LESS, "`<'");
9293   /* Look for the `>'.  */
9294   cp_parser_require (parser, CPP_GREATER, "`>'");
9295   /* We have processed another parameter list.  */
9296   ++parser->num_template_parameter_lists;
9297   /* Let the front end know that we are beginning a specialization.  */
9298   begin_specialization ();
9299
9300   /* If the next keyword is `template', we need to figure out whether
9301      or not we're looking a template-declaration.  */
9302   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9303     {
9304       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9305           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9306         cp_parser_template_declaration_after_export (parser,
9307                                                      /*member_p=*/false);
9308       else
9309         cp_parser_explicit_specialization (parser);
9310     }
9311   else
9312     /* Parse the dependent declaration.  */
9313     cp_parser_single_declaration (parser,
9314                                   /*member_p=*/false,
9315                                   /*friend_p=*/NULL);
9316
9317   /* We're done with the specialization.  */
9318   end_specialization ();
9319   /* We're done with this parameter list.  */
9320   --parser->num_template_parameter_lists;
9321 }
9322
9323 /* Parse a type-specifier.
9324
9325    type-specifier:
9326      simple-type-specifier
9327      class-specifier
9328      enum-specifier
9329      elaborated-type-specifier
9330      cv-qualifier
9331
9332    GNU Extension:
9333
9334    type-specifier:
9335      __complex__
9336
9337    Returns a representation of the type-specifier.  For a
9338    class-specifier, enum-specifier, or elaborated-type-specifier, a
9339    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9340
9341    The parser flags FLAGS is used to control type-specifier parsing.
9342
9343    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9344    in a decl-specifier-seq.
9345
9346    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9347    class-specifier, enum-specifier, or elaborated-type-specifier, then
9348    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9349    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9350    zero.
9351
9352    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9353    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9354    is set to FALSE.  */
9355
9356 static tree
9357 cp_parser_type_specifier (cp_parser* parser,
9358                           cp_parser_flags flags,
9359                           cp_decl_specifier_seq *decl_specs,
9360                           bool is_declaration,
9361                           int* declares_class_or_enum,
9362                           bool* is_cv_qualifier)
9363 {
9364   tree type_spec = NULL_TREE;
9365   cp_token *token;
9366   enum rid keyword;
9367   cp_decl_spec ds = ds_last;
9368
9369   /* Assume this type-specifier does not declare a new type.  */
9370   if (declares_class_or_enum)
9371     *declares_class_or_enum = 0;
9372   /* And that it does not specify a cv-qualifier.  */
9373   if (is_cv_qualifier)
9374     *is_cv_qualifier = false;
9375   /* Peek at the next token.  */
9376   token = cp_lexer_peek_token (parser->lexer);
9377
9378   /* If we're looking at a keyword, we can use that to guide the
9379      production we choose.  */
9380   keyword = token->keyword;
9381   switch (keyword)
9382     {
9383     case RID_ENUM:
9384       /* 'enum' [identifier] '{' introduces an enum-specifier;
9385          'enum' <anything else> introduces an elaborated-type-specifier.  */
9386       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9387           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9388               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9389                  == CPP_OPEN_BRACE))
9390         {
9391           if (parser->num_template_parameter_lists)
9392             {
9393               error ("template declaration of %qs", "enum");
9394               cp_parser_skip_to_end_of_block_or_statement (parser);
9395               type_spec = error_mark_node;
9396             }
9397           else
9398             type_spec = cp_parser_enum_specifier (parser);
9399
9400           if (declares_class_or_enum)
9401             *declares_class_or_enum = 2;
9402           if (decl_specs)
9403             cp_parser_set_decl_spec_type (decl_specs,
9404                                           type_spec,
9405                                           /*user_defined_p=*/true);
9406           return type_spec;
9407         }
9408       else
9409         goto elaborated_type_specifier;
9410
9411       /* Any of these indicate either a class-specifier, or an
9412          elaborated-type-specifier.  */
9413     case RID_CLASS:
9414     case RID_STRUCT:
9415     case RID_UNION:
9416       /* Parse tentatively so that we can back up if we don't find a
9417          class-specifier.  */
9418       cp_parser_parse_tentatively (parser);
9419       /* Look for the class-specifier.  */
9420       type_spec = cp_parser_class_specifier (parser);
9421       /* If that worked, we're done.  */
9422       if (cp_parser_parse_definitely (parser))
9423         {
9424           if (declares_class_or_enum)
9425             *declares_class_or_enum = 2;
9426           if (decl_specs)
9427             cp_parser_set_decl_spec_type (decl_specs,
9428                                           type_spec,
9429                                           /*user_defined_p=*/true);
9430           return type_spec;
9431         }
9432
9433       /* Fall through.  */
9434     elaborated_type_specifier:
9435       /* We're declaring (not defining) a class or enum.  */
9436       if (declares_class_or_enum)
9437         *declares_class_or_enum = 1;
9438
9439       /* Fall through.  */
9440     case RID_TYPENAME:
9441       /* Look for an elaborated-type-specifier.  */
9442       type_spec
9443         = (cp_parser_elaborated_type_specifier
9444            (parser,
9445             decl_specs && decl_specs->specs[(int) ds_friend],
9446             is_declaration));
9447       if (decl_specs)
9448         cp_parser_set_decl_spec_type (decl_specs,
9449                                       type_spec,
9450                                       /*user_defined_p=*/true);
9451       return type_spec;
9452
9453     case RID_CONST:
9454       ds = ds_const;
9455       if (is_cv_qualifier)
9456         *is_cv_qualifier = true;
9457       break;
9458
9459     case RID_VOLATILE:
9460       ds = ds_volatile;
9461       if (is_cv_qualifier)
9462         *is_cv_qualifier = true;
9463       break;
9464
9465     case RID_RESTRICT:
9466       ds = ds_restrict;
9467       if (is_cv_qualifier)
9468         *is_cv_qualifier = true;
9469       break;
9470
9471     case RID_COMPLEX:
9472       /* The `__complex__' keyword is a GNU extension.  */
9473       ds = ds_complex;
9474       break;
9475
9476     default:
9477       break;
9478     }
9479
9480   /* Handle simple keywords.  */
9481   if (ds != ds_last)
9482     {
9483       if (decl_specs)
9484         {
9485           ++decl_specs->specs[(int)ds];
9486           decl_specs->any_specifiers_p = true;
9487         }
9488       return cp_lexer_consume_token (parser->lexer)->value;
9489     }
9490
9491   /* If we do not already have a type-specifier, assume we are looking
9492      at a simple-type-specifier.  */
9493   type_spec = cp_parser_simple_type_specifier (parser,
9494                                                decl_specs,
9495                                                flags);
9496
9497   /* If we didn't find a type-specifier, and a type-specifier was not
9498      optional in this context, issue an error message.  */
9499   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9500     {
9501       cp_parser_error (parser, "expected type specifier");
9502       return error_mark_node;
9503     }
9504
9505   return type_spec;
9506 }
9507
9508 /* Parse a simple-type-specifier.
9509
9510    simple-type-specifier:
9511      :: [opt] nested-name-specifier [opt] type-name
9512      :: [opt] nested-name-specifier template template-id
9513      char
9514      wchar_t
9515      bool
9516      short
9517      int
9518      long
9519      signed
9520      unsigned
9521      float
9522      double
9523      void
9524
9525    GNU Extension:
9526
9527    simple-type-specifier:
9528      __typeof__ unary-expression
9529      __typeof__ ( type-id )
9530
9531    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9532    appropriately updated.  */
9533
9534 static tree
9535 cp_parser_simple_type_specifier (cp_parser* parser,
9536                                  cp_decl_specifier_seq *decl_specs,
9537                                  cp_parser_flags flags)
9538 {
9539   tree type = NULL_TREE;
9540   cp_token *token;
9541
9542   /* Peek at the next token.  */
9543   token = cp_lexer_peek_token (parser->lexer);
9544
9545   /* If we're looking at a keyword, things are easy.  */
9546   switch (token->keyword)
9547     {
9548     case RID_CHAR:
9549       if (decl_specs)
9550         decl_specs->explicit_char_p = true;
9551       type = char_type_node;
9552       break;
9553     case RID_WCHAR:
9554       type = wchar_type_node;
9555       break;
9556     case RID_BOOL:
9557       type = boolean_type_node;
9558       break;
9559     case RID_SHORT:
9560       if (decl_specs)
9561         ++decl_specs->specs[(int) ds_short];
9562       type = short_integer_type_node;
9563       break;
9564     case RID_INT:
9565       if (decl_specs)
9566         decl_specs->explicit_int_p = true;
9567       type = integer_type_node;
9568       break;
9569     case RID_LONG:
9570       if (decl_specs)
9571         ++decl_specs->specs[(int) ds_long];
9572       type = long_integer_type_node;
9573       break;
9574     case RID_SIGNED:
9575       if (decl_specs)
9576         ++decl_specs->specs[(int) ds_signed];
9577       type = integer_type_node;
9578       break;
9579     case RID_UNSIGNED:
9580       if (decl_specs)
9581         ++decl_specs->specs[(int) ds_unsigned];
9582       type = unsigned_type_node;
9583       break;
9584     case RID_FLOAT:
9585       type = float_type_node;
9586       break;
9587     case RID_DOUBLE:
9588       type = double_type_node;
9589       break;
9590     case RID_VOID:
9591       type = void_type_node;
9592       break;
9593
9594     case RID_TYPEOF:
9595       /* Consume the `typeof' token.  */
9596       cp_lexer_consume_token (parser->lexer);
9597       /* Parse the operand to `typeof'.  */
9598       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9599       /* If it is not already a TYPE, take its type.  */
9600       if (!TYPE_P (type))
9601         type = finish_typeof (type);
9602
9603       if (decl_specs)
9604         cp_parser_set_decl_spec_type (decl_specs, type,
9605                                       /*user_defined_p=*/true);
9606
9607       return type;
9608
9609     default:
9610       break;
9611     }
9612
9613   /* If the type-specifier was for a built-in type, we're done.  */
9614   if (type)
9615     {
9616       tree id;
9617
9618       /* Record the type.  */
9619       if (decl_specs
9620           && (token->keyword != RID_SIGNED
9621               && token->keyword != RID_UNSIGNED
9622               && token->keyword != RID_SHORT
9623               && token->keyword != RID_LONG))
9624         cp_parser_set_decl_spec_type (decl_specs,
9625                                       type,
9626                                       /*user_defined=*/false);
9627       if (decl_specs)
9628         decl_specs->any_specifiers_p = true;
9629
9630       /* Consume the token.  */
9631       id = cp_lexer_consume_token (parser->lexer)->value;
9632
9633       /* There is no valid C++ program where a non-template type is
9634          followed by a "<".  That usually indicates that the user thought
9635          that the type was a template.  */
9636       cp_parser_check_for_invalid_template_id (parser, type);
9637
9638       return TYPE_NAME (type);
9639     }
9640
9641   /* The type-specifier must be a user-defined type.  */
9642   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9643     {
9644       bool qualified_p;
9645       bool global_p;
9646
9647       /* Don't gobble tokens or issue error messages if this is an
9648          optional type-specifier.  */
9649       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9650         cp_parser_parse_tentatively (parser);
9651
9652       /* Look for the optional `::' operator.  */
9653       global_p
9654         = (cp_parser_global_scope_opt (parser,
9655                                        /*current_scope_valid_p=*/false)
9656            != NULL_TREE);
9657       /* Look for the nested-name specifier.  */
9658       qualified_p
9659         = (cp_parser_nested_name_specifier_opt (parser,
9660                                                 /*typename_keyword_p=*/false,
9661                                                 /*check_dependency_p=*/true,
9662                                                 /*type_p=*/false,
9663                                                 /*is_declaration=*/false)
9664            != NULL_TREE);
9665       /* If we have seen a nested-name-specifier, and the next token
9666          is `template', then we are using the template-id production.  */
9667       if (parser->scope
9668           && cp_parser_optional_template_keyword (parser))
9669         {
9670           /* Look for the template-id.  */
9671           type = cp_parser_template_id (parser,
9672                                         /*template_keyword_p=*/true,
9673                                         /*check_dependency_p=*/true,
9674                                         /*is_declaration=*/false);
9675           /* If the template-id did not name a type, we are out of
9676              luck.  */
9677           if (TREE_CODE (type) != TYPE_DECL)
9678             {
9679               cp_parser_error (parser, "expected template-id for type");
9680               type = NULL_TREE;
9681             }
9682         }
9683       /* Otherwise, look for a type-name.  */
9684       else
9685         type = cp_parser_type_name (parser);
9686       /* Keep track of all name-lookups performed in class scopes.  */
9687       if (type
9688           && !global_p
9689           && !qualified_p
9690           && TREE_CODE (type) == TYPE_DECL
9691           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9692         maybe_note_name_used_in_class (DECL_NAME (type), type);
9693       /* If it didn't work out, we don't have a TYPE.  */
9694       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9695           && !cp_parser_parse_definitely (parser))
9696         type = NULL_TREE;
9697       if (type && decl_specs)
9698         cp_parser_set_decl_spec_type (decl_specs, type,
9699                                       /*user_defined=*/true);
9700     }
9701
9702   /* If we didn't get a type-name, issue an error message.  */
9703   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9704     {
9705       cp_parser_error (parser, "expected type-name");
9706       return error_mark_node;
9707     }
9708
9709   /* There is no valid C++ program where a non-template type is
9710      followed by a "<".  That usually indicates that the user thought
9711      that the type was a template.  */
9712   if (type && type != error_mark_node)
9713     {
9714       /* As a last-ditch effort, see if TYPE is an Objective-C type.
9715          If it is, then the '<'...'>' enclose protocol names rather than
9716          template arguments, and so everything is fine.  */
9717       if (c_dialect_objc ()
9718           && (objc_is_id (type) || objc_is_class_name (type)))
9719         {
9720           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9721           tree qual_type = objc_get_protocol_qualified_type (type, protos);
9722
9723           /* Clobber the "unqualified" type previously entered into
9724              DECL_SPECS with the new, improved protocol-qualified version.  */
9725           if (decl_specs)
9726             decl_specs->type = qual_type;
9727
9728           return qual_type;
9729         }
9730
9731       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9732     }
9733
9734   return type;
9735 }
9736
9737 /* Parse a type-name.
9738
9739    type-name:
9740      class-name
9741      enum-name
9742      typedef-name
9743
9744    enum-name:
9745      identifier
9746
9747    typedef-name:
9748      identifier
9749
9750    Returns a TYPE_DECL for the type.  */
9751
9752 static tree
9753 cp_parser_type_name (cp_parser* parser)
9754 {
9755   tree type_decl;
9756   tree identifier;
9757
9758   /* We can't know yet whether it is a class-name or not.  */
9759   cp_parser_parse_tentatively (parser);
9760   /* Try a class-name.  */
9761   type_decl = cp_parser_class_name (parser,
9762                                     /*typename_keyword_p=*/false,
9763                                     /*template_keyword_p=*/false,
9764                                     none_type,
9765                                     /*check_dependency_p=*/true,
9766                                     /*class_head_p=*/false,
9767                                     /*is_declaration=*/false);
9768   /* If it's not a class-name, keep looking.  */
9769   if (!cp_parser_parse_definitely (parser))
9770     {
9771       /* It must be a typedef-name or an enum-name.  */
9772       identifier = cp_parser_identifier (parser);
9773       if (identifier == error_mark_node)
9774         return error_mark_node;
9775
9776       /* Look up the type-name.  */
9777       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9778
9779       if (TREE_CODE (type_decl) != TYPE_DECL
9780           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9781         {
9782           /* See if this is an Objective-C type.  */
9783           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9784           tree type = objc_get_protocol_qualified_type (identifier, protos);
9785           if (type)
9786             type_decl = TYPE_NAME (type);
9787         }
9788
9789       /* Issue an error if we did not find a type-name.  */
9790       if (TREE_CODE (type_decl) != TYPE_DECL)
9791         {
9792           if (!cp_parser_simulate_error (parser))
9793             cp_parser_name_lookup_error (parser, identifier, type_decl,
9794                                          "is not a type");
9795           type_decl = error_mark_node;
9796         }
9797       /* Remember that the name was used in the definition of the
9798          current class so that we can check later to see if the
9799          meaning would have been different after the class was
9800          entirely defined.  */
9801       else if (type_decl != error_mark_node
9802                && !parser->scope)
9803         maybe_note_name_used_in_class (identifier, type_decl);
9804     }
9805
9806   return type_decl;
9807 }
9808
9809
9810 /* Parse an elaborated-type-specifier.  Note that the grammar given
9811    here incorporates the resolution to DR68.
9812
9813    elaborated-type-specifier:
9814      class-key :: [opt] nested-name-specifier [opt] identifier
9815      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9816      enum :: [opt] nested-name-specifier [opt] identifier
9817      typename :: [opt] nested-name-specifier identifier
9818      typename :: [opt] nested-name-specifier template [opt]
9819        template-id
9820
9821    GNU extension:
9822
9823    elaborated-type-specifier:
9824      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9825      class-key attributes :: [opt] nested-name-specifier [opt]
9826                template [opt] template-id
9827      enum attributes :: [opt] nested-name-specifier [opt] identifier
9828
9829    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9830    declared `friend'.  If IS_DECLARATION is TRUE, then this
9831    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9832    something is being declared.
9833
9834    Returns the TYPE specified.  */
9835
9836 static tree
9837 cp_parser_elaborated_type_specifier (cp_parser* parser,
9838                                      bool is_friend,
9839                                      bool is_declaration)
9840 {
9841   enum tag_types tag_type;
9842   tree identifier;
9843   tree type = NULL_TREE;
9844   tree attributes = NULL_TREE;
9845
9846   /* See if we're looking at the `enum' keyword.  */
9847   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9848     {
9849       /* Consume the `enum' token.  */
9850       cp_lexer_consume_token (parser->lexer);
9851       /* Remember that it's an enumeration type.  */
9852       tag_type = enum_type;
9853       /* Parse the attributes.  */
9854       attributes = cp_parser_attributes_opt (parser);
9855     }
9856   /* Or, it might be `typename'.  */
9857   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9858                                            RID_TYPENAME))
9859     {
9860       /* Consume the `typename' token.  */
9861       cp_lexer_consume_token (parser->lexer);
9862       /* Remember that it's a `typename' type.  */
9863       tag_type = typename_type;
9864       /* The `typename' keyword is only allowed in templates.  */
9865       if (!processing_template_decl)
9866         pedwarn ("using %<typename%> outside of template");
9867     }
9868   /* Otherwise it must be a class-key.  */
9869   else
9870     {
9871       tag_type = cp_parser_class_key (parser);
9872       if (tag_type == none_type)
9873         return error_mark_node;
9874       /* Parse the attributes.  */
9875       attributes = cp_parser_attributes_opt (parser);
9876     }
9877
9878   /* Look for the `::' operator.  */
9879   cp_parser_global_scope_opt (parser,
9880                               /*current_scope_valid_p=*/false);
9881   /* Look for the nested-name-specifier.  */
9882   if (tag_type == typename_type)
9883     {
9884       if (!cp_parser_nested_name_specifier (parser,
9885                                            /*typename_keyword_p=*/true,
9886                                            /*check_dependency_p=*/true,
9887                                            /*type_p=*/true,
9888                                             is_declaration))
9889         return error_mark_node;
9890     }
9891   else
9892     /* Even though `typename' is not present, the proposed resolution
9893        to Core Issue 180 says that in `class A<T>::B', `B' should be
9894        considered a type-name, even if `A<T>' is dependent.  */
9895     cp_parser_nested_name_specifier_opt (parser,
9896                                          /*typename_keyword_p=*/true,
9897                                          /*check_dependency_p=*/true,
9898                                          /*type_p=*/true,
9899                                          is_declaration);
9900   /* For everything but enumeration types, consider a template-id.  */
9901   if (tag_type != enum_type)
9902     {
9903       bool template_p = false;
9904       tree decl;
9905
9906       /* Allow the `template' keyword.  */
9907       template_p = cp_parser_optional_template_keyword (parser);
9908       /* If we didn't see `template', we don't know if there's a
9909          template-id or not.  */
9910       if (!template_p)
9911         cp_parser_parse_tentatively (parser);
9912       /* Parse the template-id.  */
9913       decl = cp_parser_template_id (parser, template_p,
9914                                     /*check_dependency_p=*/true,
9915                                     is_declaration);
9916       /* If we didn't find a template-id, look for an ordinary
9917          identifier.  */
9918       if (!template_p && !cp_parser_parse_definitely (parser))
9919         ;
9920       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9921          in effect, then we must assume that, upon instantiation, the
9922          template will correspond to a class.  */
9923       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9924                && tag_type == typename_type)
9925         type = make_typename_type (parser->scope, decl,
9926                                    typename_type,
9927                                    /*complain=*/1);
9928       else
9929         type = TREE_TYPE (decl);
9930     }
9931
9932   /* For an enumeration type, consider only a plain identifier.  */
9933   if (!type)
9934     {
9935       identifier = cp_parser_identifier (parser);
9936
9937       if (identifier == error_mark_node)
9938         {
9939           parser->scope = NULL_TREE;
9940           return error_mark_node;
9941         }
9942
9943       /* For a `typename', we needn't call xref_tag.  */
9944       if (tag_type == typename_type
9945           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9946         return cp_parser_make_typename_type (parser, parser->scope,
9947                                              identifier);
9948       /* Look up a qualified name in the usual way.  */
9949       if (parser->scope)
9950         {
9951           tree decl;
9952
9953           decl = cp_parser_lookup_name (parser, identifier,
9954                                         tag_type,
9955                                         /*is_template=*/false,
9956                                         /*is_namespace=*/false,
9957                                         /*check_dependency=*/true,
9958                                         /*ambiguous_p=*/NULL);
9959
9960           /* If we are parsing friend declaration, DECL may be a
9961              TEMPLATE_DECL tree node here.  However, we need to check
9962              whether this TEMPLATE_DECL results in valid code.  Consider
9963              the following example:
9964
9965                namespace N {
9966                  template <class T> class C {};
9967                }
9968                class X {
9969                  template <class T> friend class N::C; // #1, valid code
9970                };
9971                template <class T> class Y {
9972                  friend class N::C;                    // #2, invalid code
9973                };
9974
9975              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9976              name lookup of `N::C'.  We see that friend declaration must
9977              be template for the code to be valid.  Note that
9978              processing_template_decl does not work here since it is
9979              always 1 for the above two cases.  */
9980
9981           decl = (cp_parser_maybe_treat_template_as_class
9982                   (decl, /*tag_name_p=*/is_friend
9983                          && parser->num_template_parameter_lists));
9984
9985           if (TREE_CODE (decl) != TYPE_DECL)
9986             {
9987               cp_parser_diagnose_invalid_type_name (parser,
9988                                                     parser->scope,
9989                                                     identifier);
9990               return error_mark_node;
9991             }
9992
9993           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9994             check_elaborated_type_specifier
9995               (tag_type, decl,
9996                (parser->num_template_parameter_lists
9997                 || DECL_SELF_REFERENCE_P (decl)));
9998
9999           type = TREE_TYPE (decl);
10000         }
10001       else
10002         {
10003           /* An elaborated-type-specifier sometimes introduces a new type and
10004              sometimes names an existing type.  Normally, the rule is that it
10005              introduces a new type only if there is not an existing type of
10006              the same name already in scope.  For example, given:
10007
10008                struct S {};
10009                void f() { struct S s; }
10010
10011              the `struct S' in the body of `f' is the same `struct S' as in
10012              the global scope; the existing definition is used.  However, if
10013              there were no global declaration, this would introduce a new
10014              local class named `S'.
10015
10016              An exception to this rule applies to the following code:
10017
10018                namespace N { struct S; }
10019
10020              Here, the elaborated-type-specifier names a new type
10021              unconditionally; even if there is already an `S' in the
10022              containing scope this declaration names a new type.
10023              This exception only applies if the elaborated-type-specifier
10024              forms the complete declaration:
10025
10026                [class.name]
10027
10028                A declaration consisting solely of `class-key identifier ;' is
10029                either a redeclaration of the name in the current scope or a
10030                forward declaration of the identifier as a class name.  It
10031                introduces the name into the current scope.
10032
10033              We are in this situation precisely when the next token is a `;'.
10034
10035              An exception to the exception is that a `friend' declaration does
10036              *not* name a new type; i.e., given:
10037
10038                struct S { friend struct T; };
10039
10040              `T' is not a new type in the scope of `S'.
10041
10042              Also, `new struct S' or `sizeof (struct S)' never results in the
10043              definition of a new type; a new type can only be declared in a
10044              declaration context.  */
10045
10046           tag_scope ts;
10047           if (is_friend)
10048             /* Friends have special name lookup rules.  */
10049             ts = ts_within_enclosing_non_class;
10050           else if (is_declaration
10051                    && cp_lexer_next_token_is (parser->lexer,
10052                                               CPP_SEMICOLON))
10053             /* This is a `class-key identifier ;' */
10054             ts = ts_current;
10055           else
10056             ts = ts_global;
10057
10058           /* Warn about attributes. They are ignored.  */
10059           if (attributes)
10060             warning (OPT_Wattributes,
10061                      "type attributes are honored only at type definition");
10062
10063           type = xref_tag (tag_type, identifier, ts,
10064                            parser->num_template_parameter_lists);
10065         }
10066     }
10067   if (tag_type != enum_type)
10068     cp_parser_check_class_key (tag_type, type);
10069
10070   /* A "<" cannot follow an elaborated type specifier.  If that
10071      happens, the user was probably trying to form a template-id.  */
10072   cp_parser_check_for_invalid_template_id (parser, type);
10073
10074   return type;
10075 }
10076
10077 /* Parse an enum-specifier.
10078
10079    enum-specifier:
10080      enum identifier [opt] { enumerator-list [opt] }
10081
10082    GNU Extensions:
10083      enum identifier [opt] { enumerator-list [opt] } attributes
10084
10085    Returns an ENUM_TYPE representing the enumeration.  */
10086
10087 static tree
10088 cp_parser_enum_specifier (cp_parser* parser)
10089 {
10090   tree identifier;
10091   tree type;
10092
10093   /* Caller guarantees that the current token is 'enum', an identifier
10094      possibly follows, and the token after that is an opening brace.
10095      If we don't have an identifier, fabricate an anonymous name for
10096      the enumeration being defined.  */
10097   cp_lexer_consume_token (parser->lexer);
10098
10099   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10100     identifier = cp_parser_identifier (parser);
10101   else
10102     identifier = make_anon_name ();
10103
10104   /* Issue an error message if type-definitions are forbidden here.  */
10105   cp_parser_check_type_definition (parser);
10106
10107   /* Create the new type.  We do this before consuming the opening brace
10108      so the enum will be recorded as being on the line of its tag (or the
10109      'enum' keyword, if there is no tag).  */
10110   type = start_enum (identifier);
10111
10112   /* Consume the opening brace.  */
10113   cp_lexer_consume_token (parser->lexer);
10114
10115   /* If the next token is not '}', then there are some enumerators.  */
10116   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10117     cp_parser_enumerator_list (parser, type);
10118
10119   /* Consume the final '}'.  */
10120   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10121
10122   /* Look for trailing attributes to apply to this enumeration, and
10123      apply them if appropriate.  */
10124   if (cp_parser_allow_gnu_extensions_p (parser))
10125     {
10126       tree trailing_attr = cp_parser_attributes_opt (parser);
10127       cplus_decl_attributes (&type,
10128                              trailing_attr,
10129                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10130     }
10131
10132   /* Finish up the enumeration.  */
10133   finish_enum (type);
10134
10135   return type;
10136 }
10137
10138 /* Parse an enumerator-list.  The enumerators all have the indicated
10139    TYPE.
10140
10141    enumerator-list:
10142      enumerator-definition
10143      enumerator-list , enumerator-definition  */
10144
10145 static void
10146 cp_parser_enumerator_list (cp_parser* parser, tree type)
10147 {
10148   while (true)
10149     {
10150       /* Parse an enumerator-definition.  */
10151       cp_parser_enumerator_definition (parser, type);
10152
10153       /* If the next token is not a ',', we've reached the end of
10154          the list.  */
10155       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10156         break;
10157       /* Otherwise, consume the `,' and keep going.  */
10158       cp_lexer_consume_token (parser->lexer);
10159       /* If the next token is a `}', there is a trailing comma.  */
10160       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10161         {
10162           if (pedantic && !in_system_header)
10163             pedwarn ("comma at end of enumerator list");
10164           break;
10165         }
10166     }
10167 }
10168
10169 /* Parse an enumerator-definition.  The enumerator has the indicated
10170    TYPE.
10171
10172    enumerator-definition:
10173      enumerator
10174      enumerator = constant-expression
10175
10176    enumerator:
10177      identifier  */
10178
10179 static void
10180 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10181 {
10182   tree identifier;
10183   tree value;
10184
10185   /* Look for the identifier.  */
10186   identifier = cp_parser_identifier (parser);
10187   if (identifier == error_mark_node)
10188     return;
10189
10190   /* If the next token is an '=', then there is an explicit value.  */
10191   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10192     {
10193       /* Consume the `=' token.  */
10194       cp_lexer_consume_token (parser->lexer);
10195       /* Parse the value.  */
10196       value = cp_parser_constant_expression (parser,
10197                                              /*allow_non_constant_p=*/false,
10198                                              NULL);
10199     }
10200   else
10201     value = NULL_TREE;
10202
10203   /* Create the enumerator.  */
10204   build_enumerator (identifier, value, type);
10205 }
10206
10207 /* Parse a namespace-name.
10208
10209    namespace-name:
10210      original-namespace-name
10211      namespace-alias
10212
10213    Returns the NAMESPACE_DECL for the namespace.  */
10214
10215 static tree
10216 cp_parser_namespace_name (cp_parser* parser)
10217 {
10218   tree identifier;
10219   tree namespace_decl;
10220
10221   /* Get the name of the namespace.  */
10222   identifier = cp_parser_identifier (parser);
10223   if (identifier == error_mark_node)
10224     return error_mark_node;
10225
10226   /* Look up the identifier in the currently active scope.  Look only
10227      for namespaces, due to:
10228
10229        [basic.lookup.udir]
10230
10231        When looking up a namespace-name in a using-directive or alias
10232        definition, only namespace names are considered.
10233
10234      And:
10235
10236        [basic.lookup.qual]
10237
10238        During the lookup of a name preceding the :: scope resolution
10239        operator, object, function, and enumerator names are ignored.
10240
10241      (Note that cp_parser_class_or_namespace_name only calls this
10242      function if the token after the name is the scope resolution
10243      operator.)  */
10244   namespace_decl = cp_parser_lookup_name (parser, identifier,
10245                                           none_type,
10246                                           /*is_template=*/false,
10247                                           /*is_namespace=*/true,
10248                                           /*check_dependency=*/true,
10249                                           /*ambiguous_p=*/NULL);
10250   /* If it's not a namespace, issue an error.  */
10251   if (namespace_decl == error_mark_node
10252       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10253     {
10254       cp_parser_error (parser, "expected namespace-name");
10255       namespace_decl = error_mark_node;
10256     }
10257
10258   return namespace_decl;
10259 }
10260
10261 /* Parse a namespace-definition.
10262
10263    namespace-definition:
10264      named-namespace-definition
10265      unnamed-namespace-definition
10266
10267    named-namespace-definition:
10268      original-namespace-definition
10269      extension-namespace-definition
10270
10271    original-namespace-definition:
10272      namespace identifier { namespace-body }
10273
10274    extension-namespace-definition:
10275      namespace original-namespace-name { namespace-body }
10276
10277    unnamed-namespace-definition:
10278      namespace { namespace-body } */
10279
10280 static void
10281 cp_parser_namespace_definition (cp_parser* parser)
10282 {
10283   tree identifier;
10284
10285   /* Look for the `namespace' keyword.  */
10286   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10287
10288   /* Get the name of the namespace.  We do not attempt to distinguish
10289      between an original-namespace-definition and an
10290      extension-namespace-definition at this point.  The semantic
10291      analysis routines are responsible for that.  */
10292   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10293     identifier = cp_parser_identifier (parser);
10294   else
10295     identifier = NULL_TREE;
10296
10297   /* Look for the `{' to start the namespace.  */
10298   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10299   /* Start the namespace.  */
10300   push_namespace (identifier);
10301   /* Parse the body of the namespace.  */
10302   cp_parser_namespace_body (parser);
10303   /* Finish the namespace.  */
10304   pop_namespace ();
10305   /* Look for the final `}'.  */
10306   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10307 }
10308
10309 /* Parse a namespace-body.
10310
10311    namespace-body:
10312      declaration-seq [opt]  */
10313
10314 static void
10315 cp_parser_namespace_body (cp_parser* parser)
10316 {
10317   cp_parser_declaration_seq_opt (parser);
10318 }
10319
10320 /* Parse a namespace-alias-definition.
10321
10322    namespace-alias-definition:
10323      namespace identifier = qualified-namespace-specifier ;  */
10324
10325 static void
10326 cp_parser_namespace_alias_definition (cp_parser* parser)
10327 {
10328   tree identifier;
10329   tree namespace_specifier;
10330
10331   /* Look for the `namespace' keyword.  */
10332   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10333   /* Look for the identifier.  */
10334   identifier = cp_parser_identifier (parser);
10335   if (identifier == error_mark_node)
10336     return;
10337   /* Look for the `=' token.  */
10338   cp_parser_require (parser, CPP_EQ, "`='");
10339   /* Look for the qualified-namespace-specifier.  */
10340   namespace_specifier
10341     = cp_parser_qualified_namespace_specifier (parser);
10342   /* Look for the `;' token.  */
10343   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10344
10345   /* Register the alias in the symbol table.  */
10346   do_namespace_alias (identifier, namespace_specifier);
10347 }
10348
10349 /* Parse a qualified-namespace-specifier.
10350
10351    qualified-namespace-specifier:
10352      :: [opt] nested-name-specifier [opt] namespace-name
10353
10354    Returns a NAMESPACE_DECL corresponding to the specified
10355    namespace.  */
10356
10357 static tree
10358 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10359 {
10360   /* Look for the optional `::'.  */
10361   cp_parser_global_scope_opt (parser,
10362                               /*current_scope_valid_p=*/false);
10363
10364   /* Look for the optional nested-name-specifier.  */
10365   cp_parser_nested_name_specifier_opt (parser,
10366                                        /*typename_keyword_p=*/false,
10367                                        /*check_dependency_p=*/true,
10368                                        /*type_p=*/false,
10369                                        /*is_declaration=*/true);
10370
10371   return cp_parser_namespace_name (parser);
10372 }
10373
10374 /* Parse a using-declaration.
10375
10376    using-declaration:
10377      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10378      using :: unqualified-id ;  */
10379
10380 static void
10381 cp_parser_using_declaration (cp_parser* parser)
10382 {
10383   cp_token *token;
10384   bool typename_p = false;
10385   bool global_scope_p;
10386   tree decl;
10387   tree identifier;
10388   tree qscope;
10389
10390   /* Look for the `using' keyword.  */
10391   cp_parser_require_keyword (parser, RID_USING, "`using'");
10392
10393   /* Peek at the next token.  */
10394   token = cp_lexer_peek_token (parser->lexer);
10395   /* See if it's `typename'.  */
10396   if (token->keyword == RID_TYPENAME)
10397     {
10398       /* Remember that we've seen it.  */
10399       typename_p = true;
10400       /* Consume the `typename' token.  */
10401       cp_lexer_consume_token (parser->lexer);
10402     }
10403
10404   /* Look for the optional global scope qualification.  */
10405   global_scope_p
10406     = (cp_parser_global_scope_opt (parser,
10407                                    /*current_scope_valid_p=*/false)
10408        != NULL_TREE);
10409
10410   /* If we saw `typename', or didn't see `::', then there must be a
10411      nested-name-specifier present.  */
10412   if (typename_p || !global_scope_p)
10413     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10414                                               /*check_dependency_p=*/true,
10415                                               /*type_p=*/false,
10416                                               /*is_declaration=*/true);
10417   /* Otherwise, we could be in either of the two productions.  In that
10418      case, treat the nested-name-specifier as optional.  */
10419   else
10420     qscope = cp_parser_nested_name_specifier_opt (parser,
10421                                                   /*typename_keyword_p=*/false,
10422                                                   /*check_dependency_p=*/true,
10423                                                   /*type_p=*/false,
10424                                                   /*is_declaration=*/true);
10425   if (!qscope)
10426     qscope = global_namespace;
10427
10428   /* Parse the unqualified-id.  */
10429   identifier = cp_parser_unqualified_id (parser,
10430                                          /*template_keyword_p=*/false,
10431                                          /*check_dependency_p=*/true,
10432                                          /*declarator_p=*/true);
10433
10434   /* The function we call to handle a using-declaration is different
10435      depending on what scope we are in.  */
10436   if (identifier == error_mark_node)
10437     ;
10438   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10439            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10440     /* [namespace.udecl]
10441
10442        A using declaration shall not name a template-id.  */
10443     error ("a template-id may not appear in a using-declaration");
10444   else
10445     {
10446       if (at_class_scope_p ())
10447         {
10448           /* Create the USING_DECL.  */
10449           decl = do_class_using_decl (parser->scope, identifier);
10450           /* Add it to the list of members in this class.  */
10451           finish_member_declaration (decl);
10452         }
10453       else
10454         {
10455           decl = cp_parser_lookup_name_simple (parser, identifier);
10456           if (decl == error_mark_node)
10457             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10458           else if (!at_namespace_scope_p ())
10459             do_local_using_decl (decl, qscope, identifier);
10460           else
10461             do_toplevel_using_decl (decl, qscope, identifier);
10462         }
10463     }
10464
10465   /* Look for the final `;'.  */
10466   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10467 }
10468
10469 /* Parse a using-directive.
10470
10471    using-directive:
10472      using namespace :: [opt] nested-name-specifier [opt]
10473        namespace-name ;  */
10474
10475 static void
10476 cp_parser_using_directive (cp_parser* parser)
10477 {
10478   tree namespace_decl;
10479   tree attribs;
10480
10481   /* Look for the `using' keyword.  */
10482   cp_parser_require_keyword (parser, RID_USING, "`using'");
10483   /* And the `namespace' keyword.  */
10484   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10485   /* Look for the optional `::' operator.  */
10486   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10487   /* And the optional nested-name-specifier.  */
10488   cp_parser_nested_name_specifier_opt (parser,
10489                                        /*typename_keyword_p=*/false,
10490                                        /*check_dependency_p=*/true,
10491                                        /*type_p=*/false,
10492                                        /*is_declaration=*/true);
10493   /* Get the namespace being used.  */
10494   namespace_decl = cp_parser_namespace_name (parser);
10495   /* And any specified attributes.  */
10496   attribs = cp_parser_attributes_opt (parser);
10497   /* Update the symbol table.  */
10498   parse_using_directive (namespace_decl, attribs);
10499   /* Look for the final `;'.  */
10500   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10501 }
10502
10503 /* Parse an asm-definition.
10504
10505    asm-definition:
10506      asm ( string-literal ) ;
10507
10508    GNU Extension:
10509
10510    asm-definition:
10511      asm volatile [opt] ( string-literal ) ;
10512      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10513      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10514                           : asm-operand-list [opt] ) ;
10515      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10516                           : asm-operand-list [opt]
10517                           : asm-operand-list [opt] ) ;  */
10518
10519 static void
10520 cp_parser_asm_definition (cp_parser* parser)
10521 {
10522   tree string;
10523   tree outputs = NULL_TREE;
10524   tree inputs = NULL_TREE;
10525   tree clobbers = NULL_TREE;
10526   tree asm_stmt;
10527   bool volatile_p = false;
10528   bool extended_p = false;
10529
10530   /* Look for the `asm' keyword.  */
10531   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10532   /* See if the next token is `volatile'.  */
10533   if (cp_parser_allow_gnu_extensions_p (parser)
10534       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10535     {
10536       /* Remember that we saw the `volatile' keyword.  */
10537       volatile_p = true;
10538       /* Consume the token.  */
10539       cp_lexer_consume_token (parser->lexer);
10540     }
10541   /* Look for the opening `('.  */
10542   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10543     return;
10544   /* Look for the string.  */
10545   string = cp_parser_string_literal (parser, false, false);
10546   if (string == error_mark_node)
10547     {
10548       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10549                                              /*consume_paren=*/true);
10550       return;
10551     }
10552
10553   /* If we're allowing GNU extensions, check for the extended assembly
10554      syntax.  Unfortunately, the `:' tokens need not be separated by
10555      a space in C, and so, for compatibility, we tolerate that here
10556      too.  Doing that means that we have to treat the `::' operator as
10557      two `:' tokens.  */
10558   if (cp_parser_allow_gnu_extensions_p (parser)
10559       && at_function_scope_p ()
10560       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10561           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10562     {
10563       bool inputs_p = false;
10564       bool clobbers_p = false;
10565
10566       /* The extended syntax was used.  */
10567       extended_p = true;
10568
10569       /* Look for outputs.  */
10570       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10571         {
10572           /* Consume the `:'.  */
10573           cp_lexer_consume_token (parser->lexer);
10574           /* Parse the output-operands.  */
10575           if (cp_lexer_next_token_is_not (parser->lexer,
10576                                           CPP_COLON)
10577               && cp_lexer_next_token_is_not (parser->lexer,
10578                                              CPP_SCOPE)
10579               && cp_lexer_next_token_is_not (parser->lexer,
10580                                              CPP_CLOSE_PAREN))
10581             outputs = cp_parser_asm_operand_list (parser);
10582         }
10583       /* If the next token is `::', there are no outputs, and the
10584          next token is the beginning of the inputs.  */
10585       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10586         /* The inputs are coming next.  */
10587         inputs_p = true;
10588
10589       /* Look for inputs.  */
10590       if (inputs_p
10591           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10592         {
10593           /* Consume the `:' or `::'.  */
10594           cp_lexer_consume_token (parser->lexer);
10595           /* Parse the output-operands.  */
10596           if (cp_lexer_next_token_is_not (parser->lexer,
10597                                           CPP_COLON)
10598               && cp_lexer_next_token_is_not (parser->lexer,
10599                                              CPP_CLOSE_PAREN))
10600             inputs = cp_parser_asm_operand_list (parser);
10601         }
10602       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10603         /* The clobbers are coming next.  */
10604         clobbers_p = true;
10605
10606       /* Look for clobbers.  */
10607       if (clobbers_p
10608           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10609         {
10610           /* Consume the `:' or `::'.  */
10611           cp_lexer_consume_token (parser->lexer);
10612           /* Parse the clobbers.  */
10613           if (cp_lexer_next_token_is_not (parser->lexer,
10614                                           CPP_CLOSE_PAREN))
10615             clobbers = cp_parser_asm_clobber_list (parser);
10616         }
10617     }
10618   /* Look for the closing `)'.  */
10619   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10620     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10621                                            /*consume_paren=*/true);
10622   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10623
10624   /* Create the ASM_EXPR.  */
10625   if (at_function_scope_p ())
10626     {
10627       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10628                                   inputs, clobbers);
10629       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10630       if (!extended_p)
10631         {
10632           tree temp = asm_stmt;
10633           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10634             temp = TREE_OPERAND (temp, 0);
10635
10636           ASM_INPUT_P (temp) = 1;
10637         }
10638     }
10639   else
10640     assemble_asm (string);
10641 }
10642
10643 /* Declarators [gram.dcl.decl] */
10644
10645 /* Parse an init-declarator.
10646
10647    init-declarator:
10648      declarator initializer [opt]
10649
10650    GNU Extension:
10651
10652    init-declarator:
10653      declarator asm-specification [opt] attributes [opt] initializer [opt]
10654
10655    function-definition:
10656      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10657        function-body
10658      decl-specifier-seq [opt] declarator function-try-block
10659
10660    GNU Extension:
10661
10662    function-definition:
10663      __extension__ function-definition
10664
10665    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10666    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10667    then this declarator appears in a class scope.  The new DECL created
10668    by this declarator is returned.
10669
10670    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10671    for a function-definition here as well.  If the declarator is a
10672    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10673    be TRUE upon return.  By that point, the function-definition will
10674    have been completely parsed.
10675
10676    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10677    is FALSE.  */
10678
10679 static tree
10680 cp_parser_init_declarator (cp_parser* parser,
10681                            cp_decl_specifier_seq *decl_specifiers,
10682                            bool function_definition_allowed_p,
10683                            bool member_p,
10684                            int declares_class_or_enum,
10685                            bool* function_definition_p)
10686 {
10687   cp_token *token;
10688   cp_declarator *declarator;
10689   tree prefix_attributes;
10690   tree attributes;
10691   tree asm_specification;
10692   tree initializer;
10693   tree decl = NULL_TREE;
10694   tree scope;
10695   bool is_initialized;
10696   bool is_parenthesized_init;
10697   bool is_non_constant_init;
10698   int ctor_dtor_or_conv_p;
10699   bool friend_p;
10700   tree pushed_scope = NULL;
10701
10702   /* Gather the attributes that were provided with the
10703      decl-specifiers.  */
10704   prefix_attributes = decl_specifiers->attributes;
10705
10706   /* Assume that this is not the declarator for a function
10707      definition.  */
10708   if (function_definition_p)
10709     *function_definition_p = false;
10710
10711   /* Defer access checks while parsing the declarator; we cannot know
10712      what names are accessible until we know what is being
10713      declared.  */
10714   resume_deferring_access_checks ();
10715
10716   /* Parse the declarator.  */
10717   declarator
10718     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10719                             &ctor_dtor_or_conv_p,
10720                             /*parenthesized_p=*/NULL,
10721                             /*member_p=*/false);
10722   /* Gather up the deferred checks.  */
10723   stop_deferring_access_checks ();
10724
10725   /* If the DECLARATOR was erroneous, there's no need to go
10726      further.  */
10727   if (declarator == cp_error_declarator)
10728     return error_mark_node;
10729
10730   if (declares_class_or_enum & 2)
10731     cp_parser_check_for_definition_in_return_type (declarator,
10732                                                    decl_specifiers->type);
10733
10734   /* Figure out what scope the entity declared by the DECLARATOR is
10735      located in.  `grokdeclarator' sometimes changes the scope, so
10736      we compute it now.  */
10737   scope = get_scope_of_declarator (declarator);
10738
10739   /* If we're allowing GNU extensions, look for an asm-specification
10740      and attributes.  */
10741   if (cp_parser_allow_gnu_extensions_p (parser))
10742     {
10743       /* Look for an asm-specification.  */
10744       asm_specification = cp_parser_asm_specification_opt (parser);
10745       /* And attributes.  */
10746       attributes = cp_parser_attributes_opt (parser);
10747     }
10748   else
10749     {
10750       asm_specification = NULL_TREE;
10751       attributes = NULL_TREE;
10752     }
10753
10754   /* Peek at the next token.  */
10755   token = cp_lexer_peek_token (parser->lexer);
10756   /* Check to see if the token indicates the start of a
10757      function-definition.  */
10758   if (cp_parser_token_starts_function_definition_p (token))
10759     {
10760       if (!function_definition_allowed_p)
10761         {
10762           /* If a function-definition should not appear here, issue an
10763              error message.  */
10764           cp_parser_error (parser,
10765                            "a function-definition is not allowed here");
10766           return error_mark_node;
10767         }
10768       else
10769         {
10770           /* Neither attributes nor an asm-specification are allowed
10771              on a function-definition.  */
10772           if (asm_specification)
10773             error ("an asm-specification is not allowed on a function-definition");
10774           if (attributes)
10775             error ("attributes are not allowed on a function-definition");
10776           /* This is a function-definition.  */
10777           *function_definition_p = true;
10778
10779           /* Parse the function definition.  */
10780           if (member_p)
10781             decl = cp_parser_save_member_function_body (parser,
10782                                                         decl_specifiers,
10783                                                         declarator,
10784                                                         prefix_attributes);
10785           else
10786             decl
10787               = (cp_parser_function_definition_from_specifiers_and_declarator
10788                  (parser, decl_specifiers, prefix_attributes, declarator));
10789
10790           return decl;
10791         }
10792     }
10793
10794   /* [dcl.dcl]
10795
10796      Only in function declarations for constructors, destructors, and
10797      type conversions can the decl-specifier-seq be omitted.
10798
10799      We explicitly postpone this check past the point where we handle
10800      function-definitions because we tolerate function-definitions
10801      that are missing their return types in some modes.  */
10802   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10803     {
10804       cp_parser_error (parser,
10805                        "expected constructor, destructor, or type conversion");
10806       return error_mark_node;
10807     }
10808
10809   /* An `=' or an `(' indicates an initializer.  */
10810   is_initialized = (token->type == CPP_EQ
10811                      || token->type == CPP_OPEN_PAREN);
10812   /* If the init-declarator isn't initialized and isn't followed by a
10813      `,' or `;', it's not a valid init-declarator.  */
10814   if (!is_initialized
10815       && token->type != CPP_COMMA
10816       && token->type != CPP_SEMICOLON)
10817     {
10818       cp_parser_error (parser, "expected initializer");
10819       return error_mark_node;
10820     }
10821
10822   /* Because start_decl has side-effects, we should only call it if we
10823      know we're going ahead.  By this point, we know that we cannot
10824      possibly be looking at any other construct.  */
10825   cp_parser_commit_to_tentative_parse (parser);
10826
10827   /* If the decl specifiers were bad, issue an error now that we're
10828      sure this was intended to be a declarator.  Then continue
10829      declaring the variable(s), as int, to try to cut down on further
10830      errors.  */
10831   if (decl_specifiers->any_specifiers_p
10832       && decl_specifiers->type == error_mark_node)
10833     {
10834       cp_parser_error (parser, "invalid type in declaration");
10835       decl_specifiers->type = integer_type_node;
10836     }
10837
10838   /* Check to see whether or not this declaration is a friend.  */
10839   friend_p = cp_parser_friend_p (decl_specifiers);
10840
10841   /* Check that the number of template-parameter-lists is OK.  */
10842   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10843     return error_mark_node;
10844
10845   /* Enter the newly declared entry in the symbol table.  If we're
10846      processing a declaration in a class-specifier, we wait until
10847      after processing the initializer.  */
10848   if (!member_p)
10849     {
10850       if (parser->in_unbraced_linkage_specification_p)
10851         {
10852           decl_specifiers->storage_class = sc_extern;
10853           have_extern_spec = false;
10854         }
10855       decl = start_decl (declarator, decl_specifiers,
10856                          is_initialized, attributes, prefix_attributes,
10857                          &pushed_scope);
10858     }
10859   else if (scope)
10860     /* Enter the SCOPE.  That way unqualified names appearing in the
10861        initializer will be looked up in SCOPE.  */
10862     pushed_scope = push_scope (scope);
10863
10864   /* Perform deferred access control checks, now that we know in which
10865      SCOPE the declared entity resides.  */
10866   if (!member_p && decl)
10867     {
10868       tree saved_current_function_decl = NULL_TREE;
10869
10870       /* If the entity being declared is a function, pretend that we
10871          are in its scope.  If it is a `friend', it may have access to
10872          things that would not otherwise be accessible.  */
10873       if (TREE_CODE (decl) == FUNCTION_DECL)
10874         {
10875           saved_current_function_decl = current_function_decl;
10876           current_function_decl = decl;
10877         }
10878
10879       /* Perform the access control checks for the declarator and the
10880          the decl-specifiers.  */
10881       perform_deferred_access_checks ();
10882
10883       /* Restore the saved value.  */
10884       if (TREE_CODE (decl) == FUNCTION_DECL)
10885         current_function_decl = saved_current_function_decl;
10886     }
10887
10888   /* Parse the initializer.  */
10889   if (is_initialized)
10890     initializer = cp_parser_initializer (parser,
10891                                          &is_parenthesized_init,
10892                                          &is_non_constant_init);
10893   else
10894     {
10895       initializer = NULL_TREE;
10896       is_parenthesized_init = false;
10897       is_non_constant_init = true;
10898     }
10899
10900   /* The old parser allows attributes to appear after a parenthesized
10901      initializer.  Mark Mitchell proposed removing this functionality
10902      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10903      attributes -- but ignores them.  */
10904   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10905     if (cp_parser_attributes_opt (parser))
10906       warning (OPT_Wattributes,
10907                "attributes after parenthesized initializer ignored");
10908
10909   /* For an in-class declaration, use `grokfield' to create the
10910      declaration.  */
10911   if (member_p)
10912     {
10913       if (pushed_scope)
10914         {
10915           pop_scope (pushed_scope);
10916           pushed_scope = false;
10917         }
10918       decl = grokfield (declarator, decl_specifiers,
10919                         initializer, /*asmspec=*/NULL_TREE,
10920                         /*attributes=*/NULL_TREE);
10921       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10922         cp_parser_save_default_args (parser, decl);
10923     }
10924
10925   /* Finish processing the declaration.  But, skip friend
10926      declarations.  */
10927   if (!friend_p && decl && decl != error_mark_node)
10928     {
10929       cp_finish_decl (decl,
10930                       initializer,
10931                       asm_specification,
10932                       /* If the initializer is in parentheses, then this is
10933                          a direct-initialization, which means that an
10934                          `explicit' constructor is OK.  Otherwise, an
10935                          `explicit' constructor cannot be used.  */
10936                       ((is_parenthesized_init || !is_initialized)
10937                      ? 0 : LOOKUP_ONLYCONVERTING));
10938     }
10939   if (!friend_p && pushed_scope)
10940     pop_scope (pushed_scope);
10941
10942   /* Remember whether or not variables were initialized by
10943      constant-expressions.  */
10944   if (decl && TREE_CODE (decl) == VAR_DECL
10945       && is_initialized && !is_non_constant_init)
10946     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10947
10948   return decl;
10949 }
10950
10951 /* Parse a declarator.
10952
10953    declarator:
10954      direct-declarator
10955      ptr-operator declarator
10956
10957    abstract-declarator:
10958      ptr-operator abstract-declarator [opt]
10959      direct-abstract-declarator
10960
10961    GNU Extensions:
10962
10963    declarator:
10964      attributes [opt] direct-declarator
10965      attributes [opt] ptr-operator declarator
10966
10967    abstract-declarator:
10968      attributes [opt] ptr-operator abstract-declarator [opt]
10969      attributes [opt] direct-abstract-declarator
10970
10971    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10972    detect constructor, destructor or conversion operators. It is set
10973    to -1 if the declarator is a name, and +1 if it is a
10974    function. Otherwise it is set to zero. Usually you just want to
10975    test for >0, but internally the negative value is used.
10976
10977    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10978    a decl-specifier-seq unless it declares a constructor, destructor,
10979    or conversion.  It might seem that we could check this condition in
10980    semantic analysis, rather than parsing, but that makes it difficult
10981    to handle something like `f()'.  We want to notice that there are
10982    no decl-specifiers, and therefore realize that this is an
10983    expression, not a declaration.)
10984
10985    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10986    the declarator is a direct-declarator of the form "(...)".
10987
10988    MEMBER_P is true iff this declarator is a member-declarator.  */
10989
10990 static cp_declarator *
10991 cp_parser_declarator (cp_parser* parser,
10992                       cp_parser_declarator_kind dcl_kind,
10993                       int* ctor_dtor_or_conv_p,
10994                       bool* parenthesized_p,
10995                       bool member_p)
10996 {
10997   cp_token *token;
10998   cp_declarator *declarator;
10999   enum tree_code code;
11000   cp_cv_quals cv_quals;
11001   tree class_type;
11002   tree attributes = NULL_TREE;
11003
11004   /* Assume this is not a constructor, destructor, or type-conversion
11005      operator.  */
11006   if (ctor_dtor_or_conv_p)
11007     *ctor_dtor_or_conv_p = 0;
11008
11009   if (cp_parser_allow_gnu_extensions_p (parser))
11010     attributes = cp_parser_attributes_opt (parser);
11011
11012   /* Peek at the next token.  */
11013   token = cp_lexer_peek_token (parser->lexer);
11014
11015   /* Check for the ptr-operator production.  */
11016   cp_parser_parse_tentatively (parser);
11017   /* Parse the ptr-operator.  */
11018   code = cp_parser_ptr_operator (parser,
11019                                  &class_type,
11020                                  &cv_quals);
11021   /* If that worked, then we have a ptr-operator.  */
11022   if (cp_parser_parse_definitely (parser))
11023     {
11024       /* If a ptr-operator was found, then this declarator was not
11025          parenthesized.  */
11026       if (parenthesized_p)
11027         *parenthesized_p = true;
11028       /* The dependent declarator is optional if we are parsing an
11029          abstract-declarator.  */
11030       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11031         cp_parser_parse_tentatively (parser);
11032
11033       /* Parse the dependent declarator.  */
11034       declarator = cp_parser_declarator (parser, dcl_kind,
11035                                          /*ctor_dtor_or_conv_p=*/NULL,
11036                                          /*parenthesized_p=*/NULL,
11037                                          /*member_p=*/false);
11038
11039       /* If we are parsing an abstract-declarator, we must handle the
11040          case where the dependent declarator is absent.  */
11041       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11042           && !cp_parser_parse_definitely (parser))
11043         declarator = NULL;
11044
11045       /* Build the representation of the ptr-operator.  */
11046       if (class_type)
11047         declarator = make_ptrmem_declarator (cv_quals,
11048                                              class_type,
11049                                              declarator);
11050       else if (code == INDIRECT_REF)
11051         declarator = make_pointer_declarator (cv_quals, declarator);
11052       else
11053         declarator = make_reference_declarator (cv_quals, declarator);
11054     }
11055   /* Everything else is a direct-declarator.  */
11056   else
11057     {
11058       if (parenthesized_p)
11059         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11060                                                    CPP_OPEN_PAREN);
11061       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11062                                                 ctor_dtor_or_conv_p,
11063                                                 member_p);
11064     }
11065
11066   if (attributes && declarator != cp_error_declarator)
11067     declarator->attributes = attributes;
11068
11069   return declarator;
11070 }
11071
11072 /* Parse a direct-declarator or direct-abstract-declarator.
11073
11074    direct-declarator:
11075      declarator-id
11076      direct-declarator ( parameter-declaration-clause )
11077        cv-qualifier-seq [opt]
11078        exception-specification [opt]
11079      direct-declarator [ constant-expression [opt] ]
11080      ( declarator )
11081
11082    direct-abstract-declarator:
11083      direct-abstract-declarator [opt]
11084        ( parameter-declaration-clause )
11085        cv-qualifier-seq [opt]
11086        exception-specification [opt]
11087      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11088      ( abstract-declarator )
11089
11090    Returns a representation of the declarator.  DCL_KIND is
11091    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11092    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11093    we are parsing a direct-declarator.  It is
11094    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11095    of ambiguity we prefer an abstract declarator, as per
11096    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11097    cp_parser_declarator.  */
11098
11099 static cp_declarator *
11100 cp_parser_direct_declarator (cp_parser* parser,
11101                              cp_parser_declarator_kind dcl_kind,
11102                              int* ctor_dtor_or_conv_p,
11103                              bool member_p)
11104 {
11105   cp_token *token;
11106   cp_declarator *declarator = NULL;
11107   tree scope = NULL_TREE;
11108   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11109   bool saved_in_declarator_p = parser->in_declarator_p;
11110   bool first = true;
11111   tree pushed_scope = NULL_TREE;
11112
11113   while (true)
11114     {
11115       /* Peek at the next token.  */
11116       token = cp_lexer_peek_token (parser->lexer);
11117       if (token->type == CPP_OPEN_PAREN)
11118         {
11119           /* This is either a parameter-declaration-clause, or a
11120              parenthesized declarator. When we know we are parsing a
11121              named declarator, it must be a parenthesized declarator
11122              if FIRST is true. For instance, `(int)' is a
11123              parameter-declaration-clause, with an omitted
11124              direct-abstract-declarator. But `((*))', is a
11125              parenthesized abstract declarator. Finally, when T is a
11126              template parameter `(T)' is a
11127              parameter-declaration-clause, and not a parenthesized
11128              named declarator.
11129
11130              We first try and parse a parameter-declaration-clause,
11131              and then try a nested declarator (if FIRST is true).
11132
11133              It is not an error for it not to be a
11134              parameter-declaration-clause, even when FIRST is
11135              false. Consider,
11136
11137                int i (int);
11138                int i (3);
11139
11140              The first is the declaration of a function while the
11141              second is a the definition of a variable, including its
11142              initializer.
11143
11144              Having seen only the parenthesis, we cannot know which of
11145              these two alternatives should be selected.  Even more
11146              complex are examples like:
11147
11148                int i (int (a));
11149                int i (int (3));
11150
11151              The former is a function-declaration; the latter is a
11152              variable initialization.
11153
11154              Thus again, we try a parameter-declaration-clause, and if
11155              that fails, we back out and return.  */
11156
11157           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11158             {
11159               cp_parameter_declarator *params;
11160               unsigned saved_num_template_parameter_lists;
11161
11162               /* In a member-declarator, the only valid interpretation
11163                  of a parenthesis is the start of a
11164                  parameter-declaration-clause.  (It is invalid to
11165                  initialize a static data member with a parenthesized
11166                  initializer; only the "=" form of initialization is
11167                  permitted.)  */
11168               if (!member_p)
11169                 cp_parser_parse_tentatively (parser);
11170
11171               /* Consume the `('.  */
11172               cp_lexer_consume_token (parser->lexer);
11173               if (first)
11174                 {
11175                   /* If this is going to be an abstract declarator, we're
11176                      in a declarator and we can't have default args.  */
11177                   parser->default_arg_ok_p = false;
11178                   parser->in_declarator_p = true;
11179                 }
11180
11181               /* Inside the function parameter list, surrounding
11182                  template-parameter-lists do not apply.  */
11183               saved_num_template_parameter_lists
11184                 = parser->num_template_parameter_lists;
11185               parser->num_template_parameter_lists = 0;
11186
11187               /* Parse the parameter-declaration-clause.  */
11188               params = cp_parser_parameter_declaration_clause (parser);
11189
11190               parser->num_template_parameter_lists
11191                 = saved_num_template_parameter_lists;
11192
11193               /* If all went well, parse the cv-qualifier-seq and the
11194                  exception-specification.  */
11195               if (member_p || cp_parser_parse_definitely (parser))
11196                 {
11197                   cp_cv_quals cv_quals;
11198                   tree exception_specification;
11199
11200                   if (ctor_dtor_or_conv_p)
11201                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11202                   first = false;
11203                   /* Consume the `)'.  */
11204                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11205
11206                   /* Parse the cv-qualifier-seq.  */
11207                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11208                   /* And the exception-specification.  */
11209                   exception_specification
11210                     = cp_parser_exception_specification_opt (parser);
11211
11212                   /* Create the function-declarator.  */
11213                   declarator = make_call_declarator (declarator,
11214                                                      params,
11215                                                      cv_quals,
11216                                                      exception_specification);
11217                   /* Any subsequent parameter lists are to do with
11218                      return type, so are not those of the declared
11219                      function.  */
11220                   parser->default_arg_ok_p = false;
11221
11222                   /* Repeat the main loop.  */
11223                   continue;
11224                 }
11225             }
11226
11227           /* If this is the first, we can try a parenthesized
11228              declarator.  */
11229           if (first)
11230             {
11231               bool saved_in_type_id_in_expr_p;
11232
11233               parser->default_arg_ok_p = saved_default_arg_ok_p;
11234               parser->in_declarator_p = saved_in_declarator_p;
11235
11236               /* Consume the `('.  */
11237               cp_lexer_consume_token (parser->lexer);
11238               /* Parse the nested declarator.  */
11239               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11240               parser->in_type_id_in_expr_p = true;
11241               declarator
11242                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11243                                         /*parenthesized_p=*/NULL,
11244                                         member_p);
11245               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11246               first = false;
11247               /* Expect a `)'.  */
11248               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11249                 declarator = cp_error_declarator;
11250               if (declarator == cp_error_declarator)
11251                 break;
11252
11253               goto handle_declarator;
11254             }
11255           /* Otherwise, we must be done.  */
11256           else
11257             break;
11258         }
11259       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11260                && token->type == CPP_OPEN_SQUARE)
11261         {
11262           /* Parse an array-declarator.  */
11263           tree bounds;
11264
11265           if (ctor_dtor_or_conv_p)
11266             *ctor_dtor_or_conv_p = 0;
11267
11268           first = false;
11269           parser->default_arg_ok_p = false;
11270           parser->in_declarator_p = true;
11271           /* Consume the `['.  */
11272           cp_lexer_consume_token (parser->lexer);
11273           /* Peek at the next token.  */
11274           token = cp_lexer_peek_token (parser->lexer);
11275           /* If the next token is `]', then there is no
11276              constant-expression.  */
11277           if (token->type != CPP_CLOSE_SQUARE)
11278             {
11279               bool non_constant_p;
11280
11281               bounds
11282                 = cp_parser_constant_expression (parser,
11283                                                  /*allow_non_constant=*/true,
11284                                                  &non_constant_p);
11285               if (!non_constant_p)
11286                 bounds = fold_non_dependent_expr (bounds);
11287               /* Normally, the array bound must be an integral constant
11288                  expression.  However, as an extension, we allow VLAs
11289                  in function scopes.  */
11290               else if (!at_function_scope_p ())
11291                 {
11292                   error ("array bound is not an integer constant");
11293                   bounds = error_mark_node;
11294                 }
11295             }
11296           else
11297             bounds = NULL_TREE;
11298           /* Look for the closing `]'.  */
11299           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11300             {
11301               declarator = cp_error_declarator;
11302               break;
11303             }
11304
11305           declarator = make_array_declarator (declarator, bounds);
11306         }
11307       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11308         {
11309           tree qualifying_scope;
11310           tree unqualified_name;
11311
11312           /* Parse a declarator-id */
11313           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11314             cp_parser_parse_tentatively (parser);
11315           unqualified_name = cp_parser_declarator_id (parser);
11316           qualifying_scope = parser->scope;
11317           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11318             {
11319               if (!cp_parser_parse_definitely (parser))
11320                 unqualified_name = error_mark_node;
11321               else if (qualifying_scope
11322                        || (TREE_CODE (unqualified_name)
11323                            != IDENTIFIER_NODE))
11324                 {
11325                   cp_parser_error (parser, "expected unqualified-id");
11326                   unqualified_name = error_mark_node;
11327                 }
11328             }
11329
11330           if (unqualified_name == error_mark_node)
11331             {
11332               declarator = cp_error_declarator;
11333               break;
11334             }
11335
11336           if (qualifying_scope && at_namespace_scope_p ()
11337               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11338             {
11339               /* In the declaration of a member of a template class
11340                  outside of the class itself, the SCOPE will sometimes
11341                  be a TYPENAME_TYPE.  For example, given:
11342
11343                  template <typename T>
11344                  int S<T>::R::i = 3;
11345
11346                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11347                  this context, we must resolve S<T>::R to an ordinary
11348                  type, rather than a typename type.
11349
11350                  The reason we normally avoid resolving TYPENAME_TYPEs
11351                  is that a specialization of `S' might render
11352                  `S<T>::R' not a type.  However, if `S' is
11353                  specialized, then this `i' will not be used, so there
11354                  is no harm in resolving the types here.  */
11355               tree type;
11356
11357               /* Resolve the TYPENAME_TYPE.  */
11358               type = resolve_typename_type (qualifying_scope,
11359                                             /*only_current_p=*/false);
11360               /* If that failed, the declarator is invalid.  */
11361               if (type == error_mark_node)
11362                 error ("%<%T::%D%> is not a type",
11363                        TYPE_CONTEXT (qualifying_scope),
11364                        TYPE_IDENTIFIER (qualifying_scope));
11365               qualifying_scope = type;
11366             }
11367
11368           declarator = make_id_declarator (qualifying_scope,
11369                                            unqualified_name);
11370           declarator->id_loc = token->location;
11371           if (unqualified_name)
11372             {
11373               tree class_type;
11374
11375               if (qualifying_scope
11376                   && CLASS_TYPE_P (qualifying_scope))
11377                 class_type = qualifying_scope;
11378               else
11379                 class_type = current_class_type;
11380
11381               if (class_type)
11382                 {
11383                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11384                     declarator->u.id.sfk = sfk_destructor;
11385                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11386                     declarator->u.id.sfk = sfk_conversion;
11387                   else if (/* There's no way to declare a constructor
11388                               for an anonymous type, even if the type
11389                               got a name for linkage purposes.  */
11390                            !TYPE_WAS_ANONYMOUS (class_type)
11391                            && (constructor_name_p (unqualified_name,
11392                                                    class_type)
11393                                || (TREE_CODE (unqualified_name) == TYPE_DECL
11394                                    && (same_type_p
11395                                        (TREE_TYPE (unqualified_name),
11396                                         class_type)))))
11397                     declarator->u.id.sfk = sfk_constructor;
11398
11399                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11400                     *ctor_dtor_or_conv_p = -1;
11401                   if (qualifying_scope
11402                       && TREE_CODE (unqualified_name) == TYPE_DECL
11403                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11404                     {
11405                       error ("invalid use of constructor as a template");
11406                       inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11407                               "the constructor in a qualified name",
11408                               class_type,
11409                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11410                               class_type, class_type);
11411                     }
11412                 }
11413             }
11414
11415         handle_declarator:;
11416           scope = get_scope_of_declarator (declarator);
11417           if (scope)
11418             /* Any names that appear after the declarator-id for a
11419                member are looked up in the containing scope.  */
11420             pushed_scope = push_scope (scope);
11421           parser->in_declarator_p = true;
11422           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11423               || (declarator && declarator->kind == cdk_id))
11424             /* Default args are only allowed on function
11425                declarations.  */
11426             parser->default_arg_ok_p = saved_default_arg_ok_p;
11427           else
11428             parser->default_arg_ok_p = false;
11429
11430           first = false;
11431         }
11432       /* We're done.  */
11433       else
11434         break;
11435     }
11436
11437   /* For an abstract declarator, we might wind up with nothing at this
11438      point.  That's an error; the declarator is not optional.  */
11439   if (!declarator)
11440     cp_parser_error (parser, "expected declarator");
11441
11442   /* If we entered a scope, we must exit it now.  */
11443   if (pushed_scope)
11444     pop_scope (pushed_scope);
11445
11446   parser->default_arg_ok_p = saved_default_arg_ok_p;
11447   parser->in_declarator_p = saved_in_declarator_p;
11448
11449   return declarator;
11450 }
11451
11452 /* Parse a ptr-operator.
11453
11454    ptr-operator:
11455      * cv-qualifier-seq [opt]
11456      &
11457      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11458
11459    GNU Extension:
11460
11461    ptr-operator:
11462      & cv-qualifier-seq [opt]
11463
11464    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11465    Returns ADDR_EXPR if a reference was used.  In the case of a
11466    pointer-to-member, *TYPE is filled in with the TYPE containing the
11467    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11468    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11469    ERROR_MARK if an error occurred.  */
11470
11471 static enum tree_code
11472 cp_parser_ptr_operator (cp_parser* parser,
11473                         tree* type,
11474                         cp_cv_quals *cv_quals)
11475 {
11476   enum tree_code code = ERROR_MARK;
11477   cp_token *token;
11478
11479   /* Assume that it's not a pointer-to-member.  */
11480   *type = NULL_TREE;
11481   /* And that there are no cv-qualifiers.  */
11482   *cv_quals = TYPE_UNQUALIFIED;
11483
11484   /* Peek at the next token.  */
11485   token = cp_lexer_peek_token (parser->lexer);
11486   /* If it's a `*' or `&' we have a pointer or reference.  */
11487   if (token->type == CPP_MULT || token->type == CPP_AND)
11488     {
11489       /* Remember which ptr-operator we were processing.  */
11490       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11491
11492       /* Consume the `*' or `&'.  */
11493       cp_lexer_consume_token (parser->lexer);
11494
11495       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11496          `&', if we are allowing GNU extensions.  (The only qualifier
11497          that can legally appear after `&' is `restrict', but that is
11498          enforced during semantic analysis.  */
11499       if (code == INDIRECT_REF
11500           || cp_parser_allow_gnu_extensions_p (parser))
11501         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11502     }
11503   else
11504     {
11505       /* Try the pointer-to-member case.  */
11506       cp_parser_parse_tentatively (parser);
11507       /* Look for the optional `::' operator.  */
11508       cp_parser_global_scope_opt (parser,
11509                                   /*current_scope_valid_p=*/false);
11510       /* Look for the nested-name specifier.  */
11511       cp_parser_nested_name_specifier (parser,
11512                                        /*typename_keyword_p=*/false,
11513                                        /*check_dependency_p=*/true,
11514                                        /*type_p=*/false,
11515                                        /*is_declaration=*/false);
11516       /* If we found it, and the next token is a `*', then we are
11517          indeed looking at a pointer-to-member operator.  */
11518       if (!cp_parser_error_occurred (parser)
11519           && cp_parser_require (parser, CPP_MULT, "`*'"))
11520         {
11521           /* The type of which the member is a member is given by the
11522              current SCOPE.  */
11523           *type = parser->scope;
11524           /* The next name will not be qualified.  */
11525           parser->scope = NULL_TREE;
11526           parser->qualifying_scope = NULL_TREE;
11527           parser->object_scope = NULL_TREE;
11528           /* Indicate that the `*' operator was used.  */
11529           code = INDIRECT_REF;
11530           /* Look for the optional cv-qualifier-seq.  */
11531           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11532         }
11533       /* If that didn't work we don't have a ptr-operator.  */
11534       if (!cp_parser_parse_definitely (parser))
11535         cp_parser_error (parser, "expected ptr-operator");
11536     }
11537
11538   return code;
11539 }
11540
11541 /* Parse an (optional) cv-qualifier-seq.
11542
11543    cv-qualifier-seq:
11544      cv-qualifier cv-qualifier-seq [opt]
11545
11546    cv-qualifier:
11547      const
11548      volatile
11549
11550    GNU Extension:
11551
11552    cv-qualifier:
11553      __restrict__
11554
11555    Returns a bitmask representing the cv-qualifiers.  */
11556
11557 static cp_cv_quals
11558 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11559 {
11560   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11561
11562   while (true)
11563     {
11564       cp_token *token;
11565       cp_cv_quals cv_qualifier;
11566
11567       /* Peek at the next token.  */
11568       token = cp_lexer_peek_token (parser->lexer);
11569       /* See if it's a cv-qualifier.  */
11570       switch (token->keyword)
11571         {
11572         case RID_CONST:
11573           cv_qualifier = TYPE_QUAL_CONST;
11574           break;
11575
11576         case RID_VOLATILE:
11577           cv_qualifier = TYPE_QUAL_VOLATILE;
11578           break;
11579
11580         case RID_RESTRICT:
11581           cv_qualifier = TYPE_QUAL_RESTRICT;
11582           break;
11583
11584         default:
11585           cv_qualifier = TYPE_UNQUALIFIED;
11586           break;
11587         }
11588
11589       if (!cv_qualifier)
11590         break;
11591
11592       if (cv_quals & cv_qualifier)
11593         {
11594           error ("duplicate cv-qualifier");
11595           cp_lexer_purge_token (parser->lexer);
11596         }
11597       else
11598         {
11599           cp_lexer_consume_token (parser->lexer);
11600           cv_quals |= cv_qualifier;
11601         }
11602     }
11603
11604   return cv_quals;
11605 }
11606
11607 /* Parse a declarator-id.
11608
11609    declarator-id:
11610      id-expression
11611      :: [opt] nested-name-specifier [opt] type-name
11612
11613    In the `id-expression' case, the value returned is as for
11614    cp_parser_id_expression if the id-expression was an unqualified-id.
11615    If the id-expression was a qualified-id, then a SCOPE_REF is
11616    returned.  The first operand is the scope (either a NAMESPACE_DECL
11617    or TREE_TYPE), but the second is still just a representation of an
11618    unqualified-id.  */
11619
11620 static tree
11621 cp_parser_declarator_id (cp_parser* parser)
11622 {
11623   /* The expression must be an id-expression.  Assume that qualified
11624      names are the names of types so that:
11625
11626        template <class T>
11627        int S<T>::R::i = 3;
11628
11629      will work; we must treat `S<T>::R' as the name of a type.
11630      Similarly, assume that qualified names are templates, where
11631      required, so that:
11632
11633        template <class T>
11634        int S<T>::R<T>::i = 3;
11635
11636      will work, too.  */
11637   return cp_parser_id_expression (parser,
11638                                   /*template_keyword_p=*/false,
11639                                   /*check_dependency_p=*/false,
11640                                   /*template_p=*/NULL,
11641                                   /*declarator_p=*/true);
11642 }
11643
11644 /* Parse a type-id.
11645
11646    type-id:
11647      type-specifier-seq abstract-declarator [opt]
11648
11649    Returns the TYPE specified.  */
11650
11651 static tree
11652 cp_parser_type_id (cp_parser* parser)
11653 {
11654   cp_decl_specifier_seq type_specifier_seq;
11655   cp_declarator *abstract_declarator;
11656
11657   /* Parse the type-specifier-seq.  */
11658   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11659                                 &type_specifier_seq);
11660   if (type_specifier_seq.type == error_mark_node)
11661     return error_mark_node;
11662
11663   /* There might or might not be an abstract declarator.  */
11664   cp_parser_parse_tentatively (parser);
11665   /* Look for the declarator.  */
11666   abstract_declarator
11667     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11668                             /*parenthesized_p=*/NULL,
11669                             /*member_p=*/false);
11670   /* Check to see if there really was a declarator.  */
11671   if (!cp_parser_parse_definitely (parser))
11672     abstract_declarator = NULL;
11673
11674   return groktypename (&type_specifier_seq, abstract_declarator);
11675 }
11676
11677 /* Parse a type-specifier-seq.
11678
11679    type-specifier-seq:
11680      type-specifier type-specifier-seq [opt]
11681
11682    GNU extension:
11683
11684    type-specifier-seq:
11685      attributes type-specifier-seq [opt]
11686
11687    If IS_CONDITION is true, we are at the start of a "condition",
11688    e.g., we've just seen "if (".
11689
11690    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11691
11692 static void
11693 cp_parser_type_specifier_seq (cp_parser* parser,
11694                               bool is_condition,
11695                               cp_decl_specifier_seq *type_specifier_seq)
11696 {
11697   bool seen_type_specifier = false;
11698   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11699
11700   /* Clear the TYPE_SPECIFIER_SEQ.  */
11701   clear_decl_specs (type_specifier_seq);
11702
11703   /* Parse the type-specifiers and attributes.  */
11704   while (true)
11705     {
11706       tree type_specifier;
11707       bool is_cv_qualifier;
11708
11709       /* Check for attributes first.  */
11710       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11711         {
11712           type_specifier_seq->attributes =
11713             chainon (type_specifier_seq->attributes,
11714                      cp_parser_attributes_opt (parser));
11715           continue;
11716         }
11717
11718       /* Look for the type-specifier.  */
11719       type_specifier = cp_parser_type_specifier (parser,
11720                                                  flags,
11721                                                  type_specifier_seq,
11722                                                  /*is_declaration=*/false,
11723                                                  NULL,
11724                                                  &is_cv_qualifier);
11725       if (!type_specifier)
11726         {
11727           /* If the first type-specifier could not be found, this is not a
11728              type-specifier-seq at all.  */
11729           if (!seen_type_specifier)
11730             {
11731               cp_parser_error (parser, "expected type-specifier");
11732               type_specifier_seq->type = error_mark_node;
11733               return;
11734             }
11735           /* If subsequent type-specifiers could not be found, the
11736              type-specifier-seq is complete.  */
11737           break;
11738         }
11739
11740       seen_type_specifier = true;
11741       /* The standard says that a condition can be:
11742
11743             type-specifier-seq declarator = assignment-expression
11744
11745          However, given:
11746
11747            struct S {};
11748            if (int S = ...)
11749
11750          we should treat the "S" as a declarator, not as a
11751          type-specifier.  The standard doesn't say that explicitly for
11752          type-specifier-seq, but it does say that for
11753          decl-specifier-seq in an ordinary declaration.  Perhaps it
11754          would be clearer just to allow a decl-specifier-seq here, and
11755          then add a semantic restriction that if any decl-specifiers
11756          that are not type-specifiers appear, the program is invalid.  */
11757       if (is_condition && !is_cv_qualifier)
11758         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11759     }
11760
11761   return;
11762 }
11763
11764 /* Parse a parameter-declaration-clause.
11765
11766    parameter-declaration-clause:
11767      parameter-declaration-list [opt] ... [opt]
11768      parameter-declaration-list , ...
11769
11770    Returns a representation for the parameter declarations.  A return
11771    value of NULL indicates a parameter-declaration-clause consisting
11772    only of an ellipsis.  */
11773
11774 static cp_parameter_declarator *
11775 cp_parser_parameter_declaration_clause (cp_parser* parser)
11776 {
11777   cp_parameter_declarator *parameters;
11778   cp_token *token;
11779   bool ellipsis_p;
11780   bool is_error;
11781
11782   /* Peek at the next token.  */
11783   token = cp_lexer_peek_token (parser->lexer);
11784   /* Check for trivial parameter-declaration-clauses.  */
11785   if (token->type == CPP_ELLIPSIS)
11786     {
11787       /* Consume the `...' token.  */
11788       cp_lexer_consume_token (parser->lexer);
11789       return NULL;
11790     }
11791   else if (token->type == CPP_CLOSE_PAREN)
11792     /* There are no parameters.  */
11793     {
11794 #ifndef NO_IMPLICIT_EXTERN_C
11795       if (in_system_header && current_class_type == NULL
11796           && current_lang_name == lang_name_c)
11797         return NULL;
11798       else
11799 #endif
11800         return no_parameters;
11801     }
11802   /* Check for `(void)', too, which is a special case.  */
11803   else if (token->keyword == RID_VOID
11804            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11805                == CPP_CLOSE_PAREN))
11806     {
11807       /* Consume the `void' token.  */
11808       cp_lexer_consume_token (parser->lexer);
11809       /* There are no parameters.  */
11810       return no_parameters;
11811     }
11812
11813   /* Parse the parameter-declaration-list.  */
11814   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11815   /* If a parse error occurred while parsing the
11816      parameter-declaration-list, then the entire
11817      parameter-declaration-clause is erroneous.  */
11818   if (is_error)
11819     return NULL;
11820
11821   /* Peek at the next token.  */
11822   token = cp_lexer_peek_token (parser->lexer);
11823   /* If it's a `,', the clause should terminate with an ellipsis.  */
11824   if (token->type == CPP_COMMA)
11825     {
11826       /* Consume the `,'.  */
11827       cp_lexer_consume_token (parser->lexer);
11828       /* Expect an ellipsis.  */
11829       ellipsis_p
11830         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11831     }
11832   /* It might also be `...' if the optional trailing `,' was
11833      omitted.  */
11834   else if (token->type == CPP_ELLIPSIS)
11835     {
11836       /* Consume the `...' token.  */
11837       cp_lexer_consume_token (parser->lexer);
11838       /* And remember that we saw it.  */
11839       ellipsis_p = true;
11840     }
11841   else
11842     ellipsis_p = false;
11843
11844   /* Finish the parameter list.  */
11845   if (parameters && ellipsis_p)
11846     parameters->ellipsis_p = true;
11847
11848   return parameters;
11849 }
11850
11851 /* Parse a parameter-declaration-list.
11852
11853    parameter-declaration-list:
11854      parameter-declaration
11855      parameter-declaration-list , parameter-declaration
11856
11857    Returns a representation of the parameter-declaration-list, as for
11858    cp_parser_parameter_declaration_clause.  However, the
11859    `void_list_node' is never appended to the list.  Upon return,
11860    *IS_ERROR will be true iff an error occurred.  */
11861
11862 static cp_parameter_declarator *
11863 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11864 {
11865   cp_parameter_declarator *parameters = NULL;
11866   cp_parameter_declarator **tail = &parameters;
11867
11868   /* Assume all will go well.  */
11869   *is_error = false;
11870
11871   /* Look for more parameters.  */
11872   while (true)
11873     {
11874       cp_parameter_declarator *parameter;
11875       bool parenthesized_p;
11876       /* Parse the parameter.  */
11877       parameter
11878         = cp_parser_parameter_declaration (parser,
11879                                            /*template_parm_p=*/false,
11880                                            &parenthesized_p);
11881
11882       /* If a parse error occurred parsing the parameter declaration,
11883          then the entire parameter-declaration-list is erroneous.  */
11884       if (!parameter)
11885         {
11886           *is_error = true;
11887           parameters = NULL;
11888           break;
11889         }
11890       /* Add the new parameter to the list.  */
11891       *tail = parameter;
11892       tail = &parameter->next;
11893
11894       /* Peek at the next token.  */
11895       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11896           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11897           /* These are for Objective-C++ */
11898           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11899           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11900         /* The parameter-declaration-list is complete.  */
11901         break;
11902       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11903         {
11904           cp_token *token;
11905
11906           /* Peek at the next token.  */
11907           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11908           /* If it's an ellipsis, then the list is complete.  */
11909           if (token->type == CPP_ELLIPSIS)
11910             break;
11911           /* Otherwise, there must be more parameters.  Consume the
11912              `,'.  */
11913           cp_lexer_consume_token (parser->lexer);
11914           /* When parsing something like:
11915
11916                 int i(float f, double d)
11917
11918              we can tell after seeing the declaration for "f" that we
11919              are not looking at an initialization of a variable "i",
11920              but rather at the declaration of a function "i".
11921
11922              Due to the fact that the parsing of template arguments
11923              (as specified to a template-id) requires backtracking we
11924              cannot use this technique when inside a template argument
11925              list.  */
11926           if (!parser->in_template_argument_list_p
11927               && !parser->in_type_id_in_expr_p
11928               && cp_parser_uncommitted_to_tentative_parse_p (parser)
11929               /* However, a parameter-declaration of the form
11930                  "foat(f)" (which is a valid declaration of a
11931                  parameter "f") can also be interpreted as an
11932                  expression (the conversion of "f" to "float").  */
11933               && !parenthesized_p)
11934             cp_parser_commit_to_tentative_parse (parser);
11935         }
11936       else
11937         {
11938           cp_parser_error (parser, "expected %<,%> or %<...%>");
11939           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11940             cp_parser_skip_to_closing_parenthesis (parser,
11941                                                    /*recovering=*/true,
11942                                                    /*or_comma=*/false,
11943                                                    /*consume_paren=*/false);
11944           break;
11945         }
11946     }
11947
11948   return parameters;
11949 }
11950
11951 /* Parse a parameter declaration.
11952
11953    parameter-declaration:
11954      decl-specifier-seq declarator
11955      decl-specifier-seq declarator = assignment-expression
11956      decl-specifier-seq abstract-declarator [opt]
11957      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11958
11959    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11960    declares a template parameter.  (In that case, a non-nested `>'
11961    token encountered during the parsing of the assignment-expression
11962    is not interpreted as a greater-than operator.)
11963
11964    Returns a representation of the parameter, or NULL if an error
11965    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11966    true iff the declarator is of the form "(p)".  */
11967
11968 static cp_parameter_declarator *
11969 cp_parser_parameter_declaration (cp_parser *parser,
11970                                  bool template_parm_p,
11971                                  bool *parenthesized_p)
11972 {
11973   int declares_class_or_enum;
11974   bool greater_than_is_operator_p;
11975   cp_decl_specifier_seq decl_specifiers;
11976   cp_declarator *declarator;
11977   tree default_argument;
11978   cp_token *token;
11979   const char *saved_message;
11980
11981   /* In a template parameter, `>' is not an operator.
11982
11983      [temp.param]
11984
11985      When parsing a default template-argument for a non-type
11986      template-parameter, the first non-nested `>' is taken as the end
11987      of the template parameter-list rather than a greater-than
11988      operator.  */
11989   greater_than_is_operator_p = !template_parm_p;
11990
11991   /* Type definitions may not appear in parameter types.  */
11992   saved_message = parser->type_definition_forbidden_message;
11993   parser->type_definition_forbidden_message
11994     = "types may not be defined in parameter types";
11995
11996   /* Parse the declaration-specifiers.  */
11997   cp_parser_decl_specifier_seq (parser,
11998                                 CP_PARSER_FLAGS_NONE,
11999                                 &decl_specifiers,
12000                                 &declares_class_or_enum);
12001   /* If an error occurred, there's no reason to attempt to parse the
12002      rest of the declaration.  */
12003   if (cp_parser_error_occurred (parser))
12004     {
12005       parser->type_definition_forbidden_message = saved_message;
12006       return NULL;
12007     }
12008
12009   /* Peek at the next token.  */
12010   token = cp_lexer_peek_token (parser->lexer);
12011   /* If the next token is a `)', `,', `=', `>', or `...', then there
12012      is no declarator.  */
12013   if (token->type == CPP_CLOSE_PAREN
12014       || token->type == CPP_COMMA
12015       || token->type == CPP_EQ
12016       || token->type == CPP_ELLIPSIS
12017       || token->type == CPP_GREATER)
12018     {
12019       declarator = NULL;
12020       if (parenthesized_p)
12021         *parenthesized_p = false;
12022     }
12023   /* Otherwise, there should be a declarator.  */
12024   else
12025     {
12026       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12027       parser->default_arg_ok_p = false;
12028
12029       /* After seeing a decl-specifier-seq, if the next token is not a
12030          "(", there is no possibility that the code is a valid
12031          expression.  Therefore, if parsing tentatively, we commit at
12032          this point.  */
12033       if (!parser->in_template_argument_list_p
12034           /* In an expression context, having seen:
12035
12036                (int((char ...
12037
12038              we cannot be sure whether we are looking at a
12039              function-type (taking a "char" as a parameter) or a cast
12040              of some object of type "char" to "int".  */
12041           && !parser->in_type_id_in_expr_p
12042           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12043           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12044         cp_parser_commit_to_tentative_parse (parser);
12045       /* Parse the declarator.  */
12046       declarator = cp_parser_declarator (parser,
12047                                          CP_PARSER_DECLARATOR_EITHER,
12048                                          /*ctor_dtor_or_conv_p=*/NULL,
12049                                          parenthesized_p,
12050                                          /*member_p=*/false);
12051       parser->default_arg_ok_p = saved_default_arg_ok_p;
12052       /* After the declarator, allow more attributes.  */
12053       decl_specifiers.attributes
12054         = chainon (decl_specifiers.attributes,
12055                    cp_parser_attributes_opt (parser));
12056     }
12057
12058   /* The restriction on defining new types applies only to the type
12059      of the parameter, not to the default argument.  */
12060   parser->type_definition_forbidden_message = saved_message;
12061
12062   /* If the next token is `=', then process a default argument.  */
12063   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12064     {
12065       bool saved_greater_than_is_operator_p;
12066       /* Consume the `='.  */
12067       cp_lexer_consume_token (parser->lexer);
12068
12069       /* If we are defining a class, then the tokens that make up the
12070          default argument must be saved and processed later.  */
12071       if (!template_parm_p && at_class_scope_p ()
12072           && TYPE_BEING_DEFINED (current_class_type))
12073         {
12074           unsigned depth = 0;
12075           cp_token *first_token;
12076           cp_token *token;
12077
12078           /* Add tokens until we have processed the entire default
12079              argument.  We add the range [first_token, token).  */
12080           first_token = cp_lexer_peek_token (parser->lexer);
12081           while (true)
12082             {
12083               bool done = false;
12084
12085               /* Peek at the next token.  */
12086               token = cp_lexer_peek_token (parser->lexer);
12087               /* What we do depends on what token we have.  */
12088               switch (token->type)
12089                 {
12090                   /* In valid code, a default argument must be
12091                      immediately followed by a `,' `)', or `...'.  */
12092                 case CPP_COMMA:
12093                 case CPP_CLOSE_PAREN:
12094                 case CPP_ELLIPSIS:
12095                   /* If we run into a non-nested `;', `}', or `]',
12096                      then the code is invalid -- but the default
12097                      argument is certainly over.  */
12098                 case CPP_SEMICOLON:
12099                 case CPP_CLOSE_BRACE:
12100                 case CPP_CLOSE_SQUARE:
12101                   if (depth == 0)
12102                     done = true;
12103                   /* Update DEPTH, if necessary.  */
12104                   else if (token->type == CPP_CLOSE_PAREN
12105                            || token->type == CPP_CLOSE_BRACE
12106                            || token->type == CPP_CLOSE_SQUARE)
12107                     --depth;
12108                   break;
12109
12110                 case CPP_OPEN_PAREN:
12111                 case CPP_OPEN_SQUARE:
12112                 case CPP_OPEN_BRACE:
12113                   ++depth;
12114                   break;
12115
12116                 case CPP_GREATER:
12117                   /* If we see a non-nested `>', and `>' is not an
12118                      operator, then it marks the end of the default
12119                      argument.  */
12120                   if (!depth && !greater_than_is_operator_p)
12121                     done = true;
12122                   break;
12123
12124                   /* If we run out of tokens, issue an error message.  */
12125                 case CPP_EOF:
12126                   error ("file ends in default argument");
12127                   done = true;
12128                   break;
12129
12130                 case CPP_NAME:
12131                 case CPP_SCOPE:
12132                   /* In these cases, we should look for template-ids.
12133                      For example, if the default argument is
12134                      `X<int, double>()', we need to do name lookup to
12135                      figure out whether or not `X' is a template; if
12136                      so, the `,' does not end the default argument.
12137
12138                      That is not yet done.  */
12139                   break;
12140
12141                 default:
12142                   break;
12143                 }
12144
12145               /* If we've reached the end, stop.  */
12146               if (done)
12147                 break;
12148
12149               /* Add the token to the token block.  */
12150               token = cp_lexer_consume_token (parser->lexer);
12151             }
12152
12153           /* Create a DEFAULT_ARG to represented the unparsed default
12154              argument.  */
12155           default_argument = make_node (DEFAULT_ARG);
12156           DEFARG_TOKENS (default_argument)
12157             = cp_token_cache_new (first_token, token);
12158           DEFARG_INSTANTIATIONS (default_argument) = NULL;
12159         }
12160       /* Outside of a class definition, we can just parse the
12161          assignment-expression.  */
12162       else
12163         {
12164           bool saved_local_variables_forbidden_p;
12165
12166           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12167              set correctly.  */
12168           saved_greater_than_is_operator_p
12169             = parser->greater_than_is_operator_p;
12170           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12171           /* Local variable names (and the `this' keyword) may not
12172              appear in a default argument.  */
12173           saved_local_variables_forbidden_p
12174             = parser->local_variables_forbidden_p;
12175           parser->local_variables_forbidden_p = true;
12176           /* Parse the assignment-expression.  */
12177           default_argument
12178             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12179           /* Restore saved state.  */
12180           parser->greater_than_is_operator_p
12181             = saved_greater_than_is_operator_p;
12182           parser->local_variables_forbidden_p
12183             = saved_local_variables_forbidden_p;
12184         }
12185       if (!parser->default_arg_ok_p)
12186         {
12187           if (!flag_pedantic_errors)
12188             warning (0, "deprecated use of default argument for parameter of non-function");
12189           else
12190             {
12191               error ("default arguments are only permitted for function parameters");
12192               default_argument = NULL_TREE;
12193             }
12194         }
12195     }
12196   else
12197     default_argument = NULL_TREE;
12198
12199   return make_parameter_declarator (&decl_specifiers,
12200                                     declarator,
12201                                     default_argument);
12202 }
12203
12204 /* Parse a function-body.
12205
12206    function-body:
12207      compound_statement  */
12208
12209 static void
12210 cp_parser_function_body (cp_parser *parser)
12211 {
12212   cp_parser_compound_statement (parser, NULL, false);
12213 }
12214
12215 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12216    true if a ctor-initializer was present.  */
12217
12218 static bool
12219 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12220 {
12221   tree body;
12222   bool ctor_initializer_p;
12223
12224   /* Begin the function body.  */
12225   body = begin_function_body ();
12226   /* Parse the optional ctor-initializer.  */
12227   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12228   /* Parse the function-body.  */
12229   cp_parser_function_body (parser);
12230   /* Finish the function body.  */
12231   finish_function_body (body);
12232
12233   return ctor_initializer_p;
12234 }
12235
12236 /* Parse an initializer.
12237
12238    initializer:
12239      = initializer-clause
12240      ( expression-list )
12241
12242    Returns an expression representing the initializer.  If no
12243    initializer is present, NULL_TREE is returned.
12244
12245    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12246    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12247    set to FALSE if there is no initializer present.  If there is an
12248    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12249    is set to true; otherwise it is set to false.  */
12250
12251 static tree
12252 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12253                        bool* non_constant_p)
12254 {
12255   cp_token *token;
12256   tree init;
12257
12258   /* Peek at the next token.  */
12259   token = cp_lexer_peek_token (parser->lexer);
12260
12261   /* Let our caller know whether or not this initializer was
12262      parenthesized.  */
12263   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12264   /* Assume that the initializer is constant.  */
12265   *non_constant_p = false;
12266
12267   if (token->type == CPP_EQ)
12268     {
12269       /* Consume the `='.  */
12270       cp_lexer_consume_token (parser->lexer);
12271       /* Parse the initializer-clause.  */
12272       init = cp_parser_initializer_clause (parser, non_constant_p);
12273     }
12274   else if (token->type == CPP_OPEN_PAREN)
12275     init = cp_parser_parenthesized_expression_list (parser, false,
12276                                                     /*cast_p=*/false,
12277                                                     non_constant_p);
12278   else
12279     {
12280       /* Anything else is an error.  */
12281       cp_parser_error (parser, "expected initializer");
12282       init = error_mark_node;
12283     }
12284
12285   return init;
12286 }
12287
12288 /* Parse an initializer-clause.
12289
12290    initializer-clause:
12291      assignment-expression
12292      { initializer-list , [opt] }
12293      { }
12294
12295    Returns an expression representing the initializer.
12296
12297    If the `assignment-expression' production is used the value
12298    returned is simply a representation for the expression.
12299
12300    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12301    the elements of the initializer-list (or NULL, if the last
12302    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12303    NULL_TREE.  There is no way to detect whether or not the optional
12304    trailing `,' was provided.  NON_CONSTANT_P is as for
12305    cp_parser_initializer.  */
12306
12307 static tree
12308 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12309 {
12310   tree initializer;
12311
12312   /* Assume the expression is constant.  */
12313   *non_constant_p = false;
12314
12315   /* If it is not a `{', then we are looking at an
12316      assignment-expression.  */
12317   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12318     {
12319       initializer
12320         = cp_parser_constant_expression (parser,
12321                                         /*allow_non_constant_p=*/true,
12322                                         non_constant_p);
12323       if (!*non_constant_p)
12324         initializer = fold_non_dependent_expr (initializer);
12325     }
12326   else
12327     {
12328       /* Consume the `{' token.  */
12329       cp_lexer_consume_token (parser->lexer);
12330       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12331       initializer = make_node (CONSTRUCTOR);
12332       /* If it's not a `}', then there is a non-trivial initializer.  */
12333       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12334         {
12335           /* Parse the initializer list.  */
12336           CONSTRUCTOR_ELTS (initializer)
12337             = cp_parser_initializer_list (parser, non_constant_p);
12338           /* A trailing `,' token is allowed.  */
12339           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12340             cp_lexer_consume_token (parser->lexer);
12341         }
12342       /* Now, there should be a trailing `}'.  */
12343       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12344     }
12345
12346   return initializer;
12347 }
12348
12349 /* Parse an initializer-list.
12350
12351    initializer-list:
12352      initializer-clause
12353      initializer-list , initializer-clause
12354
12355    GNU Extension:
12356
12357    initializer-list:
12358      identifier : initializer-clause
12359      initializer-list, identifier : initializer-clause
12360
12361    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12362    for the initializer.  If the INDEX of the elt is non-NULL, it is the
12363    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12364    as for cp_parser_initializer.  */
12365
12366 static VEC(constructor_elt,gc) *
12367 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12368 {
12369   VEC(constructor_elt,gc) *v = NULL;
12370
12371   /* Assume all of the expressions are constant.  */
12372   *non_constant_p = false;
12373
12374   /* Parse the rest of the list.  */
12375   while (true)
12376     {
12377       cp_token *token;
12378       tree identifier;
12379       tree initializer;
12380       bool clause_non_constant_p;
12381
12382       /* If the next token is an identifier and the following one is a
12383          colon, we are looking at the GNU designated-initializer
12384          syntax.  */
12385       if (cp_parser_allow_gnu_extensions_p (parser)
12386           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12387           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12388         {
12389           /* Consume the identifier.  */
12390           identifier = cp_lexer_consume_token (parser->lexer)->value;
12391           /* Consume the `:'.  */
12392           cp_lexer_consume_token (parser->lexer);
12393         }
12394       else
12395         identifier = NULL_TREE;
12396
12397       /* Parse the initializer.  */
12398       initializer = cp_parser_initializer_clause (parser,
12399                                                   &clause_non_constant_p);
12400       /* If any clause is non-constant, so is the entire initializer.  */
12401       if (clause_non_constant_p)
12402         *non_constant_p = true;
12403
12404       /* Add it to the vector.  */
12405       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12406
12407       /* If the next token is not a comma, we have reached the end of
12408          the list.  */
12409       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12410         break;
12411
12412       /* Peek at the next token.  */
12413       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12414       /* If the next token is a `}', then we're still done.  An
12415          initializer-clause can have a trailing `,' after the
12416          initializer-list and before the closing `}'.  */
12417       if (token->type == CPP_CLOSE_BRACE)
12418         break;
12419
12420       /* Consume the `,' token.  */
12421       cp_lexer_consume_token (parser->lexer);
12422     }
12423
12424   return v;
12425 }
12426
12427 /* Classes [gram.class] */
12428
12429 /* Parse a class-name.
12430
12431    class-name:
12432      identifier
12433      template-id
12434
12435    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12436    to indicate that names looked up in dependent types should be
12437    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12438    keyword has been used to indicate that the name that appears next
12439    is a template.  TAG_TYPE indicates the explicit tag given before
12440    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12441    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12442    is the class being defined in a class-head.
12443
12444    Returns the TYPE_DECL representing the class.  */
12445
12446 static tree
12447 cp_parser_class_name (cp_parser *parser,
12448                       bool typename_keyword_p,
12449                       bool template_keyword_p,
12450                       enum tag_types tag_type,
12451                       bool check_dependency_p,
12452                       bool class_head_p,
12453                       bool is_declaration)
12454 {
12455   tree decl;
12456   tree scope;
12457   bool typename_p;
12458   cp_token *token;
12459
12460   /* All class-names start with an identifier.  */
12461   token = cp_lexer_peek_token (parser->lexer);
12462   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12463     {
12464       cp_parser_error (parser, "expected class-name");
12465       return error_mark_node;
12466     }
12467
12468   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12469      to a template-id, so we save it here.  */
12470   scope = parser->scope;
12471   if (scope == error_mark_node)
12472     return error_mark_node;
12473
12474   /* Any name names a type if we're following the `typename' keyword
12475      in a qualified name where the enclosing scope is type-dependent.  */
12476   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12477                 && dependent_type_p (scope));
12478   /* Handle the common case (an identifier, but not a template-id)
12479      efficiently.  */
12480   if (token->type == CPP_NAME
12481       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12482     {
12483       tree identifier;
12484
12485       /* Look for the identifier.  */
12486       identifier = cp_parser_identifier (parser);
12487       /* If the next token isn't an identifier, we are certainly not
12488          looking at a class-name.  */
12489       if (identifier == error_mark_node)
12490         decl = error_mark_node;
12491       /* If we know this is a type-name, there's no need to look it
12492          up.  */
12493       else if (typename_p)
12494         decl = identifier;
12495       else
12496         {
12497           /* If the next token is a `::', then the name must be a type
12498              name.
12499
12500              [basic.lookup.qual]
12501
12502              During the lookup for a name preceding the :: scope
12503              resolution operator, object, function, and enumerator
12504              names are ignored.  */
12505           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12506             tag_type = typename_type;
12507           /* Look up the name.  */
12508           decl = cp_parser_lookup_name (parser, identifier,
12509                                         tag_type,
12510                                         /*is_template=*/false,
12511                                         /*is_namespace=*/false,
12512                                         check_dependency_p,
12513                                         /*ambiguous_p=*/NULL);
12514         }
12515     }
12516   else
12517     {
12518       /* Try a template-id.  */
12519       decl = cp_parser_template_id (parser, template_keyword_p,
12520                                     check_dependency_p,
12521                                     is_declaration);
12522       if (decl == error_mark_node)
12523         return error_mark_node;
12524     }
12525
12526   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12527
12528   /* If this is a typename, create a TYPENAME_TYPE.  */
12529   if (typename_p && decl != error_mark_node)
12530     {
12531       decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12532       if (decl != error_mark_node)
12533         decl = TYPE_NAME (decl);
12534     }
12535
12536   /* Check to see that it is really the name of a class.  */
12537   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12538       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12539       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12540     /* Situations like this:
12541
12542          template <typename T> struct A {
12543            typename T::template X<int>::I i;
12544          };
12545
12546        are problematic.  Is `T::template X<int>' a class-name?  The
12547        standard does not seem to be definitive, but there is no other
12548        valid interpretation of the following `::'.  Therefore, those
12549        names are considered class-names.  */
12550     decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12551   else if (decl == error_mark_node
12552            || TREE_CODE (decl) != TYPE_DECL
12553            || TREE_TYPE (decl) == error_mark_node
12554            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12555     {
12556       cp_parser_error (parser, "expected class-name");
12557       return error_mark_node;
12558     }
12559
12560   return decl;
12561 }
12562
12563 /* Parse a class-specifier.
12564
12565    class-specifier:
12566      class-head { member-specification [opt] }
12567
12568    Returns the TREE_TYPE representing the class.  */
12569
12570 static tree
12571 cp_parser_class_specifier (cp_parser* parser)
12572 {
12573   cp_token *token;
12574   tree type;
12575   tree attributes = NULL_TREE;
12576   int has_trailing_semicolon;
12577   bool nested_name_specifier_p;
12578   unsigned saved_num_template_parameter_lists;
12579   tree old_scope = NULL_TREE;
12580   tree scope = NULL_TREE;
12581
12582   push_deferring_access_checks (dk_no_deferred);
12583
12584   /* Parse the class-head.  */
12585   type = cp_parser_class_head (parser,
12586                                &nested_name_specifier_p,
12587                                &attributes);
12588   /* If the class-head was a semantic disaster, skip the entire body
12589      of the class.  */
12590   if (!type)
12591     {
12592       cp_parser_skip_to_end_of_block_or_statement (parser);
12593       pop_deferring_access_checks ();
12594       return error_mark_node;
12595     }
12596
12597   /* Look for the `{'.  */
12598   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12599     {
12600       pop_deferring_access_checks ();
12601       return error_mark_node;
12602     }
12603
12604   /* Issue an error message if type-definitions are forbidden here.  */
12605   cp_parser_check_type_definition (parser);
12606   /* Remember that we are defining one more class.  */
12607   ++parser->num_classes_being_defined;
12608   /* Inside the class, surrounding template-parameter-lists do not
12609      apply.  */
12610   saved_num_template_parameter_lists
12611     = parser->num_template_parameter_lists;
12612   parser->num_template_parameter_lists = 0;
12613
12614   /* Start the class.  */
12615   if (nested_name_specifier_p)
12616     {
12617       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12618       old_scope = push_inner_scope (scope);
12619     }
12620   type = begin_class_definition (type);
12621
12622   if (type == error_mark_node)
12623     /* If the type is erroneous, skip the entire body of the class.  */
12624     cp_parser_skip_to_closing_brace (parser);
12625   else
12626     /* Parse the member-specification.  */
12627     cp_parser_member_specification_opt (parser);
12628
12629   /* Look for the trailing `}'.  */
12630   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12631   /* We get better error messages by noticing a common problem: a
12632      missing trailing `;'.  */
12633   token = cp_lexer_peek_token (parser->lexer);
12634   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12635   /* Look for trailing attributes to apply to this class.  */
12636   if (cp_parser_allow_gnu_extensions_p (parser))
12637     {
12638       tree sub_attr = cp_parser_attributes_opt (parser);
12639       attributes = chainon (attributes, sub_attr);
12640     }
12641   if (type != error_mark_node)
12642     type = finish_struct (type, attributes);
12643   if (nested_name_specifier_p)
12644     pop_inner_scope (old_scope, scope);
12645   /* If this class is not itself within the scope of another class,
12646      then we need to parse the bodies of all of the queued function
12647      definitions.  Note that the queued functions defined in a class
12648      are not always processed immediately following the
12649      class-specifier for that class.  Consider:
12650
12651        struct A {
12652          struct B { void f() { sizeof (A); } };
12653        };
12654
12655      If `f' were processed before the processing of `A' were
12656      completed, there would be no way to compute the size of `A'.
12657      Note that the nesting we are interested in here is lexical --
12658      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12659      for:
12660
12661        struct A { struct B; };
12662        struct A::B { void f() { } };
12663
12664      there is no need to delay the parsing of `A::B::f'.  */
12665   if (--parser->num_classes_being_defined == 0)
12666     {
12667       tree queue_entry;
12668       tree fn;
12669       tree class_type = NULL_TREE;
12670       tree pushed_scope = NULL_TREE;
12671
12672       /* In a first pass, parse default arguments to the functions.
12673          Then, in a second pass, parse the bodies of the functions.
12674          This two-phased approach handles cases like:
12675
12676             struct S {
12677               void f() { g(); }
12678               void g(int i = 3);
12679             };
12680
12681          */
12682       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12683              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12684            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12685            TREE_PURPOSE (parser->unparsed_functions_queues)
12686              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12687         {
12688           fn = TREE_VALUE (queue_entry);
12689           /* If there are default arguments that have not yet been processed,
12690              take care of them now.  */
12691           if (class_type != TREE_PURPOSE (queue_entry))
12692             {
12693               if (pushed_scope)
12694                 pop_scope (pushed_scope);
12695               class_type = TREE_PURPOSE (queue_entry);
12696               pushed_scope = push_scope (class_type);
12697             }
12698           /* Make sure that any template parameters are in scope.  */
12699           maybe_begin_member_template_processing (fn);
12700           /* Parse the default argument expressions.  */
12701           cp_parser_late_parsing_default_args (parser, fn);
12702           /* Remove any template parameters from the symbol table.  */
12703           maybe_end_member_template_processing ();
12704         }
12705       if (pushed_scope)
12706         pop_scope (pushed_scope);
12707       /* Now parse the body of the functions.  */
12708       for (TREE_VALUE (parser->unparsed_functions_queues)
12709              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12710            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12711            TREE_VALUE (parser->unparsed_functions_queues)
12712              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12713         {
12714           /* Figure out which function we need to process.  */
12715           fn = TREE_VALUE (queue_entry);
12716
12717           /* A hack to prevent garbage collection.  */
12718           function_depth++;
12719
12720           /* Parse the function.  */
12721           cp_parser_late_parsing_for_member (parser, fn);
12722           function_depth--;
12723         }
12724     }
12725
12726   /* Put back any saved access checks.  */
12727   pop_deferring_access_checks ();
12728
12729   /* Restore the count of active template-parameter-lists.  */
12730   parser->num_template_parameter_lists
12731     = saved_num_template_parameter_lists;
12732
12733   return type;
12734 }
12735
12736 /* Parse a class-head.
12737
12738    class-head:
12739      class-key identifier [opt] base-clause [opt]
12740      class-key nested-name-specifier identifier base-clause [opt]
12741      class-key nested-name-specifier [opt] template-id
12742        base-clause [opt]
12743
12744    GNU Extensions:
12745      class-key attributes identifier [opt] base-clause [opt]
12746      class-key attributes nested-name-specifier identifier base-clause [opt]
12747      class-key attributes nested-name-specifier [opt] template-id
12748        base-clause [opt]
12749
12750    Returns the TYPE of the indicated class.  Sets
12751    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12752    involving a nested-name-specifier was used, and FALSE otherwise.
12753
12754    Returns error_mark_node if this is not a class-head.
12755
12756    Returns NULL_TREE if the class-head is syntactically valid, but
12757    semantically invalid in a way that means we should skip the entire
12758    body of the class.  */
12759
12760 static tree
12761 cp_parser_class_head (cp_parser* parser,
12762                       bool* nested_name_specifier_p,
12763                       tree *attributes_p)
12764 {
12765   tree nested_name_specifier;
12766   enum tag_types class_key;
12767   tree id = NULL_TREE;
12768   tree type = NULL_TREE;
12769   tree attributes;
12770   bool template_id_p = false;
12771   bool qualified_p = false;
12772   bool invalid_nested_name_p = false;
12773   bool invalid_explicit_specialization_p = false;
12774   tree pushed_scope = NULL_TREE;
12775   unsigned num_templates;
12776   tree bases;
12777
12778   /* Assume no nested-name-specifier will be present.  */
12779   *nested_name_specifier_p = false;
12780   /* Assume no template parameter lists will be used in defining the
12781      type.  */
12782   num_templates = 0;
12783
12784   /* Look for the class-key.  */
12785   class_key = cp_parser_class_key (parser);
12786   if (class_key == none_type)
12787     return error_mark_node;
12788
12789   /* Parse the attributes.  */
12790   attributes = cp_parser_attributes_opt (parser);
12791
12792   /* If the next token is `::', that is invalid -- but sometimes
12793      people do try to write:
12794
12795        struct ::S {};
12796
12797      Handle this gracefully by accepting the extra qualifier, and then
12798      issuing an error about it later if this really is a
12799      class-head.  If it turns out just to be an elaborated type
12800      specifier, remain silent.  */
12801   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12802     qualified_p = true;
12803
12804   push_deferring_access_checks (dk_no_check);
12805
12806   /* Determine the name of the class.  Begin by looking for an
12807      optional nested-name-specifier.  */
12808   nested_name_specifier
12809     = cp_parser_nested_name_specifier_opt (parser,
12810                                            /*typename_keyword_p=*/false,
12811                                            /*check_dependency_p=*/false,
12812                                            /*type_p=*/false,
12813                                            /*is_declaration=*/false);
12814   /* If there was a nested-name-specifier, then there *must* be an
12815      identifier.  */
12816   if (nested_name_specifier)
12817     {
12818       /* Although the grammar says `identifier', it really means
12819          `class-name' or `template-name'.  You are only allowed to
12820          define a class that has already been declared with this
12821          syntax.
12822
12823          The proposed resolution for Core Issue 180 says that whever
12824          you see `class T::X' you should treat `X' as a type-name.
12825
12826          It is OK to define an inaccessible class; for example:
12827
12828            class A { class B; };
12829            class A::B {};
12830
12831          We do not know if we will see a class-name, or a
12832          template-name.  We look for a class-name first, in case the
12833          class-name is a template-id; if we looked for the
12834          template-name first we would stop after the template-name.  */
12835       cp_parser_parse_tentatively (parser);
12836       type = cp_parser_class_name (parser,
12837                                    /*typename_keyword_p=*/false,
12838                                    /*template_keyword_p=*/false,
12839                                    class_type,
12840                                    /*check_dependency_p=*/false,
12841                                    /*class_head_p=*/true,
12842                                    /*is_declaration=*/false);
12843       /* If that didn't work, ignore the nested-name-specifier.  */
12844       if (!cp_parser_parse_definitely (parser))
12845         {
12846           invalid_nested_name_p = true;
12847           id = cp_parser_identifier (parser);
12848           if (id == error_mark_node)
12849             id = NULL_TREE;
12850         }
12851       /* If we could not find a corresponding TYPE, treat this
12852          declaration like an unqualified declaration.  */
12853       if (type == error_mark_node)
12854         nested_name_specifier = NULL_TREE;
12855       /* Otherwise, count the number of templates used in TYPE and its
12856          containing scopes.  */
12857       else
12858         {
12859           tree scope;
12860
12861           for (scope = TREE_TYPE (type);
12862                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12863                scope = (TYPE_P (scope)
12864                         ? TYPE_CONTEXT (scope)
12865                         : DECL_CONTEXT (scope)))
12866             if (TYPE_P (scope)
12867                 && CLASS_TYPE_P (scope)
12868                 && CLASSTYPE_TEMPLATE_INFO (scope)
12869                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12870                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12871               ++num_templates;
12872         }
12873     }
12874   /* Otherwise, the identifier is optional.  */
12875   else
12876     {
12877       /* We don't know whether what comes next is a template-id,
12878          an identifier, or nothing at all.  */
12879       cp_parser_parse_tentatively (parser);
12880       /* Check for a template-id.  */
12881       id = cp_parser_template_id (parser,
12882                                   /*template_keyword_p=*/false,
12883                                   /*check_dependency_p=*/true,
12884                                   /*is_declaration=*/true);
12885       /* If that didn't work, it could still be an identifier.  */
12886       if (!cp_parser_parse_definitely (parser))
12887         {
12888           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12889             id = cp_parser_identifier (parser);
12890           else
12891             id = NULL_TREE;
12892         }
12893       else
12894         {
12895           template_id_p = true;
12896           ++num_templates;
12897         }
12898     }
12899
12900   pop_deferring_access_checks ();
12901
12902   if (id)
12903     cp_parser_check_for_invalid_template_id (parser, id);
12904
12905   /* If it's not a `:' or a `{' then we can't really be looking at a
12906      class-head, since a class-head only appears as part of a
12907      class-specifier.  We have to detect this situation before calling
12908      xref_tag, since that has irreversible side-effects.  */
12909   if (!cp_parser_next_token_starts_class_definition_p (parser))
12910     {
12911       cp_parser_error (parser, "expected %<{%> or %<:%>");
12912       return error_mark_node;
12913     }
12914
12915   /* At this point, we're going ahead with the class-specifier, even
12916      if some other problem occurs.  */
12917   cp_parser_commit_to_tentative_parse (parser);
12918   /* Issue the error about the overly-qualified name now.  */
12919   if (qualified_p)
12920     cp_parser_error (parser,
12921                      "global qualification of class name is invalid");
12922   else if (invalid_nested_name_p)
12923     cp_parser_error (parser,
12924                      "qualified name does not name a class");
12925   else if (nested_name_specifier)
12926     {
12927       tree scope;
12928
12929       /* Reject typedef-names in class heads.  */
12930       if (!DECL_IMPLICIT_TYPEDEF_P (type))
12931         {
12932           error ("invalid class name in declaration of %qD", type);
12933           type = NULL_TREE;
12934           goto done;
12935         }
12936
12937       /* Figure out in what scope the declaration is being placed.  */
12938       scope = current_scope ();
12939       /* If that scope does not contain the scope in which the
12940          class was originally declared, the program is invalid.  */
12941       if (scope && !is_ancestor (scope, nested_name_specifier))
12942         {
12943           error ("declaration of %qD in %qD which does not enclose %qD",
12944                  type, scope, nested_name_specifier);
12945           type = NULL_TREE;
12946           goto done;
12947         }
12948       /* [dcl.meaning]
12949
12950          A declarator-id shall not be qualified exception of the
12951          definition of a ... nested class outside of its class
12952          ... [or] a the definition or explicit instantiation of a
12953          class member of a namespace outside of its namespace.  */
12954       if (scope == nested_name_specifier)
12955         {
12956           pedwarn ("extra qualification ignored");
12957           nested_name_specifier = NULL_TREE;
12958           num_templates = 0;
12959         }
12960     }
12961   /* An explicit-specialization must be preceded by "template <>".  If
12962      it is not, try to recover gracefully.  */
12963   if (at_namespace_scope_p ()
12964       && parser->num_template_parameter_lists == 0
12965       && template_id_p)
12966     {
12967       error ("an explicit specialization must be preceded by %<template <>%>");
12968       invalid_explicit_specialization_p = true;
12969       /* Take the same action that would have been taken by
12970          cp_parser_explicit_specialization.  */
12971       ++parser->num_template_parameter_lists;
12972       begin_specialization ();
12973     }
12974   /* There must be no "return" statements between this point and the
12975      end of this function; set "type "to the correct return value and
12976      use "goto done;" to return.  */
12977   /* Make sure that the right number of template parameters were
12978      present.  */
12979   if (!cp_parser_check_template_parameters (parser, num_templates))
12980     {
12981       /* If something went wrong, there is no point in even trying to
12982          process the class-definition.  */
12983       type = NULL_TREE;
12984       goto done;
12985     }
12986
12987   /* Look up the type.  */
12988   if (template_id_p)
12989     {
12990       type = TREE_TYPE (id);
12991       maybe_process_partial_specialization (type);
12992       if (nested_name_specifier)
12993         pushed_scope = push_scope (nested_name_specifier);
12994     }
12995   else if (nested_name_specifier)
12996     {
12997       tree class_type;
12998
12999       /* Given:
13000
13001             template <typename T> struct S { struct T };
13002             template <typename T> struct S<T>::T { };
13003
13004          we will get a TYPENAME_TYPE when processing the definition of
13005          `S::T'.  We need to resolve it to the actual type before we
13006          try to define it.  */
13007       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13008         {
13009           class_type = resolve_typename_type (TREE_TYPE (type),
13010                                               /*only_current_p=*/false);
13011           if (class_type != error_mark_node)
13012             type = TYPE_NAME (class_type);
13013           else
13014             {
13015               cp_parser_error (parser, "could not resolve typename type");
13016               type = error_mark_node;
13017             }
13018         }
13019
13020       maybe_process_partial_specialization (TREE_TYPE (type));
13021       class_type = current_class_type;
13022       /* Enter the scope indicated by the nested-name-specifier.  */
13023       pushed_scope = push_scope (nested_name_specifier);
13024       /* Get the canonical version of this type.  */
13025       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13026       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13027           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13028         {
13029           type = push_template_decl (type);
13030           if (type == error_mark_node)
13031             {
13032               type = NULL_TREE;
13033               goto done;
13034             }
13035         }
13036
13037       type = TREE_TYPE (type);
13038       *nested_name_specifier_p = true;
13039     }
13040   else      /* The name is not a nested name.  */
13041     {
13042       /* If the class was unnamed, create a dummy name.  */
13043       if (!id)
13044         id = make_anon_name ();
13045       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13046                        parser->num_template_parameter_lists);
13047     }
13048
13049   /* Indicate whether this class was declared as a `class' or as a
13050      `struct'.  */
13051   if (TREE_CODE (type) == RECORD_TYPE)
13052     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13053   cp_parser_check_class_key (class_key, type);
13054
13055   /* If this type was already complete, and we see another definition,
13056      that's an error.  */
13057   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13058     {
13059       error ("redefinition of %q#T", type);
13060       error ("previous definition of %q+#T", type);
13061       type = NULL_TREE;
13062       goto done;
13063     }
13064
13065   /* We will have entered the scope containing the class; the names of
13066      base classes should be looked up in that context.  For example:
13067
13068        struct A { struct B {}; struct C; };
13069        struct A::C : B {};
13070
13071      is valid.  */
13072   bases = NULL_TREE;
13073
13074   /* Get the list of base-classes, if there is one.  */
13075   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13076     bases = cp_parser_base_clause (parser);
13077
13078   /* Process the base classes.  */
13079   xref_basetypes (type, bases);
13080
13081  done:
13082   /* Leave the scope given by the nested-name-specifier.  We will
13083      enter the class scope itself while processing the members.  */
13084   if (pushed_scope)
13085     pop_scope (pushed_scope);
13086
13087   if (invalid_explicit_specialization_p)
13088     {
13089       end_specialization ();
13090       --parser->num_template_parameter_lists;
13091     }
13092   *attributes_p = attributes;
13093   return type;
13094 }
13095
13096 /* Parse a class-key.
13097
13098    class-key:
13099      class
13100      struct
13101      union
13102
13103    Returns the kind of class-key specified, or none_type to indicate
13104    error.  */
13105
13106 static enum tag_types
13107 cp_parser_class_key (cp_parser* parser)
13108 {
13109   cp_token *token;
13110   enum tag_types tag_type;
13111
13112   /* Look for the class-key.  */
13113   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13114   if (!token)
13115     return none_type;
13116
13117   /* Check to see if the TOKEN is a class-key.  */
13118   tag_type = cp_parser_token_is_class_key (token);
13119   if (!tag_type)
13120     cp_parser_error (parser, "expected class-key");
13121   return tag_type;
13122 }
13123
13124 /* Parse an (optional) member-specification.
13125
13126    member-specification:
13127      member-declaration member-specification [opt]
13128      access-specifier : member-specification [opt]  */
13129
13130 static void
13131 cp_parser_member_specification_opt (cp_parser* parser)
13132 {
13133   while (true)
13134     {
13135       cp_token *token;
13136       enum rid keyword;
13137
13138       /* Peek at the next token.  */
13139       token = cp_lexer_peek_token (parser->lexer);
13140       /* If it's a `}', or EOF then we've seen all the members.  */
13141       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
13142         break;
13143
13144       /* See if this token is a keyword.  */
13145       keyword = token->keyword;
13146       switch (keyword)
13147         {
13148         case RID_PUBLIC:
13149         case RID_PROTECTED:
13150         case RID_PRIVATE:
13151           /* Consume the access-specifier.  */
13152           cp_lexer_consume_token (parser->lexer);
13153           /* Remember which access-specifier is active.  */
13154           current_access_specifier = token->value;
13155           /* Look for the `:'.  */
13156           cp_parser_require (parser, CPP_COLON, "`:'");
13157           break;
13158
13159         default:
13160           /* Accept #pragmas at class scope.  */
13161           if (token->type == CPP_PRAGMA)
13162             {
13163               cp_lexer_handle_pragma (parser->lexer);
13164               break;
13165             }
13166
13167           /* Otherwise, the next construction must be a
13168              member-declaration.  */
13169           cp_parser_member_declaration (parser);
13170         }
13171     }
13172 }
13173
13174 /* Parse a member-declaration.
13175
13176    member-declaration:
13177      decl-specifier-seq [opt] member-declarator-list [opt] ;
13178      function-definition ; [opt]
13179      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13180      using-declaration
13181      template-declaration
13182
13183    member-declarator-list:
13184      member-declarator
13185      member-declarator-list , member-declarator
13186
13187    member-declarator:
13188      declarator pure-specifier [opt]
13189      declarator constant-initializer [opt]
13190      identifier [opt] : constant-expression
13191
13192    GNU Extensions:
13193
13194    member-declaration:
13195      __extension__ member-declaration
13196
13197    member-declarator:
13198      declarator attributes [opt] pure-specifier [opt]
13199      declarator attributes [opt] constant-initializer [opt]
13200      identifier [opt] attributes [opt] : constant-expression  */
13201
13202 static void
13203 cp_parser_member_declaration (cp_parser* parser)
13204 {
13205   cp_decl_specifier_seq decl_specifiers;
13206   tree prefix_attributes;
13207   tree decl;
13208   int declares_class_or_enum;
13209   bool friend_p;
13210   cp_token *token;
13211   int saved_pedantic;
13212
13213   /* Check for the `__extension__' keyword.  */
13214   if (cp_parser_extension_opt (parser, &saved_pedantic))
13215     {
13216       /* Recurse.  */
13217       cp_parser_member_declaration (parser);
13218       /* Restore the old value of the PEDANTIC flag.  */
13219       pedantic = saved_pedantic;
13220
13221       return;
13222     }
13223
13224   /* Check for a template-declaration.  */
13225   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13226     {
13227       /* Parse the template-declaration.  */
13228       cp_parser_template_declaration (parser, /*member_p=*/true);
13229
13230       return;
13231     }
13232
13233   /* Check for a using-declaration.  */
13234   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13235     {
13236       /* Parse the using-declaration.  */
13237       cp_parser_using_declaration (parser);
13238
13239       return;
13240     }
13241
13242   /* Check for @defs.  */
13243   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13244     {
13245       tree ivar, member;
13246       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13247       ivar = ivar_chains;
13248       while (ivar)
13249         {
13250           member = ivar;
13251           ivar = TREE_CHAIN (member);
13252           TREE_CHAIN (member) = NULL_TREE;
13253           finish_member_declaration (member);
13254         }
13255       return;
13256     }
13257
13258   /* Parse the decl-specifier-seq.  */
13259   cp_parser_decl_specifier_seq (parser,
13260                                 CP_PARSER_FLAGS_OPTIONAL,
13261                                 &decl_specifiers,
13262                                 &declares_class_or_enum);
13263   prefix_attributes = decl_specifiers.attributes;
13264   decl_specifiers.attributes = NULL_TREE;
13265   /* Check for an invalid type-name.  */
13266   if (!decl_specifiers.type
13267       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13268     return;
13269   /* If there is no declarator, then the decl-specifier-seq should
13270      specify a type.  */
13271   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13272     {
13273       /* If there was no decl-specifier-seq, and the next token is a
13274          `;', then we have something like:
13275
13276            struct S { ; };
13277
13278          [class.mem]
13279
13280          Each member-declaration shall declare at least one member
13281          name of the class.  */
13282       if (!decl_specifiers.any_specifiers_p)
13283         {
13284           cp_token *token = cp_lexer_peek_token (parser->lexer);
13285           if (pedantic && !token->in_system_header)
13286             pedwarn ("%Hextra %<;%>", &token->location);
13287         }
13288       else
13289         {
13290           tree type;
13291
13292           /* See if this declaration is a friend.  */
13293           friend_p = cp_parser_friend_p (&decl_specifiers);
13294           /* If there were decl-specifiers, check to see if there was
13295              a class-declaration.  */
13296           type = check_tag_decl (&decl_specifiers);
13297           /* Nested classes have already been added to the class, but
13298              a `friend' needs to be explicitly registered.  */
13299           if (friend_p)
13300             {
13301               /* If the `friend' keyword was present, the friend must
13302                  be introduced with a class-key.  */
13303                if (!declares_class_or_enum)
13304                  error ("a class-key must be used when declaring a friend");
13305                /* In this case:
13306
13307                     template <typename T> struct A {
13308                       friend struct A<T>::B;
13309                     };
13310
13311                   A<T>::B will be represented by a TYPENAME_TYPE, and
13312                   therefore not recognized by check_tag_decl.  */
13313                if (!type
13314                    && decl_specifiers.type
13315                    && TYPE_P (decl_specifiers.type))
13316                  type = decl_specifiers.type;
13317                if (!type || !TYPE_P (type))
13318                  error ("friend declaration does not name a class or "
13319                         "function");
13320                else
13321                  make_friend_class (current_class_type, type,
13322                                     /*complain=*/true);
13323             }
13324           /* If there is no TYPE, an error message will already have
13325              been issued.  */
13326           else if (!type || type == error_mark_node)
13327             ;
13328           /* An anonymous aggregate has to be handled specially; such
13329              a declaration really declares a data member (with a
13330              particular type), as opposed to a nested class.  */
13331           else if (ANON_AGGR_TYPE_P (type))
13332             {
13333               /* Remove constructors and such from TYPE, now that we
13334                  know it is an anonymous aggregate.  */
13335               fixup_anonymous_aggr (type);
13336               /* And make the corresponding data member.  */
13337               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13338               /* Add it to the class.  */
13339               finish_member_declaration (decl);
13340             }
13341           else
13342             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13343         }
13344     }
13345   else
13346     {
13347       /* See if these declarations will be friends.  */
13348       friend_p = cp_parser_friend_p (&decl_specifiers);
13349
13350       /* Keep going until we hit the `;' at the end of the
13351          declaration.  */
13352       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13353         {
13354           tree attributes = NULL_TREE;
13355           tree first_attribute;
13356
13357           /* Peek at the next token.  */
13358           token = cp_lexer_peek_token (parser->lexer);
13359
13360           /* Check for a bitfield declaration.  */
13361           if (token->type == CPP_COLON
13362               || (token->type == CPP_NAME
13363                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13364                   == CPP_COLON))
13365             {
13366               tree identifier;
13367               tree width;
13368
13369               /* Get the name of the bitfield.  Note that we cannot just
13370                  check TOKEN here because it may have been invalidated by
13371                  the call to cp_lexer_peek_nth_token above.  */
13372               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13373                 identifier = cp_parser_identifier (parser);
13374               else
13375                 identifier = NULL_TREE;
13376
13377               /* Consume the `:' token.  */
13378               cp_lexer_consume_token (parser->lexer);
13379               /* Get the width of the bitfield.  */
13380               width
13381                 = cp_parser_constant_expression (parser,
13382                                                  /*allow_non_constant=*/false,
13383                                                  NULL);
13384
13385               /* Look for attributes that apply to the bitfield.  */
13386               attributes = cp_parser_attributes_opt (parser);
13387               /* Remember which attributes are prefix attributes and
13388                  which are not.  */
13389               first_attribute = attributes;
13390               /* Combine the attributes.  */
13391               attributes = chainon (prefix_attributes, attributes);
13392
13393               /* Create the bitfield declaration.  */
13394               decl = grokbitfield (identifier
13395                                    ? make_id_declarator (NULL_TREE,
13396                                                          identifier)
13397                                    : NULL,
13398                                    &decl_specifiers,
13399                                    width);
13400               /* Apply the attributes.  */
13401               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13402             }
13403           else
13404             {
13405               cp_declarator *declarator;
13406               tree initializer;
13407               tree asm_specification;
13408               int ctor_dtor_or_conv_p;
13409
13410               /* Parse the declarator.  */
13411               declarator
13412                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13413                                         &ctor_dtor_or_conv_p,
13414                                         /*parenthesized_p=*/NULL,
13415                                         /*member_p=*/true);
13416
13417               /* If something went wrong parsing the declarator, make sure
13418                  that we at least consume some tokens.  */
13419               if (declarator == cp_error_declarator)
13420                 {
13421                   /* Skip to the end of the statement.  */
13422                   cp_parser_skip_to_end_of_statement (parser);
13423                   /* If the next token is not a semicolon, that is
13424                      probably because we just skipped over the body of
13425                      a function.  So, we consume a semicolon if
13426                      present, but do not issue an error message if it
13427                      is not present.  */
13428                   if (cp_lexer_next_token_is (parser->lexer,
13429                                               CPP_SEMICOLON))
13430                     cp_lexer_consume_token (parser->lexer);
13431                   return;
13432                 }
13433
13434               if (declares_class_or_enum & 2)
13435                 cp_parser_check_for_definition_in_return_type
13436                   (declarator, decl_specifiers.type);
13437
13438               /* Look for an asm-specification.  */
13439               asm_specification = cp_parser_asm_specification_opt (parser);
13440               /* Look for attributes that apply to the declaration.  */
13441               attributes = cp_parser_attributes_opt (parser);
13442               /* Remember which attributes are prefix attributes and
13443                  which are not.  */
13444               first_attribute = attributes;
13445               /* Combine the attributes.  */
13446               attributes = chainon (prefix_attributes, attributes);
13447
13448               /* If it's an `=', then we have a constant-initializer or a
13449                  pure-specifier.  It is not correct to parse the
13450                  initializer before registering the member declaration
13451                  since the member declaration should be in scope while
13452                  its initializer is processed.  However, the rest of the
13453                  front end does not yet provide an interface that allows
13454                  us to handle this correctly.  */
13455               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13456                 {
13457                   /* In [class.mem]:
13458
13459                      A pure-specifier shall be used only in the declaration of
13460                      a virtual function.
13461
13462                      A member-declarator can contain a constant-initializer
13463                      only if it declares a static member of integral or
13464                      enumeration type.
13465
13466                      Therefore, if the DECLARATOR is for a function, we look
13467                      for a pure-specifier; otherwise, we look for a
13468                      constant-initializer.  When we call `grokfield', it will
13469                      perform more stringent semantics checks.  */
13470                   if (declarator->kind == cdk_function)
13471                     initializer = cp_parser_pure_specifier (parser);
13472                   else
13473                     /* Parse the initializer.  */
13474                     initializer = cp_parser_constant_initializer (parser);
13475                 }
13476               /* Otherwise, there is no initializer.  */
13477               else
13478                 initializer = NULL_TREE;
13479
13480               /* See if we are probably looking at a function
13481                  definition.  We are certainly not looking at a
13482                  member-declarator.  Calling `grokfield' has
13483                  side-effects, so we must not do it unless we are sure
13484                  that we are looking at a member-declarator.  */
13485               if (cp_parser_token_starts_function_definition_p
13486                   (cp_lexer_peek_token (parser->lexer)))
13487                 {
13488                   /* The grammar does not allow a pure-specifier to be
13489                      used when a member function is defined.  (It is
13490                      possible that this fact is an oversight in the
13491                      standard, since a pure function may be defined
13492                      outside of the class-specifier.  */
13493                   if (initializer)
13494                     error ("pure-specifier on function-definition");
13495                   decl = cp_parser_save_member_function_body (parser,
13496                                                               &decl_specifiers,
13497                                                               declarator,
13498                                                               attributes);
13499                   /* If the member was not a friend, declare it here.  */
13500                   if (!friend_p)
13501                     finish_member_declaration (decl);
13502                   /* Peek at the next token.  */
13503                   token = cp_lexer_peek_token (parser->lexer);
13504                   /* If the next token is a semicolon, consume it.  */
13505                   if (token->type == CPP_SEMICOLON)
13506                     cp_lexer_consume_token (parser->lexer);
13507                   return;
13508                 }
13509               else
13510                 {
13511                   /* Create the declaration.  */
13512                   decl = grokfield (declarator, &decl_specifiers,
13513                                     initializer, asm_specification,
13514                                     attributes);
13515                   /* Any initialization must have been from a
13516                      constant-expression.  */
13517                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13518                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13519                 }
13520             }
13521
13522           /* Reset PREFIX_ATTRIBUTES.  */
13523           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13524             attributes = TREE_CHAIN (attributes);
13525           if (attributes)
13526             TREE_CHAIN (attributes) = NULL_TREE;
13527
13528           /* If there is any qualification still in effect, clear it
13529              now; we will be starting fresh with the next declarator.  */
13530           parser->scope = NULL_TREE;
13531           parser->qualifying_scope = NULL_TREE;
13532           parser->object_scope = NULL_TREE;
13533           /* If it's a `,', then there are more declarators.  */
13534           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13535             cp_lexer_consume_token (parser->lexer);
13536           /* If the next token isn't a `;', then we have a parse error.  */
13537           else if (cp_lexer_next_token_is_not (parser->lexer,
13538                                                CPP_SEMICOLON))
13539             {
13540               cp_parser_error (parser, "expected %<;%>");
13541               /* Skip tokens until we find a `;'.  */
13542               cp_parser_skip_to_end_of_statement (parser);
13543
13544               break;
13545             }
13546
13547           if (decl)
13548             {
13549               /* Add DECL to the list of members.  */
13550               if (!friend_p)
13551                 finish_member_declaration (decl);
13552
13553               if (TREE_CODE (decl) == FUNCTION_DECL)
13554                 cp_parser_save_default_args (parser, decl);
13555             }
13556         }
13557     }
13558
13559   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13560 }
13561
13562 /* Parse a pure-specifier.
13563
13564    pure-specifier:
13565      = 0
13566
13567    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13568    Otherwise, ERROR_MARK_NODE is returned.  */
13569
13570 static tree
13571 cp_parser_pure_specifier (cp_parser* parser)
13572 {
13573   cp_token *token;
13574
13575   /* Look for the `=' token.  */
13576   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13577     return error_mark_node;
13578   /* Look for the `0' token.  */
13579   token = cp_lexer_consume_token (parser->lexer);
13580   if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13581     {
13582       cp_parser_error (parser,
13583                        "invalid pure specifier (only `= 0' is allowed)");
13584       cp_parser_skip_to_end_of_statement (parser);
13585       return error_mark_node;
13586     }
13587
13588   /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13589      We need to get information from the lexer about how the number
13590      was spelled in order to fix this problem.  */
13591   return integer_zero_node;
13592 }
13593
13594 /* Parse a constant-initializer.
13595
13596    constant-initializer:
13597      = constant-expression
13598
13599    Returns a representation of the constant-expression.  */
13600
13601 static tree
13602 cp_parser_constant_initializer (cp_parser* parser)
13603 {
13604   /* Look for the `=' token.  */
13605   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13606     return error_mark_node;
13607
13608   /* It is invalid to write:
13609
13610        struct S { static const int i = { 7 }; };
13611
13612      */
13613   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13614     {
13615       cp_parser_error (parser,
13616                        "a brace-enclosed initializer is not allowed here");
13617       /* Consume the opening brace.  */
13618       cp_lexer_consume_token (parser->lexer);
13619       /* Skip the initializer.  */
13620       cp_parser_skip_to_closing_brace (parser);
13621       /* Look for the trailing `}'.  */
13622       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13623
13624       return error_mark_node;
13625     }
13626
13627   return cp_parser_constant_expression (parser,
13628                                         /*allow_non_constant=*/false,
13629                                         NULL);
13630 }
13631
13632 /* Derived classes [gram.class.derived] */
13633
13634 /* Parse a base-clause.
13635
13636    base-clause:
13637      : base-specifier-list
13638
13639    base-specifier-list:
13640      base-specifier
13641      base-specifier-list , base-specifier
13642
13643    Returns a TREE_LIST representing the base-classes, in the order in
13644    which they were declared.  The representation of each node is as
13645    described by cp_parser_base_specifier.
13646
13647    In the case that no bases are specified, this function will return
13648    NULL_TREE, not ERROR_MARK_NODE.  */
13649
13650 static tree
13651 cp_parser_base_clause (cp_parser* parser)
13652 {
13653   tree bases = NULL_TREE;
13654
13655   /* Look for the `:' that begins the list.  */
13656   cp_parser_require (parser, CPP_COLON, "`:'");
13657
13658   /* Scan the base-specifier-list.  */
13659   while (true)
13660     {
13661       cp_token *token;
13662       tree base;
13663
13664       /* Look for the base-specifier.  */
13665       base = cp_parser_base_specifier (parser);
13666       /* Add BASE to the front of the list.  */
13667       if (base != error_mark_node)
13668         {
13669           TREE_CHAIN (base) = bases;
13670           bases = base;
13671         }
13672       /* Peek at the next token.  */
13673       token = cp_lexer_peek_token (parser->lexer);
13674       /* If it's not a comma, then the list is complete.  */
13675       if (token->type != CPP_COMMA)
13676         break;
13677       /* Consume the `,'.  */
13678       cp_lexer_consume_token (parser->lexer);
13679     }
13680
13681   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13682      base class had a qualified name.  However, the next name that
13683      appears is certainly not qualified.  */
13684   parser->scope = NULL_TREE;
13685   parser->qualifying_scope = NULL_TREE;
13686   parser->object_scope = NULL_TREE;
13687
13688   return nreverse (bases);
13689 }
13690
13691 /* Parse a base-specifier.
13692
13693    base-specifier:
13694      :: [opt] nested-name-specifier [opt] class-name
13695      virtual access-specifier [opt] :: [opt] nested-name-specifier
13696        [opt] class-name
13697      access-specifier virtual [opt] :: [opt] nested-name-specifier
13698        [opt] class-name
13699
13700    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13701    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13702    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13703    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13704
13705 static tree
13706 cp_parser_base_specifier (cp_parser* parser)
13707 {
13708   cp_token *token;
13709   bool done = false;
13710   bool virtual_p = false;
13711   bool duplicate_virtual_error_issued_p = false;
13712   bool duplicate_access_error_issued_p = false;
13713   bool class_scope_p, template_p;
13714   tree access = access_default_node;
13715   tree type;
13716
13717   /* Process the optional `virtual' and `access-specifier'.  */
13718   while (!done)
13719     {
13720       /* Peek at the next token.  */
13721       token = cp_lexer_peek_token (parser->lexer);
13722       /* Process `virtual'.  */
13723       switch (token->keyword)
13724         {
13725         case RID_VIRTUAL:
13726           /* If `virtual' appears more than once, issue an error.  */
13727           if (virtual_p && !duplicate_virtual_error_issued_p)
13728             {
13729               cp_parser_error (parser,
13730                                "%<virtual%> specified more than once in base-specified");
13731               duplicate_virtual_error_issued_p = true;
13732             }
13733
13734           virtual_p = true;
13735
13736           /* Consume the `virtual' token.  */
13737           cp_lexer_consume_token (parser->lexer);
13738
13739           break;
13740
13741         case RID_PUBLIC:
13742         case RID_PROTECTED:
13743         case RID_PRIVATE:
13744           /* If more than one access specifier appears, issue an
13745              error.  */
13746           if (access != access_default_node
13747               && !duplicate_access_error_issued_p)
13748             {
13749               cp_parser_error (parser,
13750                                "more than one access specifier in base-specified");
13751               duplicate_access_error_issued_p = true;
13752             }
13753
13754           access = ridpointers[(int) token->keyword];
13755
13756           /* Consume the access-specifier.  */
13757           cp_lexer_consume_token (parser->lexer);
13758
13759           break;
13760
13761         default:
13762           done = true;
13763           break;
13764         }
13765     }
13766   /* It is not uncommon to see programs mechanically, erroneously, use
13767      the 'typename' keyword to denote (dependent) qualified types
13768      as base classes.  */
13769   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13770     {
13771       if (!processing_template_decl)
13772         error ("keyword %<typename%> not allowed outside of templates");
13773       else
13774         error ("keyword %<typename%> not allowed in this context "
13775                "(the base class is implicitly a type)");
13776       cp_lexer_consume_token (parser->lexer);
13777     }
13778
13779   /* Look for the optional `::' operator.  */
13780   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13781   /* Look for the nested-name-specifier.  The simplest way to
13782      implement:
13783
13784        [temp.res]
13785
13786        The keyword `typename' is not permitted in a base-specifier or
13787        mem-initializer; in these contexts a qualified name that
13788        depends on a template-parameter is implicitly assumed to be a
13789        type name.
13790
13791      is to pretend that we have seen the `typename' keyword at this
13792      point.  */
13793   cp_parser_nested_name_specifier_opt (parser,
13794                                        /*typename_keyword_p=*/true,
13795                                        /*check_dependency_p=*/true,
13796                                        typename_type,
13797                                        /*is_declaration=*/true);
13798   /* If the base class is given by a qualified name, assume that names
13799      we see are type names or templates, as appropriate.  */
13800   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13801   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13802
13803   /* Finally, look for the class-name.  */
13804   type = cp_parser_class_name (parser,
13805                                class_scope_p,
13806                                template_p,
13807                                typename_type,
13808                                /*check_dependency_p=*/true,
13809                                /*class_head_p=*/false,
13810                                /*is_declaration=*/true);
13811
13812   if (type == error_mark_node)
13813     return error_mark_node;
13814
13815   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13816 }
13817
13818 /* Exception handling [gram.exception] */
13819
13820 /* Parse an (optional) exception-specification.
13821
13822    exception-specification:
13823      throw ( type-id-list [opt] )
13824
13825    Returns a TREE_LIST representing the exception-specification.  The
13826    TREE_VALUE of each node is a type.  */
13827
13828 static tree
13829 cp_parser_exception_specification_opt (cp_parser* parser)
13830 {
13831   cp_token *token;
13832   tree type_id_list;
13833
13834   /* Peek at the next token.  */
13835   token = cp_lexer_peek_token (parser->lexer);
13836   /* If it's not `throw', then there's no exception-specification.  */
13837   if (!cp_parser_is_keyword (token, RID_THROW))
13838     return NULL_TREE;
13839
13840   /* Consume the `throw'.  */
13841   cp_lexer_consume_token (parser->lexer);
13842
13843   /* Look for the `('.  */
13844   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13845
13846   /* Peek at the next token.  */
13847   token = cp_lexer_peek_token (parser->lexer);
13848   /* If it's not a `)', then there is a type-id-list.  */
13849   if (token->type != CPP_CLOSE_PAREN)
13850     {
13851       const char *saved_message;
13852
13853       /* Types may not be defined in an exception-specification.  */
13854       saved_message = parser->type_definition_forbidden_message;
13855       parser->type_definition_forbidden_message
13856         = "types may not be defined in an exception-specification";
13857       /* Parse the type-id-list.  */
13858       type_id_list = cp_parser_type_id_list (parser);
13859       /* Restore the saved message.  */
13860       parser->type_definition_forbidden_message = saved_message;
13861     }
13862   else
13863     type_id_list = empty_except_spec;
13864
13865   /* Look for the `)'.  */
13866   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13867
13868   return type_id_list;
13869 }
13870
13871 /* Parse an (optional) type-id-list.
13872
13873    type-id-list:
13874      type-id
13875      type-id-list , type-id
13876
13877    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13878    in the order that the types were presented.  */
13879
13880 static tree
13881 cp_parser_type_id_list (cp_parser* parser)
13882 {
13883   tree types = NULL_TREE;
13884
13885   while (true)
13886     {
13887       cp_token *token;
13888       tree type;
13889
13890       /* Get the next type-id.  */
13891       type = cp_parser_type_id (parser);
13892       /* Add it to the list.  */
13893       types = add_exception_specifier (types, type, /*complain=*/1);
13894       /* Peek at the next token.  */
13895       token = cp_lexer_peek_token (parser->lexer);
13896       /* If it is not a `,', we are done.  */
13897       if (token->type != CPP_COMMA)
13898         break;
13899       /* Consume the `,'.  */
13900       cp_lexer_consume_token (parser->lexer);
13901     }
13902
13903   return nreverse (types);
13904 }
13905
13906 /* Parse a try-block.
13907
13908    try-block:
13909      try compound-statement handler-seq  */
13910
13911 static tree
13912 cp_parser_try_block (cp_parser* parser)
13913 {
13914   tree try_block;
13915
13916   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13917   try_block = begin_try_block ();
13918   cp_parser_compound_statement (parser, NULL, true);
13919   finish_try_block (try_block);
13920   cp_parser_handler_seq (parser);
13921   finish_handler_sequence (try_block);
13922
13923   return try_block;
13924 }
13925
13926 /* Parse a function-try-block.
13927
13928    function-try-block:
13929      try ctor-initializer [opt] function-body handler-seq  */
13930
13931 static bool
13932 cp_parser_function_try_block (cp_parser* parser)
13933 {
13934   tree try_block;
13935   bool ctor_initializer_p;
13936
13937   /* Look for the `try' keyword.  */
13938   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13939     return false;
13940   /* Let the rest of the front-end know where we are.  */
13941   try_block = begin_function_try_block ();
13942   /* Parse the function-body.  */
13943   ctor_initializer_p
13944     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13945   /* We're done with the `try' part.  */
13946   finish_function_try_block (try_block);
13947   /* Parse the handlers.  */
13948   cp_parser_handler_seq (parser);
13949   /* We're done with the handlers.  */
13950   finish_function_handler_sequence (try_block);
13951
13952   return ctor_initializer_p;
13953 }
13954
13955 /* Parse a handler-seq.
13956
13957    handler-seq:
13958      handler handler-seq [opt]  */
13959
13960 static void
13961 cp_parser_handler_seq (cp_parser* parser)
13962 {
13963   while (true)
13964     {
13965       cp_token *token;
13966
13967       /* Parse the handler.  */
13968       cp_parser_handler (parser);
13969       /* Peek at the next token.  */
13970       token = cp_lexer_peek_token (parser->lexer);
13971       /* If it's not `catch' then there are no more handlers.  */
13972       if (!cp_parser_is_keyword (token, RID_CATCH))
13973         break;
13974     }
13975 }
13976
13977 /* Parse a handler.
13978
13979    handler:
13980      catch ( exception-declaration ) compound-statement  */
13981
13982 static void
13983 cp_parser_handler (cp_parser* parser)
13984 {
13985   tree handler;
13986   tree declaration;
13987
13988   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13989   handler = begin_handler ();
13990   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13991   declaration = cp_parser_exception_declaration (parser);
13992   finish_handler_parms (declaration, handler);
13993   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13994   cp_parser_compound_statement (parser, NULL, false);
13995   finish_handler (handler);
13996 }
13997
13998 /* Parse an exception-declaration.
13999
14000    exception-declaration:
14001      type-specifier-seq declarator
14002      type-specifier-seq abstract-declarator
14003      type-specifier-seq
14004      ...
14005
14006    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14007    ellipsis variant is used.  */
14008
14009 static tree
14010 cp_parser_exception_declaration (cp_parser* parser)
14011 {
14012   tree decl;
14013   cp_decl_specifier_seq type_specifiers;
14014   cp_declarator *declarator;
14015   const char *saved_message;
14016
14017   /* If it's an ellipsis, it's easy to handle.  */
14018   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14019     {
14020       /* Consume the `...' token.  */
14021       cp_lexer_consume_token (parser->lexer);
14022       return NULL_TREE;
14023     }
14024
14025   /* Types may not be defined in exception-declarations.  */
14026   saved_message = parser->type_definition_forbidden_message;
14027   parser->type_definition_forbidden_message
14028     = "types may not be defined in exception-declarations";
14029
14030   /* Parse the type-specifier-seq.  */
14031   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14032                                 &type_specifiers);
14033   /* If it's a `)', then there is no declarator.  */
14034   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14035     declarator = NULL;
14036   else
14037     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14038                                        /*ctor_dtor_or_conv_p=*/NULL,
14039                                        /*parenthesized_p=*/NULL,
14040                                        /*member_p=*/false);
14041
14042   /* Restore the saved message.  */
14043   parser->type_definition_forbidden_message = saved_message;
14044
14045   if (type_specifiers.any_specifiers_p)
14046     {
14047       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14048       if (decl == NULL_TREE)
14049         error ("invalid catch parameter");
14050     }
14051   else
14052     decl = NULL_TREE;
14053
14054   return decl;
14055 }
14056
14057 /* Parse a throw-expression.
14058
14059    throw-expression:
14060      throw assignment-expression [opt]
14061
14062    Returns a THROW_EXPR representing the throw-expression.  */
14063
14064 static tree
14065 cp_parser_throw_expression (cp_parser* parser)
14066 {
14067   tree expression;
14068   cp_token* token;
14069
14070   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14071   token = cp_lexer_peek_token (parser->lexer);
14072   /* Figure out whether or not there is an assignment-expression
14073      following the "throw" keyword.  */
14074   if (token->type == CPP_COMMA
14075       || token->type == CPP_SEMICOLON
14076       || token->type == CPP_CLOSE_PAREN
14077       || token->type == CPP_CLOSE_SQUARE
14078       || token->type == CPP_CLOSE_BRACE
14079       || token->type == CPP_COLON)
14080     expression = NULL_TREE;
14081   else
14082     expression = cp_parser_assignment_expression (parser,
14083                                                   /*cast_p=*/false);
14084
14085   return build_throw (expression);
14086 }
14087
14088 /* GNU Extensions */
14089
14090 /* Parse an (optional) asm-specification.
14091
14092    asm-specification:
14093      asm ( string-literal )
14094
14095    If the asm-specification is present, returns a STRING_CST
14096    corresponding to the string-literal.  Otherwise, returns
14097    NULL_TREE.  */
14098
14099 static tree
14100 cp_parser_asm_specification_opt (cp_parser* parser)
14101 {
14102   cp_token *token;
14103   tree asm_specification;
14104
14105   /* Peek at the next token.  */
14106   token = cp_lexer_peek_token (parser->lexer);
14107   /* If the next token isn't the `asm' keyword, then there's no
14108      asm-specification.  */
14109   if (!cp_parser_is_keyword (token, RID_ASM))
14110     return NULL_TREE;
14111
14112   /* Consume the `asm' token.  */
14113   cp_lexer_consume_token (parser->lexer);
14114   /* Look for the `('.  */
14115   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14116
14117   /* Look for the string-literal.  */
14118   asm_specification = cp_parser_string_literal (parser, false, false);
14119
14120   /* Look for the `)'.  */
14121   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14122
14123   return asm_specification;
14124 }
14125
14126 /* Parse an asm-operand-list.
14127
14128    asm-operand-list:
14129      asm-operand
14130      asm-operand-list , asm-operand
14131
14132    asm-operand:
14133      string-literal ( expression )
14134      [ string-literal ] string-literal ( expression )
14135
14136    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14137    each node is the expression.  The TREE_PURPOSE is itself a
14138    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14139    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14140    is a STRING_CST for the string literal before the parenthesis.  */
14141
14142 static tree
14143 cp_parser_asm_operand_list (cp_parser* parser)
14144 {
14145   tree asm_operands = NULL_TREE;
14146
14147   while (true)
14148     {
14149       tree string_literal;
14150       tree expression;
14151       tree name;
14152
14153       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14154         {
14155           /* Consume the `[' token.  */
14156           cp_lexer_consume_token (parser->lexer);
14157           /* Read the operand name.  */
14158           name = cp_parser_identifier (parser);
14159           if (name != error_mark_node)
14160             name = build_string (IDENTIFIER_LENGTH (name),
14161                                  IDENTIFIER_POINTER (name));
14162           /* Look for the closing `]'.  */
14163           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14164         }
14165       else
14166         name = NULL_TREE;
14167       /* Look for the string-literal.  */
14168       string_literal = cp_parser_string_literal (parser, false, false);
14169
14170       /* Look for the `('.  */
14171       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14172       /* Parse the expression.  */
14173       expression = cp_parser_expression (parser, /*cast_p=*/false);
14174       /* Look for the `)'.  */
14175       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14176
14177       /* Add this operand to the list.  */
14178       asm_operands = tree_cons (build_tree_list (name, string_literal),
14179                                 expression,
14180                                 asm_operands);
14181       /* If the next token is not a `,', there are no more
14182          operands.  */
14183       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14184         break;
14185       /* Consume the `,'.  */
14186       cp_lexer_consume_token (parser->lexer);
14187     }
14188
14189   return nreverse (asm_operands);
14190 }
14191
14192 /* Parse an asm-clobber-list.
14193
14194    asm-clobber-list:
14195      string-literal
14196      asm-clobber-list , string-literal
14197
14198    Returns a TREE_LIST, indicating the clobbers in the order that they
14199    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14200
14201 static tree
14202 cp_parser_asm_clobber_list (cp_parser* parser)
14203 {
14204   tree clobbers = NULL_TREE;
14205
14206   while (true)
14207     {
14208       tree string_literal;
14209
14210       /* Look for the string literal.  */
14211       string_literal = cp_parser_string_literal (parser, false, false);
14212       /* Add it to the list.  */
14213       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14214       /* If the next token is not a `,', then the list is
14215          complete.  */
14216       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14217         break;
14218       /* Consume the `,' token.  */
14219       cp_lexer_consume_token (parser->lexer);
14220     }
14221
14222   return clobbers;
14223 }
14224
14225 /* Parse an (optional) series of attributes.
14226
14227    attributes:
14228      attributes attribute
14229
14230    attribute:
14231      __attribute__ (( attribute-list [opt] ))
14232
14233    The return value is as for cp_parser_attribute_list.  */
14234
14235 static tree
14236 cp_parser_attributes_opt (cp_parser* parser)
14237 {
14238   tree attributes = NULL_TREE;
14239
14240   while (true)
14241     {
14242       cp_token *token;
14243       tree attribute_list;
14244
14245       /* Peek at the next token.  */
14246       token = cp_lexer_peek_token (parser->lexer);
14247       /* If it's not `__attribute__', then we're done.  */
14248       if (token->keyword != RID_ATTRIBUTE)
14249         break;
14250
14251       /* Consume the `__attribute__' keyword.  */
14252       cp_lexer_consume_token (parser->lexer);
14253       /* Look for the two `(' tokens.  */
14254       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14255       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14256
14257       /* Peek at the next token.  */
14258       token = cp_lexer_peek_token (parser->lexer);
14259       if (token->type != CPP_CLOSE_PAREN)
14260         /* Parse the attribute-list.  */
14261         attribute_list = cp_parser_attribute_list (parser);
14262       else
14263         /* If the next token is a `)', then there is no attribute
14264            list.  */
14265         attribute_list = NULL;
14266
14267       /* Look for the two `)' tokens.  */
14268       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14269       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14270
14271       /* Add these new attributes to the list.  */
14272       attributes = chainon (attributes, attribute_list);
14273     }
14274
14275   return attributes;
14276 }
14277
14278 /* Parse an attribute-list.
14279
14280    attribute-list:
14281      attribute
14282      attribute-list , attribute
14283
14284    attribute:
14285      identifier
14286      identifier ( identifier )
14287      identifier ( identifier , expression-list )
14288      identifier ( expression-list )
14289
14290    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14291    to an attribute.  The TREE_PURPOSE of each node is the identifier
14292    indicating which attribute is in use.  The TREE_VALUE represents
14293    the arguments, if any.  */
14294
14295 static tree
14296 cp_parser_attribute_list (cp_parser* parser)
14297 {
14298   tree attribute_list = NULL_TREE;
14299   bool save_translate_strings_p = parser->translate_strings_p;
14300
14301   parser->translate_strings_p = false;
14302   while (true)
14303     {
14304       cp_token *token;
14305       tree identifier;
14306       tree attribute;
14307
14308       /* Look for the identifier.  We also allow keywords here; for
14309          example `__attribute__ ((const))' is legal.  */
14310       token = cp_lexer_peek_token (parser->lexer);
14311       if (token->type == CPP_NAME
14312           || token->type == CPP_KEYWORD)
14313         {
14314           /* Consume the token.  */
14315           token = cp_lexer_consume_token (parser->lexer);
14316
14317           /* Save away the identifier that indicates which attribute
14318              this is.  */
14319           identifier = token->value;
14320           attribute = build_tree_list (identifier, NULL_TREE);
14321
14322           /* Peek at the next token.  */
14323           token = cp_lexer_peek_token (parser->lexer);
14324           /* If it's an `(', then parse the attribute arguments.  */
14325           if (token->type == CPP_OPEN_PAREN)
14326             {
14327               tree arguments;
14328
14329               arguments = (cp_parser_parenthesized_expression_list
14330                            (parser, true, /*cast_p=*/false,
14331                             /*non_constant_p=*/NULL));
14332               /* Save the identifier and arguments away.  */
14333               TREE_VALUE (attribute) = arguments;
14334             }
14335
14336           /* Add this attribute to the list.  */
14337           TREE_CHAIN (attribute) = attribute_list;
14338           attribute_list = attribute;
14339
14340           token = cp_lexer_peek_token (parser->lexer);
14341         }
14342       /* Now, look for more attributes.  If the next token isn't a
14343          `,', we're done.  */
14344       if (token->type != CPP_COMMA)
14345         break;
14346
14347       /* Consume the comma and keep going.  */
14348       cp_lexer_consume_token (parser->lexer);
14349     }
14350   parser->translate_strings_p = save_translate_strings_p;
14351
14352   /* We built up the list in reverse order.  */
14353   return nreverse (attribute_list);
14354 }
14355
14356 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14357    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14358    current value of the PEDANTIC flag, regardless of whether or not
14359    the `__extension__' keyword is present.  The caller is responsible
14360    for restoring the value of the PEDANTIC flag.  */
14361
14362 static bool
14363 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14364 {
14365   /* Save the old value of the PEDANTIC flag.  */
14366   *saved_pedantic = pedantic;
14367
14368   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14369     {
14370       /* Consume the `__extension__' token.  */
14371       cp_lexer_consume_token (parser->lexer);
14372       /* We're not being pedantic while the `__extension__' keyword is
14373          in effect.  */
14374       pedantic = 0;
14375
14376       return true;
14377     }
14378
14379   return false;
14380 }
14381
14382 /* Parse a label declaration.
14383
14384    label-declaration:
14385      __label__ label-declarator-seq ;
14386
14387    label-declarator-seq:
14388      identifier , label-declarator-seq
14389      identifier  */
14390
14391 static void
14392 cp_parser_label_declaration (cp_parser* parser)
14393 {
14394   /* Look for the `__label__' keyword.  */
14395   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14396
14397   while (true)
14398     {
14399       tree identifier;
14400
14401       /* Look for an identifier.  */
14402       identifier = cp_parser_identifier (parser);
14403       /* If we failed, stop.  */
14404       if (identifier == error_mark_node)
14405         break;
14406       /* Declare it as a label.  */
14407       finish_label_decl (identifier);
14408       /* If the next token is a `;', stop.  */
14409       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14410         break;
14411       /* Look for the `,' separating the label declarations.  */
14412       cp_parser_require (parser, CPP_COMMA, "`,'");
14413     }
14414
14415   /* Look for the final `;'.  */
14416   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14417 }
14418
14419 /* Support Functions */
14420
14421 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14422    NAME should have one of the representations used for an
14423    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14424    is returned.  If PARSER->SCOPE is a dependent type, then a
14425    SCOPE_REF is returned.
14426
14427    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14428    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14429    was formed.  Abstractly, such entities should not be passed to this
14430    function, because they do not need to be looked up, but it is
14431    simpler to check for this special case here, rather than at the
14432    call-sites.
14433
14434    In cases not explicitly covered above, this function returns a
14435    DECL, OVERLOAD, or baselink representing the result of the lookup.
14436    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14437    is returned.
14438
14439    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14440    (e.g., "struct") that was used.  In that case bindings that do not
14441    refer to types are ignored.
14442
14443    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14444    ignored.
14445
14446    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14447    are ignored.
14448
14449    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14450    types.
14451
14452    If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14453    results in an ambiguity, and false otherwise.  */
14454
14455 static tree
14456 cp_parser_lookup_name (cp_parser *parser, tree name,
14457                        enum tag_types tag_type,
14458                        bool is_template, bool is_namespace,
14459                        bool check_dependency,
14460                        bool *ambiguous_p)
14461 {
14462   tree decl;
14463   tree object_type = parser->context->object_type;
14464
14465   /* Assume that the lookup will be unambiguous.  */
14466   if (ambiguous_p)
14467     *ambiguous_p = false;
14468
14469   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14470      no longer valid.  Note that if we are parsing tentatively, and
14471      the parse fails, OBJECT_TYPE will be automatically restored.  */
14472   parser->context->object_type = NULL_TREE;
14473
14474   if (name == error_mark_node)
14475     return error_mark_node;
14476
14477   /* A template-id has already been resolved; there is no lookup to
14478      do.  */
14479   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14480     return name;
14481   if (BASELINK_P (name))
14482     {
14483       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14484                   == TEMPLATE_ID_EXPR);
14485       return name;
14486     }
14487
14488   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14489      it should already have been checked to make sure that the name
14490      used matches the type being destroyed.  */
14491   if (TREE_CODE (name) == BIT_NOT_EXPR)
14492     {
14493       tree type;
14494
14495       /* Figure out to which type this destructor applies.  */
14496       if (parser->scope)
14497         type = parser->scope;
14498       else if (object_type)
14499         type = object_type;
14500       else
14501         type = current_class_type;
14502       /* If that's not a class type, there is no destructor.  */
14503       if (!type || !CLASS_TYPE_P (type))
14504         return error_mark_node;
14505       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14506         lazily_declare_fn (sfk_destructor, type);
14507       if (!CLASSTYPE_DESTRUCTORS (type))
14508           return error_mark_node;
14509       /* If it was a class type, return the destructor.  */
14510       return CLASSTYPE_DESTRUCTORS (type);
14511     }
14512
14513   /* By this point, the NAME should be an ordinary identifier.  If
14514      the id-expression was a qualified name, the qualifying scope is
14515      stored in PARSER->SCOPE at this point.  */
14516   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14517
14518   /* Perform the lookup.  */
14519   if (parser->scope)
14520     {
14521       bool dependent_p;
14522
14523       if (parser->scope == error_mark_node)
14524         return error_mark_node;
14525
14526       /* If the SCOPE is dependent, the lookup must be deferred until
14527          the template is instantiated -- unless we are explicitly
14528          looking up names in uninstantiated templates.  Even then, we
14529          cannot look up the name if the scope is not a class type; it
14530          might, for example, be a template type parameter.  */
14531       dependent_p = (TYPE_P (parser->scope)
14532                      && !(parser->in_declarator_p
14533                           && currently_open_class (parser->scope))
14534                      && dependent_type_p (parser->scope));
14535       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14536            && dependent_p)
14537         {
14538           if (tag_type)
14539             {
14540               tree type;
14541
14542               /* The resolution to Core Issue 180 says that `struct
14543                  A::B' should be considered a type-name, even if `A'
14544                  is dependent.  */
14545               type = make_typename_type (parser->scope, name, tag_type,
14546                                          /*complain=*/1);
14547               decl = TYPE_NAME (type);
14548             }
14549           else if (is_template)
14550             decl = make_unbound_class_template (parser->scope,
14551                                                 name, NULL_TREE,
14552                                                 /*complain=*/1);
14553           else
14554             decl = build_nt (SCOPE_REF, parser->scope, name);
14555         }
14556       else
14557         {
14558           tree pushed_scope = NULL_TREE;
14559
14560           /* If PARSER->SCOPE is a dependent type, then it must be a
14561              class type, and we must not be checking dependencies;
14562              otherwise, we would have processed this lookup above.  So
14563              that PARSER->SCOPE is not considered a dependent base by
14564              lookup_member, we must enter the scope here.  */
14565           if (dependent_p)
14566             pushed_scope = push_scope (parser->scope);
14567           /* If the PARSER->SCOPE is a template specialization, it
14568              may be instantiated during name lookup.  In that case,
14569              errors may be issued.  Even if we rollback the current
14570              tentative parse, those errors are valid.  */
14571           decl = lookup_qualified_name (parser->scope, name,
14572                                         tag_type != none_type,
14573                                         /*complain=*/true);
14574           if (pushed_scope)
14575             pop_scope (pushed_scope);
14576         }
14577       parser->qualifying_scope = parser->scope;
14578       parser->object_scope = NULL_TREE;
14579     }
14580   else if (object_type)
14581     {
14582       tree object_decl = NULL_TREE;
14583       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14584          OBJECT_TYPE is not a class.  */
14585       if (CLASS_TYPE_P (object_type))
14586         /* If the OBJECT_TYPE is a template specialization, it may
14587            be instantiated during name lookup.  In that case, errors
14588            may be issued.  Even if we rollback the current tentative
14589            parse, those errors are valid.  */
14590         object_decl = lookup_member (object_type,
14591                                      name,
14592                                      /*protect=*/0,
14593                                      tag_type != none_type);
14594       /* Look it up in the enclosing context, too.  */
14595       decl = lookup_name_real (name, tag_type != none_type,
14596                                /*nonclass=*/0,
14597                                /*block_p=*/true, is_namespace,
14598                                /*flags=*/0);
14599       parser->object_scope = object_type;
14600       parser->qualifying_scope = NULL_TREE;
14601       if (object_decl)
14602         decl = object_decl;
14603     }
14604   else
14605     {
14606       decl = lookup_name_real (name, tag_type != none_type,
14607                                /*nonclass=*/0,
14608                                /*block_p=*/true, is_namespace,
14609                                /*flags=*/0);
14610       parser->qualifying_scope = NULL_TREE;
14611       parser->object_scope = NULL_TREE;
14612     }
14613
14614   /* If the lookup failed, let our caller know.  */
14615   if (!decl || decl == error_mark_node)
14616     return error_mark_node;
14617
14618   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14619   if (TREE_CODE (decl) == TREE_LIST)
14620     {
14621       if (ambiguous_p)
14622         *ambiguous_p = true;
14623       /* The error message we have to print is too complicated for
14624          cp_parser_error, so we incorporate its actions directly.  */
14625       if (!cp_parser_simulate_error (parser))
14626         {
14627           error ("reference to %qD is ambiguous", name);
14628           print_candidates (decl);
14629         }
14630       return error_mark_node;
14631     }
14632
14633   gcc_assert (DECL_P (decl)
14634               || TREE_CODE (decl) == OVERLOAD
14635               || TREE_CODE (decl) == SCOPE_REF
14636               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14637               || BASELINK_P (decl));
14638
14639   /* If we have resolved the name of a member declaration, check to
14640      see if the declaration is accessible.  When the name resolves to
14641      set of overloaded functions, accessibility is checked when
14642      overload resolution is done.
14643
14644      During an explicit instantiation, access is not checked at all,
14645      as per [temp.explicit].  */
14646   if (DECL_P (decl))
14647     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14648
14649   return decl;
14650 }
14651
14652 /* Like cp_parser_lookup_name, but for use in the typical case where
14653    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14654    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14655
14656 static tree
14657 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14658 {
14659   return cp_parser_lookup_name (parser, name,
14660                                 none_type,
14661                                 /*is_template=*/false,
14662                                 /*is_namespace=*/false,
14663                                 /*check_dependency=*/true,
14664                                 /*ambiguous_p=*/NULL);
14665 }
14666
14667 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14668    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14669    true, the DECL indicates the class being defined in a class-head,
14670    or declared in an elaborated-type-specifier.
14671
14672    Otherwise, return DECL.  */
14673
14674 static tree
14675 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14676 {
14677   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14678      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14679
14680        struct A {
14681          template <typename T> struct B;
14682        };
14683
14684        template <typename T> struct A::B {};
14685
14686      Similarly, in an elaborated-type-specifier:
14687
14688        namespace N { struct X{}; }
14689
14690        struct A {
14691          template <typename T> friend struct N::X;
14692        };
14693
14694      However, if the DECL refers to a class type, and we are in
14695      the scope of the class, then the name lookup automatically
14696      finds the TYPE_DECL created by build_self_reference rather
14697      than a TEMPLATE_DECL.  For example, in:
14698
14699        template <class T> struct S {
14700          S s;
14701        };
14702
14703      there is no need to handle such case.  */
14704
14705   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14706     return DECL_TEMPLATE_RESULT (decl);
14707
14708   return decl;
14709 }
14710
14711 /* If too many, or too few, template-parameter lists apply to the
14712    declarator, issue an error message.  Returns TRUE if all went well,
14713    and FALSE otherwise.  */
14714
14715 static bool
14716 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14717                                                 cp_declarator *declarator)
14718 {
14719   unsigned num_templates;
14720
14721   /* We haven't seen any classes that involve template parameters yet.  */
14722   num_templates = 0;
14723
14724   switch (declarator->kind)
14725     {
14726     case cdk_id:
14727       if (declarator->u.id.qualifying_scope)
14728         {
14729           tree scope;
14730           tree member;
14731
14732           scope = declarator->u.id.qualifying_scope;
14733           member = declarator->u.id.unqualified_name;
14734
14735           while (scope && CLASS_TYPE_P (scope))
14736             {
14737               /* You're supposed to have one `template <...>'
14738                  for every template class, but you don't need one
14739                  for a full specialization.  For example:
14740
14741                  template <class T> struct S{};
14742                  template <> struct S<int> { void f(); };
14743                  void S<int>::f () {}
14744
14745                  is correct; there shouldn't be a `template <>' for
14746                  the definition of `S<int>::f'.  */
14747               if (CLASSTYPE_TEMPLATE_INFO (scope)
14748                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14749                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14750                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14751                 ++num_templates;
14752
14753               scope = TYPE_CONTEXT (scope);
14754             }
14755         }
14756       else if (TREE_CODE (declarator->u.id.unqualified_name)
14757                == TEMPLATE_ID_EXPR)
14758         /* If the DECLARATOR has the form `X<y>' then it uses one
14759            additional level of template parameters.  */
14760         ++num_templates;
14761
14762       return cp_parser_check_template_parameters (parser,
14763                                                   num_templates);
14764
14765     case cdk_function:
14766     case cdk_array:
14767     case cdk_pointer:
14768     case cdk_reference:
14769     case cdk_ptrmem:
14770       return (cp_parser_check_declarator_template_parameters
14771               (parser, declarator->declarator));
14772
14773     case cdk_error:
14774       return true;
14775
14776     default:
14777       gcc_unreachable ();
14778     }
14779   return false;
14780 }
14781
14782 /* NUM_TEMPLATES were used in the current declaration.  If that is
14783    invalid, return FALSE and issue an error messages.  Otherwise,
14784    return TRUE.  */
14785
14786 static bool
14787 cp_parser_check_template_parameters (cp_parser* parser,
14788                                      unsigned num_templates)
14789 {
14790   /* If there are more template classes than parameter lists, we have
14791      something like:
14792
14793        template <class T> void S<T>::R<T>::f ();  */
14794   if (parser->num_template_parameter_lists < num_templates)
14795     {
14796       error ("too few template-parameter-lists");
14797       return false;
14798     }
14799   /* If there are the same number of template classes and parameter
14800      lists, that's OK.  */
14801   if (parser->num_template_parameter_lists == num_templates)
14802     return true;
14803   /* If there are more, but only one more, then we are referring to a
14804      member template.  That's OK too.  */
14805   if (parser->num_template_parameter_lists == num_templates + 1)
14806       return true;
14807   /* Otherwise, there are too many template parameter lists.  We have
14808      something like:
14809
14810      template <class T> template <class U> void S::f();  */
14811   error ("too many template-parameter-lists");
14812   return false;
14813 }
14814
14815 /* Parse an optional `::' token indicating that the following name is
14816    from the global namespace.  If so, PARSER->SCOPE is set to the
14817    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14818    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14819    Returns the new value of PARSER->SCOPE, if the `::' token is
14820    present, and NULL_TREE otherwise.  */
14821
14822 static tree
14823 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14824 {
14825   cp_token *token;
14826
14827   /* Peek at the next token.  */
14828   token = cp_lexer_peek_token (parser->lexer);
14829   /* If we're looking at a `::' token then we're starting from the
14830      global namespace, not our current location.  */
14831   if (token->type == CPP_SCOPE)
14832     {
14833       /* Consume the `::' token.  */
14834       cp_lexer_consume_token (parser->lexer);
14835       /* Set the SCOPE so that we know where to start the lookup.  */
14836       parser->scope = global_namespace;
14837       parser->qualifying_scope = global_namespace;
14838       parser->object_scope = NULL_TREE;
14839
14840       return parser->scope;
14841     }
14842   else if (!current_scope_valid_p)
14843     {
14844       parser->scope = NULL_TREE;
14845       parser->qualifying_scope = NULL_TREE;
14846       parser->object_scope = NULL_TREE;
14847     }
14848
14849   return NULL_TREE;
14850 }
14851
14852 /* Returns TRUE if the upcoming token sequence is the start of a
14853    constructor declarator.  If FRIEND_P is true, the declarator is
14854    preceded by the `friend' specifier.  */
14855
14856 static bool
14857 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14858 {
14859   bool constructor_p;
14860   tree type_decl = NULL_TREE;
14861   bool nested_name_p;
14862   cp_token *next_token;
14863
14864   /* The common case is that this is not a constructor declarator, so
14865      try to avoid doing lots of work if at all possible.  It's not
14866      valid declare a constructor at function scope.  */
14867   if (at_function_scope_p ())
14868     return false;
14869   /* And only certain tokens can begin a constructor declarator.  */
14870   next_token = cp_lexer_peek_token (parser->lexer);
14871   if (next_token->type != CPP_NAME
14872       && next_token->type != CPP_SCOPE
14873       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14874       && next_token->type != CPP_TEMPLATE_ID)
14875     return false;
14876
14877   /* Parse tentatively; we are going to roll back all of the tokens
14878      consumed here.  */
14879   cp_parser_parse_tentatively (parser);
14880   /* Assume that we are looking at a constructor declarator.  */
14881   constructor_p = true;
14882
14883   /* Look for the optional `::' operator.  */
14884   cp_parser_global_scope_opt (parser,
14885                               /*current_scope_valid_p=*/false);
14886   /* Look for the nested-name-specifier.  */
14887   nested_name_p
14888     = (cp_parser_nested_name_specifier_opt (parser,
14889                                             /*typename_keyword_p=*/false,
14890                                             /*check_dependency_p=*/false,
14891                                             /*type_p=*/false,
14892                                             /*is_declaration=*/false)
14893        != NULL_TREE);
14894   /* Outside of a class-specifier, there must be a
14895      nested-name-specifier.  */
14896   if (!nested_name_p &&
14897       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14898        || friend_p))
14899     constructor_p = false;
14900   /* If we still think that this might be a constructor-declarator,
14901      look for a class-name.  */
14902   if (constructor_p)
14903     {
14904       /* If we have:
14905
14906            template <typename T> struct S { S(); };
14907            template <typename T> S<T>::S ();
14908
14909          we must recognize that the nested `S' names a class.
14910          Similarly, for:
14911
14912            template <typename T> S<T>::S<T> ();
14913
14914          we must recognize that the nested `S' names a template.  */
14915       type_decl = cp_parser_class_name (parser,
14916                                         /*typename_keyword_p=*/false,
14917                                         /*template_keyword_p=*/false,
14918                                         none_type,
14919                                         /*check_dependency_p=*/false,
14920                                         /*class_head_p=*/false,
14921                                         /*is_declaration=*/false);
14922       /* If there was no class-name, then this is not a constructor.  */
14923       constructor_p = !cp_parser_error_occurred (parser);
14924     }
14925
14926   /* If we're still considering a constructor, we have to see a `(',
14927      to begin the parameter-declaration-clause, followed by either a
14928      `)', an `...', or a decl-specifier.  We need to check for a
14929      type-specifier to avoid being fooled into thinking that:
14930
14931        S::S (f) (int);
14932
14933      is a constructor.  (It is actually a function named `f' that
14934      takes one parameter (of type `int') and returns a value of type
14935      `S::S'.  */
14936   if (constructor_p
14937       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14938     {
14939       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14940           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14941           /* A parameter declaration begins with a decl-specifier,
14942              which is either the "attribute" keyword, a storage class
14943              specifier, or (usually) a type-specifier.  */
14944           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14945           && !cp_parser_storage_class_specifier_opt (parser))
14946         {
14947           tree type;
14948           tree pushed_scope = NULL_TREE;
14949           unsigned saved_num_template_parameter_lists;
14950
14951           /* Names appearing in the type-specifier should be looked up
14952              in the scope of the class.  */
14953           if (current_class_type)
14954             type = NULL_TREE;
14955           else
14956             {
14957               type = TREE_TYPE (type_decl);
14958               if (TREE_CODE (type) == TYPENAME_TYPE)
14959                 {
14960                   type = resolve_typename_type (type,
14961                                                 /*only_current_p=*/false);
14962                   if (type == error_mark_node)
14963                     {
14964                       cp_parser_abort_tentative_parse (parser);
14965                       return false;
14966                     }
14967                 }
14968               pushed_scope = push_scope (type);
14969             }
14970
14971           /* Inside the constructor parameter list, surrounding
14972              template-parameter-lists do not apply.  */
14973           saved_num_template_parameter_lists
14974             = parser->num_template_parameter_lists;
14975           parser->num_template_parameter_lists = 0;
14976
14977           /* Look for the type-specifier.  */
14978           cp_parser_type_specifier (parser,
14979                                     CP_PARSER_FLAGS_NONE,
14980                                     /*decl_specs=*/NULL,
14981                                     /*is_declarator=*/true,
14982                                     /*declares_class_or_enum=*/NULL,
14983                                     /*is_cv_qualifier=*/NULL);
14984
14985           parser->num_template_parameter_lists
14986             = saved_num_template_parameter_lists;
14987
14988           /* Leave the scope of the class.  */
14989           if (pushed_scope)
14990             pop_scope (pushed_scope);
14991
14992           constructor_p = !cp_parser_error_occurred (parser);
14993         }
14994     }
14995   else
14996     constructor_p = false;
14997   /* We did not really want to consume any tokens.  */
14998   cp_parser_abort_tentative_parse (parser);
14999
15000   return constructor_p;
15001 }
15002
15003 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15004    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15005    they must be performed once we are in the scope of the function.
15006
15007    Returns the function defined.  */
15008
15009 static tree
15010 cp_parser_function_definition_from_specifiers_and_declarator
15011   (cp_parser* parser,
15012    cp_decl_specifier_seq *decl_specifiers,
15013    tree attributes,
15014    const cp_declarator *declarator)
15015 {
15016   tree fn;
15017   bool success_p;
15018
15019   /* Begin the function-definition.  */
15020   success_p = start_function (decl_specifiers, declarator, attributes);
15021
15022   /* The things we're about to see are not directly qualified by any
15023      template headers we've seen thus far.  */
15024   reset_specialization ();
15025
15026   /* If there were names looked up in the decl-specifier-seq that we
15027      did not check, check them now.  We must wait until we are in the
15028      scope of the function to perform the checks, since the function
15029      might be a friend.  */
15030   perform_deferred_access_checks ();
15031
15032   if (!success_p)
15033     {
15034       /* Skip the entire function.  */
15035       error ("invalid function declaration");
15036       cp_parser_skip_to_end_of_block_or_statement (parser);
15037       fn = error_mark_node;
15038     }
15039   else
15040     fn = cp_parser_function_definition_after_declarator (parser,
15041                                                          /*inline_p=*/false);
15042
15043   return fn;
15044 }
15045
15046 /* Parse the part of a function-definition that follows the
15047    declarator.  INLINE_P is TRUE iff this function is an inline
15048    function defined with a class-specifier.
15049
15050    Returns the function defined.  */
15051
15052 static tree
15053 cp_parser_function_definition_after_declarator (cp_parser* parser,
15054                                                 bool inline_p)
15055 {
15056   tree fn;
15057   bool ctor_initializer_p = false;
15058   bool saved_in_unbraced_linkage_specification_p;
15059   unsigned saved_num_template_parameter_lists;
15060
15061   /* If the next token is `return', then the code may be trying to
15062      make use of the "named return value" extension that G++ used to
15063      support.  */
15064   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15065     {
15066       /* Consume the `return' keyword.  */
15067       cp_lexer_consume_token (parser->lexer);
15068       /* Look for the identifier that indicates what value is to be
15069          returned.  */
15070       cp_parser_identifier (parser);
15071       /* Issue an error message.  */
15072       error ("named return values are no longer supported");
15073       /* Skip tokens until we reach the start of the function body.  */
15074       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
15075              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
15076         cp_lexer_consume_token (parser->lexer);
15077     }
15078   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15079      anything declared inside `f'.  */
15080   saved_in_unbraced_linkage_specification_p
15081     = parser->in_unbraced_linkage_specification_p;
15082   parser->in_unbraced_linkage_specification_p = false;
15083   /* Inside the function, surrounding template-parameter-lists do not
15084      apply.  */
15085   saved_num_template_parameter_lists
15086     = parser->num_template_parameter_lists;
15087   parser->num_template_parameter_lists = 0;
15088   /* If the next token is `try', then we are looking at a
15089      function-try-block.  */
15090   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15091     ctor_initializer_p = cp_parser_function_try_block (parser);
15092   /* A function-try-block includes the function-body, so we only do
15093      this next part if we're not processing a function-try-block.  */
15094   else
15095     ctor_initializer_p
15096       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15097
15098   /* Finish the function.  */
15099   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15100                         (inline_p ? 2 : 0));
15101   /* Generate code for it, if necessary.  */
15102   expand_or_defer_fn (fn);
15103   /* Restore the saved values.  */
15104   parser->in_unbraced_linkage_specification_p
15105     = saved_in_unbraced_linkage_specification_p;
15106   parser->num_template_parameter_lists
15107     = saved_num_template_parameter_lists;
15108
15109   return fn;
15110 }
15111
15112 /* Parse a template-declaration, assuming that the `export' (and
15113    `extern') keywords, if present, has already been scanned.  MEMBER_P
15114    is as for cp_parser_template_declaration.  */
15115
15116 static void
15117 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15118 {
15119   tree decl = NULL_TREE;
15120   tree parameter_list;
15121   bool friend_p = false;
15122
15123   /* Look for the `template' keyword.  */
15124   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15125     return;
15126
15127   /* And the `<'.  */
15128   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15129     return;
15130
15131   /* If the next token is `>', then we have an invalid
15132      specialization.  Rather than complain about an invalid template
15133      parameter, issue an error message here.  */
15134   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15135     {
15136       cp_parser_error (parser, "invalid explicit specialization");
15137       begin_specialization ();
15138       parameter_list = NULL_TREE;
15139     }
15140   else
15141     {
15142       /* Parse the template parameters.  */
15143       begin_template_parm_list ();
15144       parameter_list = cp_parser_template_parameter_list (parser);
15145       parameter_list = end_template_parm_list (parameter_list);
15146     }
15147
15148   /* Look for the `>'.  */
15149   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15150   /* We just processed one more parameter list.  */
15151   ++parser->num_template_parameter_lists;
15152   /* If the next token is `template', there are more template
15153      parameters.  */
15154   if (cp_lexer_next_token_is_keyword (parser->lexer,
15155                                       RID_TEMPLATE))
15156     cp_parser_template_declaration_after_export (parser, member_p);
15157   else
15158     {
15159       /* There are no access checks when parsing a template, as we do not
15160          know if a specialization will be a friend.  */
15161       push_deferring_access_checks (dk_no_check);
15162
15163       decl = cp_parser_single_declaration (parser,
15164                                            member_p,
15165                                            &friend_p);
15166
15167       pop_deferring_access_checks ();
15168
15169       /* If this is a member template declaration, let the front
15170          end know.  */
15171       if (member_p && !friend_p && decl)
15172         {
15173           if (TREE_CODE (decl) == TYPE_DECL)
15174             cp_parser_check_access_in_redeclaration (decl);
15175
15176           decl = finish_member_template_decl (decl);
15177         }
15178       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15179         make_friend_class (current_class_type, TREE_TYPE (decl),
15180                            /*complain=*/true);
15181     }
15182   /* We are done with the current parameter list.  */
15183   --parser->num_template_parameter_lists;
15184
15185   /* Finish up.  */
15186   finish_template_decl (parameter_list);
15187
15188   /* Register member declarations.  */
15189   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15190     finish_member_declaration (decl);
15191
15192   /* If DECL is a function template, we must return to parse it later.
15193      (Even though there is no definition, there might be default
15194      arguments that need handling.)  */
15195   if (member_p && decl
15196       && (TREE_CODE (decl) == FUNCTION_DECL
15197           || DECL_FUNCTION_TEMPLATE_P (decl)))
15198     TREE_VALUE (parser->unparsed_functions_queues)
15199       = tree_cons (NULL_TREE, decl,
15200                    TREE_VALUE (parser->unparsed_functions_queues));
15201 }
15202
15203 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15204    `function-definition' sequence.  MEMBER_P is true, this declaration
15205    appears in a class scope.
15206
15207    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15208    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15209
15210 static tree
15211 cp_parser_single_declaration (cp_parser* parser,
15212                               bool member_p,
15213                               bool* friend_p)
15214 {
15215   int declares_class_or_enum;
15216   tree decl = NULL_TREE;
15217   cp_decl_specifier_seq decl_specifiers;
15218   bool function_definition_p = false;
15219
15220   /* This function is only used when processing a template
15221      declaration.  */
15222   gcc_assert (innermost_scope_kind () == sk_template_parms
15223               || innermost_scope_kind () == sk_template_spec);
15224
15225   /* Defer access checks until we know what is being declared.  */
15226   push_deferring_access_checks (dk_deferred);
15227
15228   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15229      alternative.  */
15230   cp_parser_decl_specifier_seq (parser,
15231                                 CP_PARSER_FLAGS_OPTIONAL,
15232                                 &decl_specifiers,
15233                                 &declares_class_or_enum);
15234   if (friend_p)
15235     *friend_p = cp_parser_friend_p (&decl_specifiers);
15236
15237   /* There are no template typedefs.  */
15238   if (decl_specifiers.specs[(int) ds_typedef])
15239     {
15240       error ("template declaration of %qs", "typedef");
15241       decl = error_mark_node;
15242     }
15243
15244   /* Gather up the access checks that occurred the
15245      decl-specifier-seq.  */
15246   stop_deferring_access_checks ();
15247
15248   /* Check for the declaration of a template class.  */
15249   if (declares_class_or_enum)
15250     {
15251       if (cp_parser_declares_only_class_p (parser))
15252         {
15253           decl = shadow_tag (&decl_specifiers);
15254
15255           /* In this case:
15256
15257                struct C {
15258                  friend template <typename T> struct A<T>::B;
15259                };
15260
15261              A<T>::B will be represented by a TYPENAME_TYPE, and
15262              therefore not recognized by shadow_tag.  */
15263           if (friend_p && *friend_p
15264               && !decl
15265               && decl_specifiers.type
15266               && TYPE_P (decl_specifiers.type))
15267             decl = decl_specifiers.type;
15268
15269           if (decl && decl != error_mark_node)
15270             decl = TYPE_NAME (decl);
15271           else
15272             decl = error_mark_node;
15273         }
15274     }
15275   /* If it's not a template class, try for a template function.  If
15276      the next token is a `;', then this declaration does not declare
15277      anything.  But, if there were errors in the decl-specifiers, then
15278      the error might well have come from an attempted class-specifier.
15279      In that case, there's no need to warn about a missing declarator.  */
15280   if (!decl
15281       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15282           || decl_specifiers.type != error_mark_node))
15283     decl = cp_parser_init_declarator (parser,
15284                                       &decl_specifiers,
15285                                       /*function_definition_allowed_p=*/true,
15286                                       member_p,
15287                                       declares_class_or_enum,
15288                                       &function_definition_p);
15289
15290   pop_deferring_access_checks ();
15291
15292   /* Clear any current qualification; whatever comes next is the start
15293      of something new.  */
15294   parser->scope = NULL_TREE;
15295   parser->qualifying_scope = NULL_TREE;
15296   parser->object_scope = NULL_TREE;
15297   /* Look for a trailing `;' after the declaration.  */
15298   if (!function_definition_p
15299       && (decl == error_mark_node
15300           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15301     cp_parser_skip_to_end_of_block_or_statement (parser);
15302
15303   return decl;
15304 }
15305
15306 /* Parse a cast-expression that is not the operand of a unary "&".  */
15307
15308 static tree
15309 cp_parser_simple_cast_expression (cp_parser *parser)
15310 {
15311   return cp_parser_cast_expression (parser, /*address_p=*/false,
15312                                     /*cast_p=*/false);
15313 }
15314
15315 /* Parse a functional cast to TYPE.  Returns an expression
15316    representing the cast.  */
15317
15318 static tree
15319 cp_parser_functional_cast (cp_parser* parser, tree type)
15320 {
15321   tree expression_list;
15322   tree cast;
15323
15324   expression_list
15325     = cp_parser_parenthesized_expression_list (parser, false,
15326                                                /*cast_p=*/true,
15327                                                /*non_constant_p=*/NULL);
15328
15329   cast = build_functional_cast (type, expression_list);
15330   /* [expr.const]/1: In an integral constant expression "only type
15331      conversions to integral or enumeration type can be used".  */
15332   if (cast != error_mark_node && !type_dependent_expression_p (type)
15333       && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15334     {
15335       if (cp_parser_non_integral_constant_expression
15336           (parser, "a call to a constructor"))
15337         return error_mark_node;
15338     }
15339   return cast;
15340 }
15341
15342 /* Save the tokens that make up the body of a member function defined
15343    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15344    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15345    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15346    for the member function.  */
15347
15348 static tree
15349 cp_parser_save_member_function_body (cp_parser* parser,
15350                                      cp_decl_specifier_seq *decl_specifiers,
15351                                      cp_declarator *declarator,
15352                                      tree attributes)
15353 {
15354   cp_token *first;
15355   cp_token *last;
15356   tree fn;
15357
15358   /* Create the function-declaration.  */
15359   fn = start_method (decl_specifiers, declarator, attributes);
15360   /* If something went badly wrong, bail out now.  */
15361   if (fn == error_mark_node)
15362     {
15363       /* If there's a function-body, skip it.  */
15364       if (cp_parser_token_starts_function_definition_p
15365           (cp_lexer_peek_token (parser->lexer)))
15366         cp_parser_skip_to_end_of_block_or_statement (parser);
15367       return error_mark_node;
15368     }
15369
15370   /* Remember it, if there default args to post process.  */
15371   cp_parser_save_default_args (parser, fn);
15372
15373   /* Save away the tokens that make up the body of the
15374      function.  */
15375   first = parser->lexer->next_token;
15376   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15377   /* Handle function try blocks.  */
15378   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15379     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15380   last = parser->lexer->next_token;
15381
15382   /* Save away the inline definition; we will process it when the
15383      class is complete.  */
15384   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15385   DECL_PENDING_INLINE_P (fn) = 1;
15386
15387   /* We need to know that this was defined in the class, so that
15388      friend templates are handled correctly.  */
15389   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15390
15391   /* We're done with the inline definition.  */
15392   finish_method (fn);
15393
15394   /* Add FN to the queue of functions to be parsed later.  */
15395   TREE_VALUE (parser->unparsed_functions_queues)
15396     = tree_cons (NULL_TREE, fn,
15397                  TREE_VALUE (parser->unparsed_functions_queues));
15398
15399   return fn;
15400 }
15401
15402 /* Parse a template-argument-list, as well as the trailing ">" (but
15403    not the opening ">").  See cp_parser_template_argument_list for the
15404    return value.  */
15405
15406 static tree
15407 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15408 {
15409   tree arguments;
15410   tree saved_scope;
15411   tree saved_qualifying_scope;
15412   tree saved_object_scope;
15413   bool saved_greater_than_is_operator_p;
15414
15415   /* [temp.names]
15416
15417      When parsing a template-id, the first non-nested `>' is taken as
15418      the end of the template-argument-list rather than a greater-than
15419      operator.  */
15420   saved_greater_than_is_operator_p
15421     = parser->greater_than_is_operator_p;
15422   parser->greater_than_is_operator_p = false;
15423   /* Parsing the argument list may modify SCOPE, so we save it
15424      here.  */
15425   saved_scope = parser->scope;
15426   saved_qualifying_scope = parser->qualifying_scope;
15427   saved_object_scope = parser->object_scope;
15428   /* Parse the template-argument-list itself.  */
15429   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15430     arguments = NULL_TREE;
15431   else
15432     arguments = cp_parser_template_argument_list (parser);
15433   /* Look for the `>' that ends the template-argument-list. If we find
15434      a '>>' instead, it's probably just a typo.  */
15435   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15436     {
15437       if (!saved_greater_than_is_operator_p)
15438         {
15439           /* If we're in a nested template argument list, the '>>' has
15440             to be a typo for '> >'. We emit the error message, but we
15441             continue parsing and we push a '>' as next token, so that
15442             the argument list will be parsed correctly.  Note that the
15443             global source location is still on the token before the
15444             '>>', so we need to say explicitly where we want it.  */
15445           cp_token *token = cp_lexer_peek_token (parser->lexer);
15446           error ("%H%<>>%> should be %<> >%> "
15447                  "within a nested template argument list",
15448                  &token->location);
15449
15450           /* ??? Proper recovery should terminate two levels of
15451              template argument list here.  */
15452           token->type = CPP_GREATER;
15453         }
15454       else
15455         {
15456           /* If this is not a nested template argument list, the '>>'
15457             is a typo for '>'. Emit an error message and continue.
15458             Same deal about the token location, but here we can get it
15459             right by consuming the '>>' before issuing the diagnostic.  */
15460           cp_lexer_consume_token (parser->lexer);
15461           error ("spurious %<>>%>, use %<>%> to terminate "
15462                  "a template argument list");
15463         }
15464     }
15465   else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15466     error ("missing %<>%> to terminate the template argument list");
15467   else
15468     /* It's what we want, a '>'; consume it.  */
15469     cp_lexer_consume_token (parser->lexer);
15470   /* The `>' token might be a greater-than operator again now.  */
15471   parser->greater_than_is_operator_p
15472     = saved_greater_than_is_operator_p;
15473   /* Restore the SAVED_SCOPE.  */
15474   parser->scope = saved_scope;
15475   parser->qualifying_scope = saved_qualifying_scope;
15476   parser->object_scope = saved_object_scope;
15477
15478   return arguments;
15479 }
15480
15481 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15482    arguments, or the body of the function have not yet been parsed,
15483    parse them now.  */
15484
15485 static void
15486 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15487 {
15488   /* If this member is a template, get the underlying
15489      FUNCTION_DECL.  */
15490   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15491     member_function = DECL_TEMPLATE_RESULT (member_function);
15492
15493   /* There should not be any class definitions in progress at this
15494      point; the bodies of members are only parsed outside of all class
15495      definitions.  */
15496   gcc_assert (parser->num_classes_being_defined == 0);
15497   /* While we're parsing the member functions we might encounter more
15498      classes.  We want to handle them right away, but we don't want
15499      them getting mixed up with functions that are currently in the
15500      queue.  */
15501   parser->unparsed_functions_queues
15502     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15503
15504   /* Make sure that any template parameters are in scope.  */
15505   maybe_begin_member_template_processing (member_function);
15506
15507   /* If the body of the function has not yet been parsed, parse it
15508      now.  */
15509   if (DECL_PENDING_INLINE_P (member_function))
15510     {
15511       tree function_scope;
15512       cp_token_cache *tokens;
15513
15514       /* The function is no longer pending; we are processing it.  */
15515       tokens = DECL_PENDING_INLINE_INFO (member_function);
15516       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15517       DECL_PENDING_INLINE_P (member_function) = 0;
15518
15519       /* If this is a local class, enter the scope of the containing
15520          function.  */
15521       function_scope = current_function_decl;
15522       if (function_scope)
15523         push_function_context_to (function_scope);
15524
15525
15526       /* Push the body of the function onto the lexer stack.  */
15527       cp_parser_push_lexer_for_tokens (parser, tokens);
15528
15529       /* Let the front end know that we going to be defining this
15530          function.  */
15531       start_preparsed_function (member_function, NULL_TREE,
15532                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15533
15534       /* Don't do access checking if it is a templated function.  */
15535       if (processing_template_decl)
15536         push_deferring_access_checks (dk_no_check);
15537
15538       /* Now, parse the body of the function.  */
15539       cp_parser_function_definition_after_declarator (parser,
15540                                                       /*inline_p=*/true);
15541
15542       if (processing_template_decl)
15543         pop_deferring_access_checks ();
15544
15545       /* Leave the scope of the containing function.  */
15546       if (function_scope)
15547         pop_function_context_from (function_scope);
15548       cp_parser_pop_lexer (parser);
15549     }
15550
15551   /* Remove any template parameters from the symbol table.  */
15552   maybe_end_member_template_processing ();
15553
15554   /* Restore the queue.  */
15555   parser->unparsed_functions_queues
15556     = TREE_CHAIN (parser->unparsed_functions_queues);
15557 }
15558
15559 /* If DECL contains any default args, remember it on the unparsed
15560    functions queue.  */
15561
15562 static void
15563 cp_parser_save_default_args (cp_parser* parser, tree decl)
15564 {
15565   tree probe;
15566
15567   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15568        probe;
15569        probe = TREE_CHAIN (probe))
15570     if (TREE_PURPOSE (probe))
15571       {
15572         TREE_PURPOSE (parser->unparsed_functions_queues)
15573           = tree_cons (current_class_type, decl,
15574                        TREE_PURPOSE (parser->unparsed_functions_queues));
15575         break;
15576       }
15577   return;
15578 }
15579
15580 /* FN is a FUNCTION_DECL which may contains a parameter with an
15581    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15582    assumes that the current scope is the scope in which the default
15583    argument should be processed.  */
15584
15585 static void
15586 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15587 {
15588   bool saved_local_variables_forbidden_p;
15589   tree parm;
15590
15591   /* While we're parsing the default args, we might (due to the
15592      statement expression extension) encounter more classes.  We want
15593      to handle them right away, but we don't want them getting mixed
15594      up with default args that are currently in the queue.  */
15595   parser->unparsed_functions_queues
15596     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15597
15598   /* Local variable names (and the `this' keyword) may not appear
15599      in a default argument.  */
15600   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15601   parser->local_variables_forbidden_p = true;
15602
15603   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15604        parm;
15605        parm = TREE_CHAIN (parm))
15606     {
15607       cp_token_cache *tokens;
15608       tree default_arg = TREE_PURPOSE (parm);
15609       tree parsed_arg;
15610       VEC(tree,gc) *insts;
15611       tree copy;
15612       unsigned ix;
15613
15614       if (!default_arg)
15615         continue;
15616
15617       if (TREE_CODE (default_arg) != DEFAULT_ARG)
15618         /* This can happen for a friend declaration for a function
15619            already declared with default arguments.  */
15620         continue;
15621
15622        /* Push the saved tokens for the default argument onto the parser's
15623           lexer stack.  */
15624       tokens = DEFARG_TOKENS (default_arg);
15625       cp_parser_push_lexer_for_tokens (parser, tokens);
15626
15627       /* Parse the assignment-expression.  */
15628       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15629
15630       TREE_PURPOSE (parm) = parsed_arg;
15631
15632       /* Update any instantiations we've already created.  */
15633       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15634            VEC_iterate (tree, insts, ix, copy); ix++)
15635         TREE_PURPOSE (copy) = parsed_arg;
15636
15637       /* If the token stream has not been completely used up, then
15638          there was extra junk after the end of the default
15639          argument.  */
15640       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15641         cp_parser_error (parser, "expected %<,%>");
15642
15643       /* Revert to the main lexer.  */
15644       cp_parser_pop_lexer (parser);
15645     }
15646
15647   /* Restore the state of local_variables_forbidden_p.  */
15648   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15649
15650   /* Restore the queue.  */
15651   parser->unparsed_functions_queues
15652     = TREE_CHAIN (parser->unparsed_functions_queues);
15653 }
15654
15655 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15656    either a TYPE or an expression, depending on the form of the
15657    input.  The KEYWORD indicates which kind of expression we have
15658    encountered.  */
15659
15660 static tree
15661 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15662 {
15663   static const char *format;
15664   tree expr = NULL_TREE;
15665   const char *saved_message;
15666   bool saved_integral_constant_expression_p;
15667   bool saved_non_integral_constant_expression_p;
15668
15669   /* Initialize FORMAT the first time we get here.  */
15670   if (!format)
15671     format = "types may not be defined in '%s' expressions";
15672
15673   /* Types cannot be defined in a `sizeof' expression.  Save away the
15674      old message.  */
15675   saved_message = parser->type_definition_forbidden_message;
15676   /* And create the new one.  */
15677   parser->type_definition_forbidden_message
15678     = xmalloc (strlen (format)
15679                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15680                + 1 /* `\0' */);
15681   sprintf ((char *) parser->type_definition_forbidden_message,
15682            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15683
15684   /* The restrictions on constant-expressions do not apply inside
15685      sizeof expressions.  */
15686   saved_integral_constant_expression_p
15687     = parser->integral_constant_expression_p;
15688   saved_non_integral_constant_expression_p
15689     = parser->non_integral_constant_expression_p;
15690   parser->integral_constant_expression_p = false;
15691
15692   /* Do not actually evaluate the expression.  */
15693   ++skip_evaluation;
15694   /* If it's a `(', then we might be looking at the type-id
15695      construction.  */
15696   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15697     {
15698       tree type;
15699       bool saved_in_type_id_in_expr_p;
15700
15701       /* We can't be sure yet whether we're looking at a type-id or an
15702          expression.  */
15703       cp_parser_parse_tentatively (parser);
15704       /* Consume the `('.  */
15705       cp_lexer_consume_token (parser->lexer);
15706       /* Parse the type-id.  */
15707       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15708       parser->in_type_id_in_expr_p = true;
15709       type = cp_parser_type_id (parser);
15710       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15711       /* Now, look for the trailing `)'.  */
15712       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15713       /* If all went well, then we're done.  */
15714       if (cp_parser_parse_definitely (parser))
15715         {
15716           cp_decl_specifier_seq decl_specs;
15717
15718           /* Build a trivial decl-specifier-seq.  */
15719           clear_decl_specs (&decl_specs);
15720           decl_specs.type = type;
15721
15722           /* Call grokdeclarator to figure out what type this is.  */
15723           expr = grokdeclarator (NULL,
15724                                  &decl_specs,
15725                                  TYPENAME,
15726                                  /*initialized=*/0,
15727                                  /*attrlist=*/NULL);
15728         }
15729     }
15730
15731   /* If the type-id production did not work out, then we must be
15732      looking at the unary-expression production.  */
15733   if (!expr)
15734     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15735                                        /*cast_p=*/false);
15736   /* Go back to evaluating expressions.  */
15737   --skip_evaluation;
15738
15739   /* Free the message we created.  */
15740   free ((char *) parser->type_definition_forbidden_message);
15741   /* And restore the old one.  */
15742   parser->type_definition_forbidden_message = saved_message;
15743   parser->integral_constant_expression_p
15744     = saved_integral_constant_expression_p;
15745   parser->non_integral_constant_expression_p
15746     = saved_non_integral_constant_expression_p;
15747
15748   return expr;
15749 }
15750
15751 /* If the current declaration has no declarator, return true.  */
15752
15753 static bool
15754 cp_parser_declares_only_class_p (cp_parser *parser)
15755 {
15756   /* If the next token is a `;' or a `,' then there is no
15757      declarator.  */
15758   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15759           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15760 }
15761
15762 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15763
15764 static void
15765 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15766                              cp_storage_class storage_class)
15767 {
15768   if (decl_specs->storage_class != sc_none)
15769     decl_specs->multiple_storage_classes_p = true;
15770   else
15771     decl_specs->storage_class = storage_class;
15772 }
15773
15774 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15775    is true, the type is a user-defined type; otherwise it is a
15776    built-in type specified by a keyword.  */
15777
15778 static void
15779 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15780                               tree type_spec,
15781                               bool user_defined_p)
15782 {
15783   decl_specs->any_specifiers_p = true;
15784
15785   /* If the user tries to redeclare bool or wchar_t (with, for
15786      example, in "typedef int wchar_t;") we remember that this is what
15787      happened.  In system headers, we ignore these declarations so
15788      that G++ can work with system headers that are not C++-safe.  */
15789   if (decl_specs->specs[(int) ds_typedef]
15790       && !user_defined_p
15791       && (type_spec == boolean_type_node
15792           || type_spec == wchar_type_node)
15793       && (decl_specs->type
15794           || decl_specs->specs[(int) ds_long]
15795           || decl_specs->specs[(int) ds_short]
15796           || decl_specs->specs[(int) ds_unsigned]
15797           || decl_specs->specs[(int) ds_signed]))
15798     {
15799       decl_specs->redefined_builtin_type = type_spec;
15800       if (!decl_specs->type)
15801         {
15802           decl_specs->type = type_spec;
15803           decl_specs->user_defined_type_p = false;
15804         }
15805     }
15806   else if (decl_specs->type)
15807     decl_specs->multiple_types_p = true;
15808   else
15809     {
15810       decl_specs->type = type_spec;
15811       decl_specs->user_defined_type_p = user_defined_p;
15812       decl_specs->redefined_builtin_type = NULL_TREE;
15813     }
15814 }
15815
15816 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15817    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15818
15819 static bool
15820 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15821 {
15822   return decl_specifiers->specs[(int) ds_friend] != 0;
15823 }
15824
15825 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15826    issue an error message indicating that TOKEN_DESC was expected.
15827
15828    Returns the token consumed, if the token had the appropriate type.
15829    Otherwise, returns NULL.  */
15830
15831 static cp_token *
15832 cp_parser_require (cp_parser* parser,
15833                    enum cpp_ttype type,
15834                    const char* token_desc)
15835 {
15836   if (cp_lexer_next_token_is (parser->lexer, type))
15837     return cp_lexer_consume_token (parser->lexer);
15838   else
15839     {
15840       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15841       if (!cp_parser_simulate_error (parser))
15842         {
15843           char *message = concat ("expected ", token_desc, NULL);
15844           cp_parser_error (parser, message);
15845           free (message);
15846         }
15847       return NULL;
15848     }
15849 }
15850
15851 /* Like cp_parser_require, except that tokens will be skipped until
15852    the desired token is found.  An error message is still produced if
15853    the next token is not as expected.  */
15854
15855 static void
15856 cp_parser_skip_until_found (cp_parser* parser,
15857                             enum cpp_ttype type,
15858                             const char* token_desc)
15859 {
15860   cp_token *token;
15861   unsigned nesting_depth = 0;
15862
15863   if (cp_parser_require (parser, type, token_desc))
15864     return;
15865
15866   /* Skip tokens until the desired token is found.  */
15867   while (true)
15868     {
15869       /* Peek at the next token.  */
15870       token = cp_lexer_peek_token (parser->lexer);
15871       /* If we've reached the token we want, consume it and
15872          stop.  */
15873       if (token->type == type && !nesting_depth)
15874         {
15875           cp_lexer_consume_token (parser->lexer);
15876           return;
15877         }
15878       /* If we've run out of tokens, stop.  */
15879       if (token->type == CPP_EOF)
15880         return;
15881       if (token->type == CPP_OPEN_BRACE
15882           || token->type == CPP_OPEN_PAREN
15883           || token->type == CPP_OPEN_SQUARE)
15884         ++nesting_depth;
15885       else if (token->type == CPP_CLOSE_BRACE
15886                || token->type == CPP_CLOSE_PAREN
15887                || token->type == CPP_CLOSE_SQUARE)
15888         {
15889           if (nesting_depth-- == 0)
15890             return;
15891         }
15892       /* Consume this token.  */
15893       cp_lexer_consume_token (parser->lexer);
15894     }
15895 }
15896
15897 /* If the next token is the indicated keyword, consume it.  Otherwise,
15898    issue an error message indicating that TOKEN_DESC was expected.
15899
15900    Returns the token consumed, if the token had the appropriate type.
15901    Otherwise, returns NULL.  */
15902
15903 static cp_token *
15904 cp_parser_require_keyword (cp_parser* parser,
15905                            enum rid keyword,
15906                            const char* token_desc)
15907 {
15908   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15909
15910   if (token && token->keyword != keyword)
15911     {
15912       dyn_string_t error_msg;
15913
15914       /* Format the error message.  */
15915       error_msg = dyn_string_new (0);
15916       dyn_string_append_cstr (error_msg, "expected ");
15917       dyn_string_append_cstr (error_msg, token_desc);
15918       cp_parser_error (parser, error_msg->s);
15919       dyn_string_delete (error_msg);
15920       return NULL;
15921     }
15922
15923   return token;
15924 }
15925
15926 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15927    function-definition.  */
15928
15929 static bool
15930 cp_parser_token_starts_function_definition_p (cp_token* token)
15931 {
15932   return (/* An ordinary function-body begins with an `{'.  */
15933           token->type == CPP_OPEN_BRACE
15934           /* A ctor-initializer begins with a `:'.  */
15935           || token->type == CPP_COLON
15936           /* A function-try-block begins with `try'.  */
15937           || token->keyword == RID_TRY
15938           /* The named return value extension begins with `return'.  */
15939           || token->keyword == RID_RETURN);
15940 }
15941
15942 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15943    definition.  */
15944
15945 static bool
15946 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15947 {
15948   cp_token *token;
15949
15950   token = cp_lexer_peek_token (parser->lexer);
15951   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15952 }
15953
15954 /* Returns TRUE iff the next token is the "," or ">" ending a
15955    template-argument.  */
15956
15957 static bool
15958 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15959 {
15960   cp_token *token;
15961
15962   token = cp_lexer_peek_token (parser->lexer);
15963   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15964 }
15965
15966 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15967    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15968
15969 static bool
15970 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15971                                                      size_t n)
15972 {
15973   cp_token *token;
15974
15975   token = cp_lexer_peek_nth_token (parser->lexer, n);
15976   if (token->type == CPP_LESS)
15977     return true;
15978   /* Check for the sequence `<::' in the original code. It would be lexed as
15979      `[:', where `[' is a digraph, and there is no whitespace before
15980      `:'.  */
15981   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15982     {
15983       cp_token *token2;
15984       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15985       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15986         return true;
15987     }
15988   return false;
15989 }
15990
15991 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15992    or none_type otherwise.  */
15993
15994 static enum tag_types
15995 cp_parser_token_is_class_key (cp_token* token)
15996 {
15997   switch (token->keyword)
15998     {
15999     case RID_CLASS:
16000       return class_type;
16001     case RID_STRUCT:
16002       return record_type;
16003     case RID_UNION:
16004       return union_type;
16005
16006     default:
16007       return none_type;
16008     }
16009 }
16010
16011 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16012
16013 static void
16014 cp_parser_check_class_key (enum tag_types class_key, tree type)
16015 {
16016   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16017     pedwarn ("%qs tag used in naming %q#T",
16018             class_key == union_type ? "union"
16019              : class_key == record_type ? "struct" : "class",
16020              type);
16021 }
16022
16023 /* Issue an error message if DECL is redeclared with different
16024    access than its original declaration [class.access.spec/3].
16025    This applies to nested classes and nested class templates.
16026    [class.mem/1].  */
16027
16028 static void
16029 cp_parser_check_access_in_redeclaration (tree decl)
16030 {
16031   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16032     return;
16033
16034   if ((TREE_PRIVATE (decl)
16035        != (current_access_specifier == access_private_node))
16036       || (TREE_PROTECTED (decl)
16037           != (current_access_specifier == access_protected_node)))
16038     error ("%qD redeclared with different access", decl);
16039 }
16040
16041 /* Look for the `template' keyword, as a syntactic disambiguator.
16042    Return TRUE iff it is present, in which case it will be
16043    consumed.  */
16044
16045 static bool
16046 cp_parser_optional_template_keyword (cp_parser *parser)
16047 {
16048   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16049     {
16050       /* The `template' keyword can only be used within templates;
16051          outside templates the parser can always figure out what is a
16052          template and what is not.  */
16053       if (!processing_template_decl)
16054         {
16055           error ("%<template%> (as a disambiguator) is only allowed "
16056                  "within templates");
16057           /* If this part of the token stream is rescanned, the same
16058              error message would be generated.  So, we purge the token
16059              from the stream.  */
16060           cp_lexer_purge_token (parser->lexer);
16061           return false;
16062         }
16063       else
16064         {
16065           /* Consume the `template' keyword.  */
16066           cp_lexer_consume_token (parser->lexer);
16067           return true;
16068         }
16069     }
16070
16071   return false;
16072 }
16073
16074 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16075    set PARSER->SCOPE, and perform other related actions.  */
16076
16077 static void
16078 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16079 {
16080   tree value;
16081   tree check;
16082
16083   /* Get the stored value.  */
16084   value = cp_lexer_consume_token (parser->lexer)->value;
16085   /* Perform any access checks that were deferred.  */
16086   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16087     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16088   /* Set the scope from the stored value.  */
16089   parser->scope = TREE_VALUE (value);
16090   parser->qualifying_scope = TREE_TYPE (value);
16091   parser->object_scope = NULL_TREE;
16092 }
16093
16094 /* Consume tokens up through a non-nested END token.  */
16095
16096 static void
16097 cp_parser_cache_group (cp_parser *parser,
16098                        enum cpp_ttype end,
16099                        unsigned depth)
16100 {
16101   while (true)
16102     {
16103       cp_token *token;
16104
16105       /* Abort a parenthesized expression if we encounter a brace.  */
16106       if ((end == CPP_CLOSE_PAREN || depth == 0)
16107           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16108         return;
16109       /* If we've reached the end of the file, stop.  */
16110       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16111         return;
16112       /* Consume the next token.  */
16113       token = cp_lexer_consume_token (parser->lexer);
16114       /* See if it starts a new group.  */
16115       if (token->type == CPP_OPEN_BRACE)
16116         {
16117           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16118           if (depth == 0)
16119             return;
16120         }
16121       else if (token->type == CPP_OPEN_PAREN)
16122         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16123       else if (token->type == end)
16124         return;
16125     }
16126 }
16127
16128 /* Begin parsing tentatively.  We always save tokens while parsing
16129    tentatively so that if the tentative parsing fails we can restore the
16130    tokens.  */
16131
16132 static void
16133 cp_parser_parse_tentatively (cp_parser* parser)
16134 {
16135   /* Enter a new parsing context.  */
16136   parser->context = cp_parser_context_new (parser->context);
16137   /* Begin saving tokens.  */
16138   cp_lexer_save_tokens (parser->lexer);
16139   /* In order to avoid repetitive access control error messages,
16140      access checks are queued up until we are no longer parsing
16141      tentatively.  */
16142   push_deferring_access_checks (dk_deferred);
16143 }
16144
16145 /* Commit to the currently active tentative parse.  */
16146
16147 static void
16148 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16149 {
16150   cp_parser_context *context;
16151   cp_lexer *lexer;
16152
16153   /* Mark all of the levels as committed.  */
16154   lexer = parser->lexer;
16155   for (context = parser->context; context->next; context = context->next)
16156     {
16157       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16158         break;
16159       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16160       while (!cp_lexer_saving_tokens (lexer))
16161         lexer = lexer->next;
16162       cp_lexer_commit_tokens (lexer);
16163     }
16164 }
16165
16166 /* Abort the currently active tentative parse.  All consumed tokens
16167    will be rolled back, and no diagnostics will be issued.  */
16168
16169 static void
16170 cp_parser_abort_tentative_parse (cp_parser* parser)
16171 {
16172   cp_parser_simulate_error (parser);
16173   /* Now, pretend that we want to see if the construct was
16174      successfully parsed.  */
16175   cp_parser_parse_definitely (parser);
16176 }
16177
16178 /* Stop parsing tentatively.  If a parse error has occurred, restore the
16179    token stream.  Otherwise, commit to the tokens we have consumed.
16180    Returns true if no error occurred; false otherwise.  */
16181
16182 static bool
16183 cp_parser_parse_definitely (cp_parser* parser)
16184 {
16185   bool error_occurred;
16186   cp_parser_context *context;
16187
16188   /* Remember whether or not an error occurred, since we are about to
16189      destroy that information.  */
16190   error_occurred = cp_parser_error_occurred (parser);
16191   /* Remove the topmost context from the stack.  */
16192   context = parser->context;
16193   parser->context = context->next;
16194   /* If no parse errors occurred, commit to the tentative parse.  */
16195   if (!error_occurred)
16196     {
16197       /* Commit to the tokens read tentatively, unless that was
16198          already done.  */
16199       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16200         cp_lexer_commit_tokens (parser->lexer);
16201
16202       pop_to_parent_deferring_access_checks ();
16203     }
16204   /* Otherwise, if errors occurred, roll back our state so that things
16205      are just as they were before we began the tentative parse.  */
16206   else
16207     {
16208       cp_lexer_rollback_tokens (parser->lexer);
16209       pop_deferring_access_checks ();
16210     }
16211   /* Add the context to the front of the free list.  */
16212   context->next = cp_parser_context_free_list;
16213   cp_parser_context_free_list = context;
16214
16215   return !error_occurred;
16216 }
16217
16218 /* Returns true if we are parsing tentatively and are not committed to
16219    this tentative parse.  */
16220
16221 static bool
16222 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16223 {
16224   return (cp_parser_parsing_tentatively (parser)
16225           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16226 }
16227
16228 /* Returns nonzero iff an error has occurred during the most recent
16229    tentative parse.  */
16230
16231 static bool
16232 cp_parser_error_occurred (cp_parser* parser)
16233 {
16234   return (cp_parser_parsing_tentatively (parser)
16235           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16236 }
16237
16238 /* Returns nonzero if GNU extensions are allowed.  */
16239
16240 static bool
16241 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16242 {
16243   return parser->allow_gnu_extensions_p;
16244 }
16245 \f
16246 /* Objective-C++ Productions */
16247
16248
16249 /* Parse an Objective-C expression, which feeds into a primary-expression
16250    above.
16251
16252    objc-expression:
16253      objc-message-expression
16254      objc-string-literal
16255      objc-encode-expression
16256      objc-protocol-expression
16257      objc-selector-expression
16258
16259   Returns a tree representation of the expression.  */
16260
16261 static tree
16262 cp_parser_objc_expression (cp_parser* parser)
16263 {
16264   /* Try to figure out what kind of declaration is present.  */
16265   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16266
16267   switch (kwd->type)
16268     {
16269     case CPP_OPEN_SQUARE:
16270       return cp_parser_objc_message_expression (parser);
16271
16272     case CPP_OBJC_STRING:
16273       kwd = cp_lexer_consume_token (parser->lexer);
16274       return objc_build_string_object (kwd->value);
16275
16276     case CPP_KEYWORD:
16277       switch (kwd->keyword)
16278         {
16279         case RID_AT_ENCODE:
16280           return cp_parser_objc_encode_expression (parser);
16281
16282         case RID_AT_PROTOCOL:
16283           return cp_parser_objc_protocol_expression (parser);
16284
16285         case RID_AT_SELECTOR:
16286           return cp_parser_objc_selector_expression (parser);
16287
16288         default:
16289           break;
16290         }
16291     default:
16292       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16293       cp_parser_skip_to_end_of_block_or_statement (parser);
16294     }
16295
16296   return error_mark_node;
16297 }
16298
16299 /* Parse an Objective-C message expression.
16300
16301    objc-message-expression:
16302      [ objc-message-receiver objc-message-args ]
16303
16304    Returns a representation of an Objective-C message.  */
16305
16306 static tree
16307 cp_parser_objc_message_expression (cp_parser* parser)
16308 {
16309   tree receiver, messageargs;
16310
16311   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
16312   receiver = cp_parser_objc_message_receiver (parser);
16313   messageargs = cp_parser_objc_message_args (parser);
16314   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16315
16316   return objc_build_message_expr (build_tree_list (receiver, messageargs));
16317 }
16318
16319 /* Parse an objc-message-receiver.
16320
16321    objc-message-receiver:
16322      expression
16323      simple-type-specifier
16324
16325   Returns a representation of the type or expression.  */
16326
16327 static tree
16328 cp_parser_objc_message_receiver (cp_parser* parser)
16329 {
16330   tree rcv;
16331
16332   /* An Objective-C message receiver may be either (1) a type
16333      or (2) an expression.  */
16334   cp_parser_parse_tentatively (parser);
16335   rcv = cp_parser_expression (parser, false);
16336
16337   if (cp_parser_parse_definitely (parser))
16338     return rcv;
16339
16340   rcv = cp_parser_simple_type_specifier (parser,
16341                                          /*decl_specs=*/NULL,
16342                                          CP_PARSER_FLAGS_NONE);
16343
16344   return objc_get_class_reference (rcv);
16345 }
16346
16347 /* Parse the arguments and selectors comprising an Objective-C message.
16348
16349    objc-message-args:
16350      objc-selector
16351      objc-selector-args
16352      objc-selector-args , objc-comma-args
16353
16354    objc-selector-args:
16355      objc-selector [opt] : assignment-expression
16356      objc-selector-args objc-selector [opt] : assignment-expression
16357
16358    objc-comma-args:
16359      assignment-expression
16360      objc-comma-args , assignment-expression
16361
16362    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16363    selector arguments and TREE_VALUE containing a list of comma
16364    arguments.  */
16365
16366 static tree
16367 cp_parser_objc_message_args (cp_parser* parser)
16368 {
16369   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16370   bool maybe_unary_selector_p = true;
16371   cp_token *token = cp_lexer_peek_token (parser->lexer);
16372
16373   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16374     {
16375       tree selector = NULL_TREE, arg;
16376
16377       if (token->type != CPP_COLON)
16378         selector = cp_parser_objc_selector (parser);
16379
16380       /* Detect if we have a unary selector.  */
16381       if (maybe_unary_selector_p
16382           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16383         return build_tree_list (selector, NULL_TREE);
16384
16385       maybe_unary_selector_p = false;
16386       cp_parser_require (parser, CPP_COLON, "`:'");
16387       arg = cp_parser_assignment_expression (parser, false);
16388
16389       sel_args
16390         = chainon (sel_args,
16391                    build_tree_list (selector, arg));
16392
16393       token = cp_lexer_peek_token (parser->lexer);
16394     }
16395
16396   /* Handle non-selector arguments, if any. */
16397   while (token->type == CPP_COMMA)
16398     {
16399       tree arg;
16400
16401       cp_lexer_consume_token (parser->lexer);
16402       arg = cp_parser_assignment_expression (parser, false);
16403
16404       addl_args
16405         = chainon (addl_args,
16406                    build_tree_list (NULL_TREE, arg));
16407
16408       token = cp_lexer_peek_token (parser->lexer);
16409     }
16410
16411   return build_tree_list (sel_args, addl_args);
16412 }
16413
16414 /* Parse an Objective-C encode expression.
16415
16416    objc-encode-expression:
16417      @encode objc-typename
16418
16419    Returns an encoded representation of the type argument.  */
16420
16421 static tree
16422 cp_parser_objc_encode_expression (cp_parser* parser)
16423 {
16424   tree type;
16425
16426   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
16427   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16428   type = complete_type (cp_parser_type_id (parser));
16429   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16430
16431   if (!type)
16432     {
16433       error ("%<@encode%> must specify a type as an argument");
16434       return error_mark_node;
16435     }
16436
16437   return objc_build_encode_expr (type);
16438 }
16439
16440 /* Parse an Objective-C @defs expression.  */
16441
16442 static tree
16443 cp_parser_objc_defs_expression (cp_parser *parser)
16444 {
16445   tree name;
16446
16447   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
16448   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16449   name = cp_parser_identifier (parser);
16450   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16451
16452   return objc_get_class_ivars (name);
16453 }
16454
16455 /* Parse an Objective-C protocol expression.
16456
16457   objc-protocol-expression:
16458     @protocol ( identifier )
16459
16460   Returns a representation of the protocol expression.  */
16461
16462 static tree
16463 cp_parser_objc_protocol_expression (cp_parser* parser)
16464 {
16465   tree proto;
16466
16467   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
16468   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16469   proto = cp_parser_identifier (parser);
16470   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16471
16472   return objc_build_protocol_expr (proto);
16473 }
16474
16475 /* Parse an Objective-C selector expression.
16476
16477    objc-selector-expression:
16478      @selector ( objc-method-signature )
16479
16480    objc-method-signature:
16481      objc-selector
16482      objc-selector-seq
16483
16484    objc-selector-seq:
16485      objc-selector :
16486      objc-selector-seq objc-selector :
16487
16488   Returns a representation of the method selector.  */
16489
16490 static tree
16491 cp_parser_objc_selector_expression (cp_parser* parser)
16492 {
16493   tree sel_seq = NULL_TREE;
16494   bool maybe_unary_selector_p = true;
16495   cp_token *token;
16496
16497   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
16498   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16499   token = cp_lexer_peek_token (parser->lexer);
16500
16501   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16502     {
16503       tree selector = NULL_TREE;
16504
16505       if (token->type != CPP_COLON)
16506         selector = cp_parser_objc_selector (parser);
16507
16508       /* Detect if we have a unary selector.  */
16509       if (maybe_unary_selector_p
16510           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16511         {
16512           sel_seq = selector;
16513           goto finish_selector;
16514         }
16515
16516       maybe_unary_selector_p = false;
16517       cp_parser_require (parser, CPP_COLON, "`:'");
16518
16519       sel_seq
16520         = chainon (sel_seq,
16521                    build_tree_list (selector, NULL_TREE));
16522
16523       token = cp_lexer_peek_token (parser->lexer);
16524     }
16525
16526  finish_selector:
16527   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16528
16529   return objc_build_selector_expr (sel_seq);
16530 }
16531
16532 /* Parse a list of identifiers.
16533
16534    objc-identifier-list:
16535      identifier
16536      objc-identifier-list , identifier
16537
16538    Returns a TREE_LIST of identifier nodes.  */
16539
16540 static tree
16541 cp_parser_objc_identifier_list (cp_parser* parser)
16542 {
16543   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16544   cp_token *sep = cp_lexer_peek_token (parser->lexer);
16545
16546   while (sep->type == CPP_COMMA)
16547     {
16548       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16549       list = chainon (list,
16550                       build_tree_list (NULL_TREE,
16551                                        cp_parser_identifier (parser)));
16552       sep = cp_lexer_peek_token (parser->lexer);
16553     }
16554
16555   return list;
16556 }
16557
16558 /* Parse an Objective-C alias declaration.
16559
16560    objc-alias-declaration:
16561      @compatibility_alias identifier identifier ;
16562
16563    This function registers the alias mapping with the Objective-C front-end.
16564    It returns nothing.  */
16565
16566 static void
16567 cp_parser_objc_alias_declaration (cp_parser* parser)
16568 {
16569   tree alias, orig;
16570
16571   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
16572   alias = cp_parser_identifier (parser);
16573   orig = cp_parser_identifier (parser);
16574   objc_declare_alias (alias, orig);
16575   cp_parser_consume_semicolon_at_end_of_statement (parser);
16576 }
16577
16578 /* Parse an Objective-C class forward-declaration.
16579
16580    objc-class-declaration:
16581      @class objc-identifier-list ;
16582
16583    The function registers the forward declarations with the Objective-C
16584    front-end.  It returns nothing.  */
16585
16586 static void
16587 cp_parser_objc_class_declaration (cp_parser* parser)
16588 {
16589   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
16590   objc_declare_class (cp_parser_objc_identifier_list (parser));
16591   cp_parser_consume_semicolon_at_end_of_statement (parser);
16592 }
16593
16594 /* Parse a list of Objective-C protocol references.
16595
16596    objc-protocol-refs-opt:
16597      objc-protocol-refs [opt]
16598
16599    objc-protocol-refs:
16600      < objc-identifier-list >
16601
16602    Returns a TREE_LIST of identifiers, if any.  */
16603
16604 static tree
16605 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16606 {
16607   tree protorefs = NULL_TREE;
16608
16609   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16610     {
16611       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
16612       protorefs = cp_parser_objc_identifier_list (parser);
16613       cp_parser_require (parser, CPP_GREATER, "`>'");
16614     }
16615
16616   return protorefs;
16617 }
16618
16619 /* Parse a Objective-C visibility specification.  */
16620
16621 static void
16622 cp_parser_objc_visibility_spec (cp_parser* parser)
16623 {
16624   cp_token *vis = cp_lexer_peek_token (parser->lexer);
16625
16626   switch (vis->keyword)
16627     {
16628     case RID_AT_PRIVATE:
16629       objc_set_visibility (2);
16630       break;
16631     case RID_AT_PROTECTED:
16632       objc_set_visibility (0);
16633       break;
16634     case RID_AT_PUBLIC:
16635       objc_set_visibility (1);
16636       break;
16637     default:
16638       return;
16639     }
16640
16641   /* Eat '@private'/'@protected'/'@public'.  */
16642   cp_lexer_consume_token (parser->lexer);
16643 }
16644
16645 /* Parse an Objective-C method type.  */
16646
16647 static void
16648 cp_parser_objc_method_type (cp_parser* parser)
16649 {
16650   objc_set_method_type
16651    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16652     ? PLUS_EXPR
16653     : MINUS_EXPR);
16654 }
16655
16656 /* Parse an Objective-C protocol qualifier.  */
16657
16658 static tree
16659 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16660 {
16661   tree quals = NULL_TREE, node;
16662   cp_token *token = cp_lexer_peek_token (parser->lexer);
16663
16664   node = token->value;
16665
16666   while (node && TREE_CODE (node) == IDENTIFIER_NODE
16667          && (node == ridpointers [(int) RID_IN]
16668              || node == ridpointers [(int) RID_OUT]
16669              || node == ridpointers [(int) RID_INOUT]
16670              || node == ridpointers [(int) RID_BYCOPY]
16671              || node == ridpointers [(int) RID_BYREF]
16672              || node == ridpointers [(int) RID_ONEWAY]))
16673     {
16674       quals = tree_cons (NULL_TREE, node, quals);
16675       cp_lexer_consume_token (parser->lexer);
16676       token = cp_lexer_peek_token (parser->lexer);
16677       node = token->value;
16678     }
16679
16680   return quals;
16681 }
16682
16683 /* Parse an Objective-C typename.  */
16684
16685 static tree
16686 cp_parser_objc_typename (cp_parser* parser)
16687 {
16688   tree typename = NULL_TREE;
16689
16690   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16691     {
16692       tree proto_quals, cp_type = NULL_TREE;
16693
16694       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
16695       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16696
16697       /* An ObjC type name may consist of just protocol qualifiers, in which
16698          case the type shall default to 'id'.  */
16699       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16700         cp_type = cp_parser_type_id (parser);
16701
16702       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16703       typename = build_tree_list (proto_quals, cp_type);
16704     }
16705
16706   return typename;
16707 }
16708
16709 /* Check to see if TYPE refers to an Objective-C selector name.  */
16710
16711 static bool
16712 cp_parser_objc_selector_p (enum cpp_ttype type)
16713 {
16714   return (type == CPP_NAME || type == CPP_KEYWORD
16715           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16716           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16717           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16718           || type == CPP_XOR || type == CPP_XOR_EQ);
16719 }
16720
16721 /* Parse an Objective-C selector.  */
16722
16723 static tree
16724 cp_parser_objc_selector (cp_parser* parser)
16725 {
16726   cp_token *token = cp_lexer_consume_token (parser->lexer);
16727
16728   if (!cp_parser_objc_selector_p (token->type))
16729     {
16730       error ("invalid Objective-C++ selector name");
16731       return error_mark_node;
16732     }
16733
16734   /* C++ operator names are allowed to appear in ObjC selectors.  */
16735   switch (token->type)
16736     {
16737     case CPP_AND_AND: return get_identifier ("and");
16738     case CPP_AND_EQ: return get_identifier ("and_eq");
16739     case CPP_AND: return get_identifier ("bitand");
16740     case CPP_OR: return get_identifier ("bitor");
16741     case CPP_COMPL: return get_identifier ("compl");
16742     case CPP_NOT: return get_identifier ("not");
16743     case CPP_NOT_EQ: return get_identifier ("not_eq");
16744     case CPP_OR_OR: return get_identifier ("or");
16745     case CPP_OR_EQ: return get_identifier ("or_eq");
16746     case CPP_XOR: return get_identifier ("xor");
16747     case CPP_XOR_EQ: return get_identifier ("xor_eq");
16748     default: return token->value;
16749     }
16750 }
16751
16752 /* Parse an Objective-C params list.  */
16753
16754 static tree
16755 cp_parser_objc_method_keyword_params (cp_parser* parser)
16756 {
16757   tree params = NULL_TREE;
16758   bool maybe_unary_selector_p = true;
16759   cp_token *token = cp_lexer_peek_token (parser->lexer);
16760
16761   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16762     {
16763       tree selector = NULL_TREE, typename, identifier;
16764
16765       if (token->type != CPP_COLON)
16766         selector = cp_parser_objc_selector (parser);
16767
16768       /* Detect if we have a unary selector.  */
16769       if (maybe_unary_selector_p
16770           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16771         return selector;
16772
16773       maybe_unary_selector_p = false;
16774       cp_parser_require (parser, CPP_COLON, "`:'");
16775       typename = cp_parser_objc_typename (parser);
16776       identifier = cp_parser_identifier (parser);
16777
16778       params
16779         = chainon (params,
16780                    objc_build_keyword_decl (selector,
16781                                             typename,
16782                                             identifier));
16783
16784       token = cp_lexer_peek_token (parser->lexer);
16785     }
16786
16787   return params;
16788 }
16789
16790 /* Parse the non-keyword Objective-C params.  */
16791
16792 static tree
16793 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
16794 {
16795   tree params = make_node (TREE_LIST);
16796   cp_token *token = cp_lexer_peek_token (parser->lexer);
16797   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
16798
16799   while (token->type == CPP_COMMA)
16800     {
16801       cp_parameter_declarator *parmdecl;
16802       tree parm;
16803
16804       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16805       token = cp_lexer_peek_token (parser->lexer);
16806
16807       if (token->type == CPP_ELLIPSIS)
16808         {
16809           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
16810           *ellipsisp = true;
16811           break;
16812         }
16813
16814       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
16815       parm = grokdeclarator (parmdecl->declarator,
16816                              &parmdecl->decl_specifiers,
16817                              PARM, /*initialized=*/0,
16818                              /*attrlist=*/NULL);
16819
16820       chainon (params, build_tree_list (NULL_TREE, parm));
16821       token = cp_lexer_peek_token (parser->lexer);
16822     }
16823
16824   return params;
16825 }
16826
16827 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
16828
16829 static void
16830 cp_parser_objc_interstitial_code (cp_parser* parser)
16831 {
16832   cp_token *token = cp_lexer_peek_token (parser->lexer);
16833
16834   /* If the next token is `extern' and the following token is a string
16835      literal, then we have a linkage specification.  */
16836   if (token->keyword == RID_EXTERN
16837       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
16838     cp_parser_linkage_specification (parser);
16839   /* Handle #pragma, if any.  */
16840   else if (token->type == CPP_PRAGMA)
16841     cp_lexer_handle_pragma (parser->lexer);
16842   /* Allow stray semicolons.  */
16843   else if (token->type == CPP_SEMICOLON)
16844     cp_lexer_consume_token (parser->lexer);
16845   /* Finally, try to parse a block-declaration, or a function-definition.  */
16846   else
16847     cp_parser_block_declaration (parser, /*statement_p=*/false);
16848 }
16849
16850 /* Parse a method signature.  */
16851
16852 static tree
16853 cp_parser_objc_method_signature (cp_parser* parser)
16854 {
16855   tree rettype, kwdparms, optparms;
16856   bool ellipsis = false;
16857
16858   cp_parser_objc_method_type (parser);
16859   rettype = cp_parser_objc_typename (parser);
16860   kwdparms = cp_parser_objc_method_keyword_params (parser);
16861   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
16862
16863   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
16864 }
16865
16866 /* Pars an Objective-C method prototype list.  */
16867
16868 static void
16869 cp_parser_objc_method_prototype_list (cp_parser* parser)
16870 {
16871   cp_token *token = cp_lexer_peek_token (parser->lexer);
16872
16873   while (token->keyword != RID_AT_END)
16874     {
16875       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16876         {
16877           objc_add_method_declaration
16878            (cp_parser_objc_method_signature (parser));
16879           cp_parser_consume_semicolon_at_end_of_statement (parser);
16880         }
16881       else
16882         /* Allow for interspersed non-ObjC++ code.  */
16883         cp_parser_objc_interstitial_code (parser);
16884
16885       token = cp_lexer_peek_token (parser->lexer);
16886     }
16887
16888   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
16889   objc_finish_interface ();
16890 }
16891
16892 /* Parse an Objective-C method definition list.  */
16893
16894 static void
16895 cp_parser_objc_method_definition_list (cp_parser* parser)
16896 {
16897   cp_token *token = cp_lexer_peek_token (parser->lexer);
16898
16899   while (token->keyword != RID_AT_END)
16900     {
16901       tree meth;
16902
16903       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16904         {
16905           push_deferring_access_checks (dk_deferred);
16906           objc_start_method_definition
16907            (cp_parser_objc_method_signature (parser));
16908
16909           /* For historical reasons, we accept an optional semicolon.  */
16910           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16911             cp_lexer_consume_token (parser->lexer);
16912
16913           perform_deferred_access_checks ();
16914           stop_deferring_access_checks ();
16915           meth = cp_parser_function_definition_after_declarator (parser,
16916                                                                  false);
16917           pop_deferring_access_checks ();
16918           objc_finish_method_definition (meth);
16919         }
16920       else
16921         /* Allow for interspersed non-ObjC++ code.  */
16922         cp_parser_objc_interstitial_code (parser);
16923
16924       token = cp_lexer_peek_token (parser->lexer);
16925     }
16926
16927   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
16928   objc_finish_implementation ();
16929 }
16930
16931 /* Parse Objective-C ivars.  */
16932
16933 static void
16934 cp_parser_objc_class_ivars (cp_parser* parser)
16935 {
16936   cp_token *token = cp_lexer_peek_token (parser->lexer);
16937
16938   if (token->type != CPP_OPEN_BRACE)
16939     return;     /* No ivars specified.  */
16940
16941   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
16942   token = cp_lexer_peek_token (parser->lexer);
16943
16944   while (token->type != CPP_CLOSE_BRACE)
16945     {
16946       cp_decl_specifier_seq declspecs;
16947       int decl_class_or_enum_p;
16948       tree prefix_attributes;
16949
16950       cp_parser_objc_visibility_spec (parser);
16951
16952       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16953         break;
16954
16955       cp_parser_decl_specifier_seq (parser,
16956                                     CP_PARSER_FLAGS_OPTIONAL,
16957                                     &declspecs,
16958                                     &decl_class_or_enum_p);
16959       prefix_attributes = declspecs.attributes;
16960       declspecs.attributes = NULL_TREE;
16961
16962       /* Keep going until we hit the `;' at the end of the
16963          declaration.  */
16964       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16965         {
16966           tree width = NULL_TREE, attributes, first_attribute, decl;
16967           cp_declarator *declarator = NULL;
16968           int ctor_dtor_or_conv_p;
16969
16970           /* Check for a (possibly unnamed) bitfield declaration.  */
16971           token = cp_lexer_peek_token (parser->lexer);
16972           if (token->type == CPP_COLON)
16973             goto eat_colon;
16974
16975           if (token->type == CPP_NAME
16976               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16977                   == CPP_COLON))
16978             {
16979               /* Get the name of the bitfield.  */
16980               declarator = make_id_declarator (NULL_TREE,
16981                                                cp_parser_identifier (parser));
16982
16983              eat_colon:
16984               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
16985               /* Get the width of the bitfield.  */
16986               width
16987                 = cp_parser_constant_expression (parser,
16988                                                  /*allow_non_constant=*/false,
16989                                                  NULL);
16990             }
16991           else
16992             {
16993               /* Parse the declarator.  */
16994               declarator
16995                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16996                                         &ctor_dtor_or_conv_p,
16997                                         /*parenthesized_p=*/NULL,
16998                                         /*member_p=*/false);
16999             }
17000
17001           /* Look for attributes that apply to the ivar.  */
17002           attributes = cp_parser_attributes_opt (parser);
17003           /* Remember which attributes are prefix attributes and
17004              which are not.  */
17005           first_attribute = attributes;
17006           /* Combine the attributes.  */
17007           attributes = chainon (prefix_attributes, attributes);
17008
17009           if (width)
17010             {
17011               /* Create the bitfield declaration.  */
17012               decl = grokbitfield (declarator, &declspecs, width);
17013               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17014             }
17015           else
17016             decl = grokfield (declarator, &declspecs, NULL_TREE,
17017                               NULL_TREE, attributes);
17018
17019           /* Add the instance variable.  */
17020           objc_add_instance_variable (decl);
17021
17022           /* Reset PREFIX_ATTRIBUTES.  */
17023           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17024             attributes = TREE_CHAIN (attributes);
17025           if (attributes)
17026             TREE_CHAIN (attributes) = NULL_TREE;
17027
17028           token = cp_lexer_peek_token (parser->lexer);
17029
17030           if (token->type == CPP_COMMA)
17031             {
17032               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17033               continue;
17034             }
17035           break;
17036         }
17037
17038       cp_parser_consume_semicolon_at_end_of_statement (parser);
17039       token = cp_lexer_peek_token (parser->lexer);
17040     }
17041
17042   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17043   /* For historical reasons, we accept an optional semicolon.  */
17044   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17045     cp_lexer_consume_token (parser->lexer);
17046 }
17047
17048 /* Parse an Objective-C protocol declaration.  */
17049
17050 static void
17051 cp_parser_objc_protocol_declaration (cp_parser* parser)
17052 {
17053   tree proto, protorefs;
17054   cp_token *tok;
17055
17056   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17057   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17058     {
17059       error ("identifier expected after %<@protocol%>");
17060       goto finish;
17061     }
17062
17063   /* See if we have a forward declaration or a definition.  */
17064   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17065
17066   /* Try a forward declaration first.  */
17067   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17068     {
17069       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17070      finish:
17071       cp_parser_consume_semicolon_at_end_of_statement (parser);
17072     }
17073
17074   /* Ok, we got a full-fledged definition (or at least should).  */
17075   else
17076     {
17077       proto = cp_parser_identifier (parser);
17078       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17079       objc_start_protocol (proto, protorefs);
17080       cp_parser_objc_method_prototype_list (parser);
17081     }
17082 }
17083
17084 /* Parse an Objective-C superclass or category.  */
17085
17086 static void
17087 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17088                                                           tree *categ)
17089 {
17090   cp_token *next = cp_lexer_peek_token (parser->lexer);
17091
17092   *super = *categ = NULL_TREE;
17093   if (next->type == CPP_COLON)
17094     {
17095       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17096       *super = cp_parser_identifier (parser);
17097     }
17098   else if (next->type == CPP_OPEN_PAREN)
17099     {
17100       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17101       *categ = cp_parser_identifier (parser);
17102       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17103     }
17104 }
17105
17106 /* Parse an Objective-C class interface.  */
17107
17108 static void
17109 cp_parser_objc_class_interface (cp_parser* parser)
17110 {
17111   tree name, super, categ, protos;
17112
17113   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17114   name = cp_parser_identifier (parser);
17115   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17116   protos = cp_parser_objc_protocol_refs_opt (parser);
17117
17118   /* We have either a class or a category on our hands.  */
17119   if (categ)
17120     objc_start_category_interface (name, categ, protos);
17121   else
17122     {
17123       objc_start_class_interface (name, super, protos);
17124       /* Handle instance variable declarations, if any.  */
17125       cp_parser_objc_class_ivars (parser);
17126       objc_continue_interface ();
17127     }
17128
17129   cp_parser_objc_method_prototype_list (parser);
17130 }
17131
17132 /* Parse an Objective-C class implementation.  */
17133
17134 static void
17135 cp_parser_objc_class_implementation (cp_parser* parser)
17136 {
17137   tree name, super, categ;
17138
17139   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17140   name = cp_parser_identifier (parser);
17141   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17142
17143   /* We have either a class or a category on our hands.  */
17144   if (categ)
17145     objc_start_category_implementation (name, categ);
17146   else
17147     {
17148       objc_start_class_implementation (name, super);
17149       /* Handle instance variable declarations, if any.  */
17150       cp_parser_objc_class_ivars (parser);
17151       objc_continue_implementation ();
17152     }
17153
17154   cp_parser_objc_method_definition_list (parser);
17155 }
17156
17157 /* Consume the @end token and finish off the implementation.  */
17158
17159 static void
17160 cp_parser_objc_end_implementation (cp_parser* parser)
17161 {
17162   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17163   objc_finish_implementation ();
17164 }
17165
17166 /* Parse an Objective-C declaration.  */
17167
17168 static void
17169 cp_parser_objc_declaration (cp_parser* parser)
17170 {
17171   /* Try to figure out what kind of declaration is present.  */
17172   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17173
17174   switch (kwd->keyword)
17175     {
17176     case RID_AT_ALIAS:
17177       cp_parser_objc_alias_declaration (parser);
17178       break;
17179     case RID_AT_CLASS:
17180       cp_parser_objc_class_declaration (parser);
17181       break;
17182     case RID_AT_PROTOCOL:
17183       cp_parser_objc_protocol_declaration (parser);
17184       break;
17185     case RID_AT_INTERFACE:
17186       cp_parser_objc_class_interface (parser);
17187       break;
17188     case RID_AT_IMPLEMENTATION:
17189       cp_parser_objc_class_implementation (parser);
17190       break;
17191     case RID_AT_END:
17192       cp_parser_objc_end_implementation (parser);
17193       break;
17194     default:
17195       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17196       cp_parser_skip_to_end_of_block_or_statement (parser);
17197     }
17198 }
17199
17200 /* Parse an Objective-C try-catch-finally statement.
17201
17202    objc-try-catch-finally-stmt:
17203      @try compound-statement objc-catch-clause-seq [opt]
17204        objc-finally-clause [opt]
17205
17206    objc-catch-clause-seq:
17207      objc-catch-clause objc-catch-clause-seq [opt]
17208
17209    objc-catch-clause:
17210      @catch ( exception-declaration ) compound-statement
17211
17212    objc-finally-clause
17213      @finally compound-statement
17214
17215    Returns NULL_TREE.  */
17216
17217 static tree
17218 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17219   location_t location;
17220   tree stmt;
17221
17222   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17223   location = cp_lexer_peek_token (parser->lexer)->location;
17224   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17225      node, lest it get absorbed into the surrounding block.  */
17226   stmt = push_stmt_list ();
17227   cp_parser_compound_statement (parser, NULL, false);
17228   objc_begin_try_stmt (location, pop_stmt_list (stmt));
17229
17230   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17231     {
17232       cp_parameter_declarator *parmdecl;
17233       tree parm;
17234
17235       cp_lexer_consume_token (parser->lexer);
17236       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17237       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17238       parm = grokdeclarator (parmdecl->declarator,
17239                              &parmdecl->decl_specifiers,
17240                              PARM, /*initialized=*/0,
17241                              /*attrlist=*/NULL);
17242       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17243       objc_begin_catch_clause (parm);
17244       cp_parser_compound_statement (parser, NULL, false);
17245       objc_finish_catch_clause ();
17246     }
17247
17248   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17249     {
17250       cp_lexer_consume_token (parser->lexer);
17251       location = cp_lexer_peek_token (parser->lexer)->location;
17252       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17253          node, lest it get absorbed into the surrounding block.  */
17254       stmt = push_stmt_list ();
17255       cp_parser_compound_statement (parser, NULL, false);
17256       objc_build_finally_clause (location, pop_stmt_list (stmt));
17257     }
17258
17259   return objc_finish_try_stmt ();
17260 }
17261
17262 /* Parse an Objective-C synchronized statement.
17263
17264    objc-synchronized-stmt:
17265      @synchronized ( expression ) compound-statement
17266
17267    Returns NULL_TREE.  */
17268
17269 static tree
17270 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17271   location_t location;
17272   tree lock, stmt;
17273
17274   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17275
17276   location = cp_lexer_peek_token (parser->lexer)->location;
17277   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17278   lock = cp_parser_expression (parser, false);
17279   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17280
17281   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17282      node, lest it get absorbed into the surrounding block.  */
17283   stmt = push_stmt_list ();
17284   cp_parser_compound_statement (parser, NULL, false);
17285
17286   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17287 }
17288
17289 /* Parse an Objective-C throw statement.
17290
17291    objc-throw-stmt:
17292      @throw assignment-expression [opt] ;
17293
17294    Returns a constructed '@throw' statement.  */
17295
17296 static tree
17297 cp_parser_objc_throw_statement (cp_parser *parser) {
17298   tree expr = NULL_TREE;
17299
17300   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17301
17302   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17303     expr = cp_parser_assignment_expression (parser, false);
17304
17305   cp_parser_consume_semicolon_at_end_of_statement (parser);
17306
17307   return objc_build_throw_stmt (expr);
17308 }
17309
17310 /* Parse an Objective-C statement.  */
17311
17312 static tree
17313 cp_parser_objc_statement (cp_parser * parser) {
17314   /* Try to figure out what kind of declaration is present.  */
17315   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17316
17317   switch (kwd->keyword)
17318     {
17319     case RID_AT_TRY:
17320       return cp_parser_objc_try_catch_finally_statement (parser);
17321     case RID_AT_SYNCHRONIZED:
17322       return cp_parser_objc_synchronized_statement (parser);
17323     case RID_AT_THROW:
17324       return cp_parser_objc_throw_statement (parser);
17325     default:
17326       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17327       cp_parser_skip_to_end_of_block_or_statement (parser);
17328     }
17329
17330   return error_mark_node;
17331 }
17332 \f
17333 /* The parser.  */
17334
17335 static GTY (()) cp_parser *the_parser;
17336
17337 /* External interface.  */
17338
17339 /* Parse one entire translation unit.  */
17340
17341 void
17342 c_parse_file (void)
17343 {
17344   bool error_occurred;
17345   static bool already_called = false;
17346
17347   if (already_called)
17348     {
17349       sorry ("inter-module optimizations not implemented for C++");
17350       return;
17351     }
17352   already_called = true;
17353
17354   the_parser = cp_parser_new ();
17355   push_deferring_access_checks (flag_access_control
17356                                 ? dk_no_deferred : dk_no_check);
17357   error_occurred = cp_parser_translation_unit (the_parser);
17358   the_parser = NULL;
17359 }
17360
17361 /* This variable must be provided by every front end.  */
17362
17363 int yydebug;
17364
17365 #include "gt-cp-parser.h"