OSDN Git Service

libcpp/
[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   /* Identifier for the pragma.  */
59   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
60   /* True if this token is from a system header.  */
61   BOOL_BITFIELD in_system_header : 1;
62   /* True if this token is from a context where it is implicitly extern "C" */
63   BOOL_BITFIELD implicit_extern_c : 1;
64   /* True for a CPP_NAME token that is not a keyword (i.e., for which
65      KEYWORD is RID_MAX) iff this name was looked up and found to be
66      ambiguous.  An error has already been reported.  */
67   BOOL_BITFIELD ambiguous_p : 1;
68   /* The value associated with this token, if any.  */
69   tree value;
70   /* The location at which this token was found.  */
71   location_t location;
72 } cp_token;
73
74 /* We use a stack of token pointer for saving token sets.  */
75 typedef struct cp_token *cp_token_position;
76 DEF_VEC_P (cp_token_position);
77 DEF_VEC_ALLOC_P (cp_token_position,heap);
78
79 static const cp_token eof_token =
80 {
81   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, NULL_TREE,
82 #if USE_MAPPED_LOCATION
83   0
84 #else
85   {0, 0}
86 #endif
87 };
88
89 /* The cp_lexer structure represents the C++ lexer.  It is responsible
90    for managing the token stream from the preprocessor and supplying
91    it to the parser.  Tokens are never added to the cp_lexer after
92    it is created.  */
93
94 typedef struct cp_lexer GTY (())
95 {
96   /* The memory allocated for the buffer.  NULL if this lexer does not
97      own the token buffer.  */
98   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
99   /* If the lexer owns the buffer, this is the number of tokens in the
100      buffer.  */
101   size_t buffer_length;
102
103   /* A pointer just past the last available token.  The tokens
104      in this lexer are [buffer, last_token).  */
105   cp_token_position GTY ((skip)) last_token;
106
107   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
108      no more available tokens.  */
109   cp_token_position GTY ((skip)) next_token;
110
111   /* A stack indicating positions at which cp_lexer_save_tokens was
112      called.  The top entry is the most recent position at which we
113      began saving tokens.  If the stack is non-empty, we are saving
114      tokens.  */
115   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
116
117   /* The next lexer in a linked list of lexers.  */
118   struct cp_lexer *next;
119
120   /* True if we should output debugging information.  */
121   bool debugging_p;
122
123   /* True if we're in the context of parsing a pragma, and should not
124      increment past the end-of-line marker.  */
125   bool in_pragma;
126 } cp_lexer;
127
128 /* cp_token_cache is a range of tokens.  There is no need to represent
129    allocate heap memory for it, since tokens are never removed from the
130    lexer's array.  There is also no need for the GC to walk through
131    a cp_token_cache, since everything in here is referenced through
132    a lexer.  */
133
134 typedef struct cp_token_cache GTY(())
135 {
136   /* The beginning of the token range.  */
137   cp_token * GTY((skip)) first;
138
139   /* Points immediately after the last token in the range.  */
140   cp_token * GTY ((skip)) last;
141 } cp_token_cache;
142
143 /* Prototypes.  */
144
145 static cp_lexer *cp_lexer_new_main
146   (void);
147 static cp_lexer *cp_lexer_new_from_tokens
148   (cp_token_cache *tokens);
149 static void cp_lexer_destroy
150   (cp_lexer *);
151 static int cp_lexer_saving_tokens
152   (const cp_lexer *);
153 static cp_token_position cp_lexer_token_position
154   (cp_lexer *, bool);
155 static cp_token *cp_lexer_token_at
156   (cp_lexer *, cp_token_position);
157 static void cp_lexer_get_preprocessor_token
158   (cp_lexer *, cp_token *);
159 static inline cp_token *cp_lexer_peek_token
160   (cp_lexer *);
161 static cp_token *cp_lexer_peek_nth_token
162   (cp_lexer *, size_t);
163 static inline bool cp_lexer_next_token_is
164   (cp_lexer *, enum cpp_ttype);
165 static bool cp_lexer_next_token_is_not
166   (cp_lexer *, enum cpp_ttype);
167 static bool cp_lexer_next_token_is_keyword
168   (cp_lexer *, enum rid);
169 static cp_token *cp_lexer_consume_token
170   (cp_lexer *);
171 static void cp_lexer_purge_token
172   (cp_lexer *);
173 static void cp_lexer_purge_tokens_after
174   (cp_lexer *, cp_token_position);
175 static void cp_lexer_save_tokens
176   (cp_lexer *);
177 static void cp_lexer_commit_tokens
178   (cp_lexer *);
179 static void cp_lexer_rollback_tokens
180   (cp_lexer *);
181 #ifdef ENABLE_CHECKING
182 static void cp_lexer_print_token
183   (FILE *, cp_token *);
184 static inline bool cp_lexer_debugging_p
185   (cp_lexer *);
186 static void cp_lexer_start_debugging
187   (cp_lexer *) ATTRIBUTE_UNUSED;
188 static void cp_lexer_stop_debugging
189   (cp_lexer *) ATTRIBUTE_UNUSED;
190 #else
191 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
192    about passing NULL to functions that require non-NULL arguments
193    (fputs, fprintf).  It will never be used, so all we need is a value
194    of the right type that's guaranteed not to be NULL.  */
195 #define cp_lexer_debug_stream stdout
196 #define cp_lexer_print_token(str, tok) (void) 0
197 #define cp_lexer_debugging_p(lexer) 0
198 #endif /* ENABLE_CHECKING */
199
200 static cp_token_cache *cp_token_cache_new
201   (cp_token *, cp_token *);
202
203 static void cp_parser_initial_pragma
204   (cp_token *);
205
206 /* Manifest constants.  */
207 #define CP_LEXER_BUFFER_SIZE 10000
208 #define CP_SAVED_TOKEN_STACK 5
209
210 /* A token type for keywords, as opposed to ordinary identifiers.  */
211 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
212
213 /* A token type for template-ids.  If a template-id is processed while
214    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
215    the value of the CPP_TEMPLATE_ID is whatever was returned by
216    cp_parser_template_id.  */
217 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
218
219 /* A token type for nested-name-specifiers.  If a
220    nested-name-specifier is processed while parsing tentatively, it is
221    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
222    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
223    cp_parser_nested_name_specifier_opt.  */
224 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
225
226 /* A token type for tokens that are not tokens at all; these are used
227    to represent slots in the array where there used to be a token
228    that has now been deleted.  */
229 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
230
231 /* The number of token types, including C++-specific ones.  */
232 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
233
234 /* Variables.  */
235
236 #ifdef ENABLE_CHECKING
237 /* The stream to which debugging output should be written.  */
238 static FILE *cp_lexer_debug_stream;
239 #endif /* ENABLE_CHECKING */
240
241 /* Create a new main C++ lexer, the lexer that gets tokens from the
242    preprocessor.  */
243
244 static cp_lexer *
245 cp_lexer_new_main (void)
246 {
247   cp_token first_token;
248   cp_lexer *lexer;
249   cp_token *pos;
250   size_t alloc;
251   size_t space;
252   cp_token *buffer;
253
254   /* It's possible that parsing the first pragma will load a PCH file,
255      which is a GC collection point.  So we have to do that before
256      allocating any memory.  */
257   cp_parser_initial_pragma (&first_token);
258
259   /* Tell c_lex_with_flags not to merge string constants.  */
260   c_lex_return_raw_strings = true;
261
262   c_common_no_more_pch ();
263
264   /* Allocate the memory.  */
265   lexer = GGC_CNEW (cp_lexer);
266
267 #ifdef ENABLE_CHECKING
268   /* Initially we are not debugging.  */
269   lexer->debugging_p = false;
270 #endif /* ENABLE_CHECKING */
271   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
272                                    CP_SAVED_TOKEN_STACK);
273
274   /* Create the buffer.  */
275   alloc = CP_LEXER_BUFFER_SIZE;
276   buffer = GGC_NEWVEC (cp_token, alloc);
277
278   /* Put the first token in the buffer.  */
279   space = alloc;
280   pos = buffer;
281   *pos = first_token;
282
283   /* Get the remaining tokens from the preprocessor.  */
284   while (pos->type != CPP_EOF)
285     {
286       pos++;
287       if (!--space)
288         {
289           space = alloc;
290           alloc *= 2;
291           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
292           pos = buffer + space;
293         }
294       cp_lexer_get_preprocessor_token (lexer, pos);
295     }
296   lexer->buffer = buffer;
297   lexer->buffer_length = alloc - space;
298   lexer->last_token = pos;
299   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
300
301   /* Subsequent preprocessor diagnostics should use compiler
302      diagnostic functions to get the compiler source location.  */
303   cpp_get_options (parse_in)->client_diagnostic = true;
304   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
305
306   gcc_assert (lexer->next_token->type != CPP_PURGED);
307   return lexer;
308 }
309
310 /* Create a new lexer whose token stream is primed with the tokens in
311    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
312
313 static cp_lexer *
314 cp_lexer_new_from_tokens (cp_token_cache *cache)
315 {
316   cp_token *first = cache->first;
317   cp_token *last = cache->last;
318   cp_lexer *lexer = GGC_CNEW (cp_lexer);
319
320   /* We do not own the buffer.  */
321   lexer->buffer = NULL;
322   lexer->buffer_length = 0;
323   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
324   lexer->last_token = last;
325
326   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
327                                    CP_SAVED_TOKEN_STACK);
328
329 #ifdef ENABLE_CHECKING
330   /* Initially we are not debugging.  */
331   lexer->debugging_p = false;
332 #endif
333
334   gcc_assert (lexer->next_token->type != CPP_PURGED);
335   return lexer;
336 }
337
338 /* Frees all resources associated with LEXER.  */
339
340 static void
341 cp_lexer_destroy (cp_lexer *lexer)
342 {
343   if (lexer->buffer)
344     ggc_free (lexer->buffer);
345   VEC_free (cp_token_position, heap, lexer->saved_tokens);
346   ggc_free (lexer);
347 }
348
349 /* Returns nonzero if debugging information should be output.  */
350
351 #ifdef ENABLE_CHECKING
352
353 static inline bool
354 cp_lexer_debugging_p (cp_lexer *lexer)
355 {
356   return lexer->debugging_p;
357 }
358
359 #endif /* ENABLE_CHECKING */
360
361 static inline cp_token_position
362 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
363 {
364   gcc_assert (!previous_p || lexer->next_token != &eof_token);
365
366   return lexer->next_token - previous_p;
367 }
368
369 static inline cp_token *
370 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
371 {
372   return pos;
373 }
374
375 /* nonzero if we are presently saving tokens.  */
376
377 static inline int
378 cp_lexer_saving_tokens (const cp_lexer* lexer)
379 {
380   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
381 }
382
383 /* Store the next token from the preprocessor in *TOKEN.  Return true
384    if we reach EOF.  */
385
386 static void
387 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
388                                  cp_token *token)
389 {
390   static int is_extern_c = 0;
391
392    /* Get a new token from the preprocessor.  */
393   token->type
394     = c_lex_with_flags (&token->value, &token->location, &token->flags);
395   token->keyword = RID_MAX;
396   token->pragma_kind = PRAGMA_NONE;
397   token->in_system_header = in_system_header;
398
399   /* On some systems, some header files are surrounded by an
400      implicit extern "C" block.  Set a flag in the token if it
401      comes from such a header.  */
402   is_extern_c += pending_lang_change;
403   pending_lang_change = 0;
404   token->implicit_extern_c = is_extern_c > 0;
405
406   /* Check to see if this token is a keyword.  */
407   if (token->type == CPP_NAME)
408     {
409       if (C_IS_RESERVED_WORD (token->value))
410         {
411           /* Mark this token as a keyword.  */
412           token->type = CPP_KEYWORD;
413           /* Record which keyword.  */
414           token->keyword = C_RID_CODE (token->value);
415           /* Update the value.  Some keywords are mapped to particular
416              entities, rather than simply having the value of the
417              corresponding IDENTIFIER_NODE.  For example, `__const' is
418              mapped to `const'.  */
419           token->value = ridpointers[token->keyword];
420         }
421       else
422         {
423           token->ambiguous_p = false;
424           token->keyword = RID_MAX;
425         }
426     }
427   /* Handle Objective-C++ keywords.  */
428   else if (token->type == CPP_AT_NAME)
429     {
430       token->type = CPP_KEYWORD;
431       switch (C_RID_CODE (token->value))
432         {
433         /* Map 'class' to '@class', 'private' to '@private', etc.  */
434         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
435         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
436         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
437         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
438         case RID_THROW: token->keyword = RID_AT_THROW; break;
439         case RID_TRY: token->keyword = RID_AT_TRY; break;
440         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
441         default: token->keyword = C_RID_CODE (token->value);
442         }
443     }
444   else if (token->type == CPP_PRAGMA)
445     {
446       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
447       token->pragma_kind = TREE_INT_CST_LOW (token->value);
448       token->value = NULL;
449     }
450 }
451
452 /* Update the globals input_location and in_system_header from TOKEN.  */
453 static inline void
454 cp_lexer_set_source_position_from_token (cp_token *token)
455 {
456   if (token->type != CPP_EOF)
457     {
458       input_location = token->location;
459       in_system_header = token->in_system_header;
460     }
461 }
462
463 /* Return a pointer to the next token in the token stream, but do not
464    consume it.  */
465
466 static inline cp_token *
467 cp_lexer_peek_token (cp_lexer *lexer)
468 {
469   if (cp_lexer_debugging_p (lexer))
470     {
471       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
472       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
473       putc ('\n', cp_lexer_debug_stream);
474     }
475   return lexer->next_token;
476 }
477
478 /* Return true if the next token has the indicated TYPE.  */
479
480 static inline bool
481 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
482 {
483   return cp_lexer_peek_token (lexer)->type == type;
484 }
485
486 /* Return true if the next token does not have the indicated TYPE.  */
487
488 static inline bool
489 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
490 {
491   return !cp_lexer_next_token_is (lexer, type);
492 }
493
494 /* Return true if the next token is the indicated KEYWORD.  */
495
496 static inline bool
497 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
498 {
499   cp_token *token;
500
501   /* Peek at the next token.  */
502   token = cp_lexer_peek_token (lexer);
503   /* Check to see if it is the indicated keyword.  */
504   return token->keyword == keyword;
505 }
506
507 /* Return a pointer to the Nth token in the token stream.  If N is 1,
508    then this is precisely equivalent to cp_lexer_peek_token (except
509    that it is not inline).  One would like to disallow that case, but
510    there is one case (cp_parser_nth_token_starts_template_id) where
511    the caller passes a variable for N and it might be 1.  */
512
513 static cp_token *
514 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
515 {
516   cp_token *token;
517
518   /* N is 1-based, not zero-based.  */
519   gcc_assert (n > 0);
520   
521   if (cp_lexer_debugging_p (lexer))
522     fprintf (cp_lexer_debug_stream,
523              "cp_lexer: peeking ahead %ld at token: ", (long)n);
524
525   --n;
526   token = lexer->next_token;
527   gcc_assert (!n || token != &eof_token);
528   while (n != 0)
529     {
530       ++token;
531       if (token == lexer->last_token)
532         {
533           token = (cp_token *)&eof_token;
534           break;
535         }
536
537       if (token->type != CPP_PURGED)
538         --n;
539     }
540
541   if (cp_lexer_debugging_p (lexer))
542     {
543       cp_lexer_print_token (cp_lexer_debug_stream, token);
544       putc ('\n', cp_lexer_debug_stream);
545     }
546
547   return token;
548 }
549
550 /* Return the next token, and advance the lexer's next_token pointer
551    to point to the next non-purged token.  */
552
553 static cp_token *
554 cp_lexer_consume_token (cp_lexer* lexer)
555 {
556   cp_token *token = lexer->next_token;
557
558   gcc_assert (token != &eof_token);
559   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
560
561   do
562     {
563       lexer->next_token++;
564       if (lexer->next_token == lexer->last_token)
565         {
566           lexer->next_token = (cp_token *)&eof_token;
567           break;
568         }
569
570     }
571   while (lexer->next_token->type == CPP_PURGED);
572
573   cp_lexer_set_source_position_from_token (token);
574
575   /* Provide debugging output.  */
576   if (cp_lexer_debugging_p (lexer))
577     {
578       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
579       cp_lexer_print_token (cp_lexer_debug_stream, token);
580       putc ('\n', cp_lexer_debug_stream);
581     }
582
583   return token;
584 }
585
586 /* Permanently remove the next token from the token stream, and
587    advance the next_token pointer to refer to the next non-purged
588    token.  */
589
590 static void
591 cp_lexer_purge_token (cp_lexer *lexer)
592 {
593   cp_token *tok = lexer->next_token;
594
595   gcc_assert (tok != &eof_token);
596   tok->type = CPP_PURGED;
597   tok->location = UNKNOWN_LOCATION;
598   tok->value = NULL_TREE;
599   tok->keyword = RID_MAX;
600
601   do
602     {
603       tok++;
604       if (tok == lexer->last_token)
605         {
606           tok = (cp_token *)&eof_token;
607           break;
608         }
609     }
610   while (tok->type == CPP_PURGED);
611   lexer->next_token = tok;
612 }
613
614 /* Permanently remove all tokens after TOK, up to, but not
615    including, the token that will be returned next by
616    cp_lexer_peek_token.  */
617
618 static void
619 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
620 {
621   cp_token *peek = lexer->next_token;
622
623   if (peek == &eof_token)
624     peek = lexer->last_token;
625
626   gcc_assert (tok < peek);
627
628   for ( tok += 1; tok != peek; tok += 1)
629     {
630       tok->type = CPP_PURGED;
631       tok->location = UNKNOWN_LOCATION;
632       tok->value = NULL_TREE;
633       tok->keyword = RID_MAX;
634     }
635 }
636
637 /* Begin saving tokens.  All tokens consumed after this point will be
638    preserved.  */
639
640 static void
641 cp_lexer_save_tokens (cp_lexer* lexer)
642 {
643   /* Provide debugging output.  */
644   if (cp_lexer_debugging_p (lexer))
645     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
646
647   VEC_safe_push (cp_token_position, heap,
648                  lexer->saved_tokens, lexer->next_token);
649 }
650
651 /* Commit to the portion of the token stream most recently saved.  */
652
653 static void
654 cp_lexer_commit_tokens (cp_lexer* lexer)
655 {
656   /* Provide debugging output.  */
657   if (cp_lexer_debugging_p (lexer))
658     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
659
660   VEC_pop (cp_token_position, lexer->saved_tokens);
661 }
662
663 /* Return all tokens saved since the last call to cp_lexer_save_tokens
664    to the token stream.  Stop saving tokens.  */
665
666 static void
667 cp_lexer_rollback_tokens (cp_lexer* lexer)
668 {
669   /* Provide debugging output.  */
670   if (cp_lexer_debugging_p (lexer))
671     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
672
673   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
674 }
675
676 /* Print a representation of the TOKEN on the STREAM.  */
677
678 #ifdef ENABLE_CHECKING
679
680 static void
681 cp_lexer_print_token (FILE * stream, cp_token *token)
682 {
683   /* We don't use cpp_type2name here because the parser defines
684      a few tokens of its own.  */
685   static const char *const token_names[] = {
686     /* cpplib-defined token types */
687 #define OP(e, s) #e,
688 #define TK(e, s) #e,
689     TTYPE_TABLE
690 #undef OP
691 #undef TK
692     /* C++ parser token types - see "Manifest constants", above.  */
693     "KEYWORD",
694     "TEMPLATE_ID",
695     "NESTED_NAME_SPECIFIER",
696     "PURGED"
697   };
698
699   /* If we have a name for the token, print it out.  Otherwise, we
700      simply give the numeric code.  */
701   gcc_assert (token->type < ARRAY_SIZE(token_names));
702   fputs (token_names[token->type], stream);
703
704   /* For some tokens, print the associated data.  */
705   switch (token->type)
706     {
707     case CPP_KEYWORD:
708       /* Some keywords have a value that is not an IDENTIFIER_NODE.
709          For example, `struct' is mapped to an INTEGER_CST.  */
710       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
711         break;
712       /* else fall through */
713     case CPP_NAME:
714       fputs (IDENTIFIER_POINTER (token->value), stream);
715       break;
716
717     case CPP_STRING:
718     case CPP_WSTRING:
719       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
720       break;
721
722     default:
723       break;
724     }
725 }
726
727 /* Start emitting debugging information.  */
728
729 static void
730 cp_lexer_start_debugging (cp_lexer* lexer)
731 {
732   lexer->debugging_p = true;
733 }
734
735 /* Stop emitting debugging information.  */
736
737 static void
738 cp_lexer_stop_debugging (cp_lexer* lexer)
739 {
740   lexer->debugging_p = false;
741 }
742
743 #endif /* ENABLE_CHECKING */
744
745 /* Create a new cp_token_cache, representing a range of tokens.  */
746
747 static cp_token_cache *
748 cp_token_cache_new (cp_token *first, cp_token *last)
749 {
750   cp_token_cache *cache = GGC_NEW (cp_token_cache);
751   cache->first = first;
752   cache->last = last;
753   return cache;
754 }
755
756 \f
757 /* Decl-specifiers.  */
758
759 static void clear_decl_specs
760   (cp_decl_specifier_seq *);
761
762 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
763
764 static void
765 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
766 {
767   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
768 }
769
770 /* Declarators.  */
771
772 /* Nothing other than the parser should be creating declarators;
773    declarators are a semi-syntactic representation of C++ entities.
774    Other parts of the front end that need to create entities (like
775    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
776
777 static cp_declarator *make_call_declarator
778   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
779 static cp_declarator *make_array_declarator
780   (cp_declarator *, tree);
781 static cp_declarator *make_pointer_declarator
782   (cp_cv_quals, cp_declarator *);
783 static cp_declarator *make_reference_declarator
784   (cp_cv_quals, cp_declarator *);
785 static cp_parameter_declarator *make_parameter_declarator
786   (cp_decl_specifier_seq *, cp_declarator *, tree);
787 static cp_declarator *make_ptrmem_declarator
788   (cp_cv_quals, tree, cp_declarator *);
789
790 cp_declarator *cp_error_declarator;
791
792 /* The obstack on which declarators and related data structures are
793    allocated.  */
794 static struct obstack declarator_obstack;
795
796 /* Alloc BYTES from the declarator memory pool.  */
797
798 static inline void *
799 alloc_declarator (size_t bytes)
800 {
801   return obstack_alloc (&declarator_obstack, bytes);
802 }
803
804 /* Allocate a declarator of the indicated KIND.  Clear fields that are
805    common to all declarators.  */
806
807 static cp_declarator *
808 make_declarator (cp_declarator_kind kind)
809 {
810   cp_declarator *declarator;
811
812   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
813   declarator->kind = kind;
814   declarator->attributes = NULL_TREE;
815   declarator->declarator = NULL;
816
817   return declarator;
818 }
819
820 /* Make a declarator for a generalized identifier.  If
821    QUALIFYING_SCOPE is non-NULL, the identifier is
822    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
823    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
824    is, if any.   */
825
826 static cp_declarator *
827 make_id_declarator (tree qualifying_scope, tree unqualified_name,
828                     special_function_kind sfk)
829 {
830   cp_declarator *declarator;
831
832   /* It is valid to write:
833
834        class C { void f(); };
835        typedef C D;
836        void D::f();
837
838      The standard is not clear about whether `typedef const C D' is
839      legal; as of 2002-09-15 the committee is considering that
840      question.  EDG 3.0 allows that syntax.  Therefore, we do as
841      well.  */
842   if (qualifying_scope && TYPE_P (qualifying_scope))
843     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
844
845   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
846               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
847               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
848
849   declarator = make_declarator (cdk_id);
850   declarator->u.id.qualifying_scope = qualifying_scope;
851   declarator->u.id.unqualified_name = unqualified_name;
852   declarator->u.id.sfk = sfk;
853
854   return declarator;
855 }
856
857 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
858    of modifiers such as const or volatile to apply to the pointer
859    type, represented as identifiers.  */
860
861 cp_declarator *
862 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
863 {
864   cp_declarator *declarator;
865
866   declarator = make_declarator (cdk_pointer);
867   declarator->declarator = target;
868   declarator->u.pointer.qualifiers = cv_qualifiers;
869   declarator->u.pointer.class_type = NULL_TREE;
870
871   return declarator;
872 }
873
874 /* Like make_pointer_declarator -- but for references.  */
875
876 cp_declarator *
877 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
878 {
879   cp_declarator *declarator;
880
881   declarator = make_declarator (cdk_reference);
882   declarator->declarator = target;
883   declarator->u.pointer.qualifiers = cv_qualifiers;
884   declarator->u.pointer.class_type = NULL_TREE;
885
886   return declarator;
887 }
888
889 /* Like make_pointer_declarator -- but for a pointer to a non-static
890    member of CLASS_TYPE.  */
891
892 cp_declarator *
893 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
894                         cp_declarator *pointee)
895 {
896   cp_declarator *declarator;
897
898   declarator = make_declarator (cdk_ptrmem);
899   declarator->declarator = pointee;
900   declarator->u.pointer.qualifiers = cv_qualifiers;
901   declarator->u.pointer.class_type = class_type;
902
903   return declarator;
904 }
905
906 /* Make a declarator for the function given by TARGET, with the
907    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
908    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
909    indicates what exceptions can be thrown.  */
910
911 cp_declarator *
912 make_call_declarator (cp_declarator *target,
913                       cp_parameter_declarator *parms,
914                       cp_cv_quals cv_qualifiers,
915                       tree exception_specification)
916 {
917   cp_declarator *declarator;
918
919   declarator = make_declarator (cdk_function);
920   declarator->declarator = target;
921   declarator->u.function.parameters = parms;
922   declarator->u.function.qualifiers = cv_qualifiers;
923   declarator->u.function.exception_specification = exception_specification;
924
925   return declarator;
926 }
927
928 /* Make a declarator for an array of BOUNDS elements, each of which is
929    defined by ELEMENT.  */
930
931 cp_declarator *
932 make_array_declarator (cp_declarator *element, tree bounds)
933 {
934   cp_declarator *declarator;
935
936   declarator = make_declarator (cdk_array);
937   declarator->declarator = element;
938   declarator->u.array.bounds = bounds;
939
940   return declarator;
941 }
942
943 cp_parameter_declarator *no_parameters;
944
945 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
946    DECLARATOR and DEFAULT_ARGUMENT.  */
947
948 cp_parameter_declarator *
949 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
950                            cp_declarator *declarator,
951                            tree default_argument)
952 {
953   cp_parameter_declarator *parameter;
954
955   parameter = ((cp_parameter_declarator *)
956                alloc_declarator (sizeof (cp_parameter_declarator)));
957   parameter->next = NULL;
958   if (decl_specifiers)
959     parameter->decl_specifiers = *decl_specifiers;
960   else
961     clear_decl_specs (&parameter->decl_specifiers);
962   parameter->declarator = declarator;
963   parameter->default_argument = default_argument;
964   parameter->ellipsis_p = false;
965
966   return parameter;
967 }
968
969 /* The parser.  */
970
971 /* Overview
972    --------
973
974    A cp_parser parses the token stream as specified by the C++
975    grammar.  Its job is purely parsing, not semantic analysis.  For
976    example, the parser breaks the token stream into declarators,
977    expressions, statements, and other similar syntactic constructs.
978    It does not check that the types of the expressions on either side
979    of an assignment-statement are compatible, or that a function is
980    not declared with a parameter of type `void'.
981
982    The parser invokes routines elsewhere in the compiler to perform
983    semantic analysis and to build up the abstract syntax tree for the
984    code processed.
985
986    The parser (and the template instantiation code, which is, in a
987    way, a close relative of parsing) are the only parts of the
988    compiler that should be calling push_scope and pop_scope, or
989    related functions.  The parser (and template instantiation code)
990    keeps track of what scope is presently active; everything else
991    should simply honor that.  (The code that generates static
992    initializers may also need to set the scope, in order to check
993    access control correctly when emitting the initializers.)
994
995    Methodology
996    -----------
997
998    The parser is of the standard recursive-descent variety.  Upcoming
999    tokens in the token stream are examined in order to determine which
1000    production to use when parsing a non-terminal.  Some C++ constructs
1001    require arbitrary look ahead to disambiguate.  For example, it is
1002    impossible, in the general case, to tell whether a statement is an
1003    expression or declaration without scanning the entire statement.
1004    Therefore, the parser is capable of "parsing tentatively."  When the
1005    parser is not sure what construct comes next, it enters this mode.
1006    Then, while we attempt to parse the construct, the parser queues up
1007    error messages, rather than issuing them immediately, and saves the
1008    tokens it consumes.  If the construct is parsed successfully, the
1009    parser "commits", i.e., it issues any queued error messages and
1010    the tokens that were being preserved are permanently discarded.
1011    If, however, the construct is not parsed successfully, the parser
1012    rolls back its state completely so that it can resume parsing using
1013    a different alternative.
1014
1015    Future Improvements
1016    -------------------
1017
1018    The performance of the parser could probably be improved substantially.
1019    We could often eliminate the need to parse tentatively by looking ahead
1020    a little bit.  In some places, this approach might not entirely eliminate
1021    the need to parse tentatively, but it might still speed up the average
1022    case.  */
1023
1024 /* Flags that are passed to some parsing functions.  These values can
1025    be bitwise-ored together.  */
1026
1027 typedef enum cp_parser_flags
1028 {
1029   /* No flags.  */
1030   CP_PARSER_FLAGS_NONE = 0x0,
1031   /* The construct is optional.  If it is not present, then no error
1032      should be issued.  */
1033   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1034   /* When parsing a type-specifier, do not allow user-defined types.  */
1035   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1036 } cp_parser_flags;
1037
1038 /* The different kinds of declarators we want to parse.  */
1039
1040 typedef enum cp_parser_declarator_kind
1041 {
1042   /* We want an abstract declarator.  */
1043   CP_PARSER_DECLARATOR_ABSTRACT,
1044   /* We want a named declarator.  */
1045   CP_PARSER_DECLARATOR_NAMED,
1046   /* We don't mind, but the name must be an unqualified-id.  */
1047   CP_PARSER_DECLARATOR_EITHER
1048 } cp_parser_declarator_kind;
1049
1050 /* The precedence values used to parse binary expressions.  The minimum value
1051    of PREC must be 1, because zero is reserved to quickly discriminate
1052    binary operators from other tokens.  */
1053
1054 enum cp_parser_prec
1055 {
1056   PREC_NOT_OPERATOR,
1057   PREC_LOGICAL_OR_EXPRESSION,
1058   PREC_LOGICAL_AND_EXPRESSION,
1059   PREC_INCLUSIVE_OR_EXPRESSION,
1060   PREC_EXCLUSIVE_OR_EXPRESSION,
1061   PREC_AND_EXPRESSION,
1062   PREC_EQUALITY_EXPRESSION,
1063   PREC_RELATIONAL_EXPRESSION,
1064   PREC_SHIFT_EXPRESSION,
1065   PREC_ADDITIVE_EXPRESSION,
1066   PREC_MULTIPLICATIVE_EXPRESSION,
1067   PREC_PM_EXPRESSION,
1068   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1069 };
1070
1071 /* A mapping from a token type to a corresponding tree node type, with a
1072    precedence value.  */
1073
1074 typedef struct cp_parser_binary_operations_map_node
1075 {
1076   /* The token type.  */
1077   enum cpp_ttype token_type;
1078   /* The corresponding tree code.  */
1079   enum tree_code tree_type;
1080   /* The precedence of this operator.  */
1081   enum cp_parser_prec prec;
1082 } cp_parser_binary_operations_map_node;
1083
1084 /* The status of a tentative parse.  */
1085
1086 typedef enum cp_parser_status_kind
1087 {
1088   /* No errors have occurred.  */
1089   CP_PARSER_STATUS_KIND_NO_ERROR,
1090   /* An error has occurred.  */
1091   CP_PARSER_STATUS_KIND_ERROR,
1092   /* We are committed to this tentative parse, whether or not an error
1093      has occurred.  */
1094   CP_PARSER_STATUS_KIND_COMMITTED
1095 } cp_parser_status_kind;
1096
1097 typedef struct cp_parser_expression_stack_entry
1098 {
1099   tree lhs;
1100   enum tree_code tree_type;
1101   int prec;
1102 } cp_parser_expression_stack_entry;
1103
1104 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1105    entries because precedence levels on the stack are monotonically
1106    increasing.  */
1107 typedef struct cp_parser_expression_stack_entry
1108   cp_parser_expression_stack[NUM_PREC_VALUES];
1109
1110 /* Context that is saved and restored when parsing tentatively.  */
1111 typedef struct cp_parser_context GTY (())
1112 {
1113   /* If this is a tentative parsing context, the status of the
1114      tentative parse.  */
1115   enum cp_parser_status_kind status;
1116   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1117      that are looked up in this context must be looked up both in the
1118      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1119      the context of the containing expression.  */
1120   tree object_type;
1121
1122   /* The next parsing context in the stack.  */
1123   struct cp_parser_context *next;
1124 } cp_parser_context;
1125
1126 /* Prototypes.  */
1127
1128 /* Constructors and destructors.  */
1129
1130 static cp_parser_context *cp_parser_context_new
1131   (cp_parser_context *);
1132
1133 /* Class variables.  */
1134
1135 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1136
1137 /* The operator-precedence table used by cp_parser_binary_expression.
1138    Transformed into an associative array (binops_by_token) by
1139    cp_parser_new.  */
1140
1141 static const cp_parser_binary_operations_map_node binops[] = {
1142   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1143   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1144
1145   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1146   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1147   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1148
1149   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1150   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1151
1152   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1153   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1154
1155   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1156   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1157   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1158   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1159   { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1160   { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1161
1162   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1163   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1164
1165   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1166
1167   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1168
1169   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1170
1171   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1172
1173   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1174 };
1175
1176 /* The same as binops, but initialized by cp_parser_new so that
1177    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1178    for speed.  */
1179 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1180
1181 /* Constructors and destructors.  */
1182
1183 /* Construct a new context.  The context below this one on the stack
1184    is given by NEXT.  */
1185
1186 static cp_parser_context *
1187 cp_parser_context_new (cp_parser_context* next)
1188 {
1189   cp_parser_context *context;
1190
1191   /* Allocate the storage.  */
1192   if (cp_parser_context_free_list != NULL)
1193     {
1194       /* Pull the first entry from the free list.  */
1195       context = cp_parser_context_free_list;
1196       cp_parser_context_free_list = context->next;
1197       memset (context, 0, sizeof (*context));
1198     }
1199   else
1200     context = GGC_CNEW (cp_parser_context);
1201
1202   /* No errors have occurred yet in this context.  */
1203   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1204   /* If this is not the bottomost context, copy information that we
1205      need from the previous context.  */
1206   if (next)
1207     {
1208       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1209          expression, then we are parsing one in this context, too.  */
1210       context->object_type = next->object_type;
1211       /* Thread the stack.  */
1212       context->next = next;
1213     }
1214
1215   return context;
1216 }
1217
1218 /* The cp_parser structure represents the C++ parser.  */
1219
1220 typedef struct cp_parser GTY(())
1221 {
1222   /* The lexer from which we are obtaining tokens.  */
1223   cp_lexer *lexer;
1224
1225   /* The scope in which names should be looked up.  If NULL_TREE, then
1226      we look up names in the scope that is currently open in the
1227      source program.  If non-NULL, this is either a TYPE or
1228      NAMESPACE_DECL for the scope in which we should look.  It can
1229      also be ERROR_MARK, when we've parsed a bogus scope.
1230
1231      This value is not cleared automatically after a name is looked
1232      up, so we must be careful to clear it before starting a new look
1233      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1234      will look up `Z' in the scope of `X', rather than the current
1235      scope.)  Unfortunately, it is difficult to tell when name lookup
1236      is complete, because we sometimes peek at a token, look it up,
1237      and then decide not to consume it.   */
1238   tree scope;
1239
1240   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1241      last lookup took place.  OBJECT_SCOPE is used if an expression
1242      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1243      respectively.  QUALIFYING_SCOPE is used for an expression of the
1244      form "X::Y"; it refers to X.  */
1245   tree object_scope;
1246   tree qualifying_scope;
1247
1248   /* A stack of parsing contexts.  All but the bottom entry on the
1249      stack will be tentative contexts.
1250
1251      We parse tentatively in order to determine which construct is in
1252      use in some situations.  For example, in order to determine
1253      whether a statement is an expression-statement or a
1254      declaration-statement we parse it tentatively as a
1255      declaration-statement.  If that fails, we then reparse the same
1256      token stream as an expression-statement.  */
1257   cp_parser_context *context;
1258
1259   /* True if we are parsing GNU C++.  If this flag is not set, then
1260      GNU extensions are not recognized.  */
1261   bool allow_gnu_extensions_p;
1262
1263   /* TRUE if the `>' token should be interpreted as the greater-than
1264      operator.  FALSE if it is the end of a template-id or
1265      template-parameter-list.  */
1266   bool greater_than_is_operator_p;
1267
1268   /* TRUE if default arguments are allowed within a parameter list
1269      that starts at this point. FALSE if only a gnu extension makes
1270      them permissible.  */
1271   bool default_arg_ok_p;
1272
1273   /* TRUE if we are parsing an integral constant-expression.  See
1274      [expr.const] for a precise definition.  */
1275   bool integral_constant_expression_p;
1276
1277   /* TRUE if we are parsing an integral constant-expression -- but a
1278      non-constant expression should be permitted as well.  This flag
1279      is used when parsing an array bound so that GNU variable-length
1280      arrays are tolerated.  */
1281   bool allow_non_integral_constant_expression_p;
1282
1283   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1284      been seen that makes the expression non-constant.  */
1285   bool non_integral_constant_expression_p;
1286
1287   /* TRUE if local variable names and `this' are forbidden in the
1288      current context.  */
1289   bool local_variables_forbidden_p;
1290
1291   /* TRUE if the declaration we are parsing is part of a
1292      linkage-specification of the form `extern string-literal
1293      declaration'.  */
1294   bool in_unbraced_linkage_specification_p;
1295
1296   /* TRUE if we are presently parsing a declarator, after the
1297      direct-declarator.  */
1298   bool in_declarator_p;
1299
1300   /* TRUE if we are presently parsing a template-argument-list.  */
1301   bool in_template_argument_list_p;
1302
1303   /* TRUE if we are presently parsing the body of an
1304      iteration-statement.  */
1305   bool in_iteration_statement_p;
1306
1307   /* TRUE if we are presently parsing the body of a switch
1308      statement.  */
1309   bool in_switch_statement_p;
1310
1311   /* TRUE if we are parsing a type-id in an expression context.  In
1312      such a situation, both "type (expr)" and "type (type)" are valid
1313      alternatives.  */
1314   bool in_type_id_in_expr_p;
1315
1316   /* TRUE if we are currently in a header file where declarations are
1317      implicitly extern "C".  */
1318   bool implicit_extern_c;
1319
1320   /* TRUE if strings in expressions should be translated to the execution
1321      character set.  */
1322   bool translate_strings_p;
1323
1324   /* If non-NULL, then we are parsing a construct where new type
1325      definitions are not permitted.  The string stored here will be
1326      issued as an error message if a type is defined.  */
1327   const char *type_definition_forbidden_message;
1328
1329   /* A list of lists. The outer list is a stack, used for member
1330      functions of local classes. At each level there are two sub-list,
1331      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1332      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1333      TREE_VALUE's. The functions are chained in reverse declaration
1334      order.
1335
1336      The TREE_PURPOSE sublist contains those functions with default
1337      arguments that need post processing, and the TREE_VALUE sublist
1338      contains those functions with definitions that need post
1339      processing.
1340
1341      These lists can only be processed once the outermost class being
1342      defined is complete.  */
1343   tree unparsed_functions_queues;
1344
1345   /* The number of classes whose definitions are currently in
1346      progress.  */
1347   unsigned num_classes_being_defined;
1348
1349   /* The number of template parameter lists that apply directly to the
1350      current declaration.  */
1351   unsigned num_template_parameter_lists;
1352 } cp_parser;
1353
1354 /* The type of a function that parses some kind of expression.  */
1355 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1356
1357 /* Prototypes.  */
1358
1359 /* Constructors and destructors.  */
1360
1361 static cp_parser *cp_parser_new
1362   (void);
1363
1364 /* Routines to parse various constructs.
1365
1366    Those that return `tree' will return the error_mark_node (rather
1367    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1368    Sometimes, they will return an ordinary node if error-recovery was
1369    attempted, even though a parse error occurred.  So, to check
1370    whether or not a parse error occurred, you should always use
1371    cp_parser_error_occurred.  If the construct is optional (indicated
1372    either by an `_opt' in the name of the function that does the
1373    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1374    the construct is not present.  */
1375
1376 /* Lexical conventions [gram.lex]  */
1377
1378 static tree cp_parser_identifier
1379   (cp_parser *);
1380 static tree cp_parser_string_literal
1381   (cp_parser *, bool, bool);
1382
1383 /* Basic concepts [gram.basic]  */
1384
1385 static bool cp_parser_translation_unit
1386   (cp_parser *);
1387
1388 /* Expressions [gram.expr]  */
1389
1390 static tree cp_parser_primary_expression
1391   (cp_parser *, bool, bool, bool, cp_id_kind *);
1392 static tree cp_parser_id_expression
1393   (cp_parser *, bool, bool, bool *, bool);
1394 static tree cp_parser_unqualified_id
1395   (cp_parser *, bool, bool, bool);
1396 static tree cp_parser_nested_name_specifier_opt
1397   (cp_parser *, bool, bool, bool, bool);
1398 static tree cp_parser_nested_name_specifier
1399   (cp_parser *, bool, bool, bool, bool);
1400 static tree cp_parser_class_or_namespace_name
1401   (cp_parser *, bool, bool, bool, bool, bool);
1402 static tree cp_parser_postfix_expression
1403   (cp_parser *, bool, bool);
1404 static tree cp_parser_postfix_open_square_expression
1405   (cp_parser *, tree, bool);
1406 static tree cp_parser_postfix_dot_deref_expression
1407   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1408 static tree cp_parser_parenthesized_expression_list
1409   (cp_parser *, bool, bool, bool *);
1410 static void cp_parser_pseudo_destructor_name
1411   (cp_parser *, tree *, tree *);
1412 static tree cp_parser_unary_expression
1413   (cp_parser *, bool, bool);
1414 static enum tree_code cp_parser_unary_operator
1415   (cp_token *);
1416 static tree cp_parser_new_expression
1417   (cp_parser *);
1418 static tree cp_parser_new_placement
1419   (cp_parser *);
1420 static tree cp_parser_new_type_id
1421   (cp_parser *, tree *);
1422 static cp_declarator *cp_parser_new_declarator_opt
1423   (cp_parser *);
1424 static cp_declarator *cp_parser_direct_new_declarator
1425   (cp_parser *);
1426 static tree cp_parser_new_initializer
1427   (cp_parser *);
1428 static tree cp_parser_delete_expression
1429   (cp_parser *);
1430 static tree cp_parser_cast_expression
1431   (cp_parser *, bool, bool);
1432 static tree cp_parser_binary_expression
1433   (cp_parser *, bool);
1434 static tree cp_parser_question_colon_clause
1435   (cp_parser *, tree);
1436 static tree cp_parser_assignment_expression
1437   (cp_parser *, bool);
1438 static enum tree_code cp_parser_assignment_operator_opt
1439   (cp_parser *);
1440 static tree cp_parser_expression
1441   (cp_parser *, bool);
1442 static tree cp_parser_constant_expression
1443   (cp_parser *, bool, bool *);
1444 static tree cp_parser_builtin_offsetof
1445   (cp_parser *);
1446
1447 /* Statements [gram.stmt.stmt]  */
1448
1449 static void cp_parser_statement
1450   (cp_parser *, tree, bool);
1451 static tree cp_parser_labeled_statement
1452   (cp_parser *, tree, bool);
1453 static tree cp_parser_expression_statement
1454   (cp_parser *, tree);
1455 static tree cp_parser_compound_statement
1456   (cp_parser *, tree, bool);
1457 static void cp_parser_statement_seq_opt
1458   (cp_parser *, tree);
1459 static tree cp_parser_selection_statement
1460   (cp_parser *);
1461 static tree cp_parser_condition
1462   (cp_parser *);
1463 static tree cp_parser_iteration_statement
1464   (cp_parser *);
1465 static void cp_parser_for_init_statement
1466   (cp_parser *);
1467 static tree cp_parser_jump_statement
1468   (cp_parser *);
1469 static void cp_parser_declaration_statement
1470   (cp_parser *);
1471
1472 static tree cp_parser_implicitly_scoped_statement
1473   (cp_parser *);
1474 static void cp_parser_already_scoped_statement
1475   (cp_parser *);
1476
1477 /* Declarations [gram.dcl.dcl] */
1478
1479 static void cp_parser_declaration_seq_opt
1480   (cp_parser *);
1481 static void cp_parser_declaration
1482   (cp_parser *);
1483 static void cp_parser_block_declaration
1484   (cp_parser *, bool);
1485 static void cp_parser_simple_declaration
1486   (cp_parser *, bool);
1487 static void cp_parser_decl_specifier_seq
1488   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1489 static tree cp_parser_storage_class_specifier_opt
1490   (cp_parser *);
1491 static tree cp_parser_function_specifier_opt
1492   (cp_parser *, cp_decl_specifier_seq *);
1493 static tree cp_parser_type_specifier
1494   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1495    int *, bool *);
1496 static tree cp_parser_simple_type_specifier
1497   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1498 static tree cp_parser_type_name
1499   (cp_parser *);
1500 static tree cp_parser_elaborated_type_specifier
1501   (cp_parser *, bool, bool);
1502 static tree cp_parser_enum_specifier
1503   (cp_parser *);
1504 static void cp_parser_enumerator_list
1505   (cp_parser *, tree);
1506 static void cp_parser_enumerator_definition
1507   (cp_parser *, tree);
1508 static tree cp_parser_namespace_name
1509   (cp_parser *);
1510 static void cp_parser_namespace_definition
1511   (cp_parser *);
1512 static void cp_parser_namespace_body
1513   (cp_parser *);
1514 static tree cp_parser_qualified_namespace_specifier
1515   (cp_parser *);
1516 static void cp_parser_namespace_alias_definition
1517   (cp_parser *);
1518 static void cp_parser_using_declaration
1519   (cp_parser *);
1520 static void cp_parser_using_directive
1521   (cp_parser *);
1522 static void cp_parser_asm_definition
1523   (cp_parser *);
1524 static void cp_parser_linkage_specification
1525   (cp_parser *);
1526
1527 /* Declarators [gram.dcl.decl] */
1528
1529 static tree cp_parser_init_declarator
1530   (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1531 static cp_declarator *cp_parser_declarator
1532   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1533 static cp_declarator *cp_parser_direct_declarator
1534   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1535 static enum tree_code cp_parser_ptr_operator
1536   (cp_parser *, tree *, cp_cv_quals *);
1537 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1538   (cp_parser *);
1539 static tree cp_parser_declarator_id
1540   (cp_parser *);
1541 static tree cp_parser_type_id
1542   (cp_parser *);
1543 static void cp_parser_type_specifier_seq
1544   (cp_parser *, bool, cp_decl_specifier_seq *);
1545 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1546   (cp_parser *);
1547 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1548   (cp_parser *, bool *);
1549 static cp_parameter_declarator *cp_parser_parameter_declaration
1550   (cp_parser *, bool, bool *);
1551 static void cp_parser_function_body
1552   (cp_parser *);
1553 static tree cp_parser_initializer
1554   (cp_parser *, bool *, bool *);
1555 static tree cp_parser_initializer_clause
1556   (cp_parser *, bool *);
1557 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1558   (cp_parser *, bool *);
1559
1560 static bool cp_parser_ctor_initializer_opt_and_function_body
1561   (cp_parser *);
1562
1563 /* Classes [gram.class] */
1564
1565 static tree cp_parser_class_name
1566   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1567 static tree cp_parser_class_specifier
1568   (cp_parser *);
1569 static tree cp_parser_class_head
1570   (cp_parser *, bool *, tree *);
1571 static enum tag_types cp_parser_class_key
1572   (cp_parser *);
1573 static void cp_parser_member_specification_opt
1574   (cp_parser *);
1575 static void cp_parser_member_declaration
1576   (cp_parser *);
1577 static tree cp_parser_pure_specifier
1578   (cp_parser *);
1579 static tree cp_parser_constant_initializer
1580   (cp_parser *);
1581
1582 /* Derived classes [gram.class.derived] */
1583
1584 static tree cp_parser_base_clause
1585   (cp_parser *);
1586 static tree cp_parser_base_specifier
1587   (cp_parser *);
1588
1589 /* Special member functions [gram.special] */
1590
1591 static tree cp_parser_conversion_function_id
1592   (cp_parser *);
1593 static tree cp_parser_conversion_type_id
1594   (cp_parser *);
1595 static cp_declarator *cp_parser_conversion_declarator_opt
1596   (cp_parser *);
1597 static bool cp_parser_ctor_initializer_opt
1598   (cp_parser *);
1599 static void cp_parser_mem_initializer_list
1600   (cp_parser *);
1601 static tree cp_parser_mem_initializer
1602   (cp_parser *);
1603 static tree cp_parser_mem_initializer_id
1604   (cp_parser *);
1605
1606 /* Overloading [gram.over] */
1607
1608 static tree cp_parser_operator_function_id
1609   (cp_parser *);
1610 static tree cp_parser_operator
1611   (cp_parser *);
1612
1613 /* Templates [gram.temp] */
1614
1615 static void cp_parser_template_declaration
1616   (cp_parser *, bool);
1617 static tree cp_parser_template_parameter_list
1618   (cp_parser *);
1619 static tree cp_parser_template_parameter
1620   (cp_parser *, bool *);
1621 static tree cp_parser_type_parameter
1622   (cp_parser *);
1623 static tree cp_parser_template_id
1624   (cp_parser *, bool, bool, bool);
1625 static tree cp_parser_template_name
1626   (cp_parser *, bool, bool, bool, bool *);
1627 static tree cp_parser_template_argument_list
1628   (cp_parser *);
1629 static tree cp_parser_template_argument
1630   (cp_parser *);
1631 static void cp_parser_explicit_instantiation
1632   (cp_parser *);
1633 static void cp_parser_explicit_specialization
1634   (cp_parser *);
1635
1636 /* Exception handling [gram.exception] */
1637
1638 static tree cp_parser_try_block
1639   (cp_parser *);
1640 static bool cp_parser_function_try_block
1641   (cp_parser *);
1642 static void cp_parser_handler_seq
1643   (cp_parser *);
1644 static void cp_parser_handler
1645   (cp_parser *);
1646 static tree cp_parser_exception_declaration
1647   (cp_parser *);
1648 static tree cp_parser_throw_expression
1649   (cp_parser *);
1650 static tree cp_parser_exception_specification_opt
1651   (cp_parser *);
1652 static tree cp_parser_type_id_list
1653   (cp_parser *);
1654
1655 /* GNU Extensions */
1656
1657 static tree cp_parser_asm_specification_opt
1658   (cp_parser *);
1659 static tree cp_parser_asm_operand_list
1660   (cp_parser *);
1661 static tree cp_parser_asm_clobber_list
1662   (cp_parser *);
1663 static tree cp_parser_attributes_opt
1664   (cp_parser *);
1665 static tree cp_parser_attribute_list
1666   (cp_parser *);
1667 static bool cp_parser_extension_opt
1668   (cp_parser *, int *);
1669 static void cp_parser_label_declaration
1670   (cp_parser *);
1671
1672 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1673 static bool cp_parser_pragma
1674   (cp_parser *, enum pragma_context);
1675
1676 /* Objective-C++ Productions */
1677
1678 static tree cp_parser_objc_message_receiver
1679   (cp_parser *);
1680 static tree cp_parser_objc_message_args
1681   (cp_parser *);
1682 static tree cp_parser_objc_message_expression
1683   (cp_parser *);
1684 static tree cp_parser_objc_encode_expression
1685   (cp_parser *);
1686 static tree cp_parser_objc_defs_expression
1687   (cp_parser *);
1688 static tree cp_parser_objc_protocol_expression
1689   (cp_parser *);
1690 static tree cp_parser_objc_selector_expression
1691   (cp_parser *);
1692 static tree cp_parser_objc_expression
1693   (cp_parser *);
1694 static bool cp_parser_objc_selector_p
1695   (enum cpp_ttype);
1696 static tree cp_parser_objc_selector
1697   (cp_parser *);
1698 static tree cp_parser_objc_protocol_refs_opt
1699   (cp_parser *);
1700 static void cp_parser_objc_declaration
1701   (cp_parser *);
1702 static tree cp_parser_objc_statement
1703   (cp_parser *);
1704
1705 /* Utility Routines */
1706
1707 static tree cp_parser_lookup_name
1708   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1709 static tree cp_parser_lookup_name_simple
1710   (cp_parser *, tree);
1711 static tree cp_parser_maybe_treat_template_as_class
1712   (tree, bool);
1713 static bool cp_parser_check_declarator_template_parameters
1714   (cp_parser *, cp_declarator *);
1715 static bool cp_parser_check_template_parameters
1716   (cp_parser *, unsigned);
1717 static tree cp_parser_simple_cast_expression
1718   (cp_parser *);
1719 static tree cp_parser_global_scope_opt
1720   (cp_parser *, bool);
1721 static bool cp_parser_constructor_declarator_p
1722   (cp_parser *, bool);
1723 static tree cp_parser_function_definition_from_specifiers_and_declarator
1724   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1725 static tree cp_parser_function_definition_after_declarator
1726   (cp_parser *, bool);
1727 static void cp_parser_template_declaration_after_export
1728   (cp_parser *, bool);
1729 static tree cp_parser_single_declaration
1730   (cp_parser *, bool, bool *);
1731 static tree cp_parser_functional_cast
1732   (cp_parser *, tree);
1733 static tree cp_parser_save_member_function_body
1734   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1735 static tree cp_parser_enclosed_template_argument_list
1736   (cp_parser *);
1737 static void cp_parser_save_default_args
1738   (cp_parser *, tree);
1739 static void cp_parser_late_parsing_for_member
1740   (cp_parser *, tree);
1741 static void cp_parser_late_parsing_default_args
1742   (cp_parser *, tree);
1743 static tree cp_parser_sizeof_operand
1744   (cp_parser *, enum rid);
1745 static bool cp_parser_declares_only_class_p
1746   (cp_parser *);
1747 static void cp_parser_set_storage_class
1748   (cp_decl_specifier_seq *, cp_storage_class);
1749 static void cp_parser_set_decl_spec_type
1750   (cp_decl_specifier_seq *, tree, bool);
1751 static bool cp_parser_friend_p
1752   (const cp_decl_specifier_seq *);
1753 static cp_token *cp_parser_require
1754   (cp_parser *, enum cpp_ttype, const char *);
1755 static cp_token *cp_parser_require_keyword
1756   (cp_parser *, enum rid, const char *);
1757 static bool cp_parser_token_starts_function_definition_p
1758   (cp_token *);
1759 static bool cp_parser_next_token_starts_class_definition_p
1760   (cp_parser *);
1761 static bool cp_parser_next_token_ends_template_argument_p
1762   (cp_parser *);
1763 static bool cp_parser_nth_token_starts_template_argument_list_p
1764   (cp_parser *, size_t);
1765 static enum tag_types cp_parser_token_is_class_key
1766   (cp_token *);
1767 static void cp_parser_check_class_key
1768   (enum tag_types, tree type);
1769 static void cp_parser_check_access_in_redeclaration
1770   (tree type);
1771 static bool cp_parser_optional_template_keyword
1772   (cp_parser *);
1773 static void cp_parser_pre_parsed_nested_name_specifier
1774   (cp_parser *);
1775 static void cp_parser_cache_group
1776   (cp_parser *, enum cpp_ttype, unsigned);
1777 static void cp_parser_parse_tentatively
1778   (cp_parser *);
1779 static void cp_parser_commit_to_tentative_parse
1780   (cp_parser *);
1781 static void cp_parser_abort_tentative_parse
1782   (cp_parser *);
1783 static bool cp_parser_parse_definitely
1784   (cp_parser *);
1785 static inline bool cp_parser_parsing_tentatively
1786   (cp_parser *);
1787 static bool cp_parser_uncommitted_to_tentative_parse_p
1788   (cp_parser *);
1789 static void cp_parser_error
1790   (cp_parser *, const char *);
1791 static void cp_parser_name_lookup_error
1792   (cp_parser *, tree, tree, const char *);
1793 static bool cp_parser_simulate_error
1794   (cp_parser *);
1795 static void cp_parser_check_type_definition
1796   (cp_parser *);
1797 static void cp_parser_check_for_definition_in_return_type
1798   (cp_declarator *, tree);
1799 static void cp_parser_check_for_invalid_template_id
1800   (cp_parser *, tree);
1801 static bool cp_parser_non_integral_constant_expression
1802   (cp_parser *, const char *);
1803 static void cp_parser_diagnose_invalid_type_name
1804   (cp_parser *, tree, tree);
1805 static bool cp_parser_parse_and_diagnose_invalid_type_name
1806   (cp_parser *);
1807 static int cp_parser_skip_to_closing_parenthesis
1808   (cp_parser *, bool, bool, bool);
1809 static void cp_parser_skip_to_end_of_statement
1810   (cp_parser *);
1811 static void cp_parser_consume_semicolon_at_end_of_statement
1812   (cp_parser *);
1813 static void cp_parser_skip_to_end_of_block_or_statement
1814   (cp_parser *);
1815 static void cp_parser_skip_to_closing_brace
1816   (cp_parser *);
1817 static void cp_parser_skip_until_found
1818   (cp_parser *, enum cpp_ttype, const char *);
1819 static void cp_parser_skip_to_pragma_eol
1820   (cp_parser*, cp_token *);
1821 static bool cp_parser_error_occurred
1822   (cp_parser *);
1823 static bool cp_parser_allow_gnu_extensions_p
1824   (cp_parser *);
1825 static bool cp_parser_is_string_literal
1826   (cp_token *);
1827 static bool cp_parser_is_keyword
1828   (cp_token *, enum rid);
1829 static tree cp_parser_make_typename_type
1830   (cp_parser *, tree, tree);
1831
1832 /* Returns nonzero if we are parsing tentatively.  */
1833
1834 static inline bool
1835 cp_parser_parsing_tentatively (cp_parser* parser)
1836 {
1837   return parser->context->next != NULL;
1838 }
1839
1840 /* Returns nonzero if TOKEN is a string literal.  */
1841
1842 static bool
1843 cp_parser_is_string_literal (cp_token* token)
1844 {
1845   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1846 }
1847
1848 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1849
1850 static bool
1851 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1852 {
1853   return token->keyword == keyword;
1854 }
1855
1856 /* A minimum or maximum operator has been seen.  As these are
1857    deprecated, issue a warning.  */
1858
1859 static inline void
1860 cp_parser_warn_min_max (void)
1861 {
1862   if (warn_deprecated && !in_system_header)
1863     warning (0, "minimum/maximum operators are deprecated");
1864 }
1865
1866 /* If not parsing tentatively, issue a diagnostic of the form
1867       FILE:LINE: MESSAGE before TOKEN
1868    where TOKEN is the next token in the input stream.  MESSAGE
1869    (specified by the caller) is usually of the form "expected
1870    OTHER-TOKEN".  */
1871
1872 static void
1873 cp_parser_error (cp_parser* parser, const char* message)
1874 {
1875   if (!cp_parser_simulate_error (parser))
1876     {
1877       cp_token *token = cp_lexer_peek_token (parser->lexer);
1878       /* This diagnostic makes more sense if it is tagged to the line
1879          of the token we just peeked at.  */
1880       cp_lexer_set_source_position_from_token (token);
1881
1882       if (token->type == CPP_PRAGMA)
1883         {
1884           error ("%<#pragma%> is not allowed here");
1885           cp_parser_skip_to_pragma_eol (parser, token);
1886           return;
1887         }
1888
1889       c_parse_error (message,
1890                      /* Because c_parser_error does not understand
1891                         CPP_KEYWORD, keywords are treated like
1892                         identifiers.  */
1893                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1894                      token->value);
1895     }
1896 }
1897
1898 /* Issue an error about name-lookup failing.  NAME is the
1899    IDENTIFIER_NODE DECL is the result of
1900    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1901    the thing that we hoped to find.  */
1902
1903 static void
1904 cp_parser_name_lookup_error (cp_parser* parser,
1905                              tree name,
1906                              tree decl,
1907                              const char* desired)
1908 {
1909   /* If name lookup completely failed, tell the user that NAME was not
1910      declared.  */
1911   if (decl == error_mark_node)
1912     {
1913       if (parser->scope && parser->scope != global_namespace)
1914         error ("%<%D::%D%> has not been declared",
1915                parser->scope, name);
1916       else if (parser->scope == global_namespace)
1917         error ("%<::%D%> has not been declared", name);
1918       else if (parser->object_scope
1919                && !CLASS_TYPE_P (parser->object_scope))
1920         error ("request for member %qD in non-class type %qT",
1921                name, parser->object_scope);
1922       else if (parser->object_scope)
1923         error ("%<%T::%D%> has not been declared",
1924                parser->object_scope, name);
1925       else
1926         error ("%qD has not been declared", name);
1927     }
1928   else if (parser->scope && parser->scope != global_namespace)
1929     error ("%<%D::%D%> %s", parser->scope, name, desired);
1930   else if (parser->scope == global_namespace)
1931     error ("%<::%D%> %s", name, desired);
1932   else
1933     error ("%qD %s", name, desired);
1934 }
1935
1936 /* If we are parsing tentatively, remember that an error has occurred
1937    during this tentative parse.  Returns true if the error was
1938    simulated; false if a message should be issued by the caller.  */
1939
1940 static bool
1941 cp_parser_simulate_error (cp_parser* parser)
1942 {
1943   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1944     {
1945       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1946       return true;
1947     }
1948   return false;
1949 }
1950
1951 /* This function is called when a type is defined.  If type
1952    definitions are forbidden at this point, an error message is
1953    issued.  */
1954
1955 static void
1956 cp_parser_check_type_definition (cp_parser* parser)
1957 {
1958   /* If types are forbidden here, issue a message.  */
1959   if (parser->type_definition_forbidden_message)
1960     /* Use `%s' to print the string in case there are any escape
1961        characters in the message.  */
1962     error ("%s", parser->type_definition_forbidden_message);
1963 }
1964
1965 /* This function is called when the DECLARATOR is processed.  The TYPE
1966    was a type defined in the decl-specifiers.  If it is invalid to
1967    define a type in the decl-specifiers for DECLARATOR, an error is
1968    issued.  */
1969
1970 static void
1971 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1972                                                tree type)
1973 {
1974   /* [dcl.fct] forbids type definitions in return types.
1975      Unfortunately, it's not easy to know whether or not we are
1976      processing a return type until after the fact.  */
1977   while (declarator
1978          && (declarator->kind == cdk_pointer
1979              || declarator->kind == cdk_reference
1980              || declarator->kind == cdk_ptrmem))
1981     declarator = declarator->declarator;
1982   if (declarator
1983       && declarator->kind == cdk_function)
1984     {
1985       error ("new types may not be defined in a return type");
1986       inform ("(perhaps a semicolon is missing after the definition of %qT)",
1987               type);
1988     }
1989 }
1990
1991 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1992    "<" in any valid C++ program.  If the next token is indeed "<",
1993    issue a message warning the user about what appears to be an
1994    invalid attempt to form a template-id.  */
1995
1996 static void
1997 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1998                                          tree type)
1999 {
2000   cp_token_position start = 0;
2001
2002   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2003     {
2004       if (TYPE_P (type))
2005         error ("%qT is not a template", type);
2006       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2007         error ("%qE is not a template", type);
2008       else
2009         error ("invalid template-id");
2010       /* Remember the location of the invalid "<".  */
2011       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2012         start = cp_lexer_token_position (parser->lexer, true);
2013       /* Consume the "<".  */
2014       cp_lexer_consume_token (parser->lexer);
2015       /* Parse the template arguments.  */
2016       cp_parser_enclosed_template_argument_list (parser);
2017       /* Permanently remove the invalid template arguments so that
2018          this error message is not issued again.  */
2019       if (start)
2020         cp_lexer_purge_tokens_after (parser->lexer, start);
2021     }
2022 }
2023
2024 /* If parsing an integral constant-expression, issue an error message
2025    about the fact that THING appeared and return true.  Otherwise,
2026    return false.  In either case, set
2027    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2028
2029 static bool
2030 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2031                                             const char *thing)
2032 {
2033   parser->non_integral_constant_expression_p = true;
2034   if (parser->integral_constant_expression_p)
2035     {
2036       if (!parser->allow_non_integral_constant_expression_p)
2037         {
2038           error ("%s cannot appear in a constant-expression", thing);
2039           return true;
2040         }
2041     }
2042   return false;
2043 }
2044
2045 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2046    qualifying scope (or NULL, if none) for ID.  This function commits
2047    to the current active tentative parse, if any.  (Otherwise, the
2048    problematic construct might be encountered again later, resulting
2049    in duplicate error messages.)  */
2050
2051 static void
2052 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2053 {
2054   tree decl, old_scope;
2055   /* Try to lookup the identifier.  */
2056   old_scope = parser->scope;
2057   parser->scope = scope;
2058   decl = cp_parser_lookup_name_simple (parser, id);
2059   parser->scope = old_scope;
2060   /* If the lookup found a template-name, it means that the user forgot
2061   to specify an argument list. Emit a useful error message.  */
2062   if (TREE_CODE (decl) == TEMPLATE_DECL)
2063     error ("invalid use of template-name %qE without an argument list",
2064       decl);
2065   else if (!parser->scope)
2066     {
2067       /* Issue an error message.  */
2068       error ("%qE does not name a type", id);
2069       /* If we're in a template class, it's possible that the user was
2070          referring to a type from a base class.  For example:
2071
2072            template <typename T> struct A { typedef T X; };
2073            template <typename T> struct B : public A<T> { X x; };
2074
2075          The user should have said "typename A<T>::X".  */
2076       if (processing_template_decl && current_class_type
2077           && TYPE_BINFO (current_class_type))
2078         {
2079           tree b;
2080
2081           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2082                b;
2083                b = TREE_CHAIN (b))
2084             {
2085               tree base_type = BINFO_TYPE (b);
2086               if (CLASS_TYPE_P (base_type)
2087                   && dependent_type_p (base_type))
2088                 {
2089                   tree field;
2090                   /* Go from a particular instantiation of the
2091                      template (which will have an empty TYPE_FIELDs),
2092                      to the main version.  */
2093                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2094                   for (field = TYPE_FIELDS (base_type);
2095                        field;
2096                        field = TREE_CHAIN (field))
2097                     if (TREE_CODE (field) == TYPE_DECL
2098                         && DECL_NAME (field) == id)
2099                       {
2100                         inform ("(perhaps %<typename %T::%E%> was intended)",
2101                                 BINFO_TYPE (b), id);
2102                         break;
2103                       }
2104                   if (field)
2105                     break;
2106                 }
2107             }
2108         }
2109     }
2110   /* Here we diagnose qualified-ids where the scope is actually correct,
2111      but the identifier does not resolve to a valid type name.  */
2112   else if (parser->scope != error_mark_node)
2113     {
2114       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2115         error ("%qE in namespace %qE does not name a type",
2116                id, parser->scope);
2117       else if (TYPE_P (parser->scope))
2118         error ("%qE in class %qT does not name a type", id, parser->scope);
2119       else
2120         gcc_unreachable ();
2121     }
2122   cp_parser_commit_to_tentative_parse (parser);
2123 }
2124
2125 /* Check for a common situation where a type-name should be present,
2126    but is not, and issue a sensible error message.  Returns true if an
2127    invalid type-name was detected.
2128
2129    The situation handled by this function are variable declarations of the
2130    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2131    Usually, `ID' should name a type, but if we got here it means that it
2132    does not. We try to emit the best possible error message depending on
2133    how exactly the id-expression looks like.
2134 */
2135
2136 static bool
2137 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2138 {
2139   tree id;
2140
2141   cp_parser_parse_tentatively (parser);
2142   id = cp_parser_id_expression (parser,
2143                                 /*template_keyword_p=*/false,
2144                                 /*check_dependency_p=*/true,
2145                                 /*template_p=*/NULL,
2146                                 /*declarator_p=*/true);
2147   /* After the id-expression, there should be a plain identifier,
2148      otherwise this is not a simple variable declaration. Also, if
2149      the scope is dependent, we cannot do much.  */
2150   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2151       || (parser->scope && TYPE_P (parser->scope)
2152           && dependent_type_p (parser->scope)))
2153     {
2154       cp_parser_abort_tentative_parse (parser);
2155       return false;
2156     }
2157   if (!cp_parser_parse_definitely (parser)
2158       || TREE_CODE (id) != IDENTIFIER_NODE)
2159     return false;
2160
2161   /* Emit a diagnostic for the invalid type.  */
2162   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2163   /* Skip to the end of the declaration; there's no point in
2164      trying to process it.  */
2165   cp_parser_skip_to_end_of_block_or_statement (parser);
2166   return true;
2167 }
2168
2169 /* Consume tokens up to, and including, the next non-nested closing `)'.
2170    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2171    are doing error recovery. Returns -1 if OR_COMMA is true and we
2172    found an unnested comma.  */
2173
2174 static int
2175 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2176                                        bool recovering,
2177                                        bool or_comma,
2178                                        bool consume_paren)
2179 {
2180   unsigned paren_depth = 0;
2181   unsigned brace_depth = 0;
2182
2183   if (recovering && !or_comma
2184       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2185     return 0;
2186
2187   while (true)
2188     {
2189       cp_token * token = cp_lexer_peek_token (parser->lexer);
2190
2191       switch (token->type)
2192         {
2193         case CPP_EOF:
2194         case CPP_PRAGMA_EOL:
2195           /* If we've run out of tokens, then there is no closing `)'.  */
2196           return 0;
2197
2198         case CPP_SEMICOLON:
2199           /* This matches the processing in skip_to_end_of_statement.  */
2200           if (!brace_depth)
2201             return 0;
2202           break;
2203
2204         case CPP_OPEN_BRACE:
2205           ++brace_depth;
2206           break;
2207         case CPP_CLOSE_BRACE:
2208           if (!brace_depth--)
2209             return 0;
2210           break;
2211
2212         case CPP_COMMA:
2213           if (recovering && or_comma && !brace_depth && !paren_depth)
2214             return -1;
2215           break;
2216
2217         case CPP_OPEN_PAREN:
2218           if (!brace_depth)
2219             ++paren_depth;
2220           break;
2221
2222         case CPP_CLOSE_PAREN:
2223           if (!brace_depth && !paren_depth--)
2224             {
2225               if (consume_paren)
2226                 cp_lexer_consume_token (parser->lexer);
2227               return 1;
2228             }
2229           break;
2230
2231         default:
2232           break;
2233         }
2234
2235       /* Consume the token.  */
2236       cp_lexer_consume_token (parser->lexer);
2237     }
2238 }
2239
2240 /* Consume tokens until we reach the end of the current statement.
2241    Normally, that will be just before consuming a `;'.  However, if a
2242    non-nested `}' comes first, then we stop before consuming that.  */
2243
2244 static void
2245 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2246 {
2247   unsigned nesting_depth = 0;
2248
2249   while (true)
2250     {
2251       cp_token *token = cp_lexer_peek_token (parser->lexer);
2252
2253       switch (token->type)
2254         {
2255         case CPP_EOF:
2256         case CPP_PRAGMA_EOL:
2257           /* If we've run out of tokens, stop.  */
2258           return;
2259
2260         case CPP_SEMICOLON:
2261           /* If the next token is a `;', we have reached the end of the
2262              statement.  */
2263           if (!nesting_depth)
2264             return;
2265           break;
2266
2267         case CPP_CLOSE_BRACE:
2268           /* If this is a non-nested '}', stop before consuming it.
2269              That way, when confronted with something like:
2270
2271                { 3 + }
2272
2273              we stop before consuming the closing '}', even though we
2274              have not yet reached a `;'.  */
2275           if (nesting_depth == 0)
2276             return;
2277
2278           /* If it is the closing '}' for a block that we have
2279              scanned, stop -- but only after consuming the token.
2280              That way given:
2281
2282                 void f g () { ... }
2283                 typedef int I;
2284
2285              we will stop after the body of the erroneously declared
2286              function, but before consuming the following `typedef'
2287              declaration.  */
2288           if (--nesting_depth == 0)
2289             {
2290               cp_lexer_consume_token (parser->lexer);
2291               return;
2292             }
2293
2294         case CPP_OPEN_BRACE:
2295           ++nesting_depth;
2296           break;
2297
2298         default:
2299           break;
2300         }
2301
2302       /* Consume the token.  */
2303       cp_lexer_consume_token (parser->lexer);
2304     }
2305 }
2306
2307 /* This function is called at the end of a statement or declaration.
2308    If the next token is a semicolon, it is consumed; otherwise, error
2309    recovery is attempted.  */
2310
2311 static void
2312 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2313 {
2314   /* Look for the trailing `;'.  */
2315   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2316     {
2317       /* If there is additional (erroneous) input, skip to the end of
2318          the statement.  */
2319       cp_parser_skip_to_end_of_statement (parser);
2320       /* If the next token is now a `;', consume it.  */
2321       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2322         cp_lexer_consume_token (parser->lexer);
2323     }
2324 }
2325
2326 /* Skip tokens until we have consumed an entire block, or until we
2327    have consumed a non-nested `;'.  */
2328
2329 static void
2330 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2331 {
2332   int nesting_depth = 0;
2333
2334   while (nesting_depth >= 0)
2335     {
2336       cp_token *token = cp_lexer_peek_token (parser->lexer);
2337
2338       switch (token->type)
2339         {
2340         case CPP_EOF:
2341         case CPP_PRAGMA_EOL:
2342           /* If we've run out of tokens, stop.  */
2343           return;
2344
2345         case CPP_SEMICOLON:
2346           /* Stop if this is an unnested ';'. */
2347           if (!nesting_depth)
2348             nesting_depth = -1;
2349           break;
2350
2351         case CPP_CLOSE_BRACE:
2352           /* Stop if this is an unnested '}', or closes the outermost
2353              nesting level.  */
2354           nesting_depth--;
2355           if (!nesting_depth)
2356             nesting_depth = -1;
2357           break;
2358
2359         case CPP_OPEN_BRACE:
2360           /* Nest. */
2361           nesting_depth++;
2362           break;
2363
2364         default:
2365           break;
2366         }
2367
2368       /* Consume the token.  */
2369       cp_lexer_consume_token (parser->lexer);
2370     }
2371 }
2372
2373 /* Skip tokens until a non-nested closing curly brace is the next
2374    token.  */
2375
2376 static void
2377 cp_parser_skip_to_closing_brace (cp_parser *parser)
2378 {
2379   unsigned nesting_depth = 0;
2380
2381   while (true)
2382     {
2383       cp_token *token = cp_lexer_peek_token (parser->lexer);
2384
2385       switch (token->type)
2386         {
2387         case CPP_EOF:
2388         case CPP_PRAGMA_EOL:
2389           /* If we've run out of tokens, stop.  */
2390           return;
2391
2392         case CPP_CLOSE_BRACE:
2393           /* If the next token is a non-nested `}', then we have reached
2394              the end of the current block.  */
2395           if (nesting_depth-- == 0)
2396             return;
2397           break;
2398
2399         case CPP_OPEN_BRACE:
2400           /* If it the next token is a `{', then we are entering a new
2401              block.  Consume the entire block.  */
2402           ++nesting_depth;
2403           break;
2404
2405         default:
2406           break;
2407         }
2408
2409       /* Consume the token.  */
2410       cp_lexer_consume_token (parser->lexer);
2411     }
2412 }
2413
2414 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2415    parameter is the PRAGMA token, allowing us to purge the entire pragma
2416    sequence.  */
2417
2418 static void
2419 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2420 {
2421   cp_token *token;
2422
2423   parser->lexer->in_pragma = false;
2424
2425   do
2426     token = cp_lexer_consume_token (parser->lexer);
2427   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2428
2429   /* Ensure that the pragma is not parsed again.  */
2430   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2431 }
2432
2433 /* This is a simple wrapper around make_typename_type. When the id is
2434    an unresolved identifier node, we can provide a superior diagnostic
2435    using cp_parser_diagnose_invalid_type_name.  */
2436
2437 static tree
2438 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2439 {
2440   tree result;
2441   if (TREE_CODE (id) == IDENTIFIER_NODE)
2442     {
2443       result = make_typename_type (scope, id, typename_type,
2444                                    /*complain=*/tf_none);
2445       if (result == error_mark_node)
2446         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2447       return result;
2448     }
2449   return make_typename_type (scope, id, typename_type, tf_error);
2450 }
2451
2452
2453 /* Create a new C++ parser.  */
2454
2455 static cp_parser *
2456 cp_parser_new (void)
2457 {
2458   cp_parser *parser;
2459   cp_lexer *lexer;
2460   unsigned i;
2461
2462   /* cp_lexer_new_main is called before calling ggc_alloc because
2463      cp_lexer_new_main might load a PCH file.  */
2464   lexer = cp_lexer_new_main ();
2465
2466   /* Initialize the binops_by_token so that we can get the tree
2467      directly from the token.  */
2468   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2469     binops_by_token[binops[i].token_type] = binops[i];
2470
2471   parser = GGC_CNEW (cp_parser);
2472   parser->lexer = lexer;
2473   parser->context = cp_parser_context_new (NULL);
2474
2475   /* For now, we always accept GNU extensions.  */
2476   parser->allow_gnu_extensions_p = 1;
2477
2478   /* The `>' token is a greater-than operator, not the end of a
2479      template-id.  */
2480   parser->greater_than_is_operator_p = true;
2481
2482   parser->default_arg_ok_p = true;
2483
2484   /* We are not parsing a constant-expression.  */
2485   parser->integral_constant_expression_p = false;
2486   parser->allow_non_integral_constant_expression_p = false;
2487   parser->non_integral_constant_expression_p = false;
2488
2489   /* Local variable names are not forbidden.  */
2490   parser->local_variables_forbidden_p = false;
2491
2492   /* We are not processing an `extern "C"' declaration.  */
2493   parser->in_unbraced_linkage_specification_p = false;
2494
2495   /* We are not processing a declarator.  */
2496   parser->in_declarator_p = false;
2497
2498   /* We are not processing a template-argument-list.  */
2499   parser->in_template_argument_list_p = false;
2500
2501   /* We are not in an iteration statement.  */
2502   parser->in_iteration_statement_p = false;
2503
2504   /* We are not in a switch statement.  */
2505   parser->in_switch_statement_p = false;
2506
2507   /* We are not parsing a type-id inside an expression.  */
2508   parser->in_type_id_in_expr_p = false;
2509
2510   /* Declarations aren't implicitly extern "C".  */
2511   parser->implicit_extern_c = false;
2512
2513   /* String literals should be translated to the execution character set.  */
2514   parser->translate_strings_p = true;
2515
2516   /* The unparsed function queue is empty.  */
2517   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2518
2519   /* There are no classes being defined.  */
2520   parser->num_classes_being_defined = 0;
2521
2522   /* No template parameters apply.  */
2523   parser->num_template_parameter_lists = 0;
2524
2525   return parser;
2526 }
2527
2528 /* Create a cp_lexer structure which will emit the tokens in CACHE
2529    and push it onto the parser's lexer stack.  This is used for delayed
2530    parsing of in-class method bodies and default arguments, and should
2531    not be confused with tentative parsing.  */
2532 static void
2533 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2534 {
2535   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2536   lexer->next = parser->lexer;
2537   parser->lexer = lexer;
2538
2539   /* Move the current source position to that of the first token in the
2540      new lexer.  */
2541   cp_lexer_set_source_position_from_token (lexer->next_token);
2542 }
2543
2544 /* Pop the top lexer off the parser stack.  This is never used for the
2545    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2546 static void
2547 cp_parser_pop_lexer (cp_parser *parser)
2548 {
2549   cp_lexer *lexer = parser->lexer;
2550   parser->lexer = lexer->next;
2551   cp_lexer_destroy (lexer);
2552
2553   /* Put the current source position back where it was before this
2554      lexer was pushed.  */
2555   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2556 }
2557
2558 /* Lexical conventions [gram.lex]  */
2559
2560 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2561    identifier.  */
2562
2563 static tree
2564 cp_parser_identifier (cp_parser* parser)
2565 {
2566   cp_token *token;
2567
2568   /* Look for the identifier.  */
2569   token = cp_parser_require (parser, CPP_NAME, "identifier");
2570   /* Return the value.  */
2571   return token ? token->value : error_mark_node;
2572 }
2573
2574 /* Parse a sequence of adjacent string constants.  Returns a
2575    TREE_STRING representing the combined, nul-terminated string
2576    constant.  If TRANSLATE is true, translate the string to the
2577    execution character set.  If WIDE_OK is true, a wide string is
2578    invalid here.
2579
2580    C++98 [lex.string] says that if a narrow string literal token is
2581    adjacent to a wide string literal token, the behavior is undefined.
2582    However, C99 6.4.5p4 says that this results in a wide string literal.
2583    We follow C99 here, for consistency with the C front end.
2584
2585    This code is largely lifted from lex_string() in c-lex.c.
2586
2587    FUTURE: ObjC++ will need to handle @-strings here.  */
2588 static tree
2589 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2590 {
2591   tree value;
2592   bool wide = false;
2593   size_t count;
2594   struct obstack str_ob;
2595   cpp_string str, istr, *strs;
2596   cp_token *tok;
2597
2598   tok = cp_lexer_peek_token (parser->lexer);
2599   if (!cp_parser_is_string_literal (tok))
2600     {
2601       cp_parser_error (parser, "expected string-literal");
2602       return error_mark_node;
2603     }
2604
2605   /* Try to avoid the overhead of creating and destroying an obstack
2606      for the common case of just one string.  */
2607   if (!cp_parser_is_string_literal
2608       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2609     {
2610       cp_lexer_consume_token (parser->lexer);
2611
2612       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2613       str.len = TREE_STRING_LENGTH (tok->value);
2614       count = 1;
2615       if (tok->type == CPP_WSTRING)
2616         wide = true;
2617
2618       strs = &str;
2619     }
2620   else
2621     {
2622       gcc_obstack_init (&str_ob);
2623       count = 0;
2624
2625       do
2626         {
2627           cp_lexer_consume_token (parser->lexer);
2628           count++;
2629           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2630           str.len = TREE_STRING_LENGTH (tok->value);
2631           if (tok->type == CPP_WSTRING)
2632             wide = true;
2633
2634           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2635
2636           tok = cp_lexer_peek_token (parser->lexer);
2637         }
2638       while (cp_parser_is_string_literal (tok));
2639
2640       strs = (cpp_string *) obstack_finish (&str_ob);
2641     }
2642
2643   if (wide && !wide_ok)
2644     {
2645       cp_parser_error (parser, "a wide string is invalid in this context");
2646       wide = false;
2647     }
2648
2649   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2650       (parse_in, strs, count, &istr, wide))
2651     {
2652       value = build_string (istr.len, (char *)istr.text);
2653       free ((void *)istr.text);
2654
2655       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2656       value = fix_string_type (value);
2657     }
2658   else
2659     /* cpp_interpret_string has issued an error.  */
2660     value = error_mark_node;
2661
2662   if (count > 1)
2663     obstack_free (&str_ob, 0);
2664
2665   return value;
2666 }
2667
2668
2669 /* Basic concepts [gram.basic]  */
2670
2671 /* Parse a translation-unit.
2672
2673    translation-unit:
2674      declaration-seq [opt]
2675
2676    Returns TRUE if all went well.  */
2677
2678 static bool
2679 cp_parser_translation_unit (cp_parser* parser)
2680 {
2681   /* The address of the first non-permanent object on the declarator
2682      obstack.  */
2683   static void *declarator_obstack_base;
2684
2685   bool success;
2686
2687   /* Create the declarator obstack, if necessary.  */
2688   if (!cp_error_declarator)
2689     {
2690       gcc_obstack_init (&declarator_obstack);
2691       /* Create the error declarator.  */
2692       cp_error_declarator = make_declarator (cdk_error);
2693       /* Create the empty parameter list.  */
2694       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2695       /* Remember where the base of the declarator obstack lies.  */
2696       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2697     }
2698
2699   cp_parser_declaration_seq_opt (parser);
2700   
2701   /* If there are no tokens left then all went well.  */
2702   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2703     {
2704       /* Get rid of the token array; we don't need it any more.  */
2705       cp_lexer_destroy (parser->lexer);
2706       parser->lexer = NULL;
2707       
2708       /* This file might have been a context that's implicitly extern
2709          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2710       if (parser->implicit_extern_c)
2711         {
2712           pop_lang_context ();
2713           parser->implicit_extern_c = false;
2714         }
2715       
2716       /* Finish up.  */
2717       finish_translation_unit ();
2718       
2719       success = true;
2720     }
2721   else
2722     {
2723       cp_parser_error (parser, "expected declaration");
2724       success = false;
2725     }
2726   
2727   /* Make sure the declarator obstack was fully cleaned up.  */
2728   gcc_assert (obstack_next_free (&declarator_obstack)
2729               == declarator_obstack_base);
2730
2731   /* All went well.  */
2732   return success;
2733 }
2734
2735 /* Expressions [gram.expr] */
2736
2737 /* Parse a primary-expression.
2738
2739    primary-expression:
2740      literal
2741      this
2742      ( expression )
2743      id-expression
2744
2745    GNU Extensions:
2746
2747    primary-expression:
2748      ( compound-statement )
2749      __builtin_va_arg ( assignment-expression , type-id )
2750
2751    Objective-C++ Extension:
2752
2753    primary-expression:
2754      objc-expression
2755
2756    literal:
2757      __null
2758
2759    ADDRESS_P is true iff this expression was immediately preceded by
2760    "&" and therefore might denote a pointer-to-member.  CAST_P is true
2761    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
2762    true iff this expression is a template argument.
2763
2764    Returns a representation of the expression.  Upon return, *IDK
2765    indicates what kind of id-expression (if any) was present.  */
2766
2767 static tree
2768 cp_parser_primary_expression (cp_parser *parser,
2769                               bool address_p,
2770                               bool cast_p,
2771                               bool template_arg_p,
2772                               cp_id_kind *idk)
2773 {
2774   cp_token *token;
2775
2776   /* Assume the primary expression is not an id-expression.  */
2777   *idk = CP_ID_KIND_NONE;
2778
2779   /* Peek at the next token.  */
2780   token = cp_lexer_peek_token (parser->lexer);
2781   switch (token->type)
2782     {
2783       /* literal:
2784            integer-literal
2785            character-literal
2786            floating-literal
2787            string-literal
2788            boolean-literal  */
2789     case CPP_CHAR:
2790     case CPP_WCHAR:
2791     case CPP_NUMBER:
2792       token = cp_lexer_consume_token (parser->lexer);
2793       /* Floating-point literals are only allowed in an integral
2794          constant expression if they are cast to an integral or
2795          enumeration type.  */
2796       if (TREE_CODE (token->value) == REAL_CST
2797           && parser->integral_constant_expression_p
2798           && pedantic)
2799         {
2800           /* CAST_P will be set even in invalid code like "int(2.7 +
2801              ...)".   Therefore, we have to check that the next token
2802              is sure to end the cast.  */
2803           if (cast_p)
2804             {
2805               cp_token *next_token;
2806
2807               next_token = cp_lexer_peek_token (parser->lexer);
2808               if (/* The comma at the end of an
2809                      enumerator-definition.  */
2810                   next_token->type != CPP_COMMA
2811                   /* The curly brace at the end of an enum-specifier.  */
2812                   && next_token->type != CPP_CLOSE_BRACE
2813                   /* The end of a statement.  */
2814                   && next_token->type != CPP_SEMICOLON
2815                   /* The end of the cast-expression.  */
2816                   && next_token->type != CPP_CLOSE_PAREN
2817                   /* The end of an array bound.  */
2818                   && next_token->type != CPP_CLOSE_SQUARE
2819                   /* The closing ">" in a template-argument-list.  */
2820                   && (next_token->type != CPP_GREATER
2821                       || parser->greater_than_is_operator_p))
2822                 cast_p = false;
2823             }
2824
2825           /* If we are within a cast, then the constraint that the
2826              cast is to an integral or enumeration type will be
2827              checked at that point.  If we are not within a cast, then
2828              this code is invalid.  */
2829           if (!cast_p)
2830             cp_parser_non_integral_constant_expression
2831               (parser, "floating-point literal");
2832         }
2833       return token->value;
2834
2835     case CPP_STRING:
2836     case CPP_WSTRING:
2837       /* ??? Should wide strings be allowed when parser->translate_strings_p
2838          is false (i.e. in attributes)?  If not, we can kill the third
2839          argument to cp_parser_string_literal.  */
2840       return cp_parser_string_literal (parser,
2841                                        parser->translate_strings_p,
2842                                        true);
2843
2844     case CPP_OPEN_PAREN:
2845       {
2846         tree expr;
2847         bool saved_greater_than_is_operator_p;
2848
2849         /* Consume the `('.  */
2850         cp_lexer_consume_token (parser->lexer);
2851         /* Within a parenthesized expression, a `>' token is always
2852            the greater-than operator.  */
2853         saved_greater_than_is_operator_p
2854           = parser->greater_than_is_operator_p;
2855         parser->greater_than_is_operator_p = true;
2856         /* If we see `( { ' then we are looking at the beginning of
2857            a GNU statement-expression.  */
2858         if (cp_parser_allow_gnu_extensions_p (parser)
2859             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2860           {
2861             /* Statement-expressions are not allowed by the standard.  */
2862             if (pedantic)
2863               pedwarn ("ISO C++ forbids braced-groups within expressions");
2864
2865             /* And they're not allowed outside of a function-body; you
2866                cannot, for example, write:
2867
2868                  int i = ({ int j = 3; j + 1; });
2869
2870                at class or namespace scope.  */
2871             if (!at_function_scope_p ())
2872               error ("statement-expressions are allowed only inside functions");
2873             /* Start the statement-expression.  */
2874             expr = begin_stmt_expr ();
2875             /* Parse the compound-statement.  */
2876             cp_parser_compound_statement (parser, expr, false);
2877             /* Finish up.  */
2878             expr = finish_stmt_expr (expr, false);
2879           }
2880         else
2881           {
2882             /* Parse the parenthesized expression.  */
2883             expr = cp_parser_expression (parser, cast_p);
2884             /* Let the front end know that this expression was
2885                enclosed in parentheses. This matters in case, for
2886                example, the expression is of the form `A::B', since
2887                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2888                not.  */
2889             finish_parenthesized_expr (expr);
2890           }
2891         /* The `>' token might be the end of a template-id or
2892            template-parameter-list now.  */
2893         parser->greater_than_is_operator_p
2894           = saved_greater_than_is_operator_p;
2895         /* Consume the `)'.  */
2896         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2897           cp_parser_skip_to_end_of_statement (parser);
2898
2899         return expr;
2900       }
2901
2902     case CPP_KEYWORD:
2903       switch (token->keyword)
2904         {
2905           /* These two are the boolean literals.  */
2906         case RID_TRUE:
2907           cp_lexer_consume_token (parser->lexer);
2908           return boolean_true_node;
2909         case RID_FALSE:
2910           cp_lexer_consume_token (parser->lexer);
2911           return boolean_false_node;
2912
2913           /* The `__null' literal.  */
2914         case RID_NULL:
2915           cp_lexer_consume_token (parser->lexer);
2916           return null_node;
2917
2918           /* Recognize the `this' keyword.  */
2919         case RID_THIS:
2920           cp_lexer_consume_token (parser->lexer);
2921           if (parser->local_variables_forbidden_p)
2922             {
2923               error ("%<this%> may not be used in this context");
2924               return error_mark_node;
2925             }
2926           /* Pointers cannot appear in constant-expressions.  */
2927           if (cp_parser_non_integral_constant_expression (parser,
2928                                                           "`this'"))
2929             return error_mark_node;
2930           return finish_this_expr ();
2931
2932           /* The `operator' keyword can be the beginning of an
2933              id-expression.  */
2934         case RID_OPERATOR:
2935           goto id_expression;
2936
2937         case RID_FUNCTION_NAME:
2938         case RID_PRETTY_FUNCTION_NAME:
2939         case RID_C99_FUNCTION_NAME:
2940           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2941              __func__ are the names of variables -- but they are
2942              treated specially.  Therefore, they are handled here,
2943              rather than relying on the generic id-expression logic
2944              below.  Grammatically, these names are id-expressions.
2945
2946              Consume the token.  */
2947           token = cp_lexer_consume_token (parser->lexer);
2948           /* Look up the name.  */
2949           return finish_fname (token->value);
2950
2951         case RID_VA_ARG:
2952           {
2953             tree expression;
2954             tree type;
2955
2956             /* The `__builtin_va_arg' construct is used to handle
2957                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2958             cp_lexer_consume_token (parser->lexer);
2959             /* Look for the opening `('.  */
2960             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2961             /* Now, parse the assignment-expression.  */
2962             expression = cp_parser_assignment_expression (parser,
2963                                                           /*cast_p=*/false);
2964             /* Look for the `,'.  */
2965             cp_parser_require (parser, CPP_COMMA, "`,'");
2966             /* Parse the type-id.  */
2967             type = cp_parser_type_id (parser);
2968             /* Look for the closing `)'.  */
2969             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2970             /* Using `va_arg' in a constant-expression is not
2971                allowed.  */
2972             if (cp_parser_non_integral_constant_expression (parser,
2973                                                             "`va_arg'"))
2974               return error_mark_node;
2975             return build_x_va_arg (expression, type);
2976           }
2977
2978         case RID_OFFSETOF:
2979           return cp_parser_builtin_offsetof (parser);
2980
2981           /* Objective-C++ expressions.  */
2982         case RID_AT_ENCODE:
2983         case RID_AT_PROTOCOL:
2984         case RID_AT_SELECTOR:
2985           return cp_parser_objc_expression (parser);
2986
2987         default:
2988           cp_parser_error (parser, "expected primary-expression");
2989           return error_mark_node;
2990         }
2991
2992       /* An id-expression can start with either an identifier, a
2993          `::' as the beginning of a qualified-id, or the "operator"
2994          keyword.  */
2995     case CPP_NAME:
2996     case CPP_SCOPE:
2997     case CPP_TEMPLATE_ID:
2998     case CPP_NESTED_NAME_SPECIFIER:
2999       {
3000         tree id_expression;
3001         tree decl;
3002         const char *error_msg;
3003         bool template_p;
3004         bool done;
3005
3006       id_expression:
3007         /* Parse the id-expression.  */
3008         id_expression
3009           = cp_parser_id_expression (parser,
3010                                      /*template_keyword_p=*/false,
3011                                      /*check_dependency_p=*/true,
3012                                      &template_p,
3013                                      /*declarator_p=*/false);
3014         if (id_expression == error_mark_node)
3015           return error_mark_node;
3016         token = cp_lexer_peek_token (parser->lexer);
3017         done = (token->type != CPP_OPEN_SQUARE
3018                 && token->type != CPP_OPEN_PAREN
3019                 && token->type != CPP_DOT
3020                 && token->type != CPP_DEREF
3021                 && token->type != CPP_PLUS_PLUS
3022                 && token->type != CPP_MINUS_MINUS);
3023         /* If we have a template-id, then no further lookup is
3024            required.  If the template-id was for a template-class, we
3025            will sometimes have a TYPE_DECL at this point.  */
3026         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3027                  || TREE_CODE (id_expression) == TYPE_DECL)
3028           decl = id_expression;
3029         /* Look up the name.  */
3030         else
3031           {
3032             tree ambiguous_decls;
3033
3034             decl = cp_parser_lookup_name (parser, id_expression,
3035                                           none_type,
3036                                           template_p,
3037                                           /*is_namespace=*/false,
3038                                           /*check_dependency=*/true,
3039                                           &ambiguous_decls);
3040             /* If the lookup was ambiguous, an error will already have
3041                been issued.  */
3042             if (ambiguous_decls)
3043               return error_mark_node;
3044
3045             /* In Objective-C++, an instance variable (ivar) may be preferred
3046                to whatever cp_parser_lookup_name() found.  */
3047             decl = objc_lookup_ivar (decl, id_expression);
3048
3049             /* If name lookup gives us a SCOPE_REF, then the
3050                qualifying scope was dependent.  */
3051             if (TREE_CODE (decl) == SCOPE_REF)
3052               return decl;
3053             /* Check to see if DECL is a local variable in a context
3054                where that is forbidden.  */
3055             if (parser->local_variables_forbidden_p
3056                 && local_variable_p (decl))
3057               {
3058                 /* It might be that we only found DECL because we are
3059                    trying to be generous with pre-ISO scoping rules.
3060                    For example, consider:
3061
3062                      int i;
3063                      void g() {
3064                        for (int i = 0; i < 10; ++i) {}
3065                        extern void f(int j = i);
3066                      }
3067
3068                    Here, name look up will originally find the out
3069                    of scope `i'.  We need to issue a warning message,
3070                    but then use the global `i'.  */
3071                 decl = check_for_out_of_scope_variable (decl);
3072                 if (local_variable_p (decl))
3073                   {
3074                     error ("local variable %qD may not appear in this context",
3075                            decl);
3076                     return error_mark_node;
3077                   }
3078               }
3079           }
3080
3081         decl = (finish_id_expression 
3082                 (id_expression, decl, parser->scope,
3083                  idk,
3084                  parser->integral_constant_expression_p,
3085                  parser->allow_non_integral_constant_expression_p,
3086                  &parser->non_integral_constant_expression_p,
3087                  template_p, done, address_p,
3088                  template_arg_p,
3089                  &error_msg));
3090         if (error_msg)
3091           cp_parser_error (parser, error_msg);
3092         return decl;
3093       }
3094
3095       /* Anything else is an error.  */
3096     default:
3097       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3098       if (c_dialect_objc ()
3099           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3100         return cp_parser_objc_expression (parser);
3101
3102       cp_parser_error (parser, "expected primary-expression");
3103       return error_mark_node;
3104     }
3105 }
3106
3107 /* Parse an id-expression.
3108
3109    id-expression:
3110      unqualified-id
3111      qualified-id
3112
3113    qualified-id:
3114      :: [opt] nested-name-specifier template [opt] unqualified-id
3115      :: identifier
3116      :: operator-function-id
3117      :: template-id
3118
3119    Return a representation of the unqualified portion of the
3120    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3121    a `::' or nested-name-specifier.
3122
3123    Often, if the id-expression was a qualified-id, the caller will
3124    want to make a SCOPE_REF to represent the qualified-id.  This
3125    function does not do this in order to avoid wastefully creating
3126    SCOPE_REFs when they are not required.
3127
3128    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3129    `template' keyword.
3130
3131    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3132    uninstantiated templates.
3133
3134    If *TEMPLATE_P is non-NULL, it is set to true iff the
3135    `template' keyword is used to explicitly indicate that the entity
3136    named is a template.
3137
3138    If DECLARATOR_P is true, the id-expression is appearing as part of
3139    a declarator, rather than as part of an expression.  */
3140
3141 static tree
3142 cp_parser_id_expression (cp_parser *parser,
3143                          bool template_keyword_p,
3144                          bool check_dependency_p,
3145                          bool *template_p,
3146                          bool declarator_p)
3147 {
3148   bool global_scope_p;
3149   bool nested_name_specifier_p;
3150
3151   /* Assume the `template' keyword was not used.  */
3152   if (template_p)
3153     *template_p = template_keyword_p;
3154
3155   /* Look for the optional `::' operator.  */
3156   global_scope_p
3157     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3158        != NULL_TREE);
3159   /* Look for the optional nested-name-specifier.  */
3160   nested_name_specifier_p
3161     = (cp_parser_nested_name_specifier_opt (parser,
3162                                             /*typename_keyword_p=*/false,
3163                                             check_dependency_p,
3164                                             /*type_p=*/false,
3165                                             declarator_p)
3166        != NULL_TREE);
3167   /* If there is a nested-name-specifier, then we are looking at
3168      the first qualified-id production.  */
3169   if (nested_name_specifier_p)
3170     {
3171       tree saved_scope;
3172       tree saved_object_scope;
3173       tree saved_qualifying_scope;
3174       tree unqualified_id;
3175       bool is_template;
3176
3177       /* See if the next token is the `template' keyword.  */
3178       if (!template_p)
3179         template_p = &is_template;
3180       *template_p = cp_parser_optional_template_keyword (parser);
3181       /* Name lookup we do during the processing of the
3182          unqualified-id might obliterate SCOPE.  */
3183       saved_scope = parser->scope;
3184       saved_object_scope = parser->object_scope;
3185       saved_qualifying_scope = parser->qualifying_scope;
3186       /* Process the final unqualified-id.  */
3187       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3188                                                  check_dependency_p,
3189                                                  declarator_p);
3190       /* Restore the SAVED_SCOPE for our caller.  */
3191       parser->scope = saved_scope;
3192       parser->object_scope = saved_object_scope;
3193       parser->qualifying_scope = saved_qualifying_scope;
3194
3195       return unqualified_id;
3196     }
3197   /* Otherwise, if we are in global scope, then we are looking at one
3198      of the other qualified-id productions.  */
3199   else if (global_scope_p)
3200     {
3201       cp_token *token;
3202       tree id;
3203
3204       /* Peek at the next token.  */
3205       token = cp_lexer_peek_token (parser->lexer);
3206
3207       /* If it's an identifier, and the next token is not a "<", then
3208          we can avoid the template-id case.  This is an optimization
3209          for this common case.  */
3210       if (token->type == CPP_NAME
3211           && !cp_parser_nth_token_starts_template_argument_list_p
3212                (parser, 2))
3213         return cp_parser_identifier (parser);
3214
3215       cp_parser_parse_tentatively (parser);
3216       /* Try a template-id.  */
3217       id = cp_parser_template_id (parser,
3218                                   /*template_keyword_p=*/false,
3219                                   /*check_dependency_p=*/true,
3220                                   declarator_p);
3221       /* If that worked, we're done.  */
3222       if (cp_parser_parse_definitely (parser))
3223         return id;
3224
3225       /* Peek at the next token.  (Changes in the token buffer may
3226          have invalidated the pointer obtained above.)  */
3227       token = cp_lexer_peek_token (parser->lexer);
3228
3229       switch (token->type)
3230         {
3231         case CPP_NAME:
3232           return cp_parser_identifier (parser);
3233
3234         case CPP_KEYWORD:
3235           if (token->keyword == RID_OPERATOR)
3236             return cp_parser_operator_function_id (parser);
3237           /* Fall through.  */
3238
3239         default:
3240           cp_parser_error (parser, "expected id-expression");
3241           return error_mark_node;
3242         }
3243     }
3244   else
3245     return cp_parser_unqualified_id (parser, template_keyword_p,
3246                                      /*check_dependency_p=*/true,
3247                                      declarator_p);
3248 }
3249
3250 /* Parse an unqualified-id.
3251
3252    unqualified-id:
3253      identifier
3254      operator-function-id
3255      conversion-function-id
3256      ~ class-name
3257      template-id
3258
3259    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3260    keyword, in a construct like `A::template ...'.
3261
3262    Returns a representation of unqualified-id.  For the `identifier'
3263    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3264    production a BIT_NOT_EXPR is returned; the operand of the
3265    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3266    other productions, see the documentation accompanying the
3267    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3268    names are looked up in uninstantiated templates.  If DECLARATOR_P
3269    is true, the unqualified-id is appearing as part of a declarator,
3270    rather than as part of an expression.  */
3271
3272 static tree
3273 cp_parser_unqualified_id (cp_parser* parser,
3274                           bool template_keyword_p,
3275                           bool check_dependency_p,
3276                           bool declarator_p)
3277 {
3278   cp_token *token;
3279
3280   /* Peek at the next token.  */
3281   token = cp_lexer_peek_token (parser->lexer);
3282
3283   switch (token->type)
3284     {
3285     case CPP_NAME:
3286       {
3287         tree id;
3288
3289         /* We don't know yet whether or not this will be a
3290            template-id.  */
3291         cp_parser_parse_tentatively (parser);
3292         /* Try a template-id.  */
3293         id = cp_parser_template_id (parser, template_keyword_p,
3294                                     check_dependency_p,
3295                                     declarator_p);
3296         /* If it worked, we're done.  */
3297         if (cp_parser_parse_definitely (parser))
3298           return id;
3299         /* Otherwise, it's an ordinary identifier.  */
3300         return cp_parser_identifier (parser);
3301       }
3302
3303     case CPP_TEMPLATE_ID:
3304       return cp_parser_template_id (parser, template_keyword_p,
3305                                     check_dependency_p,
3306                                     declarator_p);
3307
3308     case CPP_COMPL:
3309       {
3310         tree type_decl;
3311         tree qualifying_scope;
3312         tree object_scope;
3313         tree scope;
3314         bool done;
3315
3316         /* Consume the `~' token.  */
3317         cp_lexer_consume_token (parser->lexer);
3318         /* Parse the class-name.  The standard, as written, seems to
3319            say that:
3320
3321              template <typename T> struct S { ~S (); };
3322              template <typename T> S<T>::~S() {}
3323
3324            is invalid, since `~' must be followed by a class-name, but
3325            `S<T>' is dependent, and so not known to be a class.
3326            That's not right; we need to look in uninstantiated
3327            templates.  A further complication arises from:
3328
3329              template <typename T> void f(T t) {
3330                t.T::~T();
3331              }
3332
3333            Here, it is not possible to look up `T' in the scope of `T'
3334            itself.  We must look in both the current scope, and the
3335            scope of the containing complete expression.
3336
3337            Yet another issue is:
3338
3339              struct S {
3340                int S;
3341                ~S();
3342              };
3343
3344              S::~S() {}
3345
3346            The standard does not seem to say that the `S' in `~S'
3347            should refer to the type `S' and not the data member
3348            `S::S'.  */
3349
3350         /* DR 244 says that we look up the name after the "~" in the
3351            same scope as we looked up the qualifying name.  That idea
3352            isn't fully worked out; it's more complicated than that.  */
3353         scope = parser->scope;
3354         object_scope = parser->object_scope;
3355         qualifying_scope = parser->qualifying_scope;
3356
3357         /* If the name is of the form "X::~X" it's OK.  */
3358         if (scope && TYPE_P (scope)
3359             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3360             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3361                 == CPP_OPEN_PAREN)
3362             && (cp_lexer_peek_token (parser->lexer)->value
3363                 == TYPE_IDENTIFIER (scope)))
3364           {
3365             cp_lexer_consume_token (parser->lexer);
3366             return build_nt (BIT_NOT_EXPR, scope);
3367           }
3368
3369         /* If there was an explicit qualification (S::~T), first look
3370            in the scope given by the qualification (i.e., S).  */
3371         done = false;
3372         type_decl = NULL_TREE;
3373         if (scope)
3374           {
3375             cp_parser_parse_tentatively (parser);
3376             type_decl = cp_parser_class_name (parser,
3377                                               /*typename_keyword_p=*/false,
3378                                               /*template_keyword_p=*/false,
3379                                               none_type,
3380                                               /*check_dependency=*/false,
3381                                               /*class_head_p=*/false,
3382                                               declarator_p);
3383             if (cp_parser_parse_definitely (parser))
3384               done = true;
3385           }
3386         /* In "N::S::~S", look in "N" as well.  */
3387         if (!done && scope && qualifying_scope)
3388           {
3389             cp_parser_parse_tentatively (parser);
3390             parser->scope = qualifying_scope;
3391             parser->object_scope = NULL_TREE;
3392             parser->qualifying_scope = NULL_TREE;
3393             type_decl
3394               = cp_parser_class_name (parser,
3395                                       /*typename_keyword_p=*/false,
3396                                       /*template_keyword_p=*/false,
3397                                       none_type,
3398                                       /*check_dependency=*/false,
3399                                       /*class_head_p=*/false,
3400                                       declarator_p);
3401             if (cp_parser_parse_definitely (parser))
3402               done = true;
3403           }
3404         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3405         else if (!done && object_scope)
3406           {
3407             cp_parser_parse_tentatively (parser);
3408             parser->scope = object_scope;
3409             parser->object_scope = NULL_TREE;
3410             parser->qualifying_scope = NULL_TREE;
3411             type_decl
3412               = cp_parser_class_name (parser,
3413                                       /*typename_keyword_p=*/false,
3414                                       /*template_keyword_p=*/false,
3415                                       none_type,
3416                                       /*check_dependency=*/false,
3417                                       /*class_head_p=*/false,
3418                                       declarator_p);
3419             if (cp_parser_parse_definitely (parser))
3420               done = true;
3421           }
3422         /* Look in the surrounding context.  */
3423         if (!done)
3424           {
3425             parser->scope = NULL_TREE;
3426             parser->object_scope = NULL_TREE;
3427             parser->qualifying_scope = NULL_TREE;
3428             type_decl
3429               = cp_parser_class_name (parser,
3430                                       /*typename_keyword_p=*/false,
3431                                       /*template_keyword_p=*/false,
3432                                       none_type,
3433                                       /*check_dependency=*/false,
3434                                       /*class_head_p=*/false,
3435                                       declarator_p);
3436           }
3437         /* If an error occurred, assume that the name of the
3438            destructor is the same as the name of the qualifying
3439            class.  That allows us to keep parsing after running
3440            into ill-formed destructor names.  */
3441         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3442           return build_nt (BIT_NOT_EXPR, scope);
3443         else if (type_decl == error_mark_node)
3444           return error_mark_node;
3445
3446         /* [class.dtor]
3447
3448            A typedef-name that names a class shall not be used as the
3449            identifier in the declarator for a destructor declaration.  */
3450         if (declarator_p
3451             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3452             && !DECL_SELF_REFERENCE_P (type_decl)
3453             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3454           error ("typedef-name %qD used as destructor declarator",
3455                  type_decl);
3456
3457         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3458       }
3459
3460     case CPP_KEYWORD:
3461       if (token->keyword == RID_OPERATOR)
3462         {
3463           tree id;
3464
3465           /* This could be a template-id, so we try that first.  */
3466           cp_parser_parse_tentatively (parser);
3467           /* Try a template-id.  */
3468           id = cp_parser_template_id (parser, template_keyword_p,
3469                                       /*check_dependency_p=*/true,
3470                                       declarator_p);
3471           /* If that worked, we're done.  */
3472           if (cp_parser_parse_definitely (parser))
3473             return id;
3474           /* We still don't know whether we're looking at an
3475              operator-function-id or a conversion-function-id.  */
3476           cp_parser_parse_tentatively (parser);
3477           /* Try an operator-function-id.  */
3478           id = cp_parser_operator_function_id (parser);
3479           /* If that didn't work, try a conversion-function-id.  */
3480           if (!cp_parser_parse_definitely (parser))
3481             id = cp_parser_conversion_function_id (parser);
3482
3483           return id;
3484         }
3485       /* Fall through.  */
3486
3487     default:
3488       cp_parser_error (parser, "expected unqualified-id");
3489       return error_mark_node;
3490     }
3491 }
3492
3493 /* Parse an (optional) nested-name-specifier.
3494
3495    nested-name-specifier:
3496      class-or-namespace-name :: nested-name-specifier [opt]
3497      class-or-namespace-name :: template nested-name-specifier [opt]
3498
3499    PARSER->SCOPE should be set appropriately before this function is
3500    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3501    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3502    in name lookups.
3503
3504    Sets PARSER->SCOPE to the class (TYPE) or namespace
3505    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3506    it unchanged if there is no nested-name-specifier.  Returns the new
3507    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3508
3509    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3510    part of a declaration and/or decl-specifier.  */
3511
3512 static tree
3513 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3514                                      bool typename_keyword_p,
3515                                      bool check_dependency_p,
3516                                      bool type_p,
3517                                      bool is_declaration)
3518 {
3519   bool success = false;
3520   tree access_check = NULL_TREE;
3521   cp_token_position start = 0;
3522   cp_token *token;
3523
3524   /* If the next token corresponds to a nested name specifier, there
3525      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3526      false, it may have been true before, in which case something
3527      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3528      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3529      CHECK_DEPENDENCY_P is false, we have to fall through into the
3530      main loop.  */
3531   if (check_dependency_p
3532       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3533     {
3534       cp_parser_pre_parsed_nested_name_specifier (parser);
3535       return parser->scope;
3536     }
3537
3538   /* Remember where the nested-name-specifier starts.  */
3539   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3540     start = cp_lexer_token_position (parser->lexer, false);
3541
3542   push_deferring_access_checks (dk_deferred);
3543
3544   while (true)
3545     {
3546       tree new_scope;
3547       tree old_scope;
3548       tree saved_qualifying_scope;
3549       bool template_keyword_p;
3550
3551       /* Spot cases that cannot be the beginning of a
3552          nested-name-specifier.  */
3553       token = cp_lexer_peek_token (parser->lexer);
3554
3555       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3556          the already parsed nested-name-specifier.  */
3557       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3558         {
3559           /* Grab the nested-name-specifier and continue the loop.  */
3560           cp_parser_pre_parsed_nested_name_specifier (parser);
3561           success = true;
3562           continue;
3563         }
3564
3565       /* Spot cases that cannot be the beginning of a
3566          nested-name-specifier.  On the second and subsequent times
3567          through the loop, we look for the `template' keyword.  */
3568       if (success && token->keyword == RID_TEMPLATE)
3569         ;
3570       /* A template-id can start a nested-name-specifier.  */
3571       else if (token->type == CPP_TEMPLATE_ID)
3572         ;
3573       else
3574         {
3575           /* If the next token is not an identifier, then it is
3576              definitely not a class-or-namespace-name.  */
3577           if (token->type != CPP_NAME)
3578             break;
3579           /* If the following token is neither a `<' (to begin a
3580              template-id), nor a `::', then we are not looking at a
3581              nested-name-specifier.  */
3582           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3583           if (token->type != CPP_SCOPE
3584               && !cp_parser_nth_token_starts_template_argument_list_p
3585                   (parser, 2))
3586             break;
3587         }
3588
3589       /* The nested-name-specifier is optional, so we parse
3590          tentatively.  */
3591       cp_parser_parse_tentatively (parser);
3592
3593       /* Look for the optional `template' keyword, if this isn't the
3594          first time through the loop.  */
3595       if (success)
3596         template_keyword_p = cp_parser_optional_template_keyword (parser);
3597       else
3598         template_keyword_p = false;
3599
3600       /* Save the old scope since the name lookup we are about to do
3601          might destroy it.  */
3602       old_scope = parser->scope;
3603       saved_qualifying_scope = parser->qualifying_scope;
3604       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3605          look up names in "X<T>::I" in order to determine that "Y" is
3606          a template.  So, if we have a typename at this point, we make
3607          an effort to look through it.  */
3608       if (is_declaration
3609           && !typename_keyword_p
3610           && parser->scope
3611           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3612         parser->scope = resolve_typename_type (parser->scope,
3613                                                /*only_current_p=*/false);
3614       /* Parse the qualifying entity.  */
3615       new_scope
3616         = cp_parser_class_or_namespace_name (parser,
3617                                              typename_keyword_p,
3618                                              template_keyword_p,
3619                                              check_dependency_p,
3620                                              type_p,
3621                                              is_declaration);
3622       /* Look for the `::' token.  */
3623       cp_parser_require (parser, CPP_SCOPE, "`::'");
3624
3625       /* If we found what we wanted, we keep going; otherwise, we're
3626          done.  */
3627       if (!cp_parser_parse_definitely (parser))
3628         {
3629           bool error_p = false;
3630
3631           /* Restore the OLD_SCOPE since it was valid before the
3632              failed attempt at finding the last
3633              class-or-namespace-name.  */
3634           parser->scope = old_scope;
3635           parser->qualifying_scope = saved_qualifying_scope;
3636           /* If the next token is an identifier, and the one after
3637              that is a `::', then any valid interpretation would have
3638              found a class-or-namespace-name.  */
3639           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3640                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3641                      == CPP_SCOPE)
3642                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3643                      != CPP_COMPL))
3644             {
3645               token = cp_lexer_consume_token (parser->lexer);
3646               if (!error_p)
3647                 {
3648                   if (!token->ambiguous_p)
3649                     {
3650                       tree decl;
3651                       tree ambiguous_decls;
3652
3653                       decl = cp_parser_lookup_name (parser, token->value,
3654                                                     none_type,
3655                                                     /*is_template=*/false,
3656                                                     /*is_namespace=*/false,
3657                                                     /*check_dependency=*/true,
3658                                                     &ambiguous_decls);
3659                       if (TREE_CODE (decl) == TEMPLATE_DECL)
3660                         error ("%qD used without template parameters", decl);
3661                       else if (ambiguous_decls)
3662                         {
3663                           error ("reference to %qD is ambiguous", 
3664                                  token->value);
3665                           print_candidates (ambiguous_decls);
3666                           decl = error_mark_node;
3667                         }
3668                       else
3669                         cp_parser_name_lookup_error
3670                           (parser, token->value, decl,
3671                            "is not a class or namespace");
3672                     }
3673                   parser->scope = error_mark_node;
3674                   error_p = true;
3675                   /* Treat this as a successful nested-name-specifier
3676                      due to:
3677
3678                      [basic.lookup.qual]
3679
3680                      If the name found is not a class-name (clause
3681                      _class_) or namespace-name (_namespace.def_), the
3682                      program is ill-formed.  */
3683                   success = true;
3684                 }
3685               cp_lexer_consume_token (parser->lexer);
3686             }
3687           break;
3688         }
3689       /* We've found one valid nested-name-specifier.  */
3690       success = true;
3691       /* Name lookup always gives us a DECL.  */
3692       if (TREE_CODE (new_scope) == TYPE_DECL)
3693         new_scope = TREE_TYPE (new_scope);
3694       /* Uses of "template" must be followed by actual templates.  */
3695       if (template_keyword_p
3696           && !(CLASS_TYPE_P (new_scope)
3697                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3698                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3699                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
3700           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3701                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3702                    == TEMPLATE_ID_EXPR)))
3703         pedwarn (TYPE_P (new_scope)
3704                  ? "%qT is not a template"
3705                  : "%qD is not a template",
3706                  new_scope);
3707       /* If it is a class scope, try to complete it; we are about to
3708          be looking up names inside the class.  */
3709       if (TYPE_P (new_scope)
3710           /* Since checking types for dependency can be expensive,
3711              avoid doing it if the type is already complete.  */
3712           && !COMPLETE_TYPE_P (new_scope)
3713           /* Do not try to complete dependent types.  */
3714           && !dependent_type_p (new_scope))
3715         new_scope = complete_type (new_scope);
3716       /* Make sure we look in the right scope the next time through
3717          the loop.  */
3718       parser->scope = new_scope;
3719     }
3720
3721   /* Retrieve any deferred checks.  Do not pop this access checks yet
3722      so the memory will not be reclaimed during token replacing below.  */
3723   access_check = get_deferred_access_checks ();
3724
3725   /* If parsing tentatively, replace the sequence of tokens that makes
3726      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3727      token.  That way, should we re-parse the token stream, we will
3728      not have to repeat the effort required to do the parse, nor will
3729      we issue duplicate error messages.  */
3730   if (success && start)
3731     {
3732       cp_token *token = cp_lexer_token_at (parser->lexer, start);
3733
3734       /* Reset the contents of the START token.  */
3735       token->type = CPP_NESTED_NAME_SPECIFIER;
3736       token->value = build_tree_list (access_check, parser->scope);
3737       TREE_TYPE (token->value) = parser->qualifying_scope;
3738       token->keyword = RID_MAX;
3739
3740       /* Purge all subsequent tokens.  */
3741       cp_lexer_purge_tokens_after (parser->lexer, start);
3742     }
3743
3744   pop_deferring_access_checks ();
3745   return success ? parser->scope : NULL_TREE;
3746 }
3747
3748 /* Parse a nested-name-specifier.  See
3749    cp_parser_nested_name_specifier_opt for details.  This function
3750    behaves identically, except that it will an issue an error if no
3751    nested-name-specifier is present.  */
3752
3753 static tree
3754 cp_parser_nested_name_specifier (cp_parser *parser,
3755                                  bool typename_keyword_p,
3756                                  bool check_dependency_p,
3757                                  bool type_p,
3758                                  bool is_declaration)
3759 {
3760   tree scope;
3761
3762   /* Look for the nested-name-specifier.  */
3763   scope = cp_parser_nested_name_specifier_opt (parser,
3764                                                typename_keyword_p,
3765                                                check_dependency_p,
3766                                                type_p,
3767                                                is_declaration);
3768   /* If it was not present, issue an error message.  */
3769   if (!scope)
3770     {
3771       cp_parser_error (parser, "expected nested-name-specifier");
3772       parser->scope = NULL_TREE;
3773     }
3774
3775   return scope;
3776 }
3777
3778 /* Parse a class-or-namespace-name.
3779
3780    class-or-namespace-name:
3781      class-name
3782      namespace-name
3783
3784    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3785    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3786    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3787    TYPE_P is TRUE iff the next name should be taken as a class-name,
3788    even the same name is declared to be another entity in the same
3789    scope.
3790
3791    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3792    specified by the class-or-namespace-name.  If neither is found the
3793    ERROR_MARK_NODE is returned.  */
3794
3795 static tree
3796 cp_parser_class_or_namespace_name (cp_parser *parser,
3797                                    bool typename_keyword_p,
3798                                    bool template_keyword_p,
3799                                    bool check_dependency_p,
3800                                    bool type_p,
3801                                    bool is_declaration)
3802 {
3803   tree saved_scope;
3804   tree saved_qualifying_scope;
3805   tree saved_object_scope;
3806   tree scope;
3807   bool only_class_p;
3808
3809   /* Before we try to parse the class-name, we must save away the
3810      current PARSER->SCOPE since cp_parser_class_name will destroy
3811      it.  */
3812   saved_scope = parser->scope;
3813   saved_qualifying_scope = parser->qualifying_scope;
3814   saved_object_scope = parser->object_scope;
3815   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3816      there is no need to look for a namespace-name.  */
3817   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3818   if (!only_class_p)
3819     cp_parser_parse_tentatively (parser);
3820   scope = cp_parser_class_name (parser,
3821                                 typename_keyword_p,
3822                                 template_keyword_p,
3823                                 type_p ? class_type : none_type,
3824                                 check_dependency_p,
3825                                 /*class_head_p=*/false,
3826                                 is_declaration);
3827   /* If that didn't work, try for a namespace-name.  */
3828   if (!only_class_p && !cp_parser_parse_definitely (parser))
3829     {
3830       /* Restore the saved scope.  */
3831       parser->scope = saved_scope;
3832       parser->qualifying_scope = saved_qualifying_scope;
3833       parser->object_scope = saved_object_scope;
3834       /* If we are not looking at an identifier followed by the scope
3835          resolution operator, then this is not part of a
3836          nested-name-specifier.  (Note that this function is only used
3837          to parse the components of a nested-name-specifier.)  */
3838       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3839           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3840         return error_mark_node;
3841       scope = cp_parser_namespace_name (parser);
3842     }
3843
3844   return scope;
3845 }
3846
3847 /* Parse a postfix-expression.
3848
3849    postfix-expression:
3850      primary-expression
3851      postfix-expression [ expression ]
3852      postfix-expression ( expression-list [opt] )
3853      simple-type-specifier ( expression-list [opt] )
3854      typename :: [opt] nested-name-specifier identifier
3855        ( expression-list [opt] )
3856      typename :: [opt] nested-name-specifier template [opt] template-id
3857        ( expression-list [opt] )
3858      postfix-expression . template [opt] id-expression
3859      postfix-expression -> template [opt] id-expression
3860      postfix-expression . pseudo-destructor-name
3861      postfix-expression -> pseudo-destructor-name
3862      postfix-expression ++
3863      postfix-expression --
3864      dynamic_cast < type-id > ( expression )
3865      static_cast < type-id > ( expression )
3866      reinterpret_cast < type-id > ( expression )
3867      const_cast < type-id > ( expression )
3868      typeid ( expression )
3869      typeid ( type-id )
3870
3871    GNU Extension:
3872
3873    postfix-expression:
3874      ( type-id ) { initializer-list , [opt] }
3875
3876    This extension is a GNU version of the C99 compound-literal
3877    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3878    but they are essentially the same concept.)
3879
3880    If ADDRESS_P is true, the postfix expression is the operand of the
3881    `&' operator.  CAST_P is true if this expression is the target of a
3882    cast.
3883
3884    Returns a representation of the expression.  */
3885
3886 static tree
3887 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3888 {
3889   cp_token *token;
3890   enum rid keyword;
3891   cp_id_kind idk = CP_ID_KIND_NONE;
3892   tree postfix_expression = NULL_TREE;
3893
3894   /* Peek at the next token.  */
3895   token = cp_lexer_peek_token (parser->lexer);
3896   /* Some of the productions are determined by keywords.  */
3897   keyword = token->keyword;
3898   switch (keyword)
3899     {
3900     case RID_DYNCAST:
3901     case RID_STATCAST:
3902     case RID_REINTCAST:
3903     case RID_CONSTCAST:
3904       {
3905         tree type;
3906         tree expression;
3907         const char *saved_message;
3908
3909         /* All of these can be handled in the same way from the point
3910            of view of parsing.  Begin by consuming the token
3911            identifying the cast.  */
3912         cp_lexer_consume_token (parser->lexer);
3913
3914         /* New types cannot be defined in the cast.  */
3915         saved_message = parser->type_definition_forbidden_message;
3916         parser->type_definition_forbidden_message
3917           = "types may not be defined in casts";
3918
3919         /* Look for the opening `<'.  */
3920         cp_parser_require (parser, CPP_LESS, "`<'");
3921         /* Parse the type to which we are casting.  */
3922         type = cp_parser_type_id (parser);
3923         /* Look for the closing `>'.  */
3924         cp_parser_require (parser, CPP_GREATER, "`>'");
3925         /* Restore the old message.  */
3926         parser->type_definition_forbidden_message = saved_message;
3927
3928         /* And the expression which is being cast.  */
3929         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3930         expression = cp_parser_expression (parser, /*cast_p=*/true);
3931         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3932
3933         /* Only type conversions to integral or enumeration types
3934            can be used in constant-expressions.  */
3935         if (parser->integral_constant_expression_p
3936             && !dependent_type_p (type)
3937             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3938             && (cp_parser_non_integral_constant_expression
3939                 (parser,
3940                  "a cast to a type other than an integral or "
3941                  "enumeration type")))
3942           return error_mark_node;
3943
3944         switch (keyword)
3945           {
3946           case RID_DYNCAST:
3947             postfix_expression
3948               = build_dynamic_cast (type, expression);
3949             break;
3950           case RID_STATCAST:
3951             postfix_expression
3952               = build_static_cast (type, expression);
3953             break;
3954           case RID_REINTCAST:
3955             postfix_expression
3956               = build_reinterpret_cast (type, expression);
3957             break;
3958           case RID_CONSTCAST:
3959             postfix_expression
3960               = build_const_cast (type, expression);
3961             break;
3962           default:
3963             gcc_unreachable ();
3964           }
3965       }
3966       break;
3967
3968     case RID_TYPEID:
3969       {
3970         tree type;
3971         const char *saved_message;
3972         bool saved_in_type_id_in_expr_p;
3973
3974         /* Consume the `typeid' token.  */
3975         cp_lexer_consume_token (parser->lexer);
3976         /* Look for the `(' token.  */
3977         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3978         /* Types cannot be defined in a `typeid' expression.  */
3979         saved_message = parser->type_definition_forbidden_message;
3980         parser->type_definition_forbidden_message
3981           = "types may not be defined in a `typeid\' expression";
3982         /* We can't be sure yet whether we're looking at a type-id or an
3983            expression.  */
3984         cp_parser_parse_tentatively (parser);
3985         /* Try a type-id first.  */
3986         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3987         parser->in_type_id_in_expr_p = true;
3988         type = cp_parser_type_id (parser);
3989         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3990         /* Look for the `)' token.  Otherwise, we can't be sure that
3991            we're not looking at an expression: consider `typeid (int
3992            (3))', for example.  */
3993         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3994         /* If all went well, simply lookup the type-id.  */
3995         if (cp_parser_parse_definitely (parser))
3996           postfix_expression = get_typeid (type);
3997         /* Otherwise, fall back to the expression variant.  */
3998         else
3999           {
4000             tree expression;
4001
4002             /* Look for an expression.  */
4003             expression = cp_parser_expression (parser, /*cast_p=*/false);
4004             /* Compute its typeid.  */
4005             postfix_expression = build_typeid (expression);
4006             /* Look for the `)' token.  */
4007             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4008           }
4009         /* `typeid' may not appear in an integral constant expression.  */
4010         if (cp_parser_non_integral_constant_expression(parser,
4011                                                        "`typeid' operator"))
4012           return error_mark_node;
4013         /* Restore the saved message.  */
4014         parser->type_definition_forbidden_message = saved_message;
4015       }
4016       break;
4017
4018     case RID_TYPENAME:
4019       {
4020         tree type;
4021         /* The syntax permitted here is the same permitted for an
4022            elaborated-type-specifier.  */
4023         type = cp_parser_elaborated_type_specifier (parser,
4024                                                     /*is_friend=*/false,
4025                                                     /*is_declaration=*/false);
4026         postfix_expression = cp_parser_functional_cast (parser, type);
4027       }
4028       break;
4029
4030     default:
4031       {
4032         tree type;
4033
4034         /* If the next thing is a simple-type-specifier, we may be
4035            looking at a functional cast.  We could also be looking at
4036            an id-expression.  So, we try the functional cast, and if
4037            that doesn't work we fall back to the primary-expression.  */
4038         cp_parser_parse_tentatively (parser);
4039         /* Look for the simple-type-specifier.  */
4040         type = cp_parser_simple_type_specifier (parser,
4041                                                 /*decl_specs=*/NULL,
4042                                                 CP_PARSER_FLAGS_NONE);
4043         /* Parse the cast itself.  */
4044         if (!cp_parser_error_occurred (parser))
4045           postfix_expression
4046             = cp_parser_functional_cast (parser, type);
4047         /* If that worked, we're done.  */
4048         if (cp_parser_parse_definitely (parser))
4049           break;
4050
4051         /* If the functional-cast didn't work out, try a
4052            compound-literal.  */
4053         if (cp_parser_allow_gnu_extensions_p (parser)
4054             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4055           {
4056             VEC(constructor_elt,gc) *initializer_list = NULL;
4057             bool saved_in_type_id_in_expr_p;
4058
4059             cp_parser_parse_tentatively (parser);
4060             /* Consume the `('.  */
4061             cp_lexer_consume_token (parser->lexer);
4062             /* Parse the type.  */
4063             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4064             parser->in_type_id_in_expr_p = true;
4065             type = cp_parser_type_id (parser);
4066             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4067             /* Look for the `)'.  */
4068             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4069             /* Look for the `{'.  */
4070             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4071             /* If things aren't going well, there's no need to
4072                keep going.  */
4073             if (!cp_parser_error_occurred (parser))
4074               {
4075                 bool non_constant_p;
4076                 /* Parse the initializer-list.  */
4077                 initializer_list
4078                   = cp_parser_initializer_list (parser, &non_constant_p);
4079                 /* Allow a trailing `,'.  */
4080                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4081                   cp_lexer_consume_token (parser->lexer);
4082                 /* Look for the final `}'.  */
4083                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4084               }
4085             /* If that worked, we're definitely looking at a
4086                compound-literal expression.  */
4087             if (cp_parser_parse_definitely (parser))
4088               {
4089                 /* Warn the user that a compound literal is not
4090                    allowed in standard C++.  */
4091                 if (pedantic)
4092                   pedwarn ("ISO C++ forbids compound-literals");
4093                 /* Form the representation of the compound-literal.  */
4094                 postfix_expression
4095                   = finish_compound_literal (type, initializer_list);
4096                 break;
4097               }
4098           }
4099
4100         /* It must be a primary-expression.  */
4101         postfix_expression 
4102           = cp_parser_primary_expression (parser, address_p, cast_p, 
4103                                           /*template_arg_p=*/false,
4104                                           &idk);
4105       }
4106       break;
4107     }
4108
4109   /* Keep looping until the postfix-expression is complete.  */
4110   while (true)
4111     {
4112       if (idk == CP_ID_KIND_UNQUALIFIED
4113           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4114           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4115         /* It is not a Koenig lookup function call.  */
4116         postfix_expression
4117           = unqualified_name_lookup_error (postfix_expression);
4118
4119       /* Peek at the next token.  */
4120       token = cp_lexer_peek_token (parser->lexer);
4121
4122       switch (token->type)
4123         {
4124         case CPP_OPEN_SQUARE:
4125           postfix_expression
4126             = cp_parser_postfix_open_square_expression (parser,
4127                                                         postfix_expression,
4128                                                         false);
4129           idk = CP_ID_KIND_NONE;
4130           break;
4131
4132         case CPP_OPEN_PAREN:
4133           /* postfix-expression ( expression-list [opt] ) */
4134           {
4135             bool koenig_p;
4136             bool is_builtin_constant_p;
4137             bool saved_integral_constant_expression_p = false;
4138             bool saved_non_integral_constant_expression_p = false;
4139             tree args;
4140
4141             is_builtin_constant_p
4142               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4143             if (is_builtin_constant_p)
4144               {
4145                 /* The whole point of __builtin_constant_p is to allow
4146                    non-constant expressions to appear as arguments.  */
4147                 saved_integral_constant_expression_p
4148                   = parser->integral_constant_expression_p;
4149                 saved_non_integral_constant_expression_p
4150                   = parser->non_integral_constant_expression_p;
4151                 parser->integral_constant_expression_p = false;
4152               }
4153             args = (cp_parser_parenthesized_expression_list
4154                     (parser, /*is_attribute_list=*/false,
4155                      /*cast_p=*/false,
4156                      /*non_constant_p=*/NULL));
4157             if (is_builtin_constant_p)
4158               {
4159                 parser->integral_constant_expression_p
4160                   = saved_integral_constant_expression_p;
4161                 parser->non_integral_constant_expression_p
4162                   = saved_non_integral_constant_expression_p;
4163               }
4164
4165             if (args == error_mark_node)
4166               {
4167                 postfix_expression = error_mark_node;
4168                 break;
4169               }
4170
4171             /* Function calls are not permitted in
4172                constant-expressions.  */
4173             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4174                 && cp_parser_non_integral_constant_expression (parser,
4175                                                                "a function call"))
4176               {
4177                 postfix_expression = error_mark_node;
4178                 break;
4179               }
4180
4181             koenig_p = false;
4182             if (idk == CP_ID_KIND_UNQUALIFIED)
4183               {
4184                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4185                   {
4186                     if (args)
4187                       {
4188                         koenig_p = true;
4189                         postfix_expression
4190                           = perform_koenig_lookup (postfix_expression, args);
4191                       }
4192                     else
4193                       postfix_expression
4194                         = unqualified_fn_lookup_error (postfix_expression);
4195                   }
4196                 /* We do not perform argument-dependent lookup if
4197                    normal lookup finds a non-function, in accordance
4198                    with the expected resolution of DR 218.  */
4199                 else if (args && is_overloaded_fn (postfix_expression))
4200                   {
4201                     tree fn = get_first_fn (postfix_expression);
4202
4203                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4204                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4205
4206                     /* Only do argument dependent lookup if regular
4207                        lookup does not find a set of member functions.
4208                        [basic.lookup.koenig]/2a  */
4209                     if (!DECL_FUNCTION_MEMBER_P (fn))
4210                       {
4211                         koenig_p = true;
4212                         postfix_expression
4213                           = perform_koenig_lookup (postfix_expression, args);
4214                       }
4215                   }
4216               }
4217
4218             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4219               {
4220                 tree instance = TREE_OPERAND (postfix_expression, 0);
4221                 tree fn = TREE_OPERAND (postfix_expression, 1);
4222
4223                 if (processing_template_decl
4224                     && (type_dependent_expression_p (instance)
4225                         || (!BASELINK_P (fn)
4226                             && TREE_CODE (fn) != FIELD_DECL)
4227                         || type_dependent_expression_p (fn)
4228                         || any_type_dependent_arguments_p (args)))
4229                   {
4230                     postfix_expression
4231                       = build_min_nt (CALL_EXPR, postfix_expression,
4232                                       args, NULL_TREE);
4233                     break;
4234                   }
4235
4236                 if (BASELINK_P (fn))
4237                   postfix_expression
4238                     = (build_new_method_call
4239                        (instance, fn, args, NULL_TREE,
4240                         (idk == CP_ID_KIND_QUALIFIED
4241                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4242                 else
4243                   postfix_expression
4244                     = finish_call_expr (postfix_expression, args,
4245                                         /*disallow_virtual=*/false,
4246                                         /*koenig_p=*/false);
4247               }
4248             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4249                      || TREE_CODE (postfix_expression) == MEMBER_REF
4250                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4251               postfix_expression = (build_offset_ref_call_from_tree
4252                                     (postfix_expression, args));
4253             else if (idk == CP_ID_KIND_QUALIFIED)
4254               /* A call to a static class member, or a namespace-scope
4255                  function.  */
4256               postfix_expression
4257                 = finish_call_expr (postfix_expression, args,
4258                                     /*disallow_virtual=*/true,
4259                                     koenig_p);
4260             else
4261               /* All other function calls.  */
4262               postfix_expression
4263                 = finish_call_expr (postfix_expression, args,
4264                                     /*disallow_virtual=*/false,
4265                                     koenig_p);
4266
4267             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4268             idk = CP_ID_KIND_NONE;
4269           }
4270           break;
4271
4272         case CPP_DOT:
4273         case CPP_DEREF:
4274           /* postfix-expression . template [opt] id-expression
4275              postfix-expression . pseudo-destructor-name
4276              postfix-expression -> template [opt] id-expression
4277              postfix-expression -> pseudo-destructor-name */
4278
4279           /* Consume the `.' or `->' operator.  */
4280           cp_lexer_consume_token (parser->lexer);
4281
4282           postfix_expression
4283             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4284                                                       postfix_expression,
4285                                                       false, &idk);
4286           break;
4287
4288         case CPP_PLUS_PLUS:
4289           /* postfix-expression ++  */
4290           /* Consume the `++' token.  */
4291           cp_lexer_consume_token (parser->lexer);
4292           /* Generate a representation for the complete expression.  */
4293           postfix_expression
4294             = finish_increment_expr (postfix_expression,
4295                                      POSTINCREMENT_EXPR);
4296           /* Increments may not appear in constant-expressions.  */
4297           if (cp_parser_non_integral_constant_expression (parser,
4298                                                           "an increment"))
4299             postfix_expression = error_mark_node;
4300           idk = CP_ID_KIND_NONE;
4301           break;
4302
4303         case CPP_MINUS_MINUS:
4304           /* postfix-expression -- */
4305           /* Consume the `--' token.  */
4306           cp_lexer_consume_token (parser->lexer);
4307           /* Generate a representation for the complete expression.  */
4308           postfix_expression
4309             = finish_increment_expr (postfix_expression,
4310                                      POSTDECREMENT_EXPR);
4311           /* Decrements may not appear in constant-expressions.  */
4312           if (cp_parser_non_integral_constant_expression (parser,
4313                                                           "a decrement"))
4314             postfix_expression = error_mark_node;
4315           idk = CP_ID_KIND_NONE;
4316           break;
4317
4318         default:
4319           return postfix_expression;
4320         }
4321     }
4322
4323   /* We should never get here.  */
4324   gcc_unreachable ();
4325   return error_mark_node;
4326 }
4327
4328 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4329    by cp_parser_builtin_offsetof.  We're looking for
4330
4331      postfix-expression [ expression ]
4332
4333    FOR_OFFSETOF is set if we're being called in that context, which
4334    changes how we deal with integer constant expressions.  */
4335
4336 static tree
4337 cp_parser_postfix_open_square_expression (cp_parser *parser,
4338                                           tree postfix_expression,
4339                                           bool for_offsetof)
4340 {
4341   tree index;
4342
4343   /* Consume the `[' token.  */
4344   cp_lexer_consume_token (parser->lexer);
4345
4346   /* Parse the index expression.  */
4347   /* ??? For offsetof, there is a question of what to allow here.  If
4348      offsetof is not being used in an integral constant expression context,
4349      then we *could* get the right answer by computing the value at runtime.
4350      If we are in an integral constant expression context, then we might
4351      could accept any constant expression; hard to say without analysis.
4352      Rather than open the barn door too wide right away, allow only integer
4353      constant expressions here.  */
4354   if (for_offsetof)
4355     index = cp_parser_constant_expression (parser, false, NULL);
4356   else
4357     index = cp_parser_expression (parser, /*cast_p=*/false);
4358
4359   /* Look for the closing `]'.  */
4360   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4361
4362   /* Build the ARRAY_REF.  */
4363   postfix_expression = grok_array_decl (postfix_expression, index);
4364
4365   /* When not doing offsetof, array references are not permitted in
4366      constant-expressions.  */
4367   if (!for_offsetof
4368       && (cp_parser_non_integral_constant_expression
4369           (parser, "an array reference")))
4370     postfix_expression = error_mark_node;
4371
4372   return postfix_expression;
4373 }
4374
4375 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4376    by cp_parser_builtin_offsetof.  We're looking for
4377
4378      postfix-expression . template [opt] id-expression
4379      postfix-expression . pseudo-destructor-name
4380      postfix-expression -> template [opt] id-expression
4381      postfix-expression -> pseudo-destructor-name
4382
4383    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4384    limits what of the above we'll actually accept, but nevermind.
4385    TOKEN_TYPE is the "." or "->" token, which will already have been
4386    removed from the stream.  */
4387
4388 static tree
4389 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4390                                         enum cpp_ttype token_type,
4391                                         tree postfix_expression,
4392                                         bool for_offsetof, cp_id_kind *idk)
4393 {
4394   tree name;
4395   bool dependent_p;
4396   bool pseudo_destructor_p;
4397   tree scope = NULL_TREE;
4398
4399   /* If this is a `->' operator, dereference the pointer.  */
4400   if (token_type == CPP_DEREF)
4401     postfix_expression = build_x_arrow (postfix_expression);
4402   /* Check to see whether or not the expression is type-dependent.  */
4403   dependent_p = type_dependent_expression_p (postfix_expression);
4404   /* The identifier following the `->' or `.' is not qualified.  */
4405   parser->scope = NULL_TREE;
4406   parser->qualifying_scope = NULL_TREE;
4407   parser->object_scope = NULL_TREE;
4408   *idk = CP_ID_KIND_NONE;
4409   /* Enter the scope corresponding to the type of the object
4410      given by the POSTFIX_EXPRESSION.  */
4411   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4412     {
4413       scope = TREE_TYPE (postfix_expression);
4414       /* According to the standard, no expression should ever have
4415          reference type.  Unfortunately, we do not currently match
4416          the standard in this respect in that our internal representation
4417          of an expression may have reference type even when the standard
4418          says it does not.  Therefore, we have to manually obtain the
4419          underlying type here.  */
4420       scope = non_reference (scope);
4421       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4422       if (scope == unknown_type_node)
4423         {
4424           error ("%qE does not have class type", postfix_expression);
4425           scope = NULL_TREE;
4426         }
4427       else
4428         scope = complete_type_or_else (scope, NULL_TREE);
4429       /* Let the name lookup machinery know that we are processing a
4430          class member access expression.  */
4431       parser->context->object_type = scope;
4432       /* If something went wrong, we want to be able to discern that case,
4433          as opposed to the case where there was no SCOPE due to the type
4434          of expression being dependent.  */
4435       if (!scope)
4436         scope = error_mark_node;
4437       /* If the SCOPE was erroneous, make the various semantic analysis
4438          functions exit quickly -- and without issuing additional error
4439          messages.  */
4440       if (scope == error_mark_node)
4441         postfix_expression = error_mark_node;
4442     }
4443
4444   /* Assume this expression is not a pseudo-destructor access.  */
4445   pseudo_destructor_p = false;
4446
4447   /* If the SCOPE is a scalar type, then, if this is a valid program,
4448      we must be looking at a pseudo-destructor-name.  */
4449   if (scope && SCALAR_TYPE_P (scope))
4450     {
4451       tree s;
4452       tree type;
4453
4454       cp_parser_parse_tentatively (parser);
4455       /* Parse the pseudo-destructor-name.  */
4456       s = NULL_TREE;
4457       cp_parser_pseudo_destructor_name (parser, &s, &type);
4458       if (cp_parser_parse_definitely (parser))
4459         {
4460           pseudo_destructor_p = true;
4461           postfix_expression
4462             = finish_pseudo_destructor_expr (postfix_expression,
4463                                              s, TREE_TYPE (type));
4464         }
4465     }
4466
4467   if (!pseudo_destructor_p)
4468     {
4469       /* If the SCOPE is not a scalar type, we are looking at an
4470          ordinary class member access expression, rather than a
4471          pseudo-destructor-name.  */
4472       bool template_p;
4473       /* Parse the id-expression.  */
4474       name = (cp_parser_id_expression 
4475               (parser, 
4476                cp_parser_optional_template_keyword (parser),
4477                /*check_dependency_p=*/true,
4478                &template_p,
4479                /*declarator_p=*/false));
4480       /* In general, build a SCOPE_REF if the member name is qualified.
4481          However, if the name was not dependent and has already been
4482          resolved; there is no need to build the SCOPE_REF.  For example;
4483
4484              struct X { void f(); };
4485              template <typename T> void f(T* t) { t->X::f(); }
4486
4487          Even though "t" is dependent, "X::f" is not and has been resolved
4488          to a BASELINK; there is no need to include scope information.  */
4489
4490       /* But we do need to remember that there was an explicit scope for
4491          virtual function calls.  */
4492       if (parser->scope)
4493         *idk = CP_ID_KIND_QUALIFIED;
4494
4495       /* If the name is a template-id that names a type, we will get a
4496          TYPE_DECL here.  That is invalid code.  */
4497       if (TREE_CODE (name) == TYPE_DECL)
4498         {
4499           error ("invalid use of %qD", name);
4500           postfix_expression = error_mark_node;
4501         }
4502       else
4503         {
4504           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4505             {
4506               name = build_qualified_name (/*type=*/NULL_TREE,
4507                                            parser->scope,
4508                                            name,
4509                                            template_p);
4510               parser->scope = NULL_TREE;
4511               parser->qualifying_scope = NULL_TREE;
4512               parser->object_scope = NULL_TREE;
4513             }
4514           if (scope && name && BASELINK_P (name))
4515             adjust_result_of_qualified_name_lookup
4516               (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4517           postfix_expression
4518             = finish_class_member_access_expr (postfix_expression, name,
4519                                                template_p);
4520         }
4521     }
4522
4523   /* We no longer need to look up names in the scope of the object on
4524      the left-hand side of the `.' or `->' operator.  */
4525   parser->context->object_type = NULL_TREE;
4526
4527   /* Outside of offsetof, these operators may not appear in
4528      constant-expressions.  */
4529   if (!for_offsetof
4530       && (cp_parser_non_integral_constant_expression
4531           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4532     postfix_expression = error_mark_node;
4533
4534   return postfix_expression;
4535 }
4536
4537 /* Parse a parenthesized expression-list.
4538
4539    expression-list:
4540      assignment-expression
4541      expression-list, assignment-expression
4542
4543    attribute-list:
4544      expression-list
4545      identifier
4546      identifier, expression-list
4547
4548    CAST_P is true if this expression is the target of a cast.
4549
4550    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4551    representation of an assignment-expression.  Note that a TREE_LIST
4552    is returned even if there is only a single expression in the list.
4553    error_mark_node is returned if the ( and or ) are
4554    missing. NULL_TREE is returned on no expressions. The parentheses
4555    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4556    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4557    indicates whether or not all of the expressions in the list were
4558    constant.  */
4559
4560 static tree
4561 cp_parser_parenthesized_expression_list (cp_parser* parser,
4562                                          bool is_attribute_list,
4563                                          bool cast_p,
4564                                          bool *non_constant_p)
4565 {
4566   tree expression_list = NULL_TREE;
4567   bool fold_expr_p = is_attribute_list;
4568   tree identifier = NULL_TREE;
4569
4570   /* Assume all the expressions will be constant.  */
4571   if (non_constant_p)
4572     *non_constant_p = false;
4573
4574   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4575     return error_mark_node;
4576
4577   /* Consume expressions until there are no more.  */
4578   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4579     while (true)
4580       {
4581         tree expr;
4582
4583         /* At the beginning of attribute lists, check to see if the
4584            next token is an identifier.  */
4585         if (is_attribute_list
4586             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4587           {
4588             cp_token *token;
4589
4590             /* Consume the identifier.  */
4591             token = cp_lexer_consume_token (parser->lexer);
4592             /* Save the identifier.  */
4593             identifier = token->value;
4594           }
4595         else
4596           {
4597             /* Parse the next assignment-expression.  */
4598             if (non_constant_p)
4599               {
4600                 bool expr_non_constant_p;
4601                 expr = (cp_parser_constant_expression
4602                         (parser, /*allow_non_constant_p=*/true,
4603                          &expr_non_constant_p));
4604                 if (expr_non_constant_p)
4605                   *non_constant_p = true;
4606               }
4607             else
4608               expr = cp_parser_assignment_expression (parser, cast_p);
4609
4610             if (fold_expr_p)
4611               expr = fold_non_dependent_expr (expr);
4612
4613              /* Add it to the list.  We add error_mark_node
4614                 expressions to the list, so that we can still tell if
4615                 the correct form for a parenthesized expression-list
4616                 is found. That gives better errors.  */
4617             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4618
4619             if (expr == error_mark_node)
4620               goto skip_comma;
4621           }
4622
4623         /* After the first item, attribute lists look the same as
4624            expression lists.  */
4625         is_attribute_list = false;
4626
4627       get_comma:;
4628         /* If the next token isn't a `,', then we are done.  */
4629         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4630           break;
4631
4632         /* Otherwise, consume the `,' and keep going.  */
4633         cp_lexer_consume_token (parser->lexer);
4634       }
4635
4636   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4637     {
4638       int ending;
4639
4640     skip_comma:;
4641       /* We try and resync to an unnested comma, as that will give the
4642          user better diagnostics.  */
4643       ending = cp_parser_skip_to_closing_parenthesis (parser,
4644                                                       /*recovering=*/true,
4645                                                       /*or_comma=*/true,
4646                                                       /*consume_paren=*/true);
4647       if (ending < 0)
4648         goto get_comma;
4649       if (!ending)
4650         return error_mark_node;
4651     }
4652
4653   /* We built up the list in reverse order so we must reverse it now.  */
4654   expression_list = nreverse (expression_list);
4655   if (identifier)
4656     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4657
4658   return expression_list;
4659 }
4660
4661 /* Parse a pseudo-destructor-name.
4662
4663    pseudo-destructor-name:
4664      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4665      :: [opt] nested-name-specifier template template-id :: ~ type-name
4666      :: [opt] nested-name-specifier [opt] ~ type-name
4667
4668    If either of the first two productions is used, sets *SCOPE to the
4669    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4670    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4671    or ERROR_MARK_NODE if the parse fails.  */
4672
4673 static void
4674 cp_parser_pseudo_destructor_name (cp_parser* parser,
4675                                   tree* scope,
4676                                   tree* type)
4677 {
4678   bool nested_name_specifier_p;
4679
4680   /* Assume that things will not work out.  */
4681   *type = error_mark_node;
4682
4683   /* Look for the optional `::' operator.  */
4684   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4685   /* Look for the optional nested-name-specifier.  */
4686   nested_name_specifier_p
4687     = (cp_parser_nested_name_specifier_opt (parser,
4688                                             /*typename_keyword_p=*/false,
4689                                             /*check_dependency_p=*/true,
4690                                             /*type_p=*/false,
4691                                             /*is_declaration=*/true)
4692        != NULL_TREE);
4693   /* Now, if we saw a nested-name-specifier, we might be doing the
4694      second production.  */
4695   if (nested_name_specifier_p
4696       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4697     {
4698       /* Consume the `template' keyword.  */
4699       cp_lexer_consume_token (parser->lexer);
4700       /* Parse the template-id.  */
4701       cp_parser_template_id (parser,
4702                              /*template_keyword_p=*/true,
4703                              /*check_dependency_p=*/false,
4704                              /*is_declaration=*/true);
4705       /* Look for the `::' token.  */
4706       cp_parser_require (parser, CPP_SCOPE, "`::'");
4707     }
4708   /* If the next token is not a `~', then there might be some
4709      additional qualification.  */
4710   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4711     {
4712       /* Look for the type-name.  */
4713       *scope = TREE_TYPE (cp_parser_type_name (parser));
4714
4715       if (*scope == error_mark_node)
4716         return;
4717
4718       /* If we don't have ::~, then something has gone wrong.  Since
4719          the only caller of this function is looking for something
4720          after `.' or `->' after a scalar type, most likely the
4721          program is trying to get a member of a non-aggregate
4722          type.  */
4723       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4724           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4725         {
4726           cp_parser_error (parser, "request for member of non-aggregate type");
4727           return;
4728         }
4729
4730       /* Look for the `::' token.  */
4731       cp_parser_require (parser, CPP_SCOPE, "`::'");
4732     }
4733   else
4734     *scope = NULL_TREE;
4735
4736   /* Look for the `~'.  */
4737   cp_parser_require (parser, CPP_COMPL, "`~'");
4738   /* Look for the type-name again.  We are not responsible for
4739      checking that it matches the first type-name.  */
4740   *type = cp_parser_type_name (parser);
4741 }
4742
4743 /* Parse a unary-expression.
4744
4745    unary-expression:
4746      postfix-expression
4747      ++ cast-expression
4748      -- cast-expression
4749      unary-operator cast-expression
4750      sizeof unary-expression
4751      sizeof ( type-id )
4752      new-expression
4753      delete-expression
4754
4755    GNU Extensions:
4756
4757    unary-expression:
4758      __extension__ cast-expression
4759      __alignof__ unary-expression
4760      __alignof__ ( type-id )
4761      __real__ cast-expression
4762      __imag__ cast-expression
4763      && identifier
4764
4765    ADDRESS_P is true iff the unary-expression is appearing as the
4766    operand of the `&' operator.   CAST_P is true if this expression is
4767    the target of a cast.
4768
4769    Returns a representation of the expression.  */
4770
4771 static tree
4772 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4773 {
4774   cp_token *token;
4775   enum tree_code unary_operator;
4776
4777   /* Peek at the next token.  */
4778   token = cp_lexer_peek_token (parser->lexer);
4779   /* Some keywords give away the kind of expression.  */
4780   if (token->type == CPP_KEYWORD)
4781     {
4782       enum rid keyword = token->keyword;
4783
4784       switch (keyword)
4785         {
4786         case RID_ALIGNOF:
4787         case RID_SIZEOF:
4788           {
4789             tree operand;
4790             enum tree_code op;
4791
4792             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4793             /* Consume the token.  */
4794             cp_lexer_consume_token (parser->lexer);
4795             /* Parse the operand.  */
4796             operand = cp_parser_sizeof_operand (parser, keyword);
4797
4798             if (TYPE_P (operand))
4799               return cxx_sizeof_or_alignof_type (operand, op, true);
4800             else
4801               return cxx_sizeof_or_alignof_expr (operand, op);
4802           }
4803
4804         case RID_NEW:
4805           return cp_parser_new_expression (parser);
4806
4807         case RID_DELETE:
4808           return cp_parser_delete_expression (parser);
4809
4810         case RID_EXTENSION:
4811           {
4812             /* The saved value of the PEDANTIC flag.  */
4813             int saved_pedantic;
4814             tree expr;
4815
4816             /* Save away the PEDANTIC flag.  */
4817             cp_parser_extension_opt (parser, &saved_pedantic);
4818             /* Parse the cast-expression.  */
4819             expr = cp_parser_simple_cast_expression (parser);
4820             /* Restore the PEDANTIC flag.  */
4821             pedantic = saved_pedantic;
4822
4823             return expr;
4824           }
4825
4826         case RID_REALPART:
4827         case RID_IMAGPART:
4828           {
4829             tree expression;
4830
4831             /* Consume the `__real__' or `__imag__' token.  */
4832             cp_lexer_consume_token (parser->lexer);
4833             /* Parse the cast-expression.  */
4834             expression = cp_parser_simple_cast_expression (parser);
4835             /* Create the complete representation.  */
4836             return build_x_unary_op ((keyword == RID_REALPART
4837                                       ? REALPART_EXPR : IMAGPART_EXPR),
4838                                      expression);
4839           }
4840           break;
4841
4842         default:
4843           break;
4844         }
4845     }
4846
4847   /* Look for the `:: new' and `:: delete', which also signal the
4848      beginning of a new-expression, or delete-expression,
4849      respectively.  If the next token is `::', then it might be one of
4850      these.  */
4851   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4852     {
4853       enum rid keyword;
4854
4855       /* See if the token after the `::' is one of the keywords in
4856          which we're interested.  */
4857       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4858       /* If it's `new', we have a new-expression.  */
4859       if (keyword == RID_NEW)
4860         return cp_parser_new_expression (parser);
4861       /* Similarly, for `delete'.  */
4862       else if (keyword == RID_DELETE)
4863         return cp_parser_delete_expression (parser);
4864     }
4865
4866   /* Look for a unary operator.  */
4867   unary_operator = cp_parser_unary_operator (token);
4868   /* The `++' and `--' operators can be handled similarly, even though
4869      they are not technically unary-operators in the grammar.  */
4870   if (unary_operator == ERROR_MARK)
4871     {
4872       if (token->type == CPP_PLUS_PLUS)
4873         unary_operator = PREINCREMENT_EXPR;
4874       else if (token->type == CPP_MINUS_MINUS)
4875         unary_operator = PREDECREMENT_EXPR;
4876       /* Handle the GNU address-of-label extension.  */
4877       else if (cp_parser_allow_gnu_extensions_p (parser)
4878                && token->type == CPP_AND_AND)
4879         {
4880           tree identifier;
4881
4882           /* Consume the '&&' token.  */
4883           cp_lexer_consume_token (parser->lexer);
4884           /* Look for the identifier.  */
4885           identifier = cp_parser_identifier (parser);
4886           /* Create an expression representing the address.  */
4887           return finish_label_address_expr (identifier);
4888         }
4889     }
4890   if (unary_operator != ERROR_MARK)
4891     {
4892       tree cast_expression;
4893       tree expression = error_mark_node;
4894       const char *non_constant_p = NULL;
4895
4896       /* Consume the operator token.  */
4897       token = cp_lexer_consume_token (parser->lexer);
4898       /* Parse the cast-expression.  */
4899       cast_expression
4900         = cp_parser_cast_expression (parser,
4901                                      unary_operator == ADDR_EXPR,
4902                                      /*cast_p=*/false);
4903       /* Now, build an appropriate representation.  */
4904       switch (unary_operator)
4905         {
4906         case INDIRECT_REF:
4907           non_constant_p = "`*'";
4908           expression = build_x_indirect_ref (cast_expression, "unary *");
4909           break;
4910
4911         case ADDR_EXPR:
4912           non_constant_p = "`&'";
4913           /* Fall through.  */
4914         case BIT_NOT_EXPR:
4915           expression = build_x_unary_op (unary_operator, cast_expression);
4916           break;
4917
4918         case PREINCREMENT_EXPR:
4919         case PREDECREMENT_EXPR:
4920           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4921                             ? "`++'" : "`--'");
4922           /* Fall through.  */
4923         case UNARY_PLUS_EXPR:
4924         case NEGATE_EXPR:
4925         case TRUTH_NOT_EXPR:
4926           expression = finish_unary_op_expr (unary_operator, cast_expression);
4927           break;
4928
4929         default:
4930           gcc_unreachable ();
4931         }
4932
4933       if (non_constant_p
4934           && cp_parser_non_integral_constant_expression (parser,
4935                                                          non_constant_p))
4936         expression = error_mark_node;
4937
4938       return expression;
4939     }
4940
4941   return cp_parser_postfix_expression (parser, address_p, cast_p);
4942 }
4943
4944 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4945    unary-operator, the corresponding tree code is returned.  */
4946
4947 static enum tree_code
4948 cp_parser_unary_operator (cp_token* token)
4949 {
4950   switch (token->type)
4951     {
4952     case CPP_MULT:
4953       return INDIRECT_REF;
4954
4955     case CPP_AND:
4956       return ADDR_EXPR;
4957
4958     case CPP_PLUS:
4959       return UNARY_PLUS_EXPR;
4960
4961     case CPP_MINUS:
4962       return NEGATE_EXPR;
4963
4964     case CPP_NOT:
4965       return TRUTH_NOT_EXPR;
4966
4967     case CPP_COMPL:
4968       return BIT_NOT_EXPR;
4969
4970     default:
4971       return ERROR_MARK;
4972     }
4973 }
4974
4975 /* Parse a new-expression.
4976
4977    new-expression:
4978      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4979      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4980
4981    Returns a representation of the expression.  */
4982
4983 static tree
4984 cp_parser_new_expression (cp_parser* parser)
4985 {
4986   bool global_scope_p;
4987   tree placement;
4988   tree type;
4989   tree initializer;
4990   tree nelts;
4991
4992   /* Look for the optional `::' operator.  */
4993   global_scope_p
4994     = (cp_parser_global_scope_opt (parser,
4995                                    /*current_scope_valid_p=*/false)
4996        != NULL_TREE);
4997   /* Look for the `new' operator.  */
4998   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4999   /* There's no easy way to tell a new-placement from the
5000      `( type-id )' construct.  */
5001   cp_parser_parse_tentatively (parser);
5002   /* Look for a new-placement.  */
5003   placement = cp_parser_new_placement (parser);
5004   /* If that didn't work out, there's no new-placement.  */
5005   if (!cp_parser_parse_definitely (parser))
5006     placement = NULL_TREE;
5007
5008   /* If the next token is a `(', then we have a parenthesized
5009      type-id.  */
5010   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5011     {
5012       /* Consume the `('.  */
5013       cp_lexer_consume_token (parser->lexer);
5014       /* Parse the type-id.  */
5015       type = cp_parser_type_id (parser);
5016       /* Look for the closing `)'.  */
5017       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5018       /* There should not be a direct-new-declarator in this production,
5019          but GCC used to allowed this, so we check and emit a sensible error
5020          message for this case.  */
5021       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5022         {
5023           error ("array bound forbidden after parenthesized type-id");
5024           inform ("try removing the parentheses around the type-id");
5025           cp_parser_direct_new_declarator (parser);
5026         }
5027       nelts = NULL_TREE;
5028     }
5029   /* Otherwise, there must be a new-type-id.  */
5030   else
5031     type = cp_parser_new_type_id (parser, &nelts);
5032
5033   /* If the next token is a `(', then we have a new-initializer.  */
5034   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5035     initializer = cp_parser_new_initializer (parser);
5036   else
5037     initializer = NULL_TREE;
5038
5039   /* A new-expression may not appear in an integral constant
5040      expression.  */
5041   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5042     return error_mark_node;
5043
5044   /* Create a representation of the new-expression.  */
5045   return build_new (placement, type, nelts, initializer, global_scope_p);
5046 }
5047
5048 /* Parse a new-placement.
5049
5050    new-placement:
5051      ( expression-list )
5052
5053    Returns the same representation as for an expression-list.  */
5054
5055 static tree
5056 cp_parser_new_placement (cp_parser* parser)
5057 {
5058   tree expression_list;
5059
5060   /* Parse the expression-list.  */
5061   expression_list = (cp_parser_parenthesized_expression_list
5062                      (parser, false, /*cast_p=*/false,
5063                       /*non_constant_p=*/NULL));
5064
5065   return expression_list;
5066 }
5067
5068 /* Parse a new-type-id.
5069
5070    new-type-id:
5071      type-specifier-seq new-declarator [opt]
5072
5073    Returns the TYPE allocated.  If the new-type-id indicates an array
5074    type, *NELTS is set to the number of elements in the last array
5075    bound; the TYPE will not include the last array bound.  */
5076
5077 static tree
5078 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5079 {
5080   cp_decl_specifier_seq type_specifier_seq;
5081   cp_declarator *new_declarator;
5082   cp_declarator *declarator;
5083   cp_declarator *outer_declarator;
5084   const char *saved_message;
5085   tree type;
5086
5087   /* The type-specifier sequence must not contain type definitions.
5088      (It cannot contain declarations of new types either, but if they
5089      are not definitions we will catch that because they are not
5090      complete.)  */
5091   saved_message = parser->type_definition_forbidden_message;
5092   parser->type_definition_forbidden_message
5093     = "types may not be defined in a new-type-id";
5094   /* Parse the type-specifier-seq.  */
5095   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5096                                 &type_specifier_seq);
5097   /* Restore the old message.  */
5098   parser->type_definition_forbidden_message = saved_message;
5099   /* Parse the new-declarator.  */
5100   new_declarator = cp_parser_new_declarator_opt (parser);
5101
5102   /* Determine the number of elements in the last array dimension, if
5103      any.  */
5104   *nelts = NULL_TREE;
5105   /* Skip down to the last array dimension.  */
5106   declarator = new_declarator;
5107   outer_declarator = NULL;
5108   while (declarator && (declarator->kind == cdk_pointer
5109                         || declarator->kind == cdk_ptrmem))
5110     {
5111       outer_declarator = declarator;
5112       declarator = declarator->declarator;
5113     }
5114   while (declarator
5115          && declarator->kind == cdk_array
5116          && declarator->declarator
5117          && declarator->declarator->kind == cdk_array)
5118     {
5119       outer_declarator = declarator;
5120       declarator = declarator->declarator;
5121     }
5122
5123   if (declarator && declarator->kind == cdk_array)
5124     {
5125       *nelts = declarator->u.array.bounds;
5126       if (*nelts == error_mark_node)
5127         *nelts = integer_one_node;
5128
5129       if (outer_declarator)
5130         outer_declarator->declarator = declarator->declarator;
5131       else
5132         new_declarator = NULL;
5133     }
5134
5135   type = groktypename (&type_specifier_seq, new_declarator);
5136   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5137     {
5138       *nelts = array_type_nelts_top (type);
5139       type = TREE_TYPE (type);
5140     }
5141   return type;
5142 }
5143
5144 /* Parse an (optional) new-declarator.
5145
5146    new-declarator:
5147      ptr-operator new-declarator [opt]
5148      direct-new-declarator
5149
5150    Returns the declarator.  */
5151
5152 static cp_declarator *
5153 cp_parser_new_declarator_opt (cp_parser* parser)
5154 {
5155   enum tree_code code;
5156   tree type;
5157   cp_cv_quals cv_quals;
5158
5159   /* We don't know if there's a ptr-operator next, or not.  */
5160   cp_parser_parse_tentatively (parser);
5161   /* Look for a ptr-operator.  */
5162   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5163   /* If that worked, look for more new-declarators.  */
5164   if (cp_parser_parse_definitely (parser))
5165     {
5166       cp_declarator *declarator;
5167
5168       /* Parse another optional declarator.  */
5169       declarator = cp_parser_new_declarator_opt (parser);
5170
5171       /* Create the representation of the declarator.  */
5172       if (type)
5173         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5174       else if (code == INDIRECT_REF)
5175         declarator = make_pointer_declarator (cv_quals, declarator);
5176       else
5177         declarator = make_reference_declarator (cv_quals, declarator);
5178
5179       return declarator;
5180     }
5181
5182   /* If the next token is a `[', there is a direct-new-declarator.  */
5183   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5184     return cp_parser_direct_new_declarator (parser);
5185
5186   return NULL;
5187 }
5188
5189 /* Parse a direct-new-declarator.
5190
5191    direct-new-declarator:
5192      [ expression ]
5193      direct-new-declarator [constant-expression]
5194
5195    */
5196
5197 static cp_declarator *
5198 cp_parser_direct_new_declarator (cp_parser* parser)
5199 {
5200   cp_declarator *declarator = NULL;
5201
5202   while (true)
5203     {
5204       tree expression;
5205
5206       /* Look for the opening `['.  */
5207       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5208       /* The first expression is not required to be constant.  */
5209       if (!declarator)
5210         {
5211           expression = cp_parser_expression (parser, /*cast_p=*/false);
5212           /* The standard requires that the expression have integral
5213              type.  DR 74 adds enumeration types.  We believe that the
5214              real intent is that these expressions be handled like the
5215              expression in a `switch' condition, which also allows
5216              classes with a single conversion to integral or
5217              enumeration type.  */
5218           if (!processing_template_decl)
5219             {
5220               expression
5221                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5222                                               expression,
5223                                               /*complain=*/true);
5224               if (!expression)
5225                 {
5226                   error ("expression in new-declarator must have integral "
5227                          "or enumeration type");
5228                   expression = error_mark_node;
5229                 }
5230             }
5231         }
5232       /* But all the other expressions must be.  */
5233       else
5234         expression
5235           = cp_parser_constant_expression (parser,
5236                                            /*allow_non_constant=*/false,
5237                                            NULL);
5238       /* Look for the closing `]'.  */
5239       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5240
5241       /* Add this bound to the declarator.  */
5242       declarator = make_array_declarator (declarator, expression);
5243
5244       /* If the next token is not a `[', then there are no more
5245          bounds.  */
5246       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5247         break;
5248     }
5249
5250   return declarator;
5251 }
5252
5253 /* Parse a new-initializer.
5254
5255    new-initializer:
5256      ( expression-list [opt] )
5257
5258    Returns a representation of the expression-list.  If there is no
5259    expression-list, VOID_ZERO_NODE is returned.  */
5260
5261 static tree
5262 cp_parser_new_initializer (cp_parser* parser)
5263 {
5264   tree expression_list;
5265
5266   expression_list = (cp_parser_parenthesized_expression_list
5267                      (parser, false, /*cast_p=*/false,
5268                       /*non_constant_p=*/NULL));
5269   if (!expression_list)
5270     expression_list = void_zero_node;
5271
5272   return expression_list;
5273 }
5274
5275 /* Parse a delete-expression.
5276
5277    delete-expression:
5278      :: [opt] delete cast-expression
5279      :: [opt] delete [ ] cast-expression
5280
5281    Returns a representation of the expression.  */
5282
5283 static tree
5284 cp_parser_delete_expression (cp_parser* parser)
5285 {
5286   bool global_scope_p;
5287   bool array_p;
5288   tree expression;
5289
5290   /* Look for the optional `::' operator.  */
5291   global_scope_p
5292     = (cp_parser_global_scope_opt (parser,
5293                                    /*current_scope_valid_p=*/false)
5294        != NULL_TREE);
5295   /* Look for the `delete' keyword.  */
5296   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5297   /* See if the array syntax is in use.  */
5298   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5299     {
5300       /* Consume the `[' token.  */
5301       cp_lexer_consume_token (parser->lexer);
5302       /* Look for the `]' token.  */
5303       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5304       /* Remember that this is the `[]' construct.  */
5305       array_p = true;
5306     }
5307   else
5308     array_p = false;
5309
5310   /* Parse the cast-expression.  */
5311   expression = cp_parser_simple_cast_expression (parser);
5312
5313   /* A delete-expression may not appear in an integral constant
5314      expression.  */
5315   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5316     return error_mark_node;
5317
5318   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5319 }
5320
5321 /* Parse a cast-expression.
5322
5323    cast-expression:
5324      unary-expression
5325      ( type-id ) cast-expression
5326
5327    ADDRESS_P is true iff the unary-expression is appearing as the
5328    operand of the `&' operator.   CAST_P is true if this expression is
5329    the target of a cast.
5330
5331    Returns a representation of the expression.  */
5332
5333 static tree
5334 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5335 {
5336   /* If it's a `(', then we might be looking at a cast.  */
5337   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5338     {
5339       tree type = NULL_TREE;
5340       tree expr = NULL_TREE;
5341       bool compound_literal_p;
5342       const char *saved_message;
5343
5344       /* There's no way to know yet whether or not this is a cast.
5345          For example, `(int (3))' is a unary-expression, while `(int)
5346          3' is a cast.  So, we resort to parsing tentatively.  */
5347       cp_parser_parse_tentatively (parser);
5348       /* Types may not be defined in a cast.  */
5349       saved_message = parser->type_definition_forbidden_message;
5350       parser->type_definition_forbidden_message
5351         = "types may not be defined in casts";
5352       /* Consume the `('.  */
5353       cp_lexer_consume_token (parser->lexer);
5354       /* A very tricky bit is that `(struct S) { 3 }' is a
5355          compound-literal (which we permit in C++ as an extension).
5356          But, that construct is not a cast-expression -- it is a
5357          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5358          is legal; if the compound-literal were a cast-expression,
5359          you'd need an extra set of parentheses.)  But, if we parse
5360          the type-id, and it happens to be a class-specifier, then we
5361          will commit to the parse at that point, because we cannot
5362          undo the action that is done when creating a new class.  So,
5363          then we cannot back up and do a postfix-expression.
5364
5365          Therefore, we scan ahead to the closing `)', and check to see
5366          if the token after the `)' is a `{'.  If so, we are not
5367          looking at a cast-expression.
5368
5369          Save tokens so that we can put them back.  */
5370       cp_lexer_save_tokens (parser->lexer);
5371       /* Skip tokens until the next token is a closing parenthesis.
5372          If we find the closing `)', and the next token is a `{', then
5373          we are looking at a compound-literal.  */
5374       compound_literal_p
5375         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5376                                                   /*consume_paren=*/true)
5377            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5378       /* Roll back the tokens we skipped.  */
5379       cp_lexer_rollback_tokens (parser->lexer);
5380       /* If we were looking at a compound-literal, simulate an error
5381          so that the call to cp_parser_parse_definitely below will
5382          fail.  */
5383       if (compound_literal_p)
5384         cp_parser_simulate_error (parser);
5385       else
5386         {
5387           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5388           parser->in_type_id_in_expr_p = true;
5389           /* Look for the type-id.  */
5390           type = cp_parser_type_id (parser);
5391           /* Look for the closing `)'.  */
5392           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5393           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5394         }
5395
5396       /* Restore the saved message.  */
5397       parser->type_definition_forbidden_message = saved_message;
5398
5399       /* If ok so far, parse the dependent expression. We cannot be
5400          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5401          ctor of T, but looks like a cast to function returning T
5402          without a dependent expression.  */
5403       if (!cp_parser_error_occurred (parser))
5404         expr = cp_parser_cast_expression (parser,
5405                                           /*address_p=*/false,
5406                                           /*cast_p=*/true);
5407
5408       if (cp_parser_parse_definitely (parser))
5409         {
5410           /* Warn about old-style casts, if so requested.  */
5411           if (warn_old_style_cast
5412               && !in_system_header
5413               && !VOID_TYPE_P (type)
5414               && current_lang_name != lang_name_c)
5415             warning (0, "use of old-style cast");
5416
5417           /* Only type conversions to integral or enumeration types
5418              can be used in constant-expressions.  */
5419           if (parser->integral_constant_expression_p
5420               && !dependent_type_p (type)
5421               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5422               && (cp_parser_non_integral_constant_expression
5423                   (parser,
5424                    "a cast to a type other than an integral or "
5425                    "enumeration type")))
5426             return error_mark_node;
5427
5428           /* Perform the cast.  */
5429           expr = build_c_cast (type, expr);
5430           return expr;
5431         }
5432     }
5433
5434   /* If we get here, then it's not a cast, so it must be a
5435      unary-expression.  */
5436   return cp_parser_unary_expression (parser, address_p, cast_p);
5437 }
5438
5439 /* Parse a binary expression of the general form:
5440
5441    pm-expression:
5442      cast-expression
5443      pm-expression .* cast-expression
5444      pm-expression ->* cast-expression
5445
5446    multiplicative-expression:
5447      pm-expression
5448      multiplicative-expression * pm-expression
5449      multiplicative-expression / pm-expression
5450      multiplicative-expression % pm-expression
5451
5452    additive-expression:
5453      multiplicative-expression
5454      additive-expression + multiplicative-expression
5455      additive-expression - multiplicative-expression
5456
5457    shift-expression:
5458      additive-expression
5459      shift-expression << additive-expression
5460      shift-expression >> additive-expression
5461
5462    relational-expression:
5463      shift-expression
5464      relational-expression < shift-expression
5465      relational-expression > shift-expression
5466      relational-expression <= shift-expression
5467      relational-expression >= shift-expression
5468
5469   GNU Extension:
5470
5471    relational-expression:
5472      relational-expression <? shift-expression
5473      relational-expression >? shift-expression
5474
5475    equality-expression:
5476      relational-expression
5477      equality-expression == relational-expression
5478      equality-expression != relational-expression
5479
5480    and-expression:
5481      equality-expression
5482      and-expression & equality-expression
5483
5484    exclusive-or-expression:
5485      and-expression
5486      exclusive-or-expression ^ and-expression
5487
5488    inclusive-or-expression:
5489      exclusive-or-expression
5490      inclusive-or-expression | exclusive-or-expression
5491
5492    logical-and-expression:
5493      inclusive-or-expression
5494      logical-and-expression && inclusive-or-expression
5495
5496    logical-or-expression:
5497      logical-and-expression
5498      logical-or-expression || logical-and-expression
5499
5500    All these are implemented with a single function like:
5501
5502    binary-expression:
5503      simple-cast-expression
5504      binary-expression <token> binary-expression
5505
5506    CAST_P is true if this expression is the target of a cast.
5507
5508    The binops_by_token map is used to get the tree codes for each <token> type.
5509    binary-expressions are associated according to a precedence table.  */
5510
5511 #define TOKEN_PRECEDENCE(token) \
5512   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5513    ? PREC_NOT_OPERATOR \
5514    : binops_by_token[token->type].prec)
5515
5516 static tree
5517 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5518 {
5519   cp_parser_expression_stack stack;
5520   cp_parser_expression_stack_entry *sp = &stack[0];
5521   tree lhs, rhs;
5522   cp_token *token;
5523   enum tree_code tree_type;
5524   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5525   bool overloaded_p;
5526
5527   /* Parse the first expression.  */
5528   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5529
5530   for (;;)
5531     {
5532       /* Get an operator token.  */
5533       token = cp_lexer_peek_token (parser->lexer);
5534       if (token->type == CPP_MIN || token->type == CPP_MAX)
5535         cp_parser_warn_min_max ();
5536
5537       new_prec = TOKEN_PRECEDENCE (token);
5538
5539       /* Popping an entry off the stack means we completed a subexpression:
5540          - either we found a token which is not an operator (`>' where it is not
5541            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5542            will happen repeatedly;
5543          - or, we found an operator which has lower priority.  This is the case
5544            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5545            parsing `3 * 4'.  */
5546       if (new_prec <= prec)
5547         {
5548           if (sp == stack)
5549             break;
5550           else
5551             goto pop;
5552         }
5553
5554      get_rhs:
5555       tree_type = binops_by_token[token->type].tree_type;
5556
5557       /* We used the operator token.  */
5558       cp_lexer_consume_token (parser->lexer);
5559
5560       /* Extract another operand.  It may be the RHS of this expression
5561          or the LHS of a new, higher priority expression.  */
5562       rhs = cp_parser_simple_cast_expression (parser);
5563
5564       /* Get another operator token.  Look up its precedence to avoid
5565          building a useless (immediately popped) stack entry for common
5566          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5567       token = cp_lexer_peek_token (parser->lexer);
5568       lookahead_prec = TOKEN_PRECEDENCE (token);
5569       if (lookahead_prec > new_prec)
5570         {
5571           /* ... and prepare to parse the RHS of the new, higher priority
5572              expression.  Since precedence levels on the stack are
5573              monotonically increasing, we do not have to care about
5574              stack overflows.  */
5575           sp->prec = prec;
5576           sp->tree_type = tree_type;
5577           sp->lhs = lhs;
5578           sp++;
5579           lhs = rhs;
5580           prec = new_prec;
5581           new_prec = lookahead_prec;
5582           goto get_rhs;
5583
5584          pop:
5585           /* If the stack is not empty, we have parsed into LHS the right side
5586              (`4' in the example above) of an expression we had suspended.
5587              We can use the information on the stack to recover the LHS (`3')
5588              from the stack together with the tree code (`MULT_EXPR'), and
5589              the precedence of the higher level subexpression
5590              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5591              which will be used to actually build the additive expression.  */
5592           --sp;
5593           prec = sp->prec;
5594           tree_type = sp->tree_type;
5595           rhs = lhs;
5596           lhs = sp->lhs;
5597         }
5598
5599       overloaded_p = false;
5600       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5601
5602       /* If the binary operator required the use of an overloaded operator,
5603          then this expression cannot be an integral constant-expression.
5604          An overloaded operator can be used even if both operands are
5605          otherwise permissible in an integral constant-expression if at
5606          least one of the operands is of enumeration type.  */
5607
5608       if (overloaded_p
5609           && (cp_parser_non_integral_constant_expression
5610               (parser, "calls to overloaded operators")))
5611         return error_mark_node;
5612     }
5613
5614   return lhs;
5615 }
5616
5617
5618 /* Parse the `? expression : assignment-expression' part of a
5619    conditional-expression.  The LOGICAL_OR_EXPR is the
5620    logical-or-expression that started the conditional-expression.
5621    Returns a representation of the entire conditional-expression.
5622
5623    This routine is used by cp_parser_assignment_expression.
5624
5625      ? expression : assignment-expression
5626
5627    GNU Extensions:
5628
5629      ? : assignment-expression */
5630
5631 static tree
5632 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5633 {
5634   tree expr;
5635   tree assignment_expr;
5636
5637   /* Consume the `?' token.  */
5638   cp_lexer_consume_token (parser->lexer);
5639   if (cp_parser_allow_gnu_extensions_p (parser)
5640       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5641     /* Implicit true clause.  */
5642     expr = NULL_TREE;
5643   else
5644     /* Parse the expression.  */
5645     expr = cp_parser_expression (parser, /*cast_p=*/false);
5646
5647   /* The next token should be a `:'.  */
5648   cp_parser_require (parser, CPP_COLON, "`:'");
5649   /* Parse the assignment-expression.  */
5650   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5651
5652   /* Build the conditional-expression.  */
5653   return build_x_conditional_expr (logical_or_expr,
5654                                    expr,
5655                                    assignment_expr);
5656 }
5657
5658 /* Parse an assignment-expression.
5659
5660    assignment-expression:
5661      conditional-expression
5662      logical-or-expression assignment-operator assignment_expression
5663      throw-expression
5664
5665    CAST_P is true if this expression is the target of a cast.
5666
5667    Returns a representation for the expression.  */
5668
5669 static tree
5670 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5671 {
5672   tree expr;
5673
5674   /* If the next token is the `throw' keyword, then we're looking at
5675      a throw-expression.  */
5676   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5677     expr = cp_parser_throw_expression (parser);
5678   /* Otherwise, it must be that we are looking at a
5679      logical-or-expression.  */
5680   else
5681     {
5682       /* Parse the binary expressions (logical-or-expression).  */
5683       expr = cp_parser_binary_expression (parser, cast_p);
5684       /* If the next token is a `?' then we're actually looking at a
5685          conditional-expression.  */
5686       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5687         return cp_parser_question_colon_clause (parser, expr);
5688       else
5689         {
5690           enum tree_code assignment_operator;
5691
5692           /* If it's an assignment-operator, we're using the second
5693              production.  */
5694           assignment_operator
5695             = cp_parser_assignment_operator_opt (parser);
5696           if (assignment_operator != ERROR_MARK)
5697             {
5698               tree rhs;
5699
5700               /* Parse the right-hand side of the assignment.  */
5701               rhs = cp_parser_assignment_expression (parser, cast_p);
5702               /* An assignment may not appear in a
5703                  constant-expression.  */
5704               if (cp_parser_non_integral_constant_expression (parser,
5705                                                               "an assignment"))
5706                 return error_mark_node;
5707               /* Build the assignment expression.  */
5708               expr = build_x_modify_expr (expr,
5709                                           assignment_operator,
5710                                           rhs);
5711             }
5712         }
5713     }
5714
5715   return expr;
5716 }
5717
5718 /* Parse an (optional) assignment-operator.
5719
5720    assignment-operator: one of
5721      = *= /= %= += -= >>= <<= &= ^= |=
5722
5723    GNU Extension:
5724
5725    assignment-operator: one of
5726      <?= >?=
5727
5728    If the next token is an assignment operator, the corresponding tree
5729    code is returned, and the token is consumed.  For example, for
5730    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5731    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5732    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5733    operator, ERROR_MARK is returned.  */
5734
5735 static enum tree_code
5736 cp_parser_assignment_operator_opt (cp_parser* parser)
5737 {
5738   enum tree_code op;
5739   cp_token *token;
5740
5741   /* Peek at the next toen.  */
5742   token = cp_lexer_peek_token (parser->lexer);
5743
5744   switch (token->type)
5745     {
5746     case CPP_EQ:
5747       op = NOP_EXPR;
5748       break;
5749
5750     case CPP_MULT_EQ:
5751       op = MULT_EXPR;
5752       break;
5753
5754     case CPP_DIV_EQ:
5755       op = TRUNC_DIV_EXPR;
5756       break;
5757
5758     case CPP_MOD_EQ:
5759       op = TRUNC_MOD_EXPR;
5760       break;
5761
5762     case CPP_PLUS_EQ:
5763       op = PLUS_EXPR;
5764       break;
5765
5766     case CPP_MINUS_EQ:
5767       op = MINUS_EXPR;
5768       break;
5769
5770     case CPP_RSHIFT_EQ:
5771       op = RSHIFT_EXPR;
5772       break;
5773
5774     case CPP_LSHIFT_EQ:
5775       op = LSHIFT_EXPR;
5776       break;
5777
5778     case CPP_AND_EQ:
5779       op = BIT_AND_EXPR;
5780       break;
5781
5782     case CPP_XOR_EQ:
5783       op = BIT_XOR_EXPR;
5784       break;
5785
5786     case CPP_OR_EQ:
5787       op = BIT_IOR_EXPR;
5788       break;
5789
5790     case CPP_MIN_EQ:
5791       op = MIN_EXPR;
5792       cp_parser_warn_min_max ();
5793       break;
5794
5795     case CPP_MAX_EQ:
5796       op = MAX_EXPR;
5797       cp_parser_warn_min_max ();
5798       break;
5799
5800     default:
5801       /* Nothing else is an assignment operator.  */
5802       op = ERROR_MARK;
5803     }
5804
5805   /* If it was an assignment operator, consume it.  */
5806   if (op != ERROR_MARK)
5807     cp_lexer_consume_token (parser->lexer);
5808
5809   return op;
5810 }
5811
5812 /* Parse an expression.
5813
5814    expression:
5815      assignment-expression
5816      expression , assignment-expression
5817
5818    CAST_P is true if this expression is the target of a cast.
5819
5820    Returns a representation of the expression.  */
5821
5822 static tree
5823 cp_parser_expression (cp_parser* parser, bool cast_p)
5824 {
5825   tree expression = NULL_TREE;
5826
5827   while (true)
5828     {
5829       tree assignment_expression;
5830
5831       /* Parse the next assignment-expression.  */
5832       assignment_expression
5833         = cp_parser_assignment_expression (parser, cast_p);
5834       /* If this is the first assignment-expression, we can just
5835          save it away.  */
5836       if (!expression)
5837         expression = assignment_expression;
5838       else
5839         expression = build_x_compound_expr (expression,
5840                                             assignment_expression);
5841       /* If the next token is not a comma, then we are done with the
5842          expression.  */
5843       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5844         break;
5845       /* Consume the `,'.  */
5846       cp_lexer_consume_token (parser->lexer);
5847       /* A comma operator cannot appear in a constant-expression.  */
5848       if (cp_parser_non_integral_constant_expression (parser,
5849                                                       "a comma operator"))
5850         expression = error_mark_node;
5851     }
5852
5853   return expression;
5854 }
5855
5856 /* Parse a constant-expression.
5857
5858    constant-expression:
5859      conditional-expression
5860
5861   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5862   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5863   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5864   is false, NON_CONSTANT_P should be NULL.  */
5865
5866 static tree
5867 cp_parser_constant_expression (cp_parser* parser,
5868                                bool allow_non_constant_p,
5869                                bool *non_constant_p)
5870 {
5871   bool saved_integral_constant_expression_p;
5872   bool saved_allow_non_integral_constant_expression_p;
5873   bool saved_non_integral_constant_expression_p;
5874   tree expression;
5875
5876   /* It might seem that we could simply parse the
5877      conditional-expression, and then check to see if it were
5878      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5879      one that the compiler can figure out is constant, possibly after
5880      doing some simplifications or optimizations.  The standard has a
5881      precise definition of constant-expression, and we must honor
5882      that, even though it is somewhat more restrictive.
5883
5884      For example:
5885
5886        int i[(2, 3)];
5887
5888      is not a legal declaration, because `(2, 3)' is not a
5889      constant-expression.  The `,' operator is forbidden in a
5890      constant-expression.  However, GCC's constant-folding machinery
5891      will fold this operation to an INTEGER_CST for `3'.  */
5892
5893   /* Save the old settings.  */
5894   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5895   saved_allow_non_integral_constant_expression_p
5896     = parser->allow_non_integral_constant_expression_p;
5897   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5898   /* We are now parsing a constant-expression.  */
5899   parser->integral_constant_expression_p = true;
5900   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5901   parser->non_integral_constant_expression_p = false;
5902   /* Although the grammar says "conditional-expression", we parse an
5903      "assignment-expression", which also permits "throw-expression"
5904      and the use of assignment operators.  In the case that
5905      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5906      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5907      actually essential that we look for an assignment-expression.
5908      For example, cp_parser_initializer_clauses uses this function to
5909      determine whether a particular assignment-expression is in fact
5910      constant.  */
5911   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5912   /* Restore the old settings.  */
5913   parser->integral_constant_expression_p
5914     = saved_integral_constant_expression_p;
5915   parser->allow_non_integral_constant_expression_p
5916     = saved_allow_non_integral_constant_expression_p;
5917   if (allow_non_constant_p)
5918     *non_constant_p = parser->non_integral_constant_expression_p;
5919   else if (parser->non_integral_constant_expression_p)
5920     expression = error_mark_node;
5921   parser->non_integral_constant_expression_p
5922     = saved_non_integral_constant_expression_p;
5923
5924   return expression;
5925 }
5926
5927 /* Parse __builtin_offsetof.
5928
5929    offsetof-expression:
5930      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5931
5932    offsetof-member-designator:
5933      id-expression
5934      | offsetof-member-designator "." id-expression
5935      | offsetof-member-designator "[" expression "]"
5936 */
5937
5938 static tree
5939 cp_parser_builtin_offsetof (cp_parser *parser)
5940 {
5941   int save_ice_p, save_non_ice_p;
5942   tree type, expr;
5943   cp_id_kind dummy;
5944
5945   /* We're about to accept non-integral-constant things, but will
5946      definitely yield an integral constant expression.  Save and
5947      restore these values around our local parsing.  */
5948   save_ice_p = parser->integral_constant_expression_p;
5949   save_non_ice_p = parser->non_integral_constant_expression_p;
5950
5951   /* Consume the "__builtin_offsetof" token.  */
5952   cp_lexer_consume_token (parser->lexer);
5953   /* Consume the opening `('.  */
5954   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5955   /* Parse the type-id.  */
5956   type = cp_parser_type_id (parser);
5957   /* Look for the `,'.  */
5958   cp_parser_require (parser, CPP_COMMA, "`,'");
5959
5960   /* Build the (type *)null that begins the traditional offsetof macro.  */
5961   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5962
5963   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5964   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5965                                                  true, &dummy);
5966   while (true)
5967     {
5968       cp_token *token = cp_lexer_peek_token (parser->lexer);
5969       switch (token->type)
5970         {
5971         case CPP_OPEN_SQUARE:
5972           /* offsetof-member-designator "[" expression "]" */
5973           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5974           break;
5975
5976         case CPP_DOT:
5977           /* offsetof-member-designator "." identifier */
5978           cp_lexer_consume_token (parser->lexer);
5979           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5980                                                          true, &dummy);
5981           break;
5982
5983         case CPP_CLOSE_PAREN:
5984           /* Consume the ")" token.  */
5985           cp_lexer_consume_token (parser->lexer);
5986           goto success;
5987
5988         default:
5989           /* Error.  We know the following require will fail, but
5990              that gives the proper error message.  */
5991           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5992           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5993           expr = error_mark_node;
5994           goto failure;
5995         }
5996     }
5997
5998  success:
5999   /* If we're processing a template, we can't finish the semantics yet.
6000      Otherwise we can fold the entire expression now.  */
6001   if (processing_template_decl)
6002     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6003   else
6004     expr = fold_offsetof (expr);
6005
6006  failure:
6007   parser->integral_constant_expression_p = save_ice_p;
6008   parser->non_integral_constant_expression_p = save_non_ice_p;
6009
6010   return expr;
6011 }
6012
6013 /* Statements [gram.stmt.stmt]  */
6014
6015 /* Parse a statement.
6016
6017    statement:
6018      labeled-statement
6019      expression-statement
6020      compound-statement
6021      selection-statement
6022      iteration-statement
6023      jump-statement
6024      declaration-statement
6025      try-block
6026
6027   IN_COMPOUND is true when the statement is nested inside a 
6028   cp_parser_compound_statement; this matters for certain pragmas.  */
6029
6030 static void
6031 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6032                      bool in_compound)
6033 {
6034   tree statement;
6035   cp_token *token;
6036   location_t statement_location;
6037
6038  restart:
6039   /* There is no statement yet.  */
6040   statement = NULL_TREE;
6041   /* Peek at the next token.  */
6042   token = cp_lexer_peek_token (parser->lexer);
6043   /* Remember the location of the first token in the statement.  */
6044   statement_location = token->location;
6045   /* If this is a keyword, then that will often determine what kind of
6046      statement we have.  */
6047   if (token->type == CPP_KEYWORD)
6048     {
6049       enum rid keyword = token->keyword;
6050
6051       switch (keyword)
6052         {
6053         case RID_CASE:
6054         case RID_DEFAULT:
6055           statement = cp_parser_labeled_statement (parser, in_statement_expr,
6056                                                    in_compound);
6057           break;
6058
6059         case RID_IF:
6060         case RID_SWITCH:
6061           statement = cp_parser_selection_statement (parser);
6062           break;
6063
6064         case RID_WHILE:
6065         case RID_DO:
6066         case RID_FOR:
6067           statement = cp_parser_iteration_statement (parser);
6068           break;
6069
6070         case RID_BREAK:
6071         case RID_CONTINUE:
6072         case RID_RETURN:
6073         case RID_GOTO:
6074           statement = cp_parser_jump_statement (parser);
6075           break;
6076
6077           /* Objective-C++ exception-handling constructs.  */
6078         case RID_AT_TRY:
6079         case RID_AT_CATCH:
6080         case RID_AT_FINALLY:
6081         case RID_AT_SYNCHRONIZED:
6082         case RID_AT_THROW:
6083           statement = cp_parser_objc_statement (parser);
6084           break;
6085
6086         case RID_TRY:
6087           statement = cp_parser_try_block (parser);
6088           break;
6089
6090         default:
6091           /* It might be a keyword like `int' that can start a
6092              declaration-statement.  */
6093           break;
6094         }
6095     }
6096   else if (token->type == CPP_NAME)
6097     {
6098       /* If the next token is a `:', then we are looking at a
6099          labeled-statement.  */
6100       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6101       if (token->type == CPP_COLON)
6102         statement = cp_parser_labeled_statement (parser, in_statement_expr,
6103                                                  in_compound);
6104     }
6105   /* Anything that starts with a `{' must be a compound-statement.  */
6106   else if (token->type == CPP_OPEN_BRACE)
6107     statement = cp_parser_compound_statement (parser, NULL, false);
6108   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6109      a statement all its own.  */
6110   else if (token->type == CPP_PRAGMA)
6111     {
6112       /* Only certain OpenMP pragmas are attached to statements, and thus
6113          are considered statements themselves.  All others are not.  In
6114          the context of a compound, accept the pragma as a "statement" and
6115          return so that we can check for a close brace.  Otherwise we 
6116          require a real statement and must go back and read one.  */
6117       if (in_compound)
6118         cp_parser_pragma (parser, pragma_compound);
6119       else if (!cp_parser_pragma (parser, pragma_stmt))
6120         goto restart;
6121       return;
6122     }
6123   else if (token->type == CPP_EOF)
6124     {
6125       cp_parser_error (parser, "expected statement");
6126       return;
6127     }
6128
6129   /* Everything else must be a declaration-statement or an
6130      expression-statement.  Try for the declaration-statement
6131      first, unless we are looking at a `;', in which case we know that
6132      we have an expression-statement.  */
6133   if (!statement)
6134     {
6135       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6136         {
6137           cp_parser_parse_tentatively (parser);
6138           /* Try to parse the declaration-statement.  */
6139           cp_parser_declaration_statement (parser);
6140           /* If that worked, we're done.  */
6141           if (cp_parser_parse_definitely (parser))
6142             return;
6143         }
6144       /* Look for an expression-statement instead.  */
6145       statement = cp_parser_expression_statement (parser, in_statement_expr);
6146     }
6147
6148   /* Set the line number for the statement.  */
6149   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6150     SET_EXPR_LOCATION (statement, statement_location);
6151 }
6152
6153 /* Parse a labeled-statement.
6154
6155    labeled-statement:
6156      identifier : statement
6157      case constant-expression : statement
6158      default : statement
6159
6160    GNU Extension:
6161
6162    labeled-statement:
6163      case constant-expression ... constant-expression : statement
6164
6165    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6166    For an ordinary label, returns a LABEL_EXPR.
6167
6168    IN_COMPOUND is as for cp_parser_statement: true when we're nested
6169    inside a compound.  */
6170
6171 static tree
6172 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6173                              bool in_compound)
6174 {
6175   cp_token *token;
6176   tree statement = error_mark_node;
6177
6178   /* The next token should be an identifier.  */
6179   token = cp_lexer_peek_token (parser->lexer);
6180   if (token->type != CPP_NAME
6181       && token->type != CPP_KEYWORD)
6182     {
6183       cp_parser_error (parser, "expected labeled-statement");
6184       return error_mark_node;
6185     }
6186
6187   switch (token->keyword)
6188     {
6189     case RID_CASE:
6190       {
6191         tree expr, expr_hi;
6192         cp_token *ellipsis;
6193
6194         /* Consume the `case' token.  */
6195         cp_lexer_consume_token (parser->lexer);
6196         /* Parse the constant-expression.  */
6197         expr = cp_parser_constant_expression (parser,
6198                                               /*allow_non_constant_p=*/false,
6199                                               NULL);
6200
6201         ellipsis = cp_lexer_peek_token (parser->lexer);
6202         if (ellipsis->type == CPP_ELLIPSIS)
6203           {
6204             /* Consume the `...' token.  */
6205             cp_lexer_consume_token (parser->lexer);
6206             expr_hi =
6207               cp_parser_constant_expression (parser,
6208                                              /*allow_non_constant_p=*/false,
6209                                              NULL);
6210             /* We don't need to emit warnings here, as the common code
6211                will do this for us.  */
6212           }
6213         else
6214           expr_hi = NULL_TREE;
6215
6216         if (parser->in_switch_statement_p)
6217           statement = finish_case_label (expr, expr_hi);
6218         else
6219           error ("case label %qE not within a switch statement", expr);
6220       }
6221       break;
6222
6223     case RID_DEFAULT:
6224       /* Consume the `default' token.  */
6225       cp_lexer_consume_token (parser->lexer);
6226
6227       if (parser->in_switch_statement_p)
6228         statement = finish_case_label (NULL_TREE, NULL_TREE);
6229       else
6230         error ("case label not within a switch statement");
6231       break;
6232
6233     default:
6234       /* Anything else must be an ordinary label.  */
6235       statement = finish_label_stmt (cp_parser_identifier (parser));
6236       break;
6237     }
6238
6239   /* Require the `:' token.  */
6240   cp_parser_require (parser, CPP_COLON, "`:'");
6241   /* Parse the labeled statement.  */
6242   cp_parser_statement (parser, in_statement_expr, in_compound);
6243
6244   /* Return the label, in the case of a `case' or `default' label.  */
6245   return statement;
6246 }
6247
6248 /* Parse an expression-statement.
6249
6250    expression-statement:
6251      expression [opt] ;
6252
6253    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6254    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6255    indicates whether this expression-statement is part of an
6256    expression statement.  */
6257
6258 static tree
6259 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6260 {
6261   tree statement = NULL_TREE;
6262
6263   /* If the next token is a ';', then there is no expression
6264      statement.  */
6265   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6266     statement = cp_parser_expression (parser, /*cast_p=*/false);
6267
6268   /* Consume the final `;'.  */
6269   cp_parser_consume_semicolon_at_end_of_statement (parser);
6270
6271   if (in_statement_expr
6272       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6273     /* This is the final expression statement of a statement
6274        expression.  */
6275     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6276   else if (statement)
6277     statement = finish_expr_stmt (statement);
6278   else
6279     finish_stmt ();
6280
6281   return statement;
6282 }
6283
6284 /* Parse a compound-statement.
6285
6286    compound-statement:
6287      { statement-seq [opt] }
6288
6289    Returns a tree representing the statement.  */
6290
6291 static tree
6292 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6293                               bool in_try)
6294 {
6295   tree compound_stmt;
6296
6297   /* Consume the `{'.  */
6298   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6299     return error_mark_node;
6300   /* Begin the compound-statement.  */
6301   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6302   /* Parse an (optional) statement-seq.  */
6303   cp_parser_statement_seq_opt (parser, in_statement_expr);
6304   /* Finish the compound-statement.  */
6305   finish_compound_stmt (compound_stmt);
6306   /* Consume the `}'.  */
6307   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6308
6309   return compound_stmt;
6310 }
6311
6312 /* Parse an (optional) statement-seq.
6313
6314    statement-seq:
6315      statement
6316      statement-seq [opt] statement  */
6317
6318 static void
6319 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6320 {
6321   /* Scan statements until there aren't any more.  */
6322   while (true)
6323     {
6324       cp_token *token = cp_lexer_peek_token (parser->lexer);
6325
6326       /* If we're looking at a `}', then we've run out of statements.  */
6327       if (token->type == CPP_CLOSE_BRACE
6328           || token->type == CPP_EOF
6329           || token->type == CPP_PRAGMA_EOL)
6330         break;
6331
6332       /* Parse the statement.  */
6333       cp_parser_statement (parser, in_statement_expr, true);
6334     }
6335 }
6336
6337 /* Parse a selection-statement.
6338
6339    selection-statement:
6340      if ( condition ) statement
6341      if ( condition ) statement else statement
6342      switch ( condition ) statement
6343
6344    Returns the new IF_STMT or SWITCH_STMT.  */
6345
6346 static tree
6347 cp_parser_selection_statement (cp_parser* parser)
6348 {
6349   cp_token *token;
6350   enum rid keyword;
6351
6352   /* Peek at the next token.  */
6353   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6354
6355   /* See what kind of keyword it is.  */
6356   keyword = token->keyword;
6357   switch (keyword)
6358     {
6359     case RID_IF:
6360     case RID_SWITCH:
6361       {
6362         tree statement;
6363         tree condition;
6364
6365         /* Look for the `('.  */
6366         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6367           {
6368             cp_parser_skip_to_end_of_statement (parser);
6369             return error_mark_node;
6370           }
6371
6372         /* Begin the selection-statement.  */
6373         if (keyword == RID_IF)
6374           statement = begin_if_stmt ();
6375         else
6376           statement = begin_switch_stmt ();
6377
6378         /* Parse the condition.  */
6379         condition = cp_parser_condition (parser);
6380         /* Look for the `)'.  */
6381         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6382           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6383                                                  /*consume_paren=*/true);
6384
6385         if (keyword == RID_IF)
6386           {
6387             /* Add the condition.  */
6388             finish_if_stmt_cond (condition, statement);
6389
6390             /* Parse the then-clause.  */
6391             cp_parser_implicitly_scoped_statement (parser);
6392             finish_then_clause (statement);
6393
6394             /* If the next token is `else', parse the else-clause.  */
6395             if (cp_lexer_next_token_is_keyword (parser->lexer,
6396                                                 RID_ELSE))
6397               {
6398                 /* Consume the `else' keyword.  */
6399                 cp_lexer_consume_token (parser->lexer);
6400                 begin_else_clause (statement);
6401                 /* Parse the else-clause.  */
6402                 cp_parser_implicitly_scoped_statement (parser);
6403                 finish_else_clause (statement);
6404               }
6405
6406             /* Now we're all done with the if-statement.  */
6407             finish_if_stmt (statement);
6408           }
6409         else
6410           {
6411             bool in_switch_statement_p;
6412
6413             /* Add the condition.  */
6414             finish_switch_cond (condition, statement);
6415
6416             /* Parse the body of the switch-statement.  */
6417             in_switch_statement_p = parser->in_switch_statement_p;
6418             parser->in_switch_statement_p = true;
6419             cp_parser_implicitly_scoped_statement (parser);
6420             parser->in_switch_statement_p = in_switch_statement_p;
6421
6422             /* Now we're all done with the switch-statement.  */
6423             finish_switch_stmt (statement);
6424           }
6425
6426         return statement;
6427       }
6428       break;
6429
6430     default:
6431       cp_parser_error (parser, "expected selection-statement");
6432       return error_mark_node;
6433     }
6434 }
6435
6436 /* Parse a condition.
6437
6438    condition:
6439      expression
6440      type-specifier-seq declarator = assignment-expression
6441
6442    GNU Extension:
6443
6444    condition:
6445      type-specifier-seq declarator asm-specification [opt]
6446        attributes [opt] = assignment-expression
6447
6448    Returns the expression that should be tested.  */
6449
6450 static tree
6451 cp_parser_condition (cp_parser* parser)
6452 {
6453   cp_decl_specifier_seq type_specifiers;
6454   const char *saved_message;
6455
6456   /* Try the declaration first.  */
6457   cp_parser_parse_tentatively (parser);
6458   /* New types are not allowed in the type-specifier-seq for a
6459      condition.  */
6460   saved_message = parser->type_definition_forbidden_message;
6461   parser->type_definition_forbidden_message
6462     = "types may not be defined in conditions";
6463   /* Parse the type-specifier-seq.  */
6464   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6465                                 &type_specifiers);
6466   /* Restore the saved message.  */
6467   parser->type_definition_forbidden_message = saved_message;
6468   /* If all is well, we might be looking at a declaration.  */
6469   if (!cp_parser_error_occurred (parser))
6470     {
6471       tree decl;
6472       tree asm_specification;
6473       tree attributes;
6474       cp_declarator *declarator;
6475       tree initializer = NULL_TREE;
6476
6477       /* Parse the declarator.  */
6478       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6479                                          /*ctor_dtor_or_conv_p=*/NULL,
6480                                          /*parenthesized_p=*/NULL,
6481                                          /*member_p=*/false);
6482       /* Parse the attributes.  */
6483       attributes = cp_parser_attributes_opt (parser);
6484       /* Parse the asm-specification.  */
6485       asm_specification = cp_parser_asm_specification_opt (parser);
6486       /* If the next token is not an `=', then we might still be
6487          looking at an expression.  For example:
6488
6489            if (A(a).x)
6490
6491          looks like a decl-specifier-seq and a declarator -- but then
6492          there is no `=', so this is an expression.  */
6493       cp_parser_require (parser, CPP_EQ, "`='");
6494       /* If we did see an `=', then we are looking at a declaration
6495          for sure.  */
6496       if (cp_parser_parse_definitely (parser))
6497         {
6498           tree pushed_scope;
6499
6500           /* Create the declaration.  */
6501           decl = start_decl (declarator, &type_specifiers,
6502                              /*initialized_p=*/true,
6503                              attributes, /*prefix_attributes=*/NULL_TREE,
6504                              &pushed_scope);
6505           /* Parse the assignment-expression.  */
6506           initializer = cp_parser_assignment_expression (parser,
6507                                                          /*cast_p=*/false);
6508
6509           /* Process the initializer.  */
6510           cp_finish_decl (decl,
6511                           initializer,
6512                           asm_specification,
6513                           LOOKUP_ONLYCONVERTING);
6514
6515           if (pushed_scope)
6516             pop_scope (pushed_scope);
6517
6518           return convert_from_reference (decl);
6519         }
6520     }
6521   /* If we didn't even get past the declarator successfully, we are
6522      definitely not looking at a declaration.  */
6523   else
6524     cp_parser_abort_tentative_parse (parser);
6525
6526   /* Otherwise, we are looking at an expression.  */
6527   return cp_parser_expression (parser, /*cast_p=*/false);
6528 }
6529
6530 /* Parse an iteration-statement.
6531
6532    iteration-statement:
6533      while ( condition ) statement
6534      do statement while ( expression ) ;
6535      for ( for-init-statement condition [opt] ; expression [opt] )
6536        statement
6537
6538    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6539
6540 static tree
6541 cp_parser_iteration_statement (cp_parser* parser)
6542 {
6543   cp_token *token;
6544   enum rid keyword;
6545   tree statement;
6546   bool in_iteration_statement_p;
6547
6548
6549   /* Peek at the next token.  */
6550   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6551   if (!token)
6552     return error_mark_node;
6553
6554   /* Remember whether or not we are already within an iteration
6555      statement.  */
6556   in_iteration_statement_p = parser->in_iteration_statement_p;
6557
6558   /* See what kind of keyword it is.  */
6559   keyword = token->keyword;
6560   switch (keyword)
6561     {
6562     case RID_WHILE:
6563       {
6564         tree condition;
6565
6566         /* Begin the while-statement.  */
6567         statement = begin_while_stmt ();
6568         /* Look for the `('.  */
6569         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6570         /* Parse the condition.  */
6571         condition = cp_parser_condition (parser);
6572         finish_while_stmt_cond (condition, statement);
6573         /* Look for the `)'.  */
6574         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6575         /* Parse the dependent statement.  */
6576         parser->in_iteration_statement_p = true;
6577         cp_parser_already_scoped_statement (parser);
6578         parser->in_iteration_statement_p = in_iteration_statement_p;
6579         /* We're done with the while-statement.  */
6580         finish_while_stmt (statement);
6581       }
6582       break;
6583
6584     case RID_DO:
6585       {
6586         tree expression;
6587
6588         /* Begin the do-statement.  */
6589         statement = begin_do_stmt ();
6590         /* Parse the body of the do-statement.  */
6591         parser->in_iteration_statement_p = true;
6592         cp_parser_implicitly_scoped_statement (parser);
6593         parser->in_iteration_statement_p = in_iteration_statement_p;
6594         finish_do_body (statement);
6595         /* Look for the `while' keyword.  */
6596         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6597         /* Look for the `('.  */
6598         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6599         /* Parse the expression.  */
6600         expression = cp_parser_expression (parser, /*cast_p=*/false);
6601         /* We're done with the do-statement.  */
6602         finish_do_stmt (expression, statement);
6603         /* Look for the `)'.  */
6604         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6605         /* Look for the `;'.  */
6606         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6607       }
6608       break;
6609
6610     case RID_FOR:
6611       {
6612         tree condition = NULL_TREE;
6613         tree expression = NULL_TREE;
6614
6615         /* Begin the for-statement.  */
6616         statement = begin_for_stmt ();
6617         /* Look for the `('.  */
6618         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6619         /* Parse the initialization.  */
6620         cp_parser_for_init_statement (parser);
6621         finish_for_init_stmt (statement);
6622
6623         /* If there's a condition, process it.  */
6624         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6625           condition = cp_parser_condition (parser);
6626         finish_for_cond (condition, statement);
6627         /* Look for the `;'.  */
6628         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6629
6630         /* If there's an expression, process it.  */
6631         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6632           expression = cp_parser_expression (parser, /*cast_p=*/false);
6633         finish_for_expr (expression, statement);
6634         /* Look for the `)'.  */
6635         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6636
6637         /* Parse the body of the for-statement.  */
6638         parser->in_iteration_statement_p = true;
6639         cp_parser_already_scoped_statement (parser);
6640         parser->in_iteration_statement_p = in_iteration_statement_p;
6641
6642         /* We're done with the for-statement.  */
6643         finish_for_stmt (statement);
6644       }
6645       break;
6646
6647     default:
6648       cp_parser_error (parser, "expected iteration-statement");
6649       statement = error_mark_node;
6650       break;
6651     }
6652
6653   return statement;
6654 }
6655
6656 /* Parse a for-init-statement.
6657
6658    for-init-statement:
6659      expression-statement
6660      simple-declaration  */
6661
6662 static void
6663 cp_parser_for_init_statement (cp_parser* parser)
6664 {
6665   /* If the next token is a `;', then we have an empty
6666      expression-statement.  Grammatically, this is also a
6667      simple-declaration, but an invalid one, because it does not
6668      declare anything.  Therefore, if we did not handle this case
6669      specially, we would issue an error message about an invalid
6670      declaration.  */
6671   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6672     {
6673       /* We're going to speculatively look for a declaration, falling back
6674          to an expression, if necessary.  */
6675       cp_parser_parse_tentatively (parser);
6676       /* Parse the declaration.  */
6677       cp_parser_simple_declaration (parser,
6678                                     /*function_definition_allowed_p=*/false);
6679       /* If the tentative parse failed, then we shall need to look for an
6680          expression-statement.  */
6681       if (cp_parser_parse_definitely (parser))
6682         return;
6683     }
6684
6685   cp_parser_expression_statement (parser, false);
6686 }
6687
6688 /* Parse a jump-statement.
6689
6690    jump-statement:
6691      break ;
6692      continue ;
6693      return expression [opt] ;
6694      goto identifier ;
6695
6696    GNU extension:
6697
6698    jump-statement:
6699      goto * expression ;
6700
6701    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6702
6703 static tree
6704 cp_parser_jump_statement (cp_parser* parser)
6705 {
6706   tree statement = error_mark_node;
6707   cp_token *token;
6708   enum rid keyword;
6709
6710   /* Peek at the next token.  */
6711   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6712   if (!token)
6713     return error_mark_node;
6714
6715   /* See what kind of keyword it is.  */
6716   keyword = token->keyword;
6717   switch (keyword)
6718     {
6719     case RID_BREAK:
6720       if (!parser->in_switch_statement_p
6721           && !parser->in_iteration_statement_p)
6722         {
6723           error ("break statement not within loop or switch");
6724           statement = error_mark_node;
6725         }
6726       else
6727         statement = finish_break_stmt ();
6728       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6729       break;
6730
6731     case RID_CONTINUE:
6732       if (!parser->in_iteration_statement_p)
6733         {
6734           error ("continue statement not within a loop");
6735           statement = error_mark_node;
6736         }
6737       else
6738         statement = finish_continue_stmt ();
6739       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6740       break;
6741
6742     case RID_RETURN:
6743       {
6744         tree expr;
6745
6746         /* If the next token is a `;', then there is no
6747            expression.  */
6748         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6749           expr = cp_parser_expression (parser, /*cast_p=*/false);
6750         else
6751           expr = NULL_TREE;
6752         /* Build the return-statement.  */
6753         statement = finish_return_stmt (expr);
6754         /* Look for the final `;'.  */
6755         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6756       }
6757       break;
6758
6759     case RID_GOTO:
6760       /* Create the goto-statement.  */
6761       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6762         {
6763           /* Issue a warning about this use of a GNU extension.  */
6764           if (pedantic)
6765             pedwarn ("ISO C++ forbids computed gotos");
6766           /* Consume the '*' token.  */
6767           cp_lexer_consume_token (parser->lexer);
6768           /* Parse the dependent expression.  */
6769           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6770         }
6771       else
6772         finish_goto_stmt (cp_parser_identifier (parser));
6773       /* Look for the final `;'.  */
6774       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6775       break;
6776
6777     default:
6778       cp_parser_error (parser, "expected jump-statement");
6779       break;
6780     }
6781
6782   return statement;
6783 }
6784
6785 /* Parse a declaration-statement.
6786
6787    declaration-statement:
6788      block-declaration  */
6789
6790 static void
6791 cp_parser_declaration_statement (cp_parser* parser)
6792 {
6793   void *p;
6794
6795   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6796   p = obstack_alloc (&declarator_obstack, 0);
6797
6798  /* Parse the block-declaration.  */
6799   cp_parser_block_declaration (parser, /*statement_p=*/true);
6800
6801   /* Free any declarators allocated.  */
6802   obstack_free (&declarator_obstack, p);
6803
6804   /* Finish off the statement.  */
6805   finish_stmt ();
6806 }
6807
6808 /* Some dependent statements (like `if (cond) statement'), are
6809    implicitly in their own scope.  In other words, if the statement is
6810    a single statement (as opposed to a compound-statement), it is
6811    none-the-less treated as if it were enclosed in braces.  Any
6812    declarations appearing in the dependent statement are out of scope
6813    after control passes that point.  This function parses a statement,
6814    but ensures that is in its own scope, even if it is not a
6815    compound-statement.
6816
6817    Returns the new statement.  */
6818
6819 static tree
6820 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6821 {
6822   tree statement;
6823
6824   /* If the token is not a `{', then we must take special action.  */
6825   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6826     {
6827       /* Create a compound-statement.  */
6828       statement = begin_compound_stmt (0);
6829       /* Parse the dependent-statement.  */
6830       cp_parser_statement (parser, NULL_TREE, false);
6831       /* Finish the dummy compound-statement.  */
6832       finish_compound_stmt (statement);
6833     }
6834   /* Otherwise, we simply parse the statement directly.  */
6835   else
6836     statement = cp_parser_compound_statement (parser, NULL, false);
6837
6838   /* Return the statement.  */
6839   return statement;
6840 }
6841
6842 /* For some dependent statements (like `while (cond) statement'), we
6843    have already created a scope.  Therefore, even if the dependent
6844    statement is a compound-statement, we do not want to create another
6845    scope.  */
6846
6847 static void
6848 cp_parser_already_scoped_statement (cp_parser* parser)
6849 {
6850   /* If the token is a `{', then we must take special action.  */
6851   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6852     cp_parser_statement (parser, NULL_TREE, false);
6853   else
6854     {
6855       /* Avoid calling cp_parser_compound_statement, so that we
6856          don't create a new scope.  Do everything else by hand.  */
6857       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6858       cp_parser_statement_seq_opt (parser, NULL_TREE);
6859       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6860     }
6861 }
6862
6863 /* Declarations [gram.dcl.dcl] */
6864
6865 /* Parse an optional declaration-sequence.
6866
6867    declaration-seq:
6868      declaration
6869      declaration-seq declaration  */
6870
6871 static void
6872 cp_parser_declaration_seq_opt (cp_parser* parser)
6873 {
6874   while (true)
6875     {
6876       cp_token *token;
6877
6878       token = cp_lexer_peek_token (parser->lexer);
6879
6880       if (token->type == CPP_CLOSE_BRACE
6881           || token->type == CPP_EOF
6882           || token->type == CPP_PRAGMA_EOL)
6883         break;
6884
6885       if (token->type == CPP_SEMICOLON)
6886         {
6887           /* A declaration consisting of a single semicolon is
6888              invalid.  Allow it unless we're being pedantic.  */
6889           cp_lexer_consume_token (parser->lexer);
6890           if (pedantic && !in_system_header)
6891             pedwarn ("extra %<;%>");
6892           continue;
6893         }
6894
6895       /* If we're entering or exiting a region that's implicitly
6896          extern "C", modify the lang context appropriately.  */
6897       if (!parser->implicit_extern_c && token->implicit_extern_c)
6898         {
6899           push_lang_context (lang_name_c);
6900           parser->implicit_extern_c = true;
6901         }
6902       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6903         {
6904           pop_lang_context ();
6905           parser->implicit_extern_c = false;
6906         }
6907
6908       if (token->type == CPP_PRAGMA)
6909         {
6910           /* A top-level declaration can consist solely of a #pragma.
6911              A nested declaration cannot, so this is done here and not
6912              in cp_parser_declaration.  (A #pragma at block scope is
6913              handled in cp_parser_statement.)  */
6914           cp_parser_pragma (parser, pragma_external);
6915           continue;
6916         }
6917
6918       /* Parse the declaration itself.  */
6919       cp_parser_declaration (parser);
6920     }
6921 }
6922
6923 /* Parse a declaration.
6924
6925    declaration:
6926      block-declaration
6927      function-definition
6928      template-declaration
6929      explicit-instantiation
6930      explicit-specialization
6931      linkage-specification
6932      namespace-definition
6933
6934    GNU extension:
6935
6936    declaration:
6937       __extension__ declaration */
6938
6939 static void
6940 cp_parser_declaration (cp_parser* parser)
6941 {
6942   cp_token token1;
6943   cp_token token2;
6944   int saved_pedantic;
6945   void *p;
6946
6947   /* Check for the `__extension__' keyword.  */
6948   if (cp_parser_extension_opt (parser, &saved_pedantic))
6949     {
6950       /* Parse the qualified declaration.  */
6951       cp_parser_declaration (parser);
6952       /* Restore the PEDANTIC flag.  */
6953       pedantic = saved_pedantic;
6954
6955       return;
6956     }
6957
6958   /* Try to figure out what kind of declaration is present.  */
6959   token1 = *cp_lexer_peek_token (parser->lexer);
6960
6961   if (token1.type != CPP_EOF)
6962     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6963   else
6964     {
6965       token2.type = CPP_EOF;
6966       token2.keyword = RID_MAX;
6967     }
6968
6969   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6970   p = obstack_alloc (&declarator_obstack, 0);
6971
6972   /* If the next token is `extern' and the following token is a string
6973      literal, then we have a linkage specification.  */
6974   if (token1.keyword == RID_EXTERN
6975       && cp_parser_is_string_literal (&token2))
6976     cp_parser_linkage_specification (parser);
6977   /* If the next token is `template', then we have either a template
6978      declaration, an explicit instantiation, or an explicit
6979      specialization.  */
6980   else if (token1.keyword == RID_TEMPLATE)
6981     {
6982       /* `template <>' indicates a template specialization.  */
6983       if (token2.type == CPP_LESS
6984           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6985         cp_parser_explicit_specialization (parser);
6986       /* `template <' indicates a template declaration.  */
6987       else if (token2.type == CPP_LESS)
6988         cp_parser_template_declaration (parser, /*member_p=*/false);
6989       /* Anything else must be an explicit instantiation.  */
6990       else
6991         cp_parser_explicit_instantiation (parser);
6992     }
6993   /* If the next token is `export', then we have a template
6994      declaration.  */
6995   else if (token1.keyword == RID_EXPORT)
6996     cp_parser_template_declaration (parser, /*member_p=*/false);
6997   /* If the next token is `extern', 'static' or 'inline' and the one
6998      after that is `template', we have a GNU extended explicit
6999      instantiation directive.  */
7000   else if (cp_parser_allow_gnu_extensions_p (parser)
7001            && (token1.keyword == RID_EXTERN
7002                || token1.keyword == RID_STATIC
7003                || token1.keyword == RID_INLINE)
7004            && token2.keyword == RID_TEMPLATE)
7005     cp_parser_explicit_instantiation (parser);
7006   /* If the next token is `namespace', check for a named or unnamed
7007      namespace definition.  */
7008   else if (token1.keyword == RID_NAMESPACE
7009            && (/* A named namespace definition.  */
7010                (token2.type == CPP_NAME
7011                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7012                     == CPP_OPEN_BRACE))
7013                /* An unnamed namespace definition.  */
7014                || token2.type == CPP_OPEN_BRACE))
7015     cp_parser_namespace_definition (parser);
7016   /* Objective-C++ declaration/definition.  */
7017   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7018     cp_parser_objc_declaration (parser);
7019   /* We must have either a block declaration or a function
7020      definition.  */
7021   else
7022     /* Try to parse a block-declaration, or a function-definition.  */
7023     cp_parser_block_declaration (parser, /*statement_p=*/false);
7024
7025   /* Free any declarators allocated.  */
7026   obstack_free (&declarator_obstack, p);
7027 }
7028
7029 /* Parse a block-declaration.
7030
7031    block-declaration:
7032      simple-declaration
7033      asm-definition
7034      namespace-alias-definition
7035      using-declaration
7036      using-directive
7037
7038    GNU Extension:
7039
7040    block-declaration:
7041      __extension__ block-declaration
7042      label-declaration
7043
7044    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7045    part of a declaration-statement.  */
7046
7047 static void
7048 cp_parser_block_declaration (cp_parser *parser,
7049                              bool      statement_p)
7050 {
7051   cp_token *token1;
7052   int saved_pedantic;
7053
7054   /* Check for the `__extension__' keyword.  */
7055   if (cp_parser_extension_opt (parser, &saved_pedantic))
7056     {
7057       /* Parse the qualified declaration.  */
7058       cp_parser_block_declaration (parser, statement_p);
7059       /* Restore the PEDANTIC flag.  */
7060       pedantic = saved_pedantic;
7061
7062       return;
7063     }
7064
7065   /* Peek at the next token to figure out which kind of declaration is
7066      present.  */
7067   token1 = cp_lexer_peek_token (parser->lexer);
7068
7069   /* If the next keyword is `asm', we have an asm-definition.  */
7070   if (token1->keyword == RID_ASM)
7071     {
7072       if (statement_p)
7073         cp_parser_commit_to_tentative_parse (parser);
7074       cp_parser_asm_definition (parser);
7075     }
7076   /* If the next keyword is `namespace', we have a
7077      namespace-alias-definition.  */
7078   else if (token1->keyword == RID_NAMESPACE)
7079     cp_parser_namespace_alias_definition (parser);
7080   /* If the next keyword is `using', we have either a
7081      using-declaration or a using-directive.  */
7082   else if (token1->keyword == RID_USING)
7083     {
7084       cp_token *token2;
7085
7086       if (statement_p)
7087         cp_parser_commit_to_tentative_parse (parser);
7088       /* If the token after `using' is `namespace', then we have a
7089          using-directive.  */
7090       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7091       if (token2->keyword == RID_NAMESPACE)
7092         cp_parser_using_directive (parser);
7093       /* Otherwise, it's a using-declaration.  */
7094       else
7095         cp_parser_using_declaration (parser);
7096     }
7097   /* If the next keyword is `__label__' we have a label declaration.  */
7098   else if (token1->keyword == RID_LABEL)
7099     {
7100       if (statement_p)
7101         cp_parser_commit_to_tentative_parse (parser);
7102       cp_parser_label_declaration (parser);
7103     }
7104   /* Anything else must be a simple-declaration.  */
7105   else
7106     cp_parser_simple_declaration (parser, !statement_p);
7107 }
7108
7109 /* Parse a simple-declaration.
7110
7111    simple-declaration:
7112      decl-specifier-seq [opt] init-declarator-list [opt] ;
7113
7114    init-declarator-list:
7115      init-declarator
7116      init-declarator-list , init-declarator
7117
7118    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7119    function-definition as a simple-declaration.  */
7120
7121 static void
7122 cp_parser_simple_declaration (cp_parser* parser,
7123                               bool function_definition_allowed_p)
7124 {
7125   cp_decl_specifier_seq decl_specifiers;
7126   int declares_class_or_enum;
7127   bool saw_declarator;
7128
7129   /* Defer access checks until we know what is being declared; the
7130      checks for names appearing in the decl-specifier-seq should be
7131      done as if we were in the scope of the thing being declared.  */
7132   push_deferring_access_checks (dk_deferred);
7133
7134   /* Parse the decl-specifier-seq.  We have to keep track of whether
7135      or not the decl-specifier-seq declares a named class or
7136      enumeration type, since that is the only case in which the
7137      init-declarator-list is allowed to be empty.
7138
7139      [dcl.dcl]
7140
7141      In a simple-declaration, the optional init-declarator-list can be
7142      omitted only when declaring a class or enumeration, that is when
7143      the decl-specifier-seq contains either a class-specifier, an
7144      elaborated-type-specifier, or an enum-specifier.  */
7145   cp_parser_decl_specifier_seq (parser,
7146                                 CP_PARSER_FLAGS_OPTIONAL,
7147                                 &decl_specifiers,
7148                                 &declares_class_or_enum);
7149   /* We no longer need to defer access checks.  */
7150   stop_deferring_access_checks ();
7151
7152   /* In a block scope, a valid declaration must always have a
7153      decl-specifier-seq.  By not trying to parse declarators, we can
7154      resolve the declaration/expression ambiguity more quickly.  */
7155   if (!function_definition_allowed_p
7156       && !decl_specifiers.any_specifiers_p)
7157     {
7158       cp_parser_error (parser, "expected declaration");
7159       goto done;
7160     }
7161
7162   /* If the next two tokens are both identifiers, the code is
7163      erroneous. The usual cause of this situation is code like:
7164
7165        T t;
7166
7167      where "T" should name a type -- but does not.  */
7168   if (!decl_specifiers.type
7169       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7170     {
7171       /* If parsing tentatively, we should commit; we really are
7172          looking at a declaration.  */
7173       cp_parser_commit_to_tentative_parse (parser);
7174       /* Give up.  */
7175       goto done;
7176     }
7177
7178   /* If we have seen at least one decl-specifier, and the next token
7179      is not a parenthesis, then we must be looking at a declaration.
7180      (After "int (" we might be looking at a functional cast.)  */
7181   if (decl_specifiers.any_specifiers_p
7182       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7183     cp_parser_commit_to_tentative_parse (parser);
7184
7185   /* Keep going until we hit the `;' at the end of the simple
7186      declaration.  */
7187   saw_declarator = false;
7188   while (cp_lexer_next_token_is_not (parser->lexer,
7189                                      CPP_SEMICOLON))
7190     {
7191       cp_token *token;
7192       bool function_definition_p;
7193       tree decl;
7194
7195       if (saw_declarator)
7196         {
7197           /* If we are processing next declarator, coma is expected */
7198           token = cp_lexer_peek_token (parser->lexer);
7199           gcc_assert (token->type == CPP_COMMA);
7200           cp_lexer_consume_token (parser->lexer);
7201         }
7202       else
7203         saw_declarator = true;
7204
7205       /* Parse the init-declarator.  */
7206       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7207                                         function_definition_allowed_p,
7208                                         /*member_p=*/false,
7209                                         declares_class_or_enum,
7210                                         &function_definition_p);
7211       /* If an error occurred while parsing tentatively, exit quickly.
7212          (That usually happens when in the body of a function; each
7213          statement is treated as a declaration-statement until proven
7214          otherwise.)  */
7215       if (cp_parser_error_occurred (parser))
7216         goto done;
7217       /* Handle function definitions specially.  */
7218       if (function_definition_p)
7219         {
7220           /* If the next token is a `,', then we are probably
7221              processing something like:
7222
7223                void f() {}, *p;
7224
7225              which is erroneous.  */
7226           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7227             error ("mixing declarations and function-definitions is forbidden");
7228           /* Otherwise, we're done with the list of declarators.  */
7229           else
7230             {
7231               pop_deferring_access_checks ();
7232               return;
7233             }
7234         }
7235       /* The next token should be either a `,' or a `;'.  */
7236       token = cp_lexer_peek_token (parser->lexer);
7237       /* If it's a `,', there are more declarators to come.  */
7238       if (token->type == CPP_COMMA)
7239         /* will be consumed next time around */;
7240       /* If it's a `;', we are done.  */
7241       else if (token->type == CPP_SEMICOLON)
7242         break;
7243       /* Anything else is an error.  */
7244       else
7245         {
7246           /* If we have already issued an error message we don't need
7247              to issue another one.  */
7248           if (decl != error_mark_node
7249               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7250             cp_parser_error (parser, "expected %<,%> or %<;%>");
7251           /* Skip tokens until we reach the end of the statement.  */
7252           cp_parser_skip_to_end_of_statement (parser);
7253           /* If the next token is now a `;', consume it.  */
7254           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7255             cp_lexer_consume_token (parser->lexer);
7256           goto done;
7257         }
7258       /* After the first time around, a function-definition is not
7259          allowed -- even if it was OK at first.  For example:
7260
7261            int i, f() {}
7262
7263          is not valid.  */
7264       function_definition_allowed_p = false;
7265     }
7266
7267   /* Issue an error message if no declarators are present, and the
7268      decl-specifier-seq does not itself declare a class or
7269      enumeration.  */
7270   if (!saw_declarator)
7271     {
7272       if (cp_parser_declares_only_class_p (parser))
7273         shadow_tag (&decl_specifiers);
7274       /* Perform any deferred access checks.  */
7275       perform_deferred_access_checks ();
7276     }
7277
7278   /* Consume the `;'.  */
7279   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7280
7281  done:
7282   pop_deferring_access_checks ();
7283 }
7284
7285 /* Parse a decl-specifier-seq.
7286
7287    decl-specifier-seq:
7288      decl-specifier-seq [opt] decl-specifier
7289
7290    decl-specifier:
7291      storage-class-specifier
7292      type-specifier
7293      function-specifier
7294      friend
7295      typedef
7296
7297    GNU Extension:
7298
7299    decl-specifier:
7300      attributes
7301
7302    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7303
7304    The parser flags FLAGS is used to control type-specifier parsing.
7305
7306    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7307    flags:
7308
7309      1: one of the decl-specifiers is an elaborated-type-specifier
7310         (i.e., a type declaration)
7311      2: one of the decl-specifiers is an enum-specifier or a
7312         class-specifier (i.e., a type definition)
7313
7314    */
7315
7316 static void
7317 cp_parser_decl_specifier_seq (cp_parser* parser,
7318                               cp_parser_flags flags,
7319                               cp_decl_specifier_seq *decl_specs,
7320                               int* declares_class_or_enum)
7321 {
7322   bool constructor_possible_p = !parser->in_declarator_p;
7323
7324   /* Clear DECL_SPECS.  */
7325   clear_decl_specs (decl_specs);
7326
7327   /* Assume no class or enumeration type is declared.  */
7328   *declares_class_or_enum = 0;
7329
7330   /* Keep reading specifiers until there are no more to read.  */
7331   while (true)
7332     {
7333       bool constructor_p;
7334       bool found_decl_spec;
7335       cp_token *token;
7336
7337       /* Peek at the next token.  */
7338       token = cp_lexer_peek_token (parser->lexer);
7339       /* Handle attributes.  */
7340       if (token->keyword == RID_ATTRIBUTE)
7341         {
7342           /* Parse the attributes.  */
7343           decl_specs->attributes
7344             = chainon (decl_specs->attributes,
7345                        cp_parser_attributes_opt (parser));
7346           continue;
7347         }
7348       /* Assume we will find a decl-specifier keyword.  */
7349       found_decl_spec = true;
7350       /* If the next token is an appropriate keyword, we can simply
7351          add it to the list.  */
7352       switch (token->keyword)
7353         {
7354           /* decl-specifier:
7355                friend  */
7356         case RID_FRIEND:
7357           if (decl_specs->specs[(int) ds_friend]++)
7358             error ("duplicate %<friend%>");
7359           /* Consume the token.  */
7360           cp_lexer_consume_token (parser->lexer);
7361           break;
7362
7363           /* function-specifier:
7364                inline
7365                virtual
7366                explicit  */
7367         case RID_INLINE:
7368         case RID_VIRTUAL:
7369         case RID_EXPLICIT:
7370           cp_parser_function_specifier_opt (parser, decl_specs);
7371           break;
7372
7373           /* decl-specifier:
7374                typedef  */
7375         case RID_TYPEDEF:
7376           ++decl_specs->specs[(int) ds_typedef];
7377           /* Consume the token.  */
7378           cp_lexer_consume_token (parser->lexer);
7379           /* A constructor declarator cannot appear in a typedef.  */
7380           constructor_possible_p = false;
7381           /* The "typedef" keyword can only occur in a declaration; we
7382              may as well commit at this point.  */
7383           cp_parser_commit_to_tentative_parse (parser);
7384           break;
7385
7386           /* storage-class-specifier:
7387                auto
7388                register
7389                static
7390                extern
7391                mutable
7392
7393              GNU Extension:
7394                thread  */
7395         case RID_AUTO:
7396           /* Consume the token.  */
7397           cp_lexer_consume_token (parser->lexer);
7398           cp_parser_set_storage_class (decl_specs, sc_auto);
7399           break;
7400         case RID_REGISTER:
7401           /* Consume the token.  */
7402           cp_lexer_consume_token (parser->lexer);
7403           cp_parser_set_storage_class (decl_specs, sc_register);
7404           break;
7405         case RID_STATIC:
7406           /* Consume the token.  */
7407           cp_lexer_consume_token (parser->lexer);
7408           if (decl_specs->specs[(int) ds_thread])
7409             {
7410               error ("%<__thread%> before %<static%>");
7411               decl_specs->specs[(int) ds_thread] = 0;
7412             }
7413           cp_parser_set_storage_class (decl_specs, sc_static);
7414           break;
7415         case RID_EXTERN:
7416           /* Consume the token.  */
7417           cp_lexer_consume_token (parser->lexer);
7418           if (decl_specs->specs[(int) ds_thread])
7419             {
7420               error ("%<__thread%> before %<extern%>");
7421               decl_specs->specs[(int) ds_thread] = 0;
7422             }
7423           cp_parser_set_storage_class (decl_specs, sc_extern);
7424           break;
7425         case RID_MUTABLE:
7426           /* Consume the token.  */
7427           cp_lexer_consume_token (parser->lexer);
7428           cp_parser_set_storage_class (decl_specs, sc_mutable);
7429           break;
7430         case RID_THREAD:
7431           /* Consume the token.  */
7432           cp_lexer_consume_token (parser->lexer);
7433           ++decl_specs->specs[(int) ds_thread];
7434           break;
7435
7436         default:
7437           /* We did not yet find a decl-specifier yet.  */
7438           found_decl_spec = false;
7439           break;
7440         }
7441
7442       /* Constructors are a special case.  The `S' in `S()' is not a
7443          decl-specifier; it is the beginning of the declarator.  */
7444       constructor_p
7445         = (!found_decl_spec
7446            && constructor_possible_p
7447            && (cp_parser_constructor_declarator_p
7448                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7449
7450       /* If we don't have a DECL_SPEC yet, then we must be looking at
7451          a type-specifier.  */
7452       if (!found_decl_spec && !constructor_p)
7453         {
7454           int decl_spec_declares_class_or_enum;
7455           bool is_cv_qualifier;
7456           tree type_spec;
7457
7458           type_spec
7459             = cp_parser_type_specifier (parser, flags,
7460                                         decl_specs,
7461                                         /*is_declaration=*/true,
7462                                         &decl_spec_declares_class_or_enum,
7463                                         &is_cv_qualifier);
7464
7465           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7466
7467           /* If this type-specifier referenced a user-defined type
7468              (a typedef, class-name, etc.), then we can't allow any
7469              more such type-specifiers henceforth.
7470
7471              [dcl.spec]
7472
7473              The longest sequence of decl-specifiers that could
7474              possibly be a type name is taken as the
7475              decl-specifier-seq of a declaration.  The sequence shall
7476              be self-consistent as described below.
7477
7478              [dcl.type]
7479
7480              As a general rule, at most one type-specifier is allowed
7481              in the complete decl-specifier-seq of a declaration.  The
7482              only exceptions are the following:
7483
7484              -- const or volatile can be combined with any other
7485                 type-specifier.
7486
7487              -- signed or unsigned can be combined with char, long,
7488                 short, or int.
7489
7490              -- ..
7491
7492              Example:
7493
7494                typedef char* Pc;
7495                void g (const int Pc);
7496
7497              Here, Pc is *not* part of the decl-specifier seq; it's
7498              the declarator.  Therefore, once we see a type-specifier
7499              (other than a cv-qualifier), we forbid any additional
7500              user-defined types.  We *do* still allow things like `int
7501              int' to be considered a decl-specifier-seq, and issue the
7502              error message later.  */
7503           if (type_spec && !is_cv_qualifier)
7504             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7505           /* A constructor declarator cannot follow a type-specifier.  */
7506           if (type_spec)
7507             {
7508               constructor_possible_p = false;
7509               found_decl_spec = true;
7510             }
7511         }
7512
7513       /* If we still do not have a DECL_SPEC, then there are no more
7514          decl-specifiers.  */
7515       if (!found_decl_spec)
7516         break;
7517
7518       decl_specs->any_specifiers_p = true;
7519       /* After we see one decl-specifier, further decl-specifiers are
7520          always optional.  */
7521       flags |= CP_PARSER_FLAGS_OPTIONAL;
7522     }
7523
7524   /* Don't allow a friend specifier with a class definition.  */
7525   if (decl_specs->specs[(int) ds_friend] != 0
7526       && (*declares_class_or_enum & 2))
7527     error ("class definition may not be declared a friend");
7528 }
7529
7530 /* Parse an (optional) storage-class-specifier.
7531
7532    storage-class-specifier:
7533      auto
7534      register
7535      static
7536      extern
7537      mutable
7538
7539    GNU Extension:
7540
7541    storage-class-specifier:
7542      thread
7543
7544    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7545
7546 static tree
7547 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7548 {
7549   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7550     {
7551     case RID_AUTO:
7552     case RID_REGISTER:
7553     case RID_STATIC:
7554     case RID_EXTERN:
7555     case RID_MUTABLE:
7556     case RID_THREAD:
7557       /* Consume the token.  */
7558       return cp_lexer_consume_token (parser->lexer)->value;
7559
7560     default:
7561       return NULL_TREE;
7562     }
7563 }
7564
7565 /* Parse an (optional) function-specifier.
7566
7567    function-specifier:
7568      inline
7569      virtual
7570      explicit
7571
7572    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7573    Updates DECL_SPECS, if it is non-NULL.  */
7574
7575 static tree
7576 cp_parser_function_specifier_opt (cp_parser* parser,
7577                                   cp_decl_specifier_seq *decl_specs)
7578 {
7579   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7580     {
7581     case RID_INLINE:
7582       if (decl_specs)
7583         ++decl_specs->specs[(int) ds_inline];
7584       break;
7585
7586     case RID_VIRTUAL:
7587       if (decl_specs)
7588         ++decl_specs->specs[(int) ds_virtual];
7589       break;
7590
7591     case RID_EXPLICIT:
7592       if (decl_specs)
7593         ++decl_specs->specs[(int) ds_explicit];
7594       break;
7595
7596     default:
7597       return NULL_TREE;
7598     }
7599
7600   /* Consume the token.  */
7601   return cp_lexer_consume_token (parser->lexer)->value;
7602 }
7603
7604 /* Parse a linkage-specification.
7605
7606    linkage-specification:
7607      extern string-literal { declaration-seq [opt] }
7608      extern string-literal declaration  */
7609
7610 static void
7611 cp_parser_linkage_specification (cp_parser* parser)
7612 {
7613   tree linkage;
7614
7615   /* Look for the `extern' keyword.  */
7616   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7617
7618   /* Look for the string-literal.  */
7619   linkage = cp_parser_string_literal (parser, false, false);
7620
7621   /* Transform the literal into an identifier.  If the literal is a
7622      wide-character string, or contains embedded NULs, then we can't
7623      handle it as the user wants.  */
7624   if (strlen (TREE_STRING_POINTER (linkage))
7625       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7626     {
7627       cp_parser_error (parser, "invalid linkage-specification");
7628       /* Assume C++ linkage.  */
7629       linkage = lang_name_cplusplus;
7630     }
7631   else
7632     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7633
7634   /* We're now using the new linkage.  */
7635   push_lang_context (linkage);
7636
7637   /* If the next token is a `{', then we're using the first
7638      production.  */
7639   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7640     {
7641       /* Consume the `{' token.  */
7642       cp_lexer_consume_token (parser->lexer);
7643       /* Parse the declarations.  */
7644       cp_parser_declaration_seq_opt (parser);
7645       /* Look for the closing `}'.  */
7646       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7647     }
7648   /* Otherwise, there's just one declaration.  */
7649   else
7650     {
7651       bool saved_in_unbraced_linkage_specification_p;
7652
7653       saved_in_unbraced_linkage_specification_p
7654         = parser->in_unbraced_linkage_specification_p;
7655       parser->in_unbraced_linkage_specification_p = true;
7656       have_extern_spec = true;
7657       cp_parser_declaration (parser);
7658       have_extern_spec = false;
7659       parser->in_unbraced_linkage_specification_p
7660         = saved_in_unbraced_linkage_specification_p;
7661     }
7662
7663   /* We're done with the linkage-specification.  */
7664   pop_lang_context ();
7665 }
7666
7667 /* Special member functions [gram.special] */
7668
7669 /* Parse a conversion-function-id.
7670
7671    conversion-function-id:
7672      operator conversion-type-id
7673
7674    Returns an IDENTIFIER_NODE representing the operator.  */
7675
7676 static tree
7677 cp_parser_conversion_function_id (cp_parser* parser)
7678 {
7679   tree type;
7680   tree saved_scope;
7681   tree saved_qualifying_scope;
7682   tree saved_object_scope;
7683   tree pushed_scope = NULL_TREE;
7684
7685   /* Look for the `operator' token.  */
7686   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7687     return error_mark_node;
7688   /* When we parse the conversion-type-id, the current scope will be
7689      reset.  However, we need that information in able to look up the
7690      conversion function later, so we save it here.  */
7691   saved_scope = parser->scope;
7692   saved_qualifying_scope = parser->qualifying_scope;
7693   saved_object_scope = parser->object_scope;
7694   /* We must enter the scope of the class so that the names of
7695      entities declared within the class are available in the
7696      conversion-type-id.  For example, consider:
7697
7698        struct S {
7699          typedef int I;
7700          operator I();
7701        };
7702
7703        S::operator I() { ... }
7704
7705      In order to see that `I' is a type-name in the definition, we
7706      must be in the scope of `S'.  */
7707   if (saved_scope)
7708     pushed_scope = push_scope (saved_scope);
7709   /* Parse the conversion-type-id.  */
7710   type = cp_parser_conversion_type_id (parser);
7711   /* Leave the scope of the class, if any.  */
7712   if (pushed_scope)
7713     pop_scope (pushed_scope);
7714   /* Restore the saved scope.  */
7715   parser->scope = saved_scope;
7716   parser->qualifying_scope = saved_qualifying_scope;
7717   parser->object_scope = saved_object_scope;
7718   /* If the TYPE is invalid, indicate failure.  */
7719   if (type == error_mark_node)
7720     return error_mark_node;
7721   return mangle_conv_op_name_for_type (type);
7722 }
7723
7724 /* Parse a conversion-type-id:
7725
7726    conversion-type-id:
7727      type-specifier-seq conversion-declarator [opt]
7728
7729    Returns the TYPE specified.  */
7730
7731 static tree
7732 cp_parser_conversion_type_id (cp_parser* parser)
7733 {
7734   tree attributes;
7735   cp_decl_specifier_seq type_specifiers;
7736   cp_declarator *declarator;
7737   tree type_specified;
7738
7739   /* Parse the attributes.  */
7740   attributes = cp_parser_attributes_opt (parser);
7741   /* Parse the type-specifiers.  */
7742   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7743                                 &type_specifiers);
7744   /* If that didn't work, stop.  */
7745   if (type_specifiers.type == error_mark_node)
7746     return error_mark_node;
7747   /* Parse the conversion-declarator.  */
7748   declarator = cp_parser_conversion_declarator_opt (parser);
7749
7750   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7751                                     /*initialized=*/0, &attributes);
7752   if (attributes)
7753     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7754   return type_specified;
7755 }
7756
7757 /* Parse an (optional) conversion-declarator.
7758
7759    conversion-declarator:
7760      ptr-operator conversion-declarator [opt]
7761
7762    */
7763
7764 static cp_declarator *
7765 cp_parser_conversion_declarator_opt (cp_parser* parser)
7766 {
7767   enum tree_code code;
7768   tree class_type;
7769   cp_cv_quals cv_quals;
7770
7771   /* We don't know if there's a ptr-operator next, or not.  */
7772   cp_parser_parse_tentatively (parser);
7773   /* Try the ptr-operator.  */
7774   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7775   /* If it worked, look for more conversion-declarators.  */
7776   if (cp_parser_parse_definitely (parser))
7777     {
7778       cp_declarator *declarator;
7779
7780       /* Parse another optional declarator.  */
7781       declarator = cp_parser_conversion_declarator_opt (parser);
7782
7783       /* Create the representation of the declarator.  */
7784       if (class_type)
7785         declarator = make_ptrmem_declarator (cv_quals, class_type,
7786                                              declarator);
7787       else if (code == INDIRECT_REF)
7788         declarator = make_pointer_declarator (cv_quals, declarator);
7789       else
7790         declarator = make_reference_declarator (cv_quals, declarator);
7791
7792       return declarator;
7793    }
7794
7795   return NULL;
7796 }
7797
7798 /* Parse an (optional) ctor-initializer.
7799
7800    ctor-initializer:
7801      : mem-initializer-list
7802
7803    Returns TRUE iff the ctor-initializer was actually present.  */
7804
7805 static bool
7806 cp_parser_ctor_initializer_opt (cp_parser* parser)
7807 {
7808   /* If the next token is not a `:', then there is no
7809      ctor-initializer.  */
7810   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7811     {
7812       /* Do default initialization of any bases and members.  */
7813       if (DECL_CONSTRUCTOR_P (current_function_decl))
7814         finish_mem_initializers (NULL_TREE);
7815
7816       return false;
7817     }
7818
7819   /* Consume the `:' token.  */
7820   cp_lexer_consume_token (parser->lexer);
7821   /* And the mem-initializer-list.  */
7822   cp_parser_mem_initializer_list (parser);
7823
7824   return true;
7825 }
7826
7827 /* Parse a mem-initializer-list.
7828
7829    mem-initializer-list:
7830      mem-initializer
7831      mem-initializer , mem-initializer-list  */
7832
7833 static void
7834 cp_parser_mem_initializer_list (cp_parser* parser)
7835 {
7836   tree mem_initializer_list = NULL_TREE;
7837
7838   /* Let the semantic analysis code know that we are starting the
7839      mem-initializer-list.  */
7840   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7841     error ("only constructors take base initializers");
7842
7843   /* Loop through the list.  */
7844   while (true)
7845     {
7846       tree mem_initializer;
7847
7848       /* Parse the mem-initializer.  */
7849       mem_initializer = cp_parser_mem_initializer (parser);
7850       /* Add it to the list, unless it was erroneous.  */
7851       if (mem_initializer != error_mark_node)
7852         {
7853           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7854           mem_initializer_list = mem_initializer;
7855         }
7856       /* If the next token is not a `,', we're done.  */
7857       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7858         break;
7859       /* Consume the `,' token.  */
7860       cp_lexer_consume_token (parser->lexer);
7861     }
7862
7863   /* Perform semantic analysis.  */
7864   if (DECL_CONSTRUCTOR_P (current_function_decl))
7865     finish_mem_initializers (mem_initializer_list);
7866 }
7867
7868 /* Parse a mem-initializer.
7869
7870    mem-initializer:
7871      mem-initializer-id ( expression-list [opt] )
7872
7873    GNU extension:
7874
7875    mem-initializer:
7876      ( expression-list [opt] )
7877
7878    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7879    class) or FIELD_DECL (for a non-static data member) to initialize;
7880    the TREE_VALUE is the expression-list.  An empty initialization
7881    list is represented by void_list_node.  */
7882
7883 static tree
7884 cp_parser_mem_initializer (cp_parser* parser)
7885 {
7886   tree mem_initializer_id;
7887   tree expression_list;
7888   tree member;
7889
7890   /* Find out what is being initialized.  */
7891   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7892     {
7893       pedwarn ("anachronistic old-style base class initializer");
7894       mem_initializer_id = NULL_TREE;
7895     }
7896   else
7897     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7898   member = expand_member_init (mem_initializer_id);
7899   if (member && !DECL_P (member))
7900     in_base_initializer = 1;
7901
7902   expression_list
7903     = cp_parser_parenthesized_expression_list (parser, false,
7904                                                /*cast_p=*/false,
7905                                                /*non_constant_p=*/NULL);
7906   if (expression_list == error_mark_node)
7907     return error_mark_node;
7908   if (!expression_list)
7909     expression_list = void_type_node;
7910
7911   in_base_initializer = 0;
7912
7913   return member ? build_tree_list (member, expression_list) : error_mark_node;
7914 }
7915
7916 /* Parse a mem-initializer-id.
7917
7918    mem-initializer-id:
7919      :: [opt] nested-name-specifier [opt] class-name
7920      identifier
7921
7922    Returns a TYPE indicating the class to be initializer for the first
7923    production.  Returns an IDENTIFIER_NODE indicating the data member
7924    to be initialized for the second production.  */
7925
7926 static tree
7927 cp_parser_mem_initializer_id (cp_parser* parser)
7928 {
7929   bool global_scope_p;
7930   bool nested_name_specifier_p;
7931   bool template_p = false;
7932   tree id;
7933
7934   /* `typename' is not allowed in this context ([temp.res]).  */
7935   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7936     {
7937       error ("keyword %<typename%> not allowed in this context (a qualified "
7938              "member initializer is implicitly a type)");
7939       cp_lexer_consume_token (parser->lexer);
7940     }
7941   /* Look for the optional `::' operator.  */
7942   global_scope_p
7943     = (cp_parser_global_scope_opt (parser,
7944                                    /*current_scope_valid_p=*/false)
7945        != NULL_TREE);
7946   /* Look for the optional nested-name-specifier.  The simplest way to
7947      implement:
7948
7949        [temp.res]
7950
7951        The keyword `typename' is not permitted in a base-specifier or
7952        mem-initializer; in these contexts a qualified name that
7953        depends on a template-parameter is implicitly assumed to be a
7954        type name.
7955
7956      is to assume that we have seen the `typename' keyword at this
7957      point.  */
7958   nested_name_specifier_p
7959     = (cp_parser_nested_name_specifier_opt (parser,
7960                                             /*typename_keyword_p=*/true,
7961                                             /*check_dependency_p=*/true,
7962                                             /*type_p=*/true,
7963                                             /*is_declaration=*/true)
7964        != NULL_TREE);
7965   if (nested_name_specifier_p)
7966     template_p = cp_parser_optional_template_keyword (parser);
7967   /* If there is a `::' operator or a nested-name-specifier, then we
7968      are definitely looking for a class-name.  */
7969   if (global_scope_p || nested_name_specifier_p)
7970     return cp_parser_class_name (parser,
7971                                  /*typename_keyword_p=*/true,
7972                                  /*template_keyword_p=*/template_p,
7973                                  none_type,
7974                                  /*check_dependency_p=*/true,
7975                                  /*class_head_p=*/false,
7976                                  /*is_declaration=*/true);
7977   /* Otherwise, we could also be looking for an ordinary identifier.  */
7978   cp_parser_parse_tentatively (parser);
7979   /* Try a class-name.  */
7980   id = cp_parser_class_name (parser,
7981                              /*typename_keyword_p=*/true,
7982                              /*template_keyword_p=*/false,
7983                              none_type,
7984                              /*check_dependency_p=*/true,
7985                              /*class_head_p=*/false,
7986                              /*is_declaration=*/true);
7987   /* If we found one, we're done.  */
7988   if (cp_parser_parse_definitely (parser))
7989     return id;
7990   /* Otherwise, look for an ordinary identifier.  */
7991   return cp_parser_identifier (parser);
7992 }
7993
7994 /* Overloading [gram.over] */
7995
7996 /* Parse an operator-function-id.
7997
7998    operator-function-id:
7999      operator operator
8000
8001    Returns an IDENTIFIER_NODE for the operator which is a
8002    human-readable spelling of the identifier, e.g., `operator +'.  */
8003
8004 static tree
8005 cp_parser_operator_function_id (cp_parser* parser)
8006 {
8007   /* Look for the `operator' keyword.  */
8008   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8009     return error_mark_node;
8010   /* And then the name of the operator itself.  */
8011   return cp_parser_operator (parser);
8012 }
8013
8014 /* Parse an operator.
8015
8016    operator:
8017      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8018      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8019      || ++ -- , ->* -> () []
8020
8021    GNU Extensions:
8022
8023    operator:
8024      <? >? <?= >?=
8025
8026    Returns an IDENTIFIER_NODE for the operator which is a
8027    human-readable spelling of the identifier, e.g., `operator +'.  */
8028
8029 static tree
8030 cp_parser_operator (cp_parser* parser)
8031 {
8032   tree id = NULL_TREE;
8033   cp_token *token;
8034
8035   /* Peek at the next token.  */
8036   token = cp_lexer_peek_token (parser->lexer);
8037   /* Figure out which operator we have.  */
8038   switch (token->type)
8039     {
8040     case CPP_KEYWORD:
8041       {
8042         enum tree_code op;
8043
8044         /* The keyword should be either `new' or `delete'.  */
8045         if (token->keyword == RID_NEW)
8046           op = NEW_EXPR;
8047         else if (token->keyword == RID_DELETE)
8048           op = DELETE_EXPR;
8049         else
8050           break;
8051
8052         /* Consume the `new' or `delete' token.  */
8053         cp_lexer_consume_token (parser->lexer);
8054
8055         /* Peek at the next token.  */
8056         token = cp_lexer_peek_token (parser->lexer);
8057         /* If it's a `[' token then this is the array variant of the
8058            operator.  */
8059         if (token->type == CPP_OPEN_SQUARE)
8060           {
8061             /* Consume the `[' token.  */
8062             cp_lexer_consume_token (parser->lexer);
8063             /* Look for the `]' token.  */
8064             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8065             id = ansi_opname (op == NEW_EXPR
8066                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8067           }
8068         /* Otherwise, we have the non-array variant.  */
8069         else
8070           id = ansi_opname (op);
8071
8072         return id;
8073       }
8074
8075     case CPP_PLUS:
8076       id = ansi_opname (PLUS_EXPR);
8077       break;
8078
8079     case CPP_MINUS:
8080       id = ansi_opname (MINUS_EXPR);
8081       break;
8082
8083     case CPP_MULT:
8084       id = ansi_opname (MULT_EXPR);
8085       break;
8086
8087     case CPP_DIV:
8088       id = ansi_opname (TRUNC_DIV_EXPR);
8089       break;
8090
8091     case CPP_MOD:
8092       id = ansi_opname (TRUNC_MOD_EXPR);
8093       break;
8094
8095     case CPP_XOR:
8096       id = ansi_opname (BIT_XOR_EXPR);
8097       break;
8098
8099     case CPP_AND:
8100       id = ansi_opname (BIT_AND_EXPR);
8101       break;
8102
8103     case CPP_OR:
8104       id = ansi_opname (BIT_IOR_EXPR);
8105       break;
8106
8107     case CPP_COMPL:
8108       id = ansi_opname (BIT_NOT_EXPR);
8109       break;
8110
8111     case CPP_NOT:
8112       id = ansi_opname (TRUTH_NOT_EXPR);
8113       break;
8114
8115     case CPP_EQ:
8116       id = ansi_assopname (NOP_EXPR);
8117       break;
8118
8119     case CPP_LESS:
8120       id = ansi_opname (LT_EXPR);
8121       break;
8122
8123     case CPP_GREATER:
8124       id = ansi_opname (GT_EXPR);
8125       break;
8126
8127     case CPP_PLUS_EQ:
8128       id = ansi_assopname (PLUS_EXPR);
8129       break;
8130
8131     case CPP_MINUS_EQ:
8132       id = ansi_assopname (MINUS_EXPR);
8133       break;
8134
8135     case CPP_MULT_EQ:
8136       id = ansi_assopname (MULT_EXPR);
8137       break;
8138
8139     case CPP_DIV_EQ:
8140       id = ansi_assopname (TRUNC_DIV_EXPR);
8141       break;
8142
8143     case CPP_MOD_EQ:
8144       id = ansi_assopname (TRUNC_MOD_EXPR);
8145       break;
8146
8147     case CPP_XOR_EQ:
8148       id = ansi_assopname (BIT_XOR_EXPR);
8149       break;
8150
8151     case CPP_AND_EQ:
8152       id = ansi_assopname (BIT_AND_EXPR);
8153       break;
8154
8155     case CPP_OR_EQ:
8156       id = ansi_assopname (BIT_IOR_EXPR);
8157       break;
8158
8159     case CPP_LSHIFT:
8160       id = ansi_opname (LSHIFT_EXPR);
8161       break;
8162
8163     case CPP_RSHIFT:
8164       id = ansi_opname (RSHIFT_EXPR);
8165       break;
8166
8167     case CPP_LSHIFT_EQ:
8168       id = ansi_assopname (LSHIFT_EXPR);
8169       break;
8170
8171     case CPP_RSHIFT_EQ:
8172       id = ansi_assopname (RSHIFT_EXPR);
8173       break;
8174
8175     case CPP_EQ_EQ:
8176       id = ansi_opname (EQ_EXPR);
8177       break;
8178
8179     case CPP_NOT_EQ:
8180       id = ansi_opname (NE_EXPR);
8181       break;
8182
8183     case CPP_LESS_EQ:
8184       id = ansi_opname (LE_EXPR);
8185       break;
8186
8187     case CPP_GREATER_EQ:
8188       id = ansi_opname (GE_EXPR);
8189       break;
8190
8191     case CPP_AND_AND:
8192       id = ansi_opname (TRUTH_ANDIF_EXPR);
8193       break;
8194
8195     case CPP_OR_OR:
8196       id = ansi_opname (TRUTH_ORIF_EXPR);
8197       break;
8198
8199     case CPP_PLUS_PLUS:
8200       id = ansi_opname (POSTINCREMENT_EXPR);
8201       break;
8202
8203     case CPP_MINUS_MINUS:
8204       id = ansi_opname (PREDECREMENT_EXPR);
8205       break;
8206
8207     case CPP_COMMA:
8208       id = ansi_opname (COMPOUND_EXPR);
8209       break;
8210
8211     case CPP_DEREF_STAR:
8212       id = ansi_opname (MEMBER_REF);
8213       break;
8214
8215     case CPP_DEREF:
8216       id = ansi_opname (COMPONENT_REF);
8217       break;
8218
8219     case CPP_OPEN_PAREN:
8220       /* Consume the `('.  */
8221       cp_lexer_consume_token (parser->lexer);
8222       /* Look for the matching `)'.  */
8223       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8224       return ansi_opname (CALL_EXPR);
8225
8226     case CPP_OPEN_SQUARE:
8227       /* Consume the `['.  */
8228       cp_lexer_consume_token (parser->lexer);
8229       /* Look for the matching `]'.  */
8230       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8231       return ansi_opname (ARRAY_REF);
8232
8233       /* Extensions.  */
8234     case CPP_MIN:
8235       id = ansi_opname (MIN_EXPR);
8236       cp_parser_warn_min_max ();
8237       break;
8238
8239     case CPP_MAX:
8240       id = ansi_opname (MAX_EXPR);
8241       cp_parser_warn_min_max ();
8242       break;
8243
8244     case CPP_MIN_EQ:
8245       id = ansi_assopname (MIN_EXPR);
8246       cp_parser_warn_min_max ();
8247       break;
8248
8249     case CPP_MAX_EQ:
8250       id = ansi_assopname (MAX_EXPR);
8251       cp_parser_warn_min_max ();
8252       break;
8253
8254     default:
8255       /* Anything else is an error.  */
8256       break;
8257     }
8258
8259   /* If we have selected an identifier, we need to consume the
8260      operator token.  */
8261   if (id)
8262     cp_lexer_consume_token (parser->lexer);
8263   /* Otherwise, no valid operator name was present.  */
8264   else
8265     {
8266       cp_parser_error (parser, "expected operator");
8267       id = error_mark_node;
8268     }
8269
8270   return id;
8271 }
8272
8273 /* Parse a template-declaration.
8274
8275    template-declaration:
8276      export [opt] template < template-parameter-list > declaration
8277
8278    If MEMBER_P is TRUE, this template-declaration occurs within a
8279    class-specifier.
8280
8281    The grammar rule given by the standard isn't correct.  What
8282    is really meant is:
8283
8284    template-declaration:
8285      export [opt] template-parameter-list-seq
8286        decl-specifier-seq [opt] init-declarator [opt] ;
8287      export [opt] template-parameter-list-seq
8288        function-definition
8289
8290    template-parameter-list-seq:
8291      template-parameter-list-seq [opt]
8292      template < template-parameter-list >  */
8293
8294 static void
8295 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8296 {
8297   /* Check for `export'.  */
8298   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8299     {
8300       /* Consume the `export' token.  */
8301       cp_lexer_consume_token (parser->lexer);
8302       /* Warn that we do not support `export'.  */
8303       warning (0, "keyword %<export%> not implemented, and will be ignored");
8304     }
8305
8306   cp_parser_template_declaration_after_export (parser, member_p);
8307 }
8308
8309 /* Parse a template-parameter-list.
8310
8311    template-parameter-list:
8312      template-parameter
8313      template-parameter-list , template-parameter
8314
8315    Returns a TREE_LIST.  Each node represents a template parameter.
8316    The nodes are connected via their TREE_CHAINs.  */
8317
8318 static tree
8319 cp_parser_template_parameter_list (cp_parser* parser)
8320 {
8321   tree parameter_list = NULL_TREE;
8322
8323   begin_template_parm_list ();
8324   while (true)
8325     {
8326       tree parameter;
8327       cp_token *token;
8328       bool is_non_type;
8329
8330       /* Parse the template-parameter.  */
8331       parameter = cp_parser_template_parameter (parser, &is_non_type);
8332       /* Add it to the list.  */
8333       if (parameter != error_mark_node)
8334         parameter_list = process_template_parm (parameter_list,
8335                                                 parameter,
8336                                                 is_non_type);
8337       /* Peek at the next token.  */
8338       token = cp_lexer_peek_token (parser->lexer);
8339       /* If it's not a `,', we're done.  */
8340       if (token->type != CPP_COMMA)
8341         break;
8342       /* Otherwise, consume the `,' token.  */
8343       cp_lexer_consume_token (parser->lexer);
8344     }
8345
8346   return end_template_parm_list (parameter_list);
8347 }
8348
8349 /* Parse a template-parameter.
8350
8351    template-parameter:
8352      type-parameter
8353      parameter-declaration
8354
8355    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8356    the parameter.  The TREE_PURPOSE is the default value, if any.
8357    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8358    iff this parameter is a non-type parameter.  */
8359
8360 static tree
8361 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8362 {
8363   cp_token *token;
8364   cp_parameter_declarator *parameter_declarator;
8365   tree parm;
8366
8367   /* Assume it is a type parameter or a template parameter.  */
8368   *is_non_type = false;
8369   /* Peek at the next token.  */
8370   token = cp_lexer_peek_token (parser->lexer);
8371   /* If it is `class' or `template', we have a type-parameter.  */
8372   if (token->keyword == RID_TEMPLATE)
8373     return cp_parser_type_parameter (parser);
8374   /* If it is `class' or `typename' we do not know yet whether it is a
8375      type parameter or a non-type parameter.  Consider:
8376
8377        template <typename T, typename T::X X> ...
8378
8379      or:
8380
8381        template <class C, class D*> ...
8382
8383      Here, the first parameter is a type parameter, and the second is
8384      a non-type parameter.  We can tell by looking at the token after
8385      the identifier -- if it is a `,', `=', or `>' then we have a type
8386      parameter.  */
8387   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8388     {
8389       /* Peek at the token after `class' or `typename'.  */
8390       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8391       /* If it's an identifier, skip it.  */
8392       if (token->type == CPP_NAME)
8393         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8394       /* Now, see if the token looks like the end of a template
8395          parameter.  */
8396       if (token->type == CPP_COMMA
8397           || token->type == CPP_EQ
8398           || token->type == CPP_GREATER)
8399         return cp_parser_type_parameter (parser);
8400     }
8401
8402   /* Otherwise, it is a non-type parameter.
8403
8404      [temp.param]
8405
8406      When parsing a default template-argument for a non-type
8407      template-parameter, the first non-nested `>' is taken as the end
8408      of the template parameter-list rather than a greater-than
8409      operator.  */
8410   *is_non_type = true;
8411   parameter_declarator
8412      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8413                                         /*parenthesized_p=*/NULL);
8414   parm = grokdeclarator (parameter_declarator->declarator,
8415                          &parameter_declarator->decl_specifiers,
8416                          PARM, /*initialized=*/0,
8417                          /*attrlist=*/NULL);
8418   if (parm == error_mark_node)
8419     return error_mark_node;
8420   return build_tree_list (parameter_declarator->default_argument, parm);
8421 }
8422
8423 /* Parse a type-parameter.
8424
8425    type-parameter:
8426      class identifier [opt]
8427      class identifier [opt] = type-id
8428      typename identifier [opt]
8429      typename identifier [opt] = type-id
8430      template < template-parameter-list > class identifier [opt]
8431      template < template-parameter-list > class identifier [opt]
8432        = id-expression
8433
8434    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8435    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8436    the declaration of the parameter.  */
8437
8438 static tree
8439 cp_parser_type_parameter (cp_parser* parser)
8440 {
8441   cp_token *token;
8442   tree parameter;
8443
8444   /* Look for a keyword to tell us what kind of parameter this is.  */
8445   token = cp_parser_require (parser, CPP_KEYWORD,
8446                              "`class', `typename', or `template'");
8447   if (!token)
8448     return error_mark_node;
8449
8450   switch (token->keyword)
8451     {
8452     case RID_CLASS:
8453     case RID_TYPENAME:
8454       {
8455         tree identifier;
8456         tree default_argument;
8457
8458         /* If the next token is an identifier, then it names the
8459            parameter.  */
8460         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8461           identifier = cp_parser_identifier (parser);
8462         else
8463           identifier = NULL_TREE;
8464
8465         /* Create the parameter.  */
8466         parameter = finish_template_type_parm (class_type_node, identifier);
8467
8468         /* If the next token is an `=', we have a default argument.  */
8469         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8470           {
8471             /* Consume the `=' token.  */
8472             cp_lexer_consume_token (parser->lexer);
8473             /* Parse the default-argument.  */
8474             default_argument = cp_parser_type_id (parser);
8475           }
8476         else
8477           default_argument = NULL_TREE;
8478
8479         /* Create the combined representation of the parameter and the
8480            default argument.  */
8481         parameter = build_tree_list (default_argument, parameter);
8482       }
8483       break;
8484
8485     case RID_TEMPLATE:
8486       {
8487         tree parameter_list;
8488         tree identifier;
8489         tree default_argument;
8490
8491         /* Look for the `<'.  */
8492         cp_parser_require (parser, CPP_LESS, "`<'");
8493         /* Parse the template-parameter-list.  */
8494         parameter_list = cp_parser_template_parameter_list (parser);
8495         /* Look for the `>'.  */
8496         cp_parser_require (parser, CPP_GREATER, "`>'");
8497         /* Look for the `class' keyword.  */
8498         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8499         /* If the next token is an `=', then there is a
8500            default-argument.  If the next token is a `>', we are at
8501            the end of the parameter-list.  If the next token is a `,',
8502            then we are at the end of this parameter.  */
8503         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8504             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8505             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8506           {
8507             identifier = cp_parser_identifier (parser);
8508             /* Treat invalid names as if the parameter were nameless.  */
8509             if (identifier == error_mark_node)
8510               identifier = NULL_TREE;
8511           }
8512         else
8513           identifier = NULL_TREE;
8514
8515         /* Create the template parameter.  */
8516         parameter = finish_template_template_parm (class_type_node,
8517                                                    identifier);
8518
8519         /* If the next token is an `=', then there is a
8520            default-argument.  */
8521         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8522           {
8523             bool is_template;
8524
8525             /* Consume the `='.  */
8526             cp_lexer_consume_token (parser->lexer);
8527             /* Parse the id-expression.  */
8528             default_argument
8529               = cp_parser_id_expression (parser,
8530                                          /*template_keyword_p=*/false,
8531                                          /*check_dependency_p=*/true,
8532                                          /*template_p=*/&is_template,
8533                                          /*declarator_p=*/false);
8534             if (TREE_CODE (default_argument) == TYPE_DECL)
8535               /* If the id-expression was a template-id that refers to
8536                  a template-class, we already have the declaration here,
8537                  so no further lookup is needed.  */
8538                  ;
8539             else
8540               /* Look up the name.  */
8541               default_argument
8542                 = cp_parser_lookup_name (parser, default_argument,
8543                                          none_type,
8544                                          /*is_template=*/is_template,
8545                                          /*is_namespace=*/false,
8546                                          /*check_dependency=*/true,
8547                                          /*ambiguous_decls=*/NULL);
8548             /* See if the default argument is valid.  */
8549             default_argument
8550               = check_template_template_default_arg (default_argument);
8551           }
8552         else
8553           default_argument = NULL_TREE;
8554
8555         /* Create the combined representation of the parameter and the
8556            default argument.  */
8557         parameter = build_tree_list (default_argument, parameter);
8558       }
8559       break;
8560
8561     default:
8562       gcc_unreachable ();
8563       break;
8564     }
8565
8566   return parameter;
8567 }
8568
8569 /* Parse a template-id.
8570
8571    template-id:
8572      template-name < template-argument-list [opt] >
8573
8574    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8575    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8576    returned.  Otherwise, if the template-name names a function, or set
8577    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8578    names a class, returns a TYPE_DECL for the specialization.
8579
8580    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8581    uninstantiated templates.  */
8582
8583 static tree
8584 cp_parser_template_id (cp_parser *parser,
8585                        bool template_keyword_p,
8586                        bool check_dependency_p,
8587                        bool is_declaration)
8588 {
8589   tree template;
8590   tree arguments;
8591   tree template_id;
8592   cp_token_position start_of_id = 0;
8593   tree access_check = NULL_TREE;
8594   cp_token *next_token, *next_token_2;
8595   bool is_identifier;
8596
8597   /* If the next token corresponds to a template-id, there is no need
8598      to reparse it.  */
8599   next_token = cp_lexer_peek_token (parser->lexer);
8600   if (next_token->type == CPP_TEMPLATE_ID)
8601     {
8602       tree value;
8603       tree check;
8604
8605       /* Get the stored value.  */
8606       value = cp_lexer_consume_token (parser->lexer)->value;
8607       /* Perform any access checks that were deferred.  */
8608       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8609         perform_or_defer_access_check (TREE_PURPOSE (check),
8610                                        TREE_VALUE (check));
8611       /* Return the stored value.  */
8612       return TREE_VALUE (value);
8613     }
8614
8615   /* Avoid performing name lookup if there is no possibility of
8616      finding a template-id.  */
8617   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8618       || (next_token->type == CPP_NAME
8619           && !cp_parser_nth_token_starts_template_argument_list_p
8620                (parser, 2)))
8621     {
8622       cp_parser_error (parser, "expected template-id");
8623       return error_mark_node;
8624     }
8625
8626   /* Remember where the template-id starts.  */
8627   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8628     start_of_id = cp_lexer_token_position (parser->lexer, false);
8629
8630   push_deferring_access_checks (dk_deferred);
8631
8632   /* Parse the template-name.  */
8633   is_identifier = false;
8634   template = cp_parser_template_name (parser, template_keyword_p,
8635                                       check_dependency_p,
8636                                       is_declaration,
8637                                       &is_identifier);
8638   if (template == error_mark_node || is_identifier)
8639     {
8640       pop_deferring_access_checks ();
8641       return template;
8642     }
8643
8644   /* If we find the sequence `[:' after a template-name, it's probably
8645      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8646      parse correctly the argument list.  */
8647   next_token = cp_lexer_peek_token (parser->lexer);
8648   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8649   if (next_token->type == CPP_OPEN_SQUARE
8650       && next_token->flags & DIGRAPH
8651       && next_token_2->type == CPP_COLON
8652       && !(next_token_2->flags & PREV_WHITE))
8653     {
8654       cp_parser_parse_tentatively (parser);
8655       /* Change `:' into `::'.  */
8656       next_token_2->type = CPP_SCOPE;
8657       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8658          CPP_LESS.  */
8659       cp_lexer_consume_token (parser->lexer);
8660       /* Parse the arguments.  */
8661       arguments = cp_parser_enclosed_template_argument_list (parser);
8662       if (!cp_parser_parse_definitely (parser))
8663         {
8664           /* If we couldn't parse an argument list, then we revert our changes
8665              and return simply an error. Maybe this is not a template-id
8666              after all.  */
8667           next_token_2->type = CPP_COLON;
8668           cp_parser_error (parser, "expected %<<%>");
8669           pop_deferring_access_checks ();
8670           return error_mark_node;
8671         }
8672       /* Otherwise, emit an error about the invalid digraph, but continue
8673          parsing because we got our argument list.  */
8674       pedwarn ("%<<::%> cannot begin a template-argument list");
8675       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8676               "between %<<%> and %<::%>");
8677       if (!flag_permissive)
8678         {
8679           static bool hint;
8680           if (!hint)
8681             {
8682               inform ("(if you use -fpermissive G++ will accept your code)");
8683               hint = true;
8684             }
8685         }
8686     }
8687   else
8688     {
8689       /* Look for the `<' that starts the template-argument-list.  */
8690       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8691         {
8692           pop_deferring_access_checks ();
8693           return error_mark_node;
8694         }
8695       /* Parse the arguments.  */
8696       arguments = cp_parser_enclosed_template_argument_list (parser);
8697     }
8698
8699   /* Build a representation of the specialization.  */
8700   if (TREE_CODE (template) == IDENTIFIER_NODE)
8701     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8702   else if (DECL_CLASS_TEMPLATE_P (template)
8703            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8704     template_id
8705       = finish_template_type (template, arguments,
8706                               cp_lexer_next_token_is (parser->lexer,
8707                                                       CPP_SCOPE));
8708   else
8709     {
8710       /* If it's not a class-template or a template-template, it should be
8711          a function-template.  */
8712       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8713                    || TREE_CODE (template) == OVERLOAD
8714                    || BASELINK_P (template)));
8715
8716       template_id = lookup_template_function (template, arguments);
8717     }
8718
8719   /* Retrieve any deferred checks.  Do not pop this access checks yet
8720      so the memory will not be reclaimed during token replacing below.  */
8721   access_check = get_deferred_access_checks ();
8722
8723   /* If parsing tentatively, replace the sequence of tokens that makes
8724      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8725      should we re-parse the token stream, we will not have to repeat
8726      the effort required to do the parse, nor will we issue duplicate
8727      error messages about problems during instantiation of the
8728      template.  */
8729   if (start_of_id)
8730     {
8731       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8732
8733       /* Reset the contents of the START_OF_ID token.  */
8734       token->type = CPP_TEMPLATE_ID;
8735       token->value = build_tree_list (access_check, template_id);
8736       token->keyword = RID_MAX;
8737
8738       /* Purge all subsequent tokens.  */
8739       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8740
8741       /* ??? Can we actually assume that, if template_id ==
8742          error_mark_node, we will have issued a diagnostic to the
8743          user, as opposed to simply marking the tentative parse as
8744          failed?  */
8745       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8746         error ("parse error in template argument list");
8747     }
8748
8749   pop_deferring_access_checks ();
8750   return template_id;
8751 }
8752
8753 /* Parse a template-name.
8754
8755    template-name:
8756      identifier
8757
8758    The standard should actually say:
8759
8760    template-name:
8761      identifier
8762      operator-function-id
8763
8764    A defect report has been filed about this issue.
8765
8766    A conversion-function-id cannot be a template name because they cannot
8767    be part of a template-id. In fact, looking at this code:
8768
8769    a.operator K<int>()
8770
8771    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8772    It is impossible to call a templated conversion-function-id with an
8773    explicit argument list, since the only allowed template parameter is
8774    the type to which it is converting.
8775
8776    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8777    `template' keyword, in a construction like:
8778
8779      T::template f<3>()
8780
8781    In that case `f' is taken to be a template-name, even though there
8782    is no way of knowing for sure.
8783
8784    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8785    name refers to a set of overloaded functions, at least one of which
8786    is a template, or an IDENTIFIER_NODE with the name of the template,
8787    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8788    names are looked up inside uninstantiated templates.  */
8789
8790 static tree
8791 cp_parser_template_name (cp_parser* parser,
8792                          bool template_keyword_p,
8793                          bool check_dependency_p,
8794                          bool is_declaration,
8795                          bool *is_identifier)
8796 {
8797   tree identifier;
8798   tree decl;
8799   tree fns;
8800
8801   /* If the next token is `operator', then we have either an
8802      operator-function-id or a conversion-function-id.  */
8803   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8804     {
8805       /* We don't know whether we're looking at an
8806          operator-function-id or a conversion-function-id.  */
8807       cp_parser_parse_tentatively (parser);
8808       /* Try an operator-function-id.  */
8809       identifier = cp_parser_operator_function_id (parser);
8810       /* If that didn't work, try a conversion-function-id.  */
8811       if (!cp_parser_parse_definitely (parser))
8812         {
8813           cp_parser_error (parser, "expected template-name");
8814           return error_mark_node;
8815         }
8816     }
8817   /* Look for the identifier.  */
8818   else
8819     identifier = cp_parser_identifier (parser);
8820
8821   /* If we didn't find an identifier, we don't have a template-id.  */
8822   if (identifier == error_mark_node)
8823     return error_mark_node;
8824
8825   /* If the name immediately followed the `template' keyword, then it
8826      is a template-name.  However, if the next token is not `<', then
8827      we do not treat it as a template-name, since it is not being used
8828      as part of a template-id.  This enables us to handle constructs
8829      like:
8830
8831        template <typename T> struct S { S(); };
8832        template <typename T> S<T>::S();
8833
8834      correctly.  We would treat `S' as a template -- if it were `S<T>'
8835      -- but we do not if there is no `<'.  */
8836
8837   if (processing_template_decl
8838       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8839     {
8840       /* In a declaration, in a dependent context, we pretend that the
8841          "template" keyword was present in order to improve error
8842          recovery.  For example, given:
8843
8844            template <typename T> void f(T::X<int>);
8845
8846          we want to treat "X<int>" as a template-id.  */
8847       if (is_declaration
8848           && !template_keyword_p
8849           && parser->scope && TYPE_P (parser->scope)
8850           && check_dependency_p
8851           && dependent_type_p (parser->scope)
8852           /* Do not do this for dtors (or ctors), since they never
8853              need the template keyword before their name.  */
8854           && !constructor_name_p (identifier, parser->scope))
8855         {
8856           cp_token_position start = 0;
8857
8858           /* Explain what went wrong.  */
8859           error ("non-template %qD used as template", identifier);
8860           inform ("use %<%T::template %D%> to indicate that it is a template",
8861                   parser->scope, identifier);
8862           /* If parsing tentatively, find the location of the "<" token.  */
8863           if (cp_parser_simulate_error (parser))
8864             start = cp_lexer_token_position (parser->lexer, true);
8865           /* Parse the template arguments so that we can issue error
8866              messages about them.  */
8867           cp_lexer_consume_token (parser->lexer);
8868           cp_parser_enclosed_template_argument_list (parser);
8869           /* Skip tokens until we find a good place from which to
8870              continue parsing.  */
8871           cp_parser_skip_to_closing_parenthesis (parser,
8872                                                  /*recovering=*/true,
8873                                                  /*or_comma=*/true,
8874                                                  /*consume_paren=*/false);
8875           /* If parsing tentatively, permanently remove the
8876              template argument list.  That will prevent duplicate
8877              error messages from being issued about the missing
8878              "template" keyword.  */
8879           if (start)
8880             cp_lexer_purge_tokens_after (parser->lexer, start);
8881           if (is_identifier)
8882             *is_identifier = true;
8883           return identifier;
8884         }
8885
8886       /* If the "template" keyword is present, then there is generally
8887          no point in doing name-lookup, so we just return IDENTIFIER.
8888          But, if the qualifying scope is non-dependent then we can
8889          (and must) do name-lookup normally.  */
8890       if (template_keyword_p
8891           && (!parser->scope
8892               || (TYPE_P (parser->scope)
8893                   && dependent_type_p (parser->scope))))
8894         return identifier;
8895     }
8896
8897   /* Look up the name.  */
8898   decl = cp_parser_lookup_name (parser, identifier,
8899                                 none_type,
8900                                 /*is_template=*/false,
8901                                 /*is_namespace=*/false,
8902                                 check_dependency_p,
8903                                 /*ambiguous_decls=*/NULL);
8904   decl = maybe_get_template_decl_from_type_decl (decl);
8905
8906   /* If DECL is a template, then the name was a template-name.  */
8907   if (TREE_CODE (decl) == TEMPLATE_DECL)
8908     ;
8909   else
8910     {
8911       tree fn = NULL_TREE;
8912
8913       /* The standard does not explicitly indicate whether a name that
8914          names a set of overloaded declarations, some of which are
8915          templates, is a template-name.  However, such a name should
8916          be a template-name; otherwise, there is no way to form a
8917          template-id for the overloaded templates.  */
8918       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8919       if (TREE_CODE (fns) == OVERLOAD)
8920         for (fn = fns; fn; fn = OVL_NEXT (fn))
8921           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8922             break;
8923
8924       if (!fn)
8925         {
8926           /* The name does not name a template.  */
8927           cp_parser_error (parser, "expected template-name");
8928           return error_mark_node;
8929         }
8930     }
8931
8932   /* If DECL is dependent, and refers to a function, then just return
8933      its name; we will look it up again during template instantiation.  */
8934   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8935     {
8936       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8937       if (TYPE_P (scope) && dependent_type_p (scope))
8938         return identifier;
8939     }
8940
8941   return decl;
8942 }
8943
8944 /* Parse a template-argument-list.
8945
8946    template-argument-list:
8947      template-argument
8948      template-argument-list , template-argument
8949
8950    Returns a TREE_VEC containing the arguments.  */
8951
8952 static tree
8953 cp_parser_template_argument_list (cp_parser* parser)
8954 {
8955   tree fixed_args[10];
8956   unsigned n_args = 0;
8957   unsigned alloced = 10;
8958   tree *arg_ary = fixed_args;
8959   tree vec;
8960   bool saved_in_template_argument_list_p;
8961   bool saved_ice_p;
8962   bool saved_non_ice_p;
8963
8964   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8965   parser->in_template_argument_list_p = true;
8966   /* Even if the template-id appears in an integral
8967      constant-expression, the contents of the argument list do 
8968      not.  */ 
8969   saved_ice_p = parser->integral_constant_expression_p;
8970   parser->integral_constant_expression_p = false;
8971   saved_non_ice_p = parser->non_integral_constant_expression_p;
8972   parser->non_integral_constant_expression_p = false;
8973   /* Parse the arguments.  */
8974   do
8975     {
8976       tree argument;
8977
8978       if (n_args)
8979         /* Consume the comma.  */
8980         cp_lexer_consume_token (parser->lexer);
8981
8982       /* Parse the template-argument.  */
8983       argument = cp_parser_template_argument (parser);
8984       if (n_args == alloced)
8985         {
8986           alloced *= 2;
8987
8988           if (arg_ary == fixed_args)
8989             {
8990               arg_ary = XNEWVEC (tree, alloced);
8991               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8992             }
8993           else
8994             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
8995         }
8996       arg_ary[n_args++] = argument;
8997     }
8998   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8999
9000   vec = make_tree_vec (n_args);
9001
9002   while (n_args--)
9003     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9004
9005   if (arg_ary != fixed_args)
9006     free (arg_ary);
9007   parser->non_integral_constant_expression_p = saved_non_ice_p;
9008   parser->integral_constant_expression_p = saved_ice_p;
9009   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9010   return vec;
9011 }
9012
9013 /* Parse a template-argument.
9014
9015    template-argument:
9016      assignment-expression
9017      type-id
9018      id-expression
9019
9020    The representation is that of an assignment-expression, type-id, or
9021    id-expression -- except that the qualified id-expression is
9022    evaluated, so that the value returned is either a DECL or an
9023    OVERLOAD.
9024
9025    Although the standard says "assignment-expression", it forbids
9026    throw-expressions or assignments in the template argument.
9027    Therefore, we use "conditional-expression" instead.  */
9028
9029 static tree
9030 cp_parser_template_argument (cp_parser* parser)
9031 {
9032   tree argument;
9033   bool template_p;
9034   bool address_p;
9035   bool maybe_type_id = false;
9036   cp_token *token;
9037   cp_id_kind idk;
9038
9039   /* There's really no way to know what we're looking at, so we just
9040      try each alternative in order.
9041
9042        [temp.arg]
9043
9044        In a template-argument, an ambiguity between a type-id and an
9045        expression is resolved to a type-id, regardless of the form of
9046        the corresponding template-parameter.
9047
9048      Therefore, we try a type-id first.  */
9049   cp_parser_parse_tentatively (parser);
9050   argument = cp_parser_type_id (parser);
9051   /* If there was no error parsing the type-id but the next token is a '>>',
9052      we probably found a typo for '> >'. But there are type-id which are
9053      also valid expressions. For instance:
9054
9055      struct X { int operator >> (int); };
9056      template <int V> struct Foo {};
9057      Foo<X () >> 5> r;
9058
9059      Here 'X()' is a valid type-id of a function type, but the user just
9060      wanted to write the expression "X() >> 5". Thus, we remember that we
9061      found a valid type-id, but we still try to parse the argument as an
9062      expression to see what happens.  */
9063   if (!cp_parser_error_occurred (parser)
9064       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9065     {
9066       maybe_type_id = true;
9067       cp_parser_abort_tentative_parse (parser);
9068     }
9069   else
9070     {
9071       /* If the next token isn't a `,' or a `>', then this argument wasn't
9072       really finished. This means that the argument is not a valid
9073       type-id.  */
9074       if (!cp_parser_next_token_ends_template_argument_p (parser))
9075         cp_parser_error (parser, "expected template-argument");
9076       /* If that worked, we're done.  */
9077       if (cp_parser_parse_definitely (parser))
9078         return argument;
9079     }
9080   /* We're still not sure what the argument will be.  */
9081   cp_parser_parse_tentatively (parser);
9082   /* Try a template.  */
9083   argument = cp_parser_id_expression (parser,
9084                                       /*template_keyword_p=*/false,
9085                                       /*check_dependency_p=*/true,
9086                                       &template_p,
9087                                       /*declarator_p=*/false);
9088   /* If the next token isn't a `,' or a `>', then this argument wasn't
9089      really finished.  */
9090   if (!cp_parser_next_token_ends_template_argument_p (parser))
9091     cp_parser_error (parser, "expected template-argument");
9092   if (!cp_parser_error_occurred (parser))
9093     {
9094       /* Figure out what is being referred to.  If the id-expression
9095          was for a class template specialization, then we will have a
9096          TYPE_DECL at this point.  There is no need to do name lookup
9097          at this point in that case.  */
9098       if (TREE_CODE (argument) != TYPE_DECL)
9099         argument = cp_parser_lookup_name (parser, argument,
9100                                           none_type,
9101                                           /*is_template=*/template_p,
9102                                           /*is_namespace=*/false,
9103                                           /*check_dependency=*/true,
9104                                           /*ambiguous_decls=*/NULL);
9105       if (TREE_CODE (argument) != TEMPLATE_DECL
9106           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9107         cp_parser_error (parser, "expected template-name");
9108     }
9109   if (cp_parser_parse_definitely (parser))
9110     return argument;
9111   /* It must be a non-type argument.  There permitted cases are given
9112      in [temp.arg.nontype]:
9113
9114      -- an integral constant-expression of integral or enumeration
9115         type; or
9116
9117      -- the name of a non-type template-parameter; or
9118
9119      -- the name of an object or function with external linkage...
9120
9121      -- the address of an object or function with external linkage...
9122
9123      -- a pointer to member...  */
9124   /* Look for a non-type template parameter.  */
9125   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9126     {
9127       cp_parser_parse_tentatively (parser);
9128       argument = cp_parser_primary_expression (parser,
9129                                                /*adress_p=*/false,
9130                                                /*cast_p=*/false,
9131                                                /*template_arg_p=*/true,
9132                                                &idk);
9133       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9134           || !cp_parser_next_token_ends_template_argument_p (parser))
9135         cp_parser_simulate_error (parser);
9136       if (cp_parser_parse_definitely (parser))
9137         return argument;
9138     }
9139
9140   /* If the next token is "&", the argument must be the address of an
9141      object or function with external linkage.  */
9142   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9143   if (address_p)
9144     cp_lexer_consume_token (parser->lexer);
9145   /* See if we might have an id-expression.  */
9146   token = cp_lexer_peek_token (parser->lexer);
9147   if (token->type == CPP_NAME
9148       || token->keyword == RID_OPERATOR
9149       || token->type == CPP_SCOPE
9150       || token->type == CPP_TEMPLATE_ID
9151       || token->type == CPP_NESTED_NAME_SPECIFIER)
9152     {
9153       cp_parser_parse_tentatively (parser);
9154       argument = cp_parser_primary_expression (parser,
9155                                                address_p,
9156                                                /*cast_p=*/false,
9157                                                /*template_arg_p=*/true,
9158                                                &idk);
9159       if (cp_parser_error_occurred (parser)
9160           || !cp_parser_next_token_ends_template_argument_p (parser))
9161         cp_parser_abort_tentative_parse (parser);
9162       else
9163         {
9164           if (TREE_CODE (argument) == INDIRECT_REF)
9165             {
9166               gcc_assert (REFERENCE_REF_P (argument));
9167               argument = TREE_OPERAND (argument, 0);
9168             }
9169
9170           if (TREE_CODE (argument) == BASELINK)
9171             /* We don't need the information about what class was used
9172                to name the overloaded functions.  */  
9173             argument = BASELINK_FUNCTIONS (argument);
9174
9175           if (TREE_CODE (argument) == VAR_DECL)
9176             {
9177               /* A variable without external linkage might still be a
9178                  valid constant-expression, so no error is issued here
9179                  if the external-linkage check fails.  */
9180               if (!DECL_EXTERNAL_LINKAGE_P (argument))
9181                 cp_parser_simulate_error (parser);
9182             }
9183           else if (is_overloaded_fn (argument))
9184             /* All overloaded functions are allowed; if the external
9185                linkage test does not pass, an error will be issued
9186                later.  */
9187             ;
9188           else if (address_p
9189                    && (TREE_CODE (argument) == OFFSET_REF
9190                        || TREE_CODE (argument) == SCOPE_REF))
9191             /* A pointer-to-member.  */
9192             ;
9193           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9194             ;
9195           else
9196             cp_parser_simulate_error (parser);
9197
9198           if (cp_parser_parse_definitely (parser))
9199             {
9200               if (address_p)
9201                 argument = build_x_unary_op (ADDR_EXPR, argument);
9202               return argument;
9203             }
9204         }
9205     }
9206   /* If the argument started with "&", there are no other valid
9207      alternatives at this point.  */
9208   if (address_p)
9209     {
9210       cp_parser_error (parser, "invalid non-type template argument");
9211       return error_mark_node;
9212     }
9213
9214   /* If the argument wasn't successfully parsed as a type-id followed
9215      by '>>', the argument can only be a constant expression now.
9216      Otherwise, we try parsing the constant-expression tentatively,
9217      because the argument could really be a type-id.  */
9218   if (maybe_type_id)
9219     cp_parser_parse_tentatively (parser);
9220   argument = cp_parser_constant_expression (parser,
9221                                             /*allow_non_constant_p=*/false,
9222                                             /*non_constant_p=*/NULL);
9223   argument = fold_non_dependent_expr (argument);
9224   if (!maybe_type_id)
9225     return argument;
9226   if (!cp_parser_next_token_ends_template_argument_p (parser))
9227     cp_parser_error (parser, "expected template-argument");
9228   if (cp_parser_parse_definitely (parser))
9229     return argument;
9230   /* We did our best to parse the argument as a non type-id, but that
9231      was the only alternative that matched (albeit with a '>' after
9232      it). We can assume it's just a typo from the user, and a
9233      diagnostic will then be issued.  */
9234   return cp_parser_type_id (parser);
9235 }
9236
9237 /* Parse an explicit-instantiation.
9238
9239    explicit-instantiation:
9240      template declaration
9241
9242    Although the standard says `declaration', what it really means is:
9243
9244    explicit-instantiation:
9245      template decl-specifier-seq [opt] declarator [opt] ;
9246
9247    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9248    supposed to be allowed.  A defect report has been filed about this
9249    issue.
9250
9251    GNU Extension:
9252
9253    explicit-instantiation:
9254      storage-class-specifier template
9255        decl-specifier-seq [opt] declarator [opt] ;
9256      function-specifier template
9257        decl-specifier-seq [opt] declarator [opt] ;  */
9258
9259 static void
9260 cp_parser_explicit_instantiation (cp_parser* parser)
9261 {
9262   int declares_class_or_enum;
9263   cp_decl_specifier_seq decl_specifiers;
9264   tree extension_specifier = NULL_TREE;
9265
9266   /* Look for an (optional) storage-class-specifier or
9267      function-specifier.  */
9268   if (cp_parser_allow_gnu_extensions_p (parser))
9269     {
9270       extension_specifier
9271         = cp_parser_storage_class_specifier_opt (parser);
9272       if (!extension_specifier)
9273         extension_specifier
9274           = cp_parser_function_specifier_opt (parser,
9275                                               /*decl_specs=*/NULL);
9276     }
9277
9278   /* Look for the `template' keyword.  */
9279   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9280   /* Let the front end know that we are processing an explicit
9281      instantiation.  */
9282   begin_explicit_instantiation ();
9283   /* [temp.explicit] says that we are supposed to ignore access
9284      control while processing explicit instantiation directives.  */
9285   push_deferring_access_checks (dk_no_check);
9286   /* Parse a decl-specifier-seq.  */
9287   cp_parser_decl_specifier_seq (parser,
9288                                 CP_PARSER_FLAGS_OPTIONAL,
9289                                 &decl_specifiers,
9290                                 &declares_class_or_enum);
9291   /* If there was exactly one decl-specifier, and it declared a class,
9292      and there's no declarator, then we have an explicit type
9293      instantiation.  */
9294   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9295     {
9296       tree type;
9297
9298       type = check_tag_decl (&decl_specifiers);
9299       /* Turn access control back on for names used during
9300          template instantiation.  */
9301       pop_deferring_access_checks ();
9302       if (type)
9303         do_type_instantiation (type, extension_specifier,
9304                                /*complain=*/tf_error);
9305     }
9306   else
9307     {
9308       cp_declarator *declarator;
9309       tree decl;
9310
9311       /* Parse the declarator.  */
9312       declarator
9313         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9314                                 /*ctor_dtor_or_conv_p=*/NULL,
9315                                 /*parenthesized_p=*/NULL,
9316                                 /*member_p=*/false);
9317       if (declares_class_or_enum & 2)
9318         cp_parser_check_for_definition_in_return_type (declarator,
9319                                                        decl_specifiers.type);
9320       if (declarator != cp_error_declarator)
9321         {
9322           decl = grokdeclarator (declarator, &decl_specifiers,
9323                                  NORMAL, 0, NULL);
9324           /* Turn access control back on for names used during
9325              template instantiation.  */
9326           pop_deferring_access_checks ();
9327           /* Do the explicit instantiation.  */
9328           do_decl_instantiation (decl, extension_specifier);
9329         }
9330       else
9331         {
9332           pop_deferring_access_checks ();
9333           /* Skip the body of the explicit instantiation.  */
9334           cp_parser_skip_to_end_of_statement (parser);
9335         }
9336     }
9337   /* We're done with the instantiation.  */
9338   end_explicit_instantiation ();
9339
9340   cp_parser_consume_semicolon_at_end_of_statement (parser);
9341 }
9342
9343 /* Parse an explicit-specialization.
9344
9345    explicit-specialization:
9346      template < > declaration
9347
9348    Although the standard says `declaration', what it really means is:
9349
9350    explicit-specialization:
9351      template <> decl-specifier [opt] init-declarator [opt] ;
9352      template <> function-definition
9353      template <> explicit-specialization
9354      template <> template-declaration  */
9355
9356 static void
9357 cp_parser_explicit_specialization (cp_parser* parser)
9358 {
9359   bool need_lang_pop;
9360   /* Look for the `template' keyword.  */
9361   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9362   /* Look for the `<'.  */
9363   cp_parser_require (parser, CPP_LESS, "`<'");
9364   /* Look for the `>'.  */
9365   cp_parser_require (parser, CPP_GREATER, "`>'");
9366   /* We have processed another parameter list.  */
9367   ++parser->num_template_parameter_lists;
9368   /* [temp]
9369    
9370      A template ... explicit specialization ... shall not have C
9371      linkage.  */ 
9372   if (current_lang_name == lang_name_c)
9373     {
9374       error ("template specialization with C linkage");
9375       /* Give it C++ linkage to avoid confusing other parts of the
9376          front end.  */
9377       push_lang_context (lang_name_cplusplus);
9378       need_lang_pop = true;
9379     }
9380   else
9381     need_lang_pop = false;
9382   /* Let the front end know that we are beginning a specialization.  */
9383   begin_specialization ();
9384   /* If the next keyword is `template', we need to figure out whether
9385      or not we're looking a template-declaration.  */
9386   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9387     {
9388       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9389           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9390         cp_parser_template_declaration_after_export (parser,
9391                                                      /*member_p=*/false);
9392       else
9393         cp_parser_explicit_specialization (parser);
9394     }
9395   else
9396     /* Parse the dependent declaration.  */
9397     cp_parser_single_declaration (parser,
9398                                   /*member_p=*/false,
9399                                   /*friend_p=*/NULL);
9400   /* We're done with the specialization.  */
9401   end_specialization ();
9402   /* For the erroneous case of a template with C linkage, we pushed an
9403      implicit C++ linkage scope; exit that scope now.  */
9404   if (need_lang_pop)
9405     pop_lang_context ();
9406   /* We're done with this parameter list.  */
9407   --parser->num_template_parameter_lists;
9408 }
9409
9410 /* Parse a type-specifier.
9411
9412    type-specifier:
9413      simple-type-specifier
9414      class-specifier
9415      enum-specifier
9416      elaborated-type-specifier
9417      cv-qualifier
9418
9419    GNU Extension:
9420
9421    type-specifier:
9422      __complex__
9423
9424    Returns a representation of the type-specifier.  For a
9425    class-specifier, enum-specifier, or elaborated-type-specifier, a
9426    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9427
9428    The parser flags FLAGS is used to control type-specifier parsing.
9429
9430    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9431    in a decl-specifier-seq.
9432
9433    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9434    class-specifier, enum-specifier, or elaborated-type-specifier, then
9435    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9436    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9437    zero.
9438
9439    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9440    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9441    is set to FALSE.  */
9442
9443 static tree
9444 cp_parser_type_specifier (cp_parser* parser,
9445                           cp_parser_flags flags,
9446                           cp_decl_specifier_seq *decl_specs,
9447                           bool is_declaration,
9448                           int* declares_class_or_enum,
9449                           bool* is_cv_qualifier)
9450 {
9451   tree type_spec = NULL_TREE;
9452   cp_token *token;
9453   enum rid keyword;
9454   cp_decl_spec ds = ds_last;
9455
9456   /* Assume this type-specifier does not declare a new type.  */
9457   if (declares_class_or_enum)
9458     *declares_class_or_enum = 0;
9459   /* And that it does not specify a cv-qualifier.  */
9460   if (is_cv_qualifier)
9461     *is_cv_qualifier = false;
9462   /* Peek at the next token.  */
9463   token = cp_lexer_peek_token (parser->lexer);
9464
9465   /* If we're looking at a keyword, we can use that to guide the
9466      production we choose.  */
9467   keyword = token->keyword;
9468   switch (keyword)
9469     {
9470     case RID_ENUM:
9471       /* 'enum' [identifier] '{' introduces an enum-specifier;
9472          'enum' <anything else> introduces an elaborated-type-specifier.  */
9473       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9474           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9475               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9476                  == CPP_OPEN_BRACE))
9477         {
9478           if (parser->num_template_parameter_lists)
9479             {
9480               error ("template declaration of %qs", "enum");
9481               cp_parser_skip_to_end_of_block_or_statement (parser);
9482               type_spec = error_mark_node;
9483             }
9484           else
9485             type_spec = cp_parser_enum_specifier (parser);
9486
9487           if (declares_class_or_enum)
9488             *declares_class_or_enum = 2;
9489           if (decl_specs)
9490             cp_parser_set_decl_spec_type (decl_specs,
9491                                           type_spec,
9492                                           /*user_defined_p=*/true);
9493           return type_spec;
9494         }
9495       else
9496         goto elaborated_type_specifier;
9497
9498       /* Any of these indicate either a class-specifier, or an
9499          elaborated-type-specifier.  */
9500     case RID_CLASS:
9501     case RID_STRUCT:
9502     case RID_UNION:
9503       /* Parse tentatively so that we can back up if we don't find a
9504          class-specifier.  */
9505       cp_parser_parse_tentatively (parser);
9506       /* Look for the class-specifier.  */
9507       type_spec = cp_parser_class_specifier (parser);
9508       /* If that worked, we're done.  */
9509       if (cp_parser_parse_definitely (parser))
9510         {
9511           if (declares_class_or_enum)
9512             *declares_class_or_enum = 2;
9513           if (decl_specs)
9514             cp_parser_set_decl_spec_type (decl_specs,
9515                                           type_spec,
9516                                           /*user_defined_p=*/true);
9517           return type_spec;
9518         }
9519
9520       /* Fall through.  */
9521     elaborated_type_specifier:
9522       /* We're declaring (not defining) a class or enum.  */
9523       if (declares_class_or_enum)
9524         *declares_class_or_enum = 1;
9525
9526       /* Fall through.  */
9527     case RID_TYPENAME:
9528       /* Look for an elaborated-type-specifier.  */
9529       type_spec
9530         = (cp_parser_elaborated_type_specifier
9531            (parser,
9532             decl_specs && decl_specs->specs[(int) ds_friend],
9533             is_declaration));
9534       if (decl_specs)
9535         cp_parser_set_decl_spec_type (decl_specs,
9536                                       type_spec,
9537                                       /*user_defined_p=*/true);
9538       return type_spec;
9539
9540     case RID_CONST:
9541       ds = ds_const;
9542       if (is_cv_qualifier)
9543         *is_cv_qualifier = true;
9544       break;
9545
9546     case RID_VOLATILE:
9547       ds = ds_volatile;
9548       if (is_cv_qualifier)
9549         *is_cv_qualifier = true;
9550       break;
9551
9552     case RID_RESTRICT:
9553       ds = ds_restrict;
9554       if (is_cv_qualifier)
9555         *is_cv_qualifier = true;
9556       break;
9557
9558     case RID_COMPLEX:
9559       /* The `__complex__' keyword is a GNU extension.  */
9560       ds = ds_complex;
9561       break;
9562
9563     default:
9564       break;
9565     }
9566
9567   /* Handle simple keywords.  */
9568   if (ds != ds_last)
9569     {
9570       if (decl_specs)
9571         {
9572           ++decl_specs->specs[(int)ds];
9573           decl_specs->any_specifiers_p = true;
9574         }
9575       return cp_lexer_consume_token (parser->lexer)->value;
9576     }
9577
9578   /* If we do not already have a type-specifier, assume we are looking
9579      at a simple-type-specifier.  */
9580   type_spec = cp_parser_simple_type_specifier (parser,
9581                                                decl_specs,
9582                                                flags);
9583
9584   /* If we didn't find a type-specifier, and a type-specifier was not
9585      optional in this context, issue an error message.  */
9586   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9587     {
9588       cp_parser_error (parser, "expected type specifier");
9589       return error_mark_node;
9590     }
9591
9592   return type_spec;
9593 }
9594
9595 /* Parse a simple-type-specifier.
9596
9597    simple-type-specifier:
9598      :: [opt] nested-name-specifier [opt] type-name
9599      :: [opt] nested-name-specifier template template-id
9600      char
9601      wchar_t
9602      bool
9603      short
9604      int
9605      long
9606      signed
9607      unsigned
9608      float
9609      double
9610      void
9611
9612    GNU Extension:
9613
9614    simple-type-specifier:
9615      __typeof__ unary-expression
9616      __typeof__ ( type-id )
9617
9618    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9619    appropriately updated.  */
9620
9621 static tree
9622 cp_parser_simple_type_specifier (cp_parser* parser,
9623                                  cp_decl_specifier_seq *decl_specs,
9624                                  cp_parser_flags flags)
9625 {
9626   tree type = NULL_TREE;
9627   cp_token *token;
9628
9629   /* Peek at the next token.  */
9630   token = cp_lexer_peek_token (parser->lexer);
9631
9632   /* If we're looking at a keyword, things are easy.  */
9633   switch (token->keyword)
9634     {
9635     case RID_CHAR:
9636       if (decl_specs)
9637         decl_specs->explicit_char_p = true;
9638       type = char_type_node;
9639       break;
9640     case RID_WCHAR:
9641       type = wchar_type_node;
9642       break;
9643     case RID_BOOL:
9644       type = boolean_type_node;
9645       break;
9646     case RID_SHORT:
9647       if (decl_specs)
9648         ++decl_specs->specs[(int) ds_short];
9649       type = short_integer_type_node;
9650       break;
9651     case RID_INT:
9652       if (decl_specs)
9653         decl_specs->explicit_int_p = true;
9654       type = integer_type_node;
9655       break;
9656     case RID_LONG:
9657       if (decl_specs)
9658         ++decl_specs->specs[(int) ds_long];
9659       type = long_integer_type_node;
9660       break;
9661     case RID_SIGNED:
9662       if (decl_specs)
9663         ++decl_specs->specs[(int) ds_signed];
9664       type = integer_type_node;
9665       break;
9666     case RID_UNSIGNED:
9667       if (decl_specs)
9668         ++decl_specs->specs[(int) ds_unsigned];
9669       type = unsigned_type_node;
9670       break;
9671     case RID_FLOAT:
9672       type = float_type_node;
9673       break;
9674     case RID_DOUBLE:
9675       type = double_type_node;
9676       break;
9677     case RID_VOID:
9678       type = void_type_node;
9679       break;
9680
9681     case RID_TYPEOF:
9682       /* Consume the `typeof' token.  */
9683       cp_lexer_consume_token (parser->lexer);
9684       /* Parse the operand to `typeof'.  */
9685       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9686       /* If it is not already a TYPE, take its type.  */
9687       if (!TYPE_P (type))
9688         type = finish_typeof (type);
9689
9690       if (decl_specs)
9691         cp_parser_set_decl_spec_type (decl_specs, type,
9692                                       /*user_defined_p=*/true);
9693
9694       return type;
9695
9696     default:
9697       break;
9698     }
9699
9700   /* If the type-specifier was for a built-in type, we're done.  */
9701   if (type)
9702     {
9703       tree id;
9704
9705       /* Record the type.  */
9706       if (decl_specs
9707           && (token->keyword != RID_SIGNED
9708               && token->keyword != RID_UNSIGNED
9709               && token->keyword != RID_SHORT
9710               && token->keyword != RID_LONG))
9711         cp_parser_set_decl_spec_type (decl_specs,
9712                                       type,
9713                                       /*user_defined=*/false);
9714       if (decl_specs)
9715         decl_specs->any_specifiers_p = true;
9716
9717       /* Consume the token.  */
9718       id = cp_lexer_consume_token (parser->lexer)->value;
9719
9720       /* There is no valid C++ program where a non-template type is
9721          followed by a "<".  That usually indicates that the user thought
9722          that the type was a template.  */
9723       cp_parser_check_for_invalid_template_id (parser, type);
9724
9725       return TYPE_NAME (type);
9726     }
9727
9728   /* The type-specifier must be a user-defined type.  */
9729   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9730     {
9731       bool qualified_p;
9732       bool global_p;
9733
9734       /* Don't gobble tokens or issue error messages if this is an
9735          optional type-specifier.  */
9736       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9737         cp_parser_parse_tentatively (parser);
9738
9739       /* Look for the optional `::' operator.  */
9740       global_p
9741         = (cp_parser_global_scope_opt (parser,
9742                                        /*current_scope_valid_p=*/false)
9743            != NULL_TREE);
9744       /* Look for the nested-name specifier.  */
9745       qualified_p
9746         = (cp_parser_nested_name_specifier_opt (parser,
9747                                                 /*typename_keyword_p=*/false,
9748                                                 /*check_dependency_p=*/true,
9749                                                 /*type_p=*/false,
9750                                                 /*is_declaration=*/false)
9751            != NULL_TREE);
9752       /* If we have seen a nested-name-specifier, and the next token
9753          is `template', then we are using the template-id production.  */
9754       if (parser->scope
9755           && cp_parser_optional_template_keyword (parser))
9756         {
9757           /* Look for the template-id.  */
9758           type = cp_parser_template_id (parser,
9759                                         /*template_keyword_p=*/true,
9760                                         /*check_dependency_p=*/true,
9761                                         /*is_declaration=*/false);
9762           /* If the template-id did not name a type, we are out of
9763              luck.  */
9764           if (TREE_CODE (type) != TYPE_DECL)
9765             {
9766               cp_parser_error (parser, "expected template-id for type");
9767               type = NULL_TREE;
9768             }
9769         }
9770       /* Otherwise, look for a type-name.  */
9771       else
9772         type = cp_parser_type_name (parser);
9773       /* Keep track of all name-lookups performed in class scopes.  */
9774       if (type
9775           && !global_p
9776           && !qualified_p
9777           && TREE_CODE (type) == TYPE_DECL
9778           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9779         maybe_note_name_used_in_class (DECL_NAME (type), type);
9780       /* If it didn't work out, we don't have a TYPE.  */
9781       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9782           && !cp_parser_parse_definitely (parser))
9783         type = NULL_TREE;
9784       if (type && decl_specs)
9785         cp_parser_set_decl_spec_type (decl_specs, type,
9786                                       /*user_defined=*/true);
9787     }
9788
9789   /* If we didn't get a type-name, issue an error message.  */
9790   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9791     {
9792       cp_parser_error (parser, "expected type-name");
9793       return error_mark_node;
9794     }
9795
9796   /* There is no valid C++ program where a non-template type is
9797      followed by a "<".  That usually indicates that the user thought
9798      that the type was a template.  */
9799   if (type && type != error_mark_node)
9800     {
9801       /* As a last-ditch effort, see if TYPE is an Objective-C type.
9802          If it is, then the '<'...'>' enclose protocol names rather than
9803          template arguments, and so everything is fine.  */
9804       if (c_dialect_objc ()
9805           && (objc_is_id (type) || objc_is_class_name (type)))
9806         {
9807           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9808           tree qual_type = objc_get_protocol_qualified_type (type, protos);
9809
9810           /* Clobber the "unqualified" type previously entered into
9811              DECL_SPECS with the new, improved protocol-qualified version.  */
9812           if (decl_specs)
9813             decl_specs->type = qual_type;
9814
9815           return qual_type;
9816         }
9817
9818       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9819     }
9820
9821   return type;
9822 }
9823
9824 /* Parse a type-name.
9825
9826    type-name:
9827      class-name
9828      enum-name
9829      typedef-name
9830
9831    enum-name:
9832      identifier
9833
9834    typedef-name:
9835      identifier
9836
9837    Returns a TYPE_DECL for the type.  */
9838
9839 static tree
9840 cp_parser_type_name (cp_parser* parser)
9841 {
9842   tree type_decl;
9843   tree identifier;
9844
9845   /* We can't know yet whether it is a class-name or not.  */
9846   cp_parser_parse_tentatively (parser);
9847   /* Try a class-name.  */
9848   type_decl = cp_parser_class_name (parser,
9849                                     /*typename_keyword_p=*/false,
9850                                     /*template_keyword_p=*/false,
9851                                     none_type,
9852                                     /*check_dependency_p=*/true,
9853                                     /*class_head_p=*/false,
9854                                     /*is_declaration=*/false);
9855   /* If it's not a class-name, keep looking.  */
9856   if (!cp_parser_parse_definitely (parser))
9857     {
9858       /* It must be a typedef-name or an enum-name.  */
9859       identifier = cp_parser_identifier (parser);
9860       if (identifier == error_mark_node)
9861         return error_mark_node;
9862
9863       /* Look up the type-name.  */
9864       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9865
9866       if (TREE_CODE (type_decl) != TYPE_DECL
9867           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9868         {
9869           /* See if this is an Objective-C type.  */
9870           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9871           tree type = objc_get_protocol_qualified_type (identifier, protos);
9872           if (type)
9873             type_decl = TYPE_NAME (type);
9874         }
9875
9876       /* Issue an error if we did not find a type-name.  */
9877       if (TREE_CODE (type_decl) != TYPE_DECL)
9878         {
9879           if (!cp_parser_simulate_error (parser))
9880             cp_parser_name_lookup_error (parser, identifier, type_decl,
9881                                          "is not a type");
9882           type_decl = error_mark_node;
9883         }
9884       /* Remember that the name was used in the definition of the
9885          current class so that we can check later to see if the
9886          meaning would have been different after the class was
9887          entirely defined.  */
9888       else if (type_decl != error_mark_node
9889                && !parser->scope)
9890         maybe_note_name_used_in_class (identifier, type_decl);
9891     }
9892
9893   return type_decl;
9894 }
9895
9896
9897 /* Parse an elaborated-type-specifier.  Note that the grammar given
9898    here incorporates the resolution to DR68.
9899
9900    elaborated-type-specifier:
9901      class-key :: [opt] nested-name-specifier [opt] identifier
9902      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9903      enum :: [opt] nested-name-specifier [opt] identifier
9904      typename :: [opt] nested-name-specifier identifier
9905      typename :: [opt] nested-name-specifier template [opt]
9906        template-id
9907
9908    GNU extension:
9909
9910    elaborated-type-specifier:
9911      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9912      class-key attributes :: [opt] nested-name-specifier [opt]
9913                template [opt] template-id
9914      enum attributes :: [opt] nested-name-specifier [opt] identifier
9915
9916    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9917    declared `friend'.  If IS_DECLARATION is TRUE, then this
9918    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9919    something is being declared.
9920
9921    Returns the TYPE specified.  */
9922
9923 static tree
9924 cp_parser_elaborated_type_specifier (cp_parser* parser,
9925                                      bool is_friend,
9926                                      bool is_declaration)
9927 {
9928   enum tag_types tag_type;
9929   tree identifier;
9930   tree type = NULL_TREE;
9931   tree attributes = NULL_TREE;
9932
9933   /* See if we're looking at the `enum' keyword.  */
9934   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9935     {
9936       /* Consume the `enum' token.  */
9937       cp_lexer_consume_token (parser->lexer);
9938       /* Remember that it's an enumeration type.  */
9939       tag_type = enum_type;
9940       /* Parse the attributes.  */
9941       attributes = cp_parser_attributes_opt (parser);
9942     }
9943   /* Or, it might be `typename'.  */
9944   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9945                                            RID_TYPENAME))
9946     {
9947       /* Consume the `typename' token.  */
9948       cp_lexer_consume_token (parser->lexer);
9949       /* Remember that it's a `typename' type.  */
9950       tag_type = typename_type;
9951       /* The `typename' keyword is only allowed in templates.  */
9952       if (!processing_template_decl)
9953         pedwarn ("using %<typename%> outside of template");
9954     }
9955   /* Otherwise it must be a class-key.  */
9956   else
9957     {
9958       tag_type = cp_parser_class_key (parser);
9959       if (tag_type == none_type)
9960         return error_mark_node;
9961       /* Parse the attributes.  */
9962       attributes = cp_parser_attributes_opt (parser);
9963     }
9964
9965   /* Look for the `::' operator.  */
9966   cp_parser_global_scope_opt (parser,
9967                               /*current_scope_valid_p=*/false);
9968   /* Look for the nested-name-specifier.  */
9969   if (tag_type == typename_type)
9970     {
9971       if (!cp_parser_nested_name_specifier (parser,
9972                                            /*typename_keyword_p=*/true,
9973                                            /*check_dependency_p=*/true,
9974                                            /*type_p=*/true,
9975                                             is_declaration))
9976         return error_mark_node;
9977     }
9978   else
9979     /* Even though `typename' is not present, the proposed resolution
9980        to Core Issue 180 says that in `class A<T>::B', `B' should be
9981        considered a type-name, even if `A<T>' is dependent.  */
9982     cp_parser_nested_name_specifier_opt (parser,
9983                                          /*typename_keyword_p=*/true,
9984                                          /*check_dependency_p=*/true,
9985                                          /*type_p=*/true,
9986                                          is_declaration);
9987   /* For everything but enumeration types, consider a template-id.  */
9988   if (tag_type != enum_type)
9989     {
9990       bool template_p = false;
9991       tree decl;
9992
9993       /* Allow the `template' keyword.  */
9994       template_p = cp_parser_optional_template_keyword (parser);
9995       /* If we didn't see `template', we don't know if there's a
9996          template-id or not.  */
9997       if (!template_p)
9998         cp_parser_parse_tentatively (parser);
9999       /* Parse the template-id.  */
10000       decl = cp_parser_template_id (parser, template_p,
10001                                     /*check_dependency_p=*/true,
10002                                     is_declaration);
10003       /* If we didn't find a template-id, look for an ordinary
10004          identifier.  */
10005       if (!template_p && !cp_parser_parse_definitely (parser))
10006         ;
10007       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10008          in effect, then we must assume that, upon instantiation, the
10009          template will correspond to a class.  */
10010       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10011                && tag_type == typename_type)
10012         type = make_typename_type (parser->scope, decl,
10013                                    typename_type,
10014                                    /*complain=*/tf_error);
10015       else
10016         type = TREE_TYPE (decl);
10017     }
10018
10019   /* For an enumeration type, consider only a plain identifier.  */
10020   if (!type)
10021     {
10022       identifier = cp_parser_identifier (parser);
10023
10024       if (identifier == error_mark_node)
10025         {
10026           parser->scope = NULL_TREE;
10027           return error_mark_node;
10028         }
10029
10030       /* For a `typename', we needn't call xref_tag.  */
10031       if (tag_type == typename_type
10032           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10033         return cp_parser_make_typename_type (parser, parser->scope,
10034                                              identifier);
10035       /* Look up a qualified name in the usual way.  */
10036       if (parser->scope)
10037         {
10038           tree decl;
10039
10040           decl = cp_parser_lookup_name (parser, identifier,
10041                                         tag_type,
10042                                         /*is_template=*/false,
10043                                         /*is_namespace=*/false,
10044                                         /*check_dependency=*/true,
10045                                         /*ambiguous_decls=*/NULL);
10046
10047           /* If we are parsing friend declaration, DECL may be a
10048              TEMPLATE_DECL tree node here.  However, we need to check
10049              whether this TEMPLATE_DECL results in valid code.  Consider
10050              the following example:
10051
10052                namespace N {
10053                  template <class T> class C {};
10054                }
10055                class X {
10056                  template <class T> friend class N::C; // #1, valid code
10057                };
10058                template <class T> class Y {
10059                  friend class N::C;                    // #2, invalid code
10060                };
10061
10062              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10063              name lookup of `N::C'.  We see that friend declaration must
10064              be template for the code to be valid.  Note that
10065              processing_template_decl does not work here since it is
10066              always 1 for the above two cases.  */
10067
10068           decl = (cp_parser_maybe_treat_template_as_class
10069                   (decl, /*tag_name_p=*/is_friend
10070                          && parser->num_template_parameter_lists));
10071
10072           if (TREE_CODE (decl) != TYPE_DECL)
10073             {
10074               cp_parser_diagnose_invalid_type_name (parser,
10075                                                     parser->scope,
10076                                                     identifier);
10077               return error_mark_node;
10078             }
10079
10080           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10081             check_elaborated_type_specifier
10082               (tag_type, decl,
10083                (parser->num_template_parameter_lists
10084                 || DECL_SELF_REFERENCE_P (decl)));
10085
10086           type = TREE_TYPE (decl);
10087         }
10088       else
10089         {
10090           /* An elaborated-type-specifier sometimes introduces a new type and
10091              sometimes names an existing type.  Normally, the rule is that it
10092              introduces a new type only if there is not an existing type of
10093              the same name already in scope.  For example, given:
10094
10095                struct S {};
10096                void f() { struct S s; }
10097
10098              the `struct S' in the body of `f' is the same `struct S' as in
10099              the global scope; the existing definition is used.  However, if
10100              there were no global declaration, this would introduce a new
10101              local class named `S'.
10102
10103              An exception to this rule applies to the following code:
10104
10105                namespace N { struct S; }
10106
10107              Here, the elaborated-type-specifier names a new type
10108              unconditionally; even if there is already an `S' in the
10109              containing scope this declaration names a new type.
10110              This exception only applies if the elaborated-type-specifier
10111              forms the complete declaration:
10112
10113                [class.name]
10114
10115                A declaration consisting solely of `class-key identifier ;' is
10116                either a redeclaration of the name in the current scope or a
10117                forward declaration of the identifier as a class name.  It
10118                introduces the name into the current scope.
10119
10120              We are in this situation precisely when the next token is a `;'.
10121
10122              An exception to the exception is that a `friend' declaration does
10123              *not* name a new type; i.e., given:
10124
10125                struct S { friend struct T; };
10126
10127              `T' is not a new type in the scope of `S'.
10128
10129              Also, `new struct S' or `sizeof (struct S)' never results in the
10130              definition of a new type; a new type can only be declared in a
10131              declaration context.  */
10132
10133           tag_scope ts;
10134           bool template_p;
10135
10136           if (is_friend)
10137             /* Friends have special name lookup rules.  */
10138             ts = ts_within_enclosing_non_class;
10139           else if (is_declaration
10140                    && cp_lexer_next_token_is (parser->lexer,
10141                                               CPP_SEMICOLON))
10142             /* This is a `class-key identifier ;' */
10143             ts = ts_current;
10144           else
10145             ts = ts_global;
10146
10147           /* Warn about attributes. They are ignored.  */
10148           if (attributes)
10149             warning (OPT_Wattributes,
10150                      "type attributes are honored only at type definition");
10151
10152           template_p = 
10153             (parser->num_template_parameter_lists
10154              && (cp_parser_next_token_starts_class_definition_p (parser)
10155                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10156           /* An unqualified name was used to reference this type, so
10157              there were no qualifying templates.  */
10158           if (!cp_parser_check_template_parameters (parser, 
10159                                                     /*num_templates=*/0))
10160             return error_mark_node;
10161           type = xref_tag (tag_type, identifier, ts, template_p);
10162         }
10163     }
10164   if (tag_type != enum_type)
10165     cp_parser_check_class_key (tag_type, type);
10166
10167   /* A "<" cannot follow an elaborated type specifier.  If that
10168      happens, the user was probably trying to form a template-id.  */
10169   cp_parser_check_for_invalid_template_id (parser, type);
10170
10171   return type;
10172 }
10173
10174 /* Parse an enum-specifier.
10175
10176    enum-specifier:
10177      enum identifier [opt] { enumerator-list [opt] }
10178
10179    GNU Extensions:
10180      enum identifier [opt] { enumerator-list [opt] } attributes
10181
10182    Returns an ENUM_TYPE representing the enumeration.  */
10183
10184 static tree
10185 cp_parser_enum_specifier (cp_parser* parser)
10186 {
10187   tree identifier;
10188   tree type;
10189
10190   /* Caller guarantees that the current token is 'enum', an identifier
10191      possibly follows, and the token after that is an opening brace.
10192      If we don't have an identifier, fabricate an anonymous name for
10193      the enumeration being defined.  */
10194   cp_lexer_consume_token (parser->lexer);
10195
10196   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10197     identifier = cp_parser_identifier (parser);
10198   else
10199     identifier = make_anon_name ();
10200
10201   /* Issue an error message if type-definitions are forbidden here.  */
10202   cp_parser_check_type_definition (parser);
10203
10204   /* Create the new type.  We do this before consuming the opening brace
10205      so the enum will be recorded as being on the line of its tag (or the
10206      'enum' keyword, if there is no tag).  */
10207   type = start_enum (identifier);
10208
10209   /* Consume the opening brace.  */
10210   cp_lexer_consume_token (parser->lexer);
10211
10212   /* If the next token is not '}', then there are some enumerators.  */
10213   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10214     cp_parser_enumerator_list (parser, type);
10215
10216   /* Consume the final '}'.  */
10217   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10218
10219   /* Look for trailing attributes to apply to this enumeration, and
10220      apply them if appropriate.  */
10221   if (cp_parser_allow_gnu_extensions_p (parser))
10222     {
10223       tree trailing_attr = cp_parser_attributes_opt (parser);
10224       cplus_decl_attributes (&type,
10225                              trailing_attr,
10226                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10227     }
10228
10229   /* Finish up the enumeration.  */
10230   finish_enum (type);
10231
10232   return type;
10233 }
10234
10235 /* Parse an enumerator-list.  The enumerators all have the indicated
10236    TYPE.
10237
10238    enumerator-list:
10239      enumerator-definition
10240      enumerator-list , enumerator-definition  */
10241
10242 static void
10243 cp_parser_enumerator_list (cp_parser* parser, tree type)
10244 {
10245   while (true)
10246     {
10247       /* Parse an enumerator-definition.  */
10248       cp_parser_enumerator_definition (parser, type);
10249
10250       /* If the next token is not a ',', we've reached the end of
10251          the list.  */
10252       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10253         break;
10254       /* Otherwise, consume the `,' and keep going.  */
10255       cp_lexer_consume_token (parser->lexer);
10256       /* If the next token is a `}', there is a trailing comma.  */
10257       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10258         {
10259           if (pedantic && !in_system_header)
10260             pedwarn ("comma at end of enumerator list");
10261           break;
10262         }
10263     }
10264 }
10265
10266 /* Parse an enumerator-definition.  The enumerator has the indicated
10267    TYPE.
10268
10269    enumerator-definition:
10270      enumerator
10271      enumerator = constant-expression
10272
10273    enumerator:
10274      identifier  */
10275
10276 static void
10277 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10278 {
10279   tree identifier;
10280   tree value;
10281
10282   /* Look for the identifier.  */
10283   identifier = cp_parser_identifier (parser);
10284   if (identifier == error_mark_node)
10285     return;
10286
10287   /* If the next token is an '=', then there is an explicit value.  */
10288   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10289     {
10290       /* Consume the `=' token.  */
10291       cp_lexer_consume_token (parser->lexer);
10292       /* Parse the value.  */
10293       value = cp_parser_constant_expression (parser,
10294                                              /*allow_non_constant_p=*/false,
10295                                              NULL);
10296     }
10297   else
10298     value = NULL_TREE;
10299
10300   /* Create the enumerator.  */
10301   build_enumerator (identifier, value, type);
10302 }
10303
10304 /* Parse a namespace-name.
10305
10306    namespace-name:
10307      original-namespace-name
10308      namespace-alias
10309
10310    Returns the NAMESPACE_DECL for the namespace.  */
10311
10312 static tree
10313 cp_parser_namespace_name (cp_parser* parser)
10314 {
10315   tree identifier;
10316   tree namespace_decl;
10317
10318   /* Get the name of the namespace.  */
10319   identifier = cp_parser_identifier (parser);
10320   if (identifier == error_mark_node)
10321     return error_mark_node;
10322
10323   /* Look up the identifier in the currently active scope.  Look only
10324      for namespaces, due to:
10325
10326        [basic.lookup.udir]
10327
10328        When looking up a namespace-name in a using-directive or alias
10329        definition, only namespace names are considered.
10330
10331      And:
10332
10333        [basic.lookup.qual]
10334
10335        During the lookup of a name preceding the :: scope resolution
10336        operator, object, function, and enumerator names are ignored.
10337
10338      (Note that cp_parser_class_or_namespace_name only calls this
10339      function if the token after the name is the scope resolution
10340      operator.)  */
10341   namespace_decl = cp_parser_lookup_name (parser, identifier,
10342                                           none_type,
10343                                           /*is_template=*/false,
10344                                           /*is_namespace=*/true,
10345                                           /*check_dependency=*/true,
10346                                           /*ambiguous_decls=*/NULL);
10347   /* If it's not a namespace, issue an error.  */
10348   if (namespace_decl == error_mark_node
10349       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10350     {
10351       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10352         error ("%qD is not a namespace-name", identifier);
10353       cp_parser_error (parser, "expected namespace-name");
10354       namespace_decl = error_mark_node;
10355     }
10356
10357   return namespace_decl;
10358 }
10359
10360 /* Parse a namespace-definition.
10361
10362    namespace-definition:
10363      named-namespace-definition
10364      unnamed-namespace-definition
10365
10366    named-namespace-definition:
10367      original-namespace-definition
10368      extension-namespace-definition
10369
10370    original-namespace-definition:
10371      namespace identifier { namespace-body }
10372
10373    extension-namespace-definition:
10374      namespace original-namespace-name { namespace-body }
10375
10376    unnamed-namespace-definition:
10377      namespace { namespace-body } */
10378
10379 static void
10380 cp_parser_namespace_definition (cp_parser* parser)
10381 {
10382   tree identifier;
10383
10384   /* Look for the `namespace' keyword.  */
10385   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10386
10387   /* Get the name of the namespace.  We do not attempt to distinguish
10388      between an original-namespace-definition and an
10389      extension-namespace-definition at this point.  The semantic
10390      analysis routines are responsible for that.  */
10391   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10392     identifier = cp_parser_identifier (parser);
10393   else
10394     identifier = NULL_TREE;
10395
10396   /* Look for the `{' to start the namespace.  */
10397   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10398   /* Start the namespace.  */
10399   push_namespace (identifier);
10400   /* Parse the body of the namespace.  */
10401   cp_parser_namespace_body (parser);
10402   /* Finish the namespace.  */
10403   pop_namespace ();
10404   /* Look for the final `}'.  */
10405   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10406 }
10407
10408 /* Parse a namespace-body.
10409
10410    namespace-body:
10411      declaration-seq [opt]  */
10412
10413 static void
10414 cp_parser_namespace_body (cp_parser* parser)
10415 {
10416   cp_parser_declaration_seq_opt (parser);
10417 }
10418
10419 /* Parse a namespace-alias-definition.
10420
10421    namespace-alias-definition:
10422      namespace identifier = qualified-namespace-specifier ;  */
10423
10424 static void
10425 cp_parser_namespace_alias_definition (cp_parser* parser)
10426 {
10427   tree identifier;
10428   tree namespace_specifier;
10429
10430   /* Look for the `namespace' keyword.  */
10431   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10432   /* Look for the identifier.  */
10433   identifier = cp_parser_identifier (parser);
10434   if (identifier == error_mark_node)
10435     return;
10436   /* Look for the `=' token.  */
10437   cp_parser_require (parser, CPP_EQ, "`='");
10438   /* Look for the qualified-namespace-specifier.  */
10439   namespace_specifier
10440     = cp_parser_qualified_namespace_specifier (parser);
10441   /* Look for the `;' token.  */
10442   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10443
10444   /* Register the alias in the symbol table.  */
10445   do_namespace_alias (identifier, namespace_specifier);
10446 }
10447
10448 /* Parse a qualified-namespace-specifier.
10449
10450    qualified-namespace-specifier:
10451      :: [opt] nested-name-specifier [opt] namespace-name
10452
10453    Returns a NAMESPACE_DECL corresponding to the specified
10454    namespace.  */
10455
10456 static tree
10457 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10458 {
10459   /* Look for the optional `::'.  */
10460   cp_parser_global_scope_opt (parser,
10461                               /*current_scope_valid_p=*/false);
10462
10463   /* Look for the optional nested-name-specifier.  */
10464   cp_parser_nested_name_specifier_opt (parser,
10465                                        /*typename_keyword_p=*/false,
10466                                        /*check_dependency_p=*/true,
10467                                        /*type_p=*/false,
10468                                        /*is_declaration=*/true);
10469
10470   return cp_parser_namespace_name (parser);
10471 }
10472
10473 /* Parse a using-declaration.
10474
10475    using-declaration:
10476      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10477      using :: unqualified-id ;  */
10478
10479 static void
10480 cp_parser_using_declaration (cp_parser* parser)
10481 {
10482   cp_token *token;
10483   bool typename_p = false;
10484   bool global_scope_p;
10485   tree decl;
10486   tree identifier;
10487   tree qscope;
10488
10489   /* Look for the `using' keyword.  */
10490   cp_parser_require_keyword (parser, RID_USING, "`using'");
10491
10492   /* Peek at the next token.  */
10493   token = cp_lexer_peek_token (parser->lexer);
10494   /* See if it's `typename'.  */
10495   if (token->keyword == RID_TYPENAME)
10496     {
10497       /* Remember that we've seen it.  */
10498       typename_p = true;
10499       /* Consume the `typename' token.  */
10500       cp_lexer_consume_token (parser->lexer);
10501     }
10502
10503   /* Look for the optional global scope qualification.  */
10504   global_scope_p
10505     = (cp_parser_global_scope_opt (parser,
10506                                    /*current_scope_valid_p=*/false)
10507        != NULL_TREE);
10508
10509   /* If we saw `typename', or didn't see `::', then there must be a
10510      nested-name-specifier present.  */
10511   if (typename_p || !global_scope_p)
10512     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10513                                               /*check_dependency_p=*/true,
10514                                               /*type_p=*/false,
10515                                               /*is_declaration=*/true);
10516   /* Otherwise, we could be in either of the two productions.  In that
10517      case, treat the nested-name-specifier as optional.  */
10518   else
10519     qscope = cp_parser_nested_name_specifier_opt (parser,
10520                                                   /*typename_keyword_p=*/false,
10521                                                   /*check_dependency_p=*/true,
10522                                                   /*type_p=*/false,
10523                                                   /*is_declaration=*/true);
10524   if (!qscope)
10525     qscope = global_namespace;
10526
10527   /* Parse the unqualified-id.  */
10528   identifier = cp_parser_unqualified_id (parser,
10529                                          /*template_keyword_p=*/false,
10530                                          /*check_dependency_p=*/true,
10531                                          /*declarator_p=*/true);
10532
10533   /* The function we call to handle a using-declaration is different
10534      depending on what scope we are in.  */
10535   if (qscope == error_mark_node || identifier == error_mark_node)
10536     ;
10537   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10538            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10539     /* [namespace.udecl]
10540
10541        A using declaration shall not name a template-id.  */
10542     error ("a template-id may not appear in a using-declaration");
10543   else
10544     {
10545       if (at_class_scope_p ())
10546         {
10547           /* Create the USING_DECL.  */
10548           decl = do_class_using_decl (parser->scope, identifier);
10549           /* Add it to the list of members in this class.  */
10550           finish_member_declaration (decl);
10551         }
10552       else
10553         {
10554           decl = cp_parser_lookup_name_simple (parser, identifier);
10555           if (decl == error_mark_node)
10556             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10557           else if (!at_namespace_scope_p ())
10558             do_local_using_decl (decl, qscope, identifier);
10559           else
10560             do_toplevel_using_decl (decl, qscope, identifier);
10561         }
10562     }
10563
10564   /* Look for the final `;'.  */
10565   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10566 }
10567
10568 /* Parse a using-directive.
10569
10570    using-directive:
10571      using namespace :: [opt] nested-name-specifier [opt]
10572        namespace-name ;  */
10573
10574 static void
10575 cp_parser_using_directive (cp_parser* parser)
10576 {
10577   tree namespace_decl;
10578   tree attribs;
10579
10580   /* Look for the `using' keyword.  */
10581   cp_parser_require_keyword (parser, RID_USING, "`using'");
10582   /* And the `namespace' keyword.  */
10583   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10584   /* Look for the optional `::' operator.  */
10585   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10586   /* And the optional nested-name-specifier.  */
10587   cp_parser_nested_name_specifier_opt (parser,
10588                                        /*typename_keyword_p=*/false,
10589                                        /*check_dependency_p=*/true,
10590                                        /*type_p=*/false,
10591                                        /*is_declaration=*/true);
10592   /* Get the namespace being used.  */
10593   namespace_decl = cp_parser_namespace_name (parser);
10594   /* And any specified attributes.  */
10595   attribs = cp_parser_attributes_opt (parser);
10596   /* Update the symbol table.  */
10597   parse_using_directive (namespace_decl, attribs);
10598   /* Look for the final `;'.  */
10599   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10600 }
10601
10602 /* Parse an asm-definition.
10603
10604    asm-definition:
10605      asm ( string-literal ) ;
10606
10607    GNU Extension:
10608
10609    asm-definition:
10610      asm volatile [opt] ( string-literal ) ;
10611      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10612      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10613                           : asm-operand-list [opt] ) ;
10614      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10615                           : asm-operand-list [opt]
10616                           : asm-operand-list [opt] ) ;  */
10617
10618 static void
10619 cp_parser_asm_definition (cp_parser* parser)
10620 {
10621   tree string;
10622   tree outputs = NULL_TREE;
10623   tree inputs = NULL_TREE;
10624   tree clobbers = NULL_TREE;
10625   tree asm_stmt;
10626   bool volatile_p = false;
10627   bool extended_p = false;
10628
10629   /* Look for the `asm' keyword.  */
10630   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10631   /* See if the next token is `volatile'.  */
10632   if (cp_parser_allow_gnu_extensions_p (parser)
10633       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10634     {
10635       /* Remember that we saw the `volatile' keyword.  */
10636       volatile_p = true;
10637       /* Consume the token.  */
10638       cp_lexer_consume_token (parser->lexer);
10639     }
10640   /* Look for the opening `('.  */
10641   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10642     return;
10643   /* Look for the string.  */
10644   string = cp_parser_string_literal (parser, false, false);
10645   if (string == error_mark_node)
10646     {
10647       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10648                                              /*consume_paren=*/true);
10649       return;
10650     }
10651
10652   /* If we're allowing GNU extensions, check for the extended assembly
10653      syntax.  Unfortunately, the `:' tokens need not be separated by
10654      a space in C, and so, for compatibility, we tolerate that here
10655      too.  Doing that means that we have to treat the `::' operator as
10656      two `:' tokens.  */
10657   if (cp_parser_allow_gnu_extensions_p (parser)
10658       && at_function_scope_p ()
10659       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10660           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10661     {
10662       bool inputs_p = false;
10663       bool clobbers_p = false;
10664
10665       /* The extended syntax was used.  */
10666       extended_p = true;
10667
10668       /* Look for outputs.  */
10669       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10670         {
10671           /* Consume the `:'.  */
10672           cp_lexer_consume_token (parser->lexer);
10673           /* Parse the output-operands.  */
10674           if (cp_lexer_next_token_is_not (parser->lexer,
10675                                           CPP_COLON)
10676               && cp_lexer_next_token_is_not (parser->lexer,
10677                                              CPP_SCOPE)
10678               && cp_lexer_next_token_is_not (parser->lexer,
10679                                              CPP_CLOSE_PAREN))
10680             outputs = cp_parser_asm_operand_list (parser);
10681         }
10682       /* If the next token is `::', there are no outputs, and the
10683          next token is the beginning of the inputs.  */
10684       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10685         /* The inputs are coming next.  */
10686         inputs_p = true;
10687
10688       /* Look for inputs.  */
10689       if (inputs_p
10690           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10691         {
10692           /* Consume the `:' or `::'.  */
10693           cp_lexer_consume_token (parser->lexer);
10694           /* Parse the output-operands.  */
10695           if (cp_lexer_next_token_is_not (parser->lexer,
10696                                           CPP_COLON)
10697               && cp_lexer_next_token_is_not (parser->lexer,
10698                                              CPP_CLOSE_PAREN))
10699             inputs = cp_parser_asm_operand_list (parser);
10700         }
10701       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10702         /* The clobbers are coming next.  */
10703         clobbers_p = true;
10704
10705       /* Look for clobbers.  */
10706       if (clobbers_p
10707           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10708         {
10709           /* Consume the `:' or `::'.  */
10710           cp_lexer_consume_token (parser->lexer);
10711           /* Parse the clobbers.  */
10712           if (cp_lexer_next_token_is_not (parser->lexer,
10713                                           CPP_CLOSE_PAREN))
10714             clobbers = cp_parser_asm_clobber_list (parser);
10715         }
10716     }
10717   /* Look for the closing `)'.  */
10718   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10719     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10720                                            /*consume_paren=*/true);
10721   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10722
10723   /* Create the ASM_EXPR.  */
10724   if (at_function_scope_p ())
10725     {
10726       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10727                                   inputs, clobbers);
10728       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10729       if (!extended_p)
10730         {
10731           tree temp = asm_stmt;
10732           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10733             temp = TREE_OPERAND (temp, 0);
10734
10735           ASM_INPUT_P (temp) = 1;
10736         }
10737     }
10738   else
10739     assemble_asm (string);
10740 }
10741
10742 /* Declarators [gram.dcl.decl] */
10743
10744 /* Parse an init-declarator.
10745
10746    init-declarator:
10747      declarator initializer [opt]
10748
10749    GNU Extension:
10750
10751    init-declarator:
10752      declarator asm-specification [opt] attributes [opt] initializer [opt]
10753
10754    function-definition:
10755      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10756        function-body
10757      decl-specifier-seq [opt] declarator function-try-block
10758
10759    GNU Extension:
10760
10761    function-definition:
10762      __extension__ function-definition
10763
10764    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10765    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10766    then this declarator appears in a class scope.  The new DECL created
10767    by this declarator is returned.
10768
10769    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10770    for a function-definition here as well.  If the declarator is a
10771    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10772    be TRUE upon return.  By that point, the function-definition will
10773    have been completely parsed.
10774
10775    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10776    is FALSE.  */
10777
10778 static tree
10779 cp_parser_init_declarator (cp_parser* parser,
10780                            cp_decl_specifier_seq *decl_specifiers,
10781                            bool function_definition_allowed_p,
10782                            bool member_p,
10783                            int declares_class_or_enum,
10784                            bool* function_definition_p)
10785 {
10786   cp_token *token;
10787   cp_declarator *declarator;
10788   tree prefix_attributes;
10789   tree attributes;
10790   tree asm_specification;
10791   tree initializer;
10792   tree decl = NULL_TREE;
10793   tree scope;
10794   bool is_initialized;
10795   bool is_parenthesized_init;
10796   bool is_non_constant_init;
10797   int ctor_dtor_or_conv_p;
10798   bool friend_p;
10799   tree pushed_scope = NULL;
10800
10801   /* Gather the attributes that were provided with the
10802      decl-specifiers.  */
10803   prefix_attributes = decl_specifiers->attributes;
10804
10805   /* Assume that this is not the declarator for a function
10806      definition.  */
10807   if (function_definition_p)
10808     *function_definition_p = false;
10809
10810   /* Defer access checks while parsing the declarator; we cannot know
10811      what names are accessible until we know what is being
10812      declared.  */
10813   resume_deferring_access_checks ();
10814
10815   /* Parse the declarator.  */
10816   declarator
10817     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10818                             &ctor_dtor_or_conv_p,
10819                             /*parenthesized_p=*/NULL,
10820                             /*member_p=*/false);
10821   /* Gather up the deferred checks.  */
10822   stop_deferring_access_checks ();
10823
10824   /* If the DECLARATOR was erroneous, there's no need to go
10825      further.  */
10826   if (declarator == cp_error_declarator)
10827     return error_mark_node;
10828
10829   if (declares_class_or_enum & 2)
10830     cp_parser_check_for_definition_in_return_type (declarator,
10831                                                    decl_specifiers->type);
10832
10833   /* Figure out what scope the entity declared by the DECLARATOR is
10834      located in.  `grokdeclarator' sometimes changes the scope, so
10835      we compute it now.  */
10836   scope = get_scope_of_declarator (declarator);
10837
10838   /* If we're allowing GNU extensions, look for an asm-specification
10839      and attributes.  */
10840   if (cp_parser_allow_gnu_extensions_p (parser))
10841     {
10842       /* Look for an asm-specification.  */
10843       asm_specification = cp_parser_asm_specification_opt (parser);
10844       /* And attributes.  */
10845       attributes = cp_parser_attributes_opt (parser);
10846     }
10847   else
10848     {
10849       asm_specification = NULL_TREE;
10850       attributes = NULL_TREE;
10851     }
10852
10853   /* Peek at the next token.  */
10854   token = cp_lexer_peek_token (parser->lexer);
10855   /* Check to see if the token indicates the start of a
10856      function-definition.  */
10857   if (cp_parser_token_starts_function_definition_p (token))
10858     {
10859       if (!function_definition_allowed_p)
10860         {
10861           /* If a function-definition should not appear here, issue an
10862              error message.  */
10863           cp_parser_error (parser,
10864                            "a function-definition is not allowed here");
10865           return error_mark_node;
10866         }
10867       else
10868         {
10869           /* Neither attributes nor an asm-specification are allowed
10870              on a function-definition.  */
10871           if (asm_specification)
10872             error ("an asm-specification is not allowed on a function-definition");
10873           if (attributes)
10874             error ("attributes are not allowed on a function-definition");
10875           /* This is a function-definition.  */
10876           *function_definition_p = true;
10877
10878           /* Parse the function definition.  */
10879           if (member_p)
10880             decl = cp_parser_save_member_function_body (parser,
10881                                                         decl_specifiers,
10882                                                         declarator,
10883                                                         prefix_attributes);
10884           else
10885             decl
10886               = (cp_parser_function_definition_from_specifiers_and_declarator
10887                  (parser, decl_specifiers, prefix_attributes, declarator));
10888
10889           return decl;
10890         }
10891     }
10892
10893   /* [dcl.dcl]
10894
10895      Only in function declarations for constructors, destructors, and
10896      type conversions can the decl-specifier-seq be omitted.
10897
10898      We explicitly postpone this check past the point where we handle
10899      function-definitions because we tolerate function-definitions
10900      that are missing their return types in some modes.  */
10901   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10902     {
10903       cp_parser_error (parser,
10904                        "expected constructor, destructor, or type conversion");
10905       return error_mark_node;
10906     }
10907
10908   /* An `=' or an `(' indicates an initializer.  */
10909   is_initialized = (token->type == CPP_EQ
10910                      || token->type == CPP_OPEN_PAREN);
10911   /* If the init-declarator isn't initialized and isn't followed by a
10912      `,' or `;', it's not a valid init-declarator.  */
10913   if (!is_initialized
10914       && token->type != CPP_COMMA
10915       && token->type != CPP_SEMICOLON)
10916     {
10917       cp_parser_error (parser, "expected initializer");
10918       return error_mark_node;
10919     }
10920
10921   /* Because start_decl has side-effects, we should only call it if we
10922      know we're going ahead.  By this point, we know that we cannot
10923      possibly be looking at any other construct.  */
10924   cp_parser_commit_to_tentative_parse (parser);
10925
10926   /* If the decl specifiers were bad, issue an error now that we're
10927      sure this was intended to be a declarator.  Then continue
10928      declaring the variable(s), as int, to try to cut down on further
10929      errors.  */
10930   if (decl_specifiers->any_specifiers_p
10931       && decl_specifiers->type == error_mark_node)
10932     {
10933       cp_parser_error (parser, "invalid type in declaration");
10934       decl_specifiers->type = integer_type_node;
10935     }
10936
10937   /* Check to see whether or not this declaration is a friend.  */
10938   friend_p = cp_parser_friend_p (decl_specifiers);
10939
10940   /* Check that the number of template-parameter-lists is OK.  */
10941   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10942     return error_mark_node;
10943
10944   /* Enter the newly declared entry in the symbol table.  If we're
10945      processing a declaration in a class-specifier, we wait until
10946      after processing the initializer.  */
10947   if (!member_p)
10948     {
10949       if (parser->in_unbraced_linkage_specification_p)
10950         {
10951           decl_specifiers->storage_class = sc_extern;
10952           have_extern_spec = false;
10953         }
10954       decl = start_decl (declarator, decl_specifiers,
10955                          is_initialized, attributes, prefix_attributes,
10956                          &pushed_scope);
10957     }
10958   else if (scope)
10959     /* Enter the SCOPE.  That way unqualified names appearing in the
10960        initializer will be looked up in SCOPE.  */
10961     pushed_scope = push_scope (scope);
10962
10963   /* Perform deferred access control checks, now that we know in which
10964      SCOPE the declared entity resides.  */
10965   if (!member_p && decl)
10966     {
10967       tree saved_current_function_decl = NULL_TREE;
10968
10969       /* If the entity being declared is a function, pretend that we
10970          are in its scope.  If it is a `friend', it may have access to
10971          things that would not otherwise be accessible.  */
10972       if (TREE_CODE (decl) == FUNCTION_DECL)
10973         {
10974           saved_current_function_decl = current_function_decl;
10975           current_function_decl = decl;
10976         }
10977
10978       /* Perform the access control checks for the declarator and the
10979          the decl-specifiers.  */
10980       perform_deferred_access_checks ();
10981
10982       /* Restore the saved value.  */
10983       if (TREE_CODE (decl) == FUNCTION_DECL)
10984         current_function_decl = saved_current_function_decl;
10985     }
10986
10987   /* Parse the initializer.  */
10988   if (is_initialized)
10989     initializer = cp_parser_initializer (parser,
10990                                          &is_parenthesized_init,
10991                                          &is_non_constant_init);
10992   else
10993     {
10994       initializer = NULL_TREE;
10995       is_parenthesized_init = false;
10996       is_non_constant_init = true;
10997     }
10998
10999   /* The old parser allows attributes to appear after a parenthesized
11000      initializer.  Mark Mitchell proposed removing this functionality
11001      on the GCC mailing lists on 2002-08-13.  This parser accepts the
11002      attributes -- but ignores them.  */
11003   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11004     if (cp_parser_attributes_opt (parser))
11005       warning (OPT_Wattributes,
11006                "attributes after parenthesized initializer ignored");
11007
11008   /* For an in-class declaration, use `grokfield' to create the
11009      declaration.  */
11010   if (member_p)
11011     {
11012       if (pushed_scope)
11013         {
11014           pop_scope (pushed_scope);
11015           pushed_scope = false;
11016         }
11017       decl = grokfield (declarator, decl_specifiers,
11018                         initializer, /*asmspec=*/NULL_TREE,
11019                         prefix_attributes);
11020       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11021         cp_parser_save_default_args (parser, decl);
11022     }
11023
11024   /* Finish processing the declaration.  But, skip friend
11025      declarations.  */
11026   if (!friend_p && decl && decl != error_mark_node)
11027     {
11028       cp_finish_decl (decl,
11029                       initializer,
11030                       asm_specification,
11031                       /* If the initializer is in parentheses, then this is
11032                          a direct-initialization, which means that an
11033                          `explicit' constructor is OK.  Otherwise, an
11034                          `explicit' constructor cannot be used.  */
11035                       ((is_parenthesized_init || !is_initialized)
11036                      ? 0 : LOOKUP_ONLYCONVERTING));
11037     }
11038   if (!friend_p && pushed_scope)
11039     pop_scope (pushed_scope);
11040
11041   /* Remember whether or not variables were initialized by
11042      constant-expressions.  */
11043   if (decl && TREE_CODE (decl) == VAR_DECL
11044       && is_initialized && !is_non_constant_init)
11045     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
11046
11047   return decl;
11048 }
11049
11050 /* Parse a declarator.
11051
11052    declarator:
11053      direct-declarator
11054      ptr-operator declarator
11055
11056    abstract-declarator:
11057      ptr-operator abstract-declarator [opt]
11058      direct-abstract-declarator
11059
11060    GNU Extensions:
11061
11062    declarator:
11063      attributes [opt] direct-declarator
11064      attributes [opt] ptr-operator declarator
11065
11066    abstract-declarator:
11067      attributes [opt] ptr-operator abstract-declarator [opt]
11068      attributes [opt] direct-abstract-declarator
11069
11070    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11071    detect constructor, destructor or conversion operators. It is set
11072    to -1 if the declarator is a name, and +1 if it is a
11073    function. Otherwise it is set to zero. Usually you just want to
11074    test for >0, but internally the negative value is used.
11075
11076    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11077    a decl-specifier-seq unless it declares a constructor, destructor,
11078    or conversion.  It might seem that we could check this condition in
11079    semantic analysis, rather than parsing, but that makes it difficult
11080    to handle something like `f()'.  We want to notice that there are
11081    no decl-specifiers, and therefore realize that this is an
11082    expression, not a declaration.)
11083
11084    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11085    the declarator is a direct-declarator of the form "(...)".
11086
11087    MEMBER_P is true iff this declarator is a member-declarator.  */
11088
11089 static cp_declarator *
11090 cp_parser_declarator (cp_parser* parser,
11091                       cp_parser_declarator_kind dcl_kind,
11092                       int* ctor_dtor_or_conv_p,
11093                       bool* parenthesized_p,
11094                       bool member_p)
11095 {
11096   cp_token *token;
11097   cp_declarator *declarator;
11098   enum tree_code code;
11099   cp_cv_quals cv_quals;
11100   tree class_type;
11101   tree attributes = NULL_TREE;
11102
11103   /* Assume this is not a constructor, destructor, or type-conversion
11104      operator.  */
11105   if (ctor_dtor_or_conv_p)
11106     *ctor_dtor_or_conv_p = 0;
11107
11108   if (cp_parser_allow_gnu_extensions_p (parser))
11109     attributes = cp_parser_attributes_opt (parser);
11110
11111   /* Peek at the next token.  */
11112   token = cp_lexer_peek_token (parser->lexer);
11113
11114   /* Check for the ptr-operator production.  */
11115   cp_parser_parse_tentatively (parser);
11116   /* Parse the ptr-operator.  */
11117   code = cp_parser_ptr_operator (parser,
11118                                  &class_type,
11119                                  &cv_quals);
11120   /* If that worked, then we have a ptr-operator.  */
11121   if (cp_parser_parse_definitely (parser))
11122     {
11123       /* If a ptr-operator was found, then this declarator was not
11124          parenthesized.  */
11125       if (parenthesized_p)
11126         *parenthesized_p = true;
11127       /* The dependent declarator is optional if we are parsing an
11128          abstract-declarator.  */
11129       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11130         cp_parser_parse_tentatively (parser);
11131
11132       /* Parse the dependent declarator.  */
11133       declarator = cp_parser_declarator (parser, dcl_kind,
11134                                          /*ctor_dtor_or_conv_p=*/NULL,
11135                                          /*parenthesized_p=*/NULL,
11136                                          /*member_p=*/false);
11137
11138       /* If we are parsing an abstract-declarator, we must handle the
11139          case where the dependent declarator is absent.  */
11140       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11141           && !cp_parser_parse_definitely (parser))
11142         declarator = NULL;
11143
11144       /* Build the representation of the ptr-operator.  */
11145       if (class_type)
11146         declarator = make_ptrmem_declarator (cv_quals,
11147                                              class_type,
11148                                              declarator);
11149       else if (code == INDIRECT_REF)
11150         declarator = make_pointer_declarator (cv_quals, declarator);
11151       else
11152         declarator = make_reference_declarator (cv_quals, declarator);
11153     }
11154   /* Everything else is a direct-declarator.  */
11155   else
11156     {
11157       if (parenthesized_p)
11158         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11159                                                    CPP_OPEN_PAREN);
11160       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11161                                                 ctor_dtor_or_conv_p,
11162                                                 member_p);
11163     }
11164
11165   if (attributes && declarator != cp_error_declarator)
11166     declarator->attributes = attributes;
11167
11168   return declarator;
11169 }
11170
11171 /* Parse a direct-declarator or direct-abstract-declarator.
11172
11173    direct-declarator:
11174      declarator-id
11175      direct-declarator ( parameter-declaration-clause )
11176        cv-qualifier-seq [opt]
11177        exception-specification [opt]
11178      direct-declarator [ constant-expression [opt] ]
11179      ( declarator )
11180
11181    direct-abstract-declarator:
11182      direct-abstract-declarator [opt]
11183        ( parameter-declaration-clause )
11184        cv-qualifier-seq [opt]
11185        exception-specification [opt]
11186      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11187      ( abstract-declarator )
11188
11189    Returns a representation of the declarator.  DCL_KIND is
11190    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11191    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11192    we are parsing a direct-declarator.  It is
11193    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11194    of ambiguity we prefer an abstract declarator, as per
11195    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11196    cp_parser_declarator.  */
11197
11198 static cp_declarator *
11199 cp_parser_direct_declarator (cp_parser* parser,
11200                              cp_parser_declarator_kind dcl_kind,
11201                              int* ctor_dtor_or_conv_p,
11202                              bool member_p)
11203 {
11204   cp_token *token;
11205   cp_declarator *declarator = NULL;
11206   tree scope = NULL_TREE;
11207   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11208   bool saved_in_declarator_p = parser->in_declarator_p;
11209   bool first = true;
11210   tree pushed_scope = NULL_TREE;
11211
11212   while (true)
11213     {
11214       /* Peek at the next token.  */
11215       token = cp_lexer_peek_token (parser->lexer);
11216       if (token->type == CPP_OPEN_PAREN)
11217         {
11218           /* This is either a parameter-declaration-clause, or a
11219              parenthesized declarator. When we know we are parsing a
11220              named declarator, it must be a parenthesized declarator
11221              if FIRST is true. For instance, `(int)' is a
11222              parameter-declaration-clause, with an omitted
11223              direct-abstract-declarator. But `((*))', is a
11224              parenthesized abstract declarator. Finally, when T is a
11225              template parameter `(T)' is a
11226              parameter-declaration-clause, and not a parenthesized
11227              named declarator.
11228
11229              We first try and parse a parameter-declaration-clause,
11230              and then try a nested declarator (if FIRST is true).
11231
11232              It is not an error for it not to be a
11233              parameter-declaration-clause, even when FIRST is
11234              false. Consider,
11235
11236                int i (int);
11237                int i (3);
11238
11239              The first is the declaration of a function while the
11240              second is a the definition of a variable, including its
11241              initializer.
11242
11243              Having seen only the parenthesis, we cannot know which of
11244              these two alternatives should be selected.  Even more
11245              complex are examples like:
11246
11247                int i (int (a));
11248                int i (int (3));
11249
11250              The former is a function-declaration; the latter is a
11251              variable initialization.
11252
11253              Thus again, we try a parameter-declaration-clause, and if
11254              that fails, we back out and return.  */
11255
11256           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11257             {
11258               cp_parameter_declarator *params;
11259               unsigned saved_num_template_parameter_lists;
11260
11261               /* In a member-declarator, the only valid interpretation
11262                  of a parenthesis is the start of a
11263                  parameter-declaration-clause.  (It is invalid to
11264                  initialize a static data member with a parenthesized
11265                  initializer; only the "=" form of initialization is
11266                  permitted.)  */
11267               if (!member_p)
11268                 cp_parser_parse_tentatively (parser);
11269
11270               /* Consume the `('.  */
11271               cp_lexer_consume_token (parser->lexer);
11272               if (first)
11273                 {
11274                   /* If this is going to be an abstract declarator, we're
11275                      in a declarator and we can't have default args.  */
11276                   parser->default_arg_ok_p = false;
11277                   parser->in_declarator_p = true;
11278                 }
11279
11280               /* Inside the function parameter list, surrounding
11281                  template-parameter-lists do not apply.  */
11282               saved_num_template_parameter_lists
11283                 = parser->num_template_parameter_lists;
11284               parser->num_template_parameter_lists = 0;
11285
11286               /* Parse the parameter-declaration-clause.  */
11287               params = cp_parser_parameter_declaration_clause (parser);
11288
11289               parser->num_template_parameter_lists
11290                 = saved_num_template_parameter_lists;
11291
11292               /* If all went well, parse the cv-qualifier-seq and the
11293                  exception-specification.  */
11294               if (member_p || cp_parser_parse_definitely (parser))
11295                 {
11296                   cp_cv_quals cv_quals;
11297                   tree exception_specification;
11298
11299                   if (ctor_dtor_or_conv_p)
11300                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11301                   first = false;
11302                   /* Consume the `)'.  */
11303                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11304
11305                   /* Parse the cv-qualifier-seq.  */
11306                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11307                   /* And the exception-specification.  */
11308                   exception_specification
11309                     = cp_parser_exception_specification_opt (parser);
11310
11311                   /* Create the function-declarator.  */
11312                   declarator = make_call_declarator (declarator,
11313                                                      params,
11314                                                      cv_quals,
11315                                                      exception_specification);
11316                   /* Any subsequent parameter lists are to do with
11317                      return type, so are not those of the declared
11318                      function.  */
11319                   parser->default_arg_ok_p = false;
11320
11321                   /* Repeat the main loop.  */
11322                   continue;
11323                 }
11324             }
11325
11326           /* If this is the first, we can try a parenthesized
11327              declarator.  */
11328           if (first)
11329             {
11330               bool saved_in_type_id_in_expr_p;
11331
11332               parser->default_arg_ok_p = saved_default_arg_ok_p;
11333               parser->in_declarator_p = saved_in_declarator_p;
11334
11335               /* Consume the `('.  */
11336               cp_lexer_consume_token (parser->lexer);
11337               /* Parse the nested declarator.  */
11338               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11339               parser->in_type_id_in_expr_p = true;
11340               declarator
11341                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11342                                         /*parenthesized_p=*/NULL,
11343                                         member_p);
11344               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11345               first = false;
11346               /* Expect a `)'.  */
11347               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11348                 declarator = cp_error_declarator;
11349               if (declarator == cp_error_declarator)
11350                 break;
11351
11352               goto handle_declarator;
11353             }
11354           /* Otherwise, we must be done.  */
11355           else
11356             break;
11357         }
11358       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11359                && token->type == CPP_OPEN_SQUARE)
11360         {
11361           /* Parse an array-declarator.  */
11362           tree bounds;
11363
11364           if (ctor_dtor_or_conv_p)
11365             *ctor_dtor_or_conv_p = 0;
11366
11367           first = false;
11368           parser->default_arg_ok_p = false;
11369           parser->in_declarator_p = true;
11370           /* Consume the `['.  */
11371           cp_lexer_consume_token (parser->lexer);
11372           /* Peek at the next token.  */
11373           token = cp_lexer_peek_token (parser->lexer);
11374           /* If the next token is `]', then there is no
11375              constant-expression.  */
11376           if (token->type != CPP_CLOSE_SQUARE)
11377             {
11378               bool non_constant_p;
11379
11380               bounds
11381                 = cp_parser_constant_expression (parser,
11382                                                  /*allow_non_constant=*/true,
11383                                                  &non_constant_p);
11384               if (!non_constant_p)
11385                 bounds = fold_non_dependent_expr (bounds);
11386               /* Normally, the array bound must be an integral constant
11387                  expression.  However, as an extension, we allow VLAs
11388                  in function scopes.  */
11389               else if (!at_function_scope_p ())
11390                 {
11391                   error ("array bound is not an integer constant");
11392                   bounds = error_mark_node;
11393                 }
11394             }
11395           else
11396             bounds = NULL_TREE;
11397           /* Look for the closing `]'.  */
11398           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11399             {
11400               declarator = cp_error_declarator;
11401               break;
11402             }
11403
11404           declarator = make_array_declarator (declarator, bounds);
11405         }
11406       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11407         {
11408           tree qualifying_scope;
11409           tree unqualified_name;
11410           special_function_kind sfk;
11411
11412           /* Parse a declarator-id */
11413           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11414             cp_parser_parse_tentatively (parser);
11415           unqualified_name = cp_parser_declarator_id (parser);
11416           qualifying_scope = parser->scope;
11417           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11418             {
11419               if (!cp_parser_parse_definitely (parser))
11420                 unqualified_name = error_mark_node;
11421               else if (qualifying_scope
11422                        || (TREE_CODE (unqualified_name)
11423                            != IDENTIFIER_NODE))
11424                 {
11425                   cp_parser_error (parser, "expected unqualified-id");
11426                   unqualified_name = error_mark_node;
11427                 }
11428             }
11429
11430           if (unqualified_name == error_mark_node)
11431             {
11432               declarator = cp_error_declarator;
11433               break;
11434             }
11435
11436           if (qualifying_scope && at_namespace_scope_p ()
11437               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11438             {
11439               /* In the declaration of a member of a template class
11440                  outside of the class itself, the SCOPE will sometimes
11441                  be a TYPENAME_TYPE.  For example, given:
11442
11443                  template <typename T>
11444                  int S<T>::R::i = 3;
11445
11446                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11447                  this context, we must resolve S<T>::R to an ordinary
11448                  type, rather than a typename type.
11449
11450                  The reason we normally avoid resolving TYPENAME_TYPEs
11451                  is that a specialization of `S' might render
11452                  `S<T>::R' not a type.  However, if `S' is
11453                  specialized, then this `i' will not be used, so there
11454                  is no harm in resolving the types here.  */
11455               tree type;
11456
11457               /* Resolve the TYPENAME_TYPE.  */
11458               type = resolve_typename_type (qualifying_scope,
11459                                             /*only_current_p=*/false);
11460               /* If that failed, the declarator is invalid.  */
11461               if (type == error_mark_node)
11462                 error ("%<%T::%D%> is not a type",
11463                        TYPE_CONTEXT (qualifying_scope),
11464                        TYPE_IDENTIFIER (qualifying_scope));
11465               qualifying_scope = type;
11466             }
11467
11468           sfk = sfk_none;
11469           if (unqualified_name)
11470             {
11471               tree class_type;
11472
11473               if (qualifying_scope
11474                   && CLASS_TYPE_P (qualifying_scope))
11475                 class_type = qualifying_scope;
11476               else
11477                 class_type = current_class_type;
11478
11479               if (TREE_CODE (unqualified_name) == TYPE_DECL)
11480                 {
11481                   if (qualifying_scope 
11482                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11483                     {
11484                       error ("invalid use of constructor as a template");
11485                       inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11486                               "the constructor in a qualified name",
11487                               class_type,
11488                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11489                               class_type, class_type);
11490                       declarator = cp_error_declarator;
11491                       break;
11492                     }
11493                   else if (class_type
11494                            && same_type_p (TREE_TYPE (unqualified_name),
11495                                            class_type))
11496                     unqualified_name = constructor_name (class_type);
11497                   else
11498                     {
11499                       /* We do not attempt to print the declarator
11500                          here because we do not have enough
11501                          information about its original syntactic
11502                          form.  */
11503                       error ("invalid declarator");
11504                       declarator = cp_error_declarator;
11505                       break;
11506                     }
11507                 }
11508
11509               if (class_type)
11510                 {
11511                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11512                     sfk = sfk_destructor;
11513                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11514                     sfk = sfk_conversion;
11515                   else if (/* There's no way to declare a constructor
11516                               for an anonymous type, even if the type
11517                               got a name for linkage purposes.  */
11518                            !TYPE_WAS_ANONYMOUS (class_type)
11519                            && constructor_name_p (unqualified_name,
11520                                                   class_type))
11521                     {
11522                       unqualified_name = constructor_name (class_type);
11523                       sfk = sfk_constructor;
11524                     }
11525
11526                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
11527                     *ctor_dtor_or_conv_p = -1;
11528                 }
11529             }
11530           declarator = make_id_declarator (qualifying_scope, 
11531                                            unqualified_name,
11532                                            sfk);
11533           declarator->id_loc = token->location;
11534
11535         handle_declarator:;
11536           scope = get_scope_of_declarator (declarator);
11537           if (scope)
11538             /* Any names that appear after the declarator-id for a
11539                member are looked up in the containing scope.  */
11540             pushed_scope = push_scope (scope);
11541           parser->in_declarator_p = true;
11542           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11543               || (declarator && declarator->kind == cdk_id))
11544             /* Default args are only allowed on function
11545                declarations.  */
11546             parser->default_arg_ok_p = saved_default_arg_ok_p;
11547           else
11548             parser->default_arg_ok_p = false;
11549
11550           first = false;
11551         }
11552       /* We're done.  */
11553       else
11554         break;
11555     }
11556
11557   /* For an abstract declarator, we might wind up with nothing at this
11558      point.  That's an error; the declarator is not optional.  */
11559   if (!declarator)
11560     cp_parser_error (parser, "expected declarator");
11561
11562   /* If we entered a scope, we must exit it now.  */
11563   if (pushed_scope)
11564     pop_scope (pushed_scope);
11565
11566   parser->default_arg_ok_p = saved_default_arg_ok_p;
11567   parser->in_declarator_p = saved_in_declarator_p;
11568
11569   return declarator;
11570 }
11571
11572 /* Parse a ptr-operator.
11573
11574    ptr-operator:
11575      * cv-qualifier-seq [opt]
11576      &
11577      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11578
11579    GNU Extension:
11580
11581    ptr-operator:
11582      & cv-qualifier-seq [opt]
11583
11584    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11585    Returns ADDR_EXPR if a reference was used.  In the case of a
11586    pointer-to-member, *TYPE is filled in with the TYPE containing the
11587    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11588    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11589    ERROR_MARK if an error occurred.  */
11590
11591 static enum tree_code
11592 cp_parser_ptr_operator (cp_parser* parser,
11593                         tree* type,
11594                         cp_cv_quals *cv_quals)
11595 {
11596   enum tree_code code = ERROR_MARK;
11597   cp_token *token;
11598
11599   /* Assume that it's not a pointer-to-member.  */
11600   *type = NULL_TREE;
11601   /* And that there are no cv-qualifiers.  */
11602   *cv_quals = TYPE_UNQUALIFIED;
11603
11604   /* Peek at the next token.  */
11605   token = cp_lexer_peek_token (parser->lexer);
11606   /* If it's a `*' or `&' we have a pointer or reference.  */
11607   if (token->type == CPP_MULT || token->type == CPP_AND)
11608     {
11609       /* Remember which ptr-operator we were processing.  */
11610       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11611
11612       /* Consume the `*' or `&'.  */
11613       cp_lexer_consume_token (parser->lexer);
11614
11615       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11616          `&', if we are allowing GNU extensions.  (The only qualifier
11617          that can legally appear after `&' is `restrict', but that is
11618          enforced during semantic analysis.  */
11619       if (code == INDIRECT_REF
11620           || cp_parser_allow_gnu_extensions_p (parser))
11621         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11622     }
11623   else
11624     {
11625       /* Try the pointer-to-member case.  */
11626       cp_parser_parse_tentatively (parser);
11627       /* Look for the optional `::' operator.  */
11628       cp_parser_global_scope_opt (parser,
11629                                   /*current_scope_valid_p=*/false);
11630       /* Look for the nested-name specifier.  */
11631       cp_parser_nested_name_specifier (parser,
11632                                        /*typename_keyword_p=*/false,
11633                                        /*check_dependency_p=*/true,
11634                                        /*type_p=*/false,
11635                                        /*is_declaration=*/false);
11636       /* If we found it, and the next token is a `*', then we are
11637          indeed looking at a pointer-to-member operator.  */
11638       if (!cp_parser_error_occurred (parser)
11639           && cp_parser_require (parser, CPP_MULT, "`*'"))
11640         {
11641           /* The type of which the member is a member is given by the
11642              current SCOPE.  */
11643           *type = parser->scope;
11644           /* The next name will not be qualified.  */
11645           parser->scope = NULL_TREE;
11646           parser->qualifying_scope = NULL_TREE;
11647           parser->object_scope = NULL_TREE;
11648           /* Indicate that the `*' operator was used.  */
11649           code = INDIRECT_REF;
11650           /* Look for the optional cv-qualifier-seq.  */
11651           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11652         }
11653       /* If that didn't work we don't have a ptr-operator.  */
11654       if (!cp_parser_parse_definitely (parser))
11655         cp_parser_error (parser, "expected ptr-operator");
11656     }
11657
11658   return code;
11659 }
11660
11661 /* Parse an (optional) cv-qualifier-seq.
11662
11663    cv-qualifier-seq:
11664      cv-qualifier cv-qualifier-seq [opt]
11665
11666    cv-qualifier:
11667      const
11668      volatile
11669
11670    GNU Extension:
11671
11672    cv-qualifier:
11673      __restrict__
11674
11675    Returns a bitmask representing the cv-qualifiers.  */
11676
11677 static cp_cv_quals
11678 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11679 {
11680   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11681
11682   while (true)
11683     {
11684       cp_token *token;
11685       cp_cv_quals cv_qualifier;
11686
11687       /* Peek at the next token.  */
11688       token = cp_lexer_peek_token (parser->lexer);
11689       /* See if it's a cv-qualifier.  */
11690       switch (token->keyword)
11691         {
11692         case RID_CONST:
11693           cv_qualifier = TYPE_QUAL_CONST;
11694           break;
11695
11696         case RID_VOLATILE:
11697           cv_qualifier = TYPE_QUAL_VOLATILE;
11698           break;
11699
11700         case RID_RESTRICT:
11701           cv_qualifier = TYPE_QUAL_RESTRICT;
11702           break;
11703
11704         default:
11705           cv_qualifier = TYPE_UNQUALIFIED;
11706           break;
11707         }
11708
11709       if (!cv_qualifier)
11710         break;
11711
11712       if (cv_quals & cv_qualifier)
11713         {
11714           error ("duplicate cv-qualifier");
11715           cp_lexer_purge_token (parser->lexer);
11716         }
11717       else
11718         {
11719           cp_lexer_consume_token (parser->lexer);
11720           cv_quals |= cv_qualifier;
11721         }
11722     }
11723
11724   return cv_quals;
11725 }
11726
11727 /* Parse a declarator-id.
11728
11729    declarator-id:
11730      id-expression
11731      :: [opt] nested-name-specifier [opt] type-name
11732
11733    In the `id-expression' case, the value returned is as for
11734    cp_parser_id_expression if the id-expression was an unqualified-id.
11735    If the id-expression was a qualified-id, then a SCOPE_REF is
11736    returned.  The first operand is the scope (either a NAMESPACE_DECL
11737    or TREE_TYPE), but the second is still just a representation of an
11738    unqualified-id.  */
11739
11740 static tree
11741 cp_parser_declarator_id (cp_parser* parser)
11742 {
11743   tree id;
11744   /* The expression must be an id-expression.  Assume that qualified
11745      names are the names of types so that:
11746
11747        template <class T>
11748        int S<T>::R::i = 3;
11749
11750      will work; we must treat `S<T>::R' as the name of a type.
11751      Similarly, assume that qualified names are templates, where
11752      required, so that:
11753
11754        template <class T>
11755        int S<T>::R<T>::i = 3;
11756
11757      will work, too.  */
11758   id = cp_parser_id_expression (parser,
11759                                 /*template_keyword_p=*/false,
11760                                 /*check_dependency_p=*/false,
11761                                 /*template_p=*/NULL,
11762                                 /*declarator_p=*/true);
11763   if (BASELINK_P (id))
11764     id = BASELINK_FUNCTIONS (id);
11765   return id;
11766 }
11767
11768 /* Parse a type-id.
11769
11770    type-id:
11771      type-specifier-seq abstract-declarator [opt]
11772
11773    Returns the TYPE specified.  */
11774
11775 static tree
11776 cp_parser_type_id (cp_parser* parser)
11777 {
11778   cp_decl_specifier_seq type_specifier_seq;
11779   cp_declarator *abstract_declarator;
11780
11781   /* Parse the type-specifier-seq.  */
11782   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11783                                 &type_specifier_seq);
11784   if (type_specifier_seq.type == error_mark_node)
11785     return error_mark_node;
11786
11787   /* There might or might not be an abstract declarator.  */
11788   cp_parser_parse_tentatively (parser);
11789   /* Look for the declarator.  */
11790   abstract_declarator
11791     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11792                             /*parenthesized_p=*/NULL,
11793                             /*member_p=*/false);
11794   /* Check to see if there really was a declarator.  */
11795   if (!cp_parser_parse_definitely (parser))
11796     abstract_declarator = NULL;
11797
11798   return groktypename (&type_specifier_seq, abstract_declarator);
11799 }
11800
11801 /* Parse a type-specifier-seq.
11802
11803    type-specifier-seq:
11804      type-specifier type-specifier-seq [opt]
11805
11806    GNU extension:
11807
11808    type-specifier-seq:
11809      attributes type-specifier-seq [opt]
11810
11811    If IS_CONDITION is true, we are at the start of a "condition",
11812    e.g., we've just seen "if (".
11813
11814    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11815
11816 static void
11817 cp_parser_type_specifier_seq (cp_parser* parser,
11818                               bool is_condition,
11819                               cp_decl_specifier_seq *type_specifier_seq)
11820 {
11821   bool seen_type_specifier = false;
11822   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11823
11824   /* Clear the TYPE_SPECIFIER_SEQ.  */
11825   clear_decl_specs (type_specifier_seq);
11826
11827   /* Parse the type-specifiers and attributes.  */
11828   while (true)
11829     {
11830       tree type_specifier;
11831       bool is_cv_qualifier;
11832
11833       /* Check for attributes first.  */
11834       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11835         {
11836           type_specifier_seq->attributes =
11837             chainon (type_specifier_seq->attributes,
11838                      cp_parser_attributes_opt (parser));
11839           continue;
11840         }
11841
11842       /* Look for the type-specifier.  */
11843       type_specifier = cp_parser_type_specifier (parser,
11844                                                  flags,
11845                                                  type_specifier_seq,
11846                                                  /*is_declaration=*/false,
11847                                                  NULL,
11848                                                  &is_cv_qualifier);
11849       if (!type_specifier)
11850         {
11851           /* If the first type-specifier could not be found, this is not a
11852              type-specifier-seq at all.  */
11853           if (!seen_type_specifier)
11854             {
11855               cp_parser_error (parser, "expected type-specifier");
11856               type_specifier_seq->type = error_mark_node;
11857               return;
11858             }
11859           /* If subsequent type-specifiers could not be found, the
11860              type-specifier-seq is complete.  */
11861           break;
11862         }
11863
11864       seen_type_specifier = true;
11865       /* The standard says that a condition can be:
11866
11867             type-specifier-seq declarator = assignment-expression
11868
11869          However, given:
11870
11871            struct S {};
11872            if (int S = ...)
11873
11874          we should treat the "S" as a declarator, not as a
11875          type-specifier.  The standard doesn't say that explicitly for
11876          type-specifier-seq, but it does say that for
11877          decl-specifier-seq in an ordinary declaration.  Perhaps it
11878          would be clearer just to allow a decl-specifier-seq here, and
11879          then add a semantic restriction that if any decl-specifiers
11880          that are not type-specifiers appear, the program is invalid.  */
11881       if (is_condition && !is_cv_qualifier)
11882         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11883     }
11884
11885   return;
11886 }
11887
11888 /* Parse a parameter-declaration-clause.
11889
11890    parameter-declaration-clause:
11891      parameter-declaration-list [opt] ... [opt]
11892      parameter-declaration-list , ...
11893
11894    Returns a representation for the parameter declarations.  A return
11895    value of NULL indicates a parameter-declaration-clause consisting
11896    only of an ellipsis.  */
11897
11898 static cp_parameter_declarator *
11899 cp_parser_parameter_declaration_clause (cp_parser* parser)
11900 {
11901   cp_parameter_declarator *parameters;
11902   cp_token *token;
11903   bool ellipsis_p;
11904   bool is_error;
11905
11906   /* Peek at the next token.  */
11907   token = cp_lexer_peek_token (parser->lexer);
11908   /* Check for trivial parameter-declaration-clauses.  */
11909   if (token->type == CPP_ELLIPSIS)
11910     {
11911       /* Consume the `...' token.  */
11912       cp_lexer_consume_token (parser->lexer);
11913       return NULL;
11914     }
11915   else if (token->type == CPP_CLOSE_PAREN)
11916     /* There are no parameters.  */
11917     {
11918 #ifndef NO_IMPLICIT_EXTERN_C
11919       if (in_system_header && current_class_type == NULL
11920           && current_lang_name == lang_name_c)
11921         return NULL;
11922       else
11923 #endif
11924         return no_parameters;
11925     }
11926   /* Check for `(void)', too, which is a special case.  */
11927   else if (token->keyword == RID_VOID
11928            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11929                == CPP_CLOSE_PAREN))
11930     {
11931       /* Consume the `void' token.  */
11932       cp_lexer_consume_token (parser->lexer);
11933       /* There are no parameters.  */
11934       return no_parameters;
11935     }
11936
11937   /* Parse the parameter-declaration-list.  */
11938   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11939   /* If a parse error occurred while parsing the
11940      parameter-declaration-list, then the entire
11941      parameter-declaration-clause is erroneous.  */
11942   if (is_error)
11943     return NULL;
11944
11945   /* Peek at the next token.  */
11946   token = cp_lexer_peek_token (parser->lexer);
11947   /* If it's a `,', the clause should terminate with an ellipsis.  */
11948   if (token->type == CPP_COMMA)
11949     {
11950       /* Consume the `,'.  */
11951       cp_lexer_consume_token (parser->lexer);
11952       /* Expect an ellipsis.  */
11953       ellipsis_p
11954         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11955     }
11956   /* It might also be `...' if the optional trailing `,' was
11957      omitted.  */
11958   else if (token->type == CPP_ELLIPSIS)
11959     {
11960       /* Consume the `...' token.  */
11961       cp_lexer_consume_token (parser->lexer);
11962       /* And remember that we saw it.  */
11963       ellipsis_p = true;
11964     }
11965   else
11966     ellipsis_p = false;
11967
11968   /* Finish the parameter list.  */
11969   if (parameters && ellipsis_p)
11970     parameters->ellipsis_p = true;
11971
11972   return parameters;
11973 }
11974
11975 /* Parse a parameter-declaration-list.
11976
11977    parameter-declaration-list:
11978      parameter-declaration
11979      parameter-declaration-list , parameter-declaration
11980
11981    Returns a representation of the parameter-declaration-list, as for
11982    cp_parser_parameter_declaration_clause.  However, the
11983    `void_list_node' is never appended to the list.  Upon return,
11984    *IS_ERROR will be true iff an error occurred.  */
11985
11986 static cp_parameter_declarator *
11987 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11988 {
11989   cp_parameter_declarator *parameters = NULL;
11990   cp_parameter_declarator **tail = &parameters;
11991
11992   /* Assume all will go well.  */
11993   *is_error = false;
11994
11995   /* Look for more parameters.  */
11996   while (true)
11997     {
11998       cp_parameter_declarator *parameter;
11999       bool parenthesized_p;
12000       /* Parse the parameter.  */
12001       parameter
12002         = cp_parser_parameter_declaration (parser,
12003                                            /*template_parm_p=*/false,
12004                                            &parenthesized_p);
12005
12006       /* If a parse error occurred parsing the parameter declaration,
12007          then the entire parameter-declaration-list is erroneous.  */
12008       if (!parameter)
12009         {
12010           *is_error = true;
12011           parameters = NULL;
12012           break;
12013         }
12014       /* Add the new parameter to the list.  */
12015       *tail = parameter;
12016       tail = &parameter->next;
12017
12018       /* Peek at the next token.  */
12019       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12020           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12021           /* These are for Objective-C++ */
12022           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12023           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12024         /* The parameter-declaration-list is complete.  */
12025         break;
12026       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12027         {
12028           cp_token *token;
12029
12030           /* Peek at the next token.  */
12031           token = cp_lexer_peek_nth_token (parser->lexer, 2);
12032           /* If it's an ellipsis, then the list is complete.  */
12033           if (token->type == CPP_ELLIPSIS)
12034             break;
12035           /* Otherwise, there must be more parameters.  Consume the
12036              `,'.  */
12037           cp_lexer_consume_token (parser->lexer);
12038           /* When parsing something like:
12039
12040                 int i(float f, double d)
12041
12042              we can tell after seeing the declaration for "f" that we
12043              are not looking at an initialization of a variable "i",
12044              but rather at the declaration of a function "i".
12045
12046              Due to the fact that the parsing of template arguments
12047              (as specified to a template-id) requires backtracking we
12048              cannot use this technique when inside a template argument
12049              list.  */
12050           if (!parser->in_template_argument_list_p
12051               && !parser->in_type_id_in_expr_p
12052               && cp_parser_uncommitted_to_tentative_parse_p (parser)
12053               /* However, a parameter-declaration of the form
12054                  "foat(f)" (which is a valid declaration of a
12055                  parameter "f") can also be interpreted as an
12056                  expression (the conversion of "f" to "float").  */
12057               && !parenthesized_p)
12058             cp_parser_commit_to_tentative_parse (parser);
12059         }
12060       else
12061         {
12062           cp_parser_error (parser, "expected %<,%> or %<...%>");
12063           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12064             cp_parser_skip_to_closing_parenthesis (parser,
12065                                                    /*recovering=*/true,
12066                                                    /*or_comma=*/false,
12067                                                    /*consume_paren=*/false);
12068           break;
12069         }
12070     }
12071
12072   return parameters;
12073 }
12074
12075 /* Parse a parameter declaration.
12076
12077    parameter-declaration:
12078      decl-specifier-seq declarator
12079      decl-specifier-seq declarator = assignment-expression
12080      decl-specifier-seq abstract-declarator [opt]
12081      decl-specifier-seq abstract-declarator [opt] = assignment-expression
12082
12083    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12084    declares a template parameter.  (In that case, a non-nested `>'
12085    token encountered during the parsing of the assignment-expression
12086    is not interpreted as a greater-than operator.)
12087
12088    Returns a representation of the parameter, or NULL if an error
12089    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12090    true iff the declarator is of the form "(p)".  */
12091
12092 static cp_parameter_declarator *
12093 cp_parser_parameter_declaration (cp_parser *parser,
12094                                  bool template_parm_p,
12095                                  bool *parenthesized_p)
12096 {
12097   int declares_class_or_enum;
12098   bool greater_than_is_operator_p;
12099   cp_decl_specifier_seq decl_specifiers;
12100   cp_declarator *declarator;
12101   tree default_argument;
12102   cp_token *token;
12103   const char *saved_message;
12104
12105   /* In a template parameter, `>' is not an operator.
12106
12107      [temp.param]
12108
12109      When parsing a default template-argument for a non-type
12110      template-parameter, the first non-nested `>' is taken as the end
12111      of the template parameter-list rather than a greater-than
12112      operator.  */
12113   greater_than_is_operator_p = !template_parm_p;
12114
12115   /* Type definitions may not appear in parameter types.  */
12116   saved_message = parser->type_definition_forbidden_message;
12117   parser->type_definition_forbidden_message
12118     = "types may not be defined in parameter types";
12119
12120   /* Parse the declaration-specifiers.  */
12121   cp_parser_decl_specifier_seq (parser,
12122                                 CP_PARSER_FLAGS_NONE,
12123                                 &decl_specifiers,
12124                                 &declares_class_or_enum);
12125   /* If an error occurred, there's no reason to attempt to parse the
12126      rest of the declaration.  */
12127   if (cp_parser_error_occurred (parser))
12128     {
12129       parser->type_definition_forbidden_message = saved_message;
12130       return NULL;
12131     }
12132
12133   /* Peek at the next token.  */
12134   token = cp_lexer_peek_token (parser->lexer);
12135   /* If the next token is a `)', `,', `=', `>', or `...', then there
12136      is no declarator.  */
12137   if (token->type == CPP_CLOSE_PAREN
12138       || token->type == CPP_COMMA
12139       || token->type == CPP_EQ
12140       || token->type == CPP_ELLIPSIS
12141       || token->type == CPP_GREATER)
12142     {
12143       declarator = NULL;
12144       if (parenthesized_p)
12145         *parenthesized_p = false;
12146     }
12147   /* Otherwise, there should be a declarator.  */
12148   else
12149     {
12150       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12151       parser->default_arg_ok_p = false;
12152
12153       /* After seeing a decl-specifier-seq, if the next token is not a
12154          "(", there is no possibility that the code is a valid
12155          expression.  Therefore, if parsing tentatively, we commit at
12156          this point.  */
12157       if (!parser->in_template_argument_list_p
12158           /* In an expression context, having seen:
12159
12160                (int((char ...
12161
12162              we cannot be sure whether we are looking at a
12163              function-type (taking a "char" as a parameter) or a cast
12164              of some object of type "char" to "int".  */
12165           && !parser->in_type_id_in_expr_p
12166           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12167           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12168         cp_parser_commit_to_tentative_parse (parser);
12169       /* Parse the declarator.  */
12170       declarator = cp_parser_declarator (parser,
12171                                          CP_PARSER_DECLARATOR_EITHER,
12172                                          /*ctor_dtor_or_conv_p=*/NULL,
12173                                          parenthesized_p,
12174                                          /*member_p=*/false);
12175       parser->default_arg_ok_p = saved_default_arg_ok_p;
12176       /* After the declarator, allow more attributes.  */
12177       decl_specifiers.attributes
12178         = chainon (decl_specifiers.attributes,
12179                    cp_parser_attributes_opt (parser));
12180     }
12181
12182   /* The restriction on defining new types applies only to the type
12183      of the parameter, not to the default argument.  */
12184   parser->type_definition_forbidden_message = saved_message;
12185
12186   /* If the next token is `=', then process a default argument.  */
12187   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12188     {
12189       bool saved_greater_than_is_operator_p;
12190       /* Consume the `='.  */
12191       cp_lexer_consume_token (parser->lexer);
12192
12193       /* If we are defining a class, then the tokens that make up the
12194          default argument must be saved and processed later.  */
12195       if (!template_parm_p && at_class_scope_p ()
12196           && TYPE_BEING_DEFINED (current_class_type))
12197         {
12198           unsigned depth = 0;
12199           cp_token *first_token;
12200           cp_token *token;
12201
12202           /* Add tokens until we have processed the entire default
12203              argument.  We add the range [first_token, token).  */
12204           first_token = cp_lexer_peek_token (parser->lexer);
12205           while (true)
12206             {
12207               bool done = false;
12208
12209               /* Peek at the next token.  */
12210               token = cp_lexer_peek_token (parser->lexer);
12211               /* What we do depends on what token we have.  */
12212               switch (token->type)
12213                 {
12214                   /* In valid code, a default argument must be
12215                      immediately followed by a `,' `)', or `...'.  */
12216                 case CPP_COMMA:
12217                 case CPP_CLOSE_PAREN:
12218                 case CPP_ELLIPSIS:
12219                   /* If we run into a non-nested `;', `}', or `]',
12220                      then the code is invalid -- but the default
12221                      argument is certainly over.  */
12222                 case CPP_SEMICOLON:
12223                 case CPP_CLOSE_BRACE:
12224                 case CPP_CLOSE_SQUARE:
12225                   if (depth == 0)
12226                     done = true;
12227                   /* Update DEPTH, if necessary.  */
12228                   else if (token->type == CPP_CLOSE_PAREN
12229                            || token->type == CPP_CLOSE_BRACE
12230                            || token->type == CPP_CLOSE_SQUARE)
12231                     --depth;
12232                   break;
12233
12234                 case CPP_OPEN_PAREN:
12235                 case CPP_OPEN_SQUARE:
12236                 case CPP_OPEN_BRACE:
12237                   ++depth;
12238                   break;
12239
12240                 case CPP_GREATER:
12241                   /* If we see a non-nested `>', and `>' is not an
12242                      operator, then it marks the end of the default
12243                      argument.  */
12244                   if (!depth && !greater_than_is_operator_p)
12245                     done = true;
12246                   break;
12247
12248                   /* If we run out of tokens, issue an error message.  */
12249                 case CPP_EOF:
12250                 case CPP_PRAGMA_EOL:
12251                   error ("file ends in default argument");
12252                   done = true;
12253                   break;
12254
12255                 case CPP_NAME:
12256                 case CPP_SCOPE:
12257                   /* In these cases, we should look for template-ids.
12258                      For example, if the default argument is
12259                      `X<int, double>()', we need to do name lookup to
12260                      figure out whether or not `X' is a template; if
12261                      so, the `,' does not end the default argument.
12262
12263                      That is not yet done.  */
12264                   break;
12265
12266                 default:
12267                   break;
12268                 }
12269
12270               /* If we've reached the end, stop.  */
12271               if (done)
12272                 break;
12273
12274               /* Add the token to the token block.  */
12275               token = cp_lexer_consume_token (parser->lexer);
12276             }
12277
12278           /* Create a DEFAULT_ARG to represented the unparsed default
12279              argument.  */
12280           default_argument = make_node (DEFAULT_ARG);
12281           DEFARG_TOKENS (default_argument)
12282             = cp_token_cache_new (first_token, token);
12283           DEFARG_INSTANTIATIONS (default_argument) = NULL;
12284         }
12285       /* Outside of a class definition, we can just parse the
12286          assignment-expression.  */
12287       else
12288         {
12289           bool saved_local_variables_forbidden_p;
12290
12291           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12292              set correctly.  */
12293           saved_greater_than_is_operator_p
12294             = parser->greater_than_is_operator_p;
12295           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12296           /* Local variable names (and the `this' keyword) may not
12297              appear in a default argument.  */
12298           saved_local_variables_forbidden_p
12299             = parser->local_variables_forbidden_p;
12300           parser->local_variables_forbidden_p = true;
12301           /* Parse the assignment-expression.  */
12302           default_argument
12303             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12304           /* Restore saved state.  */
12305           parser->greater_than_is_operator_p
12306             = saved_greater_than_is_operator_p;
12307           parser->local_variables_forbidden_p
12308             = saved_local_variables_forbidden_p;
12309         }
12310       if (!parser->default_arg_ok_p)
12311         {
12312           if (!flag_pedantic_errors)
12313             warning (0, "deprecated use of default argument for parameter of non-function");
12314           else
12315             {
12316               error ("default arguments are only permitted for function parameters");
12317               default_argument = NULL_TREE;
12318             }
12319         }
12320     }
12321   else
12322     default_argument = NULL_TREE;
12323
12324   return make_parameter_declarator (&decl_specifiers,
12325                                     declarator,
12326                                     default_argument);
12327 }
12328
12329 /* Parse a function-body.
12330
12331    function-body:
12332      compound_statement  */
12333
12334 static void
12335 cp_parser_function_body (cp_parser *parser)
12336 {
12337   cp_parser_compound_statement (parser, NULL, false);
12338 }
12339
12340 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12341    true if a ctor-initializer was present.  */
12342
12343 static bool
12344 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12345 {
12346   tree body;
12347   bool ctor_initializer_p;
12348
12349   /* Begin the function body.  */
12350   body = begin_function_body ();
12351   /* Parse the optional ctor-initializer.  */
12352   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12353   /* Parse the function-body.  */
12354   cp_parser_function_body (parser);
12355   /* Finish the function body.  */
12356   finish_function_body (body);
12357
12358   return ctor_initializer_p;
12359 }
12360
12361 /* Parse an initializer.
12362
12363    initializer:
12364      = initializer-clause
12365      ( expression-list )
12366
12367    Returns an expression representing the initializer.  If no
12368    initializer is present, NULL_TREE is returned.
12369
12370    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12371    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12372    set to FALSE if there is no initializer present.  If there is an
12373    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12374    is set to true; otherwise it is set to false.  */
12375
12376 static tree
12377 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12378                        bool* non_constant_p)
12379 {
12380   cp_token *token;
12381   tree init;
12382
12383   /* Peek at the next token.  */
12384   token = cp_lexer_peek_token (parser->lexer);
12385
12386   /* Let our caller know whether or not this initializer was
12387      parenthesized.  */
12388   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12389   /* Assume that the initializer is constant.  */
12390   *non_constant_p = false;
12391
12392   if (token->type == CPP_EQ)
12393     {
12394       /* Consume the `='.  */
12395       cp_lexer_consume_token (parser->lexer);
12396       /* Parse the initializer-clause.  */
12397       init = cp_parser_initializer_clause (parser, non_constant_p);
12398     }
12399   else if (token->type == CPP_OPEN_PAREN)
12400     init = cp_parser_parenthesized_expression_list (parser, false,
12401                                                     /*cast_p=*/false,
12402                                                     non_constant_p);
12403   else
12404     {
12405       /* Anything else is an error.  */
12406       cp_parser_error (parser, "expected initializer");
12407       init = error_mark_node;
12408     }
12409
12410   return init;
12411 }
12412
12413 /* Parse an initializer-clause.
12414
12415    initializer-clause:
12416      assignment-expression
12417      { initializer-list , [opt] }
12418      { }
12419
12420    Returns an expression representing the initializer.
12421
12422    If the `assignment-expression' production is used the value
12423    returned is simply a representation for the expression.
12424
12425    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12426    the elements of the initializer-list (or NULL, if the last
12427    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12428    NULL_TREE.  There is no way to detect whether or not the optional
12429    trailing `,' was provided.  NON_CONSTANT_P is as for
12430    cp_parser_initializer.  */
12431
12432 static tree
12433 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12434 {
12435   tree initializer;
12436
12437   /* Assume the expression is constant.  */
12438   *non_constant_p = false;
12439
12440   /* If it is not a `{', then we are looking at an
12441      assignment-expression.  */
12442   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12443     {
12444       initializer
12445         = cp_parser_constant_expression (parser,
12446                                         /*allow_non_constant_p=*/true,
12447                                         non_constant_p);
12448       if (!*non_constant_p)
12449         initializer = fold_non_dependent_expr (initializer);
12450     }
12451   else
12452     {
12453       /* Consume the `{' token.  */
12454       cp_lexer_consume_token (parser->lexer);
12455       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12456       initializer = make_node (CONSTRUCTOR);
12457       /* If it's not a `}', then there is a non-trivial initializer.  */
12458       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12459         {
12460           /* Parse the initializer list.  */
12461           CONSTRUCTOR_ELTS (initializer)
12462             = cp_parser_initializer_list (parser, non_constant_p);
12463           /* A trailing `,' token is allowed.  */
12464           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12465             cp_lexer_consume_token (parser->lexer);
12466         }
12467       /* Now, there should be a trailing `}'.  */
12468       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12469     }
12470
12471   return initializer;
12472 }
12473
12474 /* Parse an initializer-list.
12475
12476    initializer-list:
12477      initializer-clause
12478      initializer-list , initializer-clause
12479
12480    GNU Extension:
12481
12482    initializer-list:
12483      identifier : initializer-clause
12484      initializer-list, identifier : initializer-clause
12485
12486    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12487    for the initializer.  If the INDEX of the elt is non-NULL, it is the
12488    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12489    as for cp_parser_initializer.  */
12490
12491 static VEC(constructor_elt,gc) *
12492 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12493 {
12494   VEC(constructor_elt,gc) *v = NULL;
12495
12496   /* Assume all of the expressions are constant.  */
12497   *non_constant_p = false;
12498
12499   /* Parse the rest of the list.  */
12500   while (true)
12501     {
12502       cp_token *token;
12503       tree identifier;
12504       tree initializer;
12505       bool clause_non_constant_p;
12506
12507       /* If the next token is an identifier and the following one is a
12508          colon, we are looking at the GNU designated-initializer
12509          syntax.  */
12510       if (cp_parser_allow_gnu_extensions_p (parser)
12511           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12512           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12513         {
12514           /* Consume the identifier.  */
12515           identifier = cp_lexer_consume_token (parser->lexer)->value;
12516           /* Consume the `:'.  */
12517           cp_lexer_consume_token (parser->lexer);
12518         }
12519       else
12520         identifier = NULL_TREE;
12521
12522       /* Parse the initializer.  */
12523       initializer = cp_parser_initializer_clause (parser,
12524                                                   &clause_non_constant_p);
12525       /* If any clause is non-constant, so is the entire initializer.  */
12526       if (clause_non_constant_p)
12527         *non_constant_p = true;
12528
12529       /* Add it to the vector.  */
12530       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12531
12532       /* If the next token is not a comma, we have reached the end of
12533          the list.  */
12534       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12535         break;
12536
12537       /* Peek at the next token.  */
12538       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12539       /* If the next token is a `}', then we're still done.  An
12540          initializer-clause can have a trailing `,' after the
12541          initializer-list and before the closing `}'.  */
12542       if (token->type == CPP_CLOSE_BRACE)
12543         break;
12544
12545       /* Consume the `,' token.  */
12546       cp_lexer_consume_token (parser->lexer);
12547     }
12548
12549   return v;
12550 }
12551
12552 /* Classes [gram.class] */
12553
12554 /* Parse a class-name.
12555
12556    class-name:
12557      identifier
12558      template-id
12559
12560    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12561    to indicate that names looked up in dependent types should be
12562    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12563    keyword has been used to indicate that the name that appears next
12564    is a template.  TAG_TYPE indicates the explicit tag given before
12565    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12566    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12567    is the class being defined in a class-head.
12568
12569    Returns the TYPE_DECL representing the class.  */
12570
12571 static tree
12572 cp_parser_class_name (cp_parser *parser,
12573                       bool typename_keyword_p,
12574                       bool template_keyword_p,
12575                       enum tag_types tag_type,
12576                       bool check_dependency_p,
12577                       bool class_head_p,
12578                       bool is_declaration)
12579 {
12580   tree decl;
12581   tree scope;
12582   bool typename_p;
12583   cp_token *token;
12584
12585   /* All class-names start with an identifier.  */
12586   token = cp_lexer_peek_token (parser->lexer);
12587   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12588     {
12589       cp_parser_error (parser, "expected class-name");
12590       return error_mark_node;
12591     }
12592
12593   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12594      to a template-id, so we save it here.  */
12595   scope = parser->scope;
12596   if (scope == error_mark_node)
12597     return error_mark_node;
12598
12599   /* Any name names a type if we're following the `typename' keyword
12600      in a qualified name where the enclosing scope is type-dependent.  */
12601   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12602                 && dependent_type_p (scope));
12603   /* Handle the common case (an identifier, but not a template-id)
12604      efficiently.  */
12605   if (token->type == CPP_NAME
12606       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12607     {
12608       cp_token *identifier_token;
12609       tree identifier;
12610       bool ambiguous_p;
12611
12612       /* Look for the identifier.  */
12613       identifier_token = cp_lexer_peek_token (parser->lexer);
12614       ambiguous_p = identifier_token->ambiguous_p;
12615       identifier = cp_parser_identifier (parser);
12616       /* If the next token isn't an identifier, we are certainly not
12617          looking at a class-name.  */
12618       if (identifier == error_mark_node)
12619         decl = error_mark_node;
12620       /* If we know this is a type-name, there's no need to look it
12621          up.  */
12622       else if (typename_p)
12623         decl = identifier;
12624       else
12625         {
12626           tree ambiguous_decls;
12627           /* If we already know that this lookup is ambiguous, then
12628              we've already issued an error message; there's no reason
12629              to check again.  */
12630           if (ambiguous_p)
12631             {
12632               cp_parser_simulate_error (parser);
12633               return error_mark_node;
12634             }
12635           /* If the next token is a `::', then the name must be a type
12636              name.
12637
12638              [basic.lookup.qual]
12639
12640              During the lookup for a name preceding the :: scope
12641              resolution operator, object, function, and enumerator
12642              names are ignored.  */
12643           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12644             tag_type = typename_type;
12645           /* Look up the name.  */
12646           decl = cp_parser_lookup_name (parser, identifier,
12647                                         tag_type,
12648                                         /*is_template=*/false,
12649                                         /*is_namespace=*/false,
12650                                         check_dependency_p,
12651                                         &ambiguous_decls);
12652           if (ambiguous_decls)
12653             {
12654               error ("reference to %qD is ambiguous", identifier);
12655               print_candidates (ambiguous_decls);
12656               if (cp_parser_parsing_tentatively (parser))
12657                 {
12658                   identifier_token->ambiguous_p = true;
12659                   cp_parser_simulate_error (parser);
12660                 }
12661               return error_mark_node;
12662             }
12663         }
12664     }
12665   else
12666     {
12667       /* Try a template-id.  */
12668       decl = cp_parser_template_id (parser, template_keyword_p,
12669                                     check_dependency_p,
12670                                     is_declaration);
12671       if (decl == error_mark_node)
12672         return error_mark_node;
12673     }
12674
12675   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12676
12677   /* If this is a typename, create a TYPENAME_TYPE.  */
12678   if (typename_p && decl != error_mark_node)
12679     {
12680       decl = make_typename_type (scope, decl, typename_type,
12681                                  /*complain=*/tf_error);
12682       if (decl != error_mark_node)
12683         decl = TYPE_NAME (decl);
12684     }
12685
12686   /* Check to see that it is really the name of a class.  */
12687   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12688       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12689       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12690     /* Situations like this:
12691
12692          template <typename T> struct A {
12693            typename T::template X<int>::I i;
12694          };
12695
12696        are problematic.  Is `T::template X<int>' a class-name?  The
12697        standard does not seem to be definitive, but there is no other
12698        valid interpretation of the following `::'.  Therefore, those
12699        names are considered class-names.  */
12700     decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12701   else if (decl == error_mark_node
12702            || TREE_CODE (decl) != TYPE_DECL
12703            || TREE_TYPE (decl) == error_mark_node
12704            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12705     {
12706       cp_parser_error (parser, "expected class-name");
12707       return error_mark_node;
12708     }
12709
12710   return decl;
12711 }
12712
12713 /* Parse a class-specifier.
12714
12715    class-specifier:
12716      class-head { member-specification [opt] }
12717
12718    Returns the TREE_TYPE representing the class.  */
12719
12720 static tree
12721 cp_parser_class_specifier (cp_parser* parser)
12722 {
12723   cp_token *token;
12724   tree type;
12725   tree attributes = NULL_TREE;
12726   int has_trailing_semicolon;
12727   bool nested_name_specifier_p;
12728   unsigned saved_num_template_parameter_lists;
12729   tree old_scope = NULL_TREE;
12730   tree scope = NULL_TREE;
12731
12732   push_deferring_access_checks (dk_no_deferred);
12733
12734   /* Parse the class-head.  */
12735   type = cp_parser_class_head (parser,
12736                                &nested_name_specifier_p,
12737                                &attributes);
12738   /* If the class-head was a semantic disaster, skip the entire body
12739      of the class.  */
12740   if (!type)
12741     {
12742       cp_parser_skip_to_end_of_block_or_statement (parser);
12743       pop_deferring_access_checks ();
12744       return error_mark_node;
12745     }
12746
12747   /* Look for the `{'.  */
12748   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12749     {
12750       pop_deferring_access_checks ();
12751       return error_mark_node;
12752     }
12753
12754   /* Issue an error message if type-definitions are forbidden here.  */
12755   cp_parser_check_type_definition (parser);
12756   /* Remember that we are defining one more class.  */
12757   ++parser->num_classes_being_defined;
12758   /* Inside the class, surrounding template-parameter-lists do not
12759      apply.  */
12760   saved_num_template_parameter_lists
12761     = parser->num_template_parameter_lists;
12762   parser->num_template_parameter_lists = 0;
12763
12764   /* Start the class.  */
12765   if (nested_name_specifier_p)
12766     {
12767       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12768       old_scope = push_inner_scope (scope);
12769     }
12770   type = begin_class_definition (type);
12771
12772   if (type == error_mark_node)
12773     /* If the type is erroneous, skip the entire body of the class.  */
12774     cp_parser_skip_to_closing_brace (parser);
12775   else
12776     /* Parse the member-specification.  */
12777     cp_parser_member_specification_opt (parser);
12778
12779   /* Look for the trailing `}'.  */
12780   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12781   /* We get better error messages by noticing a common problem: a
12782      missing trailing `;'.  */
12783   token = cp_lexer_peek_token (parser->lexer);
12784   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12785   /* Look for trailing attributes to apply to this class.  */
12786   if (cp_parser_allow_gnu_extensions_p (parser))
12787     {
12788       tree sub_attr = cp_parser_attributes_opt (parser);
12789       attributes = chainon (attributes, sub_attr);
12790     }
12791   if (type != error_mark_node)
12792     type = finish_struct (type, attributes);
12793   if (nested_name_specifier_p)
12794     pop_inner_scope (old_scope, scope);
12795   /* If this class is not itself within the scope of another class,
12796      then we need to parse the bodies of all of the queued function
12797      definitions.  Note that the queued functions defined in a class
12798      are not always processed immediately following the
12799      class-specifier for that class.  Consider:
12800
12801        struct A {
12802          struct B { void f() { sizeof (A); } };
12803        };
12804
12805      If `f' were processed before the processing of `A' were
12806      completed, there would be no way to compute the size of `A'.
12807      Note that the nesting we are interested in here is lexical --
12808      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12809      for:
12810
12811        struct A { struct B; };
12812        struct A::B { void f() { } };
12813
12814      there is no need to delay the parsing of `A::B::f'.  */
12815   if (--parser->num_classes_being_defined == 0)
12816     {
12817       tree queue_entry;
12818       tree fn;
12819       tree class_type = NULL_TREE;
12820       tree pushed_scope = NULL_TREE;
12821  
12822       /* In a first pass, parse default arguments to the functions.
12823          Then, in a second pass, parse the bodies of the functions.
12824          This two-phased approach handles cases like:
12825
12826             struct S {
12827               void f() { g(); }
12828               void g(int i = 3);
12829             };
12830
12831          */
12832       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12833              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12834            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12835            TREE_PURPOSE (parser->unparsed_functions_queues)
12836              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12837         {
12838           fn = TREE_VALUE (queue_entry);
12839           /* If there are default arguments that have not yet been processed,
12840              take care of them now.  */
12841           if (class_type != TREE_PURPOSE (queue_entry))
12842             {
12843               if (pushed_scope)
12844                 pop_scope (pushed_scope);
12845               class_type = TREE_PURPOSE (queue_entry);
12846               pushed_scope = push_scope (class_type);
12847             }
12848           /* Make sure that any template parameters are in scope.  */
12849           maybe_begin_member_template_processing (fn);
12850           /* Parse the default argument expressions.  */
12851           cp_parser_late_parsing_default_args (parser, fn);
12852           /* Remove any template parameters from the symbol table.  */
12853           maybe_end_member_template_processing ();
12854         }
12855       if (pushed_scope)
12856         pop_scope (pushed_scope);
12857       /* Now parse the body of the functions.  */
12858       for (TREE_VALUE (parser->unparsed_functions_queues)
12859              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12860            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12861            TREE_VALUE (parser->unparsed_functions_queues)
12862              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12863         {
12864           /* Figure out which function we need to process.  */
12865           fn = TREE_VALUE (queue_entry);
12866           /* Parse the function.  */
12867           cp_parser_late_parsing_for_member (parser, fn);
12868         }
12869     }
12870
12871   /* Put back any saved access checks.  */
12872   pop_deferring_access_checks ();
12873
12874   /* Restore the count of active template-parameter-lists.  */
12875   parser->num_template_parameter_lists
12876     = saved_num_template_parameter_lists;
12877
12878   return type;
12879 }
12880
12881 /* Parse a class-head.
12882
12883    class-head:
12884      class-key identifier [opt] base-clause [opt]
12885      class-key nested-name-specifier identifier base-clause [opt]
12886      class-key nested-name-specifier [opt] template-id
12887        base-clause [opt]
12888
12889    GNU Extensions:
12890      class-key attributes identifier [opt] base-clause [opt]
12891      class-key attributes nested-name-specifier identifier base-clause [opt]
12892      class-key attributes nested-name-specifier [opt] template-id
12893        base-clause [opt]
12894
12895    Returns the TYPE of the indicated class.  Sets
12896    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12897    involving a nested-name-specifier was used, and FALSE otherwise.
12898
12899    Returns error_mark_node if this is not a class-head.
12900
12901    Returns NULL_TREE if the class-head is syntactically valid, but
12902    semantically invalid in a way that means we should skip the entire
12903    body of the class.  */
12904
12905 static tree
12906 cp_parser_class_head (cp_parser* parser,
12907                       bool* nested_name_specifier_p,
12908                       tree *attributes_p)
12909 {
12910   tree nested_name_specifier;
12911   enum tag_types class_key;
12912   tree id = NULL_TREE;
12913   tree type = NULL_TREE;
12914   tree attributes;
12915   bool template_id_p = false;
12916   bool qualified_p = false;
12917   bool invalid_nested_name_p = false;
12918   bool invalid_explicit_specialization_p = false;
12919   tree pushed_scope = NULL_TREE;
12920   unsigned num_templates;
12921   tree bases;
12922
12923   /* Assume no nested-name-specifier will be present.  */
12924   *nested_name_specifier_p = false;
12925   /* Assume no template parameter lists will be used in defining the
12926      type.  */
12927   num_templates = 0;
12928
12929   /* Look for the class-key.  */
12930   class_key = cp_parser_class_key (parser);
12931   if (class_key == none_type)
12932     return error_mark_node;
12933
12934   /* Parse the attributes.  */
12935   attributes = cp_parser_attributes_opt (parser);
12936
12937   /* If the next token is `::', that is invalid -- but sometimes
12938      people do try to write:
12939
12940        struct ::S {};
12941
12942      Handle this gracefully by accepting the extra qualifier, and then
12943      issuing an error about it later if this really is a
12944      class-head.  If it turns out just to be an elaborated type
12945      specifier, remain silent.  */
12946   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12947     qualified_p = true;
12948
12949   push_deferring_access_checks (dk_no_check);
12950
12951   /* Determine the name of the class.  Begin by looking for an
12952      optional nested-name-specifier.  */
12953   nested_name_specifier
12954     = cp_parser_nested_name_specifier_opt (parser,
12955                                            /*typename_keyword_p=*/false,
12956                                            /*check_dependency_p=*/false,
12957                                            /*type_p=*/false,
12958                                            /*is_declaration=*/false);
12959   /* If there was a nested-name-specifier, then there *must* be an
12960      identifier.  */
12961   if (nested_name_specifier)
12962     {
12963       /* Although the grammar says `identifier', it really means
12964          `class-name' or `template-name'.  You are only allowed to
12965          define a class that has already been declared with this
12966          syntax.
12967
12968          The proposed resolution for Core Issue 180 says that whever
12969          you see `class T::X' you should treat `X' as a type-name.
12970
12971          It is OK to define an inaccessible class; for example:
12972
12973            class A { class B; };
12974            class A::B {};
12975
12976          We do not know if we will see a class-name, or a
12977          template-name.  We look for a class-name first, in case the
12978          class-name is a template-id; if we looked for the
12979          template-name first we would stop after the template-name.  */
12980       cp_parser_parse_tentatively (parser);
12981       type = cp_parser_class_name (parser,
12982                                    /*typename_keyword_p=*/false,
12983                                    /*template_keyword_p=*/false,
12984                                    class_type,
12985                                    /*check_dependency_p=*/false,
12986                                    /*class_head_p=*/true,
12987                                    /*is_declaration=*/false);
12988       /* If that didn't work, ignore the nested-name-specifier.  */
12989       if (!cp_parser_parse_definitely (parser))
12990         {
12991           invalid_nested_name_p = true;
12992           id = cp_parser_identifier (parser);
12993           if (id == error_mark_node)
12994             id = NULL_TREE;
12995         }
12996       /* If we could not find a corresponding TYPE, treat this
12997          declaration like an unqualified declaration.  */
12998       if (type == error_mark_node)
12999         nested_name_specifier = NULL_TREE;
13000       /* Otherwise, count the number of templates used in TYPE and its
13001          containing scopes.  */
13002       else
13003         {
13004           tree scope;
13005
13006           for (scope = TREE_TYPE (type);
13007                scope && TREE_CODE (scope) != NAMESPACE_DECL;
13008                scope = (TYPE_P (scope)
13009                         ? TYPE_CONTEXT (scope)
13010                         : DECL_CONTEXT (scope)))
13011             if (TYPE_P (scope)
13012                 && CLASS_TYPE_P (scope)
13013                 && CLASSTYPE_TEMPLATE_INFO (scope)
13014                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13015                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13016               ++num_templates;
13017         }
13018     }
13019   /* Otherwise, the identifier is optional.  */
13020   else
13021     {
13022       /* We don't know whether what comes next is a template-id,
13023          an identifier, or nothing at all.  */
13024       cp_parser_parse_tentatively (parser);
13025       /* Check for a template-id.  */
13026       id = cp_parser_template_id (parser,
13027                                   /*template_keyword_p=*/false,
13028                                   /*check_dependency_p=*/true,
13029                                   /*is_declaration=*/true);
13030       /* If that didn't work, it could still be an identifier.  */
13031       if (!cp_parser_parse_definitely (parser))
13032         {
13033           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13034             id = cp_parser_identifier (parser);
13035           else
13036             id = NULL_TREE;
13037         }
13038       else
13039         {
13040           template_id_p = true;
13041           ++num_templates;
13042         }
13043     }
13044
13045   pop_deferring_access_checks ();
13046
13047   if (id)
13048     cp_parser_check_for_invalid_template_id (parser, id);
13049
13050   /* If it's not a `:' or a `{' then we can't really be looking at a
13051      class-head, since a class-head only appears as part of a
13052      class-specifier.  We have to detect this situation before calling
13053      xref_tag, since that has irreversible side-effects.  */
13054   if (!cp_parser_next_token_starts_class_definition_p (parser))
13055     {
13056       cp_parser_error (parser, "expected %<{%> or %<:%>");
13057       return error_mark_node;
13058     }
13059
13060   /* At this point, we're going ahead with the class-specifier, even
13061      if some other problem occurs.  */
13062   cp_parser_commit_to_tentative_parse (parser);
13063   /* Issue the error about the overly-qualified name now.  */
13064   if (qualified_p)
13065     cp_parser_error (parser,
13066                      "global qualification of class name is invalid");
13067   else if (invalid_nested_name_p)
13068     cp_parser_error (parser,
13069                      "qualified name does not name a class");
13070   else if (nested_name_specifier)
13071     {
13072       tree scope;
13073
13074       /* Reject typedef-names in class heads.  */
13075       if (!DECL_IMPLICIT_TYPEDEF_P (type))
13076         {
13077           error ("invalid class name in declaration of %qD", type);
13078           type = NULL_TREE;
13079           goto done;
13080         }
13081
13082       /* Figure out in what scope the declaration is being placed.  */
13083       scope = current_scope ();
13084       /* If that scope does not contain the scope in which the
13085          class was originally declared, the program is invalid.  */
13086       if (scope && !is_ancestor (scope, nested_name_specifier))
13087         {
13088           error ("declaration of %qD in %qD which does not enclose %qD",
13089                  type, scope, nested_name_specifier);
13090           type = NULL_TREE;
13091           goto done;
13092         }
13093       /* [dcl.meaning]
13094
13095          A declarator-id shall not be qualified exception of the
13096          definition of a ... nested class outside of its class
13097          ... [or] a the definition or explicit instantiation of a
13098          class member of a namespace outside of its namespace.  */
13099       if (scope == nested_name_specifier)
13100         {
13101           pedwarn ("extra qualification ignored");
13102           nested_name_specifier = NULL_TREE;
13103           num_templates = 0;
13104         }
13105     }
13106   /* An explicit-specialization must be preceded by "template <>".  If
13107      it is not, try to recover gracefully.  */
13108   if (at_namespace_scope_p ()
13109       && parser->num_template_parameter_lists == 0
13110       && template_id_p)
13111     {
13112       error ("an explicit specialization must be preceded by %<template <>%>");
13113       invalid_explicit_specialization_p = true;
13114       /* Take the same action that would have been taken by
13115          cp_parser_explicit_specialization.  */
13116       ++parser->num_template_parameter_lists;
13117       begin_specialization ();
13118     }
13119   /* There must be no "return" statements between this point and the
13120      end of this function; set "type "to the correct return value and
13121      use "goto done;" to return.  */
13122   /* Make sure that the right number of template parameters were
13123      present.  */
13124   if (!cp_parser_check_template_parameters (parser, num_templates))
13125     {
13126       /* If something went wrong, there is no point in even trying to
13127          process the class-definition.  */
13128       type = NULL_TREE;
13129       goto done;
13130     }
13131
13132   /* Look up the type.  */
13133   if (template_id_p)
13134     {
13135       type = TREE_TYPE (id);
13136       maybe_process_partial_specialization (type);
13137       if (nested_name_specifier)
13138         pushed_scope = push_scope (nested_name_specifier);
13139     }
13140   else if (nested_name_specifier)
13141     {
13142       tree class_type;
13143
13144       /* Given:
13145
13146             template <typename T> struct S { struct T };
13147             template <typename T> struct S<T>::T { };
13148
13149          we will get a TYPENAME_TYPE when processing the definition of
13150          `S::T'.  We need to resolve it to the actual type before we
13151          try to define it.  */
13152       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13153         {
13154           class_type = resolve_typename_type (TREE_TYPE (type),
13155                                               /*only_current_p=*/false);
13156           if (class_type != error_mark_node)
13157             type = TYPE_NAME (class_type);
13158           else
13159             {
13160               cp_parser_error (parser, "could not resolve typename type");
13161               type = error_mark_node;
13162             }
13163         }
13164
13165       maybe_process_partial_specialization (TREE_TYPE (type));
13166       class_type = current_class_type;
13167       /* Enter the scope indicated by the nested-name-specifier.  */
13168       pushed_scope = push_scope (nested_name_specifier);
13169       /* Get the canonical version of this type.  */
13170       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13171       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13172           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13173         {
13174           type = push_template_decl (type);
13175           if (type == error_mark_node)
13176             {
13177               type = NULL_TREE;
13178               goto done;
13179             }
13180         }
13181
13182       type = TREE_TYPE (type);
13183       *nested_name_specifier_p = true;
13184     }
13185   else      /* The name is not a nested name.  */
13186     {
13187       /* If the class was unnamed, create a dummy name.  */
13188       if (!id)
13189         id = make_anon_name ();
13190       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13191                        parser->num_template_parameter_lists);
13192     }
13193
13194   /* Indicate whether this class was declared as a `class' or as a
13195      `struct'.  */
13196   if (TREE_CODE (type) == RECORD_TYPE)
13197     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13198   cp_parser_check_class_key (class_key, type);
13199
13200   /* If this type was already complete, and we see another definition,
13201      that's an error.  */
13202   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13203     {
13204       error ("redefinition of %q#T", type);
13205       error ("previous definition of %q+#T", type);
13206       type = NULL_TREE;
13207       goto done;
13208     }
13209
13210   /* We will have entered the scope containing the class; the names of
13211      base classes should be looked up in that context.  For example:
13212
13213        struct A { struct B {}; struct C; };
13214        struct A::C : B {};
13215
13216      is valid.  */
13217   bases = NULL_TREE;
13218
13219   /* Get the list of base-classes, if there is one.  */
13220   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13221     bases = cp_parser_base_clause (parser);
13222
13223   /* Process the base classes.  */
13224   xref_basetypes (type, bases);
13225
13226  done:
13227   /* Leave the scope given by the nested-name-specifier.  We will
13228      enter the class scope itself while processing the members.  */
13229   if (pushed_scope)
13230     pop_scope (pushed_scope);
13231
13232   if (invalid_explicit_specialization_p)
13233     {
13234       end_specialization ();
13235       --parser->num_template_parameter_lists;
13236     }
13237   *attributes_p = attributes;
13238   return type;
13239 }
13240
13241 /* Parse a class-key.
13242
13243    class-key:
13244      class
13245      struct
13246      union
13247
13248    Returns the kind of class-key specified, or none_type to indicate
13249    error.  */
13250
13251 static enum tag_types
13252 cp_parser_class_key (cp_parser* parser)
13253 {
13254   cp_token *token;
13255   enum tag_types tag_type;
13256
13257   /* Look for the class-key.  */
13258   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13259   if (!token)
13260     return none_type;
13261
13262   /* Check to see if the TOKEN is a class-key.  */
13263   tag_type = cp_parser_token_is_class_key (token);
13264   if (!tag_type)
13265     cp_parser_error (parser, "expected class-key");
13266   return tag_type;
13267 }
13268
13269 /* Parse an (optional) member-specification.
13270
13271    member-specification:
13272      member-declaration member-specification [opt]
13273      access-specifier : member-specification [opt]  */
13274
13275 static void
13276 cp_parser_member_specification_opt (cp_parser* parser)
13277 {
13278   while (true)
13279     {
13280       cp_token *token;
13281       enum rid keyword;
13282
13283       /* Peek at the next token.  */
13284       token = cp_lexer_peek_token (parser->lexer);
13285       /* If it's a `}', or EOF then we've seen all the members.  */
13286       if (token->type == CPP_CLOSE_BRACE
13287           || token->type == CPP_EOF
13288           || token->type == CPP_PRAGMA_EOL)
13289         break;
13290
13291       /* See if this token is a keyword.  */
13292       keyword = token->keyword;
13293       switch (keyword)
13294         {
13295         case RID_PUBLIC:
13296         case RID_PROTECTED:
13297         case RID_PRIVATE:
13298           /* Consume the access-specifier.  */
13299           cp_lexer_consume_token (parser->lexer);
13300           /* Remember which access-specifier is active.  */
13301           current_access_specifier = token->value;
13302           /* Look for the `:'.  */
13303           cp_parser_require (parser, CPP_COLON, "`:'");
13304           break;
13305
13306         default:
13307           /* Accept #pragmas at class scope.  */
13308           if (token->type == CPP_PRAGMA)
13309             {
13310               cp_parser_pragma (parser, pragma_external);
13311               break;
13312             }
13313
13314           /* Otherwise, the next construction must be a
13315              member-declaration.  */
13316           cp_parser_member_declaration (parser);
13317         }
13318     }
13319 }
13320
13321 /* Parse a member-declaration.
13322
13323    member-declaration:
13324      decl-specifier-seq [opt] member-declarator-list [opt] ;
13325      function-definition ; [opt]
13326      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13327      using-declaration
13328      template-declaration
13329
13330    member-declarator-list:
13331      member-declarator
13332      member-declarator-list , member-declarator
13333
13334    member-declarator:
13335      declarator pure-specifier [opt]
13336      declarator constant-initializer [opt]
13337      identifier [opt] : constant-expression
13338
13339    GNU Extensions:
13340
13341    member-declaration:
13342      __extension__ member-declaration
13343
13344    member-declarator:
13345      declarator attributes [opt] pure-specifier [opt]
13346      declarator attributes [opt] constant-initializer [opt]
13347      identifier [opt] attributes [opt] : constant-expression  */
13348
13349 static void
13350 cp_parser_member_declaration (cp_parser* parser)
13351 {
13352   cp_decl_specifier_seq decl_specifiers;
13353   tree prefix_attributes;
13354   tree decl;
13355   int declares_class_or_enum;
13356   bool friend_p;
13357   cp_token *token;
13358   int saved_pedantic;
13359
13360   /* Check for the `__extension__' keyword.  */
13361   if (cp_parser_extension_opt (parser, &saved_pedantic))
13362     {
13363       /* Recurse.  */
13364       cp_parser_member_declaration (parser);
13365       /* Restore the old value of the PEDANTIC flag.  */
13366       pedantic = saved_pedantic;
13367
13368       return;
13369     }
13370
13371   /* Check for a template-declaration.  */
13372   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13373     {
13374       /* An explicit specialization here is an error condition, and we
13375          expect the specialization handler to detect and report this.  */
13376       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13377           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13378         cp_parser_explicit_specialization (parser);
13379       else
13380         cp_parser_template_declaration (parser, /*member_p=*/true);
13381
13382       return;
13383     }
13384
13385   /* Check for a using-declaration.  */
13386   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13387     {
13388       /* Parse the using-declaration.  */
13389       cp_parser_using_declaration (parser);
13390
13391       return;
13392     }
13393
13394   /* Check for @defs.  */
13395   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13396     {
13397       tree ivar, member;
13398       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13399       ivar = ivar_chains;
13400       while (ivar)
13401         {
13402           member = ivar;
13403           ivar = TREE_CHAIN (member);
13404           TREE_CHAIN (member) = NULL_TREE;
13405           finish_member_declaration (member);
13406         }
13407       return;
13408     }
13409
13410   /* Parse the decl-specifier-seq.  */
13411   cp_parser_decl_specifier_seq (parser,
13412                                 CP_PARSER_FLAGS_OPTIONAL,
13413                                 &decl_specifiers,
13414                                 &declares_class_or_enum);
13415   prefix_attributes = decl_specifiers.attributes;
13416   decl_specifiers.attributes = NULL_TREE;
13417   /* Check for an invalid type-name.  */
13418   if (!decl_specifiers.type
13419       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13420     return;
13421   /* If there is no declarator, then the decl-specifier-seq should
13422      specify a type.  */
13423   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13424     {
13425       /* If there was no decl-specifier-seq, and the next token is a
13426          `;', then we have something like:
13427
13428            struct S { ; };
13429
13430          [class.mem]
13431
13432          Each member-declaration shall declare at least one member
13433          name of the class.  */
13434       if (!decl_specifiers.any_specifiers_p)
13435         {
13436           cp_token *token = cp_lexer_peek_token (parser->lexer);
13437           if (pedantic && !token->in_system_header)
13438             pedwarn ("%Hextra %<;%>", &token->location);
13439         }
13440       else
13441         {
13442           tree type;
13443
13444           /* See if this declaration is a friend.  */
13445           friend_p = cp_parser_friend_p (&decl_specifiers);
13446           /* If there were decl-specifiers, check to see if there was
13447              a class-declaration.  */
13448           type = check_tag_decl (&decl_specifiers);
13449           /* Nested classes have already been added to the class, but
13450              a `friend' needs to be explicitly registered.  */
13451           if (friend_p)
13452             {
13453               /* If the `friend' keyword was present, the friend must
13454                  be introduced with a class-key.  */
13455                if (!declares_class_or_enum)
13456                  error ("a class-key must be used when declaring a friend");
13457                /* In this case:
13458
13459                     template <typename T> struct A {
13460                       friend struct A<T>::B;
13461                     };
13462
13463                   A<T>::B will be represented by a TYPENAME_TYPE, and
13464                   therefore not recognized by check_tag_decl.  */
13465                if (!type
13466                    && decl_specifiers.type
13467                    && TYPE_P (decl_specifiers.type))
13468                  type = decl_specifiers.type;
13469                if (!type || !TYPE_P (type))
13470                  error ("friend declaration does not name a class or "
13471                         "function");
13472                else
13473                  make_friend_class (current_class_type, type,
13474                                     /*complain=*/true);
13475             }
13476           /* If there is no TYPE, an error message will already have
13477              been issued.  */
13478           else if (!type || type == error_mark_node)
13479             ;
13480           /* An anonymous aggregate has to be handled specially; such
13481              a declaration really declares a data member (with a
13482              particular type), as opposed to a nested class.  */
13483           else if (ANON_AGGR_TYPE_P (type))
13484             {
13485               /* Remove constructors and such from TYPE, now that we
13486                  know it is an anonymous aggregate.  */
13487               fixup_anonymous_aggr (type);
13488               /* And make the corresponding data member.  */
13489               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13490               /* Add it to the class.  */
13491               finish_member_declaration (decl);
13492             }
13493           else
13494             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13495         }
13496     }
13497   else
13498     {
13499       /* See if these declarations will be friends.  */
13500       friend_p = cp_parser_friend_p (&decl_specifiers);
13501
13502       /* Keep going until we hit the `;' at the end of the
13503          declaration.  */
13504       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13505         {
13506           tree attributes = NULL_TREE;
13507           tree first_attribute;
13508
13509           /* Peek at the next token.  */
13510           token = cp_lexer_peek_token (parser->lexer);
13511
13512           /* Check for a bitfield declaration.  */
13513           if (token->type == CPP_COLON
13514               || (token->type == CPP_NAME
13515                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13516                   == CPP_COLON))
13517             {
13518               tree identifier;
13519               tree width;
13520
13521               /* Get the name of the bitfield.  Note that we cannot just
13522                  check TOKEN here because it may have been invalidated by
13523                  the call to cp_lexer_peek_nth_token above.  */
13524               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13525                 identifier = cp_parser_identifier (parser);
13526               else
13527                 identifier = NULL_TREE;
13528
13529               /* Consume the `:' token.  */
13530               cp_lexer_consume_token (parser->lexer);
13531               /* Get the width of the bitfield.  */
13532               width
13533                 = cp_parser_constant_expression (parser,
13534                                                  /*allow_non_constant=*/false,
13535                                                  NULL);
13536
13537               /* Look for attributes that apply to the bitfield.  */
13538               attributes = cp_parser_attributes_opt (parser);
13539               /* Remember which attributes are prefix attributes and
13540                  which are not.  */
13541               first_attribute = attributes;
13542               /* Combine the attributes.  */
13543               attributes = chainon (prefix_attributes, attributes);
13544
13545               /* Create the bitfield declaration.  */
13546               decl = grokbitfield (identifier
13547                                    ? make_id_declarator (NULL_TREE,
13548                                                          identifier,
13549                                                          sfk_none)
13550                                    : NULL,
13551                                    &decl_specifiers,
13552                                    width);
13553               /* Apply the attributes.  */
13554               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13555             }
13556           else
13557             {
13558               cp_declarator *declarator;
13559               tree initializer;
13560               tree asm_specification;
13561               int ctor_dtor_or_conv_p;
13562
13563               /* Parse the declarator.  */
13564               declarator
13565                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13566                                         &ctor_dtor_or_conv_p,
13567                                         /*parenthesized_p=*/NULL,
13568                                         /*member_p=*/true);
13569
13570               /* If something went wrong parsing the declarator, make sure
13571                  that we at least consume some tokens.  */
13572               if (declarator == cp_error_declarator)
13573                 {
13574                   /* Skip to the end of the statement.  */
13575                   cp_parser_skip_to_end_of_statement (parser);
13576                   /* If the next token is not a semicolon, that is
13577                      probably because we just skipped over the body of
13578                      a function.  So, we consume a semicolon if
13579                      present, but do not issue an error message if it
13580                      is not present.  */
13581                   if (cp_lexer_next_token_is (parser->lexer,
13582                                               CPP_SEMICOLON))
13583                     cp_lexer_consume_token (parser->lexer);
13584                   return;
13585                 }
13586
13587               if (declares_class_or_enum & 2)
13588                 cp_parser_check_for_definition_in_return_type
13589                   (declarator, decl_specifiers.type);
13590
13591               /* Look for an asm-specification.  */
13592               asm_specification = cp_parser_asm_specification_opt (parser);
13593               /* Look for attributes that apply to the declaration.  */
13594               attributes = cp_parser_attributes_opt (parser);
13595               /* Remember which attributes are prefix attributes and
13596                  which are not.  */
13597               first_attribute = attributes;
13598               /* Combine the attributes.  */
13599               attributes = chainon (prefix_attributes, attributes);
13600
13601               /* If it's an `=', then we have a constant-initializer or a
13602                  pure-specifier.  It is not correct to parse the
13603                  initializer before registering the member declaration
13604                  since the member declaration should be in scope while
13605                  its initializer is processed.  However, the rest of the
13606                  front end does not yet provide an interface that allows
13607                  us to handle this correctly.  */
13608               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13609                 {
13610                   /* In [class.mem]:
13611
13612                      A pure-specifier shall be used only in the declaration of
13613                      a virtual function.
13614
13615                      A member-declarator can contain a constant-initializer
13616                      only if it declares a static member of integral or
13617                      enumeration type.
13618
13619                      Therefore, if the DECLARATOR is for a function, we look
13620                      for a pure-specifier; otherwise, we look for a
13621                      constant-initializer.  When we call `grokfield', it will
13622                      perform more stringent semantics checks.  */
13623                   if (declarator->kind == cdk_function)
13624                     initializer = cp_parser_pure_specifier (parser);
13625                   else
13626                     /* Parse the initializer.  */
13627                     initializer = cp_parser_constant_initializer (parser);
13628                 }
13629               /* Otherwise, there is no initializer.  */
13630               else
13631                 initializer = NULL_TREE;
13632
13633               /* See if we are probably looking at a function
13634                  definition.  We are certainly not looking at a
13635                  member-declarator.  Calling `grokfield' has
13636                  side-effects, so we must not do it unless we are sure
13637                  that we are looking at a member-declarator.  */
13638               if (cp_parser_token_starts_function_definition_p
13639                   (cp_lexer_peek_token (parser->lexer)))
13640                 {
13641                   /* The grammar does not allow a pure-specifier to be
13642                      used when a member function is defined.  (It is
13643                      possible that this fact is an oversight in the
13644                      standard, since a pure function may be defined
13645                      outside of the class-specifier.  */
13646                   if (initializer)
13647                     error ("pure-specifier on function-definition");
13648                   decl = cp_parser_save_member_function_body (parser,
13649                                                               &decl_specifiers,
13650                                                               declarator,
13651                                                               attributes);
13652                   /* If the member was not a friend, declare it here.  */
13653                   if (!friend_p)
13654                     finish_member_declaration (decl);
13655                   /* Peek at the next token.  */
13656                   token = cp_lexer_peek_token (parser->lexer);
13657                   /* If the next token is a semicolon, consume it.  */
13658                   if (token->type == CPP_SEMICOLON)
13659                     cp_lexer_consume_token (parser->lexer);
13660                   return;
13661                 }
13662               else
13663                 {
13664                   /* Create the declaration.  */
13665                   decl = grokfield (declarator, &decl_specifiers,
13666                                     initializer, asm_specification,
13667                                     attributes);
13668                   /* Any initialization must have been from a
13669                      constant-expression.  */
13670                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13671                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13672                 }
13673             }
13674
13675           /* Reset PREFIX_ATTRIBUTES.  */
13676           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13677             attributes = TREE_CHAIN (attributes);
13678           if (attributes)
13679             TREE_CHAIN (attributes) = NULL_TREE;
13680
13681           /* If there is any qualification still in effect, clear it
13682              now; we will be starting fresh with the next declarator.  */
13683           parser->scope = NULL_TREE;
13684           parser->qualifying_scope = NULL_TREE;
13685           parser->object_scope = NULL_TREE;
13686           /* If it's a `,', then there are more declarators.  */
13687           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13688             cp_lexer_consume_token (parser->lexer);
13689           /* If the next token isn't a `;', then we have a parse error.  */
13690           else if (cp_lexer_next_token_is_not (parser->lexer,
13691                                                CPP_SEMICOLON))
13692             {
13693               cp_parser_error (parser, "expected %<;%>");
13694               /* Skip tokens until we find a `;'.  */
13695               cp_parser_skip_to_end_of_statement (parser);
13696
13697               break;
13698             }
13699
13700           if (decl)
13701             {
13702               /* Add DECL to the list of members.  */
13703               if (!friend_p)
13704                 finish_member_declaration (decl);
13705
13706               if (TREE_CODE (decl) == FUNCTION_DECL)
13707                 cp_parser_save_default_args (parser, decl);
13708             }
13709         }
13710     }
13711
13712   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13713 }
13714
13715 /* Parse a pure-specifier.
13716
13717    pure-specifier:
13718      = 0
13719
13720    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13721    Otherwise, ERROR_MARK_NODE is returned.  */
13722
13723 static tree
13724 cp_parser_pure_specifier (cp_parser* parser)
13725 {
13726   cp_token *token;
13727
13728   /* Look for the `=' token.  */
13729   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13730     return error_mark_node;
13731   /* Look for the `0' token.  */
13732   token = cp_lexer_consume_token (parser->lexer);
13733   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
13734   if (token->type == CPP_NUMBER && (token->flags & PURE_ZERO))
13735     return integer_zero_node;
13736
13737   cp_parser_error (parser, "invalid pure specifier (only `= 0' is allowed)");
13738   cp_parser_skip_to_end_of_statement (parser);
13739   return error_mark_node;
13740 }
13741
13742 /* Parse a constant-initializer.
13743
13744    constant-initializer:
13745      = constant-expression
13746
13747    Returns a representation of the constant-expression.  */
13748
13749 static tree
13750 cp_parser_constant_initializer (cp_parser* parser)
13751 {
13752   /* Look for the `=' token.  */
13753   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13754     return error_mark_node;
13755
13756   /* It is invalid to write:
13757
13758        struct S { static const int i = { 7 }; };
13759
13760      */
13761   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13762     {
13763       cp_parser_error (parser,
13764                        "a brace-enclosed initializer is not allowed here");
13765       /* Consume the opening brace.  */
13766       cp_lexer_consume_token (parser->lexer);
13767       /* Skip the initializer.  */
13768       cp_parser_skip_to_closing_brace (parser);
13769       /* Look for the trailing `}'.  */
13770       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13771
13772       return error_mark_node;
13773     }
13774
13775   return cp_parser_constant_expression (parser,
13776                                         /*allow_non_constant=*/false,
13777                                         NULL);
13778 }
13779
13780 /* Derived classes [gram.class.derived] */
13781
13782 /* Parse a base-clause.
13783
13784    base-clause:
13785      : base-specifier-list
13786
13787    base-specifier-list:
13788      base-specifier
13789      base-specifier-list , base-specifier
13790
13791    Returns a TREE_LIST representing the base-classes, in the order in
13792    which they were declared.  The representation of each node is as
13793    described by cp_parser_base_specifier.
13794
13795    In the case that no bases are specified, this function will return
13796    NULL_TREE, not ERROR_MARK_NODE.  */
13797
13798 static tree
13799 cp_parser_base_clause (cp_parser* parser)
13800 {
13801   tree bases = NULL_TREE;
13802
13803   /* Look for the `:' that begins the list.  */
13804   cp_parser_require (parser, CPP_COLON, "`:'");
13805
13806   /* Scan the base-specifier-list.  */
13807   while (true)
13808     {
13809       cp_token *token;
13810       tree base;
13811
13812       /* Look for the base-specifier.  */
13813       base = cp_parser_base_specifier (parser);
13814       /* Add BASE to the front of the list.  */
13815       if (base != error_mark_node)
13816         {
13817           TREE_CHAIN (base) = bases;
13818           bases = base;
13819         }
13820       /* Peek at the next token.  */
13821       token = cp_lexer_peek_token (parser->lexer);
13822       /* If it's not a comma, then the list is complete.  */
13823       if (token->type != CPP_COMMA)
13824         break;
13825       /* Consume the `,'.  */
13826       cp_lexer_consume_token (parser->lexer);
13827     }
13828
13829   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13830      base class had a qualified name.  However, the next name that
13831      appears is certainly not qualified.  */
13832   parser->scope = NULL_TREE;
13833   parser->qualifying_scope = NULL_TREE;
13834   parser->object_scope = NULL_TREE;
13835
13836   return nreverse (bases);
13837 }
13838
13839 /* Parse a base-specifier.
13840
13841    base-specifier:
13842      :: [opt] nested-name-specifier [opt] class-name
13843      virtual access-specifier [opt] :: [opt] nested-name-specifier
13844        [opt] class-name
13845      access-specifier virtual [opt] :: [opt] nested-name-specifier
13846        [opt] class-name
13847
13848    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13849    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13850    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13851    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13852
13853 static tree
13854 cp_parser_base_specifier (cp_parser* parser)
13855 {
13856   cp_token *token;
13857   bool done = false;
13858   bool virtual_p = false;
13859   bool duplicate_virtual_error_issued_p = false;
13860   bool duplicate_access_error_issued_p = false;
13861   bool class_scope_p, template_p;
13862   tree access = access_default_node;
13863   tree type;
13864
13865   /* Process the optional `virtual' and `access-specifier'.  */
13866   while (!done)
13867     {
13868       /* Peek at the next token.  */
13869       token = cp_lexer_peek_token (parser->lexer);
13870       /* Process `virtual'.  */
13871       switch (token->keyword)
13872         {
13873         case RID_VIRTUAL:
13874           /* If `virtual' appears more than once, issue an error.  */
13875           if (virtual_p && !duplicate_virtual_error_issued_p)
13876             {
13877               cp_parser_error (parser,
13878                                "%<virtual%> specified more than once in base-specified");
13879               duplicate_virtual_error_issued_p = true;
13880             }
13881
13882           virtual_p = true;
13883
13884           /* Consume the `virtual' token.  */
13885           cp_lexer_consume_token (parser->lexer);
13886
13887           break;
13888
13889         case RID_PUBLIC:
13890         case RID_PROTECTED:
13891         case RID_PRIVATE:
13892           /* If more than one access specifier appears, issue an
13893              error.  */
13894           if (access != access_default_node
13895               && !duplicate_access_error_issued_p)
13896             {
13897               cp_parser_error (parser,
13898                                "more than one access specifier in base-specified");
13899               duplicate_access_error_issued_p = true;
13900             }
13901
13902           access = ridpointers[(int) token->keyword];
13903
13904           /* Consume the access-specifier.  */
13905           cp_lexer_consume_token (parser->lexer);
13906
13907           break;
13908
13909         default:
13910           done = true;
13911           break;
13912         }
13913     }
13914   /* It is not uncommon to see programs mechanically, erroneously, use
13915      the 'typename' keyword to denote (dependent) qualified types
13916      as base classes.  */
13917   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13918     {
13919       if (!processing_template_decl)
13920         error ("keyword %<typename%> not allowed outside of templates");
13921       else
13922         error ("keyword %<typename%> not allowed in this context "
13923                "(the base class is implicitly a type)");
13924       cp_lexer_consume_token (parser->lexer);
13925     }
13926
13927   /* Look for the optional `::' operator.  */
13928   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13929   /* Look for the nested-name-specifier.  The simplest way to
13930      implement:
13931
13932        [temp.res]
13933
13934        The keyword `typename' is not permitted in a base-specifier or
13935        mem-initializer; in these contexts a qualified name that
13936        depends on a template-parameter is implicitly assumed to be a
13937        type name.
13938
13939      is to pretend that we have seen the `typename' keyword at this
13940      point.  */
13941   cp_parser_nested_name_specifier_opt (parser,
13942                                        /*typename_keyword_p=*/true,
13943                                        /*check_dependency_p=*/true,
13944                                        typename_type,
13945                                        /*is_declaration=*/true);
13946   /* If the base class is given by a qualified name, assume that names
13947      we see are type names or templates, as appropriate.  */
13948   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13949   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13950
13951   /* Finally, look for the class-name.  */
13952   type = cp_parser_class_name (parser,
13953                                class_scope_p,
13954                                template_p,
13955                                typename_type,
13956                                /*check_dependency_p=*/true,
13957                                /*class_head_p=*/false,
13958                                /*is_declaration=*/true);
13959
13960   if (type == error_mark_node)
13961     return error_mark_node;
13962
13963   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13964 }
13965
13966 /* Exception handling [gram.exception] */
13967
13968 /* Parse an (optional) exception-specification.
13969
13970    exception-specification:
13971      throw ( type-id-list [opt] )
13972
13973    Returns a TREE_LIST representing the exception-specification.  The
13974    TREE_VALUE of each node is a type.  */
13975
13976 static tree
13977 cp_parser_exception_specification_opt (cp_parser* parser)
13978 {
13979   cp_token *token;
13980   tree type_id_list;
13981
13982   /* Peek at the next token.  */
13983   token = cp_lexer_peek_token (parser->lexer);
13984   /* If it's not `throw', then there's no exception-specification.  */
13985   if (!cp_parser_is_keyword (token, RID_THROW))
13986     return NULL_TREE;
13987
13988   /* Consume the `throw'.  */
13989   cp_lexer_consume_token (parser->lexer);
13990
13991   /* Look for the `('.  */
13992   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13993
13994   /* Peek at the next token.  */
13995   token = cp_lexer_peek_token (parser->lexer);
13996   /* If it's not a `)', then there is a type-id-list.  */
13997   if (token->type != CPP_CLOSE_PAREN)
13998     {
13999       const char *saved_message;
14000
14001       /* Types may not be defined in an exception-specification.  */
14002       saved_message = parser->type_definition_forbidden_message;
14003       parser->type_definition_forbidden_message
14004         = "types may not be defined in an exception-specification";
14005       /* Parse the type-id-list.  */
14006       type_id_list = cp_parser_type_id_list (parser);
14007       /* Restore the saved message.  */
14008       parser->type_definition_forbidden_message = saved_message;
14009     }
14010   else
14011     type_id_list = empty_except_spec;
14012
14013   /* Look for the `)'.  */
14014   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14015
14016   return type_id_list;
14017 }
14018
14019 /* Parse an (optional) type-id-list.
14020
14021    type-id-list:
14022      type-id
14023      type-id-list , type-id
14024
14025    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14026    in the order that the types were presented.  */
14027
14028 static tree
14029 cp_parser_type_id_list (cp_parser* parser)
14030 {
14031   tree types = NULL_TREE;
14032
14033   while (true)
14034     {
14035       cp_token *token;
14036       tree type;
14037
14038       /* Get the next type-id.  */
14039       type = cp_parser_type_id (parser);
14040       /* Add it to the list.  */
14041       types = add_exception_specifier (types, type, /*complain=*/1);
14042       /* Peek at the next token.  */
14043       token = cp_lexer_peek_token (parser->lexer);
14044       /* If it is not a `,', we are done.  */
14045       if (token->type != CPP_COMMA)
14046         break;
14047       /* Consume the `,'.  */
14048       cp_lexer_consume_token (parser->lexer);
14049     }
14050
14051   return nreverse (types);
14052 }
14053
14054 /* Parse a try-block.
14055
14056    try-block:
14057      try compound-statement handler-seq  */
14058
14059 static tree
14060 cp_parser_try_block (cp_parser* parser)
14061 {
14062   tree try_block;
14063
14064   cp_parser_require_keyword (parser, RID_TRY, "`try'");
14065   try_block = begin_try_block ();
14066   cp_parser_compound_statement (parser, NULL, true);
14067   finish_try_block (try_block);
14068   cp_parser_handler_seq (parser);
14069   finish_handler_sequence (try_block);
14070
14071   return try_block;
14072 }
14073
14074 /* Parse a function-try-block.
14075
14076    function-try-block:
14077      try ctor-initializer [opt] function-body handler-seq  */
14078
14079 static bool
14080 cp_parser_function_try_block (cp_parser* parser)
14081 {
14082   tree try_block;
14083   bool ctor_initializer_p;
14084
14085   /* Look for the `try' keyword.  */
14086   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14087     return false;
14088   /* Let the rest of the front-end know where we are.  */
14089   try_block = begin_function_try_block ();
14090   /* Parse the function-body.  */
14091   ctor_initializer_p
14092     = cp_parser_ctor_initializer_opt_and_function_body (parser);
14093   /* We're done with the `try' part.  */
14094   finish_function_try_block (try_block);
14095   /* Parse the handlers.  */
14096   cp_parser_handler_seq (parser);
14097   /* We're done with the handlers.  */
14098   finish_function_handler_sequence (try_block);
14099
14100   return ctor_initializer_p;
14101 }
14102
14103 /* Parse a handler-seq.
14104
14105    handler-seq:
14106      handler handler-seq [opt]  */
14107
14108 static void
14109 cp_parser_handler_seq (cp_parser* parser)
14110 {
14111   while (true)
14112     {
14113       cp_token *token;
14114
14115       /* Parse the handler.  */
14116       cp_parser_handler (parser);
14117       /* Peek at the next token.  */
14118       token = cp_lexer_peek_token (parser->lexer);
14119       /* If it's not `catch' then there are no more handlers.  */
14120       if (!cp_parser_is_keyword (token, RID_CATCH))
14121         break;
14122     }
14123 }
14124
14125 /* Parse a handler.
14126
14127    handler:
14128      catch ( exception-declaration ) compound-statement  */
14129
14130 static void
14131 cp_parser_handler (cp_parser* parser)
14132 {
14133   tree handler;
14134   tree declaration;
14135
14136   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14137   handler = begin_handler ();
14138   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14139   declaration = cp_parser_exception_declaration (parser);
14140   finish_handler_parms (declaration, handler);
14141   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14142   cp_parser_compound_statement (parser, NULL, false);
14143   finish_handler (handler);
14144 }
14145
14146 /* Parse an exception-declaration.
14147
14148    exception-declaration:
14149      type-specifier-seq declarator
14150      type-specifier-seq abstract-declarator
14151      type-specifier-seq
14152      ...
14153
14154    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14155    ellipsis variant is used.  */
14156
14157 static tree
14158 cp_parser_exception_declaration (cp_parser* parser)
14159 {
14160   tree decl;
14161   cp_decl_specifier_seq type_specifiers;
14162   cp_declarator *declarator;
14163   const char *saved_message;
14164
14165   /* If it's an ellipsis, it's easy to handle.  */
14166   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14167     {
14168       /* Consume the `...' token.  */
14169       cp_lexer_consume_token (parser->lexer);
14170       return NULL_TREE;
14171     }
14172
14173   /* Types may not be defined in exception-declarations.  */
14174   saved_message = parser->type_definition_forbidden_message;
14175   parser->type_definition_forbidden_message
14176     = "types may not be defined in exception-declarations";
14177
14178   /* Parse the type-specifier-seq.  */
14179   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14180                                 &type_specifiers);
14181   /* If it's a `)', then there is no declarator.  */
14182   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14183     declarator = NULL;
14184   else
14185     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14186                                        /*ctor_dtor_or_conv_p=*/NULL,
14187                                        /*parenthesized_p=*/NULL,
14188                                        /*member_p=*/false);
14189
14190   /* Restore the saved message.  */
14191   parser->type_definition_forbidden_message = saved_message;
14192
14193   if (type_specifiers.any_specifiers_p)
14194     {
14195       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14196       if (decl == NULL_TREE)
14197         error ("invalid catch parameter");
14198     }
14199   else
14200     decl = NULL_TREE;
14201
14202   return decl;
14203 }
14204
14205 /* Parse a throw-expression.
14206
14207    throw-expression:
14208      throw assignment-expression [opt]
14209
14210    Returns a THROW_EXPR representing the throw-expression.  */
14211
14212 static tree
14213 cp_parser_throw_expression (cp_parser* parser)
14214 {
14215   tree expression;
14216   cp_token* token;
14217
14218   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14219   token = cp_lexer_peek_token (parser->lexer);
14220   /* Figure out whether or not there is an assignment-expression
14221      following the "throw" keyword.  */
14222   if (token->type == CPP_COMMA
14223       || token->type == CPP_SEMICOLON
14224       || token->type == CPP_CLOSE_PAREN
14225       || token->type == CPP_CLOSE_SQUARE
14226       || token->type == CPP_CLOSE_BRACE
14227       || token->type == CPP_COLON)
14228     expression = NULL_TREE;
14229   else
14230     expression = cp_parser_assignment_expression (parser,
14231                                                   /*cast_p=*/false);
14232
14233   return build_throw (expression);
14234 }
14235
14236 /* GNU Extensions */
14237
14238 /* Parse an (optional) asm-specification.
14239
14240    asm-specification:
14241      asm ( string-literal )
14242
14243    If the asm-specification is present, returns a STRING_CST
14244    corresponding to the string-literal.  Otherwise, returns
14245    NULL_TREE.  */
14246
14247 static tree
14248 cp_parser_asm_specification_opt (cp_parser* parser)
14249 {
14250   cp_token *token;
14251   tree asm_specification;
14252
14253   /* Peek at the next token.  */
14254   token = cp_lexer_peek_token (parser->lexer);
14255   /* If the next token isn't the `asm' keyword, then there's no
14256      asm-specification.  */
14257   if (!cp_parser_is_keyword (token, RID_ASM))
14258     return NULL_TREE;
14259
14260   /* Consume the `asm' token.  */
14261   cp_lexer_consume_token (parser->lexer);
14262   /* Look for the `('.  */
14263   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14264
14265   /* Look for the string-literal.  */
14266   asm_specification = cp_parser_string_literal (parser, false, false);
14267
14268   /* Look for the `)'.  */
14269   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14270
14271   return asm_specification;
14272 }
14273
14274 /* Parse an asm-operand-list.
14275
14276    asm-operand-list:
14277      asm-operand
14278      asm-operand-list , asm-operand
14279
14280    asm-operand:
14281      string-literal ( expression )
14282      [ string-literal ] string-literal ( expression )
14283
14284    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14285    each node is the expression.  The TREE_PURPOSE is itself a
14286    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14287    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14288    is a STRING_CST for the string literal before the parenthesis.  */
14289
14290 static tree
14291 cp_parser_asm_operand_list (cp_parser* parser)
14292 {
14293   tree asm_operands = NULL_TREE;
14294
14295   while (true)
14296     {
14297       tree string_literal;
14298       tree expression;
14299       tree name;
14300
14301       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14302         {
14303           /* Consume the `[' token.  */
14304           cp_lexer_consume_token (parser->lexer);
14305           /* Read the operand name.  */
14306           name = cp_parser_identifier (parser);
14307           if (name != error_mark_node)
14308             name = build_string (IDENTIFIER_LENGTH (name),
14309                                  IDENTIFIER_POINTER (name));
14310           /* Look for the closing `]'.  */
14311           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14312         }
14313       else
14314         name = NULL_TREE;
14315       /* Look for the string-literal.  */
14316       string_literal = cp_parser_string_literal (parser, false, false);
14317
14318       /* Look for the `('.  */
14319       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14320       /* Parse the expression.  */
14321       expression = cp_parser_expression (parser, /*cast_p=*/false);
14322       /* Look for the `)'.  */
14323       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14324
14325       /* Add this operand to the list.  */
14326       asm_operands = tree_cons (build_tree_list (name, string_literal),
14327                                 expression,
14328                                 asm_operands);
14329       /* If the next token is not a `,', there are no more
14330          operands.  */
14331       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14332         break;
14333       /* Consume the `,'.  */
14334       cp_lexer_consume_token (parser->lexer);
14335     }
14336
14337   return nreverse (asm_operands);
14338 }
14339
14340 /* Parse an asm-clobber-list.
14341
14342    asm-clobber-list:
14343      string-literal
14344      asm-clobber-list , string-literal
14345
14346    Returns a TREE_LIST, indicating the clobbers in the order that they
14347    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14348
14349 static tree
14350 cp_parser_asm_clobber_list (cp_parser* parser)
14351 {
14352   tree clobbers = NULL_TREE;
14353
14354   while (true)
14355     {
14356       tree string_literal;
14357
14358       /* Look for the string literal.  */
14359       string_literal = cp_parser_string_literal (parser, false, false);
14360       /* Add it to the list.  */
14361       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14362       /* If the next token is not a `,', then the list is
14363          complete.  */
14364       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14365         break;
14366       /* Consume the `,' token.  */
14367       cp_lexer_consume_token (parser->lexer);
14368     }
14369
14370   return clobbers;
14371 }
14372
14373 /* Parse an (optional) series of attributes.
14374
14375    attributes:
14376      attributes attribute
14377
14378    attribute:
14379      __attribute__ (( attribute-list [opt] ))
14380
14381    The return value is as for cp_parser_attribute_list.  */
14382
14383 static tree
14384 cp_parser_attributes_opt (cp_parser* parser)
14385 {
14386   tree attributes = NULL_TREE;
14387
14388   while (true)
14389     {
14390       cp_token *token;
14391       tree attribute_list;
14392
14393       /* Peek at the next token.  */
14394       token = cp_lexer_peek_token (parser->lexer);
14395       /* If it's not `__attribute__', then we're done.  */
14396       if (token->keyword != RID_ATTRIBUTE)
14397         break;
14398
14399       /* Consume the `__attribute__' keyword.  */
14400       cp_lexer_consume_token (parser->lexer);
14401       /* Look for the two `(' tokens.  */
14402       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14403       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14404
14405       /* Peek at the next token.  */
14406       token = cp_lexer_peek_token (parser->lexer);
14407       if (token->type != CPP_CLOSE_PAREN)
14408         /* Parse the attribute-list.  */
14409         attribute_list = cp_parser_attribute_list (parser);
14410       else
14411         /* If the next token is a `)', then there is no attribute
14412            list.  */
14413         attribute_list = NULL;
14414
14415       /* Look for the two `)' tokens.  */
14416       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14417       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14418
14419       /* Add these new attributes to the list.  */
14420       attributes = chainon (attributes, attribute_list);
14421     }
14422
14423   return attributes;
14424 }
14425
14426 /* Parse an attribute-list.
14427
14428    attribute-list:
14429      attribute
14430      attribute-list , attribute
14431
14432    attribute:
14433      identifier
14434      identifier ( identifier )
14435      identifier ( identifier , expression-list )
14436      identifier ( expression-list )
14437
14438    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14439    to an attribute.  The TREE_PURPOSE of each node is the identifier
14440    indicating which attribute is in use.  The TREE_VALUE represents
14441    the arguments, if any.  */
14442
14443 static tree
14444 cp_parser_attribute_list (cp_parser* parser)
14445 {
14446   tree attribute_list = NULL_TREE;
14447   bool save_translate_strings_p = parser->translate_strings_p;
14448
14449   parser->translate_strings_p = false;
14450   while (true)
14451     {
14452       cp_token *token;
14453       tree identifier;
14454       tree attribute;
14455
14456       /* Look for the identifier.  We also allow keywords here; for
14457          example `__attribute__ ((const))' is legal.  */
14458       token = cp_lexer_peek_token (parser->lexer);
14459       if (token->type == CPP_NAME
14460           || token->type == CPP_KEYWORD)
14461         {
14462           /* Consume the token.  */
14463           token = cp_lexer_consume_token (parser->lexer);
14464
14465           /* Save away the identifier that indicates which attribute
14466              this is.  */
14467           identifier = token->value;
14468           attribute = build_tree_list (identifier, NULL_TREE);
14469
14470           /* Peek at the next token.  */
14471           token = cp_lexer_peek_token (parser->lexer);
14472           /* If it's an `(', then parse the attribute arguments.  */
14473           if (token->type == CPP_OPEN_PAREN)
14474             {
14475               tree arguments;
14476
14477               arguments = (cp_parser_parenthesized_expression_list
14478                            (parser, true, /*cast_p=*/false,
14479                             /*non_constant_p=*/NULL));
14480               /* Save the identifier and arguments away.  */
14481               TREE_VALUE (attribute) = arguments;
14482             }
14483
14484           /* Add this attribute to the list.  */
14485           TREE_CHAIN (attribute) = attribute_list;
14486           attribute_list = attribute;
14487
14488           token = cp_lexer_peek_token (parser->lexer);
14489         }
14490       /* Now, look for more attributes.  If the next token isn't a
14491          `,', we're done.  */
14492       if (token->type != CPP_COMMA)
14493         break;
14494
14495       /* Consume the comma and keep going.  */
14496       cp_lexer_consume_token (parser->lexer);
14497     }
14498   parser->translate_strings_p = save_translate_strings_p;
14499
14500   /* We built up the list in reverse order.  */
14501   return nreverse (attribute_list);
14502 }
14503
14504 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14505    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14506    current value of the PEDANTIC flag, regardless of whether or not
14507    the `__extension__' keyword is present.  The caller is responsible
14508    for restoring the value of the PEDANTIC flag.  */
14509
14510 static bool
14511 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14512 {
14513   /* Save the old value of the PEDANTIC flag.  */
14514   *saved_pedantic = pedantic;
14515
14516   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14517     {
14518       /* Consume the `__extension__' token.  */
14519       cp_lexer_consume_token (parser->lexer);
14520       /* We're not being pedantic while the `__extension__' keyword is
14521          in effect.  */
14522       pedantic = 0;
14523
14524       return true;
14525     }
14526
14527   return false;
14528 }
14529
14530 /* Parse a label declaration.
14531
14532    label-declaration:
14533      __label__ label-declarator-seq ;
14534
14535    label-declarator-seq:
14536      identifier , label-declarator-seq
14537      identifier  */
14538
14539 static void
14540 cp_parser_label_declaration (cp_parser* parser)
14541 {
14542   /* Look for the `__label__' keyword.  */
14543   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14544
14545   while (true)
14546     {
14547       tree identifier;
14548
14549       /* Look for an identifier.  */
14550       identifier = cp_parser_identifier (parser);
14551       /* If we failed, stop.  */
14552       if (identifier == error_mark_node)
14553         break;
14554       /* Declare it as a label.  */
14555       finish_label_decl (identifier);
14556       /* If the next token is a `;', stop.  */
14557       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14558         break;
14559       /* Look for the `,' separating the label declarations.  */
14560       cp_parser_require (parser, CPP_COMMA, "`,'");
14561     }
14562
14563   /* Look for the final `;'.  */
14564   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14565 }
14566
14567 /* Support Functions */
14568
14569 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14570    NAME should have one of the representations used for an
14571    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14572    is returned.  If PARSER->SCOPE is a dependent type, then a
14573    SCOPE_REF is returned.
14574
14575    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14576    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14577    was formed.  Abstractly, such entities should not be passed to this
14578    function, because they do not need to be looked up, but it is
14579    simpler to check for this special case here, rather than at the
14580    call-sites.
14581
14582    In cases not explicitly covered above, this function returns a
14583    DECL, OVERLOAD, or baselink representing the result of the lookup.
14584    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14585    is returned.
14586
14587    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14588    (e.g., "struct") that was used.  In that case bindings that do not
14589    refer to types are ignored.
14590
14591    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14592    ignored.
14593
14594    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14595    are ignored.
14596
14597    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14598    types.
14599
14600    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14601    TREE_LIST of candidates if name-lookup results in an ambiguity, and
14602    NULL_TREE otherwise.  */ 
14603
14604 static tree
14605 cp_parser_lookup_name (cp_parser *parser, tree name,
14606                        enum tag_types tag_type,
14607                        bool is_template, 
14608                        bool is_namespace,
14609                        bool check_dependency,
14610                        tree *ambiguous_decls)
14611 {
14612   int flags = 0;
14613   tree decl;
14614   tree object_type = parser->context->object_type;
14615
14616   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14617     flags |= LOOKUP_COMPLAIN;
14618
14619   /* Assume that the lookup will be unambiguous.  */
14620   if (ambiguous_decls)
14621     *ambiguous_decls = NULL_TREE;
14622
14623   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14624      no longer valid.  Note that if we are parsing tentatively, and
14625      the parse fails, OBJECT_TYPE will be automatically restored.  */
14626   parser->context->object_type = NULL_TREE;
14627
14628   if (name == error_mark_node)
14629     return error_mark_node;
14630
14631   /* A template-id has already been resolved; there is no lookup to
14632      do.  */
14633   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14634     return name;
14635   if (BASELINK_P (name))
14636     {
14637       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14638                   == TEMPLATE_ID_EXPR);
14639       return name;
14640     }
14641
14642   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14643      it should already have been checked to make sure that the name
14644      used matches the type being destroyed.  */
14645   if (TREE_CODE (name) == BIT_NOT_EXPR)
14646     {
14647       tree type;
14648
14649       /* Figure out to which type this destructor applies.  */
14650       if (parser->scope)
14651         type = parser->scope;
14652       else if (object_type)
14653         type = object_type;
14654       else
14655         type = current_class_type;
14656       /* If that's not a class type, there is no destructor.  */
14657       if (!type || !CLASS_TYPE_P (type))
14658         return error_mark_node;
14659       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14660         lazily_declare_fn (sfk_destructor, type);
14661       if (!CLASSTYPE_DESTRUCTORS (type))
14662           return error_mark_node;
14663       /* If it was a class type, return the destructor.  */
14664       return CLASSTYPE_DESTRUCTORS (type);
14665     }
14666
14667   /* By this point, the NAME should be an ordinary identifier.  If
14668      the id-expression was a qualified name, the qualifying scope is
14669      stored in PARSER->SCOPE at this point.  */
14670   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14671
14672   /* Perform the lookup.  */
14673   if (parser->scope)
14674     {
14675       bool dependent_p;
14676
14677       if (parser->scope == error_mark_node)
14678         return error_mark_node;
14679
14680       /* If the SCOPE is dependent, the lookup must be deferred until
14681          the template is instantiated -- unless we are explicitly
14682          looking up names in uninstantiated templates.  Even then, we
14683          cannot look up the name if the scope is not a class type; it
14684          might, for example, be a template type parameter.  */
14685       dependent_p = (TYPE_P (parser->scope)
14686                      && !(parser->in_declarator_p
14687                           && currently_open_class (parser->scope))
14688                      && dependent_type_p (parser->scope));
14689       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14690            && dependent_p)
14691         {
14692           if (tag_type)
14693             {
14694               tree type;
14695
14696               /* The resolution to Core Issue 180 says that `struct
14697                  A::B' should be considered a type-name, even if `A'
14698                  is dependent.  */
14699               type = make_typename_type (parser->scope, name, tag_type,
14700                                          /*complain=*/tf_error);
14701               decl = TYPE_NAME (type);
14702             }
14703           else if (is_template
14704                    && (cp_parser_next_token_ends_template_argument_p (parser)
14705                        || cp_lexer_next_token_is (parser->lexer,
14706                                                   CPP_CLOSE_PAREN)))
14707             decl = make_unbound_class_template (parser->scope,
14708                                                 name, NULL_TREE,
14709                                                 /*complain=*/tf_error);
14710           else
14711             decl = build_qualified_name (/*type=*/NULL_TREE,
14712                                          parser->scope, name,
14713                                          is_template);
14714         }
14715       else
14716         {
14717           tree pushed_scope = NULL_TREE;
14718
14719           /* If PARSER->SCOPE is a dependent type, then it must be a
14720              class type, and we must not be checking dependencies;
14721              otherwise, we would have processed this lookup above.  So
14722              that PARSER->SCOPE is not considered a dependent base by
14723              lookup_member, we must enter the scope here.  */
14724           if (dependent_p)
14725             pushed_scope = push_scope (parser->scope);
14726           /* If the PARSER->SCOPE is a template specialization, it
14727              may be instantiated during name lookup.  In that case,
14728              errors may be issued.  Even if we rollback the current
14729              tentative parse, those errors are valid.  */
14730           decl = lookup_qualified_name (parser->scope, name,
14731                                         tag_type != none_type,
14732                                         /*complain=*/true);
14733           if (pushed_scope)
14734             pop_scope (pushed_scope);
14735         }
14736       parser->qualifying_scope = parser->scope;
14737       parser->object_scope = NULL_TREE;
14738     }
14739   else if (object_type)
14740     {
14741       tree object_decl = NULL_TREE;
14742       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14743          OBJECT_TYPE is not a class.  */
14744       if (CLASS_TYPE_P (object_type))
14745         /* If the OBJECT_TYPE is a template specialization, it may
14746            be instantiated during name lookup.  In that case, errors
14747            may be issued.  Even if we rollback the current tentative
14748            parse, those errors are valid.  */
14749         object_decl = lookup_member (object_type,
14750                                      name,
14751                                      /*protect=*/0,
14752                                      tag_type != none_type);
14753       /* Look it up in the enclosing context, too.  */
14754       decl = lookup_name_real (name, tag_type != none_type,
14755                                /*nonclass=*/0,
14756                                /*block_p=*/true, is_namespace, flags);
14757       parser->object_scope = object_type;
14758       parser->qualifying_scope = NULL_TREE;
14759       if (object_decl)
14760         decl = object_decl;
14761     }
14762   else
14763     {
14764       decl = lookup_name_real (name, tag_type != none_type,
14765                                /*nonclass=*/0,
14766                                /*block_p=*/true, is_namespace, flags);
14767       parser->qualifying_scope = NULL_TREE;
14768       parser->object_scope = NULL_TREE;
14769     }
14770
14771   /* If the lookup failed, let our caller know.  */
14772   if (!decl || decl == error_mark_node)
14773     return error_mark_node;
14774
14775   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14776   if (TREE_CODE (decl) == TREE_LIST)
14777     {
14778       if (ambiguous_decls)
14779         *ambiguous_decls = decl;
14780       /* The error message we have to print is too complicated for
14781          cp_parser_error, so we incorporate its actions directly.  */
14782       if (!cp_parser_simulate_error (parser))
14783         {
14784           error ("reference to %qD is ambiguous", name);
14785           print_candidates (decl);
14786         }
14787       return error_mark_node;
14788     }
14789
14790   gcc_assert (DECL_P (decl)
14791               || TREE_CODE (decl) == OVERLOAD
14792               || TREE_CODE (decl) == SCOPE_REF
14793               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14794               || BASELINK_P (decl));
14795
14796   /* If we have resolved the name of a member declaration, check to
14797      see if the declaration is accessible.  When the name resolves to
14798      set of overloaded functions, accessibility is checked when
14799      overload resolution is done.
14800
14801      During an explicit instantiation, access is not checked at all,
14802      as per [temp.explicit].  */
14803   if (DECL_P (decl))
14804     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14805
14806   return decl;
14807 }
14808
14809 /* Like cp_parser_lookup_name, but for use in the typical case where
14810    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14811    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14812
14813 static tree
14814 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14815 {
14816   return cp_parser_lookup_name (parser, name,
14817                                 none_type,
14818                                 /*is_template=*/false,
14819                                 /*is_namespace=*/false,
14820                                 /*check_dependency=*/true,
14821                                 /*ambiguous_decls=*/NULL);
14822 }
14823
14824 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14825    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14826    true, the DECL indicates the class being defined in a class-head,
14827    or declared in an elaborated-type-specifier.
14828
14829    Otherwise, return DECL.  */
14830
14831 static tree
14832 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14833 {
14834   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14835      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14836
14837        struct A {
14838          template <typename T> struct B;
14839        };
14840
14841        template <typename T> struct A::B {};
14842
14843      Similarly, in an elaborated-type-specifier:
14844
14845        namespace N { struct X{}; }
14846
14847        struct A {
14848          template <typename T> friend struct N::X;
14849        };
14850
14851      However, if the DECL refers to a class type, and we are in
14852      the scope of the class, then the name lookup automatically
14853      finds the TYPE_DECL created by build_self_reference rather
14854      than a TEMPLATE_DECL.  For example, in:
14855
14856        template <class T> struct S {
14857          S s;
14858        };
14859
14860      there is no need to handle such case.  */
14861
14862   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14863     return DECL_TEMPLATE_RESULT (decl);
14864
14865   return decl;
14866 }
14867
14868 /* If too many, or too few, template-parameter lists apply to the
14869    declarator, issue an error message.  Returns TRUE if all went well,
14870    and FALSE otherwise.  */
14871
14872 static bool
14873 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14874                                                 cp_declarator *declarator)
14875 {
14876   unsigned num_templates;
14877
14878   /* We haven't seen any classes that involve template parameters yet.  */
14879   num_templates = 0;
14880
14881   switch (declarator->kind)
14882     {
14883     case cdk_id:
14884       if (declarator->u.id.qualifying_scope)
14885         {
14886           tree scope;
14887           tree member;
14888
14889           scope = declarator->u.id.qualifying_scope;
14890           member = declarator->u.id.unqualified_name;
14891
14892           while (scope && CLASS_TYPE_P (scope))
14893             {
14894               /* You're supposed to have one `template <...>'
14895                  for every template class, but you don't need one
14896                  for a full specialization.  For example:
14897
14898                  template <class T> struct S{};
14899                  template <> struct S<int> { void f(); };
14900                  void S<int>::f () {}
14901
14902                  is correct; there shouldn't be a `template <>' for
14903                  the definition of `S<int>::f'.  */
14904               if (CLASSTYPE_TEMPLATE_INFO (scope)
14905                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14906                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14907                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14908                 ++num_templates;
14909
14910               scope = TYPE_CONTEXT (scope);
14911             }
14912         }
14913       else if (TREE_CODE (declarator->u.id.unqualified_name)
14914                == TEMPLATE_ID_EXPR)
14915         /* If the DECLARATOR has the form `X<y>' then it uses one
14916            additional level of template parameters.  */
14917         ++num_templates;
14918
14919       return cp_parser_check_template_parameters (parser,
14920                                                   num_templates);
14921
14922     case cdk_function:
14923     case cdk_array:
14924     case cdk_pointer:
14925     case cdk_reference:
14926     case cdk_ptrmem:
14927       return (cp_parser_check_declarator_template_parameters
14928               (parser, declarator->declarator));
14929
14930     case cdk_error:
14931       return true;
14932
14933     default:
14934       gcc_unreachable ();
14935     }
14936   return false;
14937 }
14938
14939 /* NUM_TEMPLATES were used in the current declaration.  If that is
14940    invalid, return FALSE and issue an error messages.  Otherwise,
14941    return TRUE.  */
14942
14943 static bool
14944 cp_parser_check_template_parameters (cp_parser* parser,
14945                                      unsigned num_templates)
14946 {
14947   /* If there are more template classes than parameter lists, we have
14948      something like:
14949
14950        template <class T> void S<T>::R<T>::f ();  */
14951   if (parser->num_template_parameter_lists < num_templates)
14952     {
14953       error ("too few template-parameter-lists");
14954       return false;
14955     }
14956   /* If there are the same number of template classes and parameter
14957      lists, that's OK.  */
14958   if (parser->num_template_parameter_lists == num_templates)
14959     return true;
14960   /* If there are more, but only one more, then we are referring to a
14961      member template.  That's OK too.  */
14962   if (parser->num_template_parameter_lists == num_templates + 1)
14963       return true;
14964   /* Otherwise, there are too many template parameter lists.  We have
14965      something like:
14966
14967      template <class T> template <class U> void S::f();  */
14968   error ("too many template-parameter-lists");
14969   return false;
14970 }
14971
14972 /* Parse an optional `::' token indicating that the following name is
14973    from the global namespace.  If so, PARSER->SCOPE is set to the
14974    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14975    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14976    Returns the new value of PARSER->SCOPE, if the `::' token is
14977    present, and NULL_TREE otherwise.  */
14978
14979 static tree
14980 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14981 {
14982   cp_token *token;
14983
14984   /* Peek at the next token.  */
14985   token = cp_lexer_peek_token (parser->lexer);
14986   /* If we're looking at a `::' token then we're starting from the
14987      global namespace, not our current location.  */
14988   if (token->type == CPP_SCOPE)
14989     {
14990       /* Consume the `::' token.  */
14991       cp_lexer_consume_token (parser->lexer);
14992       /* Set the SCOPE so that we know where to start the lookup.  */
14993       parser->scope = global_namespace;
14994       parser->qualifying_scope = global_namespace;
14995       parser->object_scope = NULL_TREE;
14996
14997       return parser->scope;
14998     }
14999   else if (!current_scope_valid_p)
15000     {
15001       parser->scope = NULL_TREE;
15002       parser->qualifying_scope = NULL_TREE;
15003       parser->object_scope = NULL_TREE;
15004     }
15005
15006   return NULL_TREE;
15007 }
15008
15009 /* Returns TRUE if the upcoming token sequence is the start of a
15010    constructor declarator.  If FRIEND_P is true, the declarator is
15011    preceded by the `friend' specifier.  */
15012
15013 static bool
15014 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15015 {
15016   bool constructor_p;
15017   tree type_decl = NULL_TREE;
15018   bool nested_name_p;
15019   cp_token *next_token;
15020
15021   /* The common case is that this is not a constructor declarator, so
15022      try to avoid doing lots of work if at all possible.  It's not
15023      valid declare a constructor at function scope.  */
15024   if (at_function_scope_p ())
15025     return false;
15026   /* And only certain tokens can begin a constructor declarator.  */
15027   next_token = cp_lexer_peek_token (parser->lexer);
15028   if (next_token->type != CPP_NAME
15029       && next_token->type != CPP_SCOPE
15030       && next_token->type != CPP_NESTED_NAME_SPECIFIER
15031       && next_token->type != CPP_TEMPLATE_ID)
15032     return false;
15033
15034   /* Parse tentatively; we are going to roll back all of the tokens
15035      consumed here.  */
15036   cp_parser_parse_tentatively (parser);
15037   /* Assume that we are looking at a constructor declarator.  */
15038   constructor_p = true;
15039
15040   /* Look for the optional `::' operator.  */
15041   cp_parser_global_scope_opt (parser,
15042                               /*current_scope_valid_p=*/false);
15043   /* Look for the nested-name-specifier.  */
15044   nested_name_p
15045     = (cp_parser_nested_name_specifier_opt (parser,
15046                                             /*typename_keyword_p=*/false,
15047                                             /*check_dependency_p=*/false,
15048                                             /*type_p=*/false,
15049                                             /*is_declaration=*/false)
15050        != NULL_TREE);
15051   /* Outside of a class-specifier, there must be a
15052      nested-name-specifier.  */
15053   if (!nested_name_p &&
15054       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15055        || friend_p))
15056     constructor_p = false;
15057   /* If we still think that this might be a constructor-declarator,
15058      look for a class-name.  */
15059   if (constructor_p)
15060     {
15061       /* If we have:
15062
15063            template <typename T> struct S { S(); };
15064            template <typename T> S<T>::S ();
15065
15066          we must recognize that the nested `S' names a class.
15067          Similarly, for:
15068
15069            template <typename T> S<T>::S<T> ();
15070
15071          we must recognize that the nested `S' names a template.  */
15072       type_decl = cp_parser_class_name (parser,
15073                                         /*typename_keyword_p=*/false,
15074                                         /*template_keyword_p=*/false,
15075                                         none_type,
15076                                         /*check_dependency_p=*/false,
15077                                         /*class_head_p=*/false,
15078                                         /*is_declaration=*/false);
15079       /* If there was no class-name, then this is not a constructor.  */
15080       constructor_p = !cp_parser_error_occurred (parser);
15081     }
15082
15083   /* If we're still considering a constructor, we have to see a `(',
15084      to begin the parameter-declaration-clause, followed by either a
15085      `)', an `...', or a decl-specifier.  We need to check for a
15086      type-specifier to avoid being fooled into thinking that:
15087
15088        S::S (f) (int);
15089
15090      is a constructor.  (It is actually a function named `f' that
15091      takes one parameter (of type `int') and returns a value of type
15092      `S::S'.  */
15093   if (constructor_p
15094       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15095     {
15096       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15097           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15098           /* A parameter declaration begins with a decl-specifier,
15099              which is either the "attribute" keyword, a storage class
15100              specifier, or (usually) a type-specifier.  */
15101           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15102           && !cp_parser_storage_class_specifier_opt (parser))
15103         {
15104           tree type;
15105           tree pushed_scope = NULL_TREE;
15106           unsigned saved_num_template_parameter_lists;
15107
15108           /* Names appearing in the type-specifier should be looked up
15109              in the scope of the class.  */
15110           if (current_class_type)
15111             type = NULL_TREE;
15112           else
15113             {
15114               type = TREE_TYPE (type_decl);
15115               if (TREE_CODE (type) == TYPENAME_TYPE)
15116                 {
15117                   type = resolve_typename_type (type,
15118                                                 /*only_current_p=*/false);
15119                   if (type == error_mark_node)
15120                     {
15121                       cp_parser_abort_tentative_parse (parser);
15122                       return false;
15123                     }
15124                 }
15125               pushed_scope = push_scope (type);
15126             }
15127
15128           /* Inside the constructor parameter list, surrounding
15129              template-parameter-lists do not apply.  */
15130           saved_num_template_parameter_lists
15131             = parser->num_template_parameter_lists;
15132           parser->num_template_parameter_lists = 0;
15133
15134           /* Look for the type-specifier.  */
15135           cp_parser_type_specifier (parser,
15136                                     CP_PARSER_FLAGS_NONE,
15137                                     /*decl_specs=*/NULL,
15138                                     /*is_declarator=*/true,
15139                                     /*declares_class_or_enum=*/NULL,
15140                                     /*is_cv_qualifier=*/NULL);
15141
15142           parser->num_template_parameter_lists
15143             = saved_num_template_parameter_lists;
15144
15145           /* Leave the scope of the class.  */
15146           if (pushed_scope)
15147             pop_scope (pushed_scope);
15148
15149           constructor_p = !cp_parser_error_occurred (parser);
15150         }
15151     }
15152   else
15153     constructor_p = false;
15154   /* We did not really want to consume any tokens.  */
15155   cp_parser_abort_tentative_parse (parser);
15156
15157   return constructor_p;
15158 }
15159
15160 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15161    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15162    they must be performed once we are in the scope of the function.
15163
15164    Returns the function defined.  */
15165
15166 static tree
15167 cp_parser_function_definition_from_specifiers_and_declarator
15168   (cp_parser* parser,
15169    cp_decl_specifier_seq *decl_specifiers,
15170    tree attributes,
15171    const cp_declarator *declarator)
15172 {
15173   tree fn;
15174   bool success_p;
15175
15176   /* Begin the function-definition.  */
15177   success_p = start_function (decl_specifiers, declarator, attributes);
15178
15179   /* The things we're about to see are not directly qualified by any
15180      template headers we've seen thus far.  */
15181   reset_specialization ();
15182
15183   /* If there were names looked up in the decl-specifier-seq that we
15184      did not check, check them now.  We must wait until we are in the
15185      scope of the function to perform the checks, since the function
15186      might be a friend.  */
15187   perform_deferred_access_checks ();
15188
15189   if (!success_p)
15190     {
15191       /* Skip the entire function.  */
15192       error ("invalid function declaration");
15193       cp_parser_skip_to_end_of_block_or_statement (parser);
15194       fn = error_mark_node;
15195     }
15196   else
15197     fn = cp_parser_function_definition_after_declarator (parser,
15198                                                          /*inline_p=*/false);
15199
15200   return fn;
15201 }
15202
15203 /* Parse the part of a function-definition that follows the
15204    declarator.  INLINE_P is TRUE iff this function is an inline
15205    function defined with a class-specifier.
15206
15207    Returns the function defined.  */
15208
15209 static tree
15210 cp_parser_function_definition_after_declarator (cp_parser* parser,
15211                                                 bool inline_p)
15212 {
15213   tree fn;
15214   bool ctor_initializer_p = false;
15215   bool saved_in_unbraced_linkage_specification_p;
15216   unsigned saved_num_template_parameter_lists;
15217
15218   /* If the next token is `return', then the code may be trying to
15219      make use of the "named return value" extension that G++ used to
15220      support.  */
15221   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15222     {
15223       /* Consume the `return' keyword.  */
15224       cp_lexer_consume_token (parser->lexer);
15225       /* Look for the identifier that indicates what value is to be
15226          returned.  */
15227       cp_parser_identifier (parser);
15228       /* Issue an error message.  */
15229       error ("named return values are no longer supported");
15230       /* Skip tokens until we reach the start of the function body.  */
15231       while (true)
15232         {
15233           cp_token *token = cp_lexer_peek_token (parser->lexer);
15234           if (token->type == CPP_OPEN_BRACE
15235               || token->type == CPP_EOF
15236               || token->type == CPP_PRAGMA_EOL)
15237             break;
15238           cp_lexer_consume_token (parser->lexer);
15239         }
15240     }
15241   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15242      anything declared inside `f'.  */
15243   saved_in_unbraced_linkage_specification_p
15244     = parser->in_unbraced_linkage_specification_p;
15245   parser->in_unbraced_linkage_specification_p = false;
15246   /* Inside the function, surrounding template-parameter-lists do not
15247      apply.  */
15248   saved_num_template_parameter_lists
15249     = parser->num_template_parameter_lists;
15250   parser->num_template_parameter_lists = 0;
15251   /* If the next token is `try', then we are looking at a
15252      function-try-block.  */
15253   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15254     ctor_initializer_p = cp_parser_function_try_block (parser);
15255   /* A function-try-block includes the function-body, so we only do
15256      this next part if we're not processing a function-try-block.  */
15257   else
15258     ctor_initializer_p
15259       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15260
15261   /* Finish the function.  */
15262   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15263                         (inline_p ? 2 : 0));
15264   /* Generate code for it, if necessary.  */
15265   expand_or_defer_fn (fn);
15266   /* Restore the saved values.  */
15267   parser->in_unbraced_linkage_specification_p
15268     = saved_in_unbraced_linkage_specification_p;
15269   parser->num_template_parameter_lists
15270     = saved_num_template_parameter_lists;
15271
15272   return fn;
15273 }
15274
15275 /* Parse a template-declaration, assuming that the `export' (and
15276    `extern') keywords, if present, has already been scanned.  MEMBER_P
15277    is as for cp_parser_template_declaration.  */
15278
15279 static void
15280 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15281 {
15282   tree decl = NULL_TREE;
15283   tree parameter_list;
15284   bool friend_p = false;
15285   bool need_lang_pop;
15286
15287   /* Look for the `template' keyword.  */
15288   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15289     return;
15290
15291   /* And the `<'.  */
15292   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15293     return;
15294   /* [temp]
15295    
15296      A template ... shall not have C linkage.  */
15297   if (current_lang_name == lang_name_c)
15298     {
15299       error ("template with C linkage");
15300       /* Give it C++ linkage to avoid confusing other parts of the
15301          front end.  */
15302       push_lang_context (lang_name_cplusplus);
15303       need_lang_pop = true;
15304     }
15305   else
15306     need_lang_pop = false;
15307   /* If the next token is `>', then we have an invalid
15308      specialization.  Rather than complain about an invalid template
15309      parameter, issue an error message here.  */
15310   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15311     {
15312       cp_parser_error (parser, "invalid explicit specialization");
15313       begin_specialization ();
15314       parameter_list = NULL_TREE;
15315     }
15316   else
15317     /* Parse the template parameters.  */
15318     parameter_list = cp_parser_template_parameter_list (parser);
15319
15320   /* Look for the `>'.  */
15321   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15322   /* We just processed one more parameter list.  */
15323   ++parser->num_template_parameter_lists;
15324   /* If the next token is `template', there are more template
15325      parameters.  */
15326   if (cp_lexer_next_token_is_keyword (parser->lexer,
15327                                       RID_TEMPLATE))
15328     cp_parser_template_declaration_after_export (parser, member_p);
15329   else
15330     {
15331       /* There are no access checks when parsing a template, as we do not
15332          know if a specialization will be a friend.  */
15333       push_deferring_access_checks (dk_no_check);
15334
15335       decl = cp_parser_single_declaration (parser,
15336                                            member_p,
15337                                            &friend_p);
15338
15339       pop_deferring_access_checks ();
15340
15341       /* If this is a member template declaration, let the front
15342          end know.  */
15343       if (member_p && !friend_p && decl)
15344         {
15345           if (TREE_CODE (decl) == TYPE_DECL)
15346             cp_parser_check_access_in_redeclaration (decl);
15347
15348           decl = finish_member_template_decl (decl);
15349         }
15350       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15351         make_friend_class (current_class_type, TREE_TYPE (decl),
15352                            /*complain=*/true);
15353     }
15354   /* We are done with the current parameter list.  */
15355   --parser->num_template_parameter_lists;
15356
15357   /* Finish up.  */
15358   finish_template_decl (parameter_list);
15359
15360   /* Register member declarations.  */
15361   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15362     finish_member_declaration (decl);
15363   /* For the erroneous case of a template with C linkage, we pushed an
15364      implicit C++ linkage scope; exit that scope now.  */
15365   if (need_lang_pop)
15366     pop_lang_context ();
15367   /* If DECL is a function template, we must return to parse it later.
15368      (Even though there is no definition, there might be default
15369      arguments that need handling.)  */
15370   if (member_p && decl
15371       && (TREE_CODE (decl) == FUNCTION_DECL
15372           || DECL_FUNCTION_TEMPLATE_P (decl)))
15373     TREE_VALUE (parser->unparsed_functions_queues)
15374       = tree_cons (NULL_TREE, decl,
15375                    TREE_VALUE (parser->unparsed_functions_queues));
15376 }
15377
15378 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15379    `function-definition' sequence.  MEMBER_P is true, this declaration
15380    appears in a class scope.
15381
15382    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15383    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15384
15385 static tree
15386 cp_parser_single_declaration (cp_parser* parser,
15387                               bool member_p,
15388                               bool* friend_p)
15389 {
15390   int declares_class_or_enum;
15391   tree decl = NULL_TREE;
15392   cp_decl_specifier_seq decl_specifiers;
15393   bool function_definition_p = false;
15394
15395   /* This function is only used when processing a template
15396      declaration.  */
15397   gcc_assert (innermost_scope_kind () == sk_template_parms
15398               || innermost_scope_kind () == sk_template_spec);
15399
15400   /* Defer access checks until we know what is being declared.  */
15401   push_deferring_access_checks (dk_deferred);
15402
15403   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15404      alternative.  */
15405   cp_parser_decl_specifier_seq (parser,
15406                                 CP_PARSER_FLAGS_OPTIONAL,
15407                                 &decl_specifiers,
15408                                 &declares_class_or_enum);
15409   if (friend_p)
15410     *friend_p = cp_parser_friend_p (&decl_specifiers);
15411
15412   /* There are no template typedefs.  */
15413   if (decl_specifiers.specs[(int) ds_typedef])
15414     {
15415       error ("template declaration of %qs", "typedef");
15416       decl = error_mark_node;
15417     }
15418
15419   /* Gather up the access checks that occurred the
15420      decl-specifier-seq.  */
15421   stop_deferring_access_checks ();
15422
15423   /* Check for the declaration of a template class.  */
15424   if (declares_class_or_enum)
15425     {
15426       if (cp_parser_declares_only_class_p (parser))
15427         {
15428           decl = shadow_tag (&decl_specifiers);
15429
15430           /* In this case:
15431
15432                struct C {
15433                  friend template <typename T> struct A<T>::B;
15434                };
15435
15436              A<T>::B will be represented by a TYPENAME_TYPE, and
15437              therefore not recognized by shadow_tag.  */
15438           if (friend_p && *friend_p
15439               && !decl
15440               && decl_specifiers.type
15441               && TYPE_P (decl_specifiers.type))
15442             decl = decl_specifiers.type;
15443
15444           if (decl && decl != error_mark_node)
15445             decl = TYPE_NAME (decl);
15446           else
15447             decl = error_mark_node;
15448         }
15449     }
15450   /* If it's not a template class, try for a template function.  If
15451      the next token is a `;', then this declaration does not declare
15452      anything.  But, if there were errors in the decl-specifiers, then
15453      the error might well have come from an attempted class-specifier.
15454      In that case, there's no need to warn about a missing declarator.  */
15455   if (!decl
15456       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15457           || decl_specifiers.type != error_mark_node))
15458     decl = cp_parser_init_declarator (parser,
15459                                       &decl_specifiers,
15460                                       /*function_definition_allowed_p=*/true,
15461                                       member_p,
15462                                       declares_class_or_enum,
15463                                       &function_definition_p);
15464
15465   pop_deferring_access_checks ();
15466
15467   /* Clear any current qualification; whatever comes next is the start
15468      of something new.  */
15469   parser->scope = NULL_TREE;
15470   parser->qualifying_scope = NULL_TREE;
15471   parser->object_scope = NULL_TREE;
15472   /* Look for a trailing `;' after the declaration.  */
15473   if (!function_definition_p
15474       && (decl == error_mark_node
15475           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15476     cp_parser_skip_to_end_of_block_or_statement (parser);
15477
15478   return decl;
15479 }
15480
15481 /* Parse a cast-expression that is not the operand of a unary "&".  */
15482
15483 static tree
15484 cp_parser_simple_cast_expression (cp_parser *parser)
15485 {
15486   return cp_parser_cast_expression (parser, /*address_p=*/false,
15487                                     /*cast_p=*/false);
15488 }
15489
15490 /* Parse a functional cast to TYPE.  Returns an expression
15491    representing the cast.  */
15492
15493 static tree
15494 cp_parser_functional_cast (cp_parser* parser, tree type)
15495 {
15496   tree expression_list;
15497   tree cast;
15498
15499   expression_list
15500     = cp_parser_parenthesized_expression_list (parser, false,
15501                                                /*cast_p=*/true,
15502                                                /*non_constant_p=*/NULL);
15503
15504   cast = build_functional_cast (type, expression_list);
15505   /* [expr.const]/1: In an integral constant expression "only type
15506      conversions to integral or enumeration type can be used".  */
15507   if (TREE_CODE (type) == TYPE_DECL)
15508     type = TREE_TYPE (type);
15509   if (cast != error_mark_node && !dependent_type_p (type)
15510       && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15511     {
15512       if (cp_parser_non_integral_constant_expression
15513           (parser, "a call to a constructor"))
15514         return error_mark_node;
15515     }
15516   return cast;
15517 }
15518
15519 /* Save the tokens that make up the body of a member function defined
15520    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15521    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15522    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15523    for the member function.  */
15524
15525 static tree
15526 cp_parser_save_member_function_body (cp_parser* parser,
15527                                      cp_decl_specifier_seq *decl_specifiers,
15528                                      cp_declarator *declarator,
15529                                      tree attributes)
15530 {
15531   cp_token *first;
15532   cp_token *last;
15533   tree fn;
15534
15535   /* Create the function-declaration.  */
15536   fn = start_method (decl_specifiers, declarator, attributes);
15537   /* If something went badly wrong, bail out now.  */
15538   if (fn == error_mark_node)
15539     {
15540       /* If there's a function-body, skip it.  */
15541       if (cp_parser_token_starts_function_definition_p
15542           (cp_lexer_peek_token (parser->lexer)))
15543         cp_parser_skip_to_end_of_block_or_statement (parser);
15544       return error_mark_node;
15545     }
15546
15547   /* Remember it, if there default args to post process.  */
15548   cp_parser_save_default_args (parser, fn);
15549
15550   /* Save away the tokens that make up the body of the
15551      function.  */
15552   first = parser->lexer->next_token;
15553   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15554   /* Handle function try blocks.  */
15555   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15556     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15557   last = parser->lexer->next_token;
15558
15559   /* Save away the inline definition; we will process it when the
15560      class is complete.  */
15561   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15562   DECL_PENDING_INLINE_P (fn) = 1;
15563
15564   /* We need to know that this was defined in the class, so that
15565      friend templates are handled correctly.  */
15566   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15567
15568   /* We're done with the inline definition.  */
15569   finish_method (fn);
15570
15571   /* Add FN to the queue of functions to be parsed later.  */
15572   TREE_VALUE (parser->unparsed_functions_queues)
15573     = tree_cons (NULL_TREE, fn,
15574                  TREE_VALUE (parser->unparsed_functions_queues));
15575
15576   return fn;
15577 }
15578
15579 /* Parse a template-argument-list, as well as the trailing ">" (but
15580    not the opening ">").  See cp_parser_template_argument_list for the
15581    return value.  */
15582
15583 static tree
15584 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15585 {
15586   tree arguments;
15587   tree saved_scope;
15588   tree saved_qualifying_scope;
15589   tree saved_object_scope;
15590   bool saved_greater_than_is_operator_p;
15591   bool saved_skip_evaluation;
15592
15593   /* [temp.names]
15594
15595      When parsing a template-id, the first non-nested `>' is taken as
15596      the end of the template-argument-list rather than a greater-than
15597      operator.  */
15598   saved_greater_than_is_operator_p
15599     = parser->greater_than_is_operator_p;
15600   parser->greater_than_is_operator_p = false;
15601   /* Parsing the argument list may modify SCOPE, so we save it
15602      here.  */
15603   saved_scope = parser->scope;
15604   saved_qualifying_scope = parser->qualifying_scope;
15605   saved_object_scope = parser->object_scope;
15606   /* We need to evaluate the template arguments, even though this
15607      template-id may be nested within a "sizeof".  */
15608   saved_skip_evaluation = skip_evaluation;
15609   skip_evaluation = false;
15610   /* Parse the template-argument-list itself.  */
15611   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15612     arguments = NULL_TREE;
15613   else
15614     arguments = cp_parser_template_argument_list (parser);
15615   /* Look for the `>' that ends the template-argument-list. If we find
15616      a '>>' instead, it's probably just a typo.  */
15617   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15618     {
15619       if (!saved_greater_than_is_operator_p)
15620         {
15621           /* If we're in a nested template argument list, the '>>' has
15622             to be a typo for '> >'. We emit the error message, but we
15623             continue parsing and we push a '>' as next token, so that
15624             the argument list will be parsed correctly.  Note that the
15625             global source location is still on the token before the
15626             '>>', so we need to say explicitly where we want it.  */
15627           cp_token *token = cp_lexer_peek_token (parser->lexer);
15628           error ("%H%<>>%> should be %<> >%> "
15629                  "within a nested template argument list",
15630                  &token->location);
15631
15632           /* ??? Proper recovery should terminate two levels of
15633              template argument list here.  */
15634           token->type = CPP_GREATER;
15635         }
15636       else
15637         {
15638           /* If this is not a nested template argument list, the '>>'
15639             is a typo for '>'. Emit an error message and continue.
15640             Same deal about the token location, but here we can get it
15641             right by consuming the '>>' before issuing the diagnostic.  */
15642           cp_lexer_consume_token (parser->lexer);
15643           error ("spurious %<>>%>, use %<>%> to terminate "
15644                  "a template argument list");
15645         }
15646     }
15647   else
15648     cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15649   /* The `>' token might be a greater-than operator again now.  */
15650   parser->greater_than_is_operator_p
15651     = saved_greater_than_is_operator_p;
15652   /* Restore the SAVED_SCOPE.  */
15653   parser->scope = saved_scope;
15654   parser->qualifying_scope = saved_qualifying_scope;
15655   parser->object_scope = saved_object_scope;
15656   skip_evaluation = saved_skip_evaluation;
15657
15658   return arguments;
15659 }
15660
15661 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15662    arguments, or the body of the function have not yet been parsed,
15663    parse them now.  */
15664
15665 static void
15666 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15667 {
15668   /* If this member is a template, get the underlying
15669      FUNCTION_DECL.  */
15670   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15671     member_function = DECL_TEMPLATE_RESULT (member_function);
15672
15673   /* There should not be any class definitions in progress at this
15674      point; the bodies of members are only parsed outside of all class
15675      definitions.  */
15676   gcc_assert (parser->num_classes_being_defined == 0);
15677   /* While we're parsing the member functions we might encounter more
15678      classes.  We want to handle them right away, but we don't want
15679      them getting mixed up with functions that are currently in the
15680      queue.  */
15681   parser->unparsed_functions_queues
15682     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15683
15684   /* Make sure that any template parameters are in scope.  */
15685   maybe_begin_member_template_processing (member_function);
15686
15687   /* If the body of the function has not yet been parsed, parse it
15688      now.  */
15689   if (DECL_PENDING_INLINE_P (member_function))
15690     {
15691       tree function_scope;
15692       cp_token_cache *tokens;
15693
15694       /* The function is no longer pending; we are processing it.  */
15695       tokens = DECL_PENDING_INLINE_INFO (member_function);
15696       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15697       DECL_PENDING_INLINE_P (member_function) = 0;
15698
15699       /* If this is a local class, enter the scope of the containing
15700          function.  */
15701       function_scope = current_function_decl;
15702       if (function_scope)
15703         push_function_context_to (function_scope);
15704
15705
15706       /* Push the body of the function onto the lexer stack.  */
15707       cp_parser_push_lexer_for_tokens (parser, tokens);
15708
15709       /* Let the front end know that we going to be defining this
15710          function.  */
15711       start_preparsed_function (member_function, NULL_TREE,
15712                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15713
15714       /* Don't do access checking if it is a templated function.  */
15715       if (processing_template_decl)
15716         push_deferring_access_checks (dk_no_check);
15717
15718       /* Now, parse the body of the function.  */
15719       cp_parser_function_definition_after_declarator (parser,
15720                                                       /*inline_p=*/true);
15721
15722       if (processing_template_decl)
15723         pop_deferring_access_checks ();
15724
15725       /* Leave the scope of the containing function.  */
15726       if (function_scope)
15727         pop_function_context_from (function_scope);
15728       cp_parser_pop_lexer (parser);
15729     }
15730
15731   /* Remove any template parameters from the symbol table.  */
15732   maybe_end_member_template_processing ();
15733
15734   /* Restore the queue.  */
15735   parser->unparsed_functions_queues
15736     = TREE_CHAIN (parser->unparsed_functions_queues);
15737 }
15738
15739 /* If DECL contains any default args, remember it on the unparsed
15740    functions queue.  */
15741
15742 static void
15743 cp_parser_save_default_args (cp_parser* parser, tree decl)
15744 {
15745   tree probe;
15746
15747   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15748        probe;
15749        probe = TREE_CHAIN (probe))
15750     if (TREE_PURPOSE (probe))
15751       {
15752         TREE_PURPOSE (parser->unparsed_functions_queues)
15753           = tree_cons (current_class_type, decl,
15754                        TREE_PURPOSE (parser->unparsed_functions_queues));
15755         break;
15756       }
15757   return;
15758 }
15759
15760 /* FN is a FUNCTION_DECL which may contains a parameter with an
15761    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15762    assumes that the current scope is the scope in which the default
15763    argument should be processed.  */
15764
15765 static void
15766 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15767 {
15768   bool saved_local_variables_forbidden_p;
15769   tree parm;
15770
15771   /* While we're parsing the default args, we might (due to the
15772      statement expression extension) encounter more classes.  We want
15773      to handle them right away, but we don't want them getting mixed
15774      up with default args that are currently in the queue.  */
15775   parser->unparsed_functions_queues
15776     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15777
15778   /* Local variable names (and the `this' keyword) may not appear
15779      in a default argument.  */
15780   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15781   parser->local_variables_forbidden_p = true;
15782
15783   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15784        parm;
15785        parm = TREE_CHAIN (parm))
15786     {
15787       cp_token_cache *tokens;
15788       tree default_arg = TREE_PURPOSE (parm);
15789       tree parsed_arg;
15790       VEC(tree,gc) *insts;
15791       tree copy;
15792       unsigned ix;
15793
15794       if (!default_arg)
15795         continue;
15796
15797       if (TREE_CODE (default_arg) != DEFAULT_ARG)
15798         /* This can happen for a friend declaration for a function
15799            already declared with default arguments.  */
15800         continue;
15801
15802        /* Push the saved tokens for the default argument onto the parser's
15803           lexer stack.  */
15804       tokens = DEFARG_TOKENS (default_arg);
15805       cp_parser_push_lexer_for_tokens (parser, tokens);
15806
15807       /* Parse the assignment-expression.  */
15808       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15809
15810       if (!processing_template_decl)
15811         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
15812       
15813       TREE_PURPOSE (parm) = parsed_arg;
15814
15815       /* Update any instantiations we've already created.  */
15816       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15817            VEC_iterate (tree, insts, ix, copy); ix++)
15818         TREE_PURPOSE (copy) = parsed_arg;
15819
15820       /* If the token stream has not been completely used up, then
15821          there was extra junk after the end of the default
15822          argument.  */
15823       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15824         cp_parser_error (parser, "expected %<,%>");
15825
15826       /* Revert to the main lexer.  */
15827       cp_parser_pop_lexer (parser);
15828     }
15829
15830   /* Restore the state of local_variables_forbidden_p.  */
15831   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15832
15833   /* Restore the queue.  */
15834   parser->unparsed_functions_queues
15835     = TREE_CHAIN (parser->unparsed_functions_queues);
15836 }
15837
15838 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15839    either a TYPE or an expression, depending on the form of the
15840    input.  The KEYWORD indicates which kind of expression we have
15841    encountered.  */
15842
15843 static tree
15844 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15845 {
15846   static const char *format;
15847   tree expr = NULL_TREE;
15848   const char *saved_message;
15849   bool saved_integral_constant_expression_p;
15850   bool saved_non_integral_constant_expression_p;
15851
15852   /* Initialize FORMAT the first time we get here.  */
15853   if (!format)
15854     format = "types may not be defined in '%s' expressions";
15855
15856   /* Types cannot be defined in a `sizeof' expression.  Save away the
15857      old message.  */
15858   saved_message = parser->type_definition_forbidden_message;
15859   /* And create the new one.  */
15860   parser->type_definition_forbidden_message
15861     = XNEWVEC (const char, strlen (format)
15862                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15863                + 1 /* `\0' */);
15864   sprintf ((char *) parser->type_definition_forbidden_message,
15865            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15866
15867   /* The restrictions on constant-expressions do not apply inside
15868      sizeof expressions.  */
15869   saved_integral_constant_expression_p
15870     = parser->integral_constant_expression_p;
15871   saved_non_integral_constant_expression_p
15872     = parser->non_integral_constant_expression_p;
15873   parser->integral_constant_expression_p = false;
15874
15875   /* Do not actually evaluate the expression.  */
15876   ++skip_evaluation;
15877   /* If it's a `(', then we might be looking at the type-id
15878      construction.  */
15879   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15880     {
15881       tree type;
15882       bool saved_in_type_id_in_expr_p;
15883
15884       /* We can't be sure yet whether we're looking at a type-id or an
15885          expression.  */
15886       cp_parser_parse_tentatively (parser);
15887       /* Consume the `('.  */
15888       cp_lexer_consume_token (parser->lexer);
15889       /* Parse the type-id.  */
15890       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15891       parser->in_type_id_in_expr_p = true;
15892       type = cp_parser_type_id (parser);
15893       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15894       /* Now, look for the trailing `)'.  */
15895       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15896       /* If all went well, then we're done.  */
15897       if (cp_parser_parse_definitely (parser))
15898         {
15899           cp_decl_specifier_seq decl_specs;
15900
15901           /* Build a trivial decl-specifier-seq.  */
15902           clear_decl_specs (&decl_specs);
15903           decl_specs.type = type;
15904
15905           /* Call grokdeclarator to figure out what type this is.  */
15906           expr = grokdeclarator (NULL,
15907                                  &decl_specs,
15908                                  TYPENAME,
15909                                  /*initialized=*/0,
15910                                  /*attrlist=*/NULL);
15911         }
15912     }
15913
15914   /* If the type-id production did not work out, then we must be
15915      looking at the unary-expression production.  */
15916   if (!expr)
15917     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15918                                        /*cast_p=*/false);
15919   /* Go back to evaluating expressions.  */
15920   --skip_evaluation;
15921
15922   /* Free the message we created.  */
15923   free ((char *) parser->type_definition_forbidden_message);
15924   /* And restore the old one.  */
15925   parser->type_definition_forbidden_message = saved_message;
15926   parser->integral_constant_expression_p
15927     = saved_integral_constant_expression_p;
15928   parser->non_integral_constant_expression_p
15929     = saved_non_integral_constant_expression_p;
15930
15931   return expr;
15932 }
15933
15934 /* If the current declaration has no declarator, return true.  */
15935
15936 static bool
15937 cp_parser_declares_only_class_p (cp_parser *parser)
15938 {
15939   /* If the next token is a `;' or a `,' then there is no
15940      declarator.  */
15941   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15942           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15943 }
15944
15945 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15946
15947 static void
15948 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15949                              cp_storage_class storage_class)
15950 {
15951   if (decl_specs->storage_class != sc_none)
15952     decl_specs->multiple_storage_classes_p = true;
15953   else
15954     decl_specs->storage_class = storage_class;
15955 }
15956
15957 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15958    is true, the type is a user-defined type; otherwise it is a
15959    built-in type specified by a keyword.  */
15960
15961 static void
15962 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15963                               tree type_spec,
15964                               bool user_defined_p)
15965 {
15966   decl_specs->any_specifiers_p = true;
15967
15968   /* If the user tries to redeclare bool or wchar_t (with, for
15969      example, in "typedef int wchar_t;") we remember that this is what
15970      happened.  In system headers, we ignore these declarations so
15971      that G++ can work with system headers that are not C++-safe.  */
15972   if (decl_specs->specs[(int) ds_typedef]
15973       && !user_defined_p
15974       && (type_spec == boolean_type_node
15975           || type_spec == wchar_type_node)
15976       && (decl_specs->type
15977           || decl_specs->specs[(int) ds_long]
15978           || decl_specs->specs[(int) ds_short]
15979           || decl_specs->specs[(int) ds_unsigned]
15980           || decl_specs->specs[(int) ds_signed]))
15981     {
15982       decl_specs->redefined_builtin_type = type_spec;
15983       if (!decl_specs->type)
15984         {
15985           decl_specs->type = type_spec;
15986           decl_specs->user_defined_type_p = false;
15987         }
15988     }
15989   else if (decl_specs->type)
15990     decl_specs->multiple_types_p = true;
15991   else
15992     {
15993       decl_specs->type = type_spec;
15994       decl_specs->user_defined_type_p = user_defined_p;
15995       decl_specs->redefined_builtin_type = NULL_TREE;
15996     }
15997 }
15998
15999 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16000    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
16001
16002 static bool
16003 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16004 {
16005   return decl_specifiers->specs[(int) ds_friend] != 0;
16006 }
16007
16008 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
16009    issue an error message indicating that TOKEN_DESC was expected.
16010
16011    Returns the token consumed, if the token had the appropriate type.
16012    Otherwise, returns NULL.  */
16013
16014 static cp_token *
16015 cp_parser_require (cp_parser* parser,
16016                    enum cpp_ttype type,
16017                    const char* token_desc)
16018 {
16019   if (cp_lexer_next_token_is (parser->lexer, type))
16020     return cp_lexer_consume_token (parser->lexer);
16021   else
16022     {
16023       /* Output the MESSAGE -- unless we're parsing tentatively.  */
16024       if (!cp_parser_simulate_error (parser))
16025         {
16026           char *message = concat ("expected ", token_desc, NULL);
16027           cp_parser_error (parser, message);
16028           free (message);
16029         }
16030       return NULL;
16031     }
16032 }
16033
16034 /* Like cp_parser_require, except that tokens will be skipped until
16035    the desired token is found.  An error message is still produced if
16036    the next token is not as expected.  */
16037
16038 static void
16039 cp_parser_skip_until_found (cp_parser* parser,
16040                             enum cpp_ttype type,
16041                             const char* token_desc)
16042 {
16043   cp_token *token;
16044   unsigned nesting_depth = 0;
16045
16046   if (cp_parser_require (parser, type, token_desc))
16047     return;
16048
16049   /* Skip tokens until the desired token is found.  */
16050   while (true)
16051     {
16052       /* Peek at the next token.  */
16053       token = cp_lexer_peek_token (parser->lexer);
16054
16055       /* If we've reached the token we want, consume it and stop.  */
16056       if (token->type == type && !nesting_depth)
16057         {
16058           cp_lexer_consume_token (parser->lexer);
16059           return;
16060         }
16061
16062       switch (token->type)
16063         {
16064         case CPP_EOF:
16065         case CPP_PRAGMA_EOL:
16066           /* If we've run out of tokens, stop.  */
16067           return;
16068
16069         case CPP_OPEN_BRACE:
16070         case CPP_OPEN_PAREN:
16071         case CPP_OPEN_SQUARE:
16072           ++nesting_depth;
16073           break;
16074
16075         case CPP_CLOSE_BRACE:
16076         case CPP_CLOSE_PAREN:
16077         case CPP_CLOSE_SQUARE:
16078           if (nesting_depth-- == 0)
16079             return;
16080           break;
16081
16082         default:
16083           break;
16084         }
16085
16086       /* Consume this token.  */
16087       cp_lexer_consume_token (parser->lexer);
16088     }
16089 }
16090
16091 /* If the next token is the indicated keyword, consume it.  Otherwise,
16092    issue an error message indicating that TOKEN_DESC was expected.
16093
16094    Returns the token consumed, if the token had the appropriate type.
16095    Otherwise, returns NULL.  */
16096
16097 static cp_token *
16098 cp_parser_require_keyword (cp_parser* parser,
16099                            enum rid keyword,
16100                            const char* token_desc)
16101 {
16102   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16103
16104   if (token && token->keyword != keyword)
16105     {
16106       dyn_string_t error_msg;
16107
16108       /* Format the error message.  */
16109       error_msg = dyn_string_new (0);
16110       dyn_string_append_cstr (error_msg, "expected ");
16111       dyn_string_append_cstr (error_msg, token_desc);
16112       cp_parser_error (parser, error_msg->s);
16113       dyn_string_delete (error_msg);
16114       return NULL;
16115     }
16116
16117   return token;
16118 }
16119
16120 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16121    function-definition.  */
16122
16123 static bool
16124 cp_parser_token_starts_function_definition_p (cp_token* token)
16125 {
16126   return (/* An ordinary function-body begins with an `{'.  */
16127           token->type == CPP_OPEN_BRACE
16128           /* A ctor-initializer begins with a `:'.  */
16129           || token->type == CPP_COLON
16130           /* A function-try-block begins with `try'.  */
16131           || token->keyword == RID_TRY
16132           /* The named return value extension begins with `return'.  */
16133           || token->keyword == RID_RETURN);
16134 }
16135
16136 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16137    definition.  */
16138
16139 static bool
16140 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16141 {
16142   cp_token *token;
16143
16144   token = cp_lexer_peek_token (parser->lexer);
16145   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16146 }
16147
16148 /* Returns TRUE iff the next token is the "," or ">" ending a
16149    template-argument.  */
16150
16151 static bool
16152 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16153 {
16154   cp_token *token;
16155
16156   token = cp_lexer_peek_token (parser->lexer);
16157   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16158 }
16159
16160 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16161    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
16162
16163 static bool
16164 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16165                                                      size_t n)
16166 {
16167   cp_token *token;
16168
16169   token = cp_lexer_peek_nth_token (parser->lexer, n);
16170   if (token->type == CPP_LESS)
16171     return true;
16172   /* Check for the sequence `<::' in the original code. It would be lexed as
16173      `[:', where `[' is a digraph, and there is no whitespace before
16174      `:'.  */
16175   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16176     {
16177       cp_token *token2;
16178       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16179       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16180         return true;
16181     }
16182   return false;
16183 }
16184
16185 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16186    or none_type otherwise.  */
16187
16188 static enum tag_types
16189 cp_parser_token_is_class_key (cp_token* token)
16190 {
16191   switch (token->keyword)
16192     {
16193     case RID_CLASS:
16194       return class_type;
16195     case RID_STRUCT:
16196       return record_type;
16197     case RID_UNION:
16198       return union_type;
16199
16200     default:
16201       return none_type;
16202     }
16203 }
16204
16205 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16206
16207 static void
16208 cp_parser_check_class_key (enum tag_types class_key, tree type)
16209 {
16210   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16211     pedwarn ("%qs tag used in naming %q#T",
16212             class_key == union_type ? "union"
16213              : class_key == record_type ? "struct" : "class",
16214              type);
16215 }
16216
16217 /* Issue an error message if DECL is redeclared with different
16218    access than its original declaration [class.access.spec/3].
16219    This applies to nested classes and nested class templates.
16220    [class.mem/1].  */
16221
16222 static void
16223 cp_parser_check_access_in_redeclaration (tree decl)
16224 {
16225   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16226     return;
16227
16228   if ((TREE_PRIVATE (decl)
16229        != (current_access_specifier == access_private_node))
16230       || (TREE_PROTECTED (decl)
16231           != (current_access_specifier == access_protected_node)))
16232     error ("%qD redeclared with different access", decl);
16233 }
16234
16235 /* Look for the `template' keyword, as a syntactic disambiguator.
16236    Return TRUE iff it is present, in which case it will be
16237    consumed.  */
16238
16239 static bool
16240 cp_parser_optional_template_keyword (cp_parser *parser)
16241 {
16242   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16243     {
16244       /* The `template' keyword can only be used within templates;
16245          outside templates the parser can always figure out what is a
16246          template and what is not.  */
16247       if (!processing_template_decl)
16248         {
16249           error ("%<template%> (as a disambiguator) is only allowed "
16250                  "within templates");
16251           /* If this part of the token stream is rescanned, the same
16252              error message would be generated.  So, we purge the token
16253              from the stream.  */
16254           cp_lexer_purge_token (parser->lexer);
16255           return false;
16256         }
16257       else
16258         {
16259           /* Consume the `template' keyword.  */
16260           cp_lexer_consume_token (parser->lexer);
16261           return true;
16262         }
16263     }
16264
16265   return false;
16266 }
16267
16268 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16269    set PARSER->SCOPE, and perform other related actions.  */
16270
16271 static void
16272 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16273 {
16274   tree value;
16275   tree check;
16276
16277   /* Get the stored value.  */
16278   value = cp_lexer_consume_token (parser->lexer)->value;
16279   /* Perform any access checks that were deferred.  */
16280   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16281     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16282   /* Set the scope from the stored value.  */
16283   parser->scope = TREE_VALUE (value);
16284   parser->qualifying_scope = TREE_TYPE (value);
16285   parser->object_scope = NULL_TREE;
16286 }
16287
16288 /* Consume tokens up through a non-nested END token.  */
16289
16290 static void
16291 cp_parser_cache_group (cp_parser *parser,
16292                        enum cpp_ttype end,
16293                        unsigned depth)
16294 {
16295   while (true)
16296     {
16297       cp_token *token;
16298
16299       /* Abort a parenthesized expression if we encounter a brace.  */
16300       if ((end == CPP_CLOSE_PAREN || depth == 0)
16301           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16302         return;
16303       /* If we've reached the end of the file, stop.  */
16304       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16305           || (end != CPP_PRAGMA_EOL
16306               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16307         return;
16308       /* Consume the next token.  */
16309       token = cp_lexer_consume_token (parser->lexer);
16310       /* See if it starts a new group.  */
16311       if (token->type == CPP_OPEN_BRACE)
16312         {
16313           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16314           if (depth == 0)
16315             return;
16316         }
16317       else if (token->type == CPP_OPEN_PAREN)
16318         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16319       else if (token->type == CPP_PRAGMA)
16320         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16321       else if (token->type == end)
16322         return;
16323     }
16324 }
16325
16326 /* Begin parsing tentatively.  We always save tokens while parsing
16327    tentatively so that if the tentative parsing fails we can restore the
16328    tokens.  */
16329
16330 static void
16331 cp_parser_parse_tentatively (cp_parser* parser)
16332 {
16333   /* Enter a new parsing context.  */
16334   parser->context = cp_parser_context_new (parser->context);
16335   /* Begin saving tokens.  */
16336   cp_lexer_save_tokens (parser->lexer);
16337   /* In order to avoid repetitive access control error messages,
16338      access checks are queued up until we are no longer parsing
16339      tentatively.  */
16340   push_deferring_access_checks (dk_deferred);
16341 }
16342
16343 /* Commit to the currently active tentative parse.  */
16344
16345 static void
16346 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16347 {
16348   cp_parser_context *context;
16349   cp_lexer *lexer;
16350
16351   /* Mark all of the levels as committed.  */
16352   lexer = parser->lexer;
16353   for (context = parser->context; context->next; context = context->next)
16354     {
16355       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16356         break;
16357       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16358       while (!cp_lexer_saving_tokens (lexer))
16359         lexer = lexer->next;
16360       cp_lexer_commit_tokens (lexer);
16361     }
16362 }
16363
16364 /* Abort the currently active tentative parse.  All consumed tokens
16365    will be rolled back, and no diagnostics will be issued.  */
16366
16367 static void
16368 cp_parser_abort_tentative_parse (cp_parser* parser)
16369 {
16370   cp_parser_simulate_error (parser);
16371   /* Now, pretend that we want to see if the construct was
16372      successfully parsed.  */
16373   cp_parser_parse_definitely (parser);
16374 }
16375
16376 /* Stop parsing tentatively.  If a parse error has occurred, restore the
16377    token stream.  Otherwise, commit to the tokens we have consumed.
16378    Returns true if no error occurred; false otherwise.  */
16379
16380 static bool
16381 cp_parser_parse_definitely (cp_parser* parser)
16382 {
16383   bool error_occurred;
16384   cp_parser_context *context;
16385
16386   /* Remember whether or not an error occurred, since we are about to
16387      destroy that information.  */
16388   error_occurred = cp_parser_error_occurred (parser);
16389   /* Remove the topmost context from the stack.  */
16390   context = parser->context;
16391   parser->context = context->next;
16392   /* If no parse errors occurred, commit to the tentative parse.  */
16393   if (!error_occurred)
16394     {
16395       /* Commit to the tokens read tentatively, unless that was
16396          already done.  */
16397       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16398         cp_lexer_commit_tokens (parser->lexer);
16399
16400       pop_to_parent_deferring_access_checks ();
16401     }
16402   /* Otherwise, if errors occurred, roll back our state so that things
16403      are just as they were before we began the tentative parse.  */
16404   else
16405     {
16406       cp_lexer_rollback_tokens (parser->lexer);
16407       pop_deferring_access_checks ();
16408     }
16409   /* Add the context to the front of the free list.  */
16410   context->next = cp_parser_context_free_list;
16411   cp_parser_context_free_list = context;
16412
16413   return !error_occurred;
16414 }
16415
16416 /* Returns true if we are parsing tentatively and are not committed to
16417    this tentative parse.  */
16418
16419 static bool
16420 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16421 {
16422   return (cp_parser_parsing_tentatively (parser)
16423           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16424 }
16425
16426 /* Returns nonzero iff an error has occurred during the most recent
16427    tentative parse.  */
16428
16429 static bool
16430 cp_parser_error_occurred (cp_parser* parser)
16431 {
16432   return (cp_parser_parsing_tentatively (parser)
16433           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16434 }
16435
16436 /* Returns nonzero if GNU extensions are allowed.  */
16437
16438 static bool
16439 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16440 {
16441   return parser->allow_gnu_extensions_p;
16442 }
16443 \f
16444 /* Objective-C++ Productions */
16445
16446
16447 /* Parse an Objective-C expression, which feeds into a primary-expression
16448    above.
16449
16450    objc-expression:
16451      objc-message-expression
16452      objc-string-literal
16453      objc-encode-expression
16454      objc-protocol-expression
16455      objc-selector-expression
16456
16457   Returns a tree representation of the expression.  */
16458
16459 static tree
16460 cp_parser_objc_expression (cp_parser* parser)
16461 {
16462   /* Try to figure out what kind of declaration is present.  */
16463   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16464
16465   switch (kwd->type)
16466     {
16467     case CPP_OPEN_SQUARE:
16468       return cp_parser_objc_message_expression (parser);
16469
16470     case CPP_OBJC_STRING:
16471       kwd = cp_lexer_consume_token (parser->lexer);
16472       return objc_build_string_object (kwd->value);
16473
16474     case CPP_KEYWORD:
16475       switch (kwd->keyword)
16476         {
16477         case RID_AT_ENCODE:
16478           return cp_parser_objc_encode_expression (parser);
16479
16480         case RID_AT_PROTOCOL:
16481           return cp_parser_objc_protocol_expression (parser);
16482
16483         case RID_AT_SELECTOR:
16484           return cp_parser_objc_selector_expression (parser);
16485
16486         default:
16487           break;
16488         }
16489     default:
16490       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16491       cp_parser_skip_to_end_of_block_or_statement (parser);
16492     }
16493
16494   return error_mark_node;
16495 }
16496
16497 /* Parse an Objective-C message expression.
16498
16499    objc-message-expression:
16500      [ objc-message-receiver objc-message-args ]
16501
16502    Returns a representation of an Objective-C message.  */
16503
16504 static tree
16505 cp_parser_objc_message_expression (cp_parser* parser)
16506 {
16507   tree receiver, messageargs;
16508
16509   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
16510   receiver = cp_parser_objc_message_receiver (parser);
16511   messageargs = cp_parser_objc_message_args (parser);
16512   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16513
16514   return objc_build_message_expr (build_tree_list (receiver, messageargs));
16515 }
16516
16517 /* Parse an objc-message-receiver.
16518
16519    objc-message-receiver:
16520      expression
16521      simple-type-specifier
16522
16523   Returns a representation of the type or expression.  */
16524
16525 static tree
16526 cp_parser_objc_message_receiver (cp_parser* parser)
16527 {
16528   tree rcv;
16529
16530   /* An Objective-C message receiver may be either (1) a type
16531      or (2) an expression.  */
16532   cp_parser_parse_tentatively (parser);
16533   rcv = cp_parser_expression (parser, false);
16534
16535   if (cp_parser_parse_definitely (parser))
16536     return rcv;
16537
16538   rcv = cp_parser_simple_type_specifier (parser,
16539                                          /*decl_specs=*/NULL,
16540                                          CP_PARSER_FLAGS_NONE);
16541
16542   return objc_get_class_reference (rcv);
16543 }
16544
16545 /* Parse the arguments and selectors comprising an Objective-C message.
16546
16547    objc-message-args:
16548      objc-selector
16549      objc-selector-args
16550      objc-selector-args , objc-comma-args
16551
16552    objc-selector-args:
16553      objc-selector [opt] : assignment-expression
16554      objc-selector-args objc-selector [opt] : assignment-expression
16555
16556    objc-comma-args:
16557      assignment-expression
16558      objc-comma-args , assignment-expression
16559
16560    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16561    selector arguments and TREE_VALUE containing a list of comma
16562    arguments.  */
16563
16564 static tree
16565 cp_parser_objc_message_args (cp_parser* parser)
16566 {
16567   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16568   bool maybe_unary_selector_p = true;
16569   cp_token *token = cp_lexer_peek_token (parser->lexer);
16570
16571   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16572     {
16573       tree selector = NULL_TREE, arg;
16574
16575       if (token->type != CPP_COLON)
16576         selector = cp_parser_objc_selector (parser);
16577
16578       /* Detect if we have a unary selector.  */
16579       if (maybe_unary_selector_p
16580           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16581         return build_tree_list (selector, NULL_TREE);
16582
16583       maybe_unary_selector_p = false;
16584       cp_parser_require (parser, CPP_COLON, "`:'");
16585       arg = cp_parser_assignment_expression (parser, false);
16586
16587       sel_args
16588         = chainon (sel_args,
16589                    build_tree_list (selector, arg));
16590
16591       token = cp_lexer_peek_token (parser->lexer);
16592     }
16593
16594   /* Handle non-selector arguments, if any. */
16595   while (token->type == CPP_COMMA)
16596     {
16597       tree arg;
16598
16599       cp_lexer_consume_token (parser->lexer);
16600       arg = cp_parser_assignment_expression (parser, false);
16601
16602       addl_args
16603         = chainon (addl_args,
16604                    build_tree_list (NULL_TREE, arg));
16605
16606       token = cp_lexer_peek_token (parser->lexer);
16607     }
16608
16609   return build_tree_list (sel_args, addl_args);
16610 }
16611
16612 /* Parse an Objective-C encode expression.
16613
16614    objc-encode-expression:
16615      @encode objc-typename
16616
16617    Returns an encoded representation of the type argument.  */
16618
16619 static tree
16620 cp_parser_objc_encode_expression (cp_parser* parser)
16621 {
16622   tree type;
16623
16624   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
16625   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16626   type = complete_type (cp_parser_type_id (parser));
16627   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16628
16629   if (!type)
16630     {
16631       error ("%<@encode%> must specify a type as an argument");
16632       return error_mark_node;
16633     }
16634
16635   return objc_build_encode_expr (type);
16636 }
16637
16638 /* Parse an Objective-C @defs expression.  */
16639
16640 static tree
16641 cp_parser_objc_defs_expression (cp_parser *parser)
16642 {
16643   tree name;
16644
16645   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
16646   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16647   name = cp_parser_identifier (parser);
16648   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16649
16650   return objc_get_class_ivars (name);
16651 }
16652
16653 /* Parse an Objective-C protocol expression.
16654
16655   objc-protocol-expression:
16656     @protocol ( identifier )
16657
16658   Returns a representation of the protocol expression.  */
16659
16660 static tree
16661 cp_parser_objc_protocol_expression (cp_parser* parser)
16662 {
16663   tree proto;
16664
16665   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
16666   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16667   proto = cp_parser_identifier (parser);
16668   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16669
16670   return objc_build_protocol_expr (proto);
16671 }
16672
16673 /* Parse an Objective-C selector expression.
16674
16675    objc-selector-expression:
16676      @selector ( objc-method-signature )
16677
16678    objc-method-signature:
16679      objc-selector
16680      objc-selector-seq
16681
16682    objc-selector-seq:
16683      objc-selector :
16684      objc-selector-seq objc-selector :
16685
16686   Returns a representation of the method selector.  */
16687
16688 static tree
16689 cp_parser_objc_selector_expression (cp_parser* parser)
16690 {
16691   tree sel_seq = NULL_TREE;
16692   bool maybe_unary_selector_p = true;
16693   cp_token *token;
16694
16695   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
16696   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16697   token = cp_lexer_peek_token (parser->lexer);
16698
16699   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16700          || token->type == CPP_SCOPE)
16701     {
16702       tree selector = NULL_TREE;
16703
16704       if (token->type != CPP_COLON
16705           || token->type == CPP_SCOPE)
16706         selector = cp_parser_objc_selector (parser);
16707
16708       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16709           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16710         {
16711           /* Detect if we have a unary selector.  */
16712           if (maybe_unary_selector_p)
16713             {
16714               sel_seq = selector;
16715               goto finish_selector;
16716             }
16717           else
16718             {
16719               cp_parser_error (parser, "expected %<:%>");
16720             }
16721         }
16722       maybe_unary_selector_p = false;
16723       token = cp_lexer_consume_token (parser->lexer);
16724       
16725       if (token->type == CPP_SCOPE)
16726         {
16727           sel_seq
16728             = chainon (sel_seq,
16729                        build_tree_list (selector, NULL_TREE));
16730           sel_seq
16731             = chainon (sel_seq,
16732                        build_tree_list (NULL_TREE, NULL_TREE));
16733         }
16734       else
16735         sel_seq
16736           = chainon (sel_seq,
16737                      build_tree_list (selector, NULL_TREE));
16738
16739       token = cp_lexer_peek_token (parser->lexer);
16740     }
16741
16742  finish_selector:
16743   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16744
16745   return objc_build_selector_expr (sel_seq);
16746 }
16747
16748 /* Parse a list of identifiers.
16749
16750    objc-identifier-list:
16751      identifier
16752      objc-identifier-list , identifier
16753
16754    Returns a TREE_LIST of identifier nodes.  */
16755
16756 static tree
16757 cp_parser_objc_identifier_list (cp_parser* parser)
16758 {
16759   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16760   cp_token *sep = cp_lexer_peek_token (parser->lexer);
16761
16762   while (sep->type == CPP_COMMA)
16763     {
16764       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16765       list = chainon (list,
16766                       build_tree_list (NULL_TREE,
16767                                        cp_parser_identifier (parser)));
16768       sep = cp_lexer_peek_token (parser->lexer);
16769     }
16770
16771   return list;
16772 }
16773
16774 /* Parse an Objective-C alias declaration.
16775
16776    objc-alias-declaration:
16777      @compatibility_alias identifier identifier ;
16778
16779    This function registers the alias mapping with the Objective-C front-end.
16780    It returns nothing.  */
16781
16782 static void
16783 cp_parser_objc_alias_declaration (cp_parser* parser)
16784 {
16785   tree alias, orig;
16786
16787   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
16788   alias = cp_parser_identifier (parser);
16789   orig = cp_parser_identifier (parser);
16790   objc_declare_alias (alias, orig);
16791   cp_parser_consume_semicolon_at_end_of_statement (parser);
16792 }
16793
16794 /* Parse an Objective-C class forward-declaration.
16795
16796    objc-class-declaration:
16797      @class objc-identifier-list ;
16798
16799    The function registers the forward declarations with the Objective-C
16800    front-end.  It returns nothing.  */
16801
16802 static void
16803 cp_parser_objc_class_declaration (cp_parser* parser)
16804 {
16805   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
16806   objc_declare_class (cp_parser_objc_identifier_list (parser));
16807   cp_parser_consume_semicolon_at_end_of_statement (parser);
16808 }
16809
16810 /* Parse a list of Objective-C protocol references.
16811
16812    objc-protocol-refs-opt:
16813      objc-protocol-refs [opt]
16814
16815    objc-protocol-refs:
16816      < objc-identifier-list >
16817
16818    Returns a TREE_LIST of identifiers, if any.  */
16819
16820 static tree
16821 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16822 {
16823   tree protorefs = NULL_TREE;
16824
16825   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16826     {
16827       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
16828       protorefs = cp_parser_objc_identifier_list (parser);
16829       cp_parser_require (parser, CPP_GREATER, "`>'");
16830     }
16831
16832   return protorefs;
16833 }
16834
16835 /* Parse a Objective-C visibility specification.  */
16836
16837 static void
16838 cp_parser_objc_visibility_spec (cp_parser* parser)
16839 {
16840   cp_token *vis = cp_lexer_peek_token (parser->lexer);
16841
16842   switch (vis->keyword)
16843     {
16844     case RID_AT_PRIVATE:
16845       objc_set_visibility (2);
16846       break;
16847     case RID_AT_PROTECTED:
16848       objc_set_visibility (0);
16849       break;
16850     case RID_AT_PUBLIC:
16851       objc_set_visibility (1);
16852       break;
16853     default:
16854       return;
16855     }
16856
16857   /* Eat '@private'/'@protected'/'@public'.  */
16858   cp_lexer_consume_token (parser->lexer);
16859 }
16860
16861 /* Parse an Objective-C method type.  */
16862
16863 static void
16864 cp_parser_objc_method_type (cp_parser* parser)
16865 {
16866   objc_set_method_type
16867    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16868     ? PLUS_EXPR
16869     : MINUS_EXPR);
16870 }
16871
16872 /* Parse an Objective-C protocol qualifier.  */
16873
16874 static tree
16875 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16876 {
16877   tree quals = NULL_TREE, node;
16878   cp_token *token = cp_lexer_peek_token (parser->lexer);
16879
16880   node = token->value;
16881
16882   while (node && TREE_CODE (node) == IDENTIFIER_NODE
16883          && (node == ridpointers [(int) RID_IN]
16884              || node == ridpointers [(int) RID_OUT]
16885              || node == ridpointers [(int) RID_INOUT]
16886              || node == ridpointers [(int) RID_BYCOPY]
16887              || node == ridpointers [(int) RID_BYREF]
16888              || node == ridpointers [(int) RID_ONEWAY]))
16889     {
16890       quals = tree_cons (NULL_TREE, node, quals);
16891       cp_lexer_consume_token (parser->lexer);
16892       token = cp_lexer_peek_token (parser->lexer);
16893       node = token->value;
16894     }
16895
16896   return quals;
16897 }
16898
16899 /* Parse an Objective-C typename.  */
16900
16901 static tree
16902 cp_parser_objc_typename (cp_parser* parser)
16903 {
16904   tree typename = NULL_TREE;
16905
16906   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16907     {
16908       tree proto_quals, cp_type = NULL_TREE;
16909
16910       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
16911       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16912
16913       /* An ObjC type name may consist of just protocol qualifiers, in which
16914          case the type shall default to 'id'.  */
16915       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16916         cp_type = cp_parser_type_id (parser);
16917
16918       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16919       typename = build_tree_list (proto_quals, cp_type);
16920     }
16921
16922   return typename;
16923 }
16924
16925 /* Check to see if TYPE refers to an Objective-C selector name.  */
16926
16927 static bool
16928 cp_parser_objc_selector_p (enum cpp_ttype type)
16929 {
16930   return (type == CPP_NAME || type == CPP_KEYWORD
16931           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16932           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16933           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16934           || type == CPP_XOR || type == CPP_XOR_EQ);
16935 }
16936
16937 /* Parse an Objective-C selector.  */
16938
16939 static tree
16940 cp_parser_objc_selector (cp_parser* parser)
16941 {
16942   cp_token *token = cp_lexer_consume_token (parser->lexer);
16943
16944   if (!cp_parser_objc_selector_p (token->type))
16945     {
16946       error ("invalid Objective-C++ selector name");
16947       return error_mark_node;
16948     }
16949
16950   /* C++ operator names are allowed to appear in ObjC selectors.  */
16951   switch (token->type)
16952     {
16953     case CPP_AND_AND: return get_identifier ("and");
16954     case CPP_AND_EQ: return get_identifier ("and_eq");
16955     case CPP_AND: return get_identifier ("bitand");
16956     case CPP_OR: return get_identifier ("bitor");
16957     case CPP_COMPL: return get_identifier ("compl");
16958     case CPP_NOT: return get_identifier ("not");
16959     case CPP_NOT_EQ: return get_identifier ("not_eq");
16960     case CPP_OR_OR: return get_identifier ("or");
16961     case CPP_OR_EQ: return get_identifier ("or_eq");
16962     case CPP_XOR: return get_identifier ("xor");
16963     case CPP_XOR_EQ: return get_identifier ("xor_eq");
16964     default: return token->value;
16965     }
16966 }
16967
16968 /* Parse an Objective-C params list.  */
16969
16970 static tree
16971 cp_parser_objc_method_keyword_params (cp_parser* parser)
16972 {
16973   tree params = NULL_TREE;
16974   bool maybe_unary_selector_p = true;
16975   cp_token *token = cp_lexer_peek_token (parser->lexer);
16976
16977   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16978     {
16979       tree selector = NULL_TREE, typename, identifier;
16980
16981       if (token->type != CPP_COLON)
16982         selector = cp_parser_objc_selector (parser);
16983
16984       /* Detect if we have a unary selector.  */
16985       if (maybe_unary_selector_p
16986           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16987         return selector;
16988
16989       maybe_unary_selector_p = false;
16990       cp_parser_require (parser, CPP_COLON, "`:'");
16991       typename = cp_parser_objc_typename (parser);
16992       identifier = cp_parser_identifier (parser);
16993
16994       params
16995         = chainon (params,
16996                    objc_build_keyword_decl (selector,
16997                                             typename,
16998                                             identifier));
16999
17000       token = cp_lexer_peek_token (parser->lexer);
17001     }
17002
17003   return params;
17004 }
17005
17006 /* Parse the non-keyword Objective-C params.  */
17007
17008 static tree
17009 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17010 {
17011   tree params = make_node (TREE_LIST);
17012   cp_token *token = cp_lexer_peek_token (parser->lexer);
17013   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
17014
17015   while (token->type == CPP_COMMA)
17016     {
17017       cp_parameter_declarator *parmdecl;
17018       tree parm;
17019
17020       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17021       token = cp_lexer_peek_token (parser->lexer);
17022
17023       if (token->type == CPP_ELLIPSIS)
17024         {
17025           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
17026           *ellipsisp = true;
17027           break;
17028         }
17029
17030       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17031       parm = grokdeclarator (parmdecl->declarator,
17032                              &parmdecl->decl_specifiers,
17033                              PARM, /*initialized=*/0,
17034                              /*attrlist=*/NULL);
17035
17036       chainon (params, build_tree_list (NULL_TREE, parm));
17037       token = cp_lexer_peek_token (parser->lexer);
17038     }
17039
17040   return params;
17041 }
17042
17043 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
17044
17045 static void
17046 cp_parser_objc_interstitial_code (cp_parser* parser)
17047 {
17048   cp_token *token = cp_lexer_peek_token (parser->lexer);
17049
17050   /* If the next token is `extern' and the following token is a string
17051      literal, then we have a linkage specification.  */
17052   if (token->keyword == RID_EXTERN
17053       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17054     cp_parser_linkage_specification (parser);
17055   /* Handle #pragma, if any.  */
17056   else if (token->type == CPP_PRAGMA)
17057     cp_parser_pragma (parser, pragma_external);
17058   /* Allow stray semicolons.  */
17059   else if (token->type == CPP_SEMICOLON)
17060     cp_lexer_consume_token (parser->lexer);
17061   /* Finally, try to parse a block-declaration, or a function-definition.  */
17062   else
17063     cp_parser_block_declaration (parser, /*statement_p=*/false);
17064 }
17065
17066 /* Parse a method signature.  */
17067
17068 static tree
17069 cp_parser_objc_method_signature (cp_parser* parser)
17070 {
17071   tree rettype, kwdparms, optparms;
17072   bool ellipsis = false;
17073
17074   cp_parser_objc_method_type (parser);
17075   rettype = cp_parser_objc_typename (parser);
17076   kwdparms = cp_parser_objc_method_keyword_params (parser);
17077   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17078
17079   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17080 }
17081
17082 /* Pars an Objective-C method prototype list.  */
17083
17084 static void
17085 cp_parser_objc_method_prototype_list (cp_parser* parser)
17086 {
17087   cp_token *token = cp_lexer_peek_token (parser->lexer);
17088
17089   while (token->keyword != RID_AT_END)
17090     {
17091       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17092         {
17093           objc_add_method_declaration
17094            (cp_parser_objc_method_signature (parser));
17095           cp_parser_consume_semicolon_at_end_of_statement (parser);
17096         }
17097       else
17098         /* Allow for interspersed non-ObjC++ code.  */
17099         cp_parser_objc_interstitial_code (parser);
17100
17101       token = cp_lexer_peek_token (parser->lexer);
17102     }
17103
17104   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17105   objc_finish_interface ();
17106 }
17107
17108 /* Parse an Objective-C method definition list.  */
17109
17110 static void
17111 cp_parser_objc_method_definition_list (cp_parser* parser)
17112 {
17113   cp_token *token = cp_lexer_peek_token (parser->lexer);
17114
17115   while (token->keyword != RID_AT_END)
17116     {
17117       tree meth;
17118
17119       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17120         {
17121           push_deferring_access_checks (dk_deferred);
17122           objc_start_method_definition
17123            (cp_parser_objc_method_signature (parser));
17124
17125           /* For historical reasons, we accept an optional semicolon.  */
17126           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17127             cp_lexer_consume_token (parser->lexer);
17128
17129           perform_deferred_access_checks ();
17130           stop_deferring_access_checks ();
17131           meth = cp_parser_function_definition_after_declarator (parser,
17132                                                                  false);
17133           pop_deferring_access_checks ();
17134           objc_finish_method_definition (meth);
17135         }
17136       else
17137         /* Allow for interspersed non-ObjC++ code.  */
17138         cp_parser_objc_interstitial_code (parser);
17139
17140       token = cp_lexer_peek_token (parser->lexer);
17141     }
17142
17143   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17144   objc_finish_implementation ();
17145 }
17146
17147 /* Parse Objective-C ivars.  */
17148
17149 static void
17150 cp_parser_objc_class_ivars (cp_parser* parser)
17151 {
17152   cp_token *token = cp_lexer_peek_token (parser->lexer);
17153
17154   if (token->type != CPP_OPEN_BRACE)
17155     return;     /* No ivars specified.  */
17156
17157   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
17158   token = cp_lexer_peek_token (parser->lexer);
17159
17160   while (token->type != CPP_CLOSE_BRACE)
17161     {
17162       cp_decl_specifier_seq declspecs;
17163       int decl_class_or_enum_p;
17164       tree prefix_attributes;
17165
17166       cp_parser_objc_visibility_spec (parser);
17167
17168       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17169         break;
17170
17171       cp_parser_decl_specifier_seq (parser,
17172                                     CP_PARSER_FLAGS_OPTIONAL,
17173                                     &declspecs,
17174                                     &decl_class_or_enum_p);
17175       prefix_attributes = declspecs.attributes;
17176       declspecs.attributes = NULL_TREE;
17177
17178       /* Keep going until we hit the `;' at the end of the
17179          declaration.  */
17180       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17181         {
17182           tree width = NULL_TREE, attributes, first_attribute, decl;
17183           cp_declarator *declarator = NULL;
17184           int ctor_dtor_or_conv_p;
17185
17186           /* Check for a (possibly unnamed) bitfield declaration.  */
17187           token = cp_lexer_peek_token (parser->lexer);
17188           if (token->type == CPP_COLON)
17189             goto eat_colon;
17190
17191           if (token->type == CPP_NAME
17192               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17193                   == CPP_COLON))
17194             {
17195               /* Get the name of the bitfield.  */
17196               declarator = make_id_declarator (NULL_TREE,
17197                                                cp_parser_identifier (parser),
17198                                                sfk_none);
17199
17200              eat_colon:
17201               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17202               /* Get the width of the bitfield.  */
17203               width
17204                 = cp_parser_constant_expression (parser,
17205                                                  /*allow_non_constant=*/false,
17206                                                  NULL);
17207             }
17208           else
17209             {
17210               /* Parse the declarator.  */
17211               declarator
17212                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17213                                         &ctor_dtor_or_conv_p,
17214                                         /*parenthesized_p=*/NULL,
17215                                         /*member_p=*/false);
17216             }
17217
17218           /* Look for attributes that apply to the ivar.  */
17219           attributes = cp_parser_attributes_opt (parser);
17220           /* Remember which attributes are prefix attributes and
17221              which are not.  */
17222           first_attribute = attributes;
17223           /* Combine the attributes.  */
17224           attributes = chainon (prefix_attributes, attributes);
17225
17226           if (width)
17227             {
17228               /* Create the bitfield declaration.  */
17229               decl = grokbitfield (declarator, &declspecs, width);
17230               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17231             }
17232           else
17233             decl = grokfield (declarator, &declspecs, NULL_TREE,
17234                               NULL_TREE, attributes);
17235
17236           /* Add the instance variable.  */
17237           objc_add_instance_variable (decl);
17238
17239           /* Reset PREFIX_ATTRIBUTES.  */
17240           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17241             attributes = TREE_CHAIN (attributes);
17242           if (attributes)
17243             TREE_CHAIN (attributes) = NULL_TREE;
17244
17245           token = cp_lexer_peek_token (parser->lexer);
17246
17247           if (token->type == CPP_COMMA)
17248             {
17249               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17250               continue;
17251             }
17252           break;
17253         }
17254
17255       cp_parser_consume_semicolon_at_end_of_statement (parser);
17256       token = cp_lexer_peek_token (parser->lexer);
17257     }
17258
17259   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17260   /* For historical reasons, we accept an optional semicolon.  */
17261   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17262     cp_lexer_consume_token (parser->lexer);
17263 }
17264
17265 /* Parse an Objective-C protocol declaration.  */
17266
17267 static void
17268 cp_parser_objc_protocol_declaration (cp_parser* parser)
17269 {
17270   tree proto, protorefs;
17271   cp_token *tok;
17272
17273   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17274   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17275     {
17276       error ("identifier expected after %<@protocol%>");
17277       goto finish;
17278     }
17279
17280   /* See if we have a forward declaration or a definition.  */
17281   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17282
17283   /* Try a forward declaration first.  */
17284   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17285     {
17286       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17287      finish:
17288       cp_parser_consume_semicolon_at_end_of_statement (parser);
17289     }
17290
17291   /* Ok, we got a full-fledged definition (or at least should).  */
17292   else
17293     {
17294       proto = cp_parser_identifier (parser);
17295       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17296       objc_start_protocol (proto, protorefs);
17297       cp_parser_objc_method_prototype_list (parser);
17298     }
17299 }
17300
17301 /* Parse an Objective-C superclass or category.  */
17302
17303 static void
17304 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17305                                                           tree *categ)
17306 {
17307   cp_token *next = cp_lexer_peek_token (parser->lexer);
17308
17309   *super = *categ = NULL_TREE;
17310   if (next->type == CPP_COLON)
17311     {
17312       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17313       *super = cp_parser_identifier (parser);
17314     }
17315   else if (next->type == CPP_OPEN_PAREN)
17316     {
17317       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17318       *categ = cp_parser_identifier (parser);
17319       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17320     }
17321 }
17322
17323 /* Parse an Objective-C class interface.  */
17324
17325 static void
17326 cp_parser_objc_class_interface (cp_parser* parser)
17327 {
17328   tree name, super, categ, protos;
17329
17330   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17331   name = cp_parser_identifier (parser);
17332   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17333   protos = cp_parser_objc_protocol_refs_opt (parser);
17334
17335   /* We have either a class or a category on our hands.  */
17336   if (categ)
17337     objc_start_category_interface (name, categ, protos);
17338   else
17339     {
17340       objc_start_class_interface (name, super, protos);
17341       /* Handle instance variable declarations, if any.  */
17342       cp_parser_objc_class_ivars (parser);
17343       objc_continue_interface ();
17344     }
17345
17346   cp_parser_objc_method_prototype_list (parser);
17347 }
17348
17349 /* Parse an Objective-C class implementation.  */
17350
17351 static void
17352 cp_parser_objc_class_implementation (cp_parser* parser)
17353 {
17354   tree name, super, categ;
17355
17356   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17357   name = cp_parser_identifier (parser);
17358   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17359
17360   /* We have either a class or a category on our hands.  */
17361   if (categ)
17362     objc_start_category_implementation (name, categ);
17363   else
17364     {
17365       objc_start_class_implementation (name, super);
17366       /* Handle instance variable declarations, if any.  */
17367       cp_parser_objc_class_ivars (parser);
17368       objc_continue_implementation ();
17369     }
17370
17371   cp_parser_objc_method_definition_list (parser);
17372 }
17373
17374 /* Consume the @end token and finish off the implementation.  */
17375
17376 static void
17377 cp_parser_objc_end_implementation (cp_parser* parser)
17378 {
17379   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17380   objc_finish_implementation ();
17381 }
17382
17383 /* Parse an Objective-C declaration.  */
17384
17385 static void
17386 cp_parser_objc_declaration (cp_parser* parser)
17387 {
17388   /* Try to figure out what kind of declaration is present.  */
17389   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17390
17391   switch (kwd->keyword)
17392     {
17393     case RID_AT_ALIAS:
17394       cp_parser_objc_alias_declaration (parser);
17395       break;
17396     case RID_AT_CLASS:
17397       cp_parser_objc_class_declaration (parser);
17398       break;
17399     case RID_AT_PROTOCOL:
17400       cp_parser_objc_protocol_declaration (parser);
17401       break;
17402     case RID_AT_INTERFACE:
17403       cp_parser_objc_class_interface (parser);
17404       break;
17405     case RID_AT_IMPLEMENTATION:
17406       cp_parser_objc_class_implementation (parser);
17407       break;
17408     case RID_AT_END:
17409       cp_parser_objc_end_implementation (parser);
17410       break;
17411     default:
17412       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17413       cp_parser_skip_to_end_of_block_or_statement (parser);
17414     }
17415 }
17416
17417 /* Parse an Objective-C try-catch-finally statement.
17418
17419    objc-try-catch-finally-stmt:
17420      @try compound-statement objc-catch-clause-seq [opt]
17421        objc-finally-clause [opt]
17422
17423    objc-catch-clause-seq:
17424      objc-catch-clause objc-catch-clause-seq [opt]
17425
17426    objc-catch-clause:
17427      @catch ( exception-declaration ) compound-statement
17428
17429    objc-finally-clause
17430      @finally compound-statement
17431
17432    Returns NULL_TREE.  */
17433
17434 static tree
17435 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17436   location_t location;
17437   tree stmt;
17438
17439   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17440   location = cp_lexer_peek_token (parser->lexer)->location;
17441   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17442      node, lest it get absorbed into the surrounding block.  */
17443   stmt = push_stmt_list ();
17444   cp_parser_compound_statement (parser, NULL, false);
17445   objc_begin_try_stmt (location, pop_stmt_list (stmt));
17446
17447   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17448     {
17449       cp_parameter_declarator *parmdecl;
17450       tree parm;
17451
17452       cp_lexer_consume_token (parser->lexer);
17453       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17454       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17455       parm = grokdeclarator (parmdecl->declarator,
17456                              &parmdecl->decl_specifiers,
17457                              PARM, /*initialized=*/0,
17458                              /*attrlist=*/NULL);
17459       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17460       objc_begin_catch_clause (parm);
17461       cp_parser_compound_statement (parser, NULL, false);
17462       objc_finish_catch_clause ();
17463     }
17464
17465   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17466     {
17467       cp_lexer_consume_token (parser->lexer);
17468       location = cp_lexer_peek_token (parser->lexer)->location;
17469       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17470          node, lest it get absorbed into the surrounding block.  */
17471       stmt = push_stmt_list ();
17472       cp_parser_compound_statement (parser, NULL, false);
17473       objc_build_finally_clause (location, pop_stmt_list (stmt));
17474     }
17475
17476   return objc_finish_try_stmt ();
17477 }
17478
17479 /* Parse an Objective-C synchronized statement.
17480
17481    objc-synchronized-stmt:
17482      @synchronized ( expression ) compound-statement
17483
17484    Returns NULL_TREE.  */
17485
17486 static tree
17487 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17488   location_t location;
17489   tree lock, stmt;
17490
17491   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17492
17493   location = cp_lexer_peek_token (parser->lexer)->location;
17494   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17495   lock = cp_parser_expression (parser, false);
17496   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17497
17498   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17499      node, lest it get absorbed into the surrounding block.  */
17500   stmt = push_stmt_list ();
17501   cp_parser_compound_statement (parser, NULL, false);
17502
17503   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17504 }
17505
17506 /* Parse an Objective-C throw statement.
17507
17508    objc-throw-stmt:
17509      @throw assignment-expression [opt] ;
17510
17511    Returns a constructed '@throw' statement.  */
17512
17513 static tree
17514 cp_parser_objc_throw_statement (cp_parser *parser) {
17515   tree expr = NULL_TREE;
17516
17517   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17518
17519   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17520     expr = cp_parser_assignment_expression (parser, false);
17521
17522   cp_parser_consume_semicolon_at_end_of_statement (parser);
17523
17524   return objc_build_throw_stmt (expr);
17525 }
17526
17527 /* Parse an Objective-C statement.  */
17528
17529 static tree
17530 cp_parser_objc_statement (cp_parser * parser) {
17531   /* Try to figure out what kind of declaration is present.  */
17532   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17533
17534   switch (kwd->keyword)
17535     {
17536     case RID_AT_TRY:
17537       return cp_parser_objc_try_catch_finally_statement (parser);
17538     case RID_AT_SYNCHRONIZED:
17539       return cp_parser_objc_synchronized_statement (parser);
17540     case RID_AT_THROW:
17541       return cp_parser_objc_throw_statement (parser);
17542     default:
17543       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17544       cp_parser_skip_to_end_of_block_or_statement (parser);
17545     }
17546
17547   return error_mark_node;
17548 }
17549 /* The parser.  */
17550
17551 static GTY (()) cp_parser *the_parser;
17552
17553 \f
17554 /* Special handling for the first token or line in the file.  The first
17555    thing in the file might be #pragma GCC pch_preprocess, which loads a
17556    PCH file, which is a GC collection point.  So we need to handle this
17557    first pragma without benefit of an existing lexer structure.
17558
17559    Always returns one token to the caller in *FIRST_TOKEN.  This is 
17560    either the true first token of the file, or the first token after
17561    the initial pragma.  */
17562
17563 static void
17564 cp_parser_initial_pragma (cp_token *first_token)
17565 {
17566   tree name = NULL;
17567
17568   cp_lexer_get_preprocessor_token (NULL, first_token);
17569   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
17570     return;
17571
17572   cp_lexer_get_preprocessor_token (NULL, first_token);
17573   if (first_token->type == CPP_STRING)
17574     {
17575       name = first_token->value;
17576
17577       cp_lexer_get_preprocessor_token (NULL, first_token);
17578       if (first_token->type != CPP_PRAGMA_EOL)
17579         error ("junk at end of %<#pragma GCC pch_preprocess%>");
17580     }
17581   else
17582     error ("expected string literal");
17583
17584   /* Skip to the end of the pragma.  */
17585   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
17586     cp_lexer_get_preprocessor_token (NULL, first_token);
17587
17588   /* Read one more token to return to our caller.  */
17589   cp_lexer_get_preprocessor_token (NULL, first_token);
17590
17591   /* Now actually load the PCH file.  */
17592   if (name)
17593     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
17594 }
17595
17596 /* Normal parsing of a pragma token.  Here we can (and must) use the
17597    regular lexer.  */
17598
17599 static bool
17600 cp_parser_pragma (cp_parser *parser, enum pragma_context context ATTRIBUTE_UNUSED)
17601 {
17602   cp_token *pragma_tok;
17603   unsigned int id;
17604
17605   pragma_tok = cp_lexer_consume_token (parser->lexer);
17606   gcc_assert (pragma_tok->type == CPP_PRAGMA);
17607   parser->lexer->in_pragma = true;
17608
17609   id = pragma_tok->pragma_kind;
17610   switch (id)
17611     {
17612     case PRAGMA_GCC_PCH_PREPROCESS:
17613       error ("%<#pragma GCC pch_preprocess%> must be first");
17614       break;
17615
17616     default:
17617       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
17618       c_invoke_pragma_handler (id);
17619       break;
17620     }
17621
17622   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
17623   return false;
17624 }
17625
17626 /* The interface the pragma parsers have to the lexer.  */
17627
17628 enum cpp_ttype
17629 pragma_lex (tree *value)
17630 {
17631   cp_token *tok;
17632   enum cpp_ttype ret;
17633
17634   tok = cp_lexer_peek_token (the_parser->lexer);
17635
17636   ret = tok->type;
17637   *value = tok->value;
17638
17639   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
17640     ret = CPP_EOF;
17641   else if (ret == CPP_STRING)
17642     *value = cp_parser_string_literal (the_parser, false, false);
17643   else
17644     {
17645       cp_lexer_consume_token (the_parser->lexer);
17646       if (ret == CPP_KEYWORD)
17647         ret = CPP_NAME;
17648     }
17649
17650   return ret;
17651 }
17652
17653 \f
17654 /* External interface.  */
17655
17656 /* Parse one entire translation unit.  */
17657
17658 void
17659 c_parse_file (void)
17660 {
17661   bool error_occurred;
17662   static bool already_called = false;
17663
17664   if (already_called)
17665     {
17666       sorry ("inter-module optimizations not implemented for C++");
17667       return;
17668     }
17669   already_called = true;
17670
17671   the_parser = cp_parser_new ();
17672   push_deferring_access_checks (flag_access_control
17673                                 ? dk_no_deferred : dk_no_check);
17674   error_occurred = cp_parser_translation_unit (the_parser);
17675   the_parser = NULL;
17676 }
17677
17678 /* This variable must be provided by every front end.  */
17679
17680 int yydebug;
17681
17682 #include "gt-cp-parser.h"