OSDN Git Service

PR c++/28559
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GCC; see the file COPYING.  If not, write to the Free
20    Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
41
42 \f
43 /* The lexer.  */
44
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46    and c-lex.c) and the C++ parser.  */
47
48 /* A C++ token.  */
49
50 typedef struct cp_token GTY (())
51 {
52   /* The kind of token.  */
53   ENUM_BITFIELD (cpp_ttype) type : 8;
54   /* If this token is a keyword, this value indicates which keyword.
55      Otherwise, this value is RID_MAX.  */
56   ENUM_BITFIELD (rid) keyword : 8;
57   /* Token flags.  */
58   unsigned char flags;
59   /* Identifier for the pragma.  */
60   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
61   /* True if this token is from a system header.  */
62   BOOL_BITFIELD in_system_header : 1;
63   /* True if this token is from a context where it is implicitly extern "C" */
64   BOOL_BITFIELD implicit_extern_c : 1;
65   /* True for a CPP_NAME token that is not a keyword (i.e., for which
66      KEYWORD is RID_MAX) iff this name was looked up and found to be
67      ambiguous.  An error has already been reported.  */
68   BOOL_BITFIELD ambiguous_p : 1;
69   /* The value associated with this token, if any.  */
70   tree value;
71   /* The location at which this token was found.  */
72   location_t location;
73 } cp_token;
74
75 /* We use a stack of token pointer for saving token sets.  */
76 typedef struct cp_token *cp_token_position;
77 DEF_VEC_P (cp_token_position);
78 DEF_VEC_ALLOC_P (cp_token_position,heap);
79
80 static const cp_token eof_token =
81 {
82   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, NULL_TREE,
83 #if USE_MAPPED_LOCATION
84   0
85 #else
86   {0, 0}
87 #endif
88 };
89
90 /* The cp_lexer structure represents the C++ lexer.  It is responsible
91    for managing the token stream from the preprocessor and supplying
92    it to the parser.  Tokens are never added to the cp_lexer after
93    it is created.  */
94
95 typedef struct cp_lexer GTY (())
96 {
97   /* The memory allocated for the buffer.  NULL if this lexer does not
98      own the token buffer.  */
99   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
100   /* If the lexer owns the buffer, this is the number of tokens in the
101      buffer.  */
102   size_t buffer_length;
103
104   /* A pointer just past the last available token.  The tokens
105      in this lexer are [buffer, last_token).  */
106   cp_token_position GTY ((skip)) last_token;
107
108   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
109      no more available tokens.  */
110   cp_token_position GTY ((skip)) next_token;
111
112   /* A stack indicating positions at which cp_lexer_save_tokens was
113      called.  The top entry is the most recent position at which we
114      began saving tokens.  If the stack is non-empty, we are saving
115      tokens.  */
116   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
117
118   /* The next lexer in a linked list of lexers.  */
119   struct cp_lexer *next;
120
121   /* True if we should output debugging information.  */
122   bool debugging_p;
123
124   /* True if we're in the context of parsing a pragma, and should not
125      increment past the end-of-line marker.  */
126   bool in_pragma;
127 } cp_lexer;
128
129 /* cp_token_cache is a range of tokens.  There is no need to represent
130    allocate heap memory for it, since tokens are never removed from the
131    lexer's array.  There is also no need for the GC to walk through
132    a cp_token_cache, since everything in here is referenced through
133    a lexer.  */
134
135 typedef struct cp_token_cache GTY(())
136 {
137   /* The beginning of the token range.  */
138   cp_token * GTY((skip)) first;
139
140   /* Points immediately after the last token in the range.  */
141   cp_token * GTY ((skip)) last;
142 } cp_token_cache;
143
144 /* Prototypes.  */
145
146 static cp_lexer *cp_lexer_new_main
147   (void);
148 static cp_lexer *cp_lexer_new_from_tokens
149   (cp_token_cache *tokens);
150 static void cp_lexer_destroy
151   (cp_lexer *);
152 static int cp_lexer_saving_tokens
153   (const cp_lexer *);
154 static cp_token_position cp_lexer_token_position
155   (cp_lexer *, bool);
156 static cp_token *cp_lexer_token_at
157   (cp_lexer *, cp_token_position);
158 static void cp_lexer_get_preprocessor_token
159   (cp_lexer *, cp_token *);
160 static inline cp_token *cp_lexer_peek_token
161   (cp_lexer *);
162 static cp_token *cp_lexer_peek_nth_token
163   (cp_lexer *, size_t);
164 static inline bool cp_lexer_next_token_is
165   (cp_lexer *, enum cpp_ttype);
166 static bool cp_lexer_next_token_is_not
167   (cp_lexer *, enum cpp_ttype);
168 static bool cp_lexer_next_token_is_keyword
169   (cp_lexer *, enum rid);
170 static cp_token *cp_lexer_consume_token
171   (cp_lexer *);
172 static void cp_lexer_purge_token
173   (cp_lexer *);
174 static void cp_lexer_purge_tokens_after
175   (cp_lexer *, cp_token_position);
176 static void cp_lexer_save_tokens
177   (cp_lexer *);
178 static void cp_lexer_commit_tokens
179   (cp_lexer *);
180 static void cp_lexer_rollback_tokens
181   (cp_lexer *);
182 #ifdef ENABLE_CHECKING
183 static void cp_lexer_print_token
184   (FILE *, cp_token *);
185 static inline bool cp_lexer_debugging_p
186   (cp_lexer *);
187 static void cp_lexer_start_debugging
188   (cp_lexer *) ATTRIBUTE_UNUSED;
189 static void cp_lexer_stop_debugging
190   (cp_lexer *) ATTRIBUTE_UNUSED;
191 #else
192 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
193    about passing NULL to functions that require non-NULL arguments
194    (fputs, fprintf).  It will never be used, so all we need is a value
195    of the right type that's guaranteed not to be NULL.  */
196 #define cp_lexer_debug_stream stdout
197 #define cp_lexer_print_token(str, tok) (void) 0
198 #define cp_lexer_debugging_p(lexer) 0
199 #endif /* ENABLE_CHECKING */
200
201 static cp_token_cache *cp_token_cache_new
202   (cp_token *, cp_token *);
203
204 static void cp_parser_initial_pragma
205   (cp_token *);
206
207 /* Manifest constants.  */
208 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
209 #define CP_SAVED_TOKEN_STACK 5
210
211 /* A token type for keywords, as opposed to ordinary identifiers.  */
212 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
213
214 /* A token type for template-ids.  If a template-id is processed while
215    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
216    the value of the CPP_TEMPLATE_ID is whatever was returned by
217    cp_parser_template_id.  */
218 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
219
220 /* A token type for nested-name-specifiers.  If a
221    nested-name-specifier is processed while parsing tentatively, it is
222    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
223    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
224    cp_parser_nested_name_specifier_opt.  */
225 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
226
227 /* A token type for tokens that are not tokens at all; these are used
228    to represent slots in the array where there used to be a token
229    that has now been deleted.  */
230 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
231
232 /* The number of token types, including C++-specific ones.  */
233 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
234
235 /* Variables.  */
236
237 #ifdef ENABLE_CHECKING
238 /* The stream to which debugging output should be written.  */
239 static FILE *cp_lexer_debug_stream;
240 #endif /* ENABLE_CHECKING */
241
242 /* Create a new main C++ lexer, the lexer that gets tokens from the
243    preprocessor.  */
244
245 static cp_lexer *
246 cp_lexer_new_main (void)
247 {
248   cp_token first_token;
249   cp_lexer *lexer;
250   cp_token *pos;
251   size_t alloc;
252   size_t space;
253   cp_token *buffer;
254
255   /* It's possible that parsing the first pragma will load a PCH file,
256      which is a GC collection point.  So we have to do that before
257      allocating any memory.  */
258   cp_parser_initial_pragma (&first_token);
259
260   /* Tell c_lex_with_flags not to merge string constants.  */
261   c_lex_return_raw_strings = true;
262
263   c_common_no_more_pch ();
264
265   /* Allocate the memory.  */
266   lexer = GGC_CNEW (cp_lexer);
267
268 #ifdef ENABLE_CHECKING
269   /* Initially we are not debugging.  */
270   lexer->debugging_p = false;
271 #endif /* ENABLE_CHECKING */
272   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
273                                    CP_SAVED_TOKEN_STACK);
274
275   /* Create the buffer.  */
276   alloc = CP_LEXER_BUFFER_SIZE;
277   buffer = GGC_NEWVEC (cp_token, alloc);
278
279   /* Put the first token in the buffer.  */
280   space = alloc;
281   pos = buffer;
282   *pos = first_token;
283
284   /* Get the remaining tokens from the preprocessor.  */
285   while (pos->type != CPP_EOF)
286     {
287       pos++;
288       if (!--space)
289         {
290           space = alloc;
291           alloc *= 2;
292           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
293           pos = buffer + space;
294         }
295       cp_lexer_get_preprocessor_token (lexer, pos);
296     }
297   lexer->buffer = buffer;
298   lexer->buffer_length = alloc - space;
299   lexer->last_token = pos;
300   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
301
302   /* Subsequent preprocessor diagnostics should use compiler
303      diagnostic functions to get the compiler source location.  */
304   cpp_get_options (parse_in)->client_diagnostic = true;
305   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
306
307   gcc_assert (lexer->next_token->type != CPP_PURGED);
308   return lexer;
309 }
310
311 /* Create a new lexer whose token stream is primed with the tokens in
312    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
313
314 static cp_lexer *
315 cp_lexer_new_from_tokens (cp_token_cache *cache)
316 {
317   cp_token *first = cache->first;
318   cp_token *last = cache->last;
319   cp_lexer *lexer = GGC_CNEW (cp_lexer);
320
321   /* We do not own the buffer.  */
322   lexer->buffer = NULL;
323   lexer->buffer_length = 0;
324   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
325   lexer->last_token = last;
326
327   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
328                                    CP_SAVED_TOKEN_STACK);
329
330 #ifdef ENABLE_CHECKING
331   /* Initially we are not debugging.  */
332   lexer->debugging_p = false;
333 #endif
334
335   gcc_assert (lexer->next_token->type != CPP_PURGED);
336   return lexer;
337 }
338
339 /* Frees all resources associated with LEXER.  */
340
341 static void
342 cp_lexer_destroy (cp_lexer *lexer)
343 {
344   if (lexer->buffer)
345     ggc_free (lexer->buffer);
346   VEC_free (cp_token_position, heap, lexer->saved_tokens);
347   ggc_free (lexer);
348 }
349
350 /* Returns nonzero if debugging information should be output.  */
351
352 #ifdef ENABLE_CHECKING
353
354 static inline bool
355 cp_lexer_debugging_p (cp_lexer *lexer)
356 {
357   return lexer->debugging_p;
358 }
359
360 #endif /* ENABLE_CHECKING */
361
362 static inline cp_token_position
363 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
364 {
365   gcc_assert (!previous_p || lexer->next_token != &eof_token);
366
367   return lexer->next_token - previous_p;
368 }
369
370 static inline cp_token *
371 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
372 {
373   return pos;
374 }
375
376 /* nonzero if we are presently saving tokens.  */
377
378 static inline int
379 cp_lexer_saving_tokens (const cp_lexer* lexer)
380 {
381   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
382 }
383
384 /* Store the next token from the preprocessor in *TOKEN.  Return true
385    if we reach EOF.  */
386
387 static void
388 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
389                                  cp_token *token)
390 {
391   static int is_extern_c = 0;
392
393    /* Get a new token from the preprocessor.  */
394   token->type
395     = c_lex_with_flags (&token->value, &token->location, &token->flags);
396   token->keyword = RID_MAX;
397   token->pragma_kind = PRAGMA_NONE;
398   token->in_system_header = in_system_header;
399
400   /* On some systems, some header files are surrounded by an
401      implicit extern "C" block.  Set a flag in the token if it
402      comes from such a header.  */
403   is_extern_c += pending_lang_change;
404   pending_lang_change = 0;
405   token->implicit_extern_c = is_extern_c > 0;
406
407   /* Check to see if this token is a keyword.  */
408   if (token->type == CPP_NAME)
409     {
410       if (C_IS_RESERVED_WORD (token->value))
411         {
412           /* Mark this token as a keyword.  */
413           token->type = CPP_KEYWORD;
414           /* Record which keyword.  */
415           token->keyword = C_RID_CODE (token->value);
416           /* Update the value.  Some keywords are mapped to particular
417              entities, rather than simply having the value of the
418              corresponding IDENTIFIER_NODE.  For example, `__const' is
419              mapped to `const'.  */
420           token->value = ridpointers[token->keyword];
421         }
422       else
423         {
424           token->ambiguous_p = false;
425           token->keyword = RID_MAX;
426         }
427     }
428   /* Handle Objective-C++ keywords.  */
429   else if (token->type == CPP_AT_NAME)
430     {
431       token->type = CPP_KEYWORD;
432       switch (C_RID_CODE (token->value))
433         {
434         /* Map 'class' to '@class', 'private' to '@private', etc.  */
435         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
436         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
437         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
438         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
439         case RID_THROW: token->keyword = RID_AT_THROW; break;
440         case RID_TRY: token->keyword = RID_AT_TRY; break;
441         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
442         default: token->keyword = C_RID_CODE (token->value);
443         }
444     }
445   else if (token->type == CPP_PRAGMA)
446     {
447       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
448       token->pragma_kind = TREE_INT_CST_LOW (token->value);
449       token->value = NULL;
450     }
451 }
452
453 /* Update the globals input_location and in_system_header from TOKEN.  */
454 static inline void
455 cp_lexer_set_source_position_from_token (cp_token *token)
456 {
457   if (token->type != CPP_EOF)
458     {
459       input_location = token->location;
460       in_system_header = token->in_system_header;
461     }
462 }
463
464 /* Return a pointer to the next token in the token stream, but do not
465    consume it.  */
466
467 static inline cp_token *
468 cp_lexer_peek_token (cp_lexer *lexer)
469 {
470   if (cp_lexer_debugging_p (lexer))
471     {
472       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
473       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
474       putc ('\n', cp_lexer_debug_stream);
475     }
476   return lexer->next_token;
477 }
478
479 /* Return true if the next token has the indicated TYPE.  */
480
481 static inline bool
482 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
483 {
484   return cp_lexer_peek_token (lexer)->type == type;
485 }
486
487 /* Return true if the next token does not have the indicated TYPE.  */
488
489 static inline bool
490 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
491 {
492   return !cp_lexer_next_token_is (lexer, type);
493 }
494
495 /* Return true if the next token is the indicated KEYWORD.  */
496
497 static inline bool
498 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
499 {
500   return cp_lexer_peek_token (lexer)->keyword == keyword;
501 }
502
503 /* Return a pointer to the Nth token in the token stream.  If N is 1,
504    then this is precisely equivalent to cp_lexer_peek_token (except
505    that it is not inline).  One would like to disallow that case, but
506    there is one case (cp_parser_nth_token_starts_template_id) where
507    the caller passes a variable for N and it might be 1.  */
508
509 static cp_token *
510 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
511 {
512   cp_token *token;
513
514   /* N is 1-based, not zero-based.  */
515   gcc_assert (n > 0);
516
517   if (cp_lexer_debugging_p (lexer))
518     fprintf (cp_lexer_debug_stream,
519              "cp_lexer: peeking ahead %ld at token: ", (long)n);
520
521   --n;
522   token = lexer->next_token;
523   gcc_assert (!n || token != &eof_token);
524   while (n != 0)
525     {
526       ++token;
527       if (token == lexer->last_token)
528         {
529           token = (cp_token *)&eof_token;
530           break;
531         }
532
533       if (token->type != CPP_PURGED)
534         --n;
535     }
536
537   if (cp_lexer_debugging_p (lexer))
538     {
539       cp_lexer_print_token (cp_lexer_debug_stream, token);
540       putc ('\n', cp_lexer_debug_stream);
541     }
542
543   return token;
544 }
545
546 /* Return the next token, and advance the lexer's next_token pointer
547    to point to the next non-purged token.  */
548
549 static cp_token *
550 cp_lexer_consume_token (cp_lexer* lexer)
551 {
552   cp_token *token = lexer->next_token;
553
554   gcc_assert (token != &eof_token);
555   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
556
557   do
558     {
559       lexer->next_token++;
560       if (lexer->next_token == lexer->last_token)
561         {
562           lexer->next_token = (cp_token *)&eof_token;
563           break;
564         }
565
566     }
567   while (lexer->next_token->type == CPP_PURGED);
568
569   cp_lexer_set_source_position_from_token (token);
570
571   /* Provide debugging output.  */
572   if (cp_lexer_debugging_p (lexer))
573     {
574       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
575       cp_lexer_print_token (cp_lexer_debug_stream, token);
576       putc ('\n', cp_lexer_debug_stream);
577     }
578
579   return token;
580 }
581
582 /* Permanently remove the next token from the token stream, and
583    advance the next_token pointer to refer to the next non-purged
584    token.  */
585
586 static void
587 cp_lexer_purge_token (cp_lexer *lexer)
588 {
589   cp_token *tok = lexer->next_token;
590
591   gcc_assert (tok != &eof_token);
592   tok->type = CPP_PURGED;
593   tok->location = UNKNOWN_LOCATION;
594   tok->value = NULL_TREE;
595   tok->keyword = RID_MAX;
596
597   do
598     {
599       tok++;
600       if (tok == lexer->last_token)
601         {
602           tok = (cp_token *)&eof_token;
603           break;
604         }
605     }
606   while (tok->type == CPP_PURGED);
607   lexer->next_token = tok;
608 }
609
610 /* Permanently remove all tokens after TOK, up to, but not
611    including, the token that will be returned next by
612    cp_lexer_peek_token.  */
613
614 static void
615 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
616 {
617   cp_token *peek = lexer->next_token;
618
619   if (peek == &eof_token)
620     peek = lexer->last_token;
621
622   gcc_assert (tok < peek);
623
624   for ( tok += 1; tok != peek; tok += 1)
625     {
626       tok->type = CPP_PURGED;
627       tok->location = UNKNOWN_LOCATION;
628       tok->value = NULL_TREE;
629       tok->keyword = RID_MAX;
630     }
631 }
632
633 /* Begin saving tokens.  All tokens consumed after this point will be
634    preserved.  */
635
636 static void
637 cp_lexer_save_tokens (cp_lexer* lexer)
638 {
639   /* Provide debugging output.  */
640   if (cp_lexer_debugging_p (lexer))
641     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
642
643   VEC_safe_push (cp_token_position, heap,
644                  lexer->saved_tokens, lexer->next_token);
645 }
646
647 /* Commit to the portion of the token stream most recently saved.  */
648
649 static void
650 cp_lexer_commit_tokens (cp_lexer* lexer)
651 {
652   /* Provide debugging output.  */
653   if (cp_lexer_debugging_p (lexer))
654     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
655
656   VEC_pop (cp_token_position, lexer->saved_tokens);
657 }
658
659 /* Return all tokens saved since the last call to cp_lexer_save_tokens
660    to the token stream.  Stop saving tokens.  */
661
662 static void
663 cp_lexer_rollback_tokens (cp_lexer* lexer)
664 {
665   /* Provide debugging output.  */
666   if (cp_lexer_debugging_p (lexer))
667     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
668
669   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
670 }
671
672 /* Print a representation of the TOKEN on the STREAM.  */
673
674 #ifdef ENABLE_CHECKING
675
676 static void
677 cp_lexer_print_token (FILE * stream, cp_token *token)
678 {
679   /* We don't use cpp_type2name here because the parser defines
680      a few tokens of its own.  */
681   static const char *const token_names[] = {
682     /* cpplib-defined token types */
683 #define OP(e, s) #e,
684 #define TK(e, s) #e,
685     TTYPE_TABLE
686 #undef OP
687 #undef TK
688     /* C++ parser token types - see "Manifest constants", above.  */
689     "KEYWORD",
690     "TEMPLATE_ID",
691     "NESTED_NAME_SPECIFIER",
692     "PURGED"
693   };
694
695   /* If we have a name for the token, print it out.  Otherwise, we
696      simply give the numeric code.  */
697   gcc_assert (token->type < ARRAY_SIZE(token_names));
698   fputs (token_names[token->type], stream);
699
700   /* For some tokens, print the associated data.  */
701   switch (token->type)
702     {
703     case CPP_KEYWORD:
704       /* Some keywords have a value that is not an IDENTIFIER_NODE.
705          For example, `struct' is mapped to an INTEGER_CST.  */
706       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
707         break;
708       /* else fall through */
709     case CPP_NAME:
710       fputs (IDENTIFIER_POINTER (token->value), stream);
711       break;
712
713     case CPP_STRING:
714     case CPP_WSTRING:
715       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
716       break;
717
718     default:
719       break;
720     }
721 }
722
723 /* Start emitting debugging information.  */
724
725 static void
726 cp_lexer_start_debugging (cp_lexer* lexer)
727 {
728   lexer->debugging_p = true;
729 }
730
731 /* Stop emitting debugging information.  */
732
733 static void
734 cp_lexer_stop_debugging (cp_lexer* lexer)
735 {
736   lexer->debugging_p = false;
737 }
738
739 #endif /* ENABLE_CHECKING */
740
741 /* Create a new cp_token_cache, representing a range of tokens.  */
742
743 static cp_token_cache *
744 cp_token_cache_new (cp_token *first, cp_token *last)
745 {
746   cp_token_cache *cache = GGC_NEW (cp_token_cache);
747   cache->first = first;
748   cache->last = last;
749   return cache;
750 }
751
752 \f
753 /* Decl-specifiers.  */
754
755 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
756
757 static void
758 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
759 {
760   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
761 }
762
763 /* Declarators.  */
764
765 /* Nothing other than the parser should be creating declarators;
766    declarators are a semi-syntactic representation of C++ entities.
767    Other parts of the front end that need to create entities (like
768    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
769
770 static cp_declarator *make_call_declarator
771   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
772 static cp_declarator *make_array_declarator
773   (cp_declarator *, tree);
774 static cp_declarator *make_pointer_declarator
775   (cp_cv_quals, cp_declarator *);
776 static cp_declarator *make_reference_declarator
777   (cp_cv_quals, cp_declarator *);
778 static cp_parameter_declarator *make_parameter_declarator
779   (cp_decl_specifier_seq *, cp_declarator *, tree);
780 static cp_declarator *make_ptrmem_declarator
781   (cp_cv_quals, tree, cp_declarator *);
782
783 /* An erroneous declarator.  */
784 static cp_declarator *cp_error_declarator;
785
786 /* The obstack on which declarators and related data structures are
787    allocated.  */
788 static struct obstack declarator_obstack;
789
790 /* Alloc BYTES from the declarator memory pool.  */
791
792 static inline void *
793 alloc_declarator (size_t bytes)
794 {
795   return obstack_alloc (&declarator_obstack, bytes);
796 }
797
798 /* Allocate a declarator of the indicated KIND.  Clear fields that are
799    common to all declarators.  */
800
801 static cp_declarator *
802 make_declarator (cp_declarator_kind kind)
803 {
804   cp_declarator *declarator;
805
806   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
807   declarator->kind = kind;
808   declarator->attributes = NULL_TREE;
809   declarator->declarator = NULL;
810
811   return declarator;
812 }
813
814 /* Make a declarator for a generalized identifier.  If
815    QUALIFYING_SCOPE is non-NULL, the identifier is
816    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
817    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
818    is, if any.   */
819
820 static cp_declarator *
821 make_id_declarator (tree qualifying_scope, tree unqualified_name,
822                     special_function_kind sfk)
823 {
824   cp_declarator *declarator;
825
826   /* It is valid to write:
827
828        class C { void f(); };
829        typedef C D;
830        void D::f();
831
832      The standard is not clear about whether `typedef const C D' is
833      legal; as of 2002-09-15 the committee is considering that
834      question.  EDG 3.0 allows that syntax.  Therefore, we do as
835      well.  */
836   if (qualifying_scope && TYPE_P (qualifying_scope))
837     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
838
839   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
840               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
841               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
842
843   declarator = make_declarator (cdk_id);
844   declarator->u.id.qualifying_scope = qualifying_scope;
845   declarator->u.id.unqualified_name = unqualified_name;
846   declarator->u.id.sfk = sfk;
847
848   return declarator;
849 }
850
851 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
852    of modifiers such as const or volatile to apply to the pointer
853    type, represented as identifiers.  */
854
855 cp_declarator *
856 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
857 {
858   cp_declarator *declarator;
859
860   declarator = make_declarator (cdk_pointer);
861   declarator->declarator = target;
862   declarator->u.pointer.qualifiers = cv_qualifiers;
863   declarator->u.pointer.class_type = NULL_TREE;
864
865   return declarator;
866 }
867
868 /* Like make_pointer_declarator -- but for references.  */
869
870 cp_declarator *
871 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
872 {
873   cp_declarator *declarator;
874
875   declarator = make_declarator (cdk_reference);
876   declarator->declarator = target;
877   declarator->u.pointer.qualifiers = cv_qualifiers;
878   declarator->u.pointer.class_type = NULL_TREE;
879
880   return declarator;
881 }
882
883 /* Like make_pointer_declarator -- but for a pointer to a non-static
884    member of CLASS_TYPE.  */
885
886 cp_declarator *
887 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
888                         cp_declarator *pointee)
889 {
890   cp_declarator *declarator;
891
892   declarator = make_declarator (cdk_ptrmem);
893   declarator->declarator = pointee;
894   declarator->u.pointer.qualifiers = cv_qualifiers;
895   declarator->u.pointer.class_type = class_type;
896
897   return declarator;
898 }
899
900 /* Make a declarator for the function given by TARGET, with the
901    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
902    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
903    indicates what exceptions can be thrown.  */
904
905 cp_declarator *
906 make_call_declarator (cp_declarator *target,
907                       cp_parameter_declarator *parms,
908                       cp_cv_quals cv_qualifiers,
909                       tree exception_specification)
910 {
911   cp_declarator *declarator;
912
913   declarator = make_declarator (cdk_function);
914   declarator->declarator = target;
915   declarator->u.function.parameters = parms;
916   declarator->u.function.qualifiers = cv_qualifiers;
917   declarator->u.function.exception_specification = exception_specification;
918
919   return declarator;
920 }
921
922 /* Make a declarator for an array of BOUNDS elements, each of which is
923    defined by ELEMENT.  */
924
925 cp_declarator *
926 make_array_declarator (cp_declarator *element, tree bounds)
927 {
928   cp_declarator *declarator;
929
930   declarator = make_declarator (cdk_array);
931   declarator->declarator = element;
932   declarator->u.array.bounds = bounds;
933
934   return declarator;
935 }
936
937 cp_parameter_declarator *no_parameters;
938
939 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
940    DECLARATOR and DEFAULT_ARGUMENT.  */
941
942 cp_parameter_declarator *
943 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
944                            cp_declarator *declarator,
945                            tree default_argument)
946 {
947   cp_parameter_declarator *parameter;
948
949   parameter = ((cp_parameter_declarator *)
950                alloc_declarator (sizeof (cp_parameter_declarator)));
951   parameter->next = NULL;
952   if (decl_specifiers)
953     parameter->decl_specifiers = *decl_specifiers;
954   else
955     clear_decl_specs (&parameter->decl_specifiers);
956   parameter->declarator = declarator;
957   parameter->default_argument = default_argument;
958   parameter->ellipsis_p = false;
959
960   return parameter;
961 }
962
963 /* The parser.  */
964
965 /* Overview
966    --------
967
968    A cp_parser parses the token stream as specified by the C++
969    grammar.  Its job is purely parsing, not semantic analysis.  For
970    example, the parser breaks the token stream into declarators,
971    expressions, statements, and other similar syntactic constructs.
972    It does not check that the types of the expressions on either side
973    of an assignment-statement are compatible, or that a function is
974    not declared with a parameter of type `void'.
975
976    The parser invokes routines elsewhere in the compiler to perform
977    semantic analysis and to build up the abstract syntax tree for the
978    code processed.
979
980    The parser (and the template instantiation code, which is, in a
981    way, a close relative of parsing) are the only parts of the
982    compiler that should be calling push_scope and pop_scope, or
983    related functions.  The parser (and template instantiation code)
984    keeps track of what scope is presently active; everything else
985    should simply honor that.  (The code that generates static
986    initializers may also need to set the scope, in order to check
987    access control correctly when emitting the initializers.)
988
989    Methodology
990    -----------
991
992    The parser is of the standard recursive-descent variety.  Upcoming
993    tokens in the token stream are examined in order to determine which
994    production to use when parsing a non-terminal.  Some C++ constructs
995    require arbitrary look ahead to disambiguate.  For example, it is
996    impossible, in the general case, to tell whether a statement is an
997    expression or declaration without scanning the entire statement.
998    Therefore, the parser is capable of "parsing tentatively."  When the
999    parser is not sure what construct comes next, it enters this mode.
1000    Then, while we attempt to parse the construct, the parser queues up
1001    error messages, rather than issuing them immediately, and saves the
1002    tokens it consumes.  If the construct is parsed successfully, the
1003    parser "commits", i.e., it issues any queued error messages and
1004    the tokens that were being preserved are permanently discarded.
1005    If, however, the construct is not parsed successfully, the parser
1006    rolls back its state completely so that it can resume parsing using
1007    a different alternative.
1008
1009    Future Improvements
1010    -------------------
1011
1012    The performance of the parser could probably be improved substantially.
1013    We could often eliminate the need to parse tentatively by looking ahead
1014    a little bit.  In some places, this approach might not entirely eliminate
1015    the need to parse tentatively, but it might still speed up the average
1016    case.  */
1017
1018 /* Flags that are passed to some parsing functions.  These values can
1019    be bitwise-ored together.  */
1020
1021 typedef enum cp_parser_flags
1022 {
1023   /* No flags.  */
1024   CP_PARSER_FLAGS_NONE = 0x0,
1025   /* The construct is optional.  If it is not present, then no error
1026      should be issued.  */
1027   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1028   /* When parsing a type-specifier, do not allow user-defined types.  */
1029   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1030 } cp_parser_flags;
1031
1032 /* The different kinds of declarators we want to parse.  */
1033
1034 typedef enum cp_parser_declarator_kind
1035 {
1036   /* We want an abstract declarator.  */
1037   CP_PARSER_DECLARATOR_ABSTRACT,
1038   /* We want a named declarator.  */
1039   CP_PARSER_DECLARATOR_NAMED,
1040   /* We don't mind, but the name must be an unqualified-id.  */
1041   CP_PARSER_DECLARATOR_EITHER
1042 } cp_parser_declarator_kind;
1043
1044 /* The precedence values used to parse binary expressions.  The minimum value
1045    of PREC must be 1, because zero is reserved to quickly discriminate
1046    binary operators from other tokens.  */
1047
1048 enum cp_parser_prec
1049 {
1050   PREC_NOT_OPERATOR,
1051   PREC_LOGICAL_OR_EXPRESSION,
1052   PREC_LOGICAL_AND_EXPRESSION,
1053   PREC_INCLUSIVE_OR_EXPRESSION,
1054   PREC_EXCLUSIVE_OR_EXPRESSION,
1055   PREC_AND_EXPRESSION,
1056   PREC_EQUALITY_EXPRESSION,
1057   PREC_RELATIONAL_EXPRESSION,
1058   PREC_SHIFT_EXPRESSION,
1059   PREC_ADDITIVE_EXPRESSION,
1060   PREC_MULTIPLICATIVE_EXPRESSION,
1061   PREC_PM_EXPRESSION,
1062   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1063 };
1064
1065 /* A mapping from a token type to a corresponding tree node type, with a
1066    precedence value.  */
1067
1068 typedef struct cp_parser_binary_operations_map_node
1069 {
1070   /* The token type.  */
1071   enum cpp_ttype token_type;
1072   /* The corresponding tree code.  */
1073   enum tree_code tree_type;
1074   /* The precedence of this operator.  */
1075   enum cp_parser_prec prec;
1076 } cp_parser_binary_operations_map_node;
1077
1078 /* The status of a tentative parse.  */
1079
1080 typedef enum cp_parser_status_kind
1081 {
1082   /* No errors have occurred.  */
1083   CP_PARSER_STATUS_KIND_NO_ERROR,
1084   /* An error has occurred.  */
1085   CP_PARSER_STATUS_KIND_ERROR,
1086   /* We are committed to this tentative parse, whether or not an error
1087      has occurred.  */
1088   CP_PARSER_STATUS_KIND_COMMITTED
1089 } cp_parser_status_kind;
1090
1091 typedef struct cp_parser_expression_stack_entry
1092 {
1093   tree lhs;
1094   enum tree_code tree_type;
1095   int prec;
1096 } cp_parser_expression_stack_entry;
1097
1098 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1099    entries because precedence levels on the stack are monotonically
1100    increasing.  */
1101 typedef struct cp_parser_expression_stack_entry
1102   cp_parser_expression_stack[NUM_PREC_VALUES];
1103
1104 /* Context that is saved and restored when parsing tentatively.  */
1105 typedef struct cp_parser_context GTY (())
1106 {
1107   /* If this is a tentative parsing context, the status of the
1108      tentative parse.  */
1109   enum cp_parser_status_kind status;
1110   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1111      that are looked up in this context must be looked up both in the
1112      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1113      the context of the containing expression.  */
1114   tree object_type;
1115
1116   /* The next parsing context in the stack.  */
1117   struct cp_parser_context *next;
1118 } cp_parser_context;
1119
1120 /* Prototypes.  */
1121
1122 /* Constructors and destructors.  */
1123
1124 static cp_parser_context *cp_parser_context_new
1125   (cp_parser_context *);
1126
1127 /* Class variables.  */
1128
1129 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1130
1131 /* The operator-precedence table used by cp_parser_binary_expression.
1132    Transformed into an associative array (binops_by_token) by
1133    cp_parser_new.  */
1134
1135 static const cp_parser_binary_operations_map_node binops[] = {
1136   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1137   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1138
1139   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1140   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1141   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1142
1143   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1144   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1145
1146   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1147   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1148
1149   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1150   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1151   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1152   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1153   { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1154   { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1155
1156   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1157   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1158
1159   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1160
1161   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1162
1163   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1164
1165   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1166
1167   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1168 };
1169
1170 /* The same as binops, but initialized by cp_parser_new so that
1171    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1172    for speed.  */
1173 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1174
1175 /* Constructors and destructors.  */
1176
1177 /* Construct a new context.  The context below this one on the stack
1178    is given by NEXT.  */
1179
1180 static cp_parser_context *
1181 cp_parser_context_new (cp_parser_context* next)
1182 {
1183   cp_parser_context *context;
1184
1185   /* Allocate the storage.  */
1186   if (cp_parser_context_free_list != NULL)
1187     {
1188       /* Pull the first entry from the free list.  */
1189       context = cp_parser_context_free_list;
1190       cp_parser_context_free_list = context->next;
1191       memset (context, 0, sizeof (*context));
1192     }
1193   else
1194     context = GGC_CNEW (cp_parser_context);
1195
1196   /* No errors have occurred yet in this context.  */
1197   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1198   /* If this is not the bottomost context, copy information that we
1199      need from the previous context.  */
1200   if (next)
1201     {
1202       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1203          expression, then we are parsing one in this context, too.  */
1204       context->object_type = next->object_type;
1205       /* Thread the stack.  */
1206       context->next = next;
1207     }
1208
1209   return context;
1210 }
1211
1212 /* The cp_parser structure represents the C++ parser.  */
1213
1214 typedef struct cp_parser GTY(())
1215 {
1216   /* The lexer from which we are obtaining tokens.  */
1217   cp_lexer *lexer;
1218
1219   /* The scope in which names should be looked up.  If NULL_TREE, then
1220      we look up names in the scope that is currently open in the
1221      source program.  If non-NULL, this is either a TYPE or
1222      NAMESPACE_DECL for the scope in which we should look.  It can
1223      also be ERROR_MARK, when we've parsed a bogus scope.
1224
1225      This value is not cleared automatically after a name is looked
1226      up, so we must be careful to clear it before starting a new look
1227      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1228      will look up `Z' in the scope of `X', rather than the current
1229      scope.)  Unfortunately, it is difficult to tell when name lookup
1230      is complete, because we sometimes peek at a token, look it up,
1231      and then decide not to consume it.   */
1232   tree scope;
1233
1234   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1235      last lookup took place.  OBJECT_SCOPE is used if an expression
1236      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1237      respectively.  QUALIFYING_SCOPE is used for an expression of the
1238      form "X::Y"; it refers to X.  */
1239   tree object_scope;
1240   tree qualifying_scope;
1241
1242   /* A stack of parsing contexts.  All but the bottom entry on the
1243      stack will be tentative contexts.
1244
1245      We parse tentatively in order to determine which construct is in
1246      use in some situations.  For example, in order to determine
1247      whether a statement is an expression-statement or a
1248      declaration-statement we parse it tentatively as a
1249      declaration-statement.  If that fails, we then reparse the same
1250      token stream as an expression-statement.  */
1251   cp_parser_context *context;
1252
1253   /* True if we are parsing GNU C++.  If this flag is not set, then
1254      GNU extensions are not recognized.  */
1255   bool allow_gnu_extensions_p;
1256
1257   /* TRUE if the `>' token should be interpreted as the greater-than
1258      operator.  FALSE if it is the end of a template-id or
1259      template-parameter-list.  */
1260   bool greater_than_is_operator_p;
1261
1262   /* TRUE if default arguments are allowed within a parameter list
1263      that starts at this point. FALSE if only a gnu extension makes
1264      them permissible.  */
1265   bool default_arg_ok_p;
1266
1267   /* TRUE if we are parsing an integral constant-expression.  See
1268      [expr.const] for a precise definition.  */
1269   bool integral_constant_expression_p;
1270
1271   /* TRUE if we are parsing an integral constant-expression -- but a
1272      non-constant expression should be permitted as well.  This flag
1273      is used when parsing an array bound so that GNU variable-length
1274      arrays are tolerated.  */
1275   bool allow_non_integral_constant_expression_p;
1276
1277   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1278      been seen that makes the expression non-constant.  */
1279   bool non_integral_constant_expression_p;
1280
1281   /* TRUE if local variable names and `this' are forbidden in the
1282      current context.  */
1283   bool local_variables_forbidden_p;
1284
1285   /* TRUE if the declaration we are parsing is part of a
1286      linkage-specification of the form `extern string-literal
1287      declaration'.  */
1288   bool in_unbraced_linkage_specification_p;
1289
1290   /* TRUE if we are presently parsing a declarator, after the
1291      direct-declarator.  */
1292   bool in_declarator_p;
1293
1294   /* TRUE if we are presently parsing a template-argument-list.  */
1295   bool in_template_argument_list_p;
1296
1297   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1298      to IN_OMP_BLOCK if parsing OpenMP structured block and
1299      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1300      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1301      iteration-statement, OpenMP block or loop within that switch.  */
1302 #define IN_SWITCH_STMT          1
1303 #define IN_ITERATION_STMT       2
1304 #define IN_OMP_BLOCK            4
1305 #define IN_OMP_FOR              8
1306   unsigned char in_statement;
1307
1308   /* TRUE if we are presently parsing the body of a switch statement.
1309      Note that this doesn't quite overlap with in_statement above.
1310      The difference relates to giving the right sets of error messages:
1311      "case not in switch" vs "break statement used with OpenMP...".  */
1312   bool in_switch_statement_p;
1313
1314   /* TRUE if we are parsing a type-id in an expression context.  In
1315      such a situation, both "type (expr)" and "type (type)" are valid
1316      alternatives.  */
1317   bool in_type_id_in_expr_p;
1318
1319   /* TRUE if we are currently in a header file where declarations are
1320      implicitly extern "C".  */
1321   bool implicit_extern_c;
1322
1323   /* TRUE if strings in expressions should be translated to the execution
1324      character set.  */
1325   bool translate_strings_p;
1326
1327   /* If non-NULL, then we are parsing a construct where new type
1328      definitions are not permitted.  The string stored here will be
1329      issued as an error message if a type is defined.  */
1330   const char *type_definition_forbidden_message;
1331
1332   /* A list of lists. The outer list is a stack, used for member
1333      functions of local classes. At each level there are two sub-list,
1334      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1335      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1336      TREE_VALUE's. The functions are chained in reverse declaration
1337      order.
1338
1339      The TREE_PURPOSE sublist contains those functions with default
1340      arguments that need post processing, and the TREE_VALUE sublist
1341      contains those functions with definitions that need post
1342      processing.
1343
1344      These lists can only be processed once the outermost class being
1345      defined is complete.  */
1346   tree unparsed_functions_queues;
1347
1348   /* The number of classes whose definitions are currently in
1349      progress.  */
1350   unsigned num_classes_being_defined;
1351
1352   /* The number of template parameter lists that apply directly to the
1353      current declaration.  */
1354   unsigned num_template_parameter_lists;
1355 } 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, bool);
1394 static tree cp_parser_unqualified_id
1395   (cp_parser *, bool, 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 *, tree, 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 *, bool);
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 void cp_parser_perform_template_parameter_access_checks
1730   (tree);
1731 static tree cp_parser_single_declaration
1732   (cp_parser *, tree, bool, bool *);
1733 static tree cp_parser_functional_cast
1734   (cp_parser *, tree);
1735 static tree cp_parser_save_member_function_body
1736   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1737 static tree cp_parser_enclosed_template_argument_list
1738   (cp_parser *);
1739 static void cp_parser_save_default_args
1740   (cp_parser *, tree);
1741 static void cp_parser_late_parsing_for_member
1742   (cp_parser *, tree);
1743 static void cp_parser_late_parsing_default_args
1744   (cp_parser *, tree);
1745 static tree cp_parser_sizeof_operand
1746   (cp_parser *, enum rid);
1747 static bool cp_parser_declares_only_class_p
1748   (cp_parser *);
1749 static void cp_parser_set_storage_class
1750   (cp_parser *, cp_decl_specifier_seq *, enum rid);
1751 static void cp_parser_set_decl_spec_type
1752   (cp_decl_specifier_seq *, tree, bool);
1753 static bool cp_parser_friend_p
1754   (const cp_decl_specifier_seq *);
1755 static cp_token *cp_parser_require
1756   (cp_parser *, enum cpp_ttype, const char *);
1757 static cp_token *cp_parser_require_keyword
1758   (cp_parser *, enum rid, const char *);
1759 static bool cp_parser_token_starts_function_definition_p
1760   (cp_token *);
1761 static bool cp_parser_next_token_starts_class_definition_p
1762   (cp_parser *);
1763 static bool cp_parser_next_token_ends_template_argument_p
1764   (cp_parser *);
1765 static bool cp_parser_nth_token_starts_template_argument_list_p
1766   (cp_parser *, size_t);
1767 static enum tag_types cp_parser_token_is_class_key
1768   (cp_token *);
1769 static void cp_parser_check_class_key
1770   (enum tag_types, tree type);
1771 static void cp_parser_check_access_in_redeclaration
1772   (tree type);
1773 static bool cp_parser_optional_template_keyword
1774   (cp_parser *);
1775 static void cp_parser_pre_parsed_nested_name_specifier
1776   (cp_parser *);
1777 static void cp_parser_cache_group
1778   (cp_parser *, enum cpp_ttype, unsigned);
1779 static void cp_parser_parse_tentatively
1780   (cp_parser *);
1781 static void cp_parser_commit_to_tentative_parse
1782   (cp_parser *);
1783 static void cp_parser_abort_tentative_parse
1784   (cp_parser *);
1785 static bool cp_parser_parse_definitely
1786   (cp_parser *);
1787 static inline bool cp_parser_parsing_tentatively
1788   (cp_parser *);
1789 static bool cp_parser_uncommitted_to_tentative_parse_p
1790   (cp_parser *);
1791 static void cp_parser_error
1792   (cp_parser *, const char *);
1793 static void cp_parser_name_lookup_error
1794   (cp_parser *, tree, tree, const char *);
1795 static bool cp_parser_simulate_error
1796   (cp_parser *);
1797 static void cp_parser_check_type_definition
1798   (cp_parser *);
1799 static void cp_parser_check_for_definition_in_return_type
1800   (cp_declarator *, tree);
1801 static void cp_parser_check_for_invalid_template_id
1802   (cp_parser *, tree);
1803 static bool cp_parser_non_integral_constant_expression
1804   (cp_parser *, const char *);
1805 static void cp_parser_diagnose_invalid_type_name
1806   (cp_parser *, tree, tree);
1807 static bool cp_parser_parse_and_diagnose_invalid_type_name
1808   (cp_parser *);
1809 static int cp_parser_skip_to_closing_parenthesis
1810   (cp_parser *, bool, bool, bool);
1811 static void cp_parser_skip_to_end_of_statement
1812   (cp_parser *);
1813 static void cp_parser_consume_semicolon_at_end_of_statement
1814   (cp_parser *);
1815 static void cp_parser_skip_to_end_of_block_or_statement
1816   (cp_parser *);
1817 static void cp_parser_skip_to_closing_brace
1818   (cp_parser *);
1819 static void cp_parser_skip_until_found
1820   (cp_parser *, enum cpp_ttype, const char *);
1821 static void cp_parser_skip_to_pragma_eol
1822   (cp_parser*, cp_token *);
1823 static bool cp_parser_error_occurred
1824   (cp_parser *);
1825 static bool cp_parser_allow_gnu_extensions_p
1826   (cp_parser *);
1827 static bool cp_parser_is_string_literal
1828   (cp_token *);
1829 static bool cp_parser_is_keyword
1830   (cp_token *, enum rid);
1831 static tree cp_parser_make_typename_type
1832   (cp_parser *, tree, tree);
1833
1834 /* Returns nonzero if we are parsing tentatively.  */
1835
1836 static inline bool
1837 cp_parser_parsing_tentatively (cp_parser* parser)
1838 {
1839   return parser->context->next != NULL;
1840 }
1841
1842 /* Returns nonzero if TOKEN is a string literal.  */
1843
1844 static bool
1845 cp_parser_is_string_literal (cp_token* token)
1846 {
1847   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1848 }
1849
1850 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1851
1852 static bool
1853 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1854 {
1855   return token->keyword == keyword;
1856 }
1857
1858 /* A minimum or maximum operator has been seen.  As these are
1859    deprecated, issue a warning.  */
1860
1861 static inline void
1862 cp_parser_warn_min_max (void)
1863 {
1864   if (warn_deprecated && !in_system_header)
1865     warning (OPT_Wdeprecated, "minimum/maximum operators are deprecated");
1866 }
1867
1868 /* If not parsing tentatively, issue a diagnostic of the form
1869       FILE:LINE: MESSAGE before TOKEN
1870    where TOKEN is the next token in the input stream.  MESSAGE
1871    (specified by the caller) is usually of the form "expected
1872    OTHER-TOKEN".  */
1873
1874 static void
1875 cp_parser_error (cp_parser* parser, const char* message)
1876 {
1877   if (!cp_parser_simulate_error (parser))
1878     {
1879       cp_token *token = cp_lexer_peek_token (parser->lexer);
1880       /* This diagnostic makes more sense if it is tagged to the line
1881          of the token we just peeked at.  */
1882       cp_lexer_set_source_position_from_token (token);
1883
1884       if (token->type == CPP_PRAGMA)
1885         {
1886           error ("%<#pragma%> is not allowed here");
1887           cp_parser_skip_to_pragma_eol (parser, token);
1888           return;
1889         }
1890
1891       c_parse_error (message,
1892                      /* Because c_parser_error does not understand
1893                         CPP_KEYWORD, keywords are treated like
1894                         identifiers.  */
1895                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1896                      token->value);
1897     }
1898 }
1899
1900 /* Issue an error about name-lookup failing.  NAME is the
1901    IDENTIFIER_NODE DECL is the result of
1902    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1903    the thing that we hoped to find.  */
1904
1905 static void
1906 cp_parser_name_lookup_error (cp_parser* parser,
1907                              tree name,
1908                              tree decl,
1909                              const char* desired)
1910 {
1911   /* If name lookup completely failed, tell the user that NAME was not
1912      declared.  */
1913   if (decl == error_mark_node)
1914     {
1915       if (parser->scope && parser->scope != global_namespace)
1916         error ("%<%D::%D%> has not been declared",
1917                parser->scope, name);
1918       else if (parser->scope == global_namespace)
1919         error ("%<::%D%> has not been declared", name);
1920       else if (parser->object_scope
1921                && !CLASS_TYPE_P (parser->object_scope))
1922         error ("request for member %qD in non-class type %qT",
1923                name, parser->object_scope);
1924       else if (parser->object_scope)
1925         error ("%<%T::%D%> has not been declared",
1926                parser->object_scope, name);
1927       else
1928         error ("%qD has not been declared", name);
1929     }
1930   else if (parser->scope && parser->scope != global_namespace)
1931     error ("%<%D::%D%> %s", parser->scope, name, desired);
1932   else if (parser->scope == global_namespace)
1933     error ("%<::%D%> %s", name, desired);
1934   else
1935     error ("%qD %s", name, desired);
1936 }
1937
1938 /* If we are parsing tentatively, remember that an error has occurred
1939    during this tentative parse.  Returns true if the error was
1940    simulated; false if a message should be issued by the caller.  */
1941
1942 static bool
1943 cp_parser_simulate_error (cp_parser* parser)
1944 {
1945   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1946     {
1947       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1948       return true;
1949     }
1950   return false;
1951 }
1952
1953 /* Check for repeated decl-specifiers.  */
1954
1955 static void
1956 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
1957 {
1958   cp_decl_spec ds;
1959
1960   for (ds = ds_first; ds != ds_last; ++ds)
1961     {
1962       unsigned count = decl_specs->specs[(int)ds];
1963       if (count < 2)
1964         continue;
1965       /* The "long" specifier is a special case because of "long long".  */
1966       if (ds == ds_long)
1967         {
1968           if (count > 2)
1969             error ("%<long long long%> is too long for GCC");
1970           else if (pedantic && !in_system_header && warn_long_long)
1971             pedwarn ("ISO C++ does not support %<long long%>");
1972         }
1973       else if (count > 1)
1974         {
1975           static const char *const decl_spec_names[] = {
1976             "signed",
1977             "unsigned",
1978             "short",
1979             "long",
1980             "const",
1981             "volatile",
1982             "restrict",
1983             "inline",
1984             "virtual",
1985             "explicit",
1986             "friend",
1987             "typedef",
1988             "__complex",
1989             "__thread"
1990           };
1991           error ("duplicate %qs", decl_spec_names[(int)ds]);
1992         }
1993     }
1994 }
1995
1996 /* This function is called when a type is defined.  If type
1997    definitions are forbidden at this point, an error message is
1998    issued.  */
1999
2000 static void
2001 cp_parser_check_type_definition (cp_parser* parser)
2002 {
2003   /* If types are forbidden here, issue a message.  */
2004   if (parser->type_definition_forbidden_message)
2005     /* Use `%s' to print the string in case there are any escape
2006        characters in the message.  */
2007     error ("%s", parser->type_definition_forbidden_message);
2008 }
2009
2010 /* This function is called when the DECLARATOR is processed.  The TYPE
2011    was a type defined in the decl-specifiers.  If it is invalid to
2012    define a type in the decl-specifiers for DECLARATOR, an error is
2013    issued.  */
2014
2015 static void
2016 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2017                                                tree type)
2018 {
2019   /* [dcl.fct] forbids type definitions in return types.
2020      Unfortunately, it's not easy to know whether or not we are
2021      processing a return type until after the fact.  */
2022   while (declarator
2023          && (declarator->kind == cdk_pointer
2024              || declarator->kind == cdk_reference
2025              || declarator->kind == cdk_ptrmem))
2026     declarator = declarator->declarator;
2027   if (declarator
2028       && declarator->kind == cdk_function)
2029     {
2030       error ("new types may not be defined in a return type");
2031       inform ("(perhaps a semicolon is missing after the definition of %qT)",
2032               type);
2033     }
2034 }
2035
2036 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2037    "<" in any valid C++ program.  If the next token is indeed "<",
2038    issue a message warning the user about what appears to be an
2039    invalid attempt to form a template-id.  */
2040
2041 static void
2042 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2043                                          tree type)
2044 {
2045   cp_token_position start = 0;
2046
2047   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2048     {
2049       if (TYPE_P (type))
2050         error ("%qT is not a template", type);
2051       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2052         error ("%qE is not a template", type);
2053       else
2054         error ("invalid template-id");
2055       /* Remember the location of the invalid "<".  */
2056       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2057         start = cp_lexer_token_position (parser->lexer, true);
2058       /* Consume the "<".  */
2059       cp_lexer_consume_token (parser->lexer);
2060       /* Parse the template arguments.  */
2061       cp_parser_enclosed_template_argument_list (parser);
2062       /* Permanently remove the invalid template arguments so that
2063          this error message is not issued again.  */
2064       if (start)
2065         cp_lexer_purge_tokens_after (parser->lexer, start);
2066     }
2067 }
2068
2069 /* If parsing an integral constant-expression, issue an error message
2070    about the fact that THING appeared and return true.  Otherwise,
2071    return false.  In either case, set
2072    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2073
2074 static bool
2075 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2076                                             const char *thing)
2077 {
2078   parser->non_integral_constant_expression_p = true;
2079   if (parser->integral_constant_expression_p)
2080     {
2081       if (!parser->allow_non_integral_constant_expression_p)
2082         {
2083           error ("%s cannot appear in a constant-expression", thing);
2084           return true;
2085         }
2086     }
2087   return false;
2088 }
2089
2090 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2091    qualifying scope (or NULL, if none) for ID.  This function commits
2092    to the current active tentative parse, if any.  (Otherwise, the
2093    problematic construct might be encountered again later, resulting
2094    in duplicate error messages.)  */
2095
2096 static void
2097 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2098 {
2099   tree decl, old_scope;
2100   /* Try to lookup the identifier.  */
2101   old_scope = parser->scope;
2102   parser->scope = scope;
2103   decl = cp_parser_lookup_name_simple (parser, id);
2104   parser->scope = old_scope;
2105   /* If the lookup found a template-name, it means that the user forgot
2106   to specify an argument list. Emit a useful error message.  */
2107   if (TREE_CODE (decl) == TEMPLATE_DECL)
2108     error ("invalid use of template-name %qE without an argument list",
2109       decl);
2110   else if (!parser->scope)
2111     {
2112       /* Issue an error message.  */
2113       error ("%qE does not name a type", id);
2114       /* If we're in a template class, it's possible that the user was
2115          referring to a type from a base class.  For example:
2116
2117            template <typename T> struct A { typedef T X; };
2118            template <typename T> struct B : public A<T> { X x; };
2119
2120          The user should have said "typename A<T>::X".  */
2121       if (processing_template_decl && current_class_type
2122           && TYPE_BINFO (current_class_type))
2123         {
2124           tree b;
2125
2126           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2127                b;
2128                b = TREE_CHAIN (b))
2129             {
2130               tree base_type = BINFO_TYPE (b);
2131               if (CLASS_TYPE_P (base_type)
2132                   && dependent_type_p (base_type))
2133                 {
2134                   tree field;
2135                   /* Go from a particular instantiation of the
2136                      template (which will have an empty TYPE_FIELDs),
2137                      to the main version.  */
2138                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2139                   for (field = TYPE_FIELDS (base_type);
2140                        field;
2141                        field = TREE_CHAIN (field))
2142                     if (TREE_CODE (field) == TYPE_DECL
2143                         && DECL_NAME (field) == id)
2144                       {
2145                         inform ("(perhaps %<typename %T::%E%> was intended)",
2146                                 BINFO_TYPE (b), id);
2147                         break;
2148                       }
2149                   if (field)
2150                     break;
2151                 }
2152             }
2153         }
2154     }
2155   /* Here we diagnose qualified-ids where the scope is actually correct,
2156      but the identifier does not resolve to a valid type name.  */
2157   else if (parser->scope != error_mark_node)
2158     {
2159       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2160         error ("%qE in namespace %qE does not name a type",
2161                id, parser->scope);
2162       else if (TYPE_P (parser->scope))
2163         error ("%qE in class %qT does not name a type", id, parser->scope);
2164       else
2165         gcc_unreachable ();
2166     }
2167   cp_parser_commit_to_tentative_parse (parser);
2168 }
2169
2170 /* Check for a common situation where a type-name should be present,
2171    but is not, and issue a sensible error message.  Returns true if an
2172    invalid type-name was detected.
2173
2174    The situation handled by this function are variable declarations of the
2175    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2176    Usually, `ID' should name a type, but if we got here it means that it
2177    does not. We try to emit the best possible error message depending on
2178    how exactly the id-expression looks like.  */
2179
2180 static bool
2181 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2182 {
2183   tree id;
2184
2185   cp_parser_parse_tentatively (parser);
2186   id = cp_parser_id_expression (parser,
2187                                 /*template_keyword_p=*/false,
2188                                 /*check_dependency_p=*/true,
2189                                 /*template_p=*/NULL,
2190                                 /*declarator_p=*/true,
2191                                 /*optional_p=*/false);
2192   /* After the id-expression, there should be a plain identifier,
2193      otherwise this is not a simple variable declaration. Also, if
2194      the scope is dependent, we cannot do much.  */
2195   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2196       || (parser->scope && TYPE_P (parser->scope)
2197           && dependent_type_p (parser->scope)))
2198     {
2199       cp_parser_abort_tentative_parse (parser);
2200       return false;
2201     }
2202   if (!cp_parser_parse_definitely (parser)
2203       || TREE_CODE (id) != IDENTIFIER_NODE)
2204     return false;
2205
2206   /* Emit a diagnostic for the invalid type.  */
2207   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2208   /* Skip to the end of the declaration; there's no point in
2209      trying to process it.  */
2210   cp_parser_skip_to_end_of_block_or_statement (parser);
2211   return true;
2212 }
2213
2214 /* Consume tokens up to, and including, the next non-nested closing `)'.
2215    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2216    are doing error recovery. Returns -1 if OR_COMMA is true and we
2217    found an unnested comma.  */
2218
2219 static int
2220 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2221                                        bool recovering,
2222                                        bool or_comma,
2223                                        bool consume_paren)
2224 {
2225   unsigned paren_depth = 0;
2226   unsigned brace_depth = 0;
2227
2228   if (recovering && !or_comma
2229       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2230     return 0;
2231
2232   while (true)
2233     {
2234       cp_token * token = cp_lexer_peek_token (parser->lexer);
2235
2236       switch (token->type)
2237         {
2238         case CPP_EOF:
2239         case CPP_PRAGMA_EOL:
2240           /* If we've run out of tokens, then there is no closing `)'.  */
2241           return 0;
2242
2243         case CPP_SEMICOLON:
2244           /* This matches the processing in skip_to_end_of_statement.  */
2245           if (!brace_depth)
2246             return 0;
2247           break;
2248
2249         case CPP_OPEN_BRACE:
2250           ++brace_depth;
2251           break;
2252         case CPP_CLOSE_BRACE:
2253           if (!brace_depth--)
2254             return 0;
2255           break;
2256
2257         case CPP_COMMA:
2258           if (recovering && or_comma && !brace_depth && !paren_depth)
2259             return -1;
2260           break;
2261
2262         case CPP_OPEN_PAREN:
2263           if (!brace_depth)
2264             ++paren_depth;
2265           break;
2266
2267         case CPP_CLOSE_PAREN:
2268           if (!brace_depth && !paren_depth--)
2269             {
2270               if (consume_paren)
2271                 cp_lexer_consume_token (parser->lexer);
2272               return 1;
2273             }
2274           break;
2275
2276         default:
2277           break;
2278         }
2279
2280       /* Consume the token.  */
2281       cp_lexer_consume_token (parser->lexer);
2282     }
2283 }
2284
2285 /* Consume tokens until we reach the end of the current statement.
2286    Normally, that will be just before consuming a `;'.  However, if a
2287    non-nested `}' comes first, then we stop before consuming that.  */
2288
2289 static void
2290 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2291 {
2292   unsigned nesting_depth = 0;
2293
2294   while (true)
2295     {
2296       cp_token *token = cp_lexer_peek_token (parser->lexer);
2297
2298       switch (token->type)
2299         {
2300         case CPP_EOF:
2301         case CPP_PRAGMA_EOL:
2302           /* If we've run out of tokens, stop.  */
2303           return;
2304
2305         case CPP_SEMICOLON:
2306           /* If the next token is a `;', we have reached the end of the
2307              statement.  */
2308           if (!nesting_depth)
2309             return;
2310           break;
2311
2312         case CPP_CLOSE_BRACE:
2313           /* If this is a non-nested '}', stop before consuming it.
2314              That way, when confronted with something like:
2315
2316                { 3 + }
2317
2318              we stop before consuming the closing '}', even though we
2319              have not yet reached a `;'.  */
2320           if (nesting_depth == 0)
2321             return;
2322
2323           /* If it is the closing '}' for a block that we have
2324              scanned, stop -- but only after consuming the token.
2325              That way given:
2326
2327                 void f g () { ... }
2328                 typedef int I;
2329
2330              we will stop after the body of the erroneously declared
2331              function, but before consuming the following `typedef'
2332              declaration.  */
2333           if (--nesting_depth == 0)
2334             {
2335               cp_lexer_consume_token (parser->lexer);
2336               return;
2337             }
2338
2339         case CPP_OPEN_BRACE:
2340           ++nesting_depth;
2341           break;
2342
2343         default:
2344           break;
2345         }
2346
2347       /* Consume the token.  */
2348       cp_lexer_consume_token (parser->lexer);
2349     }
2350 }
2351
2352 /* This function is called at the end of a statement or declaration.
2353    If the next token is a semicolon, it is consumed; otherwise, error
2354    recovery is attempted.  */
2355
2356 static void
2357 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2358 {
2359   /* Look for the trailing `;'.  */
2360   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2361     {
2362       /* If there is additional (erroneous) input, skip to the end of
2363          the statement.  */
2364       cp_parser_skip_to_end_of_statement (parser);
2365       /* If the next token is now a `;', consume it.  */
2366       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2367         cp_lexer_consume_token (parser->lexer);
2368     }
2369 }
2370
2371 /* Skip tokens until we have consumed an entire block, or until we
2372    have consumed a non-nested `;'.  */
2373
2374 static void
2375 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2376 {
2377   int nesting_depth = 0;
2378
2379   while (nesting_depth >= 0)
2380     {
2381       cp_token *token = cp_lexer_peek_token (parser->lexer);
2382
2383       switch (token->type)
2384         {
2385         case CPP_EOF:
2386         case CPP_PRAGMA_EOL:
2387           /* If we've run out of tokens, stop.  */
2388           return;
2389
2390         case CPP_SEMICOLON:
2391           /* Stop if this is an unnested ';'. */
2392           if (!nesting_depth)
2393             nesting_depth = -1;
2394           break;
2395
2396         case CPP_CLOSE_BRACE:
2397           /* Stop if this is an unnested '}', or closes the outermost
2398              nesting level.  */
2399           nesting_depth--;
2400           if (!nesting_depth)
2401             nesting_depth = -1;
2402           break;
2403
2404         case CPP_OPEN_BRACE:
2405           /* Nest. */
2406           nesting_depth++;
2407           break;
2408
2409         default:
2410           break;
2411         }
2412
2413       /* Consume the token.  */
2414       cp_lexer_consume_token (parser->lexer);
2415     }
2416 }
2417
2418 /* Skip tokens until a non-nested closing curly brace is the next
2419    token.  */
2420
2421 static void
2422 cp_parser_skip_to_closing_brace (cp_parser *parser)
2423 {
2424   unsigned nesting_depth = 0;
2425
2426   while (true)
2427     {
2428       cp_token *token = cp_lexer_peek_token (parser->lexer);
2429
2430       switch (token->type)
2431         {
2432         case CPP_EOF:
2433         case CPP_PRAGMA_EOL:
2434           /* If we've run out of tokens, stop.  */
2435           return;
2436
2437         case CPP_CLOSE_BRACE:
2438           /* If the next token is a non-nested `}', then we have reached
2439              the end of the current block.  */
2440           if (nesting_depth-- == 0)
2441             return;
2442           break;
2443
2444         case CPP_OPEN_BRACE:
2445           /* If it the next token is a `{', then we are entering a new
2446              block.  Consume the entire block.  */
2447           ++nesting_depth;
2448           break;
2449
2450         default:
2451           break;
2452         }
2453
2454       /* Consume the token.  */
2455       cp_lexer_consume_token (parser->lexer);
2456     }
2457 }
2458
2459 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2460    parameter is the PRAGMA token, allowing us to purge the entire pragma
2461    sequence.  */
2462
2463 static void
2464 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2465 {
2466   cp_token *token;
2467
2468   parser->lexer->in_pragma = false;
2469
2470   do
2471     token = cp_lexer_consume_token (parser->lexer);
2472   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2473
2474   /* Ensure that the pragma is not parsed again.  */
2475   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2476 }
2477
2478 /* Require pragma end of line, resyncing with it as necessary.  The
2479    arguments are as for cp_parser_skip_to_pragma_eol.  */
2480
2481 static void
2482 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2483 {
2484   parser->lexer->in_pragma = false;
2485   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2486     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2487 }
2488
2489 /* This is a simple wrapper around make_typename_type. When the id is
2490    an unresolved identifier node, we can provide a superior diagnostic
2491    using cp_parser_diagnose_invalid_type_name.  */
2492
2493 static tree
2494 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2495 {
2496   tree result;
2497   if (TREE_CODE (id) == IDENTIFIER_NODE)
2498     {
2499       result = make_typename_type (scope, id, typename_type,
2500                                    /*complain=*/tf_none);
2501       if (result == error_mark_node)
2502         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2503       return result;
2504     }
2505   return make_typename_type (scope, id, typename_type, tf_error);
2506 }
2507
2508
2509 /* Create a new C++ parser.  */
2510
2511 static cp_parser *
2512 cp_parser_new (void)
2513 {
2514   cp_parser *parser;
2515   cp_lexer *lexer;
2516   unsigned i;
2517
2518   /* cp_lexer_new_main is called before calling ggc_alloc because
2519      cp_lexer_new_main might load a PCH file.  */
2520   lexer = cp_lexer_new_main ();
2521
2522   /* Initialize the binops_by_token so that we can get the tree
2523      directly from the token.  */
2524   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2525     binops_by_token[binops[i].token_type] = binops[i];
2526
2527   parser = GGC_CNEW (cp_parser);
2528   parser->lexer = lexer;
2529   parser->context = cp_parser_context_new (NULL);
2530
2531   /* For now, we always accept GNU extensions.  */
2532   parser->allow_gnu_extensions_p = 1;
2533
2534   /* The `>' token is a greater-than operator, not the end of a
2535      template-id.  */
2536   parser->greater_than_is_operator_p = true;
2537
2538   parser->default_arg_ok_p = true;
2539
2540   /* We are not parsing a constant-expression.  */
2541   parser->integral_constant_expression_p = false;
2542   parser->allow_non_integral_constant_expression_p = false;
2543   parser->non_integral_constant_expression_p = false;
2544
2545   /* Local variable names are not forbidden.  */
2546   parser->local_variables_forbidden_p = false;
2547
2548   /* We are not processing an `extern "C"' declaration.  */
2549   parser->in_unbraced_linkage_specification_p = false;
2550
2551   /* We are not processing a declarator.  */
2552   parser->in_declarator_p = false;
2553
2554   /* We are not processing a template-argument-list.  */
2555   parser->in_template_argument_list_p = false;
2556
2557   /* We are not in an iteration statement.  */
2558   parser->in_statement = 0;
2559
2560   /* We are not in a switch statement.  */
2561   parser->in_switch_statement_p = false;
2562
2563   /* We are not parsing a type-id inside an expression.  */
2564   parser->in_type_id_in_expr_p = false;
2565
2566   /* Declarations aren't implicitly extern "C".  */
2567   parser->implicit_extern_c = false;
2568
2569   /* String literals should be translated to the execution character set.  */
2570   parser->translate_strings_p = true;
2571
2572   /* The unparsed function queue is empty.  */
2573   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2574
2575   /* There are no classes being defined.  */
2576   parser->num_classes_being_defined = 0;
2577
2578   /* No template parameters apply.  */
2579   parser->num_template_parameter_lists = 0;
2580
2581   return parser;
2582 }
2583
2584 /* Create a cp_lexer structure which will emit the tokens in CACHE
2585    and push it onto the parser's lexer stack.  This is used for delayed
2586    parsing of in-class method bodies and default arguments, and should
2587    not be confused with tentative parsing.  */
2588 static void
2589 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2590 {
2591   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2592   lexer->next = parser->lexer;
2593   parser->lexer = lexer;
2594
2595   /* Move the current source position to that of the first token in the
2596      new lexer.  */
2597   cp_lexer_set_source_position_from_token (lexer->next_token);
2598 }
2599
2600 /* Pop the top lexer off the parser stack.  This is never used for the
2601    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2602 static void
2603 cp_parser_pop_lexer (cp_parser *parser)
2604 {
2605   cp_lexer *lexer = parser->lexer;
2606   parser->lexer = lexer->next;
2607   cp_lexer_destroy (lexer);
2608
2609   /* Put the current source position back where it was before this
2610      lexer was pushed.  */
2611   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2612 }
2613
2614 /* Lexical conventions [gram.lex]  */
2615
2616 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2617    identifier.  */
2618
2619 static tree
2620 cp_parser_identifier (cp_parser* parser)
2621 {
2622   cp_token *token;
2623
2624   /* Look for the identifier.  */
2625   token = cp_parser_require (parser, CPP_NAME, "identifier");
2626   /* Return the value.  */
2627   return token ? token->value : error_mark_node;
2628 }
2629
2630 /* Parse a sequence of adjacent string constants.  Returns a
2631    TREE_STRING representing the combined, nul-terminated string
2632    constant.  If TRANSLATE is true, translate the string to the
2633    execution character set.  If WIDE_OK is true, a wide string is
2634    invalid here.
2635
2636    C++98 [lex.string] says that if a narrow string literal token is
2637    adjacent to a wide string literal token, the behavior is undefined.
2638    However, C99 6.4.5p4 says that this results in a wide string literal.
2639    We follow C99 here, for consistency with the C front end.
2640
2641    This code is largely lifted from lex_string() in c-lex.c.
2642
2643    FUTURE: ObjC++ will need to handle @-strings here.  */
2644 static tree
2645 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2646 {
2647   tree value;
2648   bool wide = false;
2649   size_t count;
2650   struct obstack str_ob;
2651   cpp_string str, istr, *strs;
2652   cp_token *tok;
2653
2654   tok = cp_lexer_peek_token (parser->lexer);
2655   if (!cp_parser_is_string_literal (tok))
2656     {
2657       cp_parser_error (parser, "expected string-literal");
2658       return error_mark_node;
2659     }
2660
2661   /* Try to avoid the overhead of creating and destroying an obstack
2662      for the common case of just one string.  */
2663   if (!cp_parser_is_string_literal
2664       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2665     {
2666       cp_lexer_consume_token (parser->lexer);
2667
2668       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2669       str.len = TREE_STRING_LENGTH (tok->value);
2670       count = 1;
2671       if (tok->type == CPP_WSTRING)
2672         wide = true;
2673
2674       strs = &str;
2675     }
2676   else
2677     {
2678       gcc_obstack_init (&str_ob);
2679       count = 0;
2680
2681       do
2682         {
2683           cp_lexer_consume_token (parser->lexer);
2684           count++;
2685           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2686           str.len = TREE_STRING_LENGTH (tok->value);
2687           if (tok->type == CPP_WSTRING)
2688             wide = true;
2689
2690           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2691
2692           tok = cp_lexer_peek_token (parser->lexer);
2693         }
2694       while (cp_parser_is_string_literal (tok));
2695
2696       strs = (cpp_string *) obstack_finish (&str_ob);
2697     }
2698
2699   if (wide && !wide_ok)
2700     {
2701       cp_parser_error (parser, "a wide string is invalid in this context");
2702       wide = false;
2703     }
2704
2705   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2706       (parse_in, strs, count, &istr, wide))
2707     {
2708       value = build_string (istr.len, (char *)istr.text);
2709       free ((void *)istr.text);
2710
2711       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2712       value = fix_string_type (value);
2713     }
2714   else
2715     /* cpp_interpret_string has issued an error.  */
2716     value = error_mark_node;
2717
2718   if (count > 1)
2719     obstack_free (&str_ob, 0);
2720
2721   return value;
2722 }
2723
2724
2725 /* Basic concepts [gram.basic]  */
2726
2727 /* Parse a translation-unit.
2728
2729    translation-unit:
2730      declaration-seq [opt]
2731
2732    Returns TRUE if all went well.  */
2733
2734 static bool
2735 cp_parser_translation_unit (cp_parser* parser)
2736 {
2737   /* The address of the first non-permanent object on the declarator
2738      obstack.  */
2739   static void *declarator_obstack_base;
2740
2741   bool success;
2742
2743   /* Create the declarator obstack, if necessary.  */
2744   if (!cp_error_declarator)
2745     {
2746       gcc_obstack_init (&declarator_obstack);
2747       /* Create the error declarator.  */
2748       cp_error_declarator = make_declarator (cdk_error);
2749       /* Create the empty parameter list.  */
2750       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2751       /* Remember where the base of the declarator obstack lies.  */
2752       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2753     }
2754
2755   cp_parser_declaration_seq_opt (parser);
2756
2757   /* If there are no tokens left then all went well.  */
2758   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2759     {
2760       /* Get rid of the token array; we don't need it any more.  */
2761       cp_lexer_destroy (parser->lexer);
2762       parser->lexer = NULL;
2763
2764       /* This file might have been a context that's implicitly extern
2765          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2766       if (parser->implicit_extern_c)
2767         {
2768           pop_lang_context ();
2769           parser->implicit_extern_c = false;
2770         }
2771
2772       /* Finish up.  */
2773       finish_translation_unit ();
2774
2775       success = true;
2776     }
2777   else
2778     {
2779       cp_parser_error (parser, "expected declaration");
2780       success = false;
2781     }
2782
2783   /* Make sure the declarator obstack was fully cleaned up.  */
2784   gcc_assert (obstack_next_free (&declarator_obstack)
2785               == declarator_obstack_base);
2786
2787   /* All went well.  */
2788   return success;
2789 }
2790
2791 /* Expressions [gram.expr] */
2792
2793 /* Parse a primary-expression.
2794
2795    primary-expression:
2796      literal
2797      this
2798      ( expression )
2799      id-expression
2800
2801    GNU Extensions:
2802
2803    primary-expression:
2804      ( compound-statement )
2805      __builtin_va_arg ( assignment-expression , type-id )
2806      __builtin_offsetof ( type-id , offsetof-expression )
2807
2808    Objective-C++ Extension:
2809
2810    primary-expression:
2811      objc-expression
2812
2813    literal:
2814      __null
2815
2816    ADDRESS_P is true iff this expression was immediately preceded by
2817    "&" and therefore might denote a pointer-to-member.  CAST_P is true
2818    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
2819    true iff this expression is a template argument.
2820
2821    Returns a representation of the expression.  Upon return, *IDK
2822    indicates what kind of id-expression (if any) was present.  */
2823
2824 static tree
2825 cp_parser_primary_expression (cp_parser *parser,
2826                               bool address_p,
2827                               bool cast_p,
2828                               bool template_arg_p,
2829                               cp_id_kind *idk)
2830 {
2831   cp_token *token;
2832
2833   /* Assume the primary expression is not an id-expression.  */
2834   *idk = CP_ID_KIND_NONE;
2835
2836   /* Peek at the next token.  */
2837   token = cp_lexer_peek_token (parser->lexer);
2838   switch (token->type)
2839     {
2840       /* literal:
2841            integer-literal
2842            character-literal
2843            floating-literal
2844            string-literal
2845            boolean-literal  */
2846     case CPP_CHAR:
2847     case CPP_WCHAR:
2848     case CPP_NUMBER:
2849       token = cp_lexer_consume_token (parser->lexer);
2850       /* Floating-point literals are only allowed in an integral
2851          constant expression if they are cast to an integral or
2852          enumeration type.  */
2853       if (TREE_CODE (token->value) == REAL_CST
2854           && parser->integral_constant_expression_p
2855           && pedantic)
2856         {
2857           /* CAST_P will be set even in invalid code like "int(2.7 +
2858              ...)".   Therefore, we have to check that the next token
2859              is sure to end the cast.  */
2860           if (cast_p)
2861             {
2862               cp_token *next_token;
2863
2864               next_token = cp_lexer_peek_token (parser->lexer);
2865               if (/* The comma at the end of an
2866                      enumerator-definition.  */
2867                   next_token->type != CPP_COMMA
2868                   /* The curly brace at the end of an enum-specifier.  */
2869                   && next_token->type != CPP_CLOSE_BRACE
2870                   /* The end of a statement.  */
2871                   && next_token->type != CPP_SEMICOLON
2872                   /* The end of the cast-expression.  */
2873                   && next_token->type != CPP_CLOSE_PAREN
2874                   /* The end of an array bound.  */
2875                   && next_token->type != CPP_CLOSE_SQUARE
2876                   /* The closing ">" in a template-argument-list.  */
2877                   && (next_token->type != CPP_GREATER
2878                       || parser->greater_than_is_operator_p))
2879                 cast_p = false;
2880             }
2881
2882           /* If we are within a cast, then the constraint that the
2883              cast is to an integral or enumeration type will be
2884              checked at that point.  If we are not within a cast, then
2885              this code is invalid.  */
2886           if (!cast_p)
2887             cp_parser_non_integral_constant_expression
2888               (parser, "floating-point literal");
2889         }
2890       return token->value;
2891
2892     case CPP_STRING:
2893     case CPP_WSTRING:
2894       /* ??? Should wide strings be allowed when parser->translate_strings_p
2895          is false (i.e. in attributes)?  If not, we can kill the third
2896          argument to cp_parser_string_literal.  */
2897       return cp_parser_string_literal (parser,
2898                                        parser->translate_strings_p,
2899                                        true);
2900
2901     case CPP_OPEN_PAREN:
2902       {
2903         tree expr;
2904         bool saved_greater_than_is_operator_p;
2905
2906         /* Consume the `('.  */
2907         cp_lexer_consume_token (parser->lexer);
2908         /* Within a parenthesized expression, a `>' token is always
2909            the greater-than operator.  */
2910         saved_greater_than_is_operator_p
2911           = parser->greater_than_is_operator_p;
2912         parser->greater_than_is_operator_p = true;
2913         /* If we see `( { ' then we are looking at the beginning of
2914            a GNU statement-expression.  */
2915         if (cp_parser_allow_gnu_extensions_p (parser)
2916             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2917           {
2918             /* Statement-expressions are not allowed by the standard.  */
2919             if (pedantic)
2920               pedwarn ("ISO C++ forbids braced-groups within expressions");
2921
2922             /* And they're not allowed outside of a function-body; you
2923                cannot, for example, write:
2924
2925                  int i = ({ int j = 3; j + 1; });
2926
2927                at class or namespace scope.  */
2928             if (!at_function_scope_p ())
2929               error ("statement-expressions are allowed only inside functions");
2930             /* Start the statement-expression.  */
2931             expr = begin_stmt_expr ();
2932             /* Parse the compound-statement.  */
2933             cp_parser_compound_statement (parser, expr, false);
2934             /* Finish up.  */
2935             expr = finish_stmt_expr (expr, false);
2936           }
2937         else
2938           {
2939             /* Parse the parenthesized expression.  */
2940             expr = cp_parser_expression (parser, cast_p);
2941             /* Let the front end know that this expression was
2942                enclosed in parentheses. This matters in case, for
2943                example, the expression is of the form `A::B', since
2944                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2945                not.  */
2946             finish_parenthesized_expr (expr);
2947           }
2948         /* The `>' token might be the end of a template-id or
2949            template-parameter-list now.  */
2950         parser->greater_than_is_operator_p
2951           = saved_greater_than_is_operator_p;
2952         /* Consume the `)'.  */
2953         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2954           cp_parser_skip_to_end_of_statement (parser);
2955
2956         return expr;
2957       }
2958
2959     case CPP_KEYWORD:
2960       switch (token->keyword)
2961         {
2962           /* These two are the boolean literals.  */
2963         case RID_TRUE:
2964           cp_lexer_consume_token (parser->lexer);
2965           return boolean_true_node;
2966         case RID_FALSE:
2967           cp_lexer_consume_token (parser->lexer);
2968           return boolean_false_node;
2969
2970           /* The `__null' literal.  */
2971         case RID_NULL:
2972           cp_lexer_consume_token (parser->lexer);
2973           return null_node;
2974
2975           /* Recognize the `this' keyword.  */
2976         case RID_THIS:
2977           cp_lexer_consume_token (parser->lexer);
2978           if (parser->local_variables_forbidden_p)
2979             {
2980               error ("%<this%> may not be used in this context");
2981               return error_mark_node;
2982             }
2983           /* Pointers cannot appear in constant-expressions.  */
2984           if (cp_parser_non_integral_constant_expression (parser,
2985                                                           "`this'"))
2986             return error_mark_node;
2987           return finish_this_expr ();
2988
2989           /* The `operator' keyword can be the beginning of an
2990              id-expression.  */
2991         case RID_OPERATOR:
2992           goto id_expression;
2993
2994         case RID_FUNCTION_NAME:
2995         case RID_PRETTY_FUNCTION_NAME:
2996         case RID_C99_FUNCTION_NAME:
2997           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2998              __func__ are the names of variables -- but they are
2999              treated specially.  Therefore, they are handled here,
3000              rather than relying on the generic id-expression logic
3001              below.  Grammatically, these names are id-expressions.
3002
3003              Consume the token.  */
3004           token = cp_lexer_consume_token (parser->lexer);
3005           /* Look up the name.  */
3006           return finish_fname (token->value);
3007
3008         case RID_VA_ARG:
3009           {
3010             tree expression;
3011             tree type;
3012
3013             /* The `__builtin_va_arg' construct is used to handle
3014                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3015             cp_lexer_consume_token (parser->lexer);
3016             /* Look for the opening `('.  */
3017             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3018             /* Now, parse the assignment-expression.  */
3019             expression = cp_parser_assignment_expression (parser,
3020                                                           /*cast_p=*/false);
3021             /* Look for the `,'.  */
3022             cp_parser_require (parser, CPP_COMMA, "`,'");
3023             /* Parse the type-id.  */
3024             type = cp_parser_type_id (parser);
3025             /* Look for the closing `)'.  */
3026             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3027             /* Using `va_arg' in a constant-expression is not
3028                allowed.  */
3029             if (cp_parser_non_integral_constant_expression (parser,
3030                                                             "`va_arg'"))
3031               return error_mark_node;
3032             return build_x_va_arg (expression, type);
3033           }
3034
3035         case RID_OFFSETOF:
3036           return cp_parser_builtin_offsetof (parser);
3037
3038           /* Objective-C++ expressions.  */
3039         case RID_AT_ENCODE:
3040         case RID_AT_PROTOCOL:
3041         case RID_AT_SELECTOR:
3042           return cp_parser_objc_expression (parser);
3043
3044         default:
3045           cp_parser_error (parser, "expected primary-expression");
3046           return error_mark_node;
3047         }
3048
3049       /* An id-expression can start with either an identifier, a
3050          `::' as the beginning of a qualified-id, or the "operator"
3051          keyword.  */
3052     case CPP_NAME:
3053     case CPP_SCOPE:
3054     case CPP_TEMPLATE_ID:
3055     case CPP_NESTED_NAME_SPECIFIER:
3056       {
3057         tree id_expression;
3058         tree decl;
3059         const char *error_msg;
3060         bool template_p;
3061         bool done;
3062
3063       id_expression:
3064         /* Parse the id-expression.  */
3065         id_expression
3066           = cp_parser_id_expression (parser,
3067                                      /*template_keyword_p=*/false,
3068                                      /*check_dependency_p=*/true,
3069                                      &template_p,
3070                                      /*declarator_p=*/false,
3071                                      /*optional_p=*/false);
3072         if (id_expression == error_mark_node)
3073           return error_mark_node;
3074         token = cp_lexer_peek_token (parser->lexer);
3075         done = (token->type != CPP_OPEN_SQUARE
3076                 && token->type != CPP_OPEN_PAREN
3077                 && token->type != CPP_DOT
3078                 && token->type != CPP_DEREF
3079                 && token->type != CPP_PLUS_PLUS
3080                 && token->type != CPP_MINUS_MINUS);
3081         /* If we have a template-id, then no further lookup is
3082            required.  If the template-id was for a template-class, we
3083            will sometimes have a TYPE_DECL at this point.  */
3084         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3085                  || TREE_CODE (id_expression) == TYPE_DECL)
3086           decl = id_expression;
3087         /* Look up the name.  */
3088         else
3089           {
3090             tree ambiguous_decls;
3091
3092             decl = cp_parser_lookup_name (parser, id_expression,
3093                                           none_type,
3094                                           template_p,
3095                                           /*is_namespace=*/false,
3096                                           /*check_dependency=*/true,
3097                                           &ambiguous_decls);
3098             /* If the lookup was ambiguous, an error will already have
3099                been issued.  */
3100             if (ambiguous_decls)
3101               return error_mark_node;
3102
3103             /* In Objective-C++, an instance variable (ivar) may be preferred
3104                to whatever cp_parser_lookup_name() found.  */
3105             decl = objc_lookup_ivar (decl, id_expression);
3106
3107             /* If name lookup gives us a SCOPE_REF, then the
3108                qualifying scope was dependent.  */
3109             if (TREE_CODE (decl) == SCOPE_REF)
3110               return decl;
3111             /* Check to see if DECL is a local variable in a context
3112                where that is forbidden.  */
3113             if (parser->local_variables_forbidden_p
3114                 && local_variable_p (decl))
3115               {
3116                 /* It might be that we only found DECL because we are
3117                    trying to be generous with pre-ISO scoping rules.
3118                    For example, consider:
3119
3120                      int i;
3121                      void g() {
3122                        for (int i = 0; i < 10; ++i) {}
3123                        extern void f(int j = i);
3124                      }
3125
3126                    Here, name look up will originally find the out
3127                    of scope `i'.  We need to issue a warning message,
3128                    but then use the global `i'.  */
3129                 decl = check_for_out_of_scope_variable (decl);
3130                 if (local_variable_p (decl))
3131                   {
3132                     error ("local variable %qD may not appear in this context",
3133                            decl);
3134                     return error_mark_node;
3135                   }
3136               }
3137           }
3138
3139         decl = (finish_id_expression
3140                 (id_expression, decl, parser->scope,
3141                  idk,
3142                  parser->integral_constant_expression_p,
3143                  parser->allow_non_integral_constant_expression_p,
3144                  &parser->non_integral_constant_expression_p,
3145                  template_p, done, address_p,
3146                  template_arg_p,
3147                  &error_msg));
3148         if (error_msg)
3149           cp_parser_error (parser, error_msg);
3150         return decl;
3151       }
3152
3153       /* Anything else is an error.  */
3154     default:
3155       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3156       if (c_dialect_objc ()
3157           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3158         return cp_parser_objc_expression (parser);
3159
3160       cp_parser_error (parser, "expected primary-expression");
3161       return error_mark_node;
3162     }
3163 }
3164
3165 /* Parse an id-expression.
3166
3167    id-expression:
3168      unqualified-id
3169      qualified-id
3170
3171    qualified-id:
3172      :: [opt] nested-name-specifier template [opt] unqualified-id
3173      :: identifier
3174      :: operator-function-id
3175      :: template-id
3176
3177    Return a representation of the unqualified portion of the
3178    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3179    a `::' or nested-name-specifier.
3180
3181    Often, if the id-expression was a qualified-id, the caller will
3182    want to make a SCOPE_REF to represent the qualified-id.  This
3183    function does not do this in order to avoid wastefully creating
3184    SCOPE_REFs when they are not required.
3185
3186    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3187    `template' keyword.
3188
3189    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3190    uninstantiated templates.
3191
3192    If *TEMPLATE_P is non-NULL, it is set to true iff the
3193    `template' keyword is used to explicitly indicate that the entity
3194    named is a template.
3195
3196    If DECLARATOR_P is true, the id-expression is appearing as part of
3197    a declarator, rather than as part of an expression.  */
3198
3199 static tree
3200 cp_parser_id_expression (cp_parser *parser,
3201                          bool template_keyword_p,
3202                          bool check_dependency_p,
3203                          bool *template_p,
3204                          bool declarator_p,
3205                          bool optional_p)
3206 {
3207   bool global_scope_p;
3208   bool nested_name_specifier_p;
3209
3210   /* Assume the `template' keyword was not used.  */
3211   if (template_p)
3212     *template_p = template_keyword_p;
3213
3214   /* Look for the optional `::' operator.  */
3215   global_scope_p
3216     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3217        != NULL_TREE);
3218   /* Look for the optional nested-name-specifier.  */
3219   nested_name_specifier_p
3220     = (cp_parser_nested_name_specifier_opt (parser,
3221                                             /*typename_keyword_p=*/false,
3222                                             check_dependency_p,
3223                                             /*type_p=*/false,
3224                                             declarator_p)
3225        != NULL_TREE);
3226   /* If there is a nested-name-specifier, then we are looking at
3227      the first qualified-id production.  */
3228   if (nested_name_specifier_p)
3229     {
3230       tree saved_scope;
3231       tree saved_object_scope;
3232       tree saved_qualifying_scope;
3233       tree unqualified_id;
3234       bool is_template;
3235
3236       /* See if the next token is the `template' keyword.  */
3237       if (!template_p)
3238         template_p = &is_template;
3239       *template_p = cp_parser_optional_template_keyword (parser);
3240       /* Name lookup we do during the processing of the
3241          unqualified-id might obliterate SCOPE.  */
3242       saved_scope = parser->scope;
3243       saved_object_scope = parser->object_scope;
3244       saved_qualifying_scope = parser->qualifying_scope;
3245       /* Process the final unqualified-id.  */
3246       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3247                                                  check_dependency_p,
3248                                                  declarator_p,
3249                                                  /*optional_p=*/false);
3250       /* Restore the SAVED_SCOPE for our caller.  */
3251       parser->scope = saved_scope;
3252       parser->object_scope = saved_object_scope;
3253       parser->qualifying_scope = saved_qualifying_scope;
3254
3255       return unqualified_id;
3256     }
3257   /* Otherwise, if we are in global scope, then we are looking at one
3258      of the other qualified-id productions.  */
3259   else if (global_scope_p)
3260     {
3261       cp_token *token;
3262       tree id;
3263
3264       /* Peek at the next token.  */
3265       token = cp_lexer_peek_token (parser->lexer);
3266
3267       /* If it's an identifier, and the next token is not a "<", then
3268          we can avoid the template-id case.  This is an optimization
3269          for this common case.  */
3270       if (token->type == CPP_NAME
3271           && !cp_parser_nth_token_starts_template_argument_list_p
3272                (parser, 2))
3273         return cp_parser_identifier (parser);
3274
3275       cp_parser_parse_tentatively (parser);
3276       /* Try a template-id.  */
3277       id = cp_parser_template_id (parser,
3278                                   /*template_keyword_p=*/false,
3279                                   /*check_dependency_p=*/true,
3280                                   declarator_p);
3281       /* If that worked, we're done.  */
3282       if (cp_parser_parse_definitely (parser))
3283         return id;
3284
3285       /* Peek at the next token.  (Changes in the token buffer may
3286          have invalidated the pointer obtained above.)  */
3287       token = cp_lexer_peek_token (parser->lexer);
3288
3289       switch (token->type)
3290         {
3291         case CPP_NAME:
3292           return cp_parser_identifier (parser);
3293
3294         case CPP_KEYWORD:
3295           if (token->keyword == RID_OPERATOR)
3296             return cp_parser_operator_function_id (parser);
3297           /* Fall through.  */
3298
3299         default:
3300           cp_parser_error (parser, "expected id-expression");
3301           return error_mark_node;
3302         }
3303     }
3304   else
3305     return cp_parser_unqualified_id (parser, template_keyword_p,
3306                                      /*check_dependency_p=*/true,
3307                                      declarator_p,
3308                                      optional_p);
3309 }
3310
3311 /* Parse an unqualified-id.
3312
3313    unqualified-id:
3314      identifier
3315      operator-function-id
3316      conversion-function-id
3317      ~ class-name
3318      template-id
3319
3320    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3321    keyword, in a construct like `A::template ...'.
3322
3323    Returns a representation of unqualified-id.  For the `identifier'
3324    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3325    production a BIT_NOT_EXPR is returned; the operand of the
3326    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3327    other productions, see the documentation accompanying the
3328    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3329    names are looked up in uninstantiated templates.  If DECLARATOR_P
3330    is true, the unqualified-id is appearing as part of a declarator,
3331    rather than as part of an expression.  */
3332
3333 static tree
3334 cp_parser_unqualified_id (cp_parser* parser,
3335                           bool template_keyword_p,
3336                           bool check_dependency_p,
3337                           bool declarator_p,
3338                           bool optional_p)
3339 {
3340   cp_token *token;
3341
3342   /* Peek at the next token.  */
3343   token = cp_lexer_peek_token (parser->lexer);
3344
3345   switch (token->type)
3346     {
3347     case CPP_NAME:
3348       {
3349         tree id;
3350
3351         /* We don't know yet whether or not this will be a
3352            template-id.  */
3353         cp_parser_parse_tentatively (parser);
3354         /* Try a template-id.  */
3355         id = cp_parser_template_id (parser, template_keyword_p,
3356                                     check_dependency_p,
3357                                     declarator_p);
3358         /* If it worked, we're done.  */
3359         if (cp_parser_parse_definitely (parser))
3360           return id;
3361         /* Otherwise, it's an ordinary identifier.  */
3362         return cp_parser_identifier (parser);
3363       }
3364
3365     case CPP_TEMPLATE_ID:
3366       return cp_parser_template_id (parser, template_keyword_p,
3367                                     check_dependency_p,
3368                                     declarator_p);
3369
3370     case CPP_COMPL:
3371       {
3372         tree type_decl;
3373         tree qualifying_scope;
3374         tree object_scope;
3375         tree scope;
3376         bool done;
3377
3378         /* Consume the `~' token.  */
3379         cp_lexer_consume_token (parser->lexer);
3380         /* Parse the class-name.  The standard, as written, seems to
3381            say that:
3382
3383              template <typename T> struct S { ~S (); };
3384              template <typename T> S<T>::~S() {}
3385
3386            is invalid, since `~' must be followed by a class-name, but
3387            `S<T>' is dependent, and so not known to be a class.
3388            That's not right; we need to look in uninstantiated
3389            templates.  A further complication arises from:
3390
3391              template <typename T> void f(T t) {
3392                t.T::~T();
3393              }
3394
3395            Here, it is not possible to look up `T' in the scope of `T'
3396            itself.  We must look in both the current scope, and the
3397            scope of the containing complete expression.
3398
3399            Yet another issue is:
3400
3401              struct S {
3402                int S;
3403                ~S();
3404              };
3405
3406              S::~S() {}
3407
3408            The standard does not seem to say that the `S' in `~S'
3409            should refer to the type `S' and not the data member
3410            `S::S'.  */
3411
3412         /* DR 244 says that we look up the name after the "~" in the
3413            same scope as we looked up the qualifying name.  That idea
3414            isn't fully worked out; it's more complicated than that.  */
3415         scope = parser->scope;
3416         object_scope = parser->object_scope;
3417         qualifying_scope = parser->qualifying_scope;
3418
3419         /* Check for invalid scopes.  */
3420         if (scope == error_mark_node)
3421           {
3422             cp_parser_skip_to_end_of_statement (parser);
3423             return error_mark_node;
3424           }
3425         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3426           {
3427             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3428               error ("scope %qT before %<~%> is not a class-name", scope);
3429             cp_parser_skip_to_end_of_statement (parser);
3430             return error_mark_node;
3431           }
3432         gcc_assert (!scope || TYPE_P (scope));
3433
3434         /* If the name is of the form "X::~X" it's OK.  */
3435         token = cp_lexer_peek_token (parser->lexer);
3436         if (scope
3437             && token->type == CPP_NAME
3438             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3439                 == CPP_OPEN_PAREN)
3440             && constructor_name_p (token->value, scope))
3441           {
3442             cp_lexer_consume_token (parser->lexer);
3443             return build_nt (BIT_NOT_EXPR, scope);
3444           }
3445
3446         /* If there was an explicit qualification (S::~T), first look
3447            in the scope given by the qualification (i.e., S).  */
3448         done = false;
3449         type_decl = NULL_TREE;
3450         if (scope)
3451           {
3452             cp_parser_parse_tentatively (parser);
3453             type_decl = cp_parser_class_name (parser,
3454                                               /*typename_keyword_p=*/false,
3455                                               /*template_keyword_p=*/false,
3456                                               none_type,
3457                                               /*check_dependency=*/false,
3458                                               /*class_head_p=*/false,
3459                                               declarator_p);
3460             if (cp_parser_parse_definitely (parser))
3461               done = true;
3462           }
3463         /* In "N::S::~S", look in "N" as well.  */
3464         if (!done && scope && qualifying_scope)
3465           {
3466             cp_parser_parse_tentatively (parser);
3467             parser->scope = qualifying_scope;
3468             parser->object_scope = NULL_TREE;
3469             parser->qualifying_scope = NULL_TREE;
3470             type_decl
3471               = cp_parser_class_name (parser,
3472                                       /*typename_keyword_p=*/false,
3473                                       /*template_keyword_p=*/false,
3474                                       none_type,
3475                                       /*check_dependency=*/false,
3476                                       /*class_head_p=*/false,
3477                                       declarator_p);
3478             if (cp_parser_parse_definitely (parser))
3479               done = true;
3480           }
3481         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3482         else if (!done && object_scope)
3483           {
3484             cp_parser_parse_tentatively (parser);
3485             parser->scope = object_scope;
3486             parser->object_scope = NULL_TREE;
3487             parser->qualifying_scope = NULL_TREE;
3488             type_decl
3489               = cp_parser_class_name (parser,
3490                                       /*typename_keyword_p=*/false,
3491                                       /*template_keyword_p=*/false,
3492                                       none_type,
3493                                       /*check_dependency=*/false,
3494                                       /*class_head_p=*/false,
3495                                       declarator_p);
3496             if (cp_parser_parse_definitely (parser))
3497               done = true;
3498           }
3499         /* Look in the surrounding context.  */
3500         if (!done)
3501           {
3502             parser->scope = NULL_TREE;
3503             parser->object_scope = NULL_TREE;
3504             parser->qualifying_scope = NULL_TREE;
3505             type_decl
3506               = cp_parser_class_name (parser,
3507                                       /*typename_keyword_p=*/false,
3508                                       /*template_keyword_p=*/false,
3509                                       none_type,
3510                                       /*check_dependency=*/false,
3511                                       /*class_head_p=*/false,
3512                                       declarator_p);
3513           }
3514         /* If an error occurred, assume that the name of the
3515            destructor is the same as the name of the qualifying
3516            class.  That allows us to keep parsing after running
3517            into ill-formed destructor names.  */
3518         if (type_decl == error_mark_node && scope)
3519           return build_nt (BIT_NOT_EXPR, scope);
3520         else if (type_decl == error_mark_node)
3521           return error_mark_node;
3522
3523         /* Check that destructor name and scope match.  */
3524         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3525           {
3526             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3527               error ("declaration of %<~%T%> as member of %qT",
3528                      type_decl, scope);
3529             return error_mark_node;
3530           }
3531
3532         /* [class.dtor]
3533
3534            A typedef-name that names a class shall not be used as the
3535            identifier in the declarator for a destructor declaration.  */
3536         if (declarator_p
3537             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3538             && !DECL_SELF_REFERENCE_P (type_decl)
3539             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3540           error ("typedef-name %qD used as destructor declarator",
3541                  type_decl);
3542
3543         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3544       }
3545
3546     case CPP_KEYWORD:
3547       if (token->keyword == RID_OPERATOR)
3548         {
3549           tree id;
3550
3551           /* This could be a template-id, so we try that first.  */
3552           cp_parser_parse_tentatively (parser);
3553           /* Try a template-id.  */
3554           id = cp_parser_template_id (parser, template_keyword_p,
3555                                       /*check_dependency_p=*/true,
3556                                       declarator_p);
3557           /* If that worked, we're done.  */
3558           if (cp_parser_parse_definitely (parser))
3559             return id;
3560           /* We still don't know whether we're looking at an
3561              operator-function-id or a conversion-function-id.  */
3562           cp_parser_parse_tentatively (parser);
3563           /* Try an operator-function-id.  */
3564           id = cp_parser_operator_function_id (parser);
3565           /* If that didn't work, try a conversion-function-id.  */
3566           if (!cp_parser_parse_definitely (parser))
3567             id = cp_parser_conversion_function_id (parser);
3568
3569           return id;
3570         }
3571       /* Fall through.  */
3572
3573     default:
3574       if (optional_p)
3575         return NULL_TREE;
3576       cp_parser_error (parser, "expected unqualified-id");
3577       return error_mark_node;
3578     }
3579 }
3580
3581 /* Parse an (optional) nested-name-specifier.
3582
3583    nested-name-specifier:
3584      class-or-namespace-name :: nested-name-specifier [opt]
3585      class-or-namespace-name :: template nested-name-specifier [opt]
3586
3587    PARSER->SCOPE should be set appropriately before this function is
3588    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3589    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3590    in name lookups.
3591
3592    Sets PARSER->SCOPE to the class (TYPE) or namespace
3593    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3594    it unchanged if there is no nested-name-specifier.  Returns the new
3595    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3596
3597    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3598    part of a declaration and/or decl-specifier.  */
3599
3600 static tree
3601 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3602                                      bool typename_keyword_p,
3603                                      bool check_dependency_p,
3604                                      bool type_p,
3605                                      bool is_declaration)
3606 {
3607   bool success = false;
3608   cp_token_position start = 0;
3609   cp_token *token;
3610
3611   /* Remember where the nested-name-specifier starts.  */
3612   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3613     {
3614       start = cp_lexer_token_position (parser->lexer, false);
3615       push_deferring_access_checks (dk_deferred);
3616     }
3617
3618   while (true)
3619     {
3620       tree new_scope;
3621       tree old_scope;
3622       tree saved_qualifying_scope;
3623       bool template_keyword_p;
3624
3625       /* Spot cases that cannot be the beginning of a
3626          nested-name-specifier.  */
3627       token = cp_lexer_peek_token (parser->lexer);
3628
3629       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3630          the already parsed nested-name-specifier.  */
3631       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3632         {
3633           /* Grab the nested-name-specifier and continue the loop.  */
3634           cp_parser_pre_parsed_nested_name_specifier (parser);
3635           success = true;
3636           continue;
3637         }
3638
3639       /* Spot cases that cannot be the beginning of a
3640          nested-name-specifier.  On the second and subsequent times
3641          through the loop, we look for the `template' keyword.  */
3642       if (success && token->keyword == RID_TEMPLATE)
3643         ;
3644       /* A template-id can start a nested-name-specifier.  */
3645       else if (token->type == CPP_TEMPLATE_ID)
3646         ;
3647       else
3648         {
3649           /* If the next token is not an identifier, then it is
3650              definitely not a class-or-namespace-name.  */
3651           if (token->type != CPP_NAME)
3652             break;
3653           /* If the following token is neither a `<' (to begin a
3654              template-id), nor a `::', then we are not looking at a
3655              nested-name-specifier.  */
3656           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3657           if (token->type != CPP_SCOPE
3658               && !cp_parser_nth_token_starts_template_argument_list_p
3659                   (parser, 2))
3660             break;
3661         }
3662
3663       /* The nested-name-specifier is optional, so we parse
3664          tentatively.  */
3665       cp_parser_parse_tentatively (parser);
3666
3667       /* Look for the optional `template' keyword, if this isn't the
3668          first time through the loop.  */
3669       if (success)
3670         template_keyword_p = cp_parser_optional_template_keyword (parser);
3671       else
3672         template_keyword_p = false;
3673
3674       /* Save the old scope since the name lookup we are about to do
3675          might destroy it.  */
3676       old_scope = parser->scope;
3677       saved_qualifying_scope = parser->qualifying_scope;
3678       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3679          look up names in "X<T>::I" in order to determine that "Y" is
3680          a template.  So, if we have a typename at this point, we make
3681          an effort to look through it.  */
3682       if (is_declaration
3683           && !typename_keyword_p
3684           && parser->scope
3685           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3686         parser->scope = resolve_typename_type (parser->scope,
3687                                                /*only_current_p=*/false);
3688       /* Parse the qualifying entity.  */
3689       new_scope
3690         = cp_parser_class_or_namespace_name (parser,
3691                                              typename_keyword_p,
3692                                              template_keyword_p,
3693                                              check_dependency_p,
3694                                              type_p,
3695                                              is_declaration);
3696       /* Look for the `::' token.  */
3697       cp_parser_require (parser, CPP_SCOPE, "`::'");
3698
3699       /* If we found what we wanted, we keep going; otherwise, we're
3700          done.  */
3701       if (!cp_parser_parse_definitely (parser))
3702         {
3703           bool error_p = false;
3704
3705           /* Restore the OLD_SCOPE since it was valid before the
3706              failed attempt at finding the last
3707              class-or-namespace-name.  */
3708           parser->scope = old_scope;
3709           parser->qualifying_scope = saved_qualifying_scope;
3710           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3711             break;
3712           /* If the next token is an identifier, and the one after
3713              that is a `::', then any valid interpretation would have
3714              found a class-or-namespace-name.  */
3715           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3716                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3717                      == CPP_SCOPE)
3718                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3719                      != CPP_COMPL))
3720             {
3721               token = cp_lexer_consume_token (parser->lexer);
3722               if (!error_p)
3723                 {
3724                   if (!token->ambiguous_p)
3725                     {
3726                       tree decl;
3727                       tree ambiguous_decls;
3728
3729                       decl = cp_parser_lookup_name (parser, token->value,
3730                                                     none_type,
3731                                                     /*is_template=*/false,
3732                                                     /*is_namespace=*/false,
3733                                                     /*check_dependency=*/true,
3734                                                     &ambiguous_decls);
3735                       if (TREE_CODE (decl) == TEMPLATE_DECL)
3736                         error ("%qD used without template parameters", decl);
3737                       else if (ambiguous_decls)
3738                         {
3739                           error ("reference to %qD is ambiguous",
3740                                  token->value);
3741                           print_candidates (ambiguous_decls);
3742                           decl = error_mark_node;
3743                         }
3744                       else
3745                         cp_parser_name_lookup_error
3746                           (parser, token->value, decl,
3747                            "is not a class or namespace");
3748                     }
3749                   parser->scope = error_mark_node;
3750                   error_p = true;
3751                   /* Treat this as a successful nested-name-specifier
3752                      due to:
3753
3754                      [basic.lookup.qual]
3755
3756                      If the name found is not a class-name (clause
3757                      _class_) or namespace-name (_namespace.def_), the
3758                      program is ill-formed.  */
3759                   success = true;
3760                 }
3761               cp_lexer_consume_token (parser->lexer);
3762             }
3763           break;
3764         }
3765       /* We've found one valid nested-name-specifier.  */
3766       success = true;
3767       /* Name lookup always gives us a DECL.  */
3768       if (TREE_CODE (new_scope) == TYPE_DECL)
3769         new_scope = TREE_TYPE (new_scope);
3770       /* Uses of "template" must be followed by actual templates.  */
3771       if (template_keyword_p
3772           && !(CLASS_TYPE_P (new_scope)
3773                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3774                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3775                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
3776           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3777                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3778                    == TEMPLATE_ID_EXPR)))
3779         pedwarn (TYPE_P (new_scope)
3780                  ? "%qT is not a template"
3781                  : "%qD is not a template",
3782                  new_scope);
3783       /* If it is a class scope, try to complete it; we are about to
3784          be looking up names inside the class.  */
3785       if (TYPE_P (new_scope)
3786           /* Since checking types for dependency can be expensive,
3787              avoid doing it if the type is already complete.  */
3788           && !COMPLETE_TYPE_P (new_scope)
3789           /* Do not try to complete dependent types.  */
3790           && !dependent_type_p (new_scope))
3791         new_scope = complete_type (new_scope);
3792       /* Make sure we look in the right scope the next time through
3793          the loop.  */
3794       parser->scope = new_scope;
3795     }
3796
3797   /* If parsing tentatively, replace the sequence of tokens that makes
3798      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3799      token.  That way, should we re-parse the token stream, we will
3800      not have to repeat the effort required to do the parse, nor will
3801      we issue duplicate error messages.  */
3802   if (success && start)
3803     {
3804       cp_token *token;
3805       tree access_checks;
3806
3807       token = cp_lexer_token_at (parser->lexer, start);
3808       /* Reset the contents of the START token.  */
3809       token->type = CPP_NESTED_NAME_SPECIFIER;
3810       /* Retrieve any deferred checks.  Do not pop this access checks yet
3811          so the memory will not be reclaimed during token replacing below.  */
3812       access_checks = get_deferred_access_checks ();
3813       token->value = build_tree_list (copy_list (access_checks),
3814                                       parser->scope);
3815       TREE_TYPE (token->value) = parser->qualifying_scope;
3816       token->keyword = RID_MAX;
3817
3818       /* Purge all subsequent tokens.  */
3819       cp_lexer_purge_tokens_after (parser->lexer, start);
3820     }
3821
3822   if (start)
3823     pop_to_parent_deferring_access_checks ();
3824
3825   return success ? parser->scope : NULL_TREE;
3826 }
3827
3828 /* Parse a nested-name-specifier.  See
3829    cp_parser_nested_name_specifier_opt for details.  This function
3830    behaves identically, except that it will an issue an error if no
3831    nested-name-specifier is present.  */
3832
3833 static tree
3834 cp_parser_nested_name_specifier (cp_parser *parser,
3835                                  bool typename_keyword_p,
3836                                  bool check_dependency_p,
3837                                  bool type_p,
3838                                  bool is_declaration)
3839 {
3840   tree scope;
3841
3842   /* Look for the nested-name-specifier.  */
3843   scope = cp_parser_nested_name_specifier_opt (parser,
3844                                                typename_keyword_p,
3845                                                check_dependency_p,
3846                                                type_p,
3847                                                is_declaration);
3848   /* If it was not present, issue an error message.  */
3849   if (!scope)
3850     {
3851       cp_parser_error (parser, "expected nested-name-specifier");
3852       parser->scope = NULL_TREE;
3853     }
3854
3855   return scope;
3856 }
3857
3858 /* Parse a class-or-namespace-name.
3859
3860    class-or-namespace-name:
3861      class-name
3862      namespace-name
3863
3864    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3865    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3866    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3867    TYPE_P is TRUE iff the next name should be taken as a class-name,
3868    even the same name is declared to be another entity in the same
3869    scope.
3870
3871    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3872    specified by the class-or-namespace-name.  If neither is found the
3873    ERROR_MARK_NODE is returned.  */
3874
3875 static tree
3876 cp_parser_class_or_namespace_name (cp_parser *parser,
3877                                    bool typename_keyword_p,
3878                                    bool template_keyword_p,
3879                                    bool check_dependency_p,
3880                                    bool type_p,
3881                                    bool is_declaration)
3882 {
3883   tree saved_scope;
3884   tree saved_qualifying_scope;
3885   tree saved_object_scope;
3886   tree scope;
3887   bool only_class_p;
3888
3889   /* Before we try to parse the class-name, we must save away the
3890      current PARSER->SCOPE since cp_parser_class_name will destroy
3891      it.  */
3892   saved_scope = parser->scope;
3893   saved_qualifying_scope = parser->qualifying_scope;
3894   saved_object_scope = parser->object_scope;
3895   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3896      there is no need to look for a namespace-name.  */
3897   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3898   if (!only_class_p)
3899     cp_parser_parse_tentatively (parser);
3900   scope = cp_parser_class_name (parser,
3901                                 typename_keyword_p,
3902                                 template_keyword_p,
3903                                 type_p ? class_type : none_type,
3904                                 check_dependency_p,
3905                                 /*class_head_p=*/false,
3906                                 is_declaration);
3907   /* If that didn't work, try for a namespace-name.  */
3908   if (!only_class_p && !cp_parser_parse_definitely (parser))
3909     {
3910       /* Restore the saved scope.  */
3911       parser->scope = saved_scope;
3912       parser->qualifying_scope = saved_qualifying_scope;
3913       parser->object_scope = saved_object_scope;
3914       /* If we are not looking at an identifier followed by the scope
3915          resolution operator, then this is not part of a
3916          nested-name-specifier.  (Note that this function is only used
3917          to parse the components of a nested-name-specifier.)  */
3918       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3919           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3920         return error_mark_node;
3921       scope = cp_parser_namespace_name (parser);
3922     }
3923
3924   return scope;
3925 }
3926
3927 /* Parse a postfix-expression.
3928
3929    postfix-expression:
3930      primary-expression
3931      postfix-expression [ expression ]
3932      postfix-expression ( expression-list [opt] )
3933      simple-type-specifier ( expression-list [opt] )
3934      typename :: [opt] nested-name-specifier identifier
3935        ( expression-list [opt] )
3936      typename :: [opt] nested-name-specifier template [opt] template-id
3937        ( expression-list [opt] )
3938      postfix-expression . template [opt] id-expression
3939      postfix-expression -> template [opt] id-expression
3940      postfix-expression . pseudo-destructor-name
3941      postfix-expression -> pseudo-destructor-name
3942      postfix-expression ++
3943      postfix-expression --
3944      dynamic_cast < type-id > ( expression )
3945      static_cast < type-id > ( expression )
3946      reinterpret_cast < type-id > ( expression )
3947      const_cast < type-id > ( expression )
3948      typeid ( expression )
3949      typeid ( type-id )
3950
3951    GNU Extension:
3952
3953    postfix-expression:
3954      ( type-id ) { initializer-list , [opt] }
3955
3956    This extension is a GNU version of the C99 compound-literal
3957    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3958    but they are essentially the same concept.)
3959
3960    If ADDRESS_P is true, the postfix expression is the operand of the
3961    `&' operator.  CAST_P is true if this expression is the target of a
3962    cast.
3963
3964    Returns a representation of the expression.  */
3965
3966 static tree
3967 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3968 {
3969   cp_token *token;
3970   enum rid keyword;
3971   cp_id_kind idk = CP_ID_KIND_NONE;
3972   tree postfix_expression = NULL_TREE;
3973
3974   /* Peek at the next token.  */
3975   token = cp_lexer_peek_token (parser->lexer);
3976   /* Some of the productions are determined by keywords.  */
3977   keyword = token->keyword;
3978   switch (keyword)
3979     {
3980     case RID_DYNCAST:
3981     case RID_STATCAST:
3982     case RID_REINTCAST:
3983     case RID_CONSTCAST:
3984       {
3985         tree type;
3986         tree expression;
3987         const char *saved_message;
3988
3989         /* All of these can be handled in the same way from the point
3990            of view of parsing.  Begin by consuming the token
3991            identifying the cast.  */
3992         cp_lexer_consume_token (parser->lexer);
3993
3994         /* New types cannot be defined in the cast.  */
3995         saved_message = parser->type_definition_forbidden_message;
3996         parser->type_definition_forbidden_message
3997           = "types may not be defined in casts";
3998
3999         /* Look for the opening `<'.  */
4000         cp_parser_require (parser, CPP_LESS, "`<'");
4001         /* Parse the type to which we are casting.  */
4002         type = cp_parser_type_id (parser);
4003         /* Look for the closing `>'.  */
4004         cp_parser_require (parser, CPP_GREATER, "`>'");
4005         /* Restore the old message.  */
4006         parser->type_definition_forbidden_message = saved_message;
4007
4008         /* And the expression which is being cast.  */
4009         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4010         expression = cp_parser_expression (parser, /*cast_p=*/true);
4011         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4012
4013         /* Only type conversions to integral or enumeration types
4014            can be used in constant-expressions.  */
4015         if (parser->integral_constant_expression_p
4016             && !dependent_type_p (type)
4017             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4018             && (cp_parser_non_integral_constant_expression
4019                 (parser,
4020                  "a cast to a type other than an integral or "
4021                  "enumeration type")))
4022           return error_mark_node;
4023
4024         switch (keyword)
4025           {
4026           case RID_DYNCAST:
4027             postfix_expression
4028               = build_dynamic_cast (type, expression);
4029             break;
4030           case RID_STATCAST:
4031             postfix_expression
4032               = build_static_cast (type, expression);
4033             break;
4034           case RID_REINTCAST:
4035             postfix_expression
4036               = build_reinterpret_cast (type, expression);
4037             break;
4038           case RID_CONSTCAST:
4039             postfix_expression
4040               = build_const_cast (type, expression);
4041             break;
4042           default:
4043             gcc_unreachable ();
4044           }
4045       }
4046       break;
4047
4048     case RID_TYPEID:
4049       {
4050         tree type;
4051         const char *saved_message;
4052         bool saved_in_type_id_in_expr_p;
4053
4054         /* Consume the `typeid' token.  */
4055         cp_lexer_consume_token (parser->lexer);
4056         /* Look for the `(' token.  */
4057         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4058         /* Types cannot be defined in a `typeid' expression.  */
4059         saved_message = parser->type_definition_forbidden_message;
4060         parser->type_definition_forbidden_message
4061           = "types may not be defined in a `typeid\' expression";
4062         /* We can't be sure yet whether we're looking at a type-id or an
4063            expression.  */
4064         cp_parser_parse_tentatively (parser);
4065         /* Try a type-id first.  */
4066         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4067         parser->in_type_id_in_expr_p = true;
4068         type = cp_parser_type_id (parser);
4069         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4070         /* Look for the `)' token.  Otherwise, we can't be sure that
4071            we're not looking at an expression: consider `typeid (int
4072            (3))', for example.  */
4073         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4074         /* If all went well, simply lookup the type-id.  */
4075         if (cp_parser_parse_definitely (parser))
4076           postfix_expression = get_typeid (type);
4077         /* Otherwise, fall back to the expression variant.  */
4078         else
4079           {
4080             tree expression;
4081
4082             /* Look for an expression.  */
4083             expression = cp_parser_expression (parser, /*cast_p=*/false);
4084             /* Compute its typeid.  */
4085             postfix_expression = build_typeid (expression);
4086             /* Look for the `)' token.  */
4087             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4088           }
4089         /* `typeid' may not appear in an integral constant expression.  */
4090         if (cp_parser_non_integral_constant_expression(parser,
4091                                                        "`typeid' operator"))
4092           return error_mark_node;
4093         /* Restore the saved message.  */
4094         parser->type_definition_forbidden_message = saved_message;
4095       }
4096       break;
4097
4098     case RID_TYPENAME:
4099       {
4100         tree type;
4101         /* The syntax permitted here is the same permitted for an
4102            elaborated-type-specifier.  */
4103         type = cp_parser_elaborated_type_specifier (parser,
4104                                                     /*is_friend=*/false,
4105                                                     /*is_declaration=*/false);
4106         postfix_expression = cp_parser_functional_cast (parser, type);
4107       }
4108       break;
4109
4110     default:
4111       {
4112         tree type;
4113
4114         /* If the next thing is a simple-type-specifier, we may be
4115            looking at a functional cast.  We could also be looking at
4116            an id-expression.  So, we try the functional cast, and if
4117            that doesn't work we fall back to the primary-expression.  */
4118         cp_parser_parse_tentatively (parser);
4119         /* Look for the simple-type-specifier.  */
4120         type = cp_parser_simple_type_specifier (parser,
4121                                                 /*decl_specs=*/NULL,
4122                                                 CP_PARSER_FLAGS_NONE);
4123         /* Parse the cast itself.  */
4124         if (!cp_parser_error_occurred (parser))
4125           postfix_expression
4126             = cp_parser_functional_cast (parser, type);
4127         /* If that worked, we're done.  */
4128         if (cp_parser_parse_definitely (parser))
4129           break;
4130
4131         /* If the functional-cast didn't work out, try a
4132            compound-literal.  */
4133         if (cp_parser_allow_gnu_extensions_p (parser)
4134             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4135           {
4136             VEC(constructor_elt,gc) *initializer_list = NULL;
4137             bool saved_in_type_id_in_expr_p;
4138
4139             cp_parser_parse_tentatively (parser);
4140             /* Consume the `('.  */
4141             cp_lexer_consume_token (parser->lexer);
4142             /* Parse the type.  */
4143             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4144             parser->in_type_id_in_expr_p = true;
4145             type = cp_parser_type_id (parser);
4146             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4147             /* Look for the `)'.  */
4148             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4149             /* Look for the `{'.  */
4150             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4151             /* If things aren't going well, there's no need to
4152                keep going.  */
4153             if (!cp_parser_error_occurred (parser))
4154               {
4155                 bool non_constant_p;
4156                 /* Parse the initializer-list.  */
4157                 initializer_list
4158                   = cp_parser_initializer_list (parser, &non_constant_p);
4159                 /* Allow a trailing `,'.  */
4160                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4161                   cp_lexer_consume_token (parser->lexer);
4162                 /* Look for the final `}'.  */
4163                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4164               }
4165             /* If that worked, we're definitely looking at a
4166                compound-literal expression.  */
4167             if (cp_parser_parse_definitely (parser))
4168               {
4169                 /* Warn the user that a compound literal is not
4170                    allowed in standard C++.  */
4171                 if (pedantic)
4172                   pedwarn ("ISO C++ forbids compound-literals");
4173                 /* Form the representation of the compound-literal.  */
4174                 postfix_expression
4175                   = finish_compound_literal (type, initializer_list);
4176                 break;
4177               }
4178           }
4179
4180         /* It must be a primary-expression.  */
4181         postfix_expression
4182           = cp_parser_primary_expression (parser, address_p, cast_p,
4183                                           /*template_arg_p=*/false,
4184                                           &idk);
4185       }
4186       break;
4187     }
4188
4189   /* Keep looping until the postfix-expression is complete.  */
4190   while (true)
4191     {
4192       if (idk == CP_ID_KIND_UNQUALIFIED
4193           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4194           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4195         /* It is not a Koenig lookup function call.  */
4196         postfix_expression
4197           = unqualified_name_lookup_error (postfix_expression);
4198
4199       /* Peek at the next token.  */
4200       token = cp_lexer_peek_token (parser->lexer);
4201
4202       switch (token->type)
4203         {
4204         case CPP_OPEN_SQUARE:
4205           postfix_expression
4206             = cp_parser_postfix_open_square_expression (parser,
4207                                                         postfix_expression,
4208                                                         false);
4209           idk = CP_ID_KIND_NONE;
4210           break;
4211
4212         case CPP_OPEN_PAREN:
4213           /* postfix-expression ( expression-list [opt] ) */
4214           {
4215             bool koenig_p;
4216             bool is_builtin_constant_p;
4217             bool saved_integral_constant_expression_p = false;
4218             bool saved_non_integral_constant_expression_p = false;
4219             tree args;
4220
4221             is_builtin_constant_p
4222               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4223             if (is_builtin_constant_p)
4224               {
4225                 /* The whole point of __builtin_constant_p is to allow
4226                    non-constant expressions to appear as arguments.  */
4227                 saved_integral_constant_expression_p
4228                   = parser->integral_constant_expression_p;
4229                 saved_non_integral_constant_expression_p
4230                   = parser->non_integral_constant_expression_p;
4231                 parser->integral_constant_expression_p = false;
4232               }
4233             args = (cp_parser_parenthesized_expression_list
4234                     (parser, /*is_attribute_list=*/false,
4235                      /*cast_p=*/false,
4236                      /*non_constant_p=*/NULL));
4237             if (is_builtin_constant_p)
4238               {
4239                 parser->integral_constant_expression_p
4240                   = saved_integral_constant_expression_p;
4241                 parser->non_integral_constant_expression_p
4242                   = saved_non_integral_constant_expression_p;
4243               }
4244
4245             if (args == error_mark_node)
4246               {
4247                 postfix_expression = error_mark_node;
4248                 break;
4249               }
4250
4251             /* Function calls are not permitted in
4252                constant-expressions.  */
4253             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4254                 && cp_parser_non_integral_constant_expression (parser,
4255                                                                "a function call"))
4256               {
4257                 postfix_expression = error_mark_node;
4258                 break;
4259               }
4260
4261             koenig_p = false;
4262             if (idk == CP_ID_KIND_UNQUALIFIED)
4263               {
4264                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4265                   {
4266                     if (args)
4267                       {
4268                         koenig_p = true;
4269                         postfix_expression
4270                           = perform_koenig_lookup (postfix_expression, args);
4271                       }
4272                     else
4273                       postfix_expression
4274                         = unqualified_fn_lookup_error (postfix_expression);
4275                   }
4276                 /* We do not perform argument-dependent lookup if
4277                    normal lookup finds a non-function, in accordance
4278                    with the expected resolution of DR 218.  */
4279                 else if (args && is_overloaded_fn (postfix_expression))
4280                   {
4281                     tree fn = get_first_fn (postfix_expression);
4282
4283                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4284                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4285
4286                     /* Only do argument dependent lookup if regular
4287                        lookup does not find a set of member functions.
4288                        [basic.lookup.koenig]/2a  */
4289                     if (!DECL_FUNCTION_MEMBER_P (fn))
4290                       {
4291                         koenig_p = true;
4292                         postfix_expression
4293                           = perform_koenig_lookup (postfix_expression, args);
4294                       }
4295                   }
4296               }
4297
4298             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4299               {
4300                 tree instance = TREE_OPERAND (postfix_expression, 0);
4301                 tree fn = TREE_OPERAND (postfix_expression, 1);
4302
4303                 if (processing_template_decl
4304                     && (type_dependent_expression_p (instance)
4305                         || (!BASELINK_P (fn)
4306                             && TREE_CODE (fn) != FIELD_DECL)
4307                         || type_dependent_expression_p (fn)
4308                         || any_type_dependent_arguments_p (args)))
4309                   {
4310                     postfix_expression
4311                       = build_min_nt (CALL_EXPR, postfix_expression,
4312                                       args, NULL_TREE);
4313                     break;
4314                   }
4315
4316                 if (BASELINK_P (fn))
4317                   postfix_expression
4318                     = (build_new_method_call
4319                        (instance, fn, args, NULL_TREE,
4320                         (idk == CP_ID_KIND_QUALIFIED
4321                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4322                         /*fn_p=*/NULL));
4323                 else
4324                   postfix_expression
4325                     = finish_call_expr (postfix_expression, args,
4326                                         /*disallow_virtual=*/false,
4327                                         /*koenig_p=*/false);
4328               }
4329             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4330                      || TREE_CODE (postfix_expression) == MEMBER_REF
4331                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4332               postfix_expression = (build_offset_ref_call_from_tree
4333                                     (postfix_expression, args));
4334             else if (idk == CP_ID_KIND_QUALIFIED)
4335               /* A call to a static class member, or a namespace-scope
4336                  function.  */
4337               postfix_expression
4338                 = finish_call_expr (postfix_expression, args,
4339                                     /*disallow_virtual=*/true,
4340                                     koenig_p);
4341             else
4342               /* All other function calls.  */
4343               postfix_expression
4344                 = finish_call_expr (postfix_expression, args,
4345                                     /*disallow_virtual=*/false,
4346                                     koenig_p);
4347
4348             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4349             idk = CP_ID_KIND_NONE;
4350           }
4351           break;
4352
4353         case CPP_DOT:
4354         case CPP_DEREF:
4355           /* postfix-expression . template [opt] id-expression
4356              postfix-expression . pseudo-destructor-name
4357              postfix-expression -> template [opt] id-expression
4358              postfix-expression -> pseudo-destructor-name */
4359
4360           /* Consume the `.' or `->' operator.  */
4361           cp_lexer_consume_token (parser->lexer);
4362
4363           postfix_expression
4364             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4365                                                       postfix_expression,
4366                                                       false, &idk);
4367           break;
4368
4369         case CPP_PLUS_PLUS:
4370           /* postfix-expression ++  */
4371           /* Consume the `++' token.  */
4372           cp_lexer_consume_token (parser->lexer);
4373           /* Generate a representation for the complete expression.  */
4374           postfix_expression
4375             = finish_increment_expr (postfix_expression,
4376                                      POSTINCREMENT_EXPR);
4377           /* Increments may not appear in constant-expressions.  */
4378           if (cp_parser_non_integral_constant_expression (parser,
4379                                                           "an increment"))
4380             postfix_expression = error_mark_node;
4381           idk = CP_ID_KIND_NONE;
4382           break;
4383
4384         case CPP_MINUS_MINUS:
4385           /* postfix-expression -- */
4386           /* Consume the `--' token.  */
4387           cp_lexer_consume_token (parser->lexer);
4388           /* Generate a representation for the complete expression.  */
4389           postfix_expression
4390             = finish_increment_expr (postfix_expression,
4391                                      POSTDECREMENT_EXPR);
4392           /* Decrements may not appear in constant-expressions.  */
4393           if (cp_parser_non_integral_constant_expression (parser,
4394                                                           "a decrement"))
4395             postfix_expression = error_mark_node;
4396           idk = CP_ID_KIND_NONE;
4397           break;
4398
4399         default:
4400           return postfix_expression;
4401         }
4402     }
4403
4404   /* We should never get here.  */
4405   gcc_unreachable ();
4406   return error_mark_node;
4407 }
4408
4409 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4410    by cp_parser_builtin_offsetof.  We're looking for
4411
4412      postfix-expression [ expression ]
4413
4414    FOR_OFFSETOF is set if we're being called in that context, which
4415    changes how we deal with integer constant expressions.  */
4416
4417 static tree
4418 cp_parser_postfix_open_square_expression (cp_parser *parser,
4419                                           tree postfix_expression,
4420                                           bool for_offsetof)
4421 {
4422   tree index;
4423
4424   /* Consume the `[' token.  */
4425   cp_lexer_consume_token (parser->lexer);
4426
4427   /* Parse the index expression.  */
4428   /* ??? For offsetof, there is a question of what to allow here.  If
4429      offsetof is not being used in an integral constant expression context,
4430      then we *could* get the right answer by computing the value at runtime.
4431      If we are in an integral constant expression context, then we might
4432      could accept any constant expression; hard to say without analysis.
4433      Rather than open the barn door too wide right away, allow only integer
4434      constant expressions here.  */
4435   if (for_offsetof)
4436     index = cp_parser_constant_expression (parser, false, NULL);
4437   else
4438     index = cp_parser_expression (parser, /*cast_p=*/false);
4439
4440   /* Look for the closing `]'.  */
4441   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4442
4443   /* Build the ARRAY_REF.  */
4444   postfix_expression = grok_array_decl (postfix_expression, index);
4445
4446   /* When not doing offsetof, array references are not permitted in
4447      constant-expressions.  */
4448   if (!for_offsetof
4449       && (cp_parser_non_integral_constant_expression
4450           (parser, "an array reference")))
4451     postfix_expression = error_mark_node;
4452
4453   return postfix_expression;
4454 }
4455
4456 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4457    by cp_parser_builtin_offsetof.  We're looking for
4458
4459      postfix-expression . template [opt] id-expression
4460      postfix-expression . pseudo-destructor-name
4461      postfix-expression -> template [opt] id-expression
4462      postfix-expression -> pseudo-destructor-name
4463
4464    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4465    limits what of the above we'll actually accept, but nevermind.
4466    TOKEN_TYPE is the "." or "->" token, which will already have been
4467    removed from the stream.  */
4468
4469 static tree
4470 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4471                                         enum cpp_ttype token_type,
4472                                         tree postfix_expression,
4473                                         bool for_offsetof, cp_id_kind *idk)
4474 {
4475   tree name;
4476   bool dependent_p;
4477   bool pseudo_destructor_p;
4478   tree scope = NULL_TREE;
4479
4480   /* If this is a `->' operator, dereference the pointer.  */
4481   if (token_type == CPP_DEREF)
4482     postfix_expression = build_x_arrow (postfix_expression);
4483   /* Check to see whether or not the expression is type-dependent.  */
4484   dependent_p = type_dependent_expression_p (postfix_expression);
4485   /* The identifier following the `->' or `.' is not qualified.  */
4486   parser->scope = NULL_TREE;
4487   parser->qualifying_scope = NULL_TREE;
4488   parser->object_scope = NULL_TREE;
4489   *idk = CP_ID_KIND_NONE;
4490   /* Enter the scope corresponding to the type of the object
4491      given by the POSTFIX_EXPRESSION.  */
4492   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4493     {
4494       scope = TREE_TYPE (postfix_expression);
4495       /* According to the standard, no expression should ever have
4496          reference type.  Unfortunately, we do not currently match
4497          the standard in this respect in that our internal representation
4498          of an expression may have reference type even when the standard
4499          says it does not.  Therefore, we have to manually obtain the
4500          underlying type here.  */
4501       scope = non_reference (scope);
4502       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4503       if (scope == unknown_type_node)
4504         {
4505           error ("%qE does not have class type", postfix_expression);
4506           scope = NULL_TREE;
4507         }
4508       else
4509         scope = complete_type_or_else (scope, NULL_TREE);
4510       /* Let the name lookup machinery know that we are processing a
4511          class member access expression.  */
4512       parser->context->object_type = scope;
4513       /* If something went wrong, we want to be able to discern that case,
4514          as opposed to the case where there was no SCOPE due to the type
4515          of expression being dependent.  */
4516       if (!scope)
4517         scope = error_mark_node;
4518       /* If the SCOPE was erroneous, make the various semantic analysis
4519          functions exit quickly -- and without issuing additional error
4520          messages.  */
4521       if (scope == error_mark_node)
4522         postfix_expression = error_mark_node;
4523     }
4524
4525   /* Assume this expression is not a pseudo-destructor access.  */
4526   pseudo_destructor_p = false;
4527
4528   /* If the SCOPE is a scalar type, then, if this is a valid program,
4529      we must be looking at a pseudo-destructor-name.  */
4530   if (scope && SCALAR_TYPE_P (scope))
4531     {
4532       tree s;
4533       tree type;
4534
4535       cp_parser_parse_tentatively (parser);
4536       /* Parse the pseudo-destructor-name.  */
4537       s = NULL_TREE;
4538       cp_parser_pseudo_destructor_name (parser, &s, &type);
4539       if (cp_parser_parse_definitely (parser))
4540         {
4541           pseudo_destructor_p = true;
4542           postfix_expression
4543             = finish_pseudo_destructor_expr (postfix_expression,
4544                                              s, TREE_TYPE (type));
4545         }
4546     }
4547
4548   if (!pseudo_destructor_p)
4549     {
4550       /* If the SCOPE is not a scalar type, we are looking at an
4551          ordinary class member access expression, rather than a
4552          pseudo-destructor-name.  */
4553       bool template_p;
4554       /* Parse the id-expression.  */
4555       name = (cp_parser_id_expression
4556               (parser,
4557                cp_parser_optional_template_keyword (parser),
4558                /*check_dependency_p=*/true,
4559                &template_p,
4560                /*declarator_p=*/false,
4561                /*optional_p=*/false));
4562       /* In general, build a SCOPE_REF if the member name is qualified.
4563          However, if the name was not dependent and has already been
4564          resolved; there is no need to build the SCOPE_REF.  For example;
4565
4566              struct X { void f(); };
4567              template <typename T> void f(T* t) { t->X::f(); }
4568
4569          Even though "t" is dependent, "X::f" is not and has been resolved
4570          to a BASELINK; there is no need to include scope information.  */
4571
4572       /* But we do need to remember that there was an explicit scope for
4573          virtual function calls.  */
4574       if (parser->scope)
4575         *idk = CP_ID_KIND_QUALIFIED;
4576
4577       /* If the name is a template-id that names a type, we will get a
4578          TYPE_DECL here.  That is invalid code.  */
4579       if (TREE_CODE (name) == TYPE_DECL)
4580         {
4581           error ("invalid use of %qD", name);
4582           postfix_expression = error_mark_node;
4583         }
4584       else
4585         {
4586           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4587             {
4588               name = build_qualified_name (/*type=*/NULL_TREE,
4589                                            parser->scope,
4590                                            name,
4591                                            template_p);
4592               parser->scope = NULL_TREE;
4593               parser->qualifying_scope = NULL_TREE;
4594               parser->object_scope = NULL_TREE;
4595             }
4596           if (scope && name && BASELINK_P (name))
4597             adjust_result_of_qualified_name_lookup
4598               (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4599           postfix_expression
4600             = finish_class_member_access_expr (postfix_expression, name,
4601                                                template_p);
4602         }
4603     }
4604
4605   /* We no longer need to look up names in the scope of the object on
4606      the left-hand side of the `.' or `->' operator.  */
4607   parser->context->object_type = NULL_TREE;
4608
4609   /* Outside of offsetof, these operators may not appear in
4610      constant-expressions.  */
4611   if (!for_offsetof
4612       && (cp_parser_non_integral_constant_expression
4613           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4614     postfix_expression = error_mark_node;
4615
4616   return postfix_expression;
4617 }
4618
4619 /* Parse a parenthesized expression-list.
4620
4621    expression-list:
4622      assignment-expression
4623      expression-list, assignment-expression
4624
4625    attribute-list:
4626      expression-list
4627      identifier
4628      identifier, expression-list
4629
4630    CAST_P is true if this expression is the target of a cast.
4631
4632    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4633    representation of an assignment-expression.  Note that a TREE_LIST
4634    is returned even if there is only a single expression in the list.
4635    error_mark_node is returned if the ( and or ) are
4636    missing. NULL_TREE is returned on no expressions. The parentheses
4637    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4638    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4639    indicates whether or not all of the expressions in the list were
4640    constant.  */
4641
4642 static tree
4643 cp_parser_parenthesized_expression_list (cp_parser* parser,
4644                                          bool is_attribute_list,
4645                                          bool cast_p,
4646                                          bool *non_constant_p)
4647 {
4648   tree expression_list = NULL_TREE;
4649   bool fold_expr_p = is_attribute_list;
4650   tree identifier = NULL_TREE;
4651
4652   /* Assume all the expressions will be constant.  */
4653   if (non_constant_p)
4654     *non_constant_p = false;
4655
4656   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4657     return error_mark_node;
4658
4659   /* Consume expressions until there are no more.  */
4660   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4661     while (true)
4662       {
4663         tree expr;
4664
4665         /* At the beginning of attribute lists, check to see if the
4666            next token is an identifier.  */
4667         if (is_attribute_list
4668             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4669           {
4670             cp_token *token;
4671
4672             /* Consume the identifier.  */
4673             token = cp_lexer_consume_token (parser->lexer);
4674             /* Save the identifier.  */
4675             identifier = token->value;
4676           }
4677         else
4678           {
4679             /* Parse the next assignment-expression.  */
4680             if (non_constant_p)
4681               {
4682                 bool expr_non_constant_p;
4683                 expr = (cp_parser_constant_expression
4684                         (parser, /*allow_non_constant_p=*/true,
4685                          &expr_non_constant_p));
4686                 if (expr_non_constant_p)
4687                   *non_constant_p = true;
4688               }
4689             else
4690               expr = cp_parser_assignment_expression (parser, cast_p);
4691
4692             if (fold_expr_p)
4693               expr = fold_non_dependent_expr (expr);
4694
4695              /* Add it to the list.  We add error_mark_node
4696                 expressions to the list, so that we can still tell if
4697                 the correct form for a parenthesized expression-list
4698                 is found. That gives better errors.  */
4699             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4700
4701             if (expr == error_mark_node)
4702               goto skip_comma;
4703           }
4704
4705         /* After the first item, attribute lists look the same as
4706            expression lists.  */
4707         is_attribute_list = false;
4708
4709       get_comma:;
4710         /* If the next token isn't a `,', then we are done.  */
4711         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4712           break;
4713
4714         /* Otherwise, consume the `,' and keep going.  */
4715         cp_lexer_consume_token (parser->lexer);
4716       }
4717
4718   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4719     {
4720       int ending;
4721
4722     skip_comma:;
4723       /* We try and resync to an unnested comma, as that will give the
4724          user better diagnostics.  */
4725       ending = cp_parser_skip_to_closing_parenthesis (parser,
4726                                                       /*recovering=*/true,
4727                                                       /*or_comma=*/true,
4728                                                       /*consume_paren=*/true);
4729       if (ending < 0)
4730         goto get_comma;
4731       if (!ending)
4732         return error_mark_node;
4733     }
4734
4735   /* We built up the list in reverse order so we must reverse it now.  */
4736   expression_list = nreverse (expression_list);
4737   if (identifier)
4738     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4739
4740   return expression_list;
4741 }
4742
4743 /* Parse a pseudo-destructor-name.
4744
4745    pseudo-destructor-name:
4746      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4747      :: [opt] nested-name-specifier template template-id :: ~ type-name
4748      :: [opt] nested-name-specifier [opt] ~ type-name
4749
4750    If either of the first two productions is used, sets *SCOPE to the
4751    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4752    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4753    or ERROR_MARK_NODE if the parse fails.  */
4754
4755 static void
4756 cp_parser_pseudo_destructor_name (cp_parser* parser,
4757                                   tree* scope,
4758                                   tree* type)
4759 {
4760   bool nested_name_specifier_p;
4761
4762   /* Assume that things will not work out.  */
4763   *type = error_mark_node;
4764
4765   /* Look for the optional `::' operator.  */
4766   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4767   /* Look for the optional nested-name-specifier.  */
4768   nested_name_specifier_p
4769     = (cp_parser_nested_name_specifier_opt (parser,
4770                                             /*typename_keyword_p=*/false,
4771                                             /*check_dependency_p=*/true,
4772                                             /*type_p=*/false,
4773                                             /*is_declaration=*/true)
4774        != NULL_TREE);
4775   /* Now, if we saw a nested-name-specifier, we might be doing the
4776      second production.  */
4777   if (nested_name_specifier_p
4778       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4779     {
4780       /* Consume the `template' keyword.  */
4781       cp_lexer_consume_token (parser->lexer);
4782       /* Parse the template-id.  */
4783       cp_parser_template_id (parser,
4784                              /*template_keyword_p=*/true,
4785                              /*check_dependency_p=*/false,
4786                              /*is_declaration=*/true);
4787       /* Look for the `::' token.  */
4788       cp_parser_require (parser, CPP_SCOPE, "`::'");
4789     }
4790   /* If the next token is not a `~', then there might be some
4791      additional qualification.  */
4792   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4793     {
4794       /* Look for the type-name.  */
4795       *scope = TREE_TYPE (cp_parser_type_name (parser));
4796
4797       if (*scope == error_mark_node)
4798         return;
4799
4800       /* If we don't have ::~, then something has gone wrong.  Since
4801          the only caller of this function is looking for something
4802          after `.' or `->' after a scalar type, most likely the
4803          program is trying to get a member of a non-aggregate
4804          type.  */
4805       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4806           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4807         {
4808           cp_parser_error (parser, "request for member of non-aggregate type");
4809           return;
4810         }
4811
4812       /* Look for the `::' token.  */
4813       cp_parser_require (parser, CPP_SCOPE, "`::'");
4814     }
4815   else
4816     *scope = NULL_TREE;
4817
4818   /* Look for the `~'.  */
4819   cp_parser_require (parser, CPP_COMPL, "`~'");
4820   /* Look for the type-name again.  We are not responsible for
4821      checking that it matches the first type-name.  */
4822   *type = cp_parser_type_name (parser);
4823 }
4824
4825 /* Parse a unary-expression.
4826
4827    unary-expression:
4828      postfix-expression
4829      ++ cast-expression
4830      -- cast-expression
4831      unary-operator cast-expression
4832      sizeof unary-expression
4833      sizeof ( type-id )
4834      new-expression
4835      delete-expression
4836
4837    GNU Extensions:
4838
4839    unary-expression:
4840      __extension__ cast-expression
4841      __alignof__ unary-expression
4842      __alignof__ ( type-id )
4843      __real__ cast-expression
4844      __imag__ cast-expression
4845      && identifier
4846
4847    ADDRESS_P is true iff the unary-expression is appearing as the
4848    operand of the `&' operator.   CAST_P is true if this expression is
4849    the target of a cast.
4850
4851    Returns a representation of the expression.  */
4852
4853 static tree
4854 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4855 {
4856   cp_token *token;
4857   enum tree_code unary_operator;
4858
4859   /* Peek at the next token.  */
4860   token = cp_lexer_peek_token (parser->lexer);
4861   /* Some keywords give away the kind of expression.  */
4862   if (token->type == CPP_KEYWORD)
4863     {
4864       enum rid keyword = token->keyword;
4865
4866       switch (keyword)
4867         {
4868         case RID_ALIGNOF:
4869         case RID_SIZEOF:
4870           {
4871             tree operand;
4872             enum tree_code op;
4873
4874             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4875             /* Consume the token.  */
4876             cp_lexer_consume_token (parser->lexer);
4877             /* Parse the operand.  */
4878             operand = cp_parser_sizeof_operand (parser, keyword);
4879
4880             if (TYPE_P (operand))
4881               return cxx_sizeof_or_alignof_type (operand, op, true);
4882             else
4883               return cxx_sizeof_or_alignof_expr (operand, op);
4884           }
4885
4886         case RID_NEW:
4887           return cp_parser_new_expression (parser);
4888
4889         case RID_DELETE:
4890           return cp_parser_delete_expression (parser);
4891
4892         case RID_EXTENSION:
4893           {
4894             /* The saved value of the PEDANTIC flag.  */
4895             int saved_pedantic;
4896             tree expr;
4897
4898             /* Save away the PEDANTIC flag.  */
4899             cp_parser_extension_opt (parser, &saved_pedantic);
4900             /* Parse the cast-expression.  */
4901             expr = cp_parser_simple_cast_expression (parser);
4902             /* Restore the PEDANTIC flag.  */
4903             pedantic = saved_pedantic;
4904
4905             return expr;
4906           }
4907
4908         case RID_REALPART:
4909         case RID_IMAGPART:
4910           {
4911             tree expression;
4912
4913             /* Consume the `__real__' or `__imag__' token.  */
4914             cp_lexer_consume_token (parser->lexer);
4915             /* Parse the cast-expression.  */
4916             expression = cp_parser_simple_cast_expression (parser);
4917             /* Create the complete representation.  */
4918             return build_x_unary_op ((keyword == RID_REALPART
4919                                       ? REALPART_EXPR : IMAGPART_EXPR),
4920                                      expression);
4921           }
4922           break;
4923
4924         default:
4925           break;
4926         }
4927     }
4928
4929   /* Look for the `:: new' and `:: delete', which also signal the
4930      beginning of a new-expression, or delete-expression,
4931      respectively.  If the next token is `::', then it might be one of
4932      these.  */
4933   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4934     {
4935       enum rid keyword;
4936
4937       /* See if the token after the `::' is one of the keywords in
4938          which we're interested.  */
4939       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4940       /* If it's `new', we have a new-expression.  */
4941       if (keyword == RID_NEW)
4942         return cp_parser_new_expression (parser);
4943       /* Similarly, for `delete'.  */
4944       else if (keyword == RID_DELETE)
4945         return cp_parser_delete_expression (parser);
4946     }
4947
4948   /* Look for a unary operator.  */
4949   unary_operator = cp_parser_unary_operator (token);
4950   /* The `++' and `--' operators can be handled similarly, even though
4951      they are not technically unary-operators in the grammar.  */
4952   if (unary_operator == ERROR_MARK)
4953     {
4954       if (token->type == CPP_PLUS_PLUS)
4955         unary_operator = PREINCREMENT_EXPR;
4956       else if (token->type == CPP_MINUS_MINUS)
4957         unary_operator = PREDECREMENT_EXPR;
4958       /* Handle the GNU address-of-label extension.  */
4959       else if (cp_parser_allow_gnu_extensions_p (parser)
4960                && token->type == CPP_AND_AND)
4961         {
4962           tree identifier;
4963
4964           /* Consume the '&&' token.  */
4965           cp_lexer_consume_token (parser->lexer);
4966           /* Look for the identifier.  */
4967           identifier = cp_parser_identifier (parser);
4968           /* Create an expression representing the address.  */
4969           return finish_label_address_expr (identifier);
4970         }
4971     }
4972   if (unary_operator != ERROR_MARK)
4973     {
4974       tree cast_expression;
4975       tree expression = error_mark_node;
4976       const char *non_constant_p = NULL;
4977
4978       /* Consume the operator token.  */
4979       token = cp_lexer_consume_token (parser->lexer);
4980       /* Parse the cast-expression.  */
4981       cast_expression
4982         = cp_parser_cast_expression (parser,
4983                                      unary_operator == ADDR_EXPR,
4984                                      /*cast_p=*/false);
4985       /* Now, build an appropriate representation.  */
4986       switch (unary_operator)
4987         {
4988         case INDIRECT_REF:
4989           non_constant_p = "`*'";
4990           expression = build_x_indirect_ref (cast_expression, "unary *");
4991           break;
4992
4993         case ADDR_EXPR:
4994           non_constant_p = "`&'";
4995           /* Fall through.  */
4996         case BIT_NOT_EXPR:
4997           expression = build_x_unary_op (unary_operator, cast_expression);
4998           break;
4999
5000         case PREINCREMENT_EXPR:
5001         case PREDECREMENT_EXPR:
5002           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5003                             ? "`++'" : "`--'");
5004           /* Fall through.  */
5005         case UNARY_PLUS_EXPR:
5006         case NEGATE_EXPR:
5007         case TRUTH_NOT_EXPR:
5008           expression = finish_unary_op_expr (unary_operator, cast_expression);
5009           break;
5010
5011         default:
5012           gcc_unreachable ();
5013         }
5014
5015       if (non_constant_p
5016           && cp_parser_non_integral_constant_expression (parser,
5017                                                          non_constant_p))
5018         expression = error_mark_node;
5019
5020       return expression;
5021     }
5022
5023   return cp_parser_postfix_expression (parser, address_p, cast_p);
5024 }
5025
5026 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5027    unary-operator, the corresponding tree code is returned.  */
5028
5029 static enum tree_code
5030 cp_parser_unary_operator (cp_token* token)
5031 {
5032   switch (token->type)
5033     {
5034     case CPP_MULT:
5035       return INDIRECT_REF;
5036
5037     case CPP_AND:
5038       return ADDR_EXPR;
5039
5040     case CPP_PLUS:
5041       return UNARY_PLUS_EXPR;
5042
5043     case CPP_MINUS:
5044       return NEGATE_EXPR;
5045
5046     case CPP_NOT:
5047       return TRUTH_NOT_EXPR;
5048
5049     case CPP_COMPL:
5050       return BIT_NOT_EXPR;
5051
5052     default:
5053       return ERROR_MARK;
5054     }
5055 }
5056
5057 /* Parse a new-expression.
5058
5059    new-expression:
5060      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5061      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5062
5063    Returns a representation of the expression.  */
5064
5065 static tree
5066 cp_parser_new_expression (cp_parser* parser)
5067 {
5068   bool global_scope_p;
5069   tree placement;
5070   tree type;
5071   tree initializer;
5072   tree nelts;
5073
5074   /* Look for the optional `::' operator.  */
5075   global_scope_p
5076     = (cp_parser_global_scope_opt (parser,
5077                                    /*current_scope_valid_p=*/false)
5078        != NULL_TREE);
5079   /* Look for the `new' operator.  */
5080   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5081   /* There's no easy way to tell a new-placement from the
5082      `( type-id )' construct.  */
5083   cp_parser_parse_tentatively (parser);
5084   /* Look for a new-placement.  */
5085   placement = cp_parser_new_placement (parser);
5086   /* If that didn't work out, there's no new-placement.  */
5087   if (!cp_parser_parse_definitely (parser))
5088     placement = NULL_TREE;
5089
5090   /* If the next token is a `(', then we have a parenthesized
5091      type-id.  */
5092   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5093     {
5094       /* Consume the `('.  */
5095       cp_lexer_consume_token (parser->lexer);
5096       /* Parse the type-id.  */
5097       type = cp_parser_type_id (parser);
5098       /* Look for the closing `)'.  */
5099       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5100       /* There should not be a direct-new-declarator in this production,
5101          but GCC used to allowed this, so we check and emit a sensible error
5102          message for this case.  */
5103       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5104         {
5105           error ("array bound forbidden after parenthesized type-id");
5106           inform ("try removing the parentheses around the type-id");
5107           cp_parser_direct_new_declarator (parser);
5108         }
5109       nelts = NULL_TREE;
5110     }
5111   /* Otherwise, there must be a new-type-id.  */
5112   else
5113     type = cp_parser_new_type_id (parser, &nelts);
5114
5115   /* If the next token is a `(', then we have a new-initializer.  */
5116   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5117     initializer = cp_parser_new_initializer (parser);
5118   else
5119     initializer = NULL_TREE;
5120
5121   /* A new-expression may not appear in an integral constant
5122      expression.  */
5123   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5124     return error_mark_node;
5125
5126   /* Create a representation of the new-expression.  */
5127   return build_new (placement, type, nelts, initializer, global_scope_p);
5128 }
5129
5130 /* Parse a new-placement.
5131
5132    new-placement:
5133      ( expression-list )
5134
5135    Returns the same representation as for an expression-list.  */
5136
5137 static tree
5138 cp_parser_new_placement (cp_parser* parser)
5139 {
5140   tree expression_list;
5141
5142   /* Parse the expression-list.  */
5143   expression_list = (cp_parser_parenthesized_expression_list
5144                      (parser, false, /*cast_p=*/false,
5145                       /*non_constant_p=*/NULL));
5146
5147   return expression_list;
5148 }
5149
5150 /* Parse a new-type-id.
5151
5152    new-type-id:
5153      type-specifier-seq new-declarator [opt]
5154
5155    Returns the TYPE allocated.  If the new-type-id indicates an array
5156    type, *NELTS is set to the number of elements in the last array
5157    bound; the TYPE will not include the last array bound.  */
5158
5159 static tree
5160 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5161 {
5162   cp_decl_specifier_seq type_specifier_seq;
5163   cp_declarator *new_declarator;
5164   cp_declarator *declarator;
5165   cp_declarator *outer_declarator;
5166   const char *saved_message;
5167   tree type;
5168
5169   /* The type-specifier sequence must not contain type definitions.
5170      (It cannot contain declarations of new types either, but if they
5171      are not definitions we will catch that because they are not
5172      complete.)  */
5173   saved_message = parser->type_definition_forbidden_message;
5174   parser->type_definition_forbidden_message
5175     = "types may not be defined in a new-type-id";
5176   /* Parse the type-specifier-seq.  */
5177   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5178                                 &type_specifier_seq);
5179   /* Restore the old message.  */
5180   parser->type_definition_forbidden_message = saved_message;
5181   /* Parse the new-declarator.  */
5182   new_declarator = cp_parser_new_declarator_opt (parser);
5183
5184   /* Determine the number of elements in the last array dimension, if
5185      any.  */
5186   *nelts = NULL_TREE;
5187   /* Skip down to the last array dimension.  */
5188   declarator = new_declarator;
5189   outer_declarator = NULL;
5190   while (declarator && (declarator->kind == cdk_pointer
5191                         || declarator->kind == cdk_ptrmem))
5192     {
5193       outer_declarator = declarator;
5194       declarator = declarator->declarator;
5195     }
5196   while (declarator
5197          && declarator->kind == cdk_array
5198          && declarator->declarator
5199          && declarator->declarator->kind == cdk_array)
5200     {
5201       outer_declarator = declarator;
5202       declarator = declarator->declarator;
5203     }
5204
5205   if (declarator && declarator->kind == cdk_array)
5206     {
5207       *nelts = declarator->u.array.bounds;
5208       if (*nelts == error_mark_node)
5209         *nelts = integer_one_node;
5210
5211       if (outer_declarator)
5212         outer_declarator->declarator = declarator->declarator;
5213       else
5214         new_declarator = NULL;
5215     }
5216
5217   type = groktypename (&type_specifier_seq, new_declarator);
5218   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5219     {
5220       *nelts = array_type_nelts_top (type);
5221       type = TREE_TYPE (type);
5222     }
5223   return type;
5224 }
5225
5226 /* Parse an (optional) new-declarator.
5227
5228    new-declarator:
5229      ptr-operator new-declarator [opt]
5230      direct-new-declarator
5231
5232    Returns the declarator.  */
5233
5234 static cp_declarator *
5235 cp_parser_new_declarator_opt (cp_parser* parser)
5236 {
5237   enum tree_code code;
5238   tree type;
5239   cp_cv_quals cv_quals;
5240
5241   /* We don't know if there's a ptr-operator next, or not.  */
5242   cp_parser_parse_tentatively (parser);
5243   /* Look for a ptr-operator.  */
5244   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5245   /* If that worked, look for more new-declarators.  */
5246   if (cp_parser_parse_definitely (parser))
5247     {
5248       cp_declarator *declarator;
5249
5250       /* Parse another optional declarator.  */
5251       declarator = cp_parser_new_declarator_opt (parser);
5252
5253       /* Create the representation of the declarator.  */
5254       if (type)
5255         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5256       else if (code == INDIRECT_REF)
5257         declarator = make_pointer_declarator (cv_quals, declarator);
5258       else
5259         declarator = make_reference_declarator (cv_quals, declarator);
5260
5261       return declarator;
5262     }
5263
5264   /* If the next token is a `[', there is a direct-new-declarator.  */
5265   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5266     return cp_parser_direct_new_declarator (parser);
5267
5268   return NULL;
5269 }
5270
5271 /* Parse a direct-new-declarator.
5272
5273    direct-new-declarator:
5274      [ expression ]
5275      direct-new-declarator [constant-expression]
5276
5277    */
5278
5279 static cp_declarator *
5280 cp_parser_direct_new_declarator (cp_parser* parser)
5281 {
5282   cp_declarator *declarator = NULL;
5283
5284   while (true)
5285     {
5286       tree expression;
5287
5288       /* Look for the opening `['.  */
5289       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5290       /* The first expression is not required to be constant.  */
5291       if (!declarator)
5292         {
5293           expression = cp_parser_expression (parser, /*cast_p=*/false);
5294           /* The standard requires that the expression have integral
5295              type.  DR 74 adds enumeration types.  We believe that the
5296              real intent is that these expressions be handled like the
5297              expression in a `switch' condition, which also allows
5298              classes with a single conversion to integral or
5299              enumeration type.  */
5300           if (!processing_template_decl)
5301             {
5302               expression
5303                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5304                                               expression,
5305                                               /*complain=*/true);
5306               if (!expression)
5307                 {
5308                   error ("expression in new-declarator must have integral "
5309                          "or enumeration type");
5310                   expression = error_mark_node;
5311                 }
5312             }
5313         }
5314       /* But all the other expressions must be.  */
5315       else
5316         expression
5317           = cp_parser_constant_expression (parser,
5318                                            /*allow_non_constant=*/false,
5319                                            NULL);
5320       /* Look for the closing `]'.  */
5321       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5322
5323       /* Add this bound to the declarator.  */
5324       declarator = make_array_declarator (declarator, expression);
5325
5326       /* If the next token is not a `[', then there are no more
5327          bounds.  */
5328       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5329         break;
5330     }
5331
5332   return declarator;
5333 }
5334
5335 /* Parse a new-initializer.
5336
5337    new-initializer:
5338      ( expression-list [opt] )
5339
5340    Returns a representation of the expression-list.  If there is no
5341    expression-list, VOID_ZERO_NODE is returned.  */
5342
5343 static tree
5344 cp_parser_new_initializer (cp_parser* parser)
5345 {
5346   tree expression_list;
5347
5348   expression_list = (cp_parser_parenthesized_expression_list
5349                      (parser, false, /*cast_p=*/false,
5350                       /*non_constant_p=*/NULL));
5351   if (!expression_list)
5352     expression_list = void_zero_node;
5353
5354   return expression_list;
5355 }
5356
5357 /* Parse a delete-expression.
5358
5359    delete-expression:
5360      :: [opt] delete cast-expression
5361      :: [opt] delete [ ] cast-expression
5362
5363    Returns a representation of the expression.  */
5364
5365 static tree
5366 cp_parser_delete_expression (cp_parser* parser)
5367 {
5368   bool global_scope_p;
5369   bool array_p;
5370   tree expression;
5371
5372   /* Look for the optional `::' operator.  */
5373   global_scope_p
5374     = (cp_parser_global_scope_opt (parser,
5375                                    /*current_scope_valid_p=*/false)
5376        != NULL_TREE);
5377   /* Look for the `delete' keyword.  */
5378   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5379   /* See if the array syntax is in use.  */
5380   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5381     {
5382       /* Consume the `[' token.  */
5383       cp_lexer_consume_token (parser->lexer);
5384       /* Look for the `]' token.  */
5385       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5386       /* Remember that this is the `[]' construct.  */
5387       array_p = true;
5388     }
5389   else
5390     array_p = false;
5391
5392   /* Parse the cast-expression.  */
5393   expression = cp_parser_simple_cast_expression (parser);
5394
5395   /* A delete-expression may not appear in an integral constant
5396      expression.  */
5397   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5398     return error_mark_node;
5399
5400   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5401 }
5402
5403 /* Parse a cast-expression.
5404
5405    cast-expression:
5406      unary-expression
5407      ( type-id ) cast-expression
5408
5409    ADDRESS_P is true iff the unary-expression is appearing as the
5410    operand of the `&' operator.   CAST_P is true if this expression is
5411    the target of a cast.
5412
5413    Returns a representation of the expression.  */
5414
5415 static tree
5416 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5417 {
5418   /* If it's a `(', then we might be looking at a cast.  */
5419   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5420     {
5421       tree type = NULL_TREE;
5422       tree expr = NULL_TREE;
5423       bool compound_literal_p;
5424       const char *saved_message;
5425
5426       /* There's no way to know yet whether or not this is a cast.
5427          For example, `(int (3))' is a unary-expression, while `(int)
5428          3' is a cast.  So, we resort to parsing tentatively.  */
5429       cp_parser_parse_tentatively (parser);
5430       /* Types may not be defined in a cast.  */
5431       saved_message = parser->type_definition_forbidden_message;
5432       parser->type_definition_forbidden_message
5433         = "types may not be defined in casts";
5434       /* Consume the `('.  */
5435       cp_lexer_consume_token (parser->lexer);
5436       /* A very tricky bit is that `(struct S) { 3 }' is a
5437          compound-literal (which we permit in C++ as an extension).
5438          But, that construct is not a cast-expression -- it is a
5439          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5440          is legal; if the compound-literal were a cast-expression,
5441          you'd need an extra set of parentheses.)  But, if we parse
5442          the type-id, and it happens to be a class-specifier, then we
5443          will commit to the parse at that point, because we cannot
5444          undo the action that is done when creating a new class.  So,
5445          then we cannot back up and do a postfix-expression.
5446
5447          Therefore, we scan ahead to the closing `)', and check to see
5448          if the token after the `)' is a `{'.  If so, we are not
5449          looking at a cast-expression.
5450
5451          Save tokens so that we can put them back.  */
5452       cp_lexer_save_tokens (parser->lexer);
5453       /* Skip tokens until the next token is a closing parenthesis.
5454          If we find the closing `)', and the next token is a `{', then
5455          we are looking at a compound-literal.  */
5456       compound_literal_p
5457         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5458                                                   /*consume_paren=*/true)
5459            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5460       /* Roll back the tokens we skipped.  */
5461       cp_lexer_rollback_tokens (parser->lexer);
5462       /* If we were looking at a compound-literal, simulate an error
5463          so that the call to cp_parser_parse_definitely below will
5464          fail.  */
5465       if (compound_literal_p)
5466         cp_parser_simulate_error (parser);
5467       else
5468         {
5469           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5470           parser->in_type_id_in_expr_p = true;
5471           /* Look for the type-id.  */
5472           type = cp_parser_type_id (parser);
5473           /* Look for the closing `)'.  */
5474           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5475           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5476         }
5477
5478       /* Restore the saved message.  */
5479       parser->type_definition_forbidden_message = saved_message;
5480
5481       /* If ok so far, parse the dependent expression. We cannot be
5482          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5483          ctor of T, but looks like a cast to function returning T
5484          without a dependent expression.  */
5485       if (!cp_parser_error_occurred (parser))
5486         expr = cp_parser_cast_expression (parser,
5487                                           /*address_p=*/false,
5488                                           /*cast_p=*/true);
5489
5490       if (cp_parser_parse_definitely (parser))
5491         {
5492           /* Warn about old-style casts, if so requested.  */
5493           if (warn_old_style_cast
5494               && !in_system_header
5495               && !VOID_TYPE_P (type)
5496               && current_lang_name != lang_name_c)
5497             warning (OPT_Wold_style_cast, "use of old-style cast");
5498
5499           /* Only type conversions to integral or enumeration types
5500              can be used in constant-expressions.  */
5501           if (parser->integral_constant_expression_p
5502               && !dependent_type_p (type)
5503               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5504               && (cp_parser_non_integral_constant_expression
5505                   (parser,
5506                    "a cast to a type other than an integral or "
5507                    "enumeration type")))
5508             return error_mark_node;
5509
5510           /* Perform the cast.  */
5511           expr = build_c_cast (type, expr);
5512           return expr;
5513         }
5514     }
5515
5516   /* If we get here, then it's not a cast, so it must be a
5517      unary-expression.  */
5518   return cp_parser_unary_expression (parser, address_p, cast_p);
5519 }
5520
5521 /* Parse a binary expression of the general form:
5522
5523    pm-expression:
5524      cast-expression
5525      pm-expression .* cast-expression
5526      pm-expression ->* cast-expression
5527
5528    multiplicative-expression:
5529      pm-expression
5530      multiplicative-expression * pm-expression
5531      multiplicative-expression / pm-expression
5532      multiplicative-expression % pm-expression
5533
5534    additive-expression:
5535      multiplicative-expression
5536      additive-expression + multiplicative-expression
5537      additive-expression - multiplicative-expression
5538
5539    shift-expression:
5540      additive-expression
5541      shift-expression << additive-expression
5542      shift-expression >> additive-expression
5543
5544    relational-expression:
5545      shift-expression
5546      relational-expression < shift-expression
5547      relational-expression > shift-expression
5548      relational-expression <= shift-expression
5549      relational-expression >= shift-expression
5550
5551   GNU Extension:
5552
5553    relational-expression:
5554      relational-expression <? shift-expression
5555      relational-expression >? shift-expression
5556
5557    equality-expression:
5558      relational-expression
5559      equality-expression == relational-expression
5560      equality-expression != relational-expression
5561
5562    and-expression:
5563      equality-expression
5564      and-expression & equality-expression
5565
5566    exclusive-or-expression:
5567      and-expression
5568      exclusive-or-expression ^ and-expression
5569
5570    inclusive-or-expression:
5571      exclusive-or-expression
5572      inclusive-or-expression | exclusive-or-expression
5573
5574    logical-and-expression:
5575      inclusive-or-expression
5576      logical-and-expression && inclusive-or-expression
5577
5578    logical-or-expression:
5579      logical-and-expression
5580      logical-or-expression || logical-and-expression
5581
5582    All these are implemented with a single function like:
5583
5584    binary-expression:
5585      simple-cast-expression
5586      binary-expression <token> binary-expression
5587
5588    CAST_P is true if this expression is the target of a cast.
5589
5590    The binops_by_token map is used to get the tree codes for each <token> type.
5591    binary-expressions are associated according to a precedence table.  */
5592
5593 #define TOKEN_PRECEDENCE(token) \
5594   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5595    ? PREC_NOT_OPERATOR \
5596    : binops_by_token[token->type].prec)
5597
5598 static tree
5599 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5600 {
5601   cp_parser_expression_stack stack;
5602   cp_parser_expression_stack_entry *sp = &stack[0];
5603   tree lhs, rhs;
5604   cp_token *token;
5605   enum tree_code tree_type;
5606   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5607   bool overloaded_p;
5608
5609   /* Parse the first expression.  */
5610   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5611
5612   for (;;)
5613     {
5614       /* Get an operator token.  */
5615       token = cp_lexer_peek_token (parser->lexer);
5616       if (token->type == CPP_MIN || token->type == CPP_MAX)
5617         cp_parser_warn_min_max ();
5618
5619       new_prec = TOKEN_PRECEDENCE (token);
5620
5621       /* Popping an entry off the stack means we completed a subexpression:
5622          - either we found a token which is not an operator (`>' where it is not
5623            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5624            will happen repeatedly;
5625          - or, we found an operator which has lower priority.  This is the case
5626            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5627            parsing `3 * 4'.  */
5628       if (new_prec <= prec)
5629         {
5630           if (sp == stack)
5631             break;
5632           else
5633             goto pop;
5634         }
5635
5636      get_rhs:
5637       tree_type = binops_by_token[token->type].tree_type;
5638
5639       /* We used the operator token.  */
5640       cp_lexer_consume_token (parser->lexer);
5641
5642       /* Extract another operand.  It may be the RHS of this expression
5643          or the LHS of a new, higher priority expression.  */
5644       rhs = cp_parser_simple_cast_expression (parser);
5645
5646       /* Get another operator token.  Look up its precedence to avoid
5647          building a useless (immediately popped) stack entry for common
5648          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5649       token = cp_lexer_peek_token (parser->lexer);
5650       lookahead_prec = TOKEN_PRECEDENCE (token);
5651       if (lookahead_prec > new_prec)
5652         {
5653           /* ... and prepare to parse the RHS of the new, higher priority
5654              expression.  Since precedence levels on the stack are
5655              monotonically increasing, we do not have to care about
5656              stack overflows.  */
5657           sp->prec = prec;
5658           sp->tree_type = tree_type;
5659           sp->lhs = lhs;
5660           sp++;
5661           lhs = rhs;
5662           prec = new_prec;
5663           new_prec = lookahead_prec;
5664           goto get_rhs;
5665
5666          pop:
5667           /* If the stack is not empty, we have parsed into LHS the right side
5668              (`4' in the example above) of an expression we had suspended.
5669              We can use the information on the stack to recover the LHS (`3')
5670              from the stack together with the tree code (`MULT_EXPR'), and
5671              the precedence of the higher level subexpression
5672              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5673              which will be used to actually build the additive expression.  */
5674           --sp;
5675           prec = sp->prec;
5676           tree_type = sp->tree_type;
5677           rhs = lhs;
5678           lhs = sp->lhs;
5679         }
5680
5681       overloaded_p = false;
5682       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5683
5684       /* If the binary operator required the use of an overloaded operator,
5685          then this expression cannot be an integral constant-expression.
5686          An overloaded operator can be used even if both operands are
5687          otherwise permissible in an integral constant-expression if at
5688          least one of the operands is of enumeration type.  */
5689
5690       if (overloaded_p
5691           && (cp_parser_non_integral_constant_expression
5692               (parser, "calls to overloaded operators")))
5693         return error_mark_node;
5694     }
5695
5696   return lhs;
5697 }
5698
5699
5700 /* Parse the `? expression : assignment-expression' part of a
5701    conditional-expression.  The LOGICAL_OR_EXPR is the
5702    logical-or-expression that started the conditional-expression.
5703    Returns a representation of the entire conditional-expression.
5704
5705    This routine is used by cp_parser_assignment_expression.
5706
5707      ? expression : assignment-expression
5708
5709    GNU Extensions:
5710
5711      ? : assignment-expression */
5712
5713 static tree
5714 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5715 {
5716   tree expr;
5717   tree assignment_expr;
5718
5719   /* Consume the `?' token.  */
5720   cp_lexer_consume_token (parser->lexer);
5721   if (cp_parser_allow_gnu_extensions_p (parser)
5722       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5723     /* Implicit true clause.  */
5724     expr = NULL_TREE;
5725   else
5726     /* Parse the expression.  */
5727     expr = cp_parser_expression (parser, /*cast_p=*/false);
5728
5729   /* The next token should be a `:'.  */
5730   cp_parser_require (parser, CPP_COLON, "`:'");
5731   /* Parse the assignment-expression.  */
5732   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5733
5734   /* Build the conditional-expression.  */
5735   return build_x_conditional_expr (logical_or_expr,
5736                                    expr,
5737                                    assignment_expr);
5738 }
5739
5740 /* Parse an assignment-expression.
5741
5742    assignment-expression:
5743      conditional-expression
5744      logical-or-expression assignment-operator assignment_expression
5745      throw-expression
5746
5747    CAST_P is true if this expression is the target of a cast.
5748
5749    Returns a representation for the expression.  */
5750
5751 static tree
5752 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5753 {
5754   tree expr;
5755
5756   /* If the next token is the `throw' keyword, then we're looking at
5757      a throw-expression.  */
5758   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5759     expr = cp_parser_throw_expression (parser);
5760   /* Otherwise, it must be that we are looking at a
5761      logical-or-expression.  */
5762   else
5763     {
5764       /* Parse the binary expressions (logical-or-expression).  */
5765       expr = cp_parser_binary_expression (parser, cast_p);
5766       /* If the next token is a `?' then we're actually looking at a
5767          conditional-expression.  */
5768       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5769         return cp_parser_question_colon_clause (parser, expr);
5770       else
5771         {
5772           enum tree_code assignment_operator;
5773
5774           /* If it's an assignment-operator, we're using the second
5775              production.  */
5776           assignment_operator
5777             = cp_parser_assignment_operator_opt (parser);
5778           if (assignment_operator != ERROR_MARK)
5779             {
5780               tree rhs;
5781
5782               /* Parse the right-hand side of the assignment.  */
5783               rhs = cp_parser_assignment_expression (parser, cast_p);
5784               /* An assignment may not appear in a
5785                  constant-expression.  */
5786               if (cp_parser_non_integral_constant_expression (parser,
5787                                                               "an assignment"))
5788                 return error_mark_node;
5789               /* Build the assignment expression.  */
5790               expr = build_x_modify_expr (expr,
5791                                           assignment_operator,
5792                                           rhs);
5793             }
5794         }
5795     }
5796
5797   return expr;
5798 }
5799
5800 /* Parse an (optional) assignment-operator.
5801
5802    assignment-operator: one of
5803      = *= /= %= += -= >>= <<= &= ^= |=
5804
5805    GNU Extension:
5806
5807    assignment-operator: one of
5808      <?= >?=
5809
5810    If the next token is an assignment operator, the corresponding tree
5811    code is returned, and the token is consumed.  For example, for
5812    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5813    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5814    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5815    operator, ERROR_MARK is returned.  */
5816
5817 static enum tree_code
5818 cp_parser_assignment_operator_opt (cp_parser* parser)
5819 {
5820   enum tree_code op;
5821   cp_token *token;
5822
5823   /* Peek at the next toen.  */
5824   token = cp_lexer_peek_token (parser->lexer);
5825
5826   switch (token->type)
5827     {
5828     case CPP_EQ:
5829       op = NOP_EXPR;
5830       break;
5831
5832     case CPP_MULT_EQ:
5833       op = MULT_EXPR;
5834       break;
5835
5836     case CPP_DIV_EQ:
5837       op = TRUNC_DIV_EXPR;
5838       break;
5839
5840     case CPP_MOD_EQ:
5841       op = TRUNC_MOD_EXPR;
5842       break;
5843
5844     case CPP_PLUS_EQ:
5845       op = PLUS_EXPR;
5846       break;
5847
5848     case CPP_MINUS_EQ:
5849       op = MINUS_EXPR;
5850       break;
5851
5852     case CPP_RSHIFT_EQ:
5853       op = RSHIFT_EXPR;
5854       break;
5855
5856     case CPP_LSHIFT_EQ:
5857       op = LSHIFT_EXPR;
5858       break;
5859
5860     case CPP_AND_EQ:
5861       op = BIT_AND_EXPR;
5862       break;
5863
5864     case CPP_XOR_EQ:
5865       op = BIT_XOR_EXPR;
5866       break;
5867
5868     case CPP_OR_EQ:
5869       op = BIT_IOR_EXPR;
5870       break;
5871
5872     case CPP_MIN_EQ:
5873       op = MIN_EXPR;
5874       cp_parser_warn_min_max ();
5875       break;
5876
5877     case CPP_MAX_EQ:
5878       op = MAX_EXPR;
5879       cp_parser_warn_min_max ();
5880       break;
5881
5882     default:
5883       /* Nothing else is an assignment operator.  */
5884       op = ERROR_MARK;
5885     }
5886
5887   /* If it was an assignment operator, consume it.  */
5888   if (op != ERROR_MARK)
5889     cp_lexer_consume_token (parser->lexer);
5890
5891   return op;
5892 }
5893
5894 /* Parse an expression.
5895
5896    expression:
5897      assignment-expression
5898      expression , assignment-expression
5899
5900    CAST_P is true if this expression is the target of a cast.
5901
5902    Returns a representation of the expression.  */
5903
5904 static tree
5905 cp_parser_expression (cp_parser* parser, bool cast_p)
5906 {
5907   tree expression = NULL_TREE;
5908
5909   while (true)
5910     {
5911       tree assignment_expression;
5912
5913       /* Parse the next assignment-expression.  */
5914       assignment_expression
5915         = cp_parser_assignment_expression (parser, cast_p);
5916       /* If this is the first assignment-expression, we can just
5917          save it away.  */
5918       if (!expression)
5919         expression = assignment_expression;
5920       else
5921         expression = build_x_compound_expr (expression,
5922                                             assignment_expression);
5923       /* If the next token is not a comma, then we are done with the
5924          expression.  */
5925       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5926         break;
5927       /* Consume the `,'.  */
5928       cp_lexer_consume_token (parser->lexer);
5929       /* A comma operator cannot appear in a constant-expression.  */
5930       if (cp_parser_non_integral_constant_expression (parser,
5931                                                       "a comma operator"))
5932         expression = error_mark_node;
5933     }
5934
5935   return expression;
5936 }
5937
5938 /* Parse a constant-expression.
5939
5940    constant-expression:
5941      conditional-expression
5942
5943   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5944   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5945   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5946   is false, NON_CONSTANT_P should be NULL.  */
5947
5948 static tree
5949 cp_parser_constant_expression (cp_parser* parser,
5950                                bool allow_non_constant_p,
5951                                bool *non_constant_p)
5952 {
5953   bool saved_integral_constant_expression_p;
5954   bool saved_allow_non_integral_constant_expression_p;
5955   bool saved_non_integral_constant_expression_p;
5956   tree expression;
5957
5958   /* It might seem that we could simply parse the
5959      conditional-expression, and then check to see if it were
5960      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5961      one that the compiler can figure out is constant, possibly after
5962      doing some simplifications or optimizations.  The standard has a
5963      precise definition of constant-expression, and we must honor
5964      that, even though it is somewhat more restrictive.
5965
5966      For example:
5967
5968        int i[(2, 3)];
5969
5970      is not a legal declaration, because `(2, 3)' is not a
5971      constant-expression.  The `,' operator is forbidden in a
5972      constant-expression.  However, GCC's constant-folding machinery
5973      will fold this operation to an INTEGER_CST for `3'.  */
5974
5975   /* Save the old settings.  */
5976   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5977   saved_allow_non_integral_constant_expression_p
5978     = parser->allow_non_integral_constant_expression_p;
5979   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5980   /* We are now parsing a constant-expression.  */
5981   parser->integral_constant_expression_p = true;
5982   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5983   parser->non_integral_constant_expression_p = false;
5984   /* Although the grammar says "conditional-expression", we parse an
5985      "assignment-expression", which also permits "throw-expression"
5986      and the use of assignment operators.  In the case that
5987      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5988      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5989      actually essential that we look for an assignment-expression.
5990      For example, cp_parser_initializer_clauses uses this function to
5991      determine whether a particular assignment-expression is in fact
5992      constant.  */
5993   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5994   /* Restore the old settings.  */
5995   parser->integral_constant_expression_p
5996     = saved_integral_constant_expression_p;
5997   parser->allow_non_integral_constant_expression_p
5998     = saved_allow_non_integral_constant_expression_p;
5999   if (allow_non_constant_p)
6000     *non_constant_p = parser->non_integral_constant_expression_p;
6001   else if (parser->non_integral_constant_expression_p)
6002     expression = error_mark_node;
6003   parser->non_integral_constant_expression_p
6004     = saved_non_integral_constant_expression_p;
6005
6006   return expression;
6007 }
6008
6009 /* Parse __builtin_offsetof.
6010
6011    offsetof-expression:
6012      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6013
6014    offsetof-member-designator:
6015      id-expression
6016      | offsetof-member-designator "." id-expression
6017      | offsetof-member-designator "[" expression "]"  */
6018
6019 static tree
6020 cp_parser_builtin_offsetof (cp_parser *parser)
6021 {
6022   int save_ice_p, save_non_ice_p;
6023   tree type, expr;
6024   cp_id_kind dummy;
6025
6026   /* We're about to accept non-integral-constant things, but will
6027      definitely yield an integral constant expression.  Save and
6028      restore these values around our local parsing.  */
6029   save_ice_p = parser->integral_constant_expression_p;
6030   save_non_ice_p = parser->non_integral_constant_expression_p;
6031
6032   /* Consume the "__builtin_offsetof" token.  */
6033   cp_lexer_consume_token (parser->lexer);
6034   /* Consume the opening `('.  */
6035   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6036   /* Parse the type-id.  */
6037   type = cp_parser_type_id (parser);
6038   /* Look for the `,'.  */
6039   cp_parser_require (parser, CPP_COMMA, "`,'");
6040
6041   /* Build the (type *)null that begins the traditional offsetof macro.  */
6042   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6043
6044   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6045   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6046                                                  true, &dummy);
6047   while (true)
6048     {
6049       cp_token *token = cp_lexer_peek_token (parser->lexer);
6050       switch (token->type)
6051         {
6052         case CPP_OPEN_SQUARE:
6053           /* offsetof-member-designator "[" expression "]" */
6054           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6055           break;
6056
6057         case CPP_DOT:
6058           /* offsetof-member-designator "." identifier */
6059           cp_lexer_consume_token (parser->lexer);
6060           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6061                                                          true, &dummy);
6062           break;
6063
6064         case CPP_CLOSE_PAREN:
6065           /* Consume the ")" token.  */
6066           cp_lexer_consume_token (parser->lexer);
6067           goto success;
6068
6069         default:
6070           /* Error.  We know the following require will fail, but
6071              that gives the proper error message.  */
6072           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6073           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6074           expr = error_mark_node;
6075           goto failure;
6076         }
6077     }
6078
6079  success:
6080   /* If we're processing a template, we can't finish the semantics yet.
6081      Otherwise we can fold the entire expression now.  */
6082   if (processing_template_decl)
6083     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6084   else
6085     expr = finish_offsetof (expr);
6086
6087  failure:
6088   parser->integral_constant_expression_p = save_ice_p;
6089   parser->non_integral_constant_expression_p = save_non_ice_p;
6090
6091   return expr;
6092 }
6093
6094 /* Statements [gram.stmt.stmt]  */
6095
6096 /* Parse a statement.
6097
6098    statement:
6099      labeled-statement
6100      expression-statement
6101      compound-statement
6102      selection-statement
6103      iteration-statement
6104      jump-statement
6105      declaration-statement
6106      try-block
6107
6108   IN_COMPOUND is true when the statement is nested inside a
6109   cp_parser_compound_statement; this matters for certain pragmas.  */
6110
6111 static void
6112 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6113                      bool in_compound)
6114 {
6115   tree statement;
6116   cp_token *token;
6117   location_t statement_location;
6118
6119  restart:
6120   /* There is no statement yet.  */
6121   statement = NULL_TREE;
6122   /* Peek at the next token.  */
6123   token = cp_lexer_peek_token (parser->lexer);
6124   /* Remember the location of the first token in the statement.  */
6125   statement_location = token->location;
6126   /* If this is a keyword, then that will often determine what kind of
6127      statement we have.  */
6128   if (token->type == CPP_KEYWORD)
6129     {
6130       enum rid keyword = token->keyword;
6131
6132       switch (keyword)
6133         {
6134         case RID_CASE:
6135         case RID_DEFAULT:
6136           statement = cp_parser_labeled_statement (parser, in_statement_expr,
6137                                                    in_compound);
6138           break;
6139
6140         case RID_IF:
6141         case RID_SWITCH:
6142           statement = cp_parser_selection_statement (parser);
6143           break;
6144
6145         case RID_WHILE:
6146         case RID_DO:
6147         case RID_FOR:
6148           statement = cp_parser_iteration_statement (parser);
6149           break;
6150
6151         case RID_BREAK:
6152         case RID_CONTINUE:
6153         case RID_RETURN:
6154         case RID_GOTO:
6155           statement = cp_parser_jump_statement (parser);
6156           break;
6157
6158           /* Objective-C++ exception-handling constructs.  */
6159         case RID_AT_TRY:
6160         case RID_AT_CATCH:
6161         case RID_AT_FINALLY:
6162         case RID_AT_SYNCHRONIZED:
6163         case RID_AT_THROW:
6164           statement = cp_parser_objc_statement (parser);
6165           break;
6166
6167         case RID_TRY:
6168           statement = cp_parser_try_block (parser);
6169           break;
6170
6171         default:
6172           /* It might be a keyword like `int' that can start a
6173              declaration-statement.  */
6174           break;
6175         }
6176     }
6177   else if (token->type == CPP_NAME)
6178     {
6179       /* If the next token is a `:', then we are looking at a
6180          labeled-statement.  */
6181       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6182       if (token->type == CPP_COLON)
6183         statement = cp_parser_labeled_statement (parser, in_statement_expr,
6184                                                  in_compound);
6185     }
6186   /* Anything that starts with a `{' must be a compound-statement.  */
6187   else if (token->type == CPP_OPEN_BRACE)
6188     statement = cp_parser_compound_statement (parser, NULL, false);
6189   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6190      a statement all its own.  */
6191   else if (token->type == CPP_PRAGMA)
6192     {
6193       /* Only certain OpenMP pragmas are attached to statements, and thus
6194          are considered statements themselves.  All others are not.  In
6195          the context of a compound, accept the pragma as a "statement" and
6196          return so that we can check for a close brace.  Otherwise we
6197          require a real statement and must go back and read one.  */
6198       if (in_compound)
6199         cp_parser_pragma (parser, pragma_compound);
6200       else if (!cp_parser_pragma (parser, pragma_stmt))
6201         goto restart;
6202       return;
6203     }
6204   else if (token->type == CPP_EOF)
6205     {
6206       cp_parser_error (parser, "expected statement");
6207       return;
6208     }
6209
6210   /* Everything else must be a declaration-statement or an
6211      expression-statement.  Try for the declaration-statement
6212      first, unless we are looking at a `;', in which case we know that
6213      we have an expression-statement.  */
6214   if (!statement)
6215     {
6216       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6217         {
6218           cp_parser_parse_tentatively (parser);
6219           /* Try to parse the declaration-statement.  */
6220           cp_parser_declaration_statement (parser);
6221           /* If that worked, we're done.  */
6222           if (cp_parser_parse_definitely (parser))
6223             return;
6224         }
6225       /* Look for an expression-statement instead.  */
6226       statement = cp_parser_expression_statement (parser, in_statement_expr);
6227     }
6228
6229   /* Set the line number for the statement.  */
6230   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6231     SET_EXPR_LOCATION (statement, statement_location);
6232 }
6233
6234 /* Parse a labeled-statement.
6235
6236    labeled-statement:
6237      identifier : statement
6238      case constant-expression : statement
6239      default : statement
6240
6241    GNU Extension:
6242
6243    labeled-statement:
6244      case constant-expression ... constant-expression : statement
6245
6246    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6247    For an ordinary label, returns a LABEL_EXPR.
6248
6249    IN_COMPOUND is as for cp_parser_statement: true when we're nested
6250    inside a compound.  */
6251
6252 static tree
6253 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6254                              bool in_compound)
6255 {
6256   cp_token *token;
6257   tree statement = error_mark_node;
6258
6259   /* The next token should be an identifier.  */
6260   token = cp_lexer_peek_token (parser->lexer);
6261   if (token->type != CPP_NAME
6262       && token->type != CPP_KEYWORD)
6263     {
6264       cp_parser_error (parser, "expected labeled-statement");
6265       return error_mark_node;
6266     }
6267
6268   switch (token->keyword)
6269     {
6270     case RID_CASE:
6271       {
6272         tree expr, expr_hi;
6273         cp_token *ellipsis;
6274
6275         /* Consume the `case' token.  */
6276         cp_lexer_consume_token (parser->lexer);
6277         /* Parse the constant-expression.  */
6278         expr = cp_parser_constant_expression (parser,
6279                                               /*allow_non_constant_p=*/false,
6280                                               NULL);
6281
6282         ellipsis = cp_lexer_peek_token (parser->lexer);
6283         if (ellipsis->type == CPP_ELLIPSIS)
6284           {
6285             /* Consume the `...' token.  */
6286             cp_lexer_consume_token (parser->lexer);
6287             expr_hi =
6288               cp_parser_constant_expression (parser,
6289                                              /*allow_non_constant_p=*/false,
6290                                              NULL);
6291             /* We don't need to emit warnings here, as the common code
6292                will do this for us.  */
6293           }
6294         else
6295           expr_hi = NULL_TREE;
6296
6297         if (parser->in_switch_statement_p)
6298           statement = finish_case_label (expr, expr_hi);
6299         else
6300           error ("case label %qE not within a switch statement", expr);
6301       }
6302       break;
6303
6304     case RID_DEFAULT:
6305       /* Consume the `default' token.  */
6306       cp_lexer_consume_token (parser->lexer);
6307
6308       if (parser->in_switch_statement_p)
6309         statement = finish_case_label (NULL_TREE, NULL_TREE);
6310       else
6311         error ("case label not within a switch statement");
6312       break;
6313
6314     default:
6315       /* Anything else must be an ordinary label.  */
6316       statement = finish_label_stmt (cp_parser_identifier (parser));
6317       break;
6318     }
6319
6320   /* Require the `:' token.  */
6321   cp_parser_require (parser, CPP_COLON, "`:'");
6322   /* Parse the labeled statement.  */
6323   cp_parser_statement (parser, in_statement_expr, in_compound);
6324
6325   /* Return the label, in the case of a `case' or `default' label.  */
6326   return statement;
6327 }
6328
6329 /* Parse an expression-statement.
6330
6331    expression-statement:
6332      expression [opt] ;
6333
6334    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6335    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6336    indicates whether this expression-statement is part of an
6337    expression statement.  */
6338
6339 static tree
6340 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6341 {
6342   tree statement = NULL_TREE;
6343
6344   /* If the next token is a ';', then there is no expression
6345      statement.  */
6346   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6347     statement = cp_parser_expression (parser, /*cast_p=*/false);
6348
6349   /* Consume the final `;'.  */
6350   cp_parser_consume_semicolon_at_end_of_statement (parser);
6351
6352   if (in_statement_expr
6353       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6354     /* This is the final expression statement of a statement
6355        expression.  */
6356     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6357   else if (statement)
6358     statement = finish_expr_stmt (statement);
6359   else
6360     finish_stmt ();
6361
6362   return statement;
6363 }
6364
6365 /* Parse a compound-statement.
6366
6367    compound-statement:
6368      { statement-seq [opt] }
6369
6370    Returns a tree representing the statement.  */
6371
6372 static tree
6373 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6374                               bool in_try)
6375 {
6376   tree compound_stmt;
6377
6378   /* Consume the `{'.  */
6379   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6380     return error_mark_node;
6381   /* Begin the compound-statement.  */
6382   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6383   /* Parse an (optional) statement-seq.  */
6384   cp_parser_statement_seq_opt (parser, in_statement_expr);
6385   /* Finish the compound-statement.  */
6386   finish_compound_stmt (compound_stmt);
6387   /* Consume the `}'.  */
6388   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6389
6390   return compound_stmt;
6391 }
6392
6393 /* Parse an (optional) statement-seq.
6394
6395    statement-seq:
6396      statement
6397      statement-seq [opt] statement  */
6398
6399 static void
6400 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6401 {
6402   /* Scan statements until there aren't any more.  */
6403   while (true)
6404     {
6405       cp_token *token = cp_lexer_peek_token (parser->lexer);
6406
6407       /* If we're looking at a `}', then we've run out of statements.  */
6408       if (token->type == CPP_CLOSE_BRACE
6409           || token->type == CPP_EOF
6410           || token->type == CPP_PRAGMA_EOL)
6411         break;
6412
6413       /* Parse the statement.  */
6414       cp_parser_statement (parser, in_statement_expr, true);
6415     }
6416 }
6417
6418 /* Parse a selection-statement.
6419
6420    selection-statement:
6421      if ( condition ) statement
6422      if ( condition ) statement else statement
6423      switch ( condition ) statement
6424
6425    Returns the new IF_STMT or SWITCH_STMT.  */
6426
6427 static tree
6428 cp_parser_selection_statement (cp_parser* parser)
6429 {
6430   cp_token *token;
6431   enum rid keyword;
6432
6433   /* Peek at the next token.  */
6434   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6435
6436   /* See what kind of keyword it is.  */
6437   keyword = token->keyword;
6438   switch (keyword)
6439     {
6440     case RID_IF:
6441     case RID_SWITCH:
6442       {
6443         tree statement;
6444         tree condition;
6445
6446         /* Look for the `('.  */
6447         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6448           {
6449             cp_parser_skip_to_end_of_statement (parser);
6450             return error_mark_node;
6451           }
6452
6453         /* Begin the selection-statement.  */
6454         if (keyword == RID_IF)
6455           statement = begin_if_stmt ();
6456         else
6457           statement = begin_switch_stmt ();
6458
6459         /* Parse the condition.  */
6460         condition = cp_parser_condition (parser);
6461         /* Look for the `)'.  */
6462         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6463           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6464                                                  /*consume_paren=*/true);
6465
6466         if (keyword == RID_IF)
6467           {
6468             /* Add the condition.  */
6469             finish_if_stmt_cond (condition, statement);
6470
6471             /* Parse the then-clause.  */
6472             cp_parser_implicitly_scoped_statement (parser);
6473             finish_then_clause (statement);
6474
6475             /* If the next token is `else', parse the else-clause.  */
6476             if (cp_lexer_next_token_is_keyword (parser->lexer,
6477                                                 RID_ELSE))
6478               {
6479                 /* Consume the `else' keyword.  */
6480                 cp_lexer_consume_token (parser->lexer);
6481                 begin_else_clause (statement);
6482                 /* Parse the else-clause.  */
6483                 cp_parser_implicitly_scoped_statement (parser);
6484                 finish_else_clause (statement);
6485               }
6486
6487             /* Now we're all done with the if-statement.  */
6488             finish_if_stmt (statement);
6489           }
6490         else
6491           {
6492             bool in_switch_statement_p;
6493             unsigned char in_statement;
6494
6495             /* Add the condition.  */
6496             finish_switch_cond (condition, statement);
6497
6498             /* Parse the body of the switch-statement.  */
6499             in_switch_statement_p = parser->in_switch_statement_p;
6500             in_statement = parser->in_statement;
6501             parser->in_switch_statement_p = true;
6502             parser->in_statement |= IN_SWITCH_STMT;
6503             cp_parser_implicitly_scoped_statement (parser);
6504             parser->in_switch_statement_p = in_switch_statement_p;
6505             parser->in_statement = in_statement;
6506
6507             /* Now we're all done with the switch-statement.  */
6508             finish_switch_stmt (statement);
6509           }
6510
6511         return statement;
6512       }
6513       break;
6514
6515     default:
6516       cp_parser_error (parser, "expected selection-statement");
6517       return error_mark_node;
6518     }
6519 }
6520
6521 /* Parse a condition.
6522
6523    condition:
6524      expression
6525      type-specifier-seq declarator = assignment-expression
6526
6527    GNU Extension:
6528
6529    condition:
6530      type-specifier-seq declarator asm-specification [opt]
6531        attributes [opt] = assignment-expression
6532
6533    Returns the expression that should be tested.  */
6534
6535 static tree
6536 cp_parser_condition (cp_parser* parser)
6537 {
6538   cp_decl_specifier_seq type_specifiers;
6539   const char *saved_message;
6540
6541   /* Try the declaration first.  */
6542   cp_parser_parse_tentatively (parser);
6543   /* New types are not allowed in the type-specifier-seq for a
6544      condition.  */
6545   saved_message = parser->type_definition_forbidden_message;
6546   parser->type_definition_forbidden_message
6547     = "types may not be defined in conditions";
6548   /* Parse the type-specifier-seq.  */
6549   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6550                                 &type_specifiers);
6551   /* Restore the saved message.  */
6552   parser->type_definition_forbidden_message = saved_message;
6553   /* If all is well, we might be looking at a declaration.  */
6554   if (!cp_parser_error_occurred (parser))
6555     {
6556       tree decl;
6557       tree asm_specification;
6558       tree attributes;
6559       cp_declarator *declarator;
6560       tree initializer = NULL_TREE;
6561
6562       /* Parse the declarator.  */
6563       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6564                                          /*ctor_dtor_or_conv_p=*/NULL,
6565                                          /*parenthesized_p=*/NULL,
6566                                          /*member_p=*/false);
6567       /* Parse the attributes.  */
6568       attributes = cp_parser_attributes_opt (parser);
6569       /* Parse the asm-specification.  */
6570       asm_specification = cp_parser_asm_specification_opt (parser);
6571       /* If the next token is not an `=', then we might still be
6572          looking at an expression.  For example:
6573
6574            if (A(a).x)
6575
6576          looks like a decl-specifier-seq and a declarator -- but then
6577          there is no `=', so this is an expression.  */
6578       cp_parser_require (parser, CPP_EQ, "`='");
6579       /* If we did see an `=', then we are looking at a declaration
6580          for sure.  */
6581       if (cp_parser_parse_definitely (parser))
6582         {
6583           tree pushed_scope;
6584           bool non_constant_p;
6585
6586           /* Create the declaration.  */
6587           decl = start_decl (declarator, &type_specifiers,
6588                              /*initialized_p=*/true,
6589                              attributes, /*prefix_attributes=*/NULL_TREE,
6590                              &pushed_scope);
6591           /* Parse the assignment-expression.  */
6592           initializer
6593             = cp_parser_constant_expression (parser,
6594                                              /*allow_non_constant_p=*/true,
6595                                              &non_constant_p);
6596           if (!non_constant_p)
6597             initializer = fold_non_dependent_expr (initializer);
6598
6599           /* Process the initializer.  */
6600           cp_finish_decl (decl,
6601                           initializer, !non_constant_p,
6602                           asm_specification,
6603                           LOOKUP_ONLYCONVERTING);
6604
6605           if (pushed_scope)
6606             pop_scope (pushed_scope);
6607
6608           return convert_from_reference (decl);
6609         }
6610     }
6611   /* If we didn't even get past the declarator successfully, we are
6612      definitely not looking at a declaration.  */
6613   else
6614     cp_parser_abort_tentative_parse (parser);
6615
6616   /* Otherwise, we are looking at an expression.  */
6617   return cp_parser_expression (parser, /*cast_p=*/false);
6618 }
6619
6620 /* Parse an iteration-statement.
6621
6622    iteration-statement:
6623      while ( condition ) statement
6624      do statement while ( expression ) ;
6625      for ( for-init-statement condition [opt] ; expression [opt] )
6626        statement
6627
6628    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6629
6630 static tree
6631 cp_parser_iteration_statement (cp_parser* parser)
6632 {
6633   cp_token *token;
6634   enum rid keyword;
6635   tree statement;
6636   unsigned char in_statement;
6637
6638   /* Peek at the next token.  */
6639   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6640   if (!token)
6641     return error_mark_node;
6642
6643   /* Remember whether or not we are already within an iteration
6644      statement.  */
6645   in_statement = parser->in_statement;
6646
6647   /* See what kind of keyword it is.  */
6648   keyword = token->keyword;
6649   switch (keyword)
6650     {
6651     case RID_WHILE:
6652       {
6653         tree condition;
6654
6655         /* Begin the while-statement.  */
6656         statement = begin_while_stmt ();
6657         /* Look for the `('.  */
6658         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6659         /* Parse the condition.  */
6660         condition = cp_parser_condition (parser);
6661         finish_while_stmt_cond (condition, statement);
6662         /* Look for the `)'.  */
6663         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6664         /* Parse the dependent statement.  */
6665         parser->in_statement = IN_ITERATION_STMT;
6666         cp_parser_already_scoped_statement (parser);
6667         parser->in_statement = in_statement;
6668         /* We're done with the while-statement.  */
6669         finish_while_stmt (statement);
6670       }
6671       break;
6672
6673     case RID_DO:
6674       {
6675         tree expression;
6676
6677         /* Begin the do-statement.  */
6678         statement = begin_do_stmt ();
6679         /* Parse the body of the do-statement.  */
6680         parser->in_statement = IN_ITERATION_STMT;
6681         cp_parser_implicitly_scoped_statement (parser);
6682         parser->in_statement = in_statement;
6683         finish_do_body (statement);
6684         /* Look for the `while' keyword.  */
6685         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6686         /* Look for the `('.  */
6687         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6688         /* Parse the expression.  */
6689         expression = cp_parser_expression (parser, /*cast_p=*/false);
6690         /* We're done with the do-statement.  */
6691         finish_do_stmt (expression, statement);
6692         /* Look for the `)'.  */
6693         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6694         /* Look for the `;'.  */
6695         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6696       }
6697       break;
6698
6699     case RID_FOR:
6700       {
6701         tree condition = NULL_TREE;
6702         tree expression = NULL_TREE;
6703
6704         /* Begin the for-statement.  */
6705         statement = begin_for_stmt ();
6706         /* Look for the `('.  */
6707         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6708         /* Parse the initialization.  */
6709         cp_parser_for_init_statement (parser);
6710         finish_for_init_stmt (statement);
6711
6712         /* If there's a condition, process it.  */
6713         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6714           condition = cp_parser_condition (parser);
6715         finish_for_cond (condition, statement);
6716         /* Look for the `;'.  */
6717         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6718
6719         /* If there's an expression, process it.  */
6720         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6721           expression = cp_parser_expression (parser, /*cast_p=*/false);
6722         finish_for_expr (expression, statement);
6723         /* Look for the `)'.  */
6724         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6725
6726         /* Parse the body of the for-statement.  */
6727         parser->in_statement = IN_ITERATION_STMT;
6728         cp_parser_already_scoped_statement (parser);
6729         parser->in_statement = in_statement;
6730
6731         /* We're done with the for-statement.  */
6732         finish_for_stmt (statement);
6733       }
6734       break;
6735
6736     default:
6737       cp_parser_error (parser, "expected iteration-statement");
6738       statement = error_mark_node;
6739       break;
6740     }
6741
6742   return statement;
6743 }
6744
6745 /* Parse a for-init-statement.
6746
6747    for-init-statement:
6748      expression-statement
6749      simple-declaration  */
6750
6751 static void
6752 cp_parser_for_init_statement (cp_parser* parser)
6753 {
6754   /* If the next token is a `;', then we have an empty
6755      expression-statement.  Grammatically, this is also a
6756      simple-declaration, but an invalid one, because it does not
6757      declare anything.  Therefore, if we did not handle this case
6758      specially, we would issue an error message about an invalid
6759      declaration.  */
6760   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6761     {
6762       /* We're going to speculatively look for a declaration, falling back
6763          to an expression, if necessary.  */
6764       cp_parser_parse_tentatively (parser);
6765       /* Parse the declaration.  */
6766       cp_parser_simple_declaration (parser,
6767                                     /*function_definition_allowed_p=*/false);
6768       /* If the tentative parse failed, then we shall need to look for an
6769          expression-statement.  */
6770       if (cp_parser_parse_definitely (parser))
6771         return;
6772     }
6773
6774   cp_parser_expression_statement (parser, false);
6775 }
6776
6777 /* Parse a jump-statement.
6778
6779    jump-statement:
6780      break ;
6781      continue ;
6782      return expression [opt] ;
6783      goto identifier ;
6784
6785    GNU extension:
6786
6787    jump-statement:
6788      goto * expression ;
6789
6790    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6791
6792 static tree
6793 cp_parser_jump_statement (cp_parser* parser)
6794 {
6795   tree statement = error_mark_node;
6796   cp_token *token;
6797   enum rid keyword;
6798
6799   /* Peek at the next token.  */
6800   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6801   if (!token)
6802     return error_mark_node;
6803
6804   /* See what kind of keyword it is.  */
6805   keyword = token->keyword;
6806   switch (keyword)
6807     {
6808     case RID_BREAK:
6809       switch (parser->in_statement)
6810         {
6811         case 0:
6812           error ("break statement not within loop or switch");
6813           break;
6814         default:
6815           gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6816                       || parser->in_statement == IN_ITERATION_STMT);
6817           statement = finish_break_stmt ();
6818           break;
6819         case IN_OMP_BLOCK:
6820           error ("invalid exit from OpenMP structured block");
6821           break;
6822         case IN_OMP_FOR:
6823           error ("break statement used with OpenMP for loop");
6824           break;
6825         }
6826       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6827       break;
6828
6829     case RID_CONTINUE:
6830       switch (parser->in_statement & ~IN_SWITCH_STMT)
6831         {
6832         case 0:
6833           error ("continue statement not within a loop");
6834           break;
6835         case IN_ITERATION_STMT:
6836         case IN_OMP_FOR:
6837           statement = finish_continue_stmt ();
6838           break;
6839         case IN_OMP_BLOCK:
6840           error ("invalid exit from OpenMP structured block");
6841           break;
6842         default:
6843           gcc_unreachable ();
6844         }
6845       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6846       break;
6847
6848     case RID_RETURN:
6849       {
6850         tree expr;
6851
6852         /* If the next token is a `;', then there is no
6853            expression.  */
6854         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6855           expr = cp_parser_expression (parser, /*cast_p=*/false);
6856         else
6857           expr = NULL_TREE;
6858         /* Build the return-statement.  */
6859         statement = finish_return_stmt (expr);
6860         /* Look for the final `;'.  */
6861         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6862       }
6863       break;
6864
6865     case RID_GOTO:
6866       /* Create the goto-statement.  */
6867       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6868         {
6869           /* Issue a warning about this use of a GNU extension.  */
6870           if (pedantic)
6871             pedwarn ("ISO C++ forbids computed gotos");
6872           /* Consume the '*' token.  */
6873           cp_lexer_consume_token (parser->lexer);
6874           /* Parse the dependent expression.  */
6875           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6876         }
6877       else
6878         finish_goto_stmt (cp_parser_identifier (parser));
6879       /* Look for the final `;'.  */
6880       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6881       break;
6882
6883     default:
6884       cp_parser_error (parser, "expected jump-statement");
6885       break;
6886     }
6887
6888   return statement;
6889 }
6890
6891 /* Parse a declaration-statement.
6892
6893    declaration-statement:
6894      block-declaration  */
6895
6896 static void
6897 cp_parser_declaration_statement (cp_parser* parser)
6898 {
6899   void *p;
6900
6901   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6902   p = obstack_alloc (&declarator_obstack, 0);
6903
6904  /* Parse the block-declaration.  */
6905   cp_parser_block_declaration (parser, /*statement_p=*/true);
6906
6907   /* Free any declarators allocated.  */
6908   obstack_free (&declarator_obstack, p);
6909
6910   /* Finish off the statement.  */
6911   finish_stmt ();
6912 }
6913
6914 /* Some dependent statements (like `if (cond) statement'), are
6915    implicitly in their own scope.  In other words, if the statement is
6916    a single statement (as opposed to a compound-statement), it is
6917    none-the-less treated as if it were enclosed in braces.  Any
6918    declarations appearing in the dependent statement are out of scope
6919    after control passes that point.  This function parses a statement,
6920    but ensures that is in its own scope, even if it is not a
6921    compound-statement.
6922
6923    Returns the new statement.  */
6924
6925 static tree
6926 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6927 {
6928   tree statement;
6929
6930   /* Mark if () ; with a special NOP_EXPR.  */
6931   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6932     {
6933       cp_lexer_consume_token (parser->lexer);
6934       statement = add_stmt (build_empty_stmt ());
6935     }
6936   /* if a compound is opened, we simply parse the statement directly.  */
6937   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6938     statement = cp_parser_compound_statement (parser, NULL, false);
6939   /* If the token is not a `{', then we must take special action.  */
6940   else
6941     {
6942       /* Create a compound-statement.  */
6943       statement = begin_compound_stmt (0);
6944       /* Parse the dependent-statement.  */
6945       cp_parser_statement (parser, NULL_TREE, false);
6946       /* Finish the dummy compound-statement.  */
6947       finish_compound_stmt (statement);
6948     }
6949
6950   /* Return the statement.  */
6951   return statement;
6952 }
6953
6954 /* For some dependent statements (like `while (cond) statement'), we
6955    have already created a scope.  Therefore, even if the dependent
6956    statement is a compound-statement, we do not want to create another
6957    scope.  */
6958
6959 static void
6960 cp_parser_already_scoped_statement (cp_parser* parser)
6961 {
6962   /* If the token is a `{', then we must take special action.  */
6963   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6964     cp_parser_statement (parser, NULL_TREE, false);
6965   else
6966     {
6967       /* Avoid calling cp_parser_compound_statement, so that we
6968          don't create a new scope.  Do everything else by hand.  */
6969       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6970       cp_parser_statement_seq_opt (parser, NULL_TREE);
6971       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6972     }
6973 }
6974
6975 /* Declarations [gram.dcl.dcl] */
6976
6977 /* Parse an optional declaration-sequence.
6978
6979    declaration-seq:
6980      declaration
6981      declaration-seq declaration  */
6982
6983 static void
6984 cp_parser_declaration_seq_opt (cp_parser* parser)
6985 {
6986   while (true)
6987     {
6988       cp_token *token;
6989
6990       token = cp_lexer_peek_token (parser->lexer);
6991
6992       if (token->type == CPP_CLOSE_BRACE
6993           || token->type == CPP_EOF
6994           || token->type == CPP_PRAGMA_EOL)
6995         break;
6996
6997       if (token->type == CPP_SEMICOLON)
6998         {
6999           /* A declaration consisting of a single semicolon is
7000              invalid.  Allow it unless we're being pedantic.  */
7001           cp_lexer_consume_token (parser->lexer);
7002           if (pedantic && !in_system_header)
7003             pedwarn ("extra %<;%>");
7004           continue;
7005         }
7006
7007       /* If we're entering or exiting a region that's implicitly
7008          extern "C", modify the lang context appropriately.  */
7009       if (!parser->implicit_extern_c && token->implicit_extern_c)
7010         {
7011           push_lang_context (lang_name_c);
7012           parser->implicit_extern_c = true;
7013         }
7014       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7015         {
7016           pop_lang_context ();
7017           parser->implicit_extern_c = false;
7018         }
7019
7020       if (token->type == CPP_PRAGMA)
7021         {
7022           /* A top-level declaration can consist solely of a #pragma.
7023              A nested declaration cannot, so this is done here and not
7024              in cp_parser_declaration.  (A #pragma at block scope is
7025              handled in cp_parser_statement.)  */
7026           cp_parser_pragma (parser, pragma_external);
7027           continue;
7028         }
7029
7030       /* Parse the declaration itself.  */
7031       cp_parser_declaration (parser);
7032     }
7033 }
7034
7035 /* Parse a declaration.
7036
7037    declaration:
7038      block-declaration
7039      function-definition
7040      template-declaration
7041      explicit-instantiation
7042      explicit-specialization
7043      linkage-specification
7044      namespace-definition
7045
7046    GNU extension:
7047
7048    declaration:
7049       __extension__ declaration */
7050
7051 static void
7052 cp_parser_declaration (cp_parser* parser)
7053 {
7054   cp_token token1;
7055   cp_token token2;
7056   int saved_pedantic;
7057   void *p;
7058
7059   /* Check for the `__extension__' keyword.  */
7060   if (cp_parser_extension_opt (parser, &saved_pedantic))
7061     {
7062       /* Parse the qualified declaration.  */
7063       cp_parser_declaration (parser);
7064       /* Restore the PEDANTIC flag.  */
7065       pedantic = saved_pedantic;
7066
7067       return;
7068     }
7069
7070   /* Try to figure out what kind of declaration is present.  */
7071   token1 = *cp_lexer_peek_token (parser->lexer);
7072
7073   if (token1.type != CPP_EOF)
7074     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7075   else
7076     {
7077       token2.type = CPP_EOF;
7078       token2.keyword = RID_MAX;
7079     }
7080
7081   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7082   p = obstack_alloc (&declarator_obstack, 0);
7083
7084   /* If the next token is `extern' and the following token is a string
7085      literal, then we have a linkage specification.  */
7086   if (token1.keyword == RID_EXTERN
7087       && cp_parser_is_string_literal (&token2))
7088     cp_parser_linkage_specification (parser);
7089   /* If the next token is `template', then we have either a template
7090      declaration, an explicit instantiation, or an explicit
7091      specialization.  */
7092   else if (token1.keyword == RID_TEMPLATE)
7093     {
7094       /* `template <>' indicates a template specialization.  */
7095       if (token2.type == CPP_LESS
7096           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7097         cp_parser_explicit_specialization (parser);
7098       /* `template <' indicates a template declaration.  */
7099       else if (token2.type == CPP_LESS)
7100         cp_parser_template_declaration (parser, /*member_p=*/false);
7101       /* Anything else must be an explicit instantiation.  */
7102       else
7103         cp_parser_explicit_instantiation (parser);
7104     }
7105   /* If the next token is `export', then we have a template
7106      declaration.  */
7107   else if (token1.keyword == RID_EXPORT)
7108     cp_parser_template_declaration (parser, /*member_p=*/false);
7109   /* If the next token is `extern', 'static' or 'inline' and the one
7110      after that is `template', we have a GNU extended explicit
7111      instantiation directive.  */
7112   else if (cp_parser_allow_gnu_extensions_p (parser)
7113            && (token1.keyword == RID_EXTERN
7114                || token1.keyword == RID_STATIC
7115                || token1.keyword == RID_INLINE)
7116            && token2.keyword == RID_TEMPLATE)
7117     cp_parser_explicit_instantiation (parser);
7118   /* If the next token is `namespace', check for a named or unnamed
7119      namespace definition.  */
7120   else if (token1.keyword == RID_NAMESPACE
7121            && (/* A named namespace definition.  */
7122                (token2.type == CPP_NAME
7123                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7124                     != CPP_EQ))
7125                /* An unnamed namespace definition.  */
7126                || token2.type == CPP_OPEN_BRACE
7127                || token2.keyword == RID_ATTRIBUTE))
7128     cp_parser_namespace_definition (parser);
7129   /* Objective-C++ declaration/definition.  */
7130   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7131     cp_parser_objc_declaration (parser);
7132   /* We must have either a block declaration or a function
7133      definition.  */
7134   else
7135     /* Try to parse a block-declaration, or a function-definition.  */
7136     cp_parser_block_declaration (parser, /*statement_p=*/false);
7137
7138   /* Free any declarators allocated.  */
7139   obstack_free (&declarator_obstack, p);
7140 }
7141
7142 /* Parse a block-declaration.
7143
7144    block-declaration:
7145      simple-declaration
7146      asm-definition
7147      namespace-alias-definition
7148      using-declaration
7149      using-directive
7150
7151    GNU Extension:
7152
7153    block-declaration:
7154      __extension__ block-declaration
7155      label-declaration
7156
7157    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7158    part of a declaration-statement.  */
7159
7160 static void
7161 cp_parser_block_declaration (cp_parser *parser,
7162                              bool      statement_p)
7163 {
7164   cp_token *token1;
7165   int saved_pedantic;
7166
7167   /* Check for the `__extension__' keyword.  */
7168   if (cp_parser_extension_opt (parser, &saved_pedantic))
7169     {
7170       /* Parse the qualified declaration.  */
7171       cp_parser_block_declaration (parser, statement_p);
7172       /* Restore the PEDANTIC flag.  */
7173       pedantic = saved_pedantic;
7174
7175       return;
7176     }
7177
7178   /* Peek at the next token to figure out which kind of declaration is
7179      present.  */
7180   token1 = cp_lexer_peek_token (parser->lexer);
7181
7182   /* If the next keyword is `asm', we have an asm-definition.  */
7183   if (token1->keyword == RID_ASM)
7184     {
7185       if (statement_p)
7186         cp_parser_commit_to_tentative_parse (parser);
7187       cp_parser_asm_definition (parser);
7188     }
7189   /* If the next keyword is `namespace', we have a
7190      namespace-alias-definition.  */
7191   else if (token1->keyword == RID_NAMESPACE)
7192     cp_parser_namespace_alias_definition (parser);
7193   /* If the next keyword is `using', we have either a
7194      using-declaration or a using-directive.  */
7195   else if (token1->keyword == RID_USING)
7196     {
7197       cp_token *token2;
7198
7199       if (statement_p)
7200         cp_parser_commit_to_tentative_parse (parser);
7201       /* If the token after `using' is `namespace', then we have a
7202          using-directive.  */
7203       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7204       if (token2->keyword == RID_NAMESPACE)
7205         cp_parser_using_directive (parser);
7206       /* Otherwise, it's a using-declaration.  */
7207       else
7208         cp_parser_using_declaration (parser);
7209     }
7210   /* If the next keyword is `__label__' we have a label declaration.  */
7211   else if (token1->keyword == RID_LABEL)
7212     {
7213       if (statement_p)
7214         cp_parser_commit_to_tentative_parse (parser);
7215       cp_parser_label_declaration (parser);
7216     }
7217   /* Anything else must be a simple-declaration.  */
7218   else
7219     cp_parser_simple_declaration (parser, !statement_p);
7220 }
7221
7222 /* Parse a simple-declaration.
7223
7224    simple-declaration:
7225      decl-specifier-seq [opt] init-declarator-list [opt] ;
7226
7227    init-declarator-list:
7228      init-declarator
7229      init-declarator-list , init-declarator
7230
7231    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7232    function-definition as a simple-declaration.  */
7233
7234 static void
7235 cp_parser_simple_declaration (cp_parser* parser,
7236                               bool function_definition_allowed_p)
7237 {
7238   cp_decl_specifier_seq decl_specifiers;
7239   int declares_class_or_enum;
7240   bool saw_declarator;
7241
7242   /* Defer access checks until we know what is being declared; the
7243      checks for names appearing in the decl-specifier-seq should be
7244      done as if we were in the scope of the thing being declared.  */
7245   push_deferring_access_checks (dk_deferred);
7246
7247   /* Parse the decl-specifier-seq.  We have to keep track of whether
7248      or not the decl-specifier-seq declares a named class or
7249      enumeration type, since that is the only case in which the
7250      init-declarator-list is allowed to be empty.
7251
7252      [dcl.dcl]
7253
7254      In a simple-declaration, the optional init-declarator-list can be
7255      omitted only when declaring a class or enumeration, that is when
7256      the decl-specifier-seq contains either a class-specifier, an
7257      elaborated-type-specifier, or an enum-specifier.  */
7258   cp_parser_decl_specifier_seq (parser,
7259                                 CP_PARSER_FLAGS_OPTIONAL,
7260                                 &decl_specifiers,
7261                                 &declares_class_or_enum);
7262   /* We no longer need to defer access checks.  */
7263   stop_deferring_access_checks ();
7264
7265   /* In a block scope, a valid declaration must always have a
7266      decl-specifier-seq.  By not trying to parse declarators, we can
7267      resolve the declaration/expression ambiguity more quickly.  */
7268   if (!function_definition_allowed_p
7269       && !decl_specifiers.any_specifiers_p)
7270     {
7271       cp_parser_error (parser, "expected declaration");
7272       goto done;
7273     }
7274
7275   /* If the next two tokens are both identifiers, the code is
7276      erroneous. The usual cause of this situation is code like:
7277
7278        T t;
7279
7280      where "T" should name a type -- but does not.  */
7281   if (!decl_specifiers.type
7282       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7283     {
7284       /* If parsing tentatively, we should commit; we really are
7285          looking at a declaration.  */
7286       cp_parser_commit_to_tentative_parse (parser);
7287       /* Give up.  */
7288       goto done;
7289     }
7290
7291   /* If we have seen at least one decl-specifier, and the next token
7292      is not a parenthesis, then we must be looking at a declaration.
7293      (After "int (" we might be looking at a functional cast.)  */
7294   if (decl_specifiers.any_specifiers_p
7295       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7296     cp_parser_commit_to_tentative_parse (parser);
7297
7298   /* Keep going until we hit the `;' at the end of the simple
7299      declaration.  */
7300   saw_declarator = false;
7301   while (cp_lexer_next_token_is_not (parser->lexer,
7302                                      CPP_SEMICOLON))
7303     {
7304       cp_token *token;
7305       bool function_definition_p;
7306       tree decl;
7307
7308       if (saw_declarator)
7309         {
7310           /* If we are processing next declarator, coma is expected */
7311           token = cp_lexer_peek_token (parser->lexer);
7312           gcc_assert (token->type == CPP_COMMA);
7313           cp_lexer_consume_token (parser->lexer);
7314         }
7315       else
7316         saw_declarator = true;
7317
7318       /* Parse the init-declarator.  */
7319       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7320                                         /*checks=*/NULL_TREE,
7321                                         function_definition_allowed_p,
7322                                         /*member_p=*/false,
7323                                         declares_class_or_enum,
7324                                         &function_definition_p);
7325       /* If an error occurred while parsing tentatively, exit quickly.
7326          (That usually happens when in the body of a function; each
7327          statement is treated as a declaration-statement until proven
7328          otherwise.)  */
7329       if (cp_parser_error_occurred (parser))
7330         goto done;
7331       /* Handle function definitions specially.  */
7332       if (function_definition_p)
7333         {
7334           /* If the next token is a `,', then we are probably
7335              processing something like:
7336
7337                void f() {}, *p;
7338
7339              which is erroneous.  */
7340           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7341             error ("mixing declarations and function-definitions is forbidden");
7342           /* Otherwise, we're done with the list of declarators.  */
7343           else
7344             {
7345               pop_deferring_access_checks ();
7346               return;
7347             }
7348         }
7349       /* The next token should be either a `,' or a `;'.  */
7350       token = cp_lexer_peek_token (parser->lexer);
7351       /* If it's a `,', there are more declarators to come.  */
7352       if (token->type == CPP_COMMA)
7353         /* will be consumed next time around */;
7354       /* If it's a `;', we are done.  */
7355       else if (token->type == CPP_SEMICOLON)
7356         break;
7357       /* Anything else is an error.  */
7358       else
7359         {
7360           /* If we have already issued an error message we don't need
7361              to issue another one.  */
7362           if (decl != error_mark_node
7363               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7364             cp_parser_error (parser, "expected %<,%> or %<;%>");
7365           /* Skip tokens until we reach the end of the statement.  */
7366           cp_parser_skip_to_end_of_statement (parser);
7367           /* If the next token is now a `;', consume it.  */
7368           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7369             cp_lexer_consume_token (parser->lexer);
7370           goto done;
7371         }
7372       /* After the first time around, a function-definition is not
7373          allowed -- even if it was OK at first.  For example:
7374
7375            int i, f() {}
7376
7377          is not valid.  */
7378       function_definition_allowed_p = false;
7379     }
7380
7381   /* Issue an error message if no declarators are present, and the
7382      decl-specifier-seq does not itself declare a class or
7383      enumeration.  */
7384   if (!saw_declarator)
7385     {
7386       if (cp_parser_declares_only_class_p (parser))
7387         shadow_tag (&decl_specifiers);
7388       /* Perform any deferred access checks.  */
7389       perform_deferred_access_checks ();
7390     }
7391
7392   /* Consume the `;'.  */
7393   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7394
7395  done:
7396   pop_deferring_access_checks ();
7397 }
7398
7399 /* Parse a decl-specifier-seq.
7400
7401    decl-specifier-seq:
7402      decl-specifier-seq [opt] decl-specifier
7403
7404    decl-specifier:
7405      storage-class-specifier
7406      type-specifier
7407      function-specifier
7408      friend
7409      typedef
7410
7411    GNU Extension:
7412
7413    decl-specifier:
7414      attributes
7415
7416    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7417
7418    The parser flags FLAGS is used to control type-specifier parsing.
7419
7420    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7421    flags:
7422
7423      1: one of the decl-specifiers is an elaborated-type-specifier
7424         (i.e., a type declaration)
7425      2: one of the decl-specifiers is an enum-specifier or a
7426         class-specifier (i.e., a type definition)
7427
7428    */
7429
7430 static void
7431 cp_parser_decl_specifier_seq (cp_parser* parser,
7432                               cp_parser_flags flags,
7433                               cp_decl_specifier_seq *decl_specs,
7434                               int* declares_class_or_enum)
7435 {
7436   bool constructor_possible_p = !parser->in_declarator_p;
7437
7438   /* Clear DECL_SPECS.  */
7439   clear_decl_specs (decl_specs);
7440
7441   /* Assume no class or enumeration type is declared.  */
7442   *declares_class_or_enum = 0;
7443
7444   /* Keep reading specifiers until there are no more to read.  */
7445   while (true)
7446     {
7447       bool constructor_p;
7448       bool found_decl_spec;
7449       cp_token *token;
7450
7451       /* Peek at the next token.  */
7452       token = cp_lexer_peek_token (parser->lexer);
7453       /* Handle attributes.  */
7454       if (token->keyword == RID_ATTRIBUTE)
7455         {
7456           /* Parse the attributes.  */
7457           decl_specs->attributes
7458             = chainon (decl_specs->attributes,
7459                        cp_parser_attributes_opt (parser));
7460           continue;
7461         }
7462       /* Assume we will find a decl-specifier keyword.  */
7463       found_decl_spec = true;
7464       /* If the next token is an appropriate keyword, we can simply
7465          add it to the list.  */
7466       switch (token->keyword)
7467         {
7468           /* decl-specifier:
7469                friend  */
7470         case RID_FRIEND:
7471           if (!at_class_scope_p ())
7472             {
7473               error ("%<friend%> used outside of class");
7474               cp_lexer_purge_token (parser->lexer);
7475             }
7476           else
7477             {
7478               ++decl_specs->specs[(int) ds_friend];
7479               /* Consume the token.  */
7480               cp_lexer_consume_token (parser->lexer);
7481             }
7482           break;
7483
7484           /* function-specifier:
7485                inline
7486                virtual
7487                explicit  */
7488         case RID_INLINE:
7489         case RID_VIRTUAL:
7490         case RID_EXPLICIT:
7491           cp_parser_function_specifier_opt (parser, decl_specs);
7492           break;
7493
7494           /* decl-specifier:
7495                typedef  */
7496         case RID_TYPEDEF:
7497           ++decl_specs->specs[(int) ds_typedef];
7498           /* Consume the token.  */
7499           cp_lexer_consume_token (parser->lexer);
7500           /* A constructor declarator cannot appear in a typedef.  */
7501           constructor_possible_p = false;
7502           /* The "typedef" keyword can only occur in a declaration; we
7503              may as well commit at this point.  */
7504           cp_parser_commit_to_tentative_parse (parser);
7505           break;
7506
7507           /* storage-class-specifier:
7508                auto
7509                register
7510                static
7511                extern
7512                mutable
7513
7514              GNU Extension:
7515                thread  */
7516         case RID_AUTO:
7517         case RID_REGISTER:
7518         case RID_STATIC:
7519         case RID_EXTERN:
7520         case RID_MUTABLE:
7521           /* Consume the token.  */
7522           cp_lexer_consume_token (parser->lexer);
7523           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7524           break;
7525         case RID_THREAD:
7526           /* Consume the token.  */
7527           cp_lexer_consume_token (parser->lexer);
7528           ++decl_specs->specs[(int) ds_thread];
7529           break;
7530
7531         default:
7532           /* We did not yet find a decl-specifier yet.  */
7533           found_decl_spec = false;
7534           break;
7535         }
7536
7537       /* Constructors are a special case.  The `S' in `S()' is not a
7538          decl-specifier; it is the beginning of the declarator.  */
7539       constructor_p
7540         = (!found_decl_spec
7541            && constructor_possible_p
7542            && (cp_parser_constructor_declarator_p
7543                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7544
7545       /* If we don't have a DECL_SPEC yet, then we must be looking at
7546          a type-specifier.  */
7547       if (!found_decl_spec && !constructor_p)
7548         {
7549           int decl_spec_declares_class_or_enum;
7550           bool is_cv_qualifier;
7551           tree type_spec;
7552
7553           type_spec
7554             = cp_parser_type_specifier (parser, flags,
7555                                         decl_specs,
7556                                         /*is_declaration=*/true,
7557                                         &decl_spec_declares_class_or_enum,
7558                                         &is_cv_qualifier);
7559
7560           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7561
7562           /* If this type-specifier referenced a user-defined type
7563              (a typedef, class-name, etc.), then we can't allow any
7564              more such type-specifiers henceforth.
7565
7566              [dcl.spec]
7567
7568              The longest sequence of decl-specifiers that could
7569              possibly be a type name is taken as the
7570              decl-specifier-seq of a declaration.  The sequence shall
7571              be self-consistent as described below.
7572
7573              [dcl.type]
7574
7575              As a general rule, at most one type-specifier is allowed
7576              in the complete decl-specifier-seq of a declaration.  The
7577              only exceptions are the following:
7578
7579              -- const or volatile can be combined with any other
7580                 type-specifier.
7581
7582              -- signed or unsigned can be combined with char, long,
7583                 short, or int.
7584
7585              -- ..
7586
7587              Example:
7588
7589                typedef char* Pc;
7590                void g (const int Pc);
7591
7592              Here, Pc is *not* part of the decl-specifier seq; it's
7593              the declarator.  Therefore, once we see a type-specifier
7594              (other than a cv-qualifier), we forbid any additional
7595              user-defined types.  We *do* still allow things like `int
7596              int' to be considered a decl-specifier-seq, and issue the
7597              error message later.  */
7598           if (type_spec && !is_cv_qualifier)
7599             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7600           /* A constructor declarator cannot follow a type-specifier.  */
7601           if (type_spec)
7602             {
7603               constructor_possible_p = false;
7604               found_decl_spec = true;
7605             }
7606         }
7607
7608       /* If we still do not have a DECL_SPEC, then there are no more
7609          decl-specifiers.  */
7610       if (!found_decl_spec)
7611         break;
7612
7613       decl_specs->any_specifiers_p = true;
7614       /* After we see one decl-specifier, further decl-specifiers are
7615          always optional.  */
7616       flags |= CP_PARSER_FLAGS_OPTIONAL;
7617     }
7618
7619   cp_parser_check_decl_spec (decl_specs);
7620
7621   /* Don't allow a friend specifier with a class definition.  */
7622   if (decl_specs->specs[(int) ds_friend] != 0
7623       && (*declares_class_or_enum & 2))
7624     error ("class definition may not be declared a friend");
7625 }
7626
7627 /* Parse an (optional) storage-class-specifier.
7628
7629    storage-class-specifier:
7630      auto
7631      register
7632      static
7633      extern
7634      mutable
7635
7636    GNU Extension:
7637
7638    storage-class-specifier:
7639      thread
7640
7641    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7642
7643 static tree
7644 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7645 {
7646   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7647     {
7648     case RID_AUTO:
7649     case RID_REGISTER:
7650     case RID_STATIC:
7651     case RID_EXTERN:
7652     case RID_MUTABLE:
7653     case RID_THREAD:
7654       /* Consume the token.  */
7655       return cp_lexer_consume_token (parser->lexer)->value;
7656
7657     default:
7658       return NULL_TREE;
7659     }
7660 }
7661
7662 /* Parse an (optional) function-specifier.
7663
7664    function-specifier:
7665      inline
7666      virtual
7667      explicit
7668
7669    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7670    Updates DECL_SPECS, if it is non-NULL.  */
7671
7672 static tree
7673 cp_parser_function_specifier_opt (cp_parser* parser,
7674                                   cp_decl_specifier_seq *decl_specs)
7675 {
7676   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7677     {
7678     case RID_INLINE:
7679       if (decl_specs)
7680         ++decl_specs->specs[(int) ds_inline];
7681       break;
7682
7683     case RID_VIRTUAL:
7684       /* 14.5.2.3 [temp.mem]
7685
7686          A member function template shall not be virtual.  */
7687       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7688         error ("templates may not be %<virtual%>");
7689       else if (decl_specs)
7690         ++decl_specs->specs[(int) ds_virtual];
7691       break;
7692
7693     case RID_EXPLICIT:
7694       if (decl_specs)
7695         ++decl_specs->specs[(int) ds_explicit];
7696       break;
7697
7698     default:
7699       return NULL_TREE;
7700     }
7701
7702   /* Consume the token.  */
7703   return cp_lexer_consume_token (parser->lexer)->value;
7704 }
7705
7706 /* Parse a linkage-specification.
7707
7708    linkage-specification:
7709      extern string-literal { declaration-seq [opt] }
7710      extern string-literal declaration  */
7711
7712 static void
7713 cp_parser_linkage_specification (cp_parser* parser)
7714 {
7715   tree linkage;
7716
7717   /* Look for the `extern' keyword.  */
7718   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7719
7720   /* Look for the string-literal.  */
7721   linkage = cp_parser_string_literal (parser, false, false);
7722
7723   /* Transform the literal into an identifier.  If the literal is a
7724      wide-character string, or contains embedded NULs, then we can't
7725      handle it as the user wants.  */
7726   if (strlen (TREE_STRING_POINTER (linkage))
7727       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7728     {
7729       cp_parser_error (parser, "invalid linkage-specification");
7730       /* Assume C++ linkage.  */
7731       linkage = lang_name_cplusplus;
7732     }
7733   else
7734     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7735
7736   /* We're now using the new linkage.  */
7737   push_lang_context (linkage);
7738
7739   /* If the next token is a `{', then we're using the first
7740      production.  */
7741   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7742     {
7743       /* Consume the `{' token.  */
7744       cp_lexer_consume_token (parser->lexer);
7745       /* Parse the declarations.  */
7746       cp_parser_declaration_seq_opt (parser);
7747       /* Look for the closing `}'.  */
7748       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7749     }
7750   /* Otherwise, there's just one declaration.  */
7751   else
7752     {
7753       bool saved_in_unbraced_linkage_specification_p;
7754
7755       saved_in_unbraced_linkage_specification_p
7756         = parser->in_unbraced_linkage_specification_p;
7757       parser->in_unbraced_linkage_specification_p = true;
7758       cp_parser_declaration (parser);
7759       parser->in_unbraced_linkage_specification_p
7760         = saved_in_unbraced_linkage_specification_p;
7761     }
7762
7763   /* We're done with the linkage-specification.  */
7764   pop_lang_context ();
7765 }
7766
7767 /* Special member functions [gram.special] */
7768
7769 /* Parse a conversion-function-id.
7770
7771    conversion-function-id:
7772      operator conversion-type-id
7773
7774    Returns an IDENTIFIER_NODE representing the operator.  */
7775
7776 static tree
7777 cp_parser_conversion_function_id (cp_parser* parser)
7778 {
7779   tree type;
7780   tree saved_scope;
7781   tree saved_qualifying_scope;
7782   tree saved_object_scope;
7783   tree pushed_scope = NULL_TREE;
7784
7785   /* Look for the `operator' token.  */
7786   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7787     return error_mark_node;
7788   /* When we parse the conversion-type-id, the current scope will be
7789      reset.  However, we need that information in able to look up the
7790      conversion function later, so we save it here.  */
7791   saved_scope = parser->scope;
7792   saved_qualifying_scope = parser->qualifying_scope;
7793   saved_object_scope = parser->object_scope;
7794   /* We must enter the scope of the class so that the names of
7795      entities declared within the class are available in the
7796      conversion-type-id.  For example, consider:
7797
7798        struct S {
7799          typedef int I;
7800          operator I();
7801        };
7802
7803        S::operator I() { ... }
7804
7805      In order to see that `I' is a type-name in the definition, we
7806      must be in the scope of `S'.  */
7807   if (saved_scope)
7808     pushed_scope = push_scope (saved_scope);
7809   /* Parse the conversion-type-id.  */
7810   type = cp_parser_conversion_type_id (parser);
7811   /* Leave the scope of the class, if any.  */
7812   if (pushed_scope)
7813     pop_scope (pushed_scope);
7814   /* Restore the saved scope.  */
7815   parser->scope = saved_scope;
7816   parser->qualifying_scope = saved_qualifying_scope;
7817   parser->object_scope = saved_object_scope;
7818   /* If the TYPE is invalid, indicate failure.  */
7819   if (type == error_mark_node)
7820     return error_mark_node;
7821   return mangle_conv_op_name_for_type (type);
7822 }
7823
7824 /* Parse a conversion-type-id:
7825
7826    conversion-type-id:
7827      type-specifier-seq conversion-declarator [opt]
7828
7829    Returns the TYPE specified.  */
7830
7831 static tree
7832 cp_parser_conversion_type_id (cp_parser* parser)
7833 {
7834   tree attributes;
7835   cp_decl_specifier_seq type_specifiers;
7836   cp_declarator *declarator;
7837   tree type_specified;
7838
7839   /* Parse the attributes.  */
7840   attributes = cp_parser_attributes_opt (parser);
7841   /* Parse the type-specifiers.  */
7842   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7843                                 &type_specifiers);
7844   /* If that didn't work, stop.  */
7845   if (type_specifiers.type == error_mark_node)
7846     return error_mark_node;
7847   /* Parse the conversion-declarator.  */
7848   declarator = cp_parser_conversion_declarator_opt (parser);
7849
7850   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7851                                     /*initialized=*/0, &attributes);
7852   if (attributes)
7853     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7854   return type_specified;
7855 }
7856
7857 /* Parse an (optional) conversion-declarator.
7858
7859    conversion-declarator:
7860      ptr-operator conversion-declarator [opt]
7861
7862    */
7863
7864 static cp_declarator *
7865 cp_parser_conversion_declarator_opt (cp_parser* parser)
7866 {
7867   enum tree_code code;
7868   tree class_type;
7869   cp_cv_quals cv_quals;
7870
7871   /* We don't know if there's a ptr-operator next, or not.  */
7872   cp_parser_parse_tentatively (parser);
7873   /* Try the ptr-operator.  */
7874   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7875   /* If it worked, look for more conversion-declarators.  */
7876   if (cp_parser_parse_definitely (parser))
7877     {
7878       cp_declarator *declarator;
7879
7880       /* Parse another optional declarator.  */
7881       declarator = cp_parser_conversion_declarator_opt (parser);
7882
7883       /* Create the representation of the declarator.  */
7884       if (class_type)
7885         declarator = make_ptrmem_declarator (cv_quals, class_type,
7886                                              declarator);
7887       else if (code == INDIRECT_REF)
7888         declarator = make_pointer_declarator (cv_quals, declarator);
7889       else
7890         declarator = make_reference_declarator (cv_quals, declarator);
7891
7892       return declarator;
7893    }
7894
7895   return NULL;
7896 }
7897
7898 /* Parse an (optional) ctor-initializer.
7899
7900    ctor-initializer:
7901      : mem-initializer-list
7902
7903    Returns TRUE iff the ctor-initializer was actually present.  */
7904
7905 static bool
7906 cp_parser_ctor_initializer_opt (cp_parser* parser)
7907 {
7908   /* If the next token is not a `:', then there is no
7909      ctor-initializer.  */
7910   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7911     {
7912       /* Do default initialization of any bases and members.  */
7913       if (DECL_CONSTRUCTOR_P (current_function_decl))
7914         finish_mem_initializers (NULL_TREE);
7915
7916       return false;
7917     }
7918
7919   /* Consume the `:' token.  */
7920   cp_lexer_consume_token (parser->lexer);
7921   /* And the mem-initializer-list.  */
7922   cp_parser_mem_initializer_list (parser);
7923
7924   return true;
7925 }
7926
7927 /* Parse a mem-initializer-list.
7928
7929    mem-initializer-list:
7930      mem-initializer
7931      mem-initializer , mem-initializer-list  */
7932
7933 static void
7934 cp_parser_mem_initializer_list (cp_parser* parser)
7935 {
7936   tree mem_initializer_list = NULL_TREE;
7937
7938   /* Let the semantic analysis code know that we are starting the
7939      mem-initializer-list.  */
7940   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7941     error ("only constructors take base initializers");
7942
7943   /* Loop through the list.  */
7944   while (true)
7945     {
7946       tree mem_initializer;
7947
7948       /* Parse the mem-initializer.  */
7949       mem_initializer = cp_parser_mem_initializer (parser);
7950       /* Add it to the list, unless it was erroneous.  */
7951       if (mem_initializer != error_mark_node)
7952         {
7953           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7954           mem_initializer_list = mem_initializer;
7955         }
7956       /* If the next token is not a `,', we're done.  */
7957       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7958         break;
7959       /* Consume the `,' token.  */
7960       cp_lexer_consume_token (parser->lexer);
7961     }
7962
7963   /* Perform semantic analysis.  */
7964   if (DECL_CONSTRUCTOR_P (current_function_decl))
7965     finish_mem_initializers (mem_initializer_list);
7966 }
7967
7968 /* Parse a mem-initializer.
7969
7970    mem-initializer:
7971      mem-initializer-id ( expression-list [opt] )
7972
7973    GNU extension:
7974
7975    mem-initializer:
7976      ( expression-list [opt] )
7977
7978    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7979    class) or FIELD_DECL (for a non-static data member) to initialize;
7980    the TREE_VALUE is the expression-list.  An empty initialization
7981    list is represented by void_list_node.  */
7982
7983 static tree
7984 cp_parser_mem_initializer (cp_parser* parser)
7985 {
7986   tree mem_initializer_id;
7987   tree expression_list;
7988   tree member;
7989
7990   /* Find out what is being initialized.  */
7991   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7992     {
7993       pedwarn ("anachronistic old-style base class initializer");
7994       mem_initializer_id = NULL_TREE;
7995     }
7996   else
7997     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7998   member = expand_member_init (mem_initializer_id);
7999   if (member && !DECL_P (member))
8000     in_base_initializer = 1;
8001
8002   expression_list
8003     = cp_parser_parenthesized_expression_list (parser, false,
8004                                                /*cast_p=*/false,
8005                                                /*non_constant_p=*/NULL);
8006   if (expression_list == error_mark_node)
8007     return error_mark_node;
8008   if (!expression_list)
8009     expression_list = void_type_node;
8010
8011   in_base_initializer = 0;
8012
8013   return member ? build_tree_list (member, expression_list) : error_mark_node;
8014 }
8015
8016 /* Parse a mem-initializer-id.
8017
8018    mem-initializer-id:
8019      :: [opt] nested-name-specifier [opt] class-name
8020      identifier
8021
8022    Returns a TYPE indicating the class to be initializer for the first
8023    production.  Returns an IDENTIFIER_NODE indicating the data member
8024    to be initialized for the second production.  */
8025
8026 static tree
8027 cp_parser_mem_initializer_id (cp_parser* parser)
8028 {
8029   bool global_scope_p;
8030   bool nested_name_specifier_p;
8031   bool template_p = false;
8032   tree id;
8033
8034   /* `typename' is not allowed in this context ([temp.res]).  */
8035   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8036     {
8037       error ("keyword %<typename%> not allowed in this context (a qualified "
8038              "member initializer is implicitly a type)");
8039       cp_lexer_consume_token (parser->lexer);
8040     }
8041   /* Look for the optional `::' operator.  */
8042   global_scope_p
8043     = (cp_parser_global_scope_opt (parser,
8044                                    /*current_scope_valid_p=*/false)
8045        != NULL_TREE);
8046   /* Look for the optional nested-name-specifier.  The simplest way to
8047      implement:
8048
8049        [temp.res]
8050
8051        The keyword `typename' is not permitted in a base-specifier or
8052        mem-initializer; in these contexts a qualified name that
8053        depends on a template-parameter is implicitly assumed to be a
8054        type name.
8055
8056      is to assume that we have seen the `typename' keyword at this
8057      point.  */
8058   nested_name_specifier_p
8059     = (cp_parser_nested_name_specifier_opt (parser,
8060                                             /*typename_keyword_p=*/true,
8061                                             /*check_dependency_p=*/true,
8062                                             /*type_p=*/true,
8063                                             /*is_declaration=*/true)
8064        != NULL_TREE);
8065   if (nested_name_specifier_p)
8066     template_p = cp_parser_optional_template_keyword (parser);
8067   /* If there is a `::' operator or a nested-name-specifier, then we
8068      are definitely looking for a class-name.  */
8069   if (global_scope_p || nested_name_specifier_p)
8070     return cp_parser_class_name (parser,
8071                                  /*typename_keyword_p=*/true,
8072                                  /*template_keyword_p=*/template_p,
8073                                  none_type,
8074                                  /*check_dependency_p=*/true,
8075                                  /*class_head_p=*/false,
8076                                  /*is_declaration=*/true);
8077   /* Otherwise, we could also be looking for an ordinary identifier.  */
8078   cp_parser_parse_tentatively (parser);
8079   /* Try a class-name.  */
8080   id = cp_parser_class_name (parser,
8081                              /*typename_keyword_p=*/true,
8082                              /*template_keyword_p=*/false,
8083                              none_type,
8084                              /*check_dependency_p=*/true,
8085                              /*class_head_p=*/false,
8086                              /*is_declaration=*/true);
8087   /* If we found one, we're done.  */
8088   if (cp_parser_parse_definitely (parser))
8089     return id;
8090   /* Otherwise, look for an ordinary identifier.  */
8091   return cp_parser_identifier (parser);
8092 }
8093
8094 /* Overloading [gram.over] */
8095
8096 /* Parse an operator-function-id.
8097
8098    operator-function-id:
8099      operator operator
8100
8101    Returns an IDENTIFIER_NODE for the operator which is a
8102    human-readable spelling of the identifier, e.g., `operator +'.  */
8103
8104 static tree
8105 cp_parser_operator_function_id (cp_parser* parser)
8106 {
8107   /* Look for the `operator' keyword.  */
8108   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8109     return error_mark_node;
8110   /* And then the name of the operator itself.  */
8111   return cp_parser_operator (parser);
8112 }
8113
8114 /* Parse an operator.
8115
8116    operator:
8117      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8118      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8119      || ++ -- , ->* -> () []
8120
8121    GNU Extensions:
8122
8123    operator:
8124      <? >? <?= >?=
8125
8126    Returns an IDENTIFIER_NODE for the operator which is a
8127    human-readable spelling of the identifier, e.g., `operator +'.  */
8128
8129 static tree
8130 cp_parser_operator (cp_parser* parser)
8131 {
8132   tree id = NULL_TREE;
8133   cp_token *token;
8134
8135   /* Peek at the next token.  */
8136   token = cp_lexer_peek_token (parser->lexer);
8137   /* Figure out which operator we have.  */
8138   switch (token->type)
8139     {
8140     case CPP_KEYWORD:
8141       {
8142         enum tree_code op;
8143
8144         /* The keyword should be either `new' or `delete'.  */
8145         if (token->keyword == RID_NEW)
8146           op = NEW_EXPR;
8147         else if (token->keyword == RID_DELETE)
8148           op = DELETE_EXPR;
8149         else
8150           break;
8151
8152         /* Consume the `new' or `delete' token.  */
8153         cp_lexer_consume_token (parser->lexer);
8154
8155         /* Peek at the next token.  */
8156         token = cp_lexer_peek_token (parser->lexer);
8157         /* If it's a `[' token then this is the array variant of the
8158            operator.  */
8159         if (token->type == CPP_OPEN_SQUARE)
8160           {
8161             /* Consume the `[' token.  */
8162             cp_lexer_consume_token (parser->lexer);
8163             /* Look for the `]' token.  */
8164             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8165             id = ansi_opname (op == NEW_EXPR
8166                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8167           }
8168         /* Otherwise, we have the non-array variant.  */
8169         else
8170           id = ansi_opname (op);
8171
8172         return id;
8173       }
8174
8175     case CPP_PLUS:
8176       id = ansi_opname (PLUS_EXPR);
8177       break;
8178
8179     case CPP_MINUS:
8180       id = ansi_opname (MINUS_EXPR);
8181       break;
8182
8183     case CPP_MULT:
8184       id = ansi_opname (MULT_EXPR);
8185       break;
8186
8187     case CPP_DIV:
8188       id = ansi_opname (TRUNC_DIV_EXPR);
8189       break;
8190
8191     case CPP_MOD:
8192       id = ansi_opname (TRUNC_MOD_EXPR);
8193       break;
8194
8195     case CPP_XOR:
8196       id = ansi_opname (BIT_XOR_EXPR);
8197       break;
8198
8199     case CPP_AND:
8200       id = ansi_opname (BIT_AND_EXPR);
8201       break;
8202
8203     case CPP_OR:
8204       id = ansi_opname (BIT_IOR_EXPR);
8205       break;
8206
8207     case CPP_COMPL:
8208       id = ansi_opname (BIT_NOT_EXPR);
8209       break;
8210
8211     case CPP_NOT:
8212       id = ansi_opname (TRUTH_NOT_EXPR);
8213       break;
8214
8215     case CPP_EQ:
8216       id = ansi_assopname (NOP_EXPR);
8217       break;
8218
8219     case CPP_LESS:
8220       id = ansi_opname (LT_EXPR);
8221       break;
8222
8223     case CPP_GREATER:
8224       id = ansi_opname (GT_EXPR);
8225       break;
8226
8227     case CPP_PLUS_EQ:
8228       id = ansi_assopname (PLUS_EXPR);
8229       break;
8230
8231     case CPP_MINUS_EQ:
8232       id = ansi_assopname (MINUS_EXPR);
8233       break;
8234
8235     case CPP_MULT_EQ:
8236       id = ansi_assopname (MULT_EXPR);
8237       break;
8238
8239     case CPP_DIV_EQ:
8240       id = ansi_assopname (TRUNC_DIV_EXPR);
8241       break;
8242
8243     case CPP_MOD_EQ:
8244       id = ansi_assopname (TRUNC_MOD_EXPR);
8245       break;
8246
8247     case CPP_XOR_EQ:
8248       id = ansi_assopname (BIT_XOR_EXPR);
8249       break;
8250
8251     case CPP_AND_EQ:
8252       id = ansi_assopname (BIT_AND_EXPR);
8253       break;
8254
8255     case CPP_OR_EQ:
8256       id = ansi_assopname (BIT_IOR_EXPR);
8257       break;
8258
8259     case CPP_LSHIFT:
8260       id = ansi_opname (LSHIFT_EXPR);
8261       break;
8262
8263     case CPP_RSHIFT:
8264       id = ansi_opname (RSHIFT_EXPR);
8265       break;
8266
8267     case CPP_LSHIFT_EQ:
8268       id = ansi_assopname (LSHIFT_EXPR);
8269       break;
8270
8271     case CPP_RSHIFT_EQ:
8272       id = ansi_assopname (RSHIFT_EXPR);
8273       break;
8274
8275     case CPP_EQ_EQ:
8276       id = ansi_opname (EQ_EXPR);
8277       break;
8278
8279     case CPP_NOT_EQ:
8280       id = ansi_opname (NE_EXPR);
8281       break;
8282
8283     case CPP_LESS_EQ:
8284       id = ansi_opname (LE_EXPR);
8285       break;
8286
8287     case CPP_GREATER_EQ:
8288       id = ansi_opname (GE_EXPR);
8289       break;
8290
8291     case CPP_AND_AND:
8292       id = ansi_opname (TRUTH_ANDIF_EXPR);
8293       break;
8294
8295     case CPP_OR_OR:
8296       id = ansi_opname (TRUTH_ORIF_EXPR);
8297       break;
8298
8299     case CPP_PLUS_PLUS:
8300       id = ansi_opname (POSTINCREMENT_EXPR);
8301       break;
8302
8303     case CPP_MINUS_MINUS:
8304       id = ansi_opname (PREDECREMENT_EXPR);
8305       break;
8306
8307     case CPP_COMMA:
8308       id = ansi_opname (COMPOUND_EXPR);
8309       break;
8310
8311     case CPP_DEREF_STAR:
8312       id = ansi_opname (MEMBER_REF);
8313       break;
8314
8315     case CPP_DEREF:
8316       id = ansi_opname (COMPONENT_REF);
8317       break;
8318
8319     case CPP_OPEN_PAREN:
8320       /* Consume the `('.  */
8321       cp_lexer_consume_token (parser->lexer);
8322       /* Look for the matching `)'.  */
8323       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8324       return ansi_opname (CALL_EXPR);
8325
8326     case CPP_OPEN_SQUARE:
8327       /* Consume the `['.  */
8328       cp_lexer_consume_token (parser->lexer);
8329       /* Look for the matching `]'.  */
8330       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8331       return ansi_opname (ARRAY_REF);
8332
8333       /* Extensions.  */
8334     case CPP_MIN:
8335       id = ansi_opname (MIN_EXPR);
8336       cp_parser_warn_min_max ();
8337       break;
8338
8339     case CPP_MAX:
8340       id = ansi_opname (MAX_EXPR);
8341       cp_parser_warn_min_max ();
8342       break;
8343
8344     case CPP_MIN_EQ:
8345       id = ansi_assopname (MIN_EXPR);
8346       cp_parser_warn_min_max ();
8347       break;
8348
8349     case CPP_MAX_EQ:
8350       id = ansi_assopname (MAX_EXPR);
8351       cp_parser_warn_min_max ();
8352       break;
8353
8354     default:
8355       /* Anything else is an error.  */
8356       break;
8357     }
8358
8359   /* If we have selected an identifier, we need to consume the
8360      operator token.  */
8361   if (id)
8362     cp_lexer_consume_token (parser->lexer);
8363   /* Otherwise, no valid operator name was present.  */
8364   else
8365     {
8366       cp_parser_error (parser, "expected operator");
8367       id = error_mark_node;
8368     }
8369
8370   return id;
8371 }
8372
8373 /* Parse a template-declaration.
8374
8375    template-declaration:
8376      export [opt] template < template-parameter-list > declaration
8377
8378    If MEMBER_P is TRUE, this template-declaration occurs within a
8379    class-specifier.
8380
8381    The grammar rule given by the standard isn't correct.  What
8382    is really meant is:
8383
8384    template-declaration:
8385      export [opt] template-parameter-list-seq
8386        decl-specifier-seq [opt] init-declarator [opt] ;
8387      export [opt] template-parameter-list-seq
8388        function-definition
8389
8390    template-parameter-list-seq:
8391      template-parameter-list-seq [opt]
8392      template < template-parameter-list >  */
8393
8394 static void
8395 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8396 {
8397   /* Check for `export'.  */
8398   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8399     {
8400       /* Consume the `export' token.  */
8401       cp_lexer_consume_token (parser->lexer);
8402       /* Warn that we do not support `export'.  */
8403       warning (0, "keyword %<export%> not implemented, and will be ignored");
8404     }
8405
8406   cp_parser_template_declaration_after_export (parser, member_p);
8407 }
8408
8409 /* Parse a template-parameter-list.
8410
8411    template-parameter-list:
8412      template-parameter
8413      template-parameter-list , template-parameter
8414
8415    Returns a TREE_LIST.  Each node represents a template parameter.
8416    The nodes are connected via their TREE_CHAINs.  */
8417
8418 static tree
8419 cp_parser_template_parameter_list (cp_parser* parser)
8420 {
8421   tree parameter_list = NULL_TREE;
8422
8423   begin_template_parm_list ();
8424   while (true)
8425     {
8426       tree parameter;
8427       cp_token *token;
8428       bool is_non_type;
8429
8430       /* Parse the template-parameter.  */
8431       parameter = cp_parser_template_parameter (parser, &is_non_type);
8432       /* Add it to the list.  */
8433       if (parameter != error_mark_node)
8434         parameter_list = process_template_parm (parameter_list,
8435                                                 parameter,
8436                                                 is_non_type);
8437       /* Peek at the next token.  */
8438       token = cp_lexer_peek_token (parser->lexer);
8439       /* If it's not a `,', we're done.  */
8440       if (token->type != CPP_COMMA)
8441         break;
8442       /* Otherwise, consume the `,' token.  */
8443       cp_lexer_consume_token (parser->lexer);
8444     }
8445
8446   return end_template_parm_list (parameter_list);
8447 }
8448
8449 /* Parse a template-parameter.
8450
8451    template-parameter:
8452      type-parameter
8453      parameter-declaration
8454
8455    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8456    the parameter.  The TREE_PURPOSE is the default value, if any.
8457    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8458    iff this parameter is a non-type parameter.  */
8459
8460 static tree
8461 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8462 {
8463   cp_token *token;
8464   cp_parameter_declarator *parameter_declarator;
8465   tree parm;
8466
8467   /* Assume it is a type parameter or a template parameter.  */
8468   *is_non_type = false;
8469   /* Peek at the next token.  */
8470   token = cp_lexer_peek_token (parser->lexer);
8471   /* If it is `class' or `template', we have a type-parameter.  */
8472   if (token->keyword == RID_TEMPLATE)
8473     return cp_parser_type_parameter (parser);
8474   /* If it is `class' or `typename' we do not know yet whether it is a
8475      type parameter or a non-type parameter.  Consider:
8476
8477        template <typename T, typename T::X X> ...
8478
8479      or:
8480
8481        template <class C, class D*> ...
8482
8483      Here, the first parameter is a type parameter, and the second is
8484      a non-type parameter.  We can tell by looking at the token after
8485      the identifier -- if it is a `,', `=', or `>' then we have a type
8486      parameter.  */
8487   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8488     {
8489       /* Peek at the token after `class' or `typename'.  */
8490       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8491       /* If it's an identifier, skip it.  */
8492       if (token->type == CPP_NAME)
8493         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8494       /* Now, see if the token looks like the end of a template
8495          parameter.  */
8496       if (token->type == CPP_COMMA
8497           || token->type == CPP_EQ
8498           || token->type == CPP_GREATER)
8499         return cp_parser_type_parameter (parser);
8500     }
8501
8502   /* Otherwise, it is a non-type parameter.
8503
8504      [temp.param]
8505
8506      When parsing a default template-argument for a non-type
8507      template-parameter, the first non-nested `>' is taken as the end
8508      of the template parameter-list rather than a greater-than
8509      operator.  */
8510   *is_non_type = true;
8511   parameter_declarator
8512      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8513                                         /*parenthesized_p=*/NULL);
8514   parm = grokdeclarator (parameter_declarator->declarator,
8515                          &parameter_declarator->decl_specifiers,
8516                          PARM, /*initialized=*/0,
8517                          /*attrlist=*/NULL);
8518   if (parm == error_mark_node)
8519     return error_mark_node;
8520   return build_tree_list (parameter_declarator->default_argument, parm);
8521 }
8522
8523 /* Parse a type-parameter.
8524
8525    type-parameter:
8526      class identifier [opt]
8527      class identifier [opt] = type-id
8528      typename identifier [opt]
8529      typename identifier [opt] = type-id
8530      template < template-parameter-list > class identifier [opt]
8531      template < template-parameter-list > class identifier [opt]
8532        = id-expression
8533
8534    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8535    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8536    the declaration of the parameter.  */
8537
8538 static tree
8539 cp_parser_type_parameter (cp_parser* parser)
8540 {
8541   cp_token *token;
8542   tree parameter;
8543
8544   /* Look for a keyword to tell us what kind of parameter this is.  */
8545   token = cp_parser_require (parser, CPP_KEYWORD,
8546                              "`class', `typename', or `template'");
8547   if (!token)
8548     return error_mark_node;
8549
8550   switch (token->keyword)
8551     {
8552     case RID_CLASS:
8553     case RID_TYPENAME:
8554       {
8555         tree identifier;
8556         tree default_argument;
8557
8558         /* If the next token is an identifier, then it names the
8559            parameter.  */
8560         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8561           identifier = cp_parser_identifier (parser);
8562         else
8563           identifier = NULL_TREE;
8564
8565         /* Create the parameter.  */
8566         parameter = finish_template_type_parm (class_type_node, identifier);
8567
8568         /* If the next token is an `=', we have a default argument.  */
8569         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8570           {
8571             /* Consume the `=' token.  */
8572             cp_lexer_consume_token (parser->lexer);
8573             /* Parse the default-argument.  */
8574             push_deferring_access_checks (dk_no_deferred);
8575             default_argument = cp_parser_type_id (parser);
8576             pop_deferring_access_checks ();
8577           }
8578         else
8579           default_argument = NULL_TREE;
8580
8581         /* Create the combined representation of the parameter and the
8582            default argument.  */
8583         parameter = build_tree_list (default_argument, parameter);
8584       }
8585       break;
8586
8587     case RID_TEMPLATE:
8588       {
8589         tree parameter_list;
8590         tree identifier;
8591         tree default_argument;
8592
8593         /* Look for the `<'.  */
8594         cp_parser_require (parser, CPP_LESS, "`<'");
8595         /* Parse the template-parameter-list.  */
8596         parameter_list = cp_parser_template_parameter_list (parser);
8597         /* Look for the `>'.  */
8598         cp_parser_require (parser, CPP_GREATER, "`>'");
8599         /* Look for the `class' keyword.  */
8600         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8601         /* If the next token is an `=', then there is a
8602            default-argument.  If the next token is a `>', we are at
8603            the end of the parameter-list.  If the next token is a `,',
8604            then we are at the end of this parameter.  */
8605         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8606             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8607             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8608           {
8609             identifier = cp_parser_identifier (parser);
8610             /* Treat invalid names as if the parameter were nameless.  */
8611             if (identifier == error_mark_node)
8612               identifier = NULL_TREE;
8613           }
8614         else
8615           identifier = NULL_TREE;
8616
8617         /* Create the template parameter.  */
8618         parameter = finish_template_template_parm (class_type_node,
8619                                                    identifier);
8620
8621         /* If the next token is an `=', then there is a
8622            default-argument.  */
8623         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8624           {
8625             bool is_template;
8626
8627             /* Consume the `='.  */
8628             cp_lexer_consume_token (parser->lexer);
8629             /* Parse the id-expression.  */
8630             push_deferring_access_checks (dk_no_deferred);
8631             default_argument
8632               = cp_parser_id_expression (parser,
8633                                          /*template_keyword_p=*/false,
8634                                          /*check_dependency_p=*/true,
8635                                          /*template_p=*/&is_template,
8636                                          /*declarator_p=*/false,
8637                                          /*optional_p=*/false);
8638             if (TREE_CODE (default_argument) == TYPE_DECL)
8639               /* If the id-expression was a template-id that refers to
8640                  a template-class, we already have the declaration here,
8641                  so no further lookup is needed.  */
8642                  ;
8643             else
8644               /* Look up the name.  */
8645               default_argument
8646                 = cp_parser_lookup_name (parser, default_argument,
8647                                          none_type,
8648                                          /*is_template=*/is_template,
8649                                          /*is_namespace=*/false,
8650                                          /*check_dependency=*/true,
8651                                          /*ambiguous_decls=*/NULL);
8652             /* See if the default argument is valid.  */
8653             default_argument
8654               = check_template_template_default_arg (default_argument);
8655             pop_deferring_access_checks ();
8656           }
8657         else
8658           default_argument = NULL_TREE;
8659
8660         /* Create the combined representation of the parameter and the
8661            default argument.  */
8662         parameter = build_tree_list (default_argument, parameter);
8663       }
8664       break;
8665
8666     default:
8667       gcc_unreachable ();
8668       break;
8669     }
8670
8671   return parameter;
8672 }
8673
8674 /* Parse a template-id.
8675
8676    template-id:
8677      template-name < template-argument-list [opt] >
8678
8679    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8680    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8681    returned.  Otherwise, if the template-name names a function, or set
8682    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8683    names a class, returns a TYPE_DECL for the specialization.
8684
8685    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8686    uninstantiated templates.  */
8687
8688 static tree
8689 cp_parser_template_id (cp_parser *parser,
8690                        bool template_keyword_p,
8691                        bool check_dependency_p,
8692                        bool is_declaration)
8693 {
8694   tree template;
8695   tree arguments;
8696   tree template_id;
8697   cp_token_position start_of_id = 0;
8698   tree access_check = NULL_TREE;
8699   cp_token *next_token, *next_token_2;
8700   bool is_identifier;
8701
8702   /* If the next token corresponds to a template-id, there is no need
8703      to reparse it.  */
8704   next_token = cp_lexer_peek_token (parser->lexer);
8705   if (next_token->type == CPP_TEMPLATE_ID)
8706     {
8707       tree value;
8708       tree check;
8709
8710       /* Get the stored value.  */
8711       value = cp_lexer_consume_token (parser->lexer)->value;
8712       /* Perform any access checks that were deferred.  */
8713       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8714         perform_or_defer_access_check (TREE_PURPOSE (check),
8715                                        TREE_VALUE (check));
8716       /* Return the stored value.  */
8717       return TREE_VALUE (value);
8718     }
8719
8720   /* Avoid performing name lookup if there is no possibility of
8721      finding a template-id.  */
8722   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8723       || (next_token->type == CPP_NAME
8724           && !cp_parser_nth_token_starts_template_argument_list_p
8725                (parser, 2)))
8726     {
8727       cp_parser_error (parser, "expected template-id");
8728       return error_mark_node;
8729     }
8730
8731   /* Remember where the template-id starts.  */
8732   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8733     start_of_id = cp_lexer_token_position (parser->lexer, false);
8734
8735   push_deferring_access_checks (dk_deferred);
8736
8737   /* Parse the template-name.  */
8738   is_identifier = false;
8739   template = cp_parser_template_name (parser, template_keyword_p,
8740                                       check_dependency_p,
8741                                       is_declaration,
8742                                       &is_identifier);
8743   if (template == error_mark_node || is_identifier)
8744     {
8745       pop_deferring_access_checks ();
8746       return template;
8747     }
8748
8749   /* If we find the sequence `[:' after a template-name, it's probably
8750      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8751      parse correctly the argument list.  */
8752   next_token = cp_lexer_peek_token (parser->lexer);
8753   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8754   if (next_token->type == CPP_OPEN_SQUARE
8755       && next_token->flags & DIGRAPH
8756       && next_token_2->type == CPP_COLON
8757       && !(next_token_2->flags & PREV_WHITE))
8758     {
8759       cp_parser_parse_tentatively (parser);
8760       /* Change `:' into `::'.  */
8761       next_token_2->type = CPP_SCOPE;
8762       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8763          CPP_LESS.  */
8764       cp_lexer_consume_token (parser->lexer);
8765       /* Parse the arguments.  */
8766       arguments = cp_parser_enclosed_template_argument_list (parser);
8767       if (!cp_parser_parse_definitely (parser))
8768         {
8769           /* If we couldn't parse an argument list, then we revert our changes
8770              and return simply an error. Maybe this is not a template-id
8771              after all.  */
8772           next_token_2->type = CPP_COLON;
8773           cp_parser_error (parser, "expected %<<%>");
8774           pop_deferring_access_checks ();
8775           return error_mark_node;
8776         }
8777       /* Otherwise, emit an error about the invalid digraph, but continue
8778          parsing because we got our argument list.  */
8779       pedwarn ("%<<::%> cannot begin a template-argument list");
8780       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8781               "between %<<%> and %<::%>");
8782       if (!flag_permissive)
8783         {
8784           static bool hint;
8785           if (!hint)
8786             {
8787               inform ("(if you use -fpermissive G++ will accept your code)");
8788               hint = true;
8789             }
8790         }
8791     }
8792   else
8793     {
8794       /* Look for the `<' that starts the template-argument-list.  */
8795       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8796         {
8797           pop_deferring_access_checks ();
8798           return error_mark_node;
8799         }
8800       /* Parse the arguments.  */
8801       arguments = cp_parser_enclosed_template_argument_list (parser);
8802     }
8803
8804   /* Build a representation of the specialization.  */
8805   if (TREE_CODE (template) == IDENTIFIER_NODE)
8806     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8807   else if (DECL_CLASS_TEMPLATE_P (template)
8808            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8809     {
8810       bool entering_scope;
8811       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8812          template (rather than some instantiation thereof) only if
8813          is not nested within some other construct.  For example, in
8814          "template <typename T> void f(T) { A<T>::", A<T> is just an
8815          instantiation of A.  */
8816       entering_scope = (template_parm_scope_p ()
8817                         && cp_lexer_next_token_is (parser->lexer,
8818                                                    CPP_SCOPE));
8819       template_id
8820         = finish_template_type (template, arguments, entering_scope);
8821     }
8822   else
8823     {
8824       /* If it's not a class-template or a template-template, it should be
8825          a function-template.  */
8826       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8827                    || TREE_CODE (template) == OVERLOAD
8828                    || BASELINK_P (template)));
8829
8830       template_id = lookup_template_function (template, arguments);
8831     }
8832
8833   /* Retrieve any deferred checks.  Do not pop this access checks yet
8834      so the memory will not be reclaimed during token replacing below.  */
8835   access_check = get_deferred_access_checks ();
8836
8837   /* If parsing tentatively, replace the sequence of tokens that makes
8838      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8839      should we re-parse the token stream, we will not have to repeat
8840      the effort required to do the parse, nor will we issue duplicate
8841      error messages about problems during instantiation of the
8842      template.  */
8843   if (start_of_id)
8844     {
8845       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8846
8847       /* Reset the contents of the START_OF_ID token.  */
8848       token->type = CPP_TEMPLATE_ID;
8849       token->value = build_tree_list (access_check, template_id);
8850       token->keyword = RID_MAX;
8851
8852       /* Purge all subsequent tokens.  */
8853       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8854
8855       /* ??? Can we actually assume that, if template_id ==
8856          error_mark_node, we will have issued a diagnostic to the
8857          user, as opposed to simply marking the tentative parse as
8858          failed?  */
8859       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8860         error ("parse error in template argument list");
8861     }
8862
8863   pop_deferring_access_checks ();
8864   return template_id;
8865 }
8866
8867 /* Parse a template-name.
8868
8869    template-name:
8870      identifier
8871
8872    The standard should actually say:
8873
8874    template-name:
8875      identifier
8876      operator-function-id
8877
8878    A defect report has been filed about this issue.
8879
8880    A conversion-function-id cannot be a template name because they cannot
8881    be part of a template-id. In fact, looking at this code:
8882
8883    a.operator K<int>()
8884
8885    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8886    It is impossible to call a templated conversion-function-id with an
8887    explicit argument list, since the only allowed template parameter is
8888    the type to which it is converting.
8889
8890    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8891    `template' keyword, in a construction like:
8892
8893      T::template f<3>()
8894
8895    In that case `f' is taken to be a template-name, even though there
8896    is no way of knowing for sure.
8897
8898    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8899    name refers to a set of overloaded functions, at least one of which
8900    is a template, or an IDENTIFIER_NODE with the name of the template,
8901    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8902    names are looked up inside uninstantiated templates.  */
8903
8904 static tree
8905 cp_parser_template_name (cp_parser* parser,
8906                          bool template_keyword_p,
8907                          bool check_dependency_p,
8908                          bool is_declaration,
8909                          bool *is_identifier)
8910 {
8911   tree identifier;
8912   tree decl;
8913   tree fns;
8914
8915   /* If the next token is `operator', then we have either an
8916      operator-function-id or a conversion-function-id.  */
8917   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8918     {
8919       /* We don't know whether we're looking at an
8920          operator-function-id or a conversion-function-id.  */
8921       cp_parser_parse_tentatively (parser);
8922       /* Try an operator-function-id.  */
8923       identifier = cp_parser_operator_function_id (parser);
8924       /* If that didn't work, try a conversion-function-id.  */
8925       if (!cp_parser_parse_definitely (parser))
8926         {
8927           cp_parser_error (parser, "expected template-name");
8928           return error_mark_node;
8929         }
8930     }
8931   /* Look for the identifier.  */
8932   else
8933     identifier = cp_parser_identifier (parser);
8934
8935   /* If we didn't find an identifier, we don't have a template-id.  */
8936   if (identifier == error_mark_node)
8937     return error_mark_node;
8938
8939   /* If the name immediately followed the `template' keyword, then it
8940      is a template-name.  However, if the next token is not `<', then
8941      we do not treat it as a template-name, since it is not being used
8942      as part of a template-id.  This enables us to handle constructs
8943      like:
8944
8945        template <typename T> struct S { S(); };
8946        template <typename T> S<T>::S();
8947
8948      correctly.  We would treat `S' as a template -- if it were `S<T>'
8949      -- but we do not if there is no `<'.  */
8950
8951   if (processing_template_decl
8952       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8953     {
8954       /* In a declaration, in a dependent context, we pretend that the
8955          "template" keyword was present in order to improve error
8956          recovery.  For example, given:
8957
8958            template <typename T> void f(T::X<int>);
8959
8960          we want to treat "X<int>" as a template-id.  */
8961       if (is_declaration
8962           && !template_keyword_p
8963           && parser->scope && TYPE_P (parser->scope)
8964           && check_dependency_p
8965           && dependent_type_p (parser->scope)
8966           /* Do not do this for dtors (or ctors), since they never
8967              need the template keyword before their name.  */
8968           && !constructor_name_p (identifier, parser->scope))
8969         {
8970           cp_token_position start = 0;
8971
8972           /* Explain what went wrong.  */
8973           error ("non-template %qD used as template", identifier);
8974           inform ("use %<%T::template %D%> to indicate that it is a template",
8975                   parser->scope, identifier);
8976           /* If parsing tentatively, find the location of the "<" token.  */
8977           if (cp_parser_simulate_error (parser))
8978             start = cp_lexer_token_position (parser->lexer, true);
8979           /* Parse the template arguments so that we can issue error
8980              messages about them.  */
8981           cp_lexer_consume_token (parser->lexer);
8982           cp_parser_enclosed_template_argument_list (parser);
8983           /* Skip tokens until we find a good place from which to
8984              continue parsing.  */
8985           cp_parser_skip_to_closing_parenthesis (parser,
8986                                                  /*recovering=*/true,
8987                                                  /*or_comma=*/true,
8988                                                  /*consume_paren=*/false);
8989           /* If parsing tentatively, permanently remove the
8990              template argument list.  That will prevent duplicate
8991              error messages from being issued about the missing
8992              "template" keyword.  */
8993           if (start)
8994             cp_lexer_purge_tokens_after (parser->lexer, start);
8995           if (is_identifier)
8996             *is_identifier = true;
8997           return identifier;
8998         }
8999
9000       /* If the "template" keyword is present, then there is generally
9001          no point in doing name-lookup, so we just return IDENTIFIER.
9002          But, if the qualifying scope is non-dependent then we can
9003          (and must) do name-lookup normally.  */
9004       if (template_keyword_p
9005           && (!parser->scope
9006               || (TYPE_P (parser->scope)
9007                   && dependent_type_p (parser->scope))))
9008         return identifier;
9009     }
9010
9011   /* Look up the name.  */
9012   decl = cp_parser_lookup_name (parser, identifier,
9013                                 none_type,
9014                                 /*is_template=*/false,
9015                                 /*is_namespace=*/false,
9016                                 check_dependency_p,
9017                                 /*ambiguous_decls=*/NULL);
9018   decl = maybe_get_template_decl_from_type_decl (decl);
9019
9020   /* If DECL is a template, then the name was a template-name.  */
9021   if (TREE_CODE (decl) == TEMPLATE_DECL)
9022     ;
9023   else
9024     {
9025       tree fn = NULL_TREE;
9026
9027       /* The standard does not explicitly indicate whether a name that
9028          names a set of overloaded declarations, some of which are
9029          templates, is a template-name.  However, such a name should
9030          be a template-name; otherwise, there is no way to form a
9031          template-id for the overloaded templates.  */
9032       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9033       if (TREE_CODE (fns) == OVERLOAD)
9034         for (fn = fns; fn; fn = OVL_NEXT (fn))
9035           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9036             break;
9037
9038       if (!fn)
9039         {
9040           /* The name does not name a template.  */
9041           cp_parser_error (parser, "expected template-name");
9042           return error_mark_node;
9043         }
9044     }
9045
9046   /* If DECL is dependent, and refers to a function, then just return
9047      its name; we will look it up again during template instantiation.  */
9048   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9049     {
9050       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9051       if (TYPE_P (scope) && dependent_type_p (scope))
9052         return identifier;
9053     }
9054
9055   return decl;
9056 }
9057
9058 /* Parse a template-argument-list.
9059
9060    template-argument-list:
9061      template-argument
9062      template-argument-list , template-argument
9063
9064    Returns a TREE_VEC containing the arguments.  */
9065
9066 static tree
9067 cp_parser_template_argument_list (cp_parser* parser)
9068 {
9069   tree fixed_args[10];
9070   unsigned n_args = 0;
9071   unsigned alloced = 10;
9072   tree *arg_ary = fixed_args;
9073   tree vec;
9074   bool saved_in_template_argument_list_p;
9075   bool saved_ice_p;
9076   bool saved_non_ice_p;
9077
9078   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9079   parser->in_template_argument_list_p = true;
9080   /* Even if the template-id appears in an integral
9081      constant-expression, the contents of the argument list do
9082      not.  */
9083   saved_ice_p = parser->integral_constant_expression_p;
9084   parser->integral_constant_expression_p = false;
9085   saved_non_ice_p = parser->non_integral_constant_expression_p;
9086   parser->non_integral_constant_expression_p = false;
9087   /* Parse the arguments.  */
9088   do
9089     {
9090       tree argument;
9091
9092       if (n_args)
9093         /* Consume the comma.  */
9094         cp_lexer_consume_token (parser->lexer);
9095
9096       /* Parse the template-argument.  */
9097       argument = cp_parser_template_argument (parser);
9098       if (n_args == alloced)
9099         {
9100           alloced *= 2;
9101
9102           if (arg_ary == fixed_args)
9103             {
9104               arg_ary = XNEWVEC (tree, alloced);
9105               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9106             }
9107           else
9108             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9109         }
9110       arg_ary[n_args++] = argument;
9111     }
9112   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9113
9114   vec = make_tree_vec (n_args);
9115
9116   while (n_args--)
9117     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9118
9119   if (arg_ary != fixed_args)
9120     free (arg_ary);
9121   parser->non_integral_constant_expression_p = saved_non_ice_p;
9122   parser->integral_constant_expression_p = saved_ice_p;
9123   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9124   return vec;
9125 }
9126
9127 /* Parse a template-argument.
9128
9129    template-argument:
9130      assignment-expression
9131      type-id
9132      id-expression
9133
9134    The representation is that of an assignment-expression, type-id, or
9135    id-expression -- except that the qualified id-expression is
9136    evaluated, so that the value returned is either a DECL or an
9137    OVERLOAD.
9138
9139    Although the standard says "assignment-expression", it forbids
9140    throw-expressions or assignments in the template argument.
9141    Therefore, we use "conditional-expression" instead.  */
9142
9143 static tree
9144 cp_parser_template_argument (cp_parser* parser)
9145 {
9146   tree argument;
9147   bool template_p;
9148   bool address_p;
9149   bool maybe_type_id = false;
9150   cp_token *token;
9151   cp_id_kind idk;
9152
9153   /* There's really no way to know what we're looking at, so we just
9154      try each alternative in order.
9155
9156        [temp.arg]
9157
9158        In a template-argument, an ambiguity between a type-id and an
9159        expression is resolved to a type-id, regardless of the form of
9160        the corresponding template-parameter.
9161
9162      Therefore, we try a type-id first.  */
9163   cp_parser_parse_tentatively (parser);
9164   argument = cp_parser_type_id (parser);
9165   /* If there was no error parsing the type-id but the next token is a '>>',
9166      we probably found a typo for '> >'. But there are type-id which are
9167      also valid expressions. For instance:
9168
9169      struct X { int operator >> (int); };
9170      template <int V> struct Foo {};
9171      Foo<X () >> 5> r;
9172
9173      Here 'X()' is a valid type-id of a function type, but the user just
9174      wanted to write the expression "X() >> 5". Thus, we remember that we
9175      found a valid type-id, but we still try to parse the argument as an
9176      expression to see what happens.  */
9177   if (!cp_parser_error_occurred (parser)
9178       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9179     {
9180       maybe_type_id = true;
9181       cp_parser_abort_tentative_parse (parser);
9182     }
9183   else
9184     {
9185       /* If the next token isn't a `,' or a `>', then this argument wasn't
9186       really finished. This means that the argument is not a valid
9187       type-id.  */
9188       if (!cp_parser_next_token_ends_template_argument_p (parser))
9189         cp_parser_error (parser, "expected template-argument");
9190       /* If that worked, we're done.  */
9191       if (cp_parser_parse_definitely (parser))
9192         return argument;
9193     }
9194   /* We're still not sure what the argument will be.  */
9195   cp_parser_parse_tentatively (parser);
9196   /* Try a template.  */
9197   argument = cp_parser_id_expression (parser,
9198                                       /*template_keyword_p=*/false,
9199                                       /*check_dependency_p=*/true,
9200                                       &template_p,
9201                                       /*declarator_p=*/false,
9202                                       /*optional_p=*/false);
9203   /* If the next token isn't a `,' or a `>', then this argument wasn't
9204      really finished.  */
9205   if (!cp_parser_next_token_ends_template_argument_p (parser))
9206     cp_parser_error (parser, "expected template-argument");
9207   if (!cp_parser_error_occurred (parser))
9208     {
9209       /* Figure out what is being referred to.  If the id-expression
9210          was for a class template specialization, then we will have a
9211          TYPE_DECL at this point.  There is no need to do name lookup
9212          at this point in that case.  */
9213       if (TREE_CODE (argument) != TYPE_DECL)
9214         argument = cp_parser_lookup_name (parser, argument,
9215                                           none_type,
9216                                           /*is_template=*/template_p,
9217                                           /*is_namespace=*/false,
9218                                           /*check_dependency=*/true,
9219                                           /*ambiguous_decls=*/NULL);
9220       if (TREE_CODE (argument) != TEMPLATE_DECL
9221           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9222         cp_parser_error (parser, "expected template-name");
9223     }
9224   if (cp_parser_parse_definitely (parser))
9225     return argument;
9226   /* It must be a non-type argument.  There permitted cases are given
9227      in [temp.arg.nontype]:
9228
9229      -- an integral constant-expression of integral or enumeration
9230         type; or
9231
9232      -- the name of a non-type template-parameter; or
9233
9234      -- the name of an object or function with external linkage...
9235
9236      -- the address of an object or function with external linkage...
9237
9238      -- a pointer to member...  */
9239   /* Look for a non-type template parameter.  */
9240   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9241     {
9242       cp_parser_parse_tentatively (parser);
9243       argument = cp_parser_primary_expression (parser,
9244                                                /*adress_p=*/false,
9245                                                /*cast_p=*/false,
9246                                                /*template_arg_p=*/true,
9247                                                &idk);
9248       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9249           || !cp_parser_next_token_ends_template_argument_p (parser))
9250         cp_parser_simulate_error (parser);
9251       if (cp_parser_parse_definitely (parser))
9252         return argument;
9253     }
9254
9255   /* If the next token is "&", the argument must be the address of an
9256      object or function with external linkage.  */
9257   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9258   if (address_p)
9259     cp_lexer_consume_token (parser->lexer);
9260   /* See if we might have an id-expression.  */
9261   token = cp_lexer_peek_token (parser->lexer);
9262   if (token->type == CPP_NAME
9263       || token->keyword == RID_OPERATOR
9264       || token->type == CPP_SCOPE
9265       || token->type == CPP_TEMPLATE_ID
9266       || token->type == CPP_NESTED_NAME_SPECIFIER)
9267     {
9268       cp_parser_parse_tentatively (parser);
9269       argument = cp_parser_primary_expression (parser,
9270                                                address_p,
9271                                                /*cast_p=*/false,
9272                                                /*template_arg_p=*/true,
9273                                                &idk);
9274       if (cp_parser_error_occurred (parser)
9275           || !cp_parser_next_token_ends_template_argument_p (parser))
9276         cp_parser_abort_tentative_parse (parser);
9277       else
9278         {
9279           if (TREE_CODE (argument) == INDIRECT_REF)
9280             {
9281               gcc_assert (REFERENCE_REF_P (argument));
9282               argument = TREE_OPERAND (argument, 0);
9283             }
9284
9285           if (TREE_CODE (argument) == BASELINK)
9286             /* We don't need the information about what class was used
9287                to name the overloaded functions.  */
9288             argument = BASELINK_FUNCTIONS (argument);
9289
9290           if (TREE_CODE (argument) == VAR_DECL)
9291             {
9292               /* A variable without external linkage might still be a
9293                  valid constant-expression, so no error is issued here
9294                  if the external-linkage check fails.  */
9295               if (!DECL_EXTERNAL_LINKAGE_P (argument))
9296                 cp_parser_simulate_error (parser);
9297             }
9298           else if (is_overloaded_fn (argument))
9299             /* All overloaded functions are allowed; if the external
9300                linkage test does not pass, an error will be issued
9301                later.  */
9302             ;
9303           else if (address_p
9304                    && (TREE_CODE (argument) == OFFSET_REF
9305                        || TREE_CODE (argument) == SCOPE_REF))
9306             /* A pointer-to-member.  */
9307             ;
9308           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9309             ;
9310           else
9311             cp_parser_simulate_error (parser);
9312
9313           if (cp_parser_parse_definitely (parser))
9314             {
9315               if (address_p)
9316                 argument = build_x_unary_op (ADDR_EXPR, argument);
9317               return argument;
9318             }
9319         }
9320     }
9321   /* If the argument started with "&", there are no other valid
9322      alternatives at this point.  */
9323   if (address_p)
9324     {
9325       cp_parser_error (parser, "invalid non-type template argument");
9326       return error_mark_node;
9327     }
9328
9329   /* If the argument wasn't successfully parsed as a type-id followed
9330      by '>>', the argument can only be a constant expression now.
9331      Otherwise, we try parsing the constant-expression tentatively,
9332      because the argument could really be a type-id.  */
9333   if (maybe_type_id)
9334     cp_parser_parse_tentatively (parser);
9335   argument = cp_parser_constant_expression (parser,
9336                                             /*allow_non_constant_p=*/false,
9337                                             /*non_constant_p=*/NULL);
9338   argument = fold_non_dependent_expr (argument);
9339   if (!maybe_type_id)
9340     return argument;
9341   if (!cp_parser_next_token_ends_template_argument_p (parser))
9342     cp_parser_error (parser, "expected template-argument");
9343   if (cp_parser_parse_definitely (parser))
9344     return argument;
9345   /* We did our best to parse the argument as a non type-id, but that
9346      was the only alternative that matched (albeit with a '>' after
9347      it). We can assume it's just a typo from the user, and a
9348      diagnostic will then be issued.  */
9349   return cp_parser_type_id (parser);
9350 }
9351
9352 /* Parse an explicit-instantiation.
9353
9354    explicit-instantiation:
9355      template declaration
9356
9357    Although the standard says `declaration', what it really means is:
9358
9359    explicit-instantiation:
9360      template decl-specifier-seq [opt] declarator [opt] ;
9361
9362    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9363    supposed to be allowed.  A defect report has been filed about this
9364    issue.
9365
9366    GNU Extension:
9367
9368    explicit-instantiation:
9369      storage-class-specifier template
9370        decl-specifier-seq [opt] declarator [opt] ;
9371      function-specifier template
9372        decl-specifier-seq [opt] declarator [opt] ;  */
9373
9374 static void
9375 cp_parser_explicit_instantiation (cp_parser* parser)
9376 {
9377   int declares_class_or_enum;
9378   cp_decl_specifier_seq decl_specifiers;
9379   tree extension_specifier = NULL_TREE;
9380
9381   /* Look for an (optional) storage-class-specifier or
9382      function-specifier.  */
9383   if (cp_parser_allow_gnu_extensions_p (parser))
9384     {
9385       extension_specifier
9386         = cp_parser_storage_class_specifier_opt (parser);
9387       if (!extension_specifier)
9388         extension_specifier
9389           = cp_parser_function_specifier_opt (parser,
9390                                               /*decl_specs=*/NULL);
9391     }
9392
9393   /* Look for the `template' keyword.  */
9394   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9395   /* Let the front end know that we are processing an explicit
9396      instantiation.  */
9397   begin_explicit_instantiation ();
9398   /* [temp.explicit] says that we are supposed to ignore access
9399      control while processing explicit instantiation directives.  */
9400   push_deferring_access_checks (dk_no_check);
9401   /* Parse a decl-specifier-seq.  */
9402   cp_parser_decl_specifier_seq (parser,
9403                                 CP_PARSER_FLAGS_OPTIONAL,
9404                                 &decl_specifiers,
9405                                 &declares_class_or_enum);
9406   /* If there was exactly one decl-specifier, and it declared a class,
9407      and there's no declarator, then we have an explicit type
9408      instantiation.  */
9409   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9410     {
9411       tree type;
9412
9413       type = check_tag_decl (&decl_specifiers);
9414       /* Turn access control back on for names used during
9415          template instantiation.  */
9416       pop_deferring_access_checks ();
9417       if (type)
9418         do_type_instantiation (type, extension_specifier,
9419                                /*complain=*/tf_error);
9420     }
9421   else
9422     {
9423       cp_declarator *declarator;
9424       tree decl;
9425
9426       /* Parse the declarator.  */
9427       declarator
9428         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9429                                 /*ctor_dtor_or_conv_p=*/NULL,
9430                                 /*parenthesized_p=*/NULL,
9431                                 /*member_p=*/false);
9432       if (declares_class_or_enum & 2)
9433         cp_parser_check_for_definition_in_return_type (declarator,
9434                                                        decl_specifiers.type);
9435       if (declarator != cp_error_declarator)
9436         {
9437           decl = grokdeclarator (declarator, &decl_specifiers,
9438                                  NORMAL, 0, &decl_specifiers.attributes);
9439           /* Turn access control back on for names used during
9440              template instantiation.  */
9441           pop_deferring_access_checks ();
9442           /* Do the explicit instantiation.  */
9443           do_decl_instantiation (decl, extension_specifier);
9444         }
9445       else
9446         {
9447           pop_deferring_access_checks ();
9448           /* Skip the body of the explicit instantiation.  */
9449           cp_parser_skip_to_end_of_statement (parser);
9450         }
9451     }
9452   /* We're done with the instantiation.  */
9453   end_explicit_instantiation ();
9454
9455   cp_parser_consume_semicolon_at_end_of_statement (parser);
9456 }
9457
9458 /* Parse an explicit-specialization.
9459
9460    explicit-specialization:
9461      template < > declaration
9462
9463    Although the standard says `declaration', what it really means is:
9464
9465    explicit-specialization:
9466      template <> decl-specifier [opt] init-declarator [opt] ;
9467      template <> function-definition
9468      template <> explicit-specialization
9469      template <> template-declaration  */
9470
9471 static void
9472 cp_parser_explicit_specialization (cp_parser* parser)
9473 {
9474   bool need_lang_pop;
9475   /* Look for the `template' keyword.  */
9476   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9477   /* Look for the `<'.  */
9478   cp_parser_require (parser, CPP_LESS, "`<'");
9479   /* Look for the `>'.  */
9480   cp_parser_require (parser, CPP_GREATER, "`>'");
9481   /* We have processed another parameter list.  */
9482   ++parser->num_template_parameter_lists;
9483   /* [temp]
9484
9485      A template ... explicit specialization ... shall not have C
9486      linkage.  */
9487   if (current_lang_name == lang_name_c)
9488     {
9489       error ("template specialization with C linkage");
9490       /* Give it C++ linkage to avoid confusing other parts of the
9491          front end.  */
9492       push_lang_context (lang_name_cplusplus);
9493       need_lang_pop = true;
9494     }
9495   else
9496     need_lang_pop = false;
9497   /* Let the front end know that we are beginning a specialization.  */
9498   begin_specialization ();
9499   /* If the next keyword is `template', we need to figure out whether
9500      or not we're looking a template-declaration.  */
9501   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9502     {
9503       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9504           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9505         cp_parser_template_declaration_after_export (parser,
9506                                                      /*member_p=*/false);
9507       else
9508         cp_parser_explicit_specialization (parser);
9509     }
9510   else
9511     /* Parse the dependent declaration.  */
9512     cp_parser_single_declaration (parser,
9513                                   /*checks=*/NULL_TREE,
9514                                   /*member_p=*/false,
9515                                   /*friend_p=*/NULL);
9516   /* We're done with the specialization.  */
9517   end_specialization ();
9518   /* For the erroneous case of a template with C linkage, we pushed an
9519      implicit C++ linkage scope; exit that scope now.  */
9520   if (need_lang_pop)
9521     pop_lang_context ();
9522   /* We're done with this parameter list.  */
9523   --parser->num_template_parameter_lists;
9524 }
9525
9526 /* Parse a type-specifier.
9527
9528    type-specifier:
9529      simple-type-specifier
9530      class-specifier
9531      enum-specifier
9532      elaborated-type-specifier
9533      cv-qualifier
9534
9535    GNU Extension:
9536
9537    type-specifier:
9538      __complex__
9539
9540    Returns a representation of the type-specifier.  For a
9541    class-specifier, enum-specifier, or elaborated-type-specifier, a
9542    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9543
9544    The parser flags FLAGS is used to control type-specifier parsing.
9545
9546    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9547    in a decl-specifier-seq.
9548
9549    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9550    class-specifier, enum-specifier, or elaborated-type-specifier, then
9551    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9552    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9553    zero.
9554
9555    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9556    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9557    is set to FALSE.  */
9558
9559 static tree
9560 cp_parser_type_specifier (cp_parser* parser,
9561                           cp_parser_flags flags,
9562                           cp_decl_specifier_seq *decl_specs,
9563                           bool is_declaration,
9564                           int* declares_class_or_enum,
9565                           bool* is_cv_qualifier)
9566 {
9567   tree type_spec = NULL_TREE;
9568   cp_token *token;
9569   enum rid keyword;
9570   cp_decl_spec ds = ds_last;
9571
9572   /* Assume this type-specifier does not declare a new type.  */
9573   if (declares_class_or_enum)
9574     *declares_class_or_enum = 0;
9575   /* And that it does not specify a cv-qualifier.  */
9576   if (is_cv_qualifier)
9577     *is_cv_qualifier = false;
9578   /* Peek at the next token.  */
9579   token = cp_lexer_peek_token (parser->lexer);
9580
9581   /* If we're looking at a keyword, we can use that to guide the
9582      production we choose.  */
9583   keyword = token->keyword;
9584   switch (keyword)
9585     {
9586     case RID_ENUM:
9587       /* Look for the enum-specifier.  */
9588       type_spec = cp_parser_enum_specifier (parser);
9589       /* If that worked, we're done.  */
9590       if (type_spec)
9591         {
9592           if (declares_class_or_enum)
9593             *declares_class_or_enum = 2;
9594           if (decl_specs)
9595             cp_parser_set_decl_spec_type (decl_specs,
9596                                           type_spec,
9597                                           /*user_defined_p=*/true);
9598           return type_spec;
9599         }
9600       else
9601         goto elaborated_type_specifier;
9602
9603       /* Any of these indicate either a class-specifier, or an
9604          elaborated-type-specifier.  */
9605     case RID_CLASS:
9606     case RID_STRUCT:
9607     case RID_UNION:
9608       /* Parse tentatively so that we can back up if we don't find a
9609          class-specifier.  */
9610       cp_parser_parse_tentatively (parser);
9611       /* Look for the class-specifier.  */
9612       type_spec = cp_parser_class_specifier (parser);
9613       /* If that worked, we're done.  */
9614       if (cp_parser_parse_definitely (parser))
9615         {
9616           if (declares_class_or_enum)
9617             *declares_class_or_enum = 2;
9618           if (decl_specs)
9619             cp_parser_set_decl_spec_type (decl_specs,
9620                                           type_spec,
9621                                           /*user_defined_p=*/true);
9622           return type_spec;
9623         }
9624
9625       /* Fall through.  */
9626     elaborated_type_specifier:
9627       /* We're declaring (not defining) a class or enum.  */
9628       if (declares_class_or_enum)
9629         *declares_class_or_enum = 1;
9630
9631       /* Fall through.  */
9632     case RID_TYPENAME:
9633       /* Look for an elaborated-type-specifier.  */
9634       type_spec
9635         = (cp_parser_elaborated_type_specifier
9636            (parser,
9637             decl_specs && decl_specs->specs[(int) ds_friend],
9638             is_declaration));
9639       if (decl_specs)
9640         cp_parser_set_decl_spec_type (decl_specs,
9641                                       type_spec,
9642                                       /*user_defined_p=*/true);
9643       return type_spec;
9644
9645     case RID_CONST:
9646       ds = ds_const;
9647       if (is_cv_qualifier)
9648         *is_cv_qualifier = true;
9649       break;
9650
9651     case RID_VOLATILE:
9652       ds = ds_volatile;
9653       if (is_cv_qualifier)
9654         *is_cv_qualifier = true;
9655       break;
9656
9657     case RID_RESTRICT:
9658       ds = ds_restrict;
9659       if (is_cv_qualifier)
9660         *is_cv_qualifier = true;
9661       break;
9662
9663     case RID_COMPLEX:
9664       /* The `__complex__' keyword is a GNU extension.  */
9665       ds = ds_complex;
9666       break;
9667
9668     default:
9669       break;
9670     }
9671
9672   /* Handle simple keywords.  */
9673   if (ds != ds_last)
9674     {
9675       if (decl_specs)
9676         {
9677           ++decl_specs->specs[(int)ds];
9678           decl_specs->any_specifiers_p = true;
9679         }
9680       return cp_lexer_consume_token (parser->lexer)->value;
9681     }
9682
9683   /* If we do not already have a type-specifier, assume we are looking
9684      at a simple-type-specifier.  */
9685   type_spec = cp_parser_simple_type_specifier (parser,
9686                                                decl_specs,
9687                                                flags);
9688
9689   /* If we didn't find a type-specifier, and a type-specifier was not
9690      optional in this context, issue an error message.  */
9691   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9692     {
9693       cp_parser_error (parser, "expected type specifier");
9694       return error_mark_node;
9695     }
9696
9697   return type_spec;
9698 }
9699
9700 /* Parse a simple-type-specifier.
9701
9702    simple-type-specifier:
9703      :: [opt] nested-name-specifier [opt] type-name
9704      :: [opt] nested-name-specifier template template-id
9705      char
9706      wchar_t
9707      bool
9708      short
9709      int
9710      long
9711      signed
9712      unsigned
9713      float
9714      double
9715      void
9716
9717    GNU Extension:
9718
9719    simple-type-specifier:
9720      __typeof__ unary-expression
9721      __typeof__ ( type-id )
9722
9723    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9724    appropriately updated.  */
9725
9726 static tree
9727 cp_parser_simple_type_specifier (cp_parser* parser,
9728                                  cp_decl_specifier_seq *decl_specs,
9729                                  cp_parser_flags flags)
9730 {
9731   tree type = NULL_TREE;
9732   cp_token *token;
9733
9734   /* Peek at the next token.  */
9735   token = cp_lexer_peek_token (parser->lexer);
9736
9737   /* If we're looking at a keyword, things are easy.  */
9738   switch (token->keyword)
9739     {
9740     case RID_CHAR:
9741       if (decl_specs)
9742         decl_specs->explicit_char_p = true;
9743       type = char_type_node;
9744       break;
9745     case RID_WCHAR:
9746       type = wchar_type_node;
9747       break;
9748     case RID_BOOL:
9749       type = boolean_type_node;
9750       break;
9751     case RID_SHORT:
9752       if (decl_specs)
9753         ++decl_specs->specs[(int) ds_short];
9754       type = short_integer_type_node;
9755       break;
9756     case RID_INT:
9757       if (decl_specs)
9758         decl_specs->explicit_int_p = true;
9759       type = integer_type_node;
9760       break;
9761     case RID_LONG:
9762       if (decl_specs)
9763         ++decl_specs->specs[(int) ds_long];
9764       type = long_integer_type_node;
9765       break;
9766     case RID_SIGNED:
9767       if (decl_specs)
9768         ++decl_specs->specs[(int) ds_signed];
9769       type = integer_type_node;
9770       break;
9771     case RID_UNSIGNED:
9772       if (decl_specs)
9773         ++decl_specs->specs[(int) ds_unsigned];
9774       type = unsigned_type_node;
9775       break;
9776     case RID_FLOAT:
9777       type = float_type_node;
9778       break;
9779     case RID_DOUBLE:
9780       type = double_type_node;
9781       break;
9782     case RID_VOID:
9783       type = void_type_node;
9784       break;
9785
9786     case RID_TYPEOF:
9787       /* Consume the `typeof' token.  */
9788       cp_lexer_consume_token (parser->lexer);
9789       /* Parse the operand to `typeof'.  */
9790       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9791       /* If it is not already a TYPE, take its type.  */
9792       if (!TYPE_P (type))
9793         type = finish_typeof (type);
9794
9795       if (decl_specs)
9796         cp_parser_set_decl_spec_type (decl_specs, type,
9797                                       /*user_defined_p=*/true);
9798
9799       return type;
9800
9801     default:
9802       break;
9803     }
9804
9805   /* If the type-specifier was for a built-in type, we're done.  */
9806   if (type)
9807     {
9808       tree id;
9809
9810       /* Record the type.  */
9811       if (decl_specs
9812           && (token->keyword != RID_SIGNED
9813               && token->keyword != RID_UNSIGNED
9814               && token->keyword != RID_SHORT
9815               && token->keyword != RID_LONG))
9816         cp_parser_set_decl_spec_type (decl_specs,
9817                                       type,
9818                                       /*user_defined=*/false);
9819       if (decl_specs)
9820         decl_specs->any_specifiers_p = true;
9821
9822       /* Consume the token.  */
9823       id = cp_lexer_consume_token (parser->lexer)->value;
9824
9825       /* There is no valid C++ program where a non-template type is
9826          followed by a "<".  That usually indicates that the user thought
9827          that the type was a template.  */
9828       cp_parser_check_for_invalid_template_id (parser, type);
9829
9830       return TYPE_NAME (type);
9831     }
9832
9833   /* The type-specifier must be a user-defined type.  */
9834   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9835     {
9836       bool qualified_p;
9837       bool global_p;
9838
9839       /* Don't gobble tokens or issue error messages if this is an
9840          optional type-specifier.  */
9841       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9842         cp_parser_parse_tentatively (parser);
9843
9844       /* Look for the optional `::' operator.  */
9845       global_p
9846         = (cp_parser_global_scope_opt (parser,
9847                                        /*current_scope_valid_p=*/false)
9848            != NULL_TREE);
9849       /* Look for the nested-name specifier.  */
9850       qualified_p
9851         = (cp_parser_nested_name_specifier_opt (parser,
9852                                                 /*typename_keyword_p=*/false,
9853                                                 /*check_dependency_p=*/true,
9854                                                 /*type_p=*/false,
9855                                                 /*is_declaration=*/false)
9856            != NULL_TREE);
9857       /* If we have seen a nested-name-specifier, and the next token
9858          is `template', then we are using the template-id production.  */
9859       if (parser->scope
9860           && cp_parser_optional_template_keyword (parser))
9861         {
9862           /* Look for the template-id.  */
9863           type = cp_parser_template_id (parser,
9864                                         /*template_keyword_p=*/true,
9865                                         /*check_dependency_p=*/true,
9866                                         /*is_declaration=*/false);
9867           /* If the template-id did not name a type, we are out of
9868              luck.  */
9869           if (TREE_CODE (type) != TYPE_DECL)
9870             {
9871               cp_parser_error (parser, "expected template-id for type");
9872               type = NULL_TREE;
9873             }
9874         }
9875       /* Otherwise, look for a type-name.  */
9876       else
9877         type = cp_parser_type_name (parser);
9878       /* Keep track of all name-lookups performed in class scopes.  */
9879       if (type
9880           && !global_p
9881           && !qualified_p
9882           && TREE_CODE (type) == TYPE_DECL
9883           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9884         maybe_note_name_used_in_class (DECL_NAME (type), type);
9885       /* If it didn't work out, we don't have a TYPE.  */
9886       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9887           && !cp_parser_parse_definitely (parser))
9888         type = NULL_TREE;
9889       if (type && decl_specs)
9890         cp_parser_set_decl_spec_type (decl_specs, type,
9891                                       /*user_defined=*/true);
9892     }
9893
9894   /* If we didn't get a type-name, issue an error message.  */
9895   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9896     {
9897       cp_parser_error (parser, "expected type-name");
9898       return error_mark_node;
9899     }
9900
9901   /* There is no valid C++ program where a non-template type is
9902      followed by a "<".  That usually indicates that the user thought
9903      that the type was a template.  */
9904   if (type && type != error_mark_node)
9905     {
9906       /* As a last-ditch effort, see if TYPE is an Objective-C type.
9907          If it is, then the '<'...'>' enclose protocol names rather than
9908          template arguments, and so everything is fine.  */
9909       if (c_dialect_objc ()
9910           && (objc_is_id (type) || objc_is_class_name (type)))
9911         {
9912           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9913           tree qual_type = objc_get_protocol_qualified_type (type, protos);
9914
9915           /* Clobber the "unqualified" type previously entered into
9916              DECL_SPECS with the new, improved protocol-qualified version.  */
9917           if (decl_specs)
9918             decl_specs->type = qual_type;
9919
9920           return qual_type;
9921         }
9922
9923       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9924     }
9925
9926   return type;
9927 }
9928
9929 /* Parse a type-name.
9930
9931    type-name:
9932      class-name
9933      enum-name
9934      typedef-name
9935
9936    enum-name:
9937      identifier
9938
9939    typedef-name:
9940      identifier
9941
9942    Returns a TYPE_DECL for the type.  */
9943
9944 static tree
9945 cp_parser_type_name (cp_parser* parser)
9946 {
9947   tree type_decl;
9948   tree identifier;
9949
9950   /* We can't know yet whether it is a class-name or not.  */
9951   cp_parser_parse_tentatively (parser);
9952   /* Try a class-name.  */
9953   type_decl = cp_parser_class_name (parser,
9954                                     /*typename_keyword_p=*/false,
9955                                     /*template_keyword_p=*/false,
9956                                     none_type,
9957                                     /*check_dependency_p=*/true,
9958                                     /*class_head_p=*/false,
9959                                     /*is_declaration=*/false);
9960   /* If it's not a class-name, keep looking.  */
9961   if (!cp_parser_parse_definitely (parser))
9962     {
9963       /* It must be a typedef-name or an enum-name.  */
9964       identifier = cp_parser_identifier (parser);
9965       if (identifier == error_mark_node)
9966         return error_mark_node;
9967
9968       /* Look up the type-name.  */
9969       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9970
9971       if (TREE_CODE (type_decl) != TYPE_DECL
9972           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9973         {
9974           /* See if this is an Objective-C type.  */
9975           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9976           tree type = objc_get_protocol_qualified_type (identifier, protos);
9977           if (type)
9978             type_decl = TYPE_NAME (type);
9979         }
9980
9981       /* Issue an error if we did not find a type-name.  */
9982       if (TREE_CODE (type_decl) != TYPE_DECL)
9983         {
9984           if (!cp_parser_simulate_error (parser))
9985             cp_parser_name_lookup_error (parser, identifier, type_decl,
9986                                          "is not a type");
9987           type_decl = error_mark_node;
9988         }
9989       /* Remember that the name was used in the definition of the
9990          current class so that we can check later to see if the
9991          meaning would have been different after the class was
9992          entirely defined.  */
9993       else if (type_decl != error_mark_node
9994                && !parser->scope)
9995         maybe_note_name_used_in_class (identifier, type_decl);
9996     }
9997
9998   return type_decl;
9999 }
10000
10001
10002 /* Parse an elaborated-type-specifier.  Note that the grammar given
10003    here incorporates the resolution to DR68.
10004
10005    elaborated-type-specifier:
10006      class-key :: [opt] nested-name-specifier [opt] identifier
10007      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10008      enum :: [opt] nested-name-specifier [opt] identifier
10009      typename :: [opt] nested-name-specifier identifier
10010      typename :: [opt] nested-name-specifier template [opt]
10011        template-id
10012
10013    GNU extension:
10014
10015    elaborated-type-specifier:
10016      class-key attributes :: [opt] nested-name-specifier [opt] identifier
10017      class-key attributes :: [opt] nested-name-specifier [opt]
10018                template [opt] template-id
10019      enum attributes :: [opt] nested-name-specifier [opt] identifier
10020
10021    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10022    declared `friend'.  If IS_DECLARATION is TRUE, then this
10023    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10024    something is being declared.
10025
10026    Returns the TYPE specified.  */
10027
10028 static tree
10029 cp_parser_elaborated_type_specifier (cp_parser* parser,
10030                                      bool is_friend,
10031                                      bool is_declaration)
10032 {
10033   enum tag_types tag_type;
10034   tree identifier;
10035   tree type = NULL_TREE;
10036   tree attributes = NULL_TREE;
10037
10038   /* See if we're looking at the `enum' keyword.  */
10039   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10040     {
10041       /* Consume the `enum' token.  */
10042       cp_lexer_consume_token (parser->lexer);
10043       /* Remember that it's an enumeration type.  */
10044       tag_type = enum_type;
10045       /* Parse the attributes.  */
10046       attributes = cp_parser_attributes_opt (parser);
10047     }
10048   /* Or, it might be `typename'.  */
10049   else if (cp_lexer_next_token_is_keyword (parser->lexer,
10050                                            RID_TYPENAME))
10051     {
10052       /* Consume the `typename' token.  */
10053       cp_lexer_consume_token (parser->lexer);
10054       /* Remember that it's a `typename' type.  */
10055       tag_type = typename_type;
10056       /* The `typename' keyword is only allowed in templates.  */
10057       if (!processing_template_decl)
10058         pedwarn ("using %<typename%> outside of template");
10059     }
10060   /* Otherwise it must be a class-key.  */
10061   else
10062     {
10063       tag_type = cp_parser_class_key (parser);
10064       if (tag_type == none_type)
10065         return error_mark_node;
10066       /* Parse the attributes.  */
10067       attributes = cp_parser_attributes_opt (parser);
10068     }
10069
10070   /* Look for the `::' operator.  */
10071   cp_parser_global_scope_opt (parser,
10072                               /*current_scope_valid_p=*/false);
10073   /* Look for the nested-name-specifier.  */
10074   if (tag_type == typename_type)
10075     {
10076       if (!cp_parser_nested_name_specifier (parser,
10077                                            /*typename_keyword_p=*/true,
10078                                            /*check_dependency_p=*/true,
10079                                            /*type_p=*/true,
10080                                             is_declaration))
10081         return error_mark_node;
10082     }
10083   else
10084     /* Even though `typename' is not present, the proposed resolution
10085        to Core Issue 180 says that in `class A<T>::B', `B' should be
10086        considered a type-name, even if `A<T>' is dependent.  */
10087     cp_parser_nested_name_specifier_opt (parser,
10088                                          /*typename_keyword_p=*/true,
10089                                          /*check_dependency_p=*/true,
10090                                          /*type_p=*/true,
10091                                          is_declaration);
10092   /* For everything but enumeration types, consider a template-id.  */
10093   /* For an enumeration type, consider only a plain identifier.  */
10094   if (tag_type != enum_type)
10095     {
10096       bool template_p = false;
10097       tree decl;
10098
10099       /* Allow the `template' keyword.  */
10100       template_p = cp_parser_optional_template_keyword (parser);
10101       /* If we didn't see `template', we don't know if there's a
10102          template-id or not.  */
10103       if (!template_p)
10104         cp_parser_parse_tentatively (parser);
10105       /* Parse the template-id.  */
10106       decl = cp_parser_template_id (parser, template_p,
10107                                     /*check_dependency_p=*/true,
10108                                     is_declaration);
10109       /* If we didn't find a template-id, look for an ordinary
10110          identifier.  */
10111       if (!template_p && !cp_parser_parse_definitely (parser))
10112         ;
10113       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10114          in effect, then we must assume that, upon instantiation, the
10115          template will correspond to a class.  */
10116       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10117                && tag_type == typename_type)
10118         type = make_typename_type (parser->scope, decl,
10119                                    typename_type,
10120                                    /*complain=*/tf_error);
10121       else
10122         type = TREE_TYPE (decl);
10123     }
10124
10125   if (!type)
10126     {
10127       identifier = cp_parser_identifier (parser);
10128
10129       if (identifier == error_mark_node)
10130         {
10131           parser->scope = NULL_TREE;
10132           return error_mark_node;
10133         }
10134
10135       /* For a `typename', we needn't call xref_tag.  */
10136       if (tag_type == typename_type
10137           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10138         return cp_parser_make_typename_type (parser, parser->scope,
10139                                              identifier);
10140       /* Look up a qualified name in the usual way.  */
10141       if (parser->scope)
10142         {
10143           tree decl;
10144
10145           decl = cp_parser_lookup_name (parser, identifier,
10146                                         tag_type,
10147                                         /*is_template=*/false,
10148                                         /*is_namespace=*/false,
10149                                         /*check_dependency=*/true,
10150                                         /*ambiguous_decls=*/NULL);
10151
10152           /* If we are parsing friend declaration, DECL may be a
10153              TEMPLATE_DECL tree node here.  However, we need to check
10154              whether this TEMPLATE_DECL results in valid code.  Consider
10155              the following example:
10156
10157                namespace N {
10158                  template <class T> class C {};
10159                }
10160                class X {
10161                  template <class T> friend class N::C; // #1, valid code
10162                };
10163                template <class T> class Y {
10164                  friend class N::C;                    // #2, invalid code
10165                };
10166
10167              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10168              name lookup of `N::C'.  We see that friend declaration must
10169              be template for the code to be valid.  Note that
10170              processing_template_decl does not work here since it is
10171              always 1 for the above two cases.  */
10172
10173           decl = (cp_parser_maybe_treat_template_as_class
10174                   (decl, /*tag_name_p=*/is_friend
10175                          && parser->num_template_parameter_lists));
10176
10177           if (TREE_CODE (decl) != TYPE_DECL)
10178             {
10179               cp_parser_diagnose_invalid_type_name (parser,
10180                                                     parser->scope,
10181                                                     identifier);
10182               return error_mark_node;
10183             }
10184
10185           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10186             check_elaborated_type_specifier
10187               (tag_type, decl,
10188                (parser->num_template_parameter_lists
10189                 || DECL_SELF_REFERENCE_P (decl)));
10190
10191           type = TREE_TYPE (decl);
10192         }
10193       else
10194         {
10195           /* An elaborated-type-specifier sometimes introduces a new type and
10196              sometimes names an existing type.  Normally, the rule is that it
10197              introduces a new type only if there is not an existing type of
10198              the same name already in scope.  For example, given:
10199
10200                struct S {};
10201                void f() { struct S s; }
10202
10203              the `struct S' in the body of `f' is the same `struct S' as in
10204              the global scope; the existing definition is used.  However, if
10205              there were no global declaration, this would introduce a new
10206              local class named `S'.
10207
10208              An exception to this rule applies to the following code:
10209
10210                namespace N { struct S; }
10211
10212              Here, the elaborated-type-specifier names a new type
10213              unconditionally; even if there is already an `S' in the
10214              containing scope this declaration names a new type.
10215              This exception only applies if the elaborated-type-specifier
10216              forms the complete declaration:
10217
10218                [class.name]
10219
10220                A declaration consisting solely of `class-key identifier ;' is
10221                either a redeclaration of the name in the current scope or a
10222                forward declaration of the identifier as a class name.  It
10223                introduces the name into the current scope.
10224
10225              We are in this situation precisely when the next token is a `;'.
10226
10227              An exception to the exception is that a `friend' declaration does
10228              *not* name a new type; i.e., given:
10229
10230                struct S { friend struct T; };
10231
10232              `T' is not a new type in the scope of `S'.
10233
10234              Also, `new struct S' or `sizeof (struct S)' never results in the
10235              definition of a new type; a new type can only be declared in a
10236              declaration context.  */
10237
10238           tag_scope ts;
10239           bool template_p;
10240
10241           if (is_friend)
10242             /* Friends have special name lookup rules.  */
10243             ts = ts_within_enclosing_non_class;
10244           else if (is_declaration
10245                    && cp_lexer_next_token_is (parser->lexer,
10246                                               CPP_SEMICOLON))
10247             /* This is a `class-key identifier ;' */
10248             ts = ts_current;
10249           else
10250             ts = ts_global;
10251
10252           template_p =
10253             (parser->num_template_parameter_lists
10254              && (cp_parser_next_token_starts_class_definition_p (parser)
10255                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10256           /* An unqualified name was used to reference this type, so
10257              there were no qualifying templates.  */
10258           if (!cp_parser_check_template_parameters (parser,
10259                                                     /*num_templates=*/0))
10260             return error_mark_node;
10261           type = xref_tag (tag_type, identifier, ts, template_p);
10262         }
10263     }
10264
10265   if (type == error_mark_node)
10266     return error_mark_node;
10267
10268   /* Allow attributes on forward declarations of classes.  */
10269   if (attributes)
10270     {
10271       if (TREE_CODE (type) == TYPENAME_TYPE)
10272         warning (OPT_Wattributes,
10273                  "attributes ignored on uninstantiated type");
10274       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10275                && ! processing_explicit_instantiation)
10276         warning (OPT_Wattributes,
10277                  "attributes ignored on template instantiation");
10278       else if (is_declaration && cp_parser_declares_only_class_p (parser))
10279         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10280       else
10281         warning (OPT_Wattributes,
10282                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10283     }
10284
10285   if (tag_type != enum_type)
10286     cp_parser_check_class_key (tag_type, type);
10287
10288   /* A "<" cannot follow an elaborated type specifier.  If that
10289      happens, the user was probably trying to form a template-id.  */
10290   cp_parser_check_for_invalid_template_id (parser, type);
10291
10292   return type;
10293 }
10294
10295 /* Parse an enum-specifier.
10296
10297    enum-specifier:
10298      enum identifier [opt] { enumerator-list [opt] }
10299
10300    GNU Extensions:
10301      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10302        attributes[opt]
10303
10304    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10305    if the token stream isn't an enum-specifier after all.  */
10306
10307 static tree
10308 cp_parser_enum_specifier (cp_parser* parser)
10309 {
10310   tree identifier;
10311   tree type;
10312   tree attributes;
10313
10314   /* Parse tentatively so that we can back up if we don't find a
10315      enum-specifier.  */
10316   cp_parser_parse_tentatively (parser);
10317
10318   /* Caller guarantees that the current token is 'enum', an identifier
10319      possibly follows, and the token after that is an opening brace.
10320      If we don't have an identifier, fabricate an anonymous name for
10321      the enumeration being defined.  */
10322   cp_lexer_consume_token (parser->lexer);
10323
10324   attributes = cp_parser_attributes_opt (parser);
10325
10326   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10327     identifier = cp_parser_identifier (parser);
10328   else
10329     identifier = make_anon_name ();
10330
10331   /* Look for the `{' but don't consume it yet.  */
10332   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10333     cp_parser_simulate_error (parser);
10334
10335   if (!cp_parser_parse_definitely (parser))
10336     return NULL_TREE;
10337
10338   /* Issue an error message if type-definitions are forbidden here.  */
10339   cp_parser_check_type_definition (parser);
10340
10341   /* Create the new type.  We do this before consuming the opening brace
10342      so the enum will be recorded as being on the line of its tag (or the
10343      'enum' keyword, if there is no tag).  */
10344   type = start_enum (identifier);
10345
10346   /* Consume the opening brace.  */
10347   cp_lexer_consume_token (parser->lexer);
10348
10349   if (type == error_mark_node)
10350     {
10351       cp_parser_skip_to_end_of_block_or_statement (parser);
10352       return error_mark_node;
10353     }
10354
10355   /* If the next token is not '}', then there are some enumerators.  */
10356   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10357     cp_parser_enumerator_list (parser, type);
10358
10359   /* Consume the final '}'.  */
10360   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10361
10362   /* Look for trailing attributes to apply to this enumeration, and
10363      apply them if appropriate.  */
10364   if (cp_parser_allow_gnu_extensions_p (parser))
10365     {
10366       tree trailing_attr = cp_parser_attributes_opt (parser);
10367       cplus_decl_attributes (&type,
10368                              trailing_attr,
10369                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10370     }
10371
10372   /* Finish up the enumeration.  */
10373   finish_enum (type);
10374
10375   return type;
10376 }
10377
10378 /* Parse an enumerator-list.  The enumerators all have the indicated
10379    TYPE.
10380
10381    enumerator-list:
10382      enumerator-definition
10383      enumerator-list , enumerator-definition  */
10384
10385 static void
10386 cp_parser_enumerator_list (cp_parser* parser, tree type)
10387 {
10388   while (true)
10389     {
10390       /* Parse an enumerator-definition.  */
10391       cp_parser_enumerator_definition (parser, type);
10392
10393       /* If the next token is not a ',', we've reached the end of
10394          the list.  */
10395       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10396         break;
10397       /* Otherwise, consume the `,' and keep going.  */
10398       cp_lexer_consume_token (parser->lexer);
10399       /* If the next token is a `}', there is a trailing comma.  */
10400       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10401         {
10402           if (pedantic && !in_system_header)
10403             pedwarn ("comma at end of enumerator list");
10404           break;
10405         }
10406     }
10407 }
10408
10409 /* Parse an enumerator-definition.  The enumerator has the indicated
10410    TYPE.
10411
10412    enumerator-definition:
10413      enumerator
10414      enumerator = constant-expression
10415
10416    enumerator:
10417      identifier  */
10418
10419 static void
10420 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10421 {
10422   tree identifier;
10423   tree value;
10424
10425   /* Look for the identifier.  */
10426   identifier = cp_parser_identifier (parser);
10427   if (identifier == error_mark_node)
10428     return;
10429
10430   /* If the next token is an '=', then there is an explicit value.  */
10431   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10432     {
10433       /* Consume the `=' token.  */
10434       cp_lexer_consume_token (parser->lexer);
10435       /* Parse the value.  */
10436       value = cp_parser_constant_expression (parser,
10437                                              /*allow_non_constant_p=*/false,
10438                                              NULL);
10439     }
10440   else
10441     value = NULL_TREE;
10442
10443   /* Create the enumerator.  */
10444   build_enumerator (identifier, value, type);
10445 }
10446
10447 /* Parse a namespace-name.
10448
10449    namespace-name:
10450      original-namespace-name
10451      namespace-alias
10452
10453    Returns the NAMESPACE_DECL for the namespace.  */
10454
10455 static tree
10456 cp_parser_namespace_name (cp_parser* parser)
10457 {
10458   tree identifier;
10459   tree namespace_decl;
10460
10461   /* Get the name of the namespace.  */
10462   identifier = cp_parser_identifier (parser);
10463   if (identifier == error_mark_node)
10464     return error_mark_node;
10465
10466   /* Look up the identifier in the currently active scope.  Look only
10467      for namespaces, due to:
10468
10469        [basic.lookup.udir]
10470
10471        When looking up a namespace-name in a using-directive or alias
10472        definition, only namespace names are considered.
10473
10474      And:
10475
10476        [basic.lookup.qual]
10477
10478        During the lookup of a name preceding the :: scope resolution
10479        operator, object, function, and enumerator names are ignored.
10480
10481      (Note that cp_parser_class_or_namespace_name only calls this
10482      function if the token after the name is the scope resolution
10483      operator.)  */
10484   namespace_decl = cp_parser_lookup_name (parser, identifier,
10485                                           none_type,
10486                                           /*is_template=*/false,
10487                                           /*is_namespace=*/true,
10488                                           /*check_dependency=*/true,
10489                                           /*ambiguous_decls=*/NULL);
10490   /* If it's not a namespace, issue an error.  */
10491   if (namespace_decl == error_mark_node
10492       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10493     {
10494       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10495         error ("%qD is not a namespace-name", identifier);
10496       cp_parser_error (parser, "expected namespace-name");
10497       namespace_decl = error_mark_node;
10498     }
10499
10500   return namespace_decl;
10501 }
10502
10503 /* Parse a namespace-definition.
10504
10505    namespace-definition:
10506      named-namespace-definition
10507      unnamed-namespace-definition
10508
10509    named-namespace-definition:
10510      original-namespace-definition
10511      extension-namespace-definition
10512
10513    original-namespace-definition:
10514      namespace identifier { namespace-body }
10515
10516    extension-namespace-definition:
10517      namespace original-namespace-name { namespace-body }
10518
10519    unnamed-namespace-definition:
10520      namespace { namespace-body } */
10521
10522 static void
10523 cp_parser_namespace_definition (cp_parser* parser)
10524 {
10525   tree identifier, attribs;
10526
10527   /* Look for the `namespace' keyword.  */
10528   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10529
10530   /* Get the name of the namespace.  We do not attempt to distinguish
10531      between an original-namespace-definition and an
10532      extension-namespace-definition at this point.  The semantic
10533      analysis routines are responsible for that.  */
10534   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10535     identifier = cp_parser_identifier (parser);
10536   else
10537     identifier = NULL_TREE;
10538
10539   /* Parse any specified attributes.  */
10540   attribs = cp_parser_attributes_opt (parser);
10541
10542   /* Look for the `{' to start the namespace.  */
10543   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10544   /* Start the namespace.  */
10545   push_namespace_with_attribs (identifier, attribs);
10546   /* Parse the body of the namespace.  */
10547   cp_parser_namespace_body (parser);
10548   /* Finish the namespace.  */
10549   pop_namespace ();
10550   /* Look for the final `}'.  */
10551   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10552 }
10553
10554 /* Parse a namespace-body.
10555
10556    namespace-body:
10557      declaration-seq [opt]  */
10558
10559 static void
10560 cp_parser_namespace_body (cp_parser* parser)
10561 {
10562   cp_parser_declaration_seq_opt (parser);
10563 }
10564
10565 /* Parse a namespace-alias-definition.
10566
10567    namespace-alias-definition:
10568      namespace identifier = qualified-namespace-specifier ;  */
10569
10570 static void
10571 cp_parser_namespace_alias_definition (cp_parser* parser)
10572 {
10573   tree identifier;
10574   tree namespace_specifier;
10575
10576   /* Look for the `namespace' keyword.  */
10577   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10578   /* Look for the identifier.  */
10579   identifier = cp_parser_identifier (parser);
10580   if (identifier == error_mark_node)
10581     return;
10582   /* Look for the `=' token.  */
10583   cp_parser_require (parser, CPP_EQ, "`='");
10584   /* Look for the qualified-namespace-specifier.  */
10585   namespace_specifier
10586     = cp_parser_qualified_namespace_specifier (parser);
10587   /* Look for the `;' token.  */
10588   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10589
10590   /* Register the alias in the symbol table.  */
10591   do_namespace_alias (identifier, namespace_specifier);
10592 }
10593
10594 /* Parse a qualified-namespace-specifier.
10595
10596    qualified-namespace-specifier:
10597      :: [opt] nested-name-specifier [opt] namespace-name
10598
10599    Returns a NAMESPACE_DECL corresponding to the specified
10600    namespace.  */
10601
10602 static tree
10603 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10604 {
10605   /* Look for the optional `::'.  */
10606   cp_parser_global_scope_opt (parser,
10607                               /*current_scope_valid_p=*/false);
10608
10609   /* Look for the optional nested-name-specifier.  */
10610   cp_parser_nested_name_specifier_opt (parser,
10611                                        /*typename_keyword_p=*/false,
10612                                        /*check_dependency_p=*/true,
10613                                        /*type_p=*/false,
10614                                        /*is_declaration=*/true);
10615
10616   return cp_parser_namespace_name (parser);
10617 }
10618
10619 /* Parse a using-declaration.
10620
10621    using-declaration:
10622      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10623      using :: unqualified-id ;  */
10624
10625 static void
10626 cp_parser_using_declaration (cp_parser* parser)
10627 {
10628   cp_token *token;
10629   bool typename_p = false;
10630   bool global_scope_p;
10631   tree decl;
10632   tree identifier;
10633   tree qscope;
10634
10635   /* Look for the `using' keyword.  */
10636   cp_parser_require_keyword (parser, RID_USING, "`using'");
10637
10638   /* Peek at the next token.  */
10639   token = cp_lexer_peek_token (parser->lexer);
10640   /* See if it's `typename'.  */
10641   if (token->keyword == RID_TYPENAME)
10642     {
10643       /* Remember that we've seen it.  */
10644       typename_p = true;
10645       /* Consume the `typename' token.  */
10646       cp_lexer_consume_token (parser->lexer);
10647     }
10648
10649   /* Look for the optional global scope qualification.  */
10650   global_scope_p
10651     = (cp_parser_global_scope_opt (parser,
10652                                    /*current_scope_valid_p=*/false)
10653        != NULL_TREE);
10654
10655   /* If we saw `typename', or didn't see `::', then there must be a
10656      nested-name-specifier present.  */
10657   if (typename_p || !global_scope_p)
10658     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10659                                               /*check_dependency_p=*/true,
10660                                               /*type_p=*/false,
10661                                               /*is_declaration=*/true);
10662   /* Otherwise, we could be in either of the two productions.  In that
10663      case, treat the nested-name-specifier as optional.  */
10664   else
10665     qscope = cp_parser_nested_name_specifier_opt (parser,
10666                                                   /*typename_keyword_p=*/false,
10667                                                   /*check_dependency_p=*/true,
10668                                                   /*type_p=*/false,
10669                                                   /*is_declaration=*/true);
10670   if (!qscope)
10671     qscope = global_namespace;
10672
10673   /* Parse the unqualified-id.  */
10674   identifier = cp_parser_unqualified_id (parser,
10675                                          /*template_keyword_p=*/false,
10676                                          /*check_dependency_p=*/true,
10677                                          /*declarator_p=*/true,
10678                                          /*optional_p=*/false);
10679
10680   /* The function we call to handle a using-declaration is different
10681      depending on what scope we are in.  */
10682   if (qscope == error_mark_node || identifier == error_mark_node)
10683     ;
10684   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10685            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10686     /* [namespace.udecl]
10687
10688        A using declaration shall not name a template-id.  */
10689     error ("a template-id may not appear in a using-declaration");
10690   else
10691     {
10692       if (at_class_scope_p ())
10693         {
10694           /* Create the USING_DECL.  */
10695           decl = do_class_using_decl (parser->scope, identifier);
10696           /* Add it to the list of members in this class.  */
10697           finish_member_declaration (decl);
10698         }
10699       else
10700         {
10701           decl = cp_parser_lookup_name_simple (parser, identifier);
10702           if (decl == error_mark_node)
10703             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10704           else if (!at_namespace_scope_p ())
10705             do_local_using_decl (decl, qscope, identifier);
10706           else
10707             do_toplevel_using_decl (decl, qscope, identifier);
10708         }
10709     }
10710
10711   /* Look for the final `;'.  */
10712   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10713 }
10714
10715 /* Parse a using-directive.
10716
10717    using-directive:
10718      using namespace :: [opt] nested-name-specifier [opt]
10719        namespace-name ;  */
10720
10721 static void
10722 cp_parser_using_directive (cp_parser* parser)
10723 {
10724   tree namespace_decl;
10725   tree attribs;
10726
10727   /* Look for the `using' keyword.  */
10728   cp_parser_require_keyword (parser, RID_USING, "`using'");
10729   /* And the `namespace' keyword.  */
10730   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10731   /* Look for the optional `::' operator.  */
10732   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10733   /* And the optional nested-name-specifier.  */
10734   cp_parser_nested_name_specifier_opt (parser,
10735                                        /*typename_keyword_p=*/false,
10736                                        /*check_dependency_p=*/true,
10737                                        /*type_p=*/false,
10738                                        /*is_declaration=*/true);
10739   /* Get the namespace being used.  */
10740   namespace_decl = cp_parser_namespace_name (parser);
10741   /* And any specified attributes.  */
10742   attribs = cp_parser_attributes_opt (parser);
10743   /* Update the symbol table.  */
10744   parse_using_directive (namespace_decl, attribs);
10745   /* Look for the final `;'.  */
10746   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10747 }
10748
10749 /* Parse an asm-definition.
10750
10751    asm-definition:
10752      asm ( string-literal ) ;
10753
10754    GNU Extension:
10755
10756    asm-definition:
10757      asm volatile [opt] ( string-literal ) ;
10758      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10759      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10760                           : asm-operand-list [opt] ) ;
10761      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10762                           : asm-operand-list [opt]
10763                           : asm-operand-list [opt] ) ;  */
10764
10765 static void
10766 cp_parser_asm_definition (cp_parser* parser)
10767 {
10768   tree string;
10769   tree outputs = NULL_TREE;
10770   tree inputs = NULL_TREE;
10771   tree clobbers = NULL_TREE;
10772   tree asm_stmt;
10773   bool volatile_p = false;
10774   bool extended_p = false;
10775
10776   /* Look for the `asm' keyword.  */
10777   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10778   /* See if the next token is `volatile'.  */
10779   if (cp_parser_allow_gnu_extensions_p (parser)
10780       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10781     {
10782       /* Remember that we saw the `volatile' keyword.  */
10783       volatile_p = true;
10784       /* Consume the token.  */
10785       cp_lexer_consume_token (parser->lexer);
10786     }
10787   /* Look for the opening `('.  */
10788   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10789     return;
10790   /* Look for the string.  */
10791   string = cp_parser_string_literal (parser, false, false);
10792   if (string == error_mark_node)
10793     {
10794       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10795                                              /*consume_paren=*/true);
10796       return;
10797     }
10798
10799   /* If we're allowing GNU extensions, check for the extended assembly
10800      syntax.  Unfortunately, the `:' tokens need not be separated by
10801      a space in C, and so, for compatibility, we tolerate that here
10802      too.  Doing that means that we have to treat the `::' operator as
10803      two `:' tokens.  */
10804   if (cp_parser_allow_gnu_extensions_p (parser)
10805       && at_function_scope_p ()
10806       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10807           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10808     {
10809       bool inputs_p = false;
10810       bool clobbers_p = false;
10811
10812       /* The extended syntax was used.  */
10813       extended_p = true;
10814
10815       /* Look for outputs.  */
10816       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10817         {
10818           /* Consume the `:'.  */
10819           cp_lexer_consume_token (parser->lexer);
10820           /* Parse the output-operands.  */
10821           if (cp_lexer_next_token_is_not (parser->lexer,
10822                                           CPP_COLON)
10823               && cp_lexer_next_token_is_not (parser->lexer,
10824                                              CPP_SCOPE)
10825               && cp_lexer_next_token_is_not (parser->lexer,
10826                                              CPP_CLOSE_PAREN))
10827             outputs = cp_parser_asm_operand_list (parser);
10828         }
10829       /* If the next token is `::', there are no outputs, and the
10830          next token is the beginning of the inputs.  */
10831       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10832         /* The inputs are coming next.  */
10833         inputs_p = true;
10834
10835       /* Look for inputs.  */
10836       if (inputs_p
10837           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10838         {
10839           /* Consume the `:' or `::'.  */
10840           cp_lexer_consume_token (parser->lexer);
10841           /* Parse the output-operands.  */
10842           if (cp_lexer_next_token_is_not (parser->lexer,
10843                                           CPP_COLON)
10844               && cp_lexer_next_token_is_not (parser->lexer,
10845                                              CPP_CLOSE_PAREN))
10846             inputs = cp_parser_asm_operand_list (parser);
10847         }
10848       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10849         /* The clobbers are coming next.  */
10850         clobbers_p = true;
10851
10852       /* Look for clobbers.  */
10853       if (clobbers_p
10854           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10855         {
10856           /* Consume the `:' or `::'.  */
10857           cp_lexer_consume_token (parser->lexer);
10858           /* Parse the clobbers.  */
10859           if (cp_lexer_next_token_is_not (parser->lexer,
10860                                           CPP_CLOSE_PAREN))
10861             clobbers = cp_parser_asm_clobber_list (parser);
10862         }
10863     }
10864   /* Look for the closing `)'.  */
10865   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10866     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10867                                            /*consume_paren=*/true);
10868   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10869
10870   /* Create the ASM_EXPR.  */
10871   if (at_function_scope_p ())
10872     {
10873       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10874                                   inputs, clobbers);
10875       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10876       if (!extended_p)
10877         {
10878           tree temp = asm_stmt;
10879           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10880             temp = TREE_OPERAND (temp, 0);
10881
10882           ASM_INPUT_P (temp) = 1;
10883         }
10884     }
10885   else
10886     cgraph_add_asm_node (string);
10887 }
10888
10889 /* Declarators [gram.dcl.decl] */
10890
10891 /* Parse an init-declarator.
10892
10893    init-declarator:
10894      declarator initializer [opt]
10895
10896    GNU Extension:
10897
10898    init-declarator:
10899      declarator asm-specification [opt] attributes [opt] initializer [opt]
10900
10901    function-definition:
10902      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10903        function-body
10904      decl-specifier-seq [opt] declarator function-try-block
10905
10906    GNU Extension:
10907
10908    function-definition:
10909      __extension__ function-definition
10910
10911    The DECL_SPECIFIERS apply to this declarator.  Returns a
10912    representation of the entity declared.  If MEMBER_P is TRUE, then
10913    this declarator appears in a class scope.  The new DECL created by
10914    this declarator is returned.
10915
10916    The CHECKS are access checks that should be performed once we know
10917    what entity is being declared (and, therefore, what classes have
10918    befriended it).
10919
10920    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10921    for a function-definition here as well.  If the declarator is a
10922    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10923    be TRUE upon return.  By that point, the function-definition will
10924    have been completely parsed.
10925
10926    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10927    is FALSE.  */
10928
10929 static tree
10930 cp_parser_init_declarator (cp_parser* parser,
10931                            cp_decl_specifier_seq *decl_specifiers,
10932                            tree checks,
10933                            bool function_definition_allowed_p,
10934                            bool member_p,
10935                            int declares_class_or_enum,
10936                            bool* function_definition_p)
10937 {
10938   cp_token *token;
10939   cp_declarator *declarator;
10940   tree prefix_attributes;
10941   tree attributes;
10942   tree asm_specification;
10943   tree initializer;
10944   tree decl = NULL_TREE;
10945   tree scope;
10946   bool is_initialized;
10947   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
10948      initialized with "= ..", CPP_OPEN_PAREN if initialized with
10949      "(...)".  */
10950   enum cpp_ttype initialization_kind;
10951   bool is_parenthesized_init = false;
10952   bool is_non_constant_init;
10953   int ctor_dtor_or_conv_p;
10954   bool friend_p;
10955   tree pushed_scope = NULL;
10956
10957   /* Gather the attributes that were provided with the
10958      decl-specifiers.  */
10959   prefix_attributes = decl_specifiers->attributes;
10960
10961   /* Assume that this is not the declarator for a function
10962      definition.  */
10963   if (function_definition_p)
10964     *function_definition_p = false;
10965
10966   /* Defer access checks while parsing the declarator; we cannot know
10967      what names are accessible until we know what is being
10968      declared.  */
10969   resume_deferring_access_checks ();
10970
10971   /* Parse the declarator.  */
10972   declarator
10973     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10974                             &ctor_dtor_or_conv_p,
10975                             /*parenthesized_p=*/NULL,
10976                             /*member_p=*/false);
10977   /* Gather up the deferred checks.  */
10978   stop_deferring_access_checks ();
10979
10980   /* If the DECLARATOR was erroneous, there's no need to go
10981      further.  */
10982   if (declarator == cp_error_declarator)
10983     return error_mark_node;
10984
10985   if (declares_class_or_enum & 2)
10986     cp_parser_check_for_definition_in_return_type (declarator,
10987                                                    decl_specifiers->type);
10988
10989   /* Figure out what scope the entity declared by the DECLARATOR is
10990      located in.  `grokdeclarator' sometimes changes the scope, so
10991      we compute it now.  */
10992   scope = get_scope_of_declarator (declarator);
10993
10994   /* If we're allowing GNU extensions, look for an asm-specification
10995      and attributes.  */
10996   if (cp_parser_allow_gnu_extensions_p (parser))
10997     {
10998       /* Look for an asm-specification.  */
10999       asm_specification = cp_parser_asm_specification_opt (parser);
11000       /* And attributes.  */
11001       attributes = cp_parser_attributes_opt (parser);
11002     }
11003   else
11004     {
11005       asm_specification = NULL_TREE;
11006       attributes = NULL_TREE;
11007     }
11008
11009   /* Peek at the next token.  */
11010   token = cp_lexer_peek_token (parser->lexer);
11011   /* Check to see if the token indicates the start of a
11012      function-definition.  */
11013   if (cp_parser_token_starts_function_definition_p (token))
11014     {
11015       if (!function_definition_allowed_p)
11016         {
11017           /* If a function-definition should not appear here, issue an
11018              error message.  */
11019           cp_parser_error (parser,
11020                            "a function-definition is not allowed here");
11021           return error_mark_node;
11022         }
11023       else
11024         {
11025           /* Neither attributes nor an asm-specification are allowed
11026              on a function-definition.  */
11027           if (asm_specification)
11028             error ("an asm-specification is not allowed on a function-definition");
11029           if (attributes)
11030             error ("attributes are not allowed on a function-definition");
11031           /* This is a function-definition.  */
11032           *function_definition_p = true;
11033
11034           /* Parse the function definition.  */
11035           if (member_p)
11036             decl = cp_parser_save_member_function_body (parser,
11037                                                         decl_specifiers,
11038                                                         declarator,
11039                                                         prefix_attributes);
11040           else
11041             decl
11042               = (cp_parser_function_definition_from_specifiers_and_declarator
11043                  (parser, decl_specifiers, prefix_attributes, declarator));
11044
11045           return decl;
11046         }
11047     }
11048
11049   /* [dcl.dcl]
11050
11051      Only in function declarations for constructors, destructors, and
11052      type conversions can the decl-specifier-seq be omitted.
11053
11054      We explicitly postpone this check past the point where we handle
11055      function-definitions because we tolerate function-definitions
11056      that are missing their return types in some modes.  */
11057   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11058     {
11059       cp_parser_error (parser,
11060                        "expected constructor, destructor, or type conversion");
11061       return error_mark_node;
11062     }
11063
11064   /* An `=' or an `(' indicates an initializer.  */
11065   if (token->type == CPP_EQ
11066       || token->type == CPP_OPEN_PAREN)
11067     {
11068       is_initialized = true;
11069       initialization_kind = token->type;
11070     }
11071   else
11072     {
11073       /* If the init-declarator isn't initialized and isn't followed by a
11074          `,' or `;', it's not a valid init-declarator.  */
11075       if (token->type != CPP_COMMA
11076           && token->type != CPP_SEMICOLON)
11077         {
11078           cp_parser_error (parser, "expected initializer");
11079           return error_mark_node;
11080         }
11081       is_initialized = false;
11082       initialization_kind = CPP_EOF;
11083     }
11084
11085   /* Because start_decl has side-effects, we should only call it if we
11086      know we're going ahead.  By this point, we know that we cannot
11087      possibly be looking at any other construct.  */
11088   cp_parser_commit_to_tentative_parse (parser);
11089
11090   /* If the decl specifiers were bad, issue an error now that we're
11091      sure this was intended to be a declarator.  Then continue
11092      declaring the variable(s), as int, to try to cut down on further
11093      errors.  */
11094   if (decl_specifiers->any_specifiers_p
11095       && decl_specifiers->type == error_mark_node)
11096     {
11097       cp_parser_error (parser, "invalid type in declaration");
11098       decl_specifiers->type = integer_type_node;
11099     }
11100
11101   /* Check to see whether or not this declaration is a friend.  */
11102   friend_p = cp_parser_friend_p (decl_specifiers);
11103
11104   /* Check that the number of template-parameter-lists is OK.  */
11105   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11106     return error_mark_node;
11107
11108   /* Enter the newly declared entry in the symbol table.  If we're
11109      processing a declaration in a class-specifier, we wait until
11110      after processing the initializer.  */
11111   if (!member_p)
11112     {
11113       if (parser->in_unbraced_linkage_specification_p)
11114         decl_specifiers->storage_class = sc_extern;
11115       decl = start_decl (declarator, decl_specifiers,
11116                          is_initialized, attributes, prefix_attributes,
11117                          &pushed_scope);
11118     }
11119   else if (scope)
11120     /* Enter the SCOPE.  That way unqualified names appearing in the
11121        initializer will be looked up in SCOPE.  */
11122     pushed_scope = push_scope (scope);
11123
11124   /* Perform deferred access control checks, now that we know in which
11125      SCOPE the declared entity resides.  */
11126   if (!member_p && decl)
11127     {
11128       tree saved_current_function_decl = NULL_TREE;
11129
11130       /* If the entity being declared is a function, pretend that we
11131          are in its scope.  If it is a `friend', it may have access to
11132          things that would not otherwise be accessible.  */
11133       if (TREE_CODE (decl) == FUNCTION_DECL)
11134         {
11135           saved_current_function_decl = current_function_decl;
11136           current_function_decl = decl;
11137         }
11138
11139       /* Perform access checks for template parameters.  */
11140       cp_parser_perform_template_parameter_access_checks (checks);
11141
11142       /* Perform the access control checks for the declarator and the
11143          the decl-specifiers.  */
11144       perform_deferred_access_checks ();
11145
11146       /* Restore the saved value.  */
11147       if (TREE_CODE (decl) == FUNCTION_DECL)
11148         current_function_decl = saved_current_function_decl;
11149     }
11150
11151   /* Parse the initializer.  */
11152   initializer = NULL_TREE;
11153   is_parenthesized_init = false;
11154   is_non_constant_init = true;
11155   if (is_initialized)
11156     {
11157       if (declarator->kind == cdk_function
11158           && declarator->declarator->kind == cdk_id
11159           && initialization_kind == CPP_EQ)
11160         initializer = cp_parser_pure_specifier (parser);
11161       else
11162         initializer = cp_parser_initializer (parser,
11163                                              &is_parenthesized_init,
11164                                              &is_non_constant_init);
11165     }
11166
11167   /* The old parser allows attributes to appear after a parenthesized
11168      initializer.  Mark Mitchell proposed removing this functionality
11169      on the GCC mailing lists on 2002-08-13.  This parser accepts the
11170      attributes -- but ignores them.  */
11171   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11172     if (cp_parser_attributes_opt (parser))
11173       warning (OPT_Wattributes,
11174                "attributes after parenthesized initializer ignored");
11175
11176   /* For an in-class declaration, use `grokfield' to create the
11177      declaration.  */
11178   if (member_p)
11179     {
11180       if (pushed_scope)
11181         {
11182           pop_scope (pushed_scope);
11183           pushed_scope = false;
11184         }
11185       decl = grokfield (declarator, decl_specifiers,
11186                         initializer, !is_non_constant_init,
11187                         /*asmspec=*/NULL_TREE,
11188                         prefix_attributes);
11189       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11190         cp_parser_save_default_args (parser, decl);
11191     }
11192
11193   /* Finish processing the declaration.  But, skip friend
11194      declarations.  */
11195   if (!friend_p && decl && decl != error_mark_node)
11196     {
11197       cp_finish_decl (decl,
11198                       initializer, !is_non_constant_init,
11199                       asm_specification,
11200                       /* If the initializer is in parentheses, then this is
11201                          a direct-initialization, which means that an
11202                          `explicit' constructor is OK.  Otherwise, an
11203                          `explicit' constructor cannot be used.  */
11204                       ((is_parenthesized_init || !is_initialized)
11205                      ? 0 : LOOKUP_ONLYCONVERTING));
11206     }
11207   if (!friend_p && pushed_scope)
11208     pop_scope (pushed_scope);
11209
11210   return decl;
11211 }
11212
11213 /* Parse a declarator.
11214
11215    declarator:
11216      direct-declarator
11217      ptr-operator declarator
11218
11219    abstract-declarator:
11220      ptr-operator abstract-declarator [opt]
11221      direct-abstract-declarator
11222
11223    GNU Extensions:
11224
11225    declarator:
11226      attributes [opt] direct-declarator
11227      attributes [opt] ptr-operator declarator
11228
11229    abstract-declarator:
11230      attributes [opt] ptr-operator abstract-declarator [opt]
11231      attributes [opt] direct-abstract-declarator
11232
11233    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11234    detect constructor, destructor or conversion operators. It is set
11235    to -1 if the declarator is a name, and +1 if it is a
11236    function. Otherwise it is set to zero. Usually you just want to
11237    test for >0, but internally the negative value is used.
11238
11239    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11240    a decl-specifier-seq unless it declares a constructor, destructor,
11241    or conversion.  It might seem that we could check this condition in
11242    semantic analysis, rather than parsing, but that makes it difficult
11243    to handle something like `f()'.  We want to notice that there are
11244    no decl-specifiers, and therefore realize that this is an
11245    expression, not a declaration.)
11246
11247    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11248    the declarator is a direct-declarator of the form "(...)".
11249
11250    MEMBER_P is true iff this declarator is a member-declarator.  */
11251
11252 static cp_declarator *
11253 cp_parser_declarator (cp_parser* parser,
11254                       cp_parser_declarator_kind dcl_kind,
11255                       int* ctor_dtor_or_conv_p,
11256                       bool* parenthesized_p,
11257                       bool member_p)
11258 {
11259   cp_token *token;
11260   cp_declarator *declarator;
11261   enum tree_code code;
11262   cp_cv_quals cv_quals;
11263   tree class_type;
11264   tree attributes = NULL_TREE;
11265
11266   /* Assume this is not a constructor, destructor, or type-conversion
11267      operator.  */
11268   if (ctor_dtor_or_conv_p)
11269     *ctor_dtor_or_conv_p = 0;
11270
11271   if (cp_parser_allow_gnu_extensions_p (parser))
11272     attributes = cp_parser_attributes_opt (parser);
11273
11274   /* Peek at the next token.  */
11275   token = cp_lexer_peek_token (parser->lexer);
11276
11277   /* Check for the ptr-operator production.  */
11278   cp_parser_parse_tentatively (parser);
11279   /* Parse the ptr-operator.  */
11280   code = cp_parser_ptr_operator (parser,
11281                                  &class_type,
11282                                  &cv_quals);
11283   /* If that worked, then we have a ptr-operator.  */
11284   if (cp_parser_parse_definitely (parser))
11285     {
11286       /* If a ptr-operator was found, then this declarator was not
11287          parenthesized.  */
11288       if (parenthesized_p)
11289         *parenthesized_p = true;
11290       /* The dependent declarator is optional if we are parsing an
11291          abstract-declarator.  */
11292       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11293         cp_parser_parse_tentatively (parser);
11294
11295       /* Parse the dependent declarator.  */
11296       declarator = cp_parser_declarator (parser, dcl_kind,
11297                                          /*ctor_dtor_or_conv_p=*/NULL,
11298                                          /*parenthesized_p=*/NULL,
11299                                          /*member_p=*/false);
11300
11301       /* If we are parsing an abstract-declarator, we must handle the
11302          case where the dependent declarator is absent.  */
11303       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11304           && !cp_parser_parse_definitely (parser))
11305         declarator = NULL;
11306
11307       /* Build the representation of the ptr-operator.  */
11308       if (class_type)
11309         declarator = make_ptrmem_declarator (cv_quals,
11310                                              class_type,
11311                                              declarator);
11312       else if (code == INDIRECT_REF)
11313         declarator = make_pointer_declarator (cv_quals, declarator);
11314       else
11315         declarator = make_reference_declarator (cv_quals, declarator);
11316     }
11317   /* Everything else is a direct-declarator.  */
11318   else
11319     {
11320       if (parenthesized_p)
11321         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11322                                                    CPP_OPEN_PAREN);
11323       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11324                                                 ctor_dtor_or_conv_p,
11325                                                 member_p);
11326     }
11327
11328   if (attributes && declarator && declarator != cp_error_declarator)
11329     declarator->attributes = attributes;
11330
11331   return declarator;
11332 }
11333
11334 /* Parse a direct-declarator or direct-abstract-declarator.
11335
11336    direct-declarator:
11337      declarator-id
11338      direct-declarator ( parameter-declaration-clause )
11339        cv-qualifier-seq [opt]
11340        exception-specification [opt]
11341      direct-declarator [ constant-expression [opt] ]
11342      ( declarator )
11343
11344    direct-abstract-declarator:
11345      direct-abstract-declarator [opt]
11346        ( parameter-declaration-clause )
11347        cv-qualifier-seq [opt]
11348        exception-specification [opt]
11349      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11350      ( abstract-declarator )
11351
11352    Returns a representation of the declarator.  DCL_KIND is
11353    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11354    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11355    we are parsing a direct-declarator.  It is
11356    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11357    of ambiguity we prefer an abstract declarator, as per
11358    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11359    cp_parser_declarator.  */
11360
11361 static cp_declarator *
11362 cp_parser_direct_declarator (cp_parser* parser,
11363                              cp_parser_declarator_kind dcl_kind,
11364                              int* ctor_dtor_or_conv_p,
11365                              bool member_p)
11366 {
11367   cp_token *token;
11368   cp_declarator *declarator = NULL;
11369   tree scope = NULL_TREE;
11370   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11371   bool saved_in_declarator_p = parser->in_declarator_p;
11372   bool first = true;
11373   tree pushed_scope = NULL_TREE;
11374
11375   while (true)
11376     {
11377       /* Peek at the next token.  */
11378       token = cp_lexer_peek_token (parser->lexer);
11379       if (token->type == CPP_OPEN_PAREN)
11380         {
11381           /* This is either a parameter-declaration-clause, or a
11382              parenthesized declarator. When we know we are parsing a
11383              named declarator, it must be a parenthesized declarator
11384              if FIRST is true. For instance, `(int)' is a
11385              parameter-declaration-clause, with an omitted
11386              direct-abstract-declarator. But `((*))', is a
11387              parenthesized abstract declarator. Finally, when T is a
11388              template parameter `(T)' is a
11389              parameter-declaration-clause, and not a parenthesized
11390              named declarator.
11391
11392              We first try and parse a parameter-declaration-clause,
11393              and then try a nested declarator (if FIRST is true).
11394
11395              It is not an error for it not to be a
11396              parameter-declaration-clause, even when FIRST is
11397              false. Consider,
11398
11399                int i (int);
11400                int i (3);
11401
11402              The first is the declaration of a function while the
11403              second is a the definition of a variable, including its
11404              initializer.
11405
11406              Having seen only the parenthesis, we cannot know which of
11407              these two alternatives should be selected.  Even more
11408              complex are examples like:
11409
11410                int i (int (a));
11411                int i (int (3));
11412
11413              The former is a function-declaration; the latter is a
11414              variable initialization.
11415
11416              Thus again, we try a parameter-declaration-clause, and if
11417              that fails, we back out and return.  */
11418
11419           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11420             {
11421               cp_parameter_declarator *params;
11422               unsigned saved_num_template_parameter_lists;
11423
11424               /* In a member-declarator, the only valid interpretation
11425                  of a parenthesis is the start of a
11426                  parameter-declaration-clause.  (It is invalid to
11427                  initialize a static data member with a parenthesized
11428                  initializer; only the "=" form of initialization is
11429                  permitted.)  */
11430               if (!member_p)
11431                 cp_parser_parse_tentatively (parser);
11432
11433               /* Consume the `('.  */
11434               cp_lexer_consume_token (parser->lexer);
11435               if (first)
11436                 {
11437                   /* If this is going to be an abstract declarator, we're
11438                      in a declarator and we can't have default args.  */
11439                   parser->default_arg_ok_p = false;
11440                   parser->in_declarator_p = true;
11441                 }
11442
11443               /* Inside the function parameter list, surrounding
11444                  template-parameter-lists do not apply.  */
11445               saved_num_template_parameter_lists
11446                 = parser->num_template_parameter_lists;
11447               parser->num_template_parameter_lists = 0;
11448
11449               /* Parse the parameter-declaration-clause.  */
11450               params = cp_parser_parameter_declaration_clause (parser);
11451
11452               parser->num_template_parameter_lists
11453                 = saved_num_template_parameter_lists;
11454
11455               /* If all went well, parse the cv-qualifier-seq and the
11456                  exception-specification.  */
11457               if (member_p || cp_parser_parse_definitely (parser))
11458                 {
11459                   cp_cv_quals cv_quals;
11460                   tree exception_specification;
11461
11462                   if (ctor_dtor_or_conv_p)
11463                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11464                   first = false;
11465                   /* Consume the `)'.  */
11466                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11467
11468                   /* Parse the cv-qualifier-seq.  */
11469                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11470                   /* And the exception-specification.  */
11471                   exception_specification
11472                     = cp_parser_exception_specification_opt (parser);
11473
11474                   /* Create the function-declarator.  */
11475                   declarator = make_call_declarator (declarator,
11476                                                      params,
11477                                                      cv_quals,
11478                                                      exception_specification);
11479                   /* Any subsequent parameter lists are to do with
11480                      return type, so are not those of the declared
11481                      function.  */
11482                   parser->default_arg_ok_p = false;
11483
11484                   /* Repeat the main loop.  */
11485                   continue;
11486                 }
11487             }
11488
11489           /* If this is the first, we can try a parenthesized
11490              declarator.  */
11491           if (first)
11492             {
11493               bool saved_in_type_id_in_expr_p;
11494
11495               parser->default_arg_ok_p = saved_default_arg_ok_p;
11496               parser->in_declarator_p = saved_in_declarator_p;
11497
11498               /* Consume the `('.  */
11499               cp_lexer_consume_token (parser->lexer);
11500               /* Parse the nested declarator.  */
11501               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11502               parser->in_type_id_in_expr_p = true;
11503               declarator
11504                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11505                                         /*parenthesized_p=*/NULL,
11506                                         member_p);
11507               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11508               first = false;
11509               /* Expect a `)'.  */
11510               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11511                 declarator = cp_error_declarator;
11512               if (declarator == cp_error_declarator)
11513                 break;
11514
11515               goto handle_declarator;
11516             }
11517           /* Otherwise, we must be done.  */
11518           else
11519             break;
11520         }
11521       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11522                && token->type == CPP_OPEN_SQUARE)
11523         {
11524           /* Parse an array-declarator.  */
11525           tree bounds;
11526
11527           if (ctor_dtor_or_conv_p)
11528             *ctor_dtor_or_conv_p = 0;
11529
11530           first = false;
11531           parser->default_arg_ok_p = false;
11532           parser->in_declarator_p = true;
11533           /* Consume the `['.  */
11534           cp_lexer_consume_token (parser->lexer);
11535           /* Peek at the next token.  */
11536           token = cp_lexer_peek_token (parser->lexer);
11537           /* If the next token is `]', then there is no
11538              constant-expression.  */
11539           if (token->type != CPP_CLOSE_SQUARE)
11540             {
11541               bool non_constant_p;
11542
11543               bounds
11544                 = cp_parser_constant_expression (parser,
11545                                                  /*allow_non_constant=*/true,
11546                                                  &non_constant_p);
11547               if (!non_constant_p)
11548                 bounds = fold_non_dependent_expr (bounds);
11549               /* Normally, the array bound must be an integral constant
11550                  expression.  However, as an extension, we allow VLAs
11551                  in function scopes.  */
11552               else if (!at_function_scope_p ())
11553                 {
11554                   error ("array bound is not an integer constant");
11555                   bounds = error_mark_node;
11556                 }
11557             }
11558           else
11559             bounds = NULL_TREE;
11560           /* Look for the closing `]'.  */
11561           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11562             {
11563               declarator = cp_error_declarator;
11564               break;
11565             }
11566
11567           declarator = make_array_declarator (declarator, bounds);
11568         }
11569       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11570         {
11571           tree qualifying_scope;
11572           tree unqualified_name;
11573           special_function_kind sfk;
11574           bool abstract_ok;
11575
11576           /* Parse a declarator-id */
11577           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11578           if (abstract_ok)
11579             cp_parser_parse_tentatively (parser);
11580           unqualified_name
11581             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11582           qualifying_scope = parser->scope;
11583           if (abstract_ok)
11584             {
11585               if (!cp_parser_parse_definitely (parser))
11586                 unqualified_name = error_mark_node;
11587               else if (unqualified_name
11588                        && (qualifying_scope
11589                            || (TREE_CODE (unqualified_name)
11590                                != IDENTIFIER_NODE)))
11591                 {
11592                   cp_parser_error (parser, "expected unqualified-id");
11593                   unqualified_name = error_mark_node;
11594                 }
11595             }
11596
11597           if (!unqualified_name)
11598             return NULL;
11599           if (unqualified_name == error_mark_node)
11600             {
11601               declarator = cp_error_declarator;
11602               break;
11603             }
11604
11605           if (qualifying_scope && at_namespace_scope_p ()
11606               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11607             {
11608               /* In the declaration of a member of a template class
11609                  outside of the class itself, the SCOPE will sometimes
11610                  be a TYPENAME_TYPE.  For example, given:
11611
11612                  template <typename T>
11613                  int S<T>::R::i = 3;
11614
11615                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11616                  this context, we must resolve S<T>::R to an ordinary
11617                  type, rather than a typename type.
11618
11619                  The reason we normally avoid resolving TYPENAME_TYPEs
11620                  is that a specialization of `S' might render
11621                  `S<T>::R' not a type.  However, if `S' is
11622                  specialized, then this `i' will not be used, so there
11623                  is no harm in resolving the types here.  */
11624               tree type;
11625
11626               /* Resolve the TYPENAME_TYPE.  */
11627               type = resolve_typename_type (qualifying_scope,
11628                                             /*only_current_p=*/false);
11629               /* If that failed, the declarator is invalid.  */
11630               if (type == error_mark_node)
11631                 error ("%<%T::%D%> is not a type",
11632                        TYPE_CONTEXT (qualifying_scope),
11633                        TYPE_IDENTIFIER (qualifying_scope));
11634               qualifying_scope = type;
11635             }
11636
11637           sfk = sfk_none;
11638           if (unqualified_name)
11639             {
11640               tree class_type;
11641
11642               if (qualifying_scope
11643                   && CLASS_TYPE_P (qualifying_scope))
11644                 class_type = qualifying_scope;
11645               else
11646                 class_type = current_class_type;
11647
11648               if (TREE_CODE (unqualified_name) == TYPE_DECL)
11649                 {
11650                   tree name_type = TREE_TYPE (unqualified_name);
11651                   if (class_type && same_type_p (name_type, class_type))
11652                     {
11653                       if (qualifying_scope
11654                           && CLASSTYPE_USE_TEMPLATE (name_type))
11655                         {
11656                           error ("invalid use of constructor as a template");
11657                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11658                                   "name the constructor in a qualified name",
11659                                   class_type,
11660                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11661                                   class_type, name_type);
11662                           declarator = cp_error_declarator;
11663                           break;
11664                         }
11665                       else
11666                         unqualified_name = constructor_name (class_type);
11667                     }
11668                   else
11669                     {
11670                       /* We do not attempt to print the declarator
11671                          here because we do not have enough
11672                          information about its original syntactic
11673                          form.  */
11674                       cp_parser_error (parser, "invalid declarator");
11675                       declarator = cp_error_declarator;
11676                       break;
11677                     }
11678                 }
11679
11680               if (class_type)
11681                 {
11682                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11683                     sfk = sfk_destructor;
11684                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11685                     sfk = sfk_conversion;
11686                   else if (/* There's no way to declare a constructor
11687                               for an anonymous type, even if the type
11688                               got a name for linkage purposes.  */
11689                            !TYPE_WAS_ANONYMOUS (class_type)
11690                            && constructor_name_p (unqualified_name,
11691                                                   class_type))
11692                     {
11693                       unqualified_name = constructor_name (class_type);
11694                       sfk = sfk_constructor;
11695                     }
11696
11697                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
11698                     *ctor_dtor_or_conv_p = -1;
11699                 }
11700             }
11701           declarator = make_id_declarator (qualifying_scope,
11702                                            unqualified_name,
11703                                            sfk);
11704           declarator->id_loc = token->location;
11705
11706         handle_declarator:;
11707           scope = get_scope_of_declarator (declarator);
11708           if (scope)
11709             /* Any names that appear after the declarator-id for a
11710                member are looked up in the containing scope.  */
11711             pushed_scope = push_scope (scope);
11712           parser->in_declarator_p = true;
11713           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11714               || (declarator && declarator->kind == cdk_id))
11715             /* Default args are only allowed on function
11716                declarations.  */
11717             parser->default_arg_ok_p = saved_default_arg_ok_p;
11718           else
11719             parser->default_arg_ok_p = false;
11720
11721           first = false;
11722         }
11723       /* We're done.  */
11724       else
11725         break;
11726     }
11727
11728   /* For an abstract declarator, we might wind up with nothing at this
11729      point.  That's an error; the declarator is not optional.  */
11730   if (!declarator)
11731     cp_parser_error (parser, "expected declarator");
11732
11733   /* If we entered a scope, we must exit it now.  */
11734   if (pushed_scope)
11735     pop_scope (pushed_scope);
11736
11737   parser->default_arg_ok_p = saved_default_arg_ok_p;
11738   parser->in_declarator_p = saved_in_declarator_p;
11739
11740   return declarator;
11741 }
11742
11743 /* Parse a ptr-operator.
11744
11745    ptr-operator:
11746      * cv-qualifier-seq [opt]
11747      &
11748      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11749
11750    GNU Extension:
11751
11752    ptr-operator:
11753      & cv-qualifier-seq [opt]
11754
11755    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11756    Returns ADDR_EXPR if a reference was used.  In the case of a
11757    pointer-to-member, *TYPE is filled in with the TYPE containing the
11758    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11759    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11760    ERROR_MARK if an error occurred.  */
11761
11762 static enum tree_code
11763 cp_parser_ptr_operator (cp_parser* parser,
11764                         tree* type,
11765                         cp_cv_quals *cv_quals)
11766 {
11767   enum tree_code code = ERROR_MARK;
11768   cp_token *token;
11769
11770   /* Assume that it's not a pointer-to-member.  */
11771   *type = NULL_TREE;
11772   /* And that there are no cv-qualifiers.  */
11773   *cv_quals = TYPE_UNQUALIFIED;
11774
11775   /* Peek at the next token.  */
11776   token = cp_lexer_peek_token (parser->lexer);
11777   /* If it's a `*' or `&' we have a pointer or reference.  */
11778   if (token->type == CPP_MULT || token->type == CPP_AND)
11779     {
11780       /* Remember which ptr-operator we were processing.  */
11781       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11782
11783       /* Consume the `*' or `&'.  */
11784       cp_lexer_consume_token (parser->lexer);
11785
11786       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11787          `&', if we are allowing GNU extensions.  (The only qualifier
11788          that can legally appear after `&' is `restrict', but that is
11789          enforced during semantic analysis.  */
11790       if (code == INDIRECT_REF
11791           || cp_parser_allow_gnu_extensions_p (parser))
11792         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11793     }
11794   else
11795     {
11796       /* Try the pointer-to-member case.  */
11797       cp_parser_parse_tentatively (parser);
11798       /* Look for the optional `::' operator.  */
11799       cp_parser_global_scope_opt (parser,
11800                                   /*current_scope_valid_p=*/false);
11801       /* Look for the nested-name specifier.  */
11802       cp_parser_nested_name_specifier (parser,
11803                                        /*typename_keyword_p=*/false,
11804                                        /*check_dependency_p=*/true,
11805                                        /*type_p=*/false,
11806                                        /*is_declaration=*/false);
11807       /* If we found it, and the next token is a `*', then we are
11808          indeed looking at a pointer-to-member operator.  */
11809       if (!cp_parser_error_occurred (parser)
11810           && cp_parser_require (parser, CPP_MULT, "`*'"))
11811         {
11812           /* Indicate that the `*' operator was used.  */
11813           code = INDIRECT_REF;
11814
11815           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11816             error ("%qD is a namespace", parser->scope);
11817           else
11818             {
11819               /* The type of which the member is a member is given by the
11820                  current SCOPE.  */
11821               *type = parser->scope;
11822               /* The next name will not be qualified.  */
11823               parser->scope = NULL_TREE;
11824               parser->qualifying_scope = NULL_TREE;
11825               parser->object_scope = NULL_TREE;
11826               /* Look for the optional cv-qualifier-seq.  */
11827               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11828             }
11829         }
11830       /* If that didn't work we don't have a ptr-operator.  */
11831       if (!cp_parser_parse_definitely (parser))
11832         cp_parser_error (parser, "expected ptr-operator");
11833     }
11834
11835   return code;
11836 }
11837
11838 /* Parse an (optional) cv-qualifier-seq.
11839
11840    cv-qualifier-seq:
11841      cv-qualifier cv-qualifier-seq [opt]
11842
11843    cv-qualifier:
11844      const
11845      volatile
11846
11847    GNU Extension:
11848
11849    cv-qualifier:
11850      __restrict__
11851
11852    Returns a bitmask representing the cv-qualifiers.  */
11853
11854 static cp_cv_quals
11855 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11856 {
11857   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11858
11859   while (true)
11860     {
11861       cp_token *token;
11862       cp_cv_quals cv_qualifier;
11863
11864       /* Peek at the next token.  */
11865       token = cp_lexer_peek_token (parser->lexer);
11866       /* See if it's a cv-qualifier.  */
11867       switch (token->keyword)
11868         {
11869         case RID_CONST:
11870           cv_qualifier = TYPE_QUAL_CONST;
11871           break;
11872
11873         case RID_VOLATILE:
11874           cv_qualifier = TYPE_QUAL_VOLATILE;
11875           break;
11876
11877         case RID_RESTRICT:
11878           cv_qualifier = TYPE_QUAL_RESTRICT;
11879           break;
11880
11881         default:
11882           cv_qualifier = TYPE_UNQUALIFIED;
11883           break;
11884         }
11885
11886       if (!cv_qualifier)
11887         break;
11888
11889       if (cv_quals & cv_qualifier)
11890         {
11891           error ("duplicate cv-qualifier");
11892           cp_lexer_purge_token (parser->lexer);
11893         }
11894       else
11895         {
11896           cp_lexer_consume_token (parser->lexer);
11897           cv_quals |= cv_qualifier;
11898         }
11899     }
11900
11901   return cv_quals;
11902 }
11903
11904 /* Parse a declarator-id.
11905
11906    declarator-id:
11907      id-expression
11908      :: [opt] nested-name-specifier [opt] type-name
11909
11910    In the `id-expression' case, the value returned is as for
11911    cp_parser_id_expression if the id-expression was an unqualified-id.
11912    If the id-expression was a qualified-id, then a SCOPE_REF is
11913    returned.  The first operand is the scope (either a NAMESPACE_DECL
11914    or TREE_TYPE), but the second is still just a representation of an
11915    unqualified-id.  */
11916
11917 static tree
11918 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
11919 {
11920   tree id;
11921   /* The expression must be an id-expression.  Assume that qualified
11922      names are the names of types so that:
11923
11924        template <class T>
11925        int S<T>::R::i = 3;
11926
11927      will work; we must treat `S<T>::R' as the name of a type.
11928      Similarly, assume that qualified names are templates, where
11929      required, so that:
11930
11931        template <class T>
11932        int S<T>::R<T>::i = 3;
11933
11934      will work, too.  */
11935   id = cp_parser_id_expression (parser,
11936                                 /*template_keyword_p=*/false,
11937                                 /*check_dependency_p=*/false,
11938                                 /*template_p=*/NULL,
11939                                 /*declarator_p=*/true,
11940                                 optional_p);
11941   if (id && BASELINK_P (id))
11942     id = BASELINK_FUNCTIONS (id);
11943   return id;
11944 }
11945
11946 /* Parse a type-id.
11947
11948    type-id:
11949      type-specifier-seq abstract-declarator [opt]
11950
11951    Returns the TYPE specified.  */
11952
11953 static tree
11954 cp_parser_type_id (cp_parser* parser)
11955 {
11956   cp_decl_specifier_seq type_specifier_seq;
11957   cp_declarator *abstract_declarator;
11958
11959   /* Parse the type-specifier-seq.  */
11960   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11961                                 &type_specifier_seq);
11962   if (type_specifier_seq.type == error_mark_node)
11963     return error_mark_node;
11964
11965   /* There might or might not be an abstract declarator.  */
11966   cp_parser_parse_tentatively (parser);
11967   /* Look for the declarator.  */
11968   abstract_declarator
11969     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11970                             /*parenthesized_p=*/NULL,
11971                             /*member_p=*/false);
11972   /* Check to see if there really was a declarator.  */
11973   if (!cp_parser_parse_definitely (parser))
11974     abstract_declarator = NULL;
11975
11976   return groktypename (&type_specifier_seq, abstract_declarator);
11977 }
11978
11979 /* Parse a type-specifier-seq.
11980
11981    type-specifier-seq:
11982      type-specifier type-specifier-seq [opt]
11983
11984    GNU extension:
11985
11986    type-specifier-seq:
11987      attributes type-specifier-seq [opt]
11988
11989    If IS_CONDITION is true, we are at the start of a "condition",
11990    e.g., we've just seen "if (".
11991
11992    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11993
11994 static void
11995 cp_parser_type_specifier_seq (cp_parser* parser,
11996                               bool is_condition,
11997                               cp_decl_specifier_seq *type_specifier_seq)
11998 {
11999   bool seen_type_specifier = false;
12000   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12001
12002   /* Clear the TYPE_SPECIFIER_SEQ.  */
12003   clear_decl_specs (type_specifier_seq);
12004
12005   /* Parse the type-specifiers and attributes.  */
12006   while (true)
12007     {
12008       tree type_specifier;
12009       bool is_cv_qualifier;
12010
12011       /* Check for attributes first.  */
12012       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12013         {
12014           type_specifier_seq->attributes =
12015             chainon (type_specifier_seq->attributes,
12016                      cp_parser_attributes_opt (parser));
12017           continue;
12018         }
12019
12020       /* Look for the type-specifier.  */
12021       type_specifier = cp_parser_type_specifier (parser,
12022                                                  flags,
12023                                                  type_specifier_seq,
12024                                                  /*is_declaration=*/false,
12025                                                  NULL,
12026                                                  &is_cv_qualifier);
12027       if (!type_specifier)
12028         {
12029           /* If the first type-specifier could not be found, this is not a
12030              type-specifier-seq at all.  */
12031           if (!seen_type_specifier)
12032             {
12033               cp_parser_error (parser, "expected type-specifier");
12034               type_specifier_seq->type = error_mark_node;
12035               return;
12036             }
12037           /* If subsequent type-specifiers could not be found, the
12038              type-specifier-seq is complete.  */
12039           break;
12040         }
12041
12042       seen_type_specifier = true;
12043       /* The standard says that a condition can be:
12044
12045             type-specifier-seq declarator = assignment-expression
12046
12047          However, given:
12048
12049            struct S {};
12050            if (int S = ...)
12051
12052          we should treat the "S" as a declarator, not as a
12053          type-specifier.  The standard doesn't say that explicitly for
12054          type-specifier-seq, but it does say that for
12055          decl-specifier-seq in an ordinary declaration.  Perhaps it
12056          would be clearer just to allow a decl-specifier-seq here, and
12057          then add a semantic restriction that if any decl-specifiers
12058          that are not type-specifiers appear, the program is invalid.  */
12059       if (is_condition && !is_cv_qualifier)
12060         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12061     }
12062
12063   cp_parser_check_decl_spec (type_specifier_seq);
12064 }
12065
12066 /* Parse a parameter-declaration-clause.
12067
12068    parameter-declaration-clause:
12069      parameter-declaration-list [opt] ... [opt]
12070      parameter-declaration-list , ...
12071
12072    Returns a representation for the parameter declarations.  A return
12073    value of NULL indicates a parameter-declaration-clause consisting
12074    only of an ellipsis.  */
12075
12076 static cp_parameter_declarator *
12077 cp_parser_parameter_declaration_clause (cp_parser* parser)
12078 {
12079   cp_parameter_declarator *parameters;
12080   cp_token *token;
12081   bool ellipsis_p;
12082   bool is_error;
12083
12084   /* Peek at the next token.  */
12085   token = cp_lexer_peek_token (parser->lexer);
12086   /* Check for trivial parameter-declaration-clauses.  */
12087   if (token->type == CPP_ELLIPSIS)
12088     {
12089       /* Consume the `...' token.  */
12090       cp_lexer_consume_token (parser->lexer);
12091       return NULL;
12092     }
12093   else if (token->type == CPP_CLOSE_PAREN)
12094     /* There are no parameters.  */
12095     {
12096 #ifndef NO_IMPLICIT_EXTERN_C
12097       if (in_system_header && current_class_type == NULL
12098           && current_lang_name == lang_name_c)
12099         return NULL;
12100       else
12101 #endif
12102         return no_parameters;
12103     }
12104   /* Check for `(void)', too, which is a special case.  */
12105   else if (token->keyword == RID_VOID
12106            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12107                == CPP_CLOSE_PAREN))
12108     {
12109       /* Consume the `void' token.  */
12110       cp_lexer_consume_token (parser->lexer);
12111       /* There are no parameters.  */
12112       return no_parameters;
12113     }
12114
12115   /* Parse the parameter-declaration-list.  */
12116   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12117   /* If a parse error occurred while parsing the
12118      parameter-declaration-list, then the entire
12119      parameter-declaration-clause is erroneous.  */
12120   if (is_error)
12121     return NULL;
12122
12123   /* Peek at the next token.  */
12124   token = cp_lexer_peek_token (parser->lexer);
12125   /* If it's a `,', the clause should terminate with an ellipsis.  */
12126   if (token->type == CPP_COMMA)
12127     {
12128       /* Consume the `,'.  */
12129       cp_lexer_consume_token (parser->lexer);
12130       /* Expect an ellipsis.  */
12131       ellipsis_p
12132         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12133     }
12134   /* It might also be `...' if the optional trailing `,' was
12135      omitted.  */
12136   else if (token->type == CPP_ELLIPSIS)
12137     {
12138       /* Consume the `...' token.  */
12139       cp_lexer_consume_token (parser->lexer);
12140       /* And remember that we saw it.  */
12141       ellipsis_p = true;
12142     }
12143   else
12144     ellipsis_p = false;
12145
12146   /* Finish the parameter list.  */
12147   if (parameters && ellipsis_p)
12148     parameters->ellipsis_p = true;
12149
12150   return parameters;
12151 }
12152
12153 /* Parse a parameter-declaration-list.
12154
12155    parameter-declaration-list:
12156      parameter-declaration
12157      parameter-declaration-list , parameter-declaration
12158
12159    Returns a representation of the parameter-declaration-list, as for
12160    cp_parser_parameter_declaration_clause.  However, the
12161    `void_list_node' is never appended to the list.  Upon return,
12162    *IS_ERROR will be true iff an error occurred.  */
12163
12164 static cp_parameter_declarator *
12165 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12166 {
12167   cp_parameter_declarator *parameters = NULL;
12168   cp_parameter_declarator **tail = &parameters;
12169   bool saved_in_unbraced_linkage_specification_p;
12170
12171   /* Assume all will go well.  */
12172   *is_error = false;
12173   /* The special considerations that apply to a function within an
12174      unbraced linkage specifications do not apply to the parameters
12175      to the function.  */
12176   saved_in_unbraced_linkage_specification_p 
12177     = parser->in_unbraced_linkage_specification_p;
12178   parser->in_unbraced_linkage_specification_p = false;
12179
12180   /* Look for more parameters.  */
12181   while (true)
12182     {
12183       cp_parameter_declarator *parameter;
12184       bool parenthesized_p;
12185       /* Parse the parameter.  */
12186       parameter
12187         = cp_parser_parameter_declaration (parser,
12188                                            /*template_parm_p=*/false,
12189                                            &parenthesized_p);
12190
12191       /* If a parse error occurred parsing the parameter declaration,
12192          then the entire parameter-declaration-list is erroneous.  */
12193       if (!parameter)
12194         {
12195           *is_error = true;
12196           parameters = NULL;
12197           break;
12198         }
12199       /* Add the new parameter to the list.  */
12200       *tail = parameter;
12201       tail = &parameter->next;
12202
12203       /* Peek at the next token.  */
12204       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12205           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12206           /* These are for Objective-C++ */
12207           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12208           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12209         /* The parameter-declaration-list is complete.  */
12210         break;
12211       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12212         {
12213           cp_token *token;
12214
12215           /* Peek at the next token.  */
12216           token = cp_lexer_peek_nth_token (parser->lexer, 2);
12217           /* If it's an ellipsis, then the list is complete.  */
12218           if (token->type == CPP_ELLIPSIS)
12219             break;
12220           /* Otherwise, there must be more parameters.  Consume the
12221              `,'.  */
12222           cp_lexer_consume_token (parser->lexer);
12223           /* When parsing something like:
12224
12225                 int i(float f, double d)
12226
12227              we can tell after seeing the declaration for "f" that we
12228              are not looking at an initialization of a variable "i",
12229              but rather at the declaration of a function "i".
12230
12231              Due to the fact that the parsing of template arguments
12232              (as specified to a template-id) requires backtracking we
12233              cannot use this technique when inside a template argument
12234              list.  */
12235           if (!parser->in_template_argument_list_p
12236               && !parser->in_type_id_in_expr_p
12237               && cp_parser_uncommitted_to_tentative_parse_p (parser)
12238               /* However, a parameter-declaration of the form
12239                  "foat(f)" (which is a valid declaration of a
12240                  parameter "f") can also be interpreted as an
12241                  expression (the conversion of "f" to "float").  */
12242               && !parenthesized_p)
12243             cp_parser_commit_to_tentative_parse (parser);
12244         }
12245       else
12246         {
12247           cp_parser_error (parser, "expected %<,%> or %<...%>");
12248           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12249             cp_parser_skip_to_closing_parenthesis (parser,
12250                                                    /*recovering=*/true,
12251                                                    /*or_comma=*/false,
12252                                                    /*consume_paren=*/false);
12253           break;
12254         }
12255     }
12256
12257   parser->in_unbraced_linkage_specification_p
12258     = saved_in_unbraced_linkage_specification_p;
12259
12260   return parameters;
12261 }
12262
12263 /* Parse a parameter declaration.
12264
12265    parameter-declaration:
12266      decl-specifier-seq declarator
12267      decl-specifier-seq declarator = assignment-expression
12268      decl-specifier-seq abstract-declarator [opt]
12269      decl-specifier-seq abstract-declarator [opt] = assignment-expression
12270
12271    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12272    declares a template parameter.  (In that case, a non-nested `>'
12273    token encountered during the parsing of the assignment-expression
12274    is not interpreted as a greater-than operator.)
12275
12276    Returns a representation of the parameter, or NULL if an error
12277    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12278    true iff the declarator is of the form "(p)".  */
12279
12280 static cp_parameter_declarator *
12281 cp_parser_parameter_declaration (cp_parser *parser,
12282                                  bool template_parm_p,
12283                                  bool *parenthesized_p)
12284 {
12285   int declares_class_or_enum;
12286   bool greater_than_is_operator_p;
12287   cp_decl_specifier_seq decl_specifiers;
12288   cp_declarator *declarator;
12289   tree default_argument;
12290   cp_token *token;
12291   const char *saved_message;
12292
12293   /* In a template parameter, `>' is not an operator.
12294
12295      [temp.param]
12296
12297      When parsing a default template-argument for a non-type
12298      template-parameter, the first non-nested `>' is taken as the end
12299      of the template parameter-list rather than a greater-than
12300      operator.  */
12301   greater_than_is_operator_p = !template_parm_p;
12302
12303   /* Type definitions may not appear in parameter types.  */
12304   saved_message = parser->type_definition_forbidden_message;
12305   parser->type_definition_forbidden_message
12306     = "types may not be defined in parameter types";
12307
12308   /* Parse the declaration-specifiers.  */
12309   cp_parser_decl_specifier_seq (parser,
12310                                 CP_PARSER_FLAGS_NONE,
12311                                 &decl_specifiers,
12312                                 &declares_class_or_enum);
12313   /* If an error occurred, there's no reason to attempt to parse the
12314      rest of the declaration.  */
12315   if (cp_parser_error_occurred (parser))
12316     {
12317       parser->type_definition_forbidden_message = saved_message;
12318       return NULL;
12319     }
12320
12321   /* Peek at the next token.  */
12322   token = cp_lexer_peek_token (parser->lexer);
12323   /* If the next token is a `)', `,', `=', `>', or `...', then there
12324      is no declarator.  */
12325   if (token->type == CPP_CLOSE_PAREN
12326       || token->type == CPP_COMMA
12327       || token->type == CPP_EQ
12328       || token->type == CPP_ELLIPSIS
12329       || token->type == CPP_GREATER)
12330     {
12331       declarator = NULL;
12332       if (parenthesized_p)
12333         *parenthesized_p = false;
12334     }
12335   /* Otherwise, there should be a declarator.  */
12336   else
12337     {
12338       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12339       parser->default_arg_ok_p = false;
12340
12341       /* After seeing a decl-specifier-seq, if the next token is not a
12342          "(", there is no possibility that the code is a valid
12343          expression.  Therefore, if parsing tentatively, we commit at
12344          this point.  */
12345       if (!parser->in_template_argument_list_p
12346           /* In an expression context, having seen:
12347
12348                (int((char ...
12349
12350              we cannot be sure whether we are looking at a
12351              function-type (taking a "char" as a parameter) or a cast
12352              of some object of type "char" to "int".  */
12353           && !parser->in_type_id_in_expr_p
12354           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12355           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12356         cp_parser_commit_to_tentative_parse (parser);
12357       /* Parse the declarator.  */
12358       declarator = cp_parser_declarator (parser,
12359                                          CP_PARSER_DECLARATOR_EITHER,
12360                                          /*ctor_dtor_or_conv_p=*/NULL,
12361                                          parenthesized_p,
12362                                          /*member_p=*/false);
12363       parser->default_arg_ok_p = saved_default_arg_ok_p;
12364       /* After the declarator, allow more attributes.  */
12365       decl_specifiers.attributes
12366         = chainon (decl_specifiers.attributes,
12367                    cp_parser_attributes_opt (parser));
12368     }
12369
12370   /* The restriction on defining new types applies only to the type
12371      of the parameter, not to the default argument.  */
12372   parser->type_definition_forbidden_message = saved_message;
12373
12374   /* If the next token is `=', then process a default argument.  */
12375   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12376     {
12377       bool saved_greater_than_is_operator_p;
12378       /* Consume the `='.  */
12379       cp_lexer_consume_token (parser->lexer);
12380
12381       /* If we are defining a class, then the tokens that make up the
12382          default argument must be saved and processed later.  */
12383       if (!template_parm_p && at_class_scope_p ()
12384           && TYPE_BEING_DEFINED (current_class_type))
12385         {
12386           unsigned depth = 0;
12387           cp_token *first_token;
12388           cp_token *token;
12389
12390           /* Add tokens until we have processed the entire default
12391              argument.  We add the range [first_token, token).  */
12392           first_token = cp_lexer_peek_token (parser->lexer);
12393           while (true)
12394             {
12395               bool done = false;
12396
12397               /* Peek at the next token.  */
12398               token = cp_lexer_peek_token (parser->lexer);
12399               /* What we do depends on what token we have.  */
12400               switch (token->type)
12401                 {
12402                   /* In valid code, a default argument must be
12403                      immediately followed by a `,' `)', or `...'.  */
12404                 case CPP_COMMA:
12405                 case CPP_CLOSE_PAREN:
12406                 case CPP_ELLIPSIS:
12407                   /* If we run into a non-nested `;', `}', or `]',
12408                      then the code is invalid -- but the default
12409                      argument is certainly over.  */
12410                 case CPP_SEMICOLON:
12411                 case CPP_CLOSE_BRACE:
12412                 case CPP_CLOSE_SQUARE:
12413                   if (depth == 0)
12414                     done = true;
12415                   /* Update DEPTH, if necessary.  */
12416                   else if (token->type == CPP_CLOSE_PAREN
12417                            || token->type == CPP_CLOSE_BRACE
12418                            || token->type == CPP_CLOSE_SQUARE)
12419                     --depth;
12420                   break;
12421
12422                 case CPP_OPEN_PAREN:
12423                 case CPP_OPEN_SQUARE:
12424                 case CPP_OPEN_BRACE:
12425                   ++depth;
12426                   break;
12427
12428                 case CPP_GREATER:
12429                   /* If we see a non-nested `>', and `>' is not an
12430                      operator, then it marks the end of the default
12431                      argument.  */
12432                   if (!depth && !greater_than_is_operator_p)
12433                     done = true;
12434                   break;
12435
12436                   /* If we run out of tokens, issue an error message.  */
12437                 case CPP_EOF:
12438                 case CPP_PRAGMA_EOL:
12439                   error ("file ends in default argument");
12440                   done = true;
12441                   break;
12442
12443                 case CPP_NAME:
12444                 case CPP_SCOPE:
12445                   /* In these cases, we should look for template-ids.
12446                      For example, if the default argument is
12447                      `X<int, double>()', we need to do name lookup to
12448                      figure out whether or not `X' is a template; if
12449                      so, the `,' does not end the default argument.
12450
12451                      That is not yet done.  */
12452                   break;
12453
12454                 default:
12455                   break;
12456                 }
12457
12458               /* If we've reached the end, stop.  */
12459               if (done)
12460                 break;
12461
12462               /* Add the token to the token block.  */
12463               token = cp_lexer_consume_token (parser->lexer);
12464             }
12465
12466           /* Create a DEFAULT_ARG to represented the unparsed default
12467              argument.  */
12468           default_argument = make_node (DEFAULT_ARG);
12469           DEFARG_TOKENS (default_argument)
12470             = cp_token_cache_new (first_token, token);
12471           DEFARG_INSTANTIATIONS (default_argument) = NULL;
12472         }
12473       /* Outside of a class definition, we can just parse the
12474          assignment-expression.  */
12475       else
12476         {
12477           bool saved_local_variables_forbidden_p;
12478
12479           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12480              set correctly.  */
12481           saved_greater_than_is_operator_p
12482             = parser->greater_than_is_operator_p;
12483           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12484           /* Local variable names (and the `this' keyword) may not
12485              appear in a default argument.  */
12486           saved_local_variables_forbidden_p
12487             = parser->local_variables_forbidden_p;
12488           parser->local_variables_forbidden_p = true;
12489           /* The default argument expression may cause implicitly
12490              defined member functions to be synthesized, which will
12491              result in garbage collection.  We must treat this
12492              situation as if we were within the body of function so as
12493              to avoid collecting live data on the stack.  */
12494           ++function_depth;
12495           /* Parse the assignment-expression.  */
12496           if (template_parm_p)
12497             push_deferring_access_checks (dk_no_deferred);
12498           default_argument
12499             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12500           if (template_parm_p)
12501             pop_deferring_access_checks ();
12502           /* Restore saved state.  */
12503           --function_depth;
12504           parser->greater_than_is_operator_p
12505             = saved_greater_than_is_operator_p;
12506           parser->local_variables_forbidden_p
12507             = saved_local_variables_forbidden_p;
12508         }
12509       if (!parser->default_arg_ok_p)
12510         {
12511           if (!flag_pedantic_errors)
12512             warning (0, "deprecated use of default argument for parameter of non-function");
12513           else
12514             {
12515               error ("default arguments are only permitted for function parameters");
12516               default_argument = NULL_TREE;
12517             }
12518         }
12519     }
12520   else
12521     default_argument = NULL_TREE;
12522
12523   return make_parameter_declarator (&decl_specifiers,
12524                                     declarator,
12525                                     default_argument);
12526 }
12527
12528 /* Parse a function-body.
12529
12530    function-body:
12531      compound_statement  */
12532
12533 static void
12534 cp_parser_function_body (cp_parser *parser)
12535 {
12536   cp_parser_compound_statement (parser, NULL, false);
12537 }
12538
12539 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12540    true if a ctor-initializer was present.  */
12541
12542 static bool
12543 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12544 {
12545   tree body;
12546   bool ctor_initializer_p;
12547
12548   /* Begin the function body.  */
12549   body = begin_function_body ();
12550   /* Parse the optional ctor-initializer.  */
12551   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12552   /* Parse the function-body.  */
12553   cp_parser_function_body (parser);
12554   /* Finish the function body.  */
12555   finish_function_body (body);
12556
12557   return ctor_initializer_p;
12558 }
12559
12560 /* Parse an initializer.
12561
12562    initializer:
12563      = initializer-clause
12564      ( expression-list )
12565
12566    Returns an expression representing the initializer.  If no
12567    initializer is present, NULL_TREE is returned.
12568
12569    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12570    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12571    set to FALSE if there is no initializer present.  If there is an
12572    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12573    is set to true; otherwise it is set to false.  */
12574
12575 static tree
12576 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12577                        bool* non_constant_p)
12578 {
12579   cp_token *token;
12580   tree init;
12581
12582   /* Peek at the next token.  */
12583   token = cp_lexer_peek_token (parser->lexer);
12584
12585   /* Let our caller know whether or not this initializer was
12586      parenthesized.  */
12587   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12588   /* Assume that the initializer is constant.  */
12589   *non_constant_p = false;
12590
12591   if (token->type == CPP_EQ)
12592     {
12593       /* Consume the `='.  */
12594       cp_lexer_consume_token (parser->lexer);
12595       /* Parse the initializer-clause.  */
12596       init = cp_parser_initializer_clause (parser, non_constant_p);
12597     }
12598   else if (token->type == CPP_OPEN_PAREN)
12599     init = cp_parser_parenthesized_expression_list (parser, false,
12600                                                     /*cast_p=*/false,
12601                                                     non_constant_p);
12602   else
12603     {
12604       /* Anything else is an error.  */
12605       cp_parser_error (parser, "expected initializer");
12606       init = error_mark_node;
12607     }
12608
12609   return init;
12610 }
12611
12612 /* Parse an initializer-clause.
12613
12614    initializer-clause:
12615      assignment-expression
12616      { initializer-list , [opt] }
12617      { }
12618
12619    Returns an expression representing the initializer.
12620
12621    If the `assignment-expression' production is used the value
12622    returned is simply a representation for the expression.
12623
12624    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12625    the elements of the initializer-list (or NULL, if the last
12626    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12627    NULL_TREE.  There is no way to detect whether or not the optional
12628    trailing `,' was provided.  NON_CONSTANT_P is as for
12629    cp_parser_initializer.  */
12630
12631 static tree
12632 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12633 {
12634   tree initializer;
12635
12636   /* Assume the expression is constant.  */
12637   *non_constant_p = false;
12638
12639   /* If it is not a `{', then we are looking at an
12640      assignment-expression.  */
12641   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12642     {
12643       initializer
12644         = cp_parser_constant_expression (parser,
12645                                         /*allow_non_constant_p=*/true,
12646                                         non_constant_p);
12647       if (!*non_constant_p)
12648         initializer = fold_non_dependent_expr (initializer);
12649     }
12650   else
12651     {
12652       /* Consume the `{' token.  */
12653       cp_lexer_consume_token (parser->lexer);
12654       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12655       initializer = make_node (CONSTRUCTOR);
12656       /* If it's not a `}', then there is a non-trivial initializer.  */
12657       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12658         {
12659           /* Parse the initializer list.  */
12660           CONSTRUCTOR_ELTS (initializer)
12661             = cp_parser_initializer_list (parser, non_constant_p);
12662           /* A trailing `,' token is allowed.  */
12663           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12664             cp_lexer_consume_token (parser->lexer);
12665         }
12666       /* Now, there should be a trailing `}'.  */
12667       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12668     }
12669
12670   return initializer;
12671 }
12672
12673 /* Parse an initializer-list.
12674
12675    initializer-list:
12676      initializer-clause
12677      initializer-list , initializer-clause
12678
12679    GNU Extension:
12680
12681    initializer-list:
12682      identifier : initializer-clause
12683      initializer-list, identifier : initializer-clause
12684
12685    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12686    for the initializer.  If the INDEX of the elt is non-NULL, it is the
12687    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12688    as for cp_parser_initializer.  */
12689
12690 static VEC(constructor_elt,gc) *
12691 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12692 {
12693   VEC(constructor_elt,gc) *v = NULL;
12694
12695   /* Assume all of the expressions are constant.  */
12696   *non_constant_p = false;
12697
12698   /* Parse the rest of the list.  */
12699   while (true)
12700     {
12701       cp_token *token;
12702       tree identifier;
12703       tree initializer;
12704       bool clause_non_constant_p;
12705
12706       /* If the next token is an identifier and the following one is a
12707          colon, we are looking at the GNU designated-initializer
12708          syntax.  */
12709       if (cp_parser_allow_gnu_extensions_p (parser)
12710           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12711           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12712         {
12713           /* Consume the identifier.  */
12714           identifier = cp_lexer_consume_token (parser->lexer)->value;
12715           /* Consume the `:'.  */
12716           cp_lexer_consume_token (parser->lexer);
12717         }
12718       else
12719         identifier = NULL_TREE;
12720
12721       /* Parse the initializer.  */
12722       initializer = cp_parser_initializer_clause (parser,
12723                                                   &clause_non_constant_p);
12724       /* If any clause is non-constant, so is the entire initializer.  */
12725       if (clause_non_constant_p)
12726         *non_constant_p = true;
12727
12728       /* Add it to the vector.  */
12729       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12730
12731       /* If the next token is not a comma, we have reached the end of
12732          the list.  */
12733       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12734         break;
12735
12736       /* Peek at the next token.  */
12737       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12738       /* If the next token is a `}', then we're still done.  An
12739          initializer-clause can have a trailing `,' after the
12740          initializer-list and before the closing `}'.  */
12741       if (token->type == CPP_CLOSE_BRACE)
12742         break;
12743
12744       /* Consume the `,' token.  */
12745       cp_lexer_consume_token (parser->lexer);
12746     }
12747
12748   return v;
12749 }
12750
12751 /* Classes [gram.class] */
12752
12753 /* Parse a class-name.
12754
12755    class-name:
12756      identifier
12757      template-id
12758
12759    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12760    to indicate that names looked up in dependent types should be
12761    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12762    keyword has been used to indicate that the name that appears next
12763    is a template.  TAG_TYPE indicates the explicit tag given before
12764    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12765    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12766    is the class being defined in a class-head.
12767
12768    Returns the TYPE_DECL representing the class.  */
12769
12770 static tree
12771 cp_parser_class_name (cp_parser *parser,
12772                       bool typename_keyword_p,
12773                       bool template_keyword_p,
12774                       enum tag_types tag_type,
12775                       bool check_dependency_p,
12776                       bool class_head_p,
12777                       bool is_declaration)
12778 {
12779   tree decl;
12780   tree scope;
12781   bool typename_p;
12782   cp_token *token;
12783
12784   /* All class-names start with an identifier.  */
12785   token = cp_lexer_peek_token (parser->lexer);
12786   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12787     {
12788       cp_parser_error (parser, "expected class-name");
12789       return error_mark_node;
12790     }
12791
12792   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12793      to a template-id, so we save it here.  */
12794   scope = parser->scope;
12795   if (scope == error_mark_node)
12796     return error_mark_node;
12797
12798   /* Any name names a type if we're following the `typename' keyword
12799      in a qualified name where the enclosing scope is type-dependent.  */
12800   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12801                 && dependent_type_p (scope));
12802   /* Handle the common case (an identifier, but not a template-id)
12803      efficiently.  */
12804   if (token->type == CPP_NAME
12805       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12806     {
12807       cp_token *identifier_token;
12808       tree identifier;
12809       bool ambiguous_p;
12810
12811       /* Look for the identifier.  */
12812       identifier_token = cp_lexer_peek_token (parser->lexer);
12813       ambiguous_p = identifier_token->ambiguous_p;
12814       identifier = cp_parser_identifier (parser);
12815       /* If the next token isn't an identifier, we are certainly not
12816          looking at a class-name.  */
12817       if (identifier == error_mark_node)
12818         decl = error_mark_node;
12819       /* If we know this is a type-name, there's no need to look it
12820          up.  */
12821       else if (typename_p)
12822         decl = identifier;
12823       else
12824         {
12825           tree ambiguous_decls;
12826           /* If we already know that this lookup is ambiguous, then
12827              we've already issued an error message; there's no reason
12828              to check again.  */
12829           if (ambiguous_p)
12830             {
12831               cp_parser_simulate_error (parser);
12832               return error_mark_node;
12833             }
12834           /* If the next token is a `::', then the name must be a type
12835              name.
12836
12837              [basic.lookup.qual]
12838
12839              During the lookup for a name preceding the :: scope
12840              resolution operator, object, function, and enumerator
12841              names are ignored.  */
12842           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12843             tag_type = typename_type;
12844           /* Look up the name.  */
12845           decl = cp_parser_lookup_name (parser, identifier,
12846                                         tag_type,
12847                                         /*is_template=*/false,
12848                                         /*is_namespace=*/false,
12849                                         check_dependency_p,
12850                                         &ambiguous_decls);
12851           if (ambiguous_decls)
12852             {
12853               error ("reference to %qD is ambiguous", identifier);
12854               print_candidates (ambiguous_decls);
12855               if (cp_parser_parsing_tentatively (parser))
12856                 {
12857                   identifier_token->ambiguous_p = true;
12858                   cp_parser_simulate_error (parser);
12859                 }
12860               return error_mark_node;
12861             }
12862         }
12863     }
12864   else
12865     {
12866       /* Try a template-id.  */
12867       decl = cp_parser_template_id (parser, template_keyword_p,
12868                                     check_dependency_p,
12869                                     is_declaration);
12870       if (decl == error_mark_node)
12871         return error_mark_node;
12872     }
12873
12874   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12875
12876   /* If this is a typename, create a TYPENAME_TYPE.  */
12877   if (typename_p && decl != error_mark_node)
12878     {
12879       decl = make_typename_type (scope, decl, typename_type,
12880                                  /*complain=*/tf_error);
12881       if (decl != error_mark_node)
12882         decl = TYPE_NAME (decl);
12883     }
12884
12885   /* Check to see that it is really the name of a class.  */
12886   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12887       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12888       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12889     /* Situations like this:
12890
12891          template <typename T> struct A {
12892            typename T::template X<int>::I i;
12893          };
12894
12895        are problematic.  Is `T::template X<int>' a class-name?  The
12896        standard does not seem to be definitive, but there is no other
12897        valid interpretation of the following `::'.  Therefore, those
12898        names are considered class-names.  */
12899     {
12900       decl = make_typename_type (scope, decl, tag_type, tf_error);
12901       if (decl != error_mark_node)
12902         decl = TYPE_NAME (decl);
12903     }
12904   else if (TREE_CODE (decl) != TYPE_DECL
12905            || TREE_TYPE (decl) == error_mark_node
12906            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12907     decl = error_mark_node;
12908
12909   if (decl == error_mark_node)
12910     cp_parser_error (parser, "expected class-name");
12911
12912   return decl;
12913 }
12914
12915 /* Parse a class-specifier.
12916
12917    class-specifier:
12918      class-head { member-specification [opt] }
12919
12920    Returns the TREE_TYPE representing the class.  */
12921
12922 static tree
12923 cp_parser_class_specifier (cp_parser* parser)
12924 {
12925   cp_token *token;
12926   tree type;
12927   tree attributes = NULL_TREE;
12928   int has_trailing_semicolon;
12929   bool nested_name_specifier_p;
12930   unsigned saved_num_template_parameter_lists;
12931   tree old_scope = NULL_TREE;
12932   tree scope = NULL_TREE;
12933
12934   push_deferring_access_checks (dk_no_deferred);
12935
12936   /* Parse the class-head.  */
12937   type = cp_parser_class_head (parser,
12938                                &nested_name_specifier_p,
12939                                &attributes);
12940   /* If the class-head was a semantic disaster, skip the entire body
12941      of the class.  */
12942   if (!type)
12943     {
12944       cp_parser_skip_to_end_of_block_or_statement (parser);
12945       pop_deferring_access_checks ();
12946       return error_mark_node;
12947     }
12948
12949   /* Look for the `{'.  */
12950   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12951     {
12952       pop_deferring_access_checks ();
12953       return error_mark_node;
12954     }
12955
12956   /* Issue an error message if type-definitions are forbidden here.  */
12957   cp_parser_check_type_definition (parser);
12958   /* Remember that we are defining one more class.  */
12959   ++parser->num_classes_being_defined;
12960   /* Inside the class, surrounding template-parameter-lists do not
12961      apply.  */
12962   saved_num_template_parameter_lists
12963     = parser->num_template_parameter_lists;
12964   parser->num_template_parameter_lists = 0;
12965
12966   /* Start the class.  */
12967   if (nested_name_specifier_p)
12968     {
12969       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12970       old_scope = push_inner_scope (scope);
12971     }
12972   type = begin_class_definition (type, attributes);
12973
12974   if (type == error_mark_node)
12975     /* If the type is erroneous, skip the entire body of the class.  */
12976     cp_parser_skip_to_closing_brace (parser);
12977   else
12978     /* Parse the member-specification.  */
12979     cp_parser_member_specification_opt (parser);
12980
12981   /* Look for the trailing `}'.  */
12982   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12983   /* We get better error messages by noticing a common problem: a
12984      missing trailing `;'.  */
12985   token = cp_lexer_peek_token (parser->lexer);
12986   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12987   /* Look for trailing attributes to apply to this class.  */
12988   if (cp_parser_allow_gnu_extensions_p (parser))
12989     attributes = cp_parser_attributes_opt (parser);
12990   if (type != error_mark_node)
12991     type = finish_struct (type, attributes);
12992   if (nested_name_specifier_p)
12993     pop_inner_scope (old_scope, scope);
12994   /* If this class is not itself within the scope of another class,
12995      then we need to parse the bodies of all of the queued function
12996      definitions.  Note that the queued functions defined in a class
12997      are not always processed immediately following the
12998      class-specifier for that class.  Consider:
12999
13000        struct A {
13001          struct B { void f() { sizeof (A); } };
13002        };
13003
13004      If `f' were processed before the processing of `A' were
13005      completed, there would be no way to compute the size of `A'.
13006      Note that the nesting we are interested in here is lexical --
13007      not the semantic nesting given by TYPE_CONTEXT.  In particular,
13008      for:
13009
13010        struct A { struct B; };
13011        struct A::B { void f() { } };
13012
13013      there is no need to delay the parsing of `A::B::f'.  */
13014   if (--parser->num_classes_being_defined == 0)
13015     {
13016       tree queue_entry;
13017       tree fn;
13018       tree class_type = NULL_TREE;
13019       tree pushed_scope = NULL_TREE;
13020
13021       /* In a first pass, parse default arguments to the functions.
13022          Then, in a second pass, parse the bodies of the functions.
13023          This two-phased approach handles cases like:
13024
13025             struct S {
13026               void f() { g(); }
13027               void g(int i = 3);
13028             };
13029
13030          */
13031       for (TREE_PURPOSE (parser->unparsed_functions_queues)
13032              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13033            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13034            TREE_PURPOSE (parser->unparsed_functions_queues)
13035              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13036         {
13037           fn = TREE_VALUE (queue_entry);
13038           /* If there are default arguments that have not yet been processed,
13039              take care of them now.  */
13040           if (class_type != TREE_PURPOSE (queue_entry))
13041             {
13042               if (pushed_scope)
13043                 pop_scope (pushed_scope);
13044               class_type = TREE_PURPOSE (queue_entry);
13045               pushed_scope = push_scope (class_type);
13046             }
13047           /* Make sure that any template parameters are in scope.  */
13048           maybe_begin_member_template_processing (fn);
13049           /* Parse the default argument expressions.  */
13050           cp_parser_late_parsing_default_args (parser, fn);
13051           /* Remove any template parameters from the symbol table.  */
13052           maybe_end_member_template_processing ();
13053         }
13054       if (pushed_scope)
13055         pop_scope (pushed_scope);
13056       /* Now parse the body of the functions.  */
13057       for (TREE_VALUE (parser->unparsed_functions_queues)
13058              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13059            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13060            TREE_VALUE (parser->unparsed_functions_queues)
13061              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13062         {
13063           /* Figure out which function we need to process.  */
13064           fn = TREE_VALUE (queue_entry);
13065           /* Parse the function.  */
13066           cp_parser_late_parsing_for_member (parser, fn);
13067         }
13068     }
13069
13070   /* Put back any saved access checks.  */
13071   pop_deferring_access_checks ();
13072
13073   /* Restore the count of active template-parameter-lists.  */
13074   parser->num_template_parameter_lists
13075     = saved_num_template_parameter_lists;
13076
13077   return type;
13078 }
13079
13080 /* Parse a class-head.
13081
13082    class-head:
13083      class-key identifier [opt] base-clause [opt]
13084      class-key nested-name-specifier identifier base-clause [opt]
13085      class-key nested-name-specifier [opt] template-id
13086        base-clause [opt]
13087
13088    GNU Extensions:
13089      class-key attributes identifier [opt] base-clause [opt]
13090      class-key attributes nested-name-specifier identifier base-clause [opt]
13091      class-key attributes nested-name-specifier [opt] template-id
13092        base-clause [opt]
13093
13094    Returns the TYPE of the indicated class.  Sets
13095    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13096    involving a nested-name-specifier was used, and FALSE otherwise.
13097
13098    Returns error_mark_node if this is not a class-head.
13099
13100    Returns NULL_TREE if the class-head is syntactically valid, but
13101    semantically invalid in a way that means we should skip the entire
13102    body of the class.  */
13103
13104 static tree
13105 cp_parser_class_head (cp_parser* parser,
13106                       bool* nested_name_specifier_p,
13107                       tree *attributes_p)
13108 {
13109   tree nested_name_specifier;
13110   enum tag_types class_key;
13111   tree id = NULL_TREE;
13112   tree type = NULL_TREE;
13113   tree attributes;
13114   bool template_id_p = false;
13115   bool qualified_p = false;
13116   bool invalid_nested_name_p = false;
13117   bool invalid_explicit_specialization_p = false;
13118   tree pushed_scope = NULL_TREE;
13119   unsigned num_templates;
13120   tree bases;
13121
13122   /* Assume no nested-name-specifier will be present.  */
13123   *nested_name_specifier_p = false;
13124   /* Assume no template parameter lists will be used in defining the
13125      type.  */
13126   num_templates = 0;
13127
13128   /* Look for the class-key.  */
13129   class_key = cp_parser_class_key (parser);
13130   if (class_key == none_type)
13131     return error_mark_node;
13132
13133   /* Parse the attributes.  */
13134   attributes = cp_parser_attributes_opt (parser);
13135
13136   /* If the next token is `::', that is invalid -- but sometimes
13137      people do try to write:
13138
13139        struct ::S {};
13140
13141      Handle this gracefully by accepting the extra qualifier, and then
13142      issuing an error about it later if this really is a
13143      class-head.  If it turns out just to be an elaborated type
13144      specifier, remain silent.  */
13145   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13146     qualified_p = true;
13147
13148   push_deferring_access_checks (dk_no_check);
13149
13150   /* Determine the name of the class.  Begin by looking for an
13151      optional nested-name-specifier.  */
13152   nested_name_specifier
13153     = cp_parser_nested_name_specifier_opt (parser,
13154                                            /*typename_keyword_p=*/false,
13155                                            /*check_dependency_p=*/false,
13156                                            /*type_p=*/false,
13157                                            /*is_declaration=*/false);
13158   /* If there was a nested-name-specifier, then there *must* be an
13159      identifier.  */
13160   if (nested_name_specifier)
13161     {
13162       /* Although the grammar says `identifier', it really means
13163          `class-name' or `template-name'.  You are only allowed to
13164          define a class that has already been declared with this
13165          syntax.
13166
13167          The proposed resolution for Core Issue 180 says that wherever
13168          you see `class T::X' you should treat `X' as a type-name.
13169
13170          It is OK to define an inaccessible class; for example:
13171
13172            class A { class B; };
13173            class A::B {};
13174
13175          We do not know if we will see a class-name, or a
13176          template-name.  We look for a class-name first, in case the
13177          class-name is a template-id; if we looked for the
13178          template-name first we would stop after the template-name.  */
13179       cp_parser_parse_tentatively (parser);
13180       type = cp_parser_class_name (parser,
13181                                    /*typename_keyword_p=*/false,
13182                                    /*template_keyword_p=*/false,
13183                                    class_type,
13184                                    /*check_dependency_p=*/false,
13185                                    /*class_head_p=*/true,
13186                                    /*is_declaration=*/false);
13187       /* If that didn't work, ignore the nested-name-specifier.  */
13188       if (!cp_parser_parse_definitely (parser))
13189         {
13190           invalid_nested_name_p = true;
13191           id = cp_parser_identifier (parser);
13192           if (id == error_mark_node)
13193             id = NULL_TREE;
13194         }
13195       /* If we could not find a corresponding TYPE, treat this
13196          declaration like an unqualified declaration.  */
13197       if (type == error_mark_node)
13198         nested_name_specifier = NULL_TREE;
13199       /* Otherwise, count the number of templates used in TYPE and its
13200          containing scopes.  */
13201       else
13202         {
13203           tree scope;
13204
13205           for (scope = TREE_TYPE (type);
13206                scope && TREE_CODE (scope) != NAMESPACE_DECL;
13207                scope = (TYPE_P (scope)
13208                         ? TYPE_CONTEXT (scope)
13209                         : DECL_CONTEXT (scope)))
13210             if (TYPE_P (scope)
13211                 && CLASS_TYPE_P (scope)
13212                 && CLASSTYPE_TEMPLATE_INFO (scope)
13213                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13214                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13215               ++num_templates;
13216         }
13217     }
13218   /* Otherwise, the identifier is optional.  */
13219   else
13220     {
13221       /* We don't know whether what comes next is a template-id,
13222          an identifier, or nothing at all.  */
13223       cp_parser_parse_tentatively (parser);
13224       /* Check for a template-id.  */
13225       id = cp_parser_template_id (parser,
13226                                   /*template_keyword_p=*/false,
13227                                   /*check_dependency_p=*/true,
13228                                   /*is_declaration=*/true);
13229       /* If that didn't work, it could still be an identifier.  */
13230       if (!cp_parser_parse_definitely (parser))
13231         {
13232           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13233             id = cp_parser_identifier (parser);
13234           else
13235             id = NULL_TREE;
13236         }
13237       else
13238         {
13239           template_id_p = true;
13240           ++num_templates;
13241         }
13242     }
13243
13244   pop_deferring_access_checks ();
13245
13246   if (id)
13247     cp_parser_check_for_invalid_template_id (parser, id);
13248
13249   /* If it's not a `:' or a `{' then we can't really be looking at a
13250      class-head, since a class-head only appears as part of a
13251      class-specifier.  We have to detect this situation before calling
13252      xref_tag, since that has irreversible side-effects.  */
13253   if (!cp_parser_next_token_starts_class_definition_p (parser))
13254     {
13255       cp_parser_error (parser, "expected %<{%> or %<:%>");
13256       return error_mark_node;
13257     }
13258
13259   /* At this point, we're going ahead with the class-specifier, even
13260      if some other problem occurs.  */
13261   cp_parser_commit_to_tentative_parse (parser);
13262   /* Issue the error about the overly-qualified name now.  */
13263   if (qualified_p)
13264     cp_parser_error (parser,
13265                      "global qualification of class name is invalid");
13266   else if (invalid_nested_name_p)
13267     cp_parser_error (parser,
13268                      "qualified name does not name a class");
13269   else if (nested_name_specifier)
13270     {
13271       tree scope;
13272
13273       /* Reject typedef-names in class heads.  */
13274       if (!DECL_IMPLICIT_TYPEDEF_P (type))
13275         {
13276           error ("invalid class name in declaration of %qD", type);
13277           type = NULL_TREE;
13278           goto done;
13279         }
13280
13281       /* Figure out in what scope the declaration is being placed.  */
13282       scope = current_scope ();
13283       /* If that scope does not contain the scope in which the
13284          class was originally declared, the program is invalid.  */
13285       if (scope && !is_ancestor (scope, nested_name_specifier))
13286         {
13287           error ("declaration of %qD in %qD which does not enclose %qD",
13288                  type, scope, nested_name_specifier);
13289           type = NULL_TREE;
13290           goto done;
13291         }
13292       /* [dcl.meaning]
13293
13294          A declarator-id shall not be qualified exception of the
13295          definition of a ... nested class outside of its class
13296          ... [or] a the definition or explicit instantiation of a
13297          class member of a namespace outside of its namespace.  */
13298       if (scope == nested_name_specifier)
13299         {
13300           pedwarn ("extra qualification ignored");
13301           nested_name_specifier = NULL_TREE;
13302           num_templates = 0;
13303         }
13304     }
13305   /* An explicit-specialization must be preceded by "template <>".  If
13306      it is not, try to recover gracefully.  */
13307   if (at_namespace_scope_p ()
13308       && parser->num_template_parameter_lists == 0
13309       && template_id_p)
13310     {
13311       error ("an explicit specialization must be preceded by %<template <>%>");
13312       invalid_explicit_specialization_p = true;
13313       /* Take the same action that would have been taken by
13314          cp_parser_explicit_specialization.  */
13315       ++parser->num_template_parameter_lists;
13316       begin_specialization ();
13317     }
13318   /* There must be no "return" statements between this point and the
13319      end of this function; set "type "to the correct return value and
13320      use "goto done;" to return.  */
13321   /* Make sure that the right number of template parameters were
13322      present.  */
13323   if (!cp_parser_check_template_parameters (parser, num_templates))
13324     {
13325       /* If something went wrong, there is no point in even trying to
13326          process the class-definition.  */
13327       type = NULL_TREE;
13328       goto done;
13329     }
13330
13331   /* Look up the type.  */
13332   if (template_id_p)
13333     {
13334       type = TREE_TYPE (id);
13335       maybe_process_partial_specialization (type);
13336       if (nested_name_specifier)
13337         pushed_scope = push_scope (nested_name_specifier);
13338     }
13339   else if (nested_name_specifier)
13340     {
13341       tree class_type;
13342
13343       /* Given:
13344
13345             template <typename T> struct S { struct T };
13346             template <typename T> struct S<T>::T { };
13347
13348          we will get a TYPENAME_TYPE when processing the definition of
13349          `S::T'.  We need to resolve it to the actual type before we
13350          try to define it.  */
13351       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13352         {
13353           class_type = resolve_typename_type (TREE_TYPE (type),
13354                                               /*only_current_p=*/false);
13355           if (class_type != error_mark_node)
13356             type = TYPE_NAME (class_type);
13357           else
13358             {
13359               cp_parser_error (parser, "could not resolve typename type");
13360               type = error_mark_node;
13361             }
13362         }
13363
13364       maybe_process_partial_specialization (TREE_TYPE (type));
13365       class_type = current_class_type;
13366       /* Enter the scope indicated by the nested-name-specifier.  */
13367       pushed_scope = push_scope (nested_name_specifier);
13368       /* Get the canonical version of this type.  */
13369       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13370       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13371           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13372         {
13373           type = push_template_decl (type);
13374           if (type == error_mark_node)
13375             {
13376               type = NULL_TREE;
13377               goto done;
13378             }
13379         }
13380
13381       type = TREE_TYPE (type);
13382       *nested_name_specifier_p = true;
13383     }
13384   else      /* The name is not a nested name.  */
13385     {
13386       /* If the class was unnamed, create a dummy name.  */
13387       if (!id)
13388         id = make_anon_name ();
13389       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13390                        parser->num_template_parameter_lists);
13391     }
13392
13393   /* Indicate whether this class was declared as a `class' or as a
13394      `struct'.  */
13395   if (TREE_CODE (type) == RECORD_TYPE)
13396     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13397   cp_parser_check_class_key (class_key, type);
13398
13399   /* If this type was already complete, and we see another definition,
13400      that's an error.  */
13401   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13402     {
13403       error ("redefinition of %q#T", type);
13404       error ("previous definition of %q+#T", type);
13405       type = NULL_TREE;
13406       goto done;
13407     }
13408
13409   /* We will have entered the scope containing the class; the names of
13410      base classes should be looked up in that context.  For example:
13411
13412        struct A { struct B {}; struct C; };
13413        struct A::C : B {};
13414
13415      is valid.  */
13416   bases = NULL_TREE;
13417
13418   /* Get the list of base-classes, if there is one.  */
13419   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13420     bases = cp_parser_base_clause (parser);
13421
13422   /* Process the base classes.  */
13423   xref_basetypes (type, bases);
13424
13425  done:
13426   /* Leave the scope given by the nested-name-specifier.  We will
13427      enter the class scope itself while processing the members.  */
13428   if (pushed_scope)
13429     pop_scope (pushed_scope);
13430
13431   if (invalid_explicit_specialization_p)
13432     {
13433       end_specialization ();
13434       --parser->num_template_parameter_lists;
13435     }
13436   *attributes_p = attributes;
13437   return type;
13438 }
13439
13440 /* Parse a class-key.
13441
13442    class-key:
13443      class
13444      struct
13445      union
13446
13447    Returns the kind of class-key specified, or none_type to indicate
13448    error.  */
13449
13450 static enum tag_types
13451 cp_parser_class_key (cp_parser* parser)
13452 {
13453   cp_token *token;
13454   enum tag_types tag_type;
13455
13456   /* Look for the class-key.  */
13457   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13458   if (!token)
13459     return none_type;
13460
13461   /* Check to see if the TOKEN is a class-key.  */
13462   tag_type = cp_parser_token_is_class_key (token);
13463   if (!tag_type)
13464     cp_parser_error (parser, "expected class-key");
13465   return tag_type;
13466 }
13467
13468 /* Parse an (optional) member-specification.
13469
13470    member-specification:
13471      member-declaration member-specification [opt]
13472      access-specifier : member-specification [opt]  */
13473
13474 static void
13475 cp_parser_member_specification_opt (cp_parser* parser)
13476 {
13477   while (true)
13478     {
13479       cp_token *token;
13480       enum rid keyword;
13481
13482       /* Peek at the next token.  */
13483       token = cp_lexer_peek_token (parser->lexer);
13484       /* If it's a `}', or EOF then we've seen all the members.  */
13485       if (token->type == CPP_CLOSE_BRACE
13486           || token->type == CPP_EOF
13487           || token->type == CPP_PRAGMA_EOL)
13488         break;
13489
13490       /* See if this token is a keyword.  */
13491       keyword = token->keyword;
13492       switch (keyword)
13493         {
13494         case RID_PUBLIC:
13495         case RID_PROTECTED:
13496         case RID_PRIVATE:
13497           /* Consume the access-specifier.  */
13498           cp_lexer_consume_token (parser->lexer);
13499           /* Remember which access-specifier is active.  */
13500           current_access_specifier = token->value;
13501           /* Look for the `:'.  */
13502           cp_parser_require (parser, CPP_COLON, "`:'");
13503           break;
13504
13505         default:
13506           /* Accept #pragmas at class scope.  */
13507           if (token->type == CPP_PRAGMA)
13508             {
13509               cp_parser_pragma (parser, pragma_external);
13510               break;
13511             }
13512
13513           /* Otherwise, the next construction must be a
13514              member-declaration.  */
13515           cp_parser_member_declaration (parser);
13516         }
13517     }
13518 }
13519
13520 /* Parse a member-declaration.
13521
13522    member-declaration:
13523      decl-specifier-seq [opt] member-declarator-list [opt] ;
13524      function-definition ; [opt]
13525      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13526      using-declaration
13527      template-declaration
13528
13529    member-declarator-list:
13530      member-declarator
13531      member-declarator-list , member-declarator
13532
13533    member-declarator:
13534      declarator pure-specifier [opt]
13535      declarator constant-initializer [opt]
13536      identifier [opt] : constant-expression
13537
13538    GNU Extensions:
13539
13540    member-declaration:
13541      __extension__ member-declaration
13542
13543    member-declarator:
13544      declarator attributes [opt] pure-specifier [opt]
13545      declarator attributes [opt] constant-initializer [opt]
13546      identifier [opt] attributes [opt] : constant-expression  */
13547
13548 static void
13549 cp_parser_member_declaration (cp_parser* parser)
13550 {
13551   cp_decl_specifier_seq decl_specifiers;
13552   tree prefix_attributes;
13553   tree decl;
13554   int declares_class_or_enum;
13555   bool friend_p;
13556   cp_token *token;
13557   int saved_pedantic;
13558
13559   /* Check for the `__extension__' keyword.  */
13560   if (cp_parser_extension_opt (parser, &saved_pedantic))
13561     {
13562       /* Recurse.  */
13563       cp_parser_member_declaration (parser);
13564       /* Restore the old value of the PEDANTIC flag.  */
13565       pedantic = saved_pedantic;
13566
13567       return;
13568     }
13569
13570   /* Check for a template-declaration.  */
13571   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13572     {
13573       /* An explicit specialization here is an error condition, and we
13574          expect the specialization handler to detect and report this.  */
13575       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13576           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13577         cp_parser_explicit_specialization (parser);
13578       else
13579         cp_parser_template_declaration (parser, /*member_p=*/true);
13580
13581       return;
13582     }
13583
13584   /* Check for a using-declaration.  */
13585   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13586     {
13587       /* Parse the using-declaration.  */
13588       cp_parser_using_declaration (parser);
13589
13590       return;
13591     }
13592
13593   /* Check for @defs.  */
13594   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13595     {
13596       tree ivar, member;
13597       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13598       ivar = ivar_chains;
13599       while (ivar)
13600         {
13601           member = ivar;
13602           ivar = TREE_CHAIN (member);
13603           TREE_CHAIN (member) = NULL_TREE;
13604           finish_member_declaration (member);
13605         }
13606       return;
13607     }
13608
13609   /* Parse the decl-specifier-seq.  */
13610   cp_parser_decl_specifier_seq (parser,
13611                                 CP_PARSER_FLAGS_OPTIONAL,
13612                                 &decl_specifiers,
13613                                 &declares_class_or_enum);
13614   prefix_attributes = decl_specifiers.attributes;
13615   decl_specifiers.attributes = NULL_TREE;
13616   /* Check for an invalid type-name.  */
13617   if (!decl_specifiers.type
13618       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13619     return;
13620   /* If there is no declarator, then the decl-specifier-seq should
13621      specify a type.  */
13622   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13623     {
13624       /* If there was no decl-specifier-seq, and the next token is a
13625          `;', then we have something like:
13626
13627            struct S { ; };
13628
13629          [class.mem]
13630
13631          Each member-declaration shall declare at least one member
13632          name of the class.  */
13633       if (!decl_specifiers.any_specifiers_p)
13634         {
13635           cp_token *token = cp_lexer_peek_token (parser->lexer);
13636           if (pedantic && !token->in_system_header)
13637             pedwarn ("%Hextra %<;%>", &token->location);
13638         }
13639       else
13640         {
13641           tree type;
13642
13643           /* See if this declaration is a friend.  */
13644           friend_p = cp_parser_friend_p (&decl_specifiers);
13645           /* If there were decl-specifiers, check to see if there was
13646              a class-declaration.  */
13647           type = check_tag_decl (&decl_specifiers);
13648           /* Nested classes have already been added to the class, but
13649              a `friend' needs to be explicitly registered.  */
13650           if (friend_p)
13651             {
13652               /* If the `friend' keyword was present, the friend must
13653                  be introduced with a class-key.  */
13654                if (!declares_class_or_enum)
13655                  error ("a class-key must be used when declaring a friend");
13656                /* In this case:
13657
13658                     template <typename T> struct A {
13659                       friend struct A<T>::B;
13660                     };
13661
13662                   A<T>::B will be represented by a TYPENAME_TYPE, and
13663                   therefore not recognized by check_tag_decl.  */
13664                if (!type
13665                    && decl_specifiers.type
13666                    && TYPE_P (decl_specifiers.type))
13667                  type = decl_specifiers.type;
13668                if (!type || !TYPE_P (type))
13669                  error ("friend declaration does not name a class or "
13670                         "function");
13671                else
13672                  make_friend_class (current_class_type, type,
13673                                     /*complain=*/true);
13674             }
13675           /* If there is no TYPE, an error message will already have
13676              been issued.  */
13677           else if (!type || type == error_mark_node)
13678             ;
13679           /* An anonymous aggregate has to be handled specially; such
13680              a declaration really declares a data member (with a
13681              particular type), as opposed to a nested class.  */
13682           else if (ANON_AGGR_TYPE_P (type))
13683             {
13684               /* Remove constructors and such from TYPE, now that we
13685                  know it is an anonymous aggregate.  */
13686               fixup_anonymous_aggr (type);
13687               /* And make the corresponding data member.  */
13688               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13689               /* Add it to the class.  */
13690               finish_member_declaration (decl);
13691             }
13692           else
13693             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13694         }
13695     }
13696   else
13697     {
13698       /* See if these declarations will be friends.  */
13699       friend_p = cp_parser_friend_p (&decl_specifiers);
13700
13701       /* Keep going until we hit the `;' at the end of the
13702          declaration.  */
13703       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13704         {
13705           tree attributes = NULL_TREE;
13706           tree first_attribute;
13707
13708           /* Peek at the next token.  */
13709           token = cp_lexer_peek_token (parser->lexer);
13710
13711           /* Check for a bitfield declaration.  */
13712           if (token->type == CPP_COLON
13713               || (token->type == CPP_NAME
13714                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13715                   == CPP_COLON))
13716             {
13717               tree identifier;
13718               tree width;
13719
13720               /* Get the name of the bitfield.  Note that we cannot just
13721                  check TOKEN here because it may have been invalidated by
13722                  the call to cp_lexer_peek_nth_token above.  */
13723               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13724                 identifier = cp_parser_identifier (parser);
13725               else
13726                 identifier = NULL_TREE;
13727
13728               /* Consume the `:' token.  */
13729               cp_lexer_consume_token (parser->lexer);
13730               /* Get the width of the bitfield.  */
13731               width
13732                 = cp_parser_constant_expression (parser,
13733                                                  /*allow_non_constant=*/false,
13734                                                  NULL);
13735
13736               /* Look for attributes that apply to the bitfield.  */
13737               attributes = cp_parser_attributes_opt (parser);
13738               /* Remember which attributes are prefix attributes and
13739                  which are not.  */
13740               first_attribute = attributes;
13741               /* Combine the attributes.  */
13742               attributes = chainon (prefix_attributes, attributes);
13743
13744               /* Create the bitfield declaration.  */
13745               decl = grokbitfield (identifier
13746                                    ? make_id_declarator (NULL_TREE,
13747                                                          identifier,
13748                                                          sfk_none)
13749                                    : NULL,
13750                                    &decl_specifiers,
13751                                    width);
13752               /* Apply the attributes.  */
13753               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13754             }
13755           else
13756             {
13757               cp_declarator *declarator;
13758               tree initializer;
13759               tree asm_specification;
13760               int ctor_dtor_or_conv_p;
13761
13762               /* Parse the declarator.  */
13763               declarator
13764                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13765                                         &ctor_dtor_or_conv_p,
13766                                         /*parenthesized_p=*/NULL,
13767                                         /*member_p=*/true);
13768
13769               /* If something went wrong parsing the declarator, make sure
13770                  that we at least consume some tokens.  */
13771               if (declarator == cp_error_declarator)
13772                 {
13773                   /* Skip to the end of the statement.  */
13774                   cp_parser_skip_to_end_of_statement (parser);
13775                   /* If the next token is not a semicolon, that is
13776                      probably because we just skipped over the body of
13777                      a function.  So, we consume a semicolon if
13778                      present, but do not issue an error message if it
13779                      is not present.  */
13780                   if (cp_lexer_next_token_is (parser->lexer,
13781                                               CPP_SEMICOLON))
13782                     cp_lexer_consume_token (parser->lexer);
13783                   return;
13784                 }
13785
13786               if (declares_class_or_enum & 2)
13787                 cp_parser_check_for_definition_in_return_type
13788                   (declarator, decl_specifiers.type);
13789
13790               /* Look for an asm-specification.  */
13791               asm_specification = cp_parser_asm_specification_opt (parser);
13792               /* Look for attributes that apply to the declaration.  */
13793               attributes = cp_parser_attributes_opt (parser);
13794               /* Remember which attributes are prefix attributes and
13795                  which are not.  */
13796               first_attribute = attributes;
13797               /* Combine the attributes.  */
13798               attributes = chainon (prefix_attributes, attributes);
13799
13800               /* If it's an `=', then we have a constant-initializer or a
13801                  pure-specifier.  It is not correct to parse the
13802                  initializer before registering the member declaration
13803                  since the member declaration should be in scope while
13804                  its initializer is processed.  However, the rest of the
13805                  front end does not yet provide an interface that allows
13806                  us to handle this correctly.  */
13807               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13808                 {
13809                   /* In [class.mem]:
13810
13811                      A pure-specifier shall be used only in the declaration of
13812                      a virtual function.
13813
13814                      A member-declarator can contain a constant-initializer
13815                      only if it declares a static member of integral or
13816                      enumeration type.
13817
13818                      Therefore, if the DECLARATOR is for a function, we look
13819                      for a pure-specifier; otherwise, we look for a
13820                      constant-initializer.  When we call `grokfield', it will
13821                      perform more stringent semantics checks.  */
13822                   if (declarator->kind == cdk_function
13823                       && declarator->declarator->kind == cdk_id)
13824                     initializer = cp_parser_pure_specifier (parser);
13825                   else
13826                     /* Parse the initializer.  */
13827                     initializer = cp_parser_constant_initializer (parser);
13828                 }
13829               /* Otherwise, there is no initializer.  */
13830               else
13831                 initializer = NULL_TREE;
13832
13833               /* See if we are probably looking at a function
13834                  definition.  We are certainly not looking at a
13835                  member-declarator.  Calling `grokfield' has
13836                  side-effects, so we must not do it unless we are sure
13837                  that we are looking at a member-declarator.  */
13838               if (cp_parser_token_starts_function_definition_p
13839                   (cp_lexer_peek_token (parser->lexer)))
13840                 {
13841                   /* The grammar does not allow a pure-specifier to be
13842                      used when a member function is defined.  (It is
13843                      possible that this fact is an oversight in the
13844                      standard, since a pure function may be defined
13845                      outside of the class-specifier.  */
13846                   if (initializer)
13847                     error ("pure-specifier on function-definition");
13848                   decl = cp_parser_save_member_function_body (parser,
13849                                                               &decl_specifiers,
13850                                                               declarator,
13851                                                               attributes);
13852                   /* If the member was not a friend, declare it here.  */
13853                   if (!friend_p)
13854                     finish_member_declaration (decl);
13855                   /* Peek at the next token.  */
13856                   token = cp_lexer_peek_token (parser->lexer);
13857                   /* If the next token is a semicolon, consume it.  */
13858                   if (token->type == CPP_SEMICOLON)
13859                     cp_lexer_consume_token (parser->lexer);
13860                   return;
13861                 }
13862               else
13863                 /* Create the declaration.  */
13864                 decl = grokfield (declarator, &decl_specifiers,
13865                                   initializer, /*init_const_expr_p=*/true,
13866                                   asm_specification,
13867                                   attributes);
13868             }
13869
13870           /* Reset PREFIX_ATTRIBUTES.  */
13871           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13872             attributes = TREE_CHAIN (attributes);
13873           if (attributes)
13874             TREE_CHAIN (attributes) = NULL_TREE;
13875
13876           /* If there is any qualification still in effect, clear it
13877              now; we will be starting fresh with the next declarator.  */
13878           parser->scope = NULL_TREE;
13879           parser->qualifying_scope = NULL_TREE;
13880           parser->object_scope = NULL_TREE;
13881           /* If it's a `,', then there are more declarators.  */
13882           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13883             cp_lexer_consume_token (parser->lexer);
13884           /* If the next token isn't a `;', then we have a parse error.  */
13885           else if (cp_lexer_next_token_is_not (parser->lexer,
13886                                                CPP_SEMICOLON))
13887             {
13888               cp_parser_error (parser, "expected %<;%>");
13889               /* Skip tokens until we find a `;'.  */
13890               cp_parser_skip_to_end_of_statement (parser);
13891
13892               break;
13893             }
13894
13895           if (decl)
13896             {
13897               /* Add DECL to the list of members.  */
13898               if (!friend_p)
13899                 finish_member_declaration (decl);
13900
13901               if (TREE_CODE (decl) == FUNCTION_DECL)
13902                 cp_parser_save_default_args (parser, decl);
13903             }
13904         }
13905     }
13906
13907   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13908 }
13909
13910 /* Parse a pure-specifier.
13911
13912    pure-specifier:
13913      = 0
13914
13915    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13916    Otherwise, ERROR_MARK_NODE is returned.  */
13917
13918 static tree
13919 cp_parser_pure_specifier (cp_parser* parser)
13920 {
13921   cp_token *token;
13922
13923   /* Look for the `=' token.  */
13924   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13925     return error_mark_node;
13926   /* Look for the `0' token.  */
13927   token = cp_lexer_consume_token (parser->lexer);
13928   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
13929   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
13930     {
13931       cp_parser_error (parser,
13932                        "invalid pure specifier (only `= 0' is allowed)");
13933       cp_parser_skip_to_end_of_statement (parser);
13934       return error_mark_node;
13935     }
13936   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
13937     {
13938       error ("templates may not be %<virtual%>");
13939       return error_mark_node;
13940     }
13941
13942   return integer_zero_node;
13943 }
13944
13945 /* Parse a constant-initializer.
13946
13947    constant-initializer:
13948      = constant-expression
13949
13950    Returns a representation of the constant-expression.  */
13951
13952 static tree
13953 cp_parser_constant_initializer (cp_parser* parser)
13954 {
13955   /* Look for the `=' token.  */
13956   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13957     return error_mark_node;
13958
13959   /* It is invalid to write:
13960
13961        struct S { static const int i = { 7 }; };
13962
13963      */
13964   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13965     {
13966       cp_parser_error (parser,
13967                        "a brace-enclosed initializer is not allowed here");
13968       /* Consume the opening brace.  */
13969       cp_lexer_consume_token (parser->lexer);
13970       /* Skip the initializer.  */
13971       cp_parser_skip_to_closing_brace (parser);
13972       /* Look for the trailing `}'.  */
13973       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13974
13975       return error_mark_node;
13976     }
13977
13978   return cp_parser_constant_expression (parser,
13979                                         /*allow_non_constant=*/false,
13980                                         NULL);
13981 }
13982
13983 /* Derived classes [gram.class.derived] */
13984
13985 /* Parse a base-clause.
13986
13987    base-clause:
13988      : base-specifier-list
13989
13990    base-specifier-list:
13991      base-specifier
13992      base-specifier-list , base-specifier
13993
13994    Returns a TREE_LIST representing the base-classes, in the order in
13995    which they were declared.  The representation of each node is as
13996    described by cp_parser_base_specifier.
13997
13998    In the case that no bases are specified, this function will return
13999    NULL_TREE, not ERROR_MARK_NODE.  */
14000
14001 static tree
14002 cp_parser_base_clause (cp_parser* parser)
14003 {
14004   tree bases = NULL_TREE;
14005
14006   /* Look for the `:' that begins the list.  */
14007   cp_parser_require (parser, CPP_COLON, "`:'");
14008
14009   /* Scan the base-specifier-list.  */
14010   while (true)
14011     {
14012       cp_token *token;
14013       tree base;
14014
14015       /* Look for the base-specifier.  */
14016       base = cp_parser_base_specifier (parser);
14017       /* Add BASE to the front of the list.  */
14018       if (base != error_mark_node)
14019         {
14020           TREE_CHAIN (base) = bases;
14021           bases = base;
14022         }
14023       /* Peek at the next token.  */
14024       token = cp_lexer_peek_token (parser->lexer);
14025       /* If it's not a comma, then the list is complete.  */
14026       if (token->type != CPP_COMMA)
14027         break;
14028       /* Consume the `,'.  */
14029       cp_lexer_consume_token (parser->lexer);
14030     }
14031
14032   /* PARSER->SCOPE may still be non-NULL at this point, if the last
14033      base class had a qualified name.  However, the next name that
14034      appears is certainly not qualified.  */
14035   parser->scope = NULL_TREE;
14036   parser->qualifying_scope = NULL_TREE;
14037   parser->object_scope = NULL_TREE;
14038
14039   return nreverse (bases);
14040 }
14041
14042 /* Parse a base-specifier.
14043
14044    base-specifier:
14045      :: [opt] nested-name-specifier [opt] class-name
14046      virtual access-specifier [opt] :: [opt] nested-name-specifier
14047        [opt] class-name
14048      access-specifier virtual [opt] :: [opt] nested-name-specifier
14049        [opt] class-name
14050
14051    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
14052    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14053    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
14054    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
14055
14056 static tree
14057 cp_parser_base_specifier (cp_parser* parser)
14058 {
14059   cp_token *token;
14060   bool done = false;
14061   bool virtual_p = false;
14062   bool duplicate_virtual_error_issued_p = false;
14063   bool duplicate_access_error_issued_p = false;
14064   bool class_scope_p, template_p;
14065   tree access = access_default_node;
14066   tree type;
14067
14068   /* Process the optional `virtual' and `access-specifier'.  */
14069   while (!done)
14070     {
14071       /* Peek at the next token.  */
14072       token = cp_lexer_peek_token (parser->lexer);
14073       /* Process `virtual'.  */
14074       switch (token->keyword)
14075         {
14076         case RID_VIRTUAL:
14077           /* If `virtual' appears more than once, issue an error.  */
14078           if (virtual_p && !duplicate_virtual_error_issued_p)
14079             {
14080               cp_parser_error (parser,
14081                                "%<virtual%> specified more than once in base-specified");
14082               duplicate_virtual_error_issued_p = true;
14083             }
14084
14085           virtual_p = true;
14086
14087           /* Consume the `virtual' token.  */
14088           cp_lexer_consume_token (parser->lexer);
14089
14090           break;
14091
14092         case RID_PUBLIC:
14093         case RID_PROTECTED:
14094         case RID_PRIVATE:
14095           /* If more than one access specifier appears, issue an
14096              error.  */
14097           if (access != access_default_node
14098               && !duplicate_access_error_issued_p)
14099             {
14100               cp_parser_error (parser,
14101                                "more than one access specifier in base-specified");
14102               duplicate_access_error_issued_p = true;
14103             }
14104
14105           access = ridpointers[(int) token->keyword];
14106
14107           /* Consume the access-specifier.  */
14108           cp_lexer_consume_token (parser->lexer);
14109
14110           break;
14111
14112         default:
14113           done = true;
14114           break;
14115         }
14116     }
14117   /* It is not uncommon to see programs mechanically, erroneously, use
14118      the 'typename' keyword to denote (dependent) qualified types
14119      as base classes.  */
14120   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14121     {
14122       if (!processing_template_decl)
14123         error ("keyword %<typename%> not allowed outside of templates");
14124       else
14125         error ("keyword %<typename%> not allowed in this context "
14126                "(the base class is implicitly a type)");
14127       cp_lexer_consume_token (parser->lexer);
14128     }
14129
14130   /* Look for the optional `::' operator.  */
14131   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14132   /* Look for the nested-name-specifier.  The simplest way to
14133      implement:
14134
14135        [temp.res]
14136
14137        The keyword `typename' is not permitted in a base-specifier or
14138        mem-initializer; in these contexts a qualified name that
14139        depends on a template-parameter is implicitly assumed to be a
14140        type name.
14141
14142      is to pretend that we have seen the `typename' keyword at this
14143      point.  */
14144   cp_parser_nested_name_specifier_opt (parser,
14145                                        /*typename_keyword_p=*/true,
14146                                        /*check_dependency_p=*/true,
14147                                        typename_type,
14148                                        /*is_declaration=*/true);
14149   /* If the base class is given by a qualified name, assume that names
14150      we see are type names or templates, as appropriate.  */
14151   class_scope_p = (parser->scope && TYPE_P (parser->scope));
14152   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14153
14154   /* Finally, look for the class-name.  */
14155   type = cp_parser_class_name (parser,
14156                                class_scope_p,
14157                                template_p,
14158                                typename_type,
14159                                /*check_dependency_p=*/true,
14160                                /*class_head_p=*/false,
14161                                /*is_declaration=*/true);
14162
14163   if (type == error_mark_node)
14164     return error_mark_node;
14165
14166   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14167 }
14168
14169 /* Exception handling [gram.exception] */
14170
14171 /* Parse an (optional) exception-specification.
14172
14173    exception-specification:
14174      throw ( type-id-list [opt] )
14175
14176    Returns a TREE_LIST representing the exception-specification.  The
14177    TREE_VALUE of each node is a type.  */
14178
14179 static tree
14180 cp_parser_exception_specification_opt (cp_parser* parser)
14181 {
14182   cp_token *token;
14183   tree type_id_list;
14184
14185   /* Peek at the next token.  */
14186   token = cp_lexer_peek_token (parser->lexer);
14187   /* If it's not `throw', then there's no exception-specification.  */
14188   if (!cp_parser_is_keyword (token, RID_THROW))
14189     return NULL_TREE;
14190
14191   /* Consume the `throw'.  */
14192   cp_lexer_consume_token (parser->lexer);
14193
14194   /* Look for the `('.  */
14195   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14196
14197   /* Peek at the next token.  */
14198   token = cp_lexer_peek_token (parser->lexer);
14199   /* If it's not a `)', then there is a type-id-list.  */
14200   if (token->type != CPP_CLOSE_PAREN)
14201     {
14202       const char *saved_message;
14203
14204       /* Types may not be defined in an exception-specification.  */
14205       saved_message = parser->type_definition_forbidden_message;
14206       parser->type_definition_forbidden_message
14207         = "types may not be defined in an exception-specification";
14208       /* Parse the type-id-list.  */
14209       type_id_list = cp_parser_type_id_list (parser);
14210       /* Restore the saved message.  */
14211       parser->type_definition_forbidden_message = saved_message;
14212     }
14213   else
14214     type_id_list = empty_except_spec;
14215
14216   /* Look for the `)'.  */
14217   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14218
14219   return type_id_list;
14220 }
14221
14222 /* Parse an (optional) type-id-list.
14223
14224    type-id-list:
14225      type-id
14226      type-id-list , type-id
14227
14228    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14229    in the order that the types were presented.  */
14230
14231 static tree
14232 cp_parser_type_id_list (cp_parser* parser)
14233 {
14234   tree types = NULL_TREE;
14235
14236   while (true)
14237     {
14238       cp_token *token;
14239       tree type;
14240
14241       /* Get the next type-id.  */
14242       type = cp_parser_type_id (parser);
14243       /* Add it to the list.  */
14244       types = add_exception_specifier (types, type, /*complain=*/1);
14245       /* Peek at the next token.  */
14246       token = cp_lexer_peek_token (parser->lexer);
14247       /* If it is not a `,', we are done.  */
14248       if (token->type != CPP_COMMA)
14249         break;
14250       /* Consume the `,'.  */
14251       cp_lexer_consume_token (parser->lexer);
14252     }
14253
14254   return nreverse (types);
14255 }
14256
14257 /* Parse a try-block.
14258
14259    try-block:
14260      try compound-statement handler-seq  */
14261
14262 static tree
14263 cp_parser_try_block (cp_parser* parser)
14264 {
14265   tree try_block;
14266
14267   cp_parser_require_keyword (parser, RID_TRY, "`try'");
14268   try_block = begin_try_block ();
14269   cp_parser_compound_statement (parser, NULL, true);
14270   finish_try_block (try_block);
14271   cp_parser_handler_seq (parser);
14272   finish_handler_sequence (try_block);
14273
14274   return try_block;
14275 }
14276
14277 /* Parse a function-try-block.
14278
14279    function-try-block:
14280      try ctor-initializer [opt] function-body handler-seq  */
14281
14282 static bool
14283 cp_parser_function_try_block (cp_parser* parser)
14284 {
14285   tree compound_stmt;
14286   tree try_block;
14287   bool ctor_initializer_p;
14288
14289   /* Look for the `try' keyword.  */
14290   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14291     return false;
14292   /* Let the rest of the front-end know where we are.  */
14293   try_block = begin_function_try_block (&compound_stmt);
14294   /* Parse the function-body.  */
14295   ctor_initializer_p
14296     = cp_parser_ctor_initializer_opt_and_function_body (parser);
14297   /* We're done with the `try' part.  */
14298   finish_function_try_block (try_block);
14299   /* Parse the handlers.  */
14300   cp_parser_handler_seq (parser);
14301   /* We're done with the handlers.  */
14302   finish_function_handler_sequence (try_block, compound_stmt);
14303
14304   return ctor_initializer_p;
14305 }
14306
14307 /* Parse a handler-seq.
14308
14309    handler-seq:
14310      handler handler-seq [opt]  */
14311
14312 static void
14313 cp_parser_handler_seq (cp_parser* parser)
14314 {
14315   while (true)
14316     {
14317       cp_token *token;
14318
14319       /* Parse the handler.  */
14320       cp_parser_handler (parser);
14321       /* Peek at the next token.  */
14322       token = cp_lexer_peek_token (parser->lexer);
14323       /* If it's not `catch' then there are no more handlers.  */
14324       if (!cp_parser_is_keyword (token, RID_CATCH))
14325         break;
14326     }
14327 }
14328
14329 /* Parse a handler.
14330
14331    handler:
14332      catch ( exception-declaration ) compound-statement  */
14333
14334 static void
14335 cp_parser_handler (cp_parser* parser)
14336 {
14337   tree handler;
14338   tree declaration;
14339
14340   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14341   handler = begin_handler ();
14342   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14343   declaration = cp_parser_exception_declaration (parser);
14344   finish_handler_parms (declaration, handler);
14345   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14346   cp_parser_compound_statement (parser, NULL, false);
14347   finish_handler (handler);
14348 }
14349
14350 /* Parse an exception-declaration.
14351
14352    exception-declaration:
14353      type-specifier-seq declarator
14354      type-specifier-seq abstract-declarator
14355      type-specifier-seq
14356      ...
14357
14358    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14359    ellipsis variant is used.  */
14360
14361 static tree
14362 cp_parser_exception_declaration (cp_parser* parser)
14363 {
14364   cp_decl_specifier_seq type_specifiers;
14365   cp_declarator *declarator;
14366   const char *saved_message;
14367
14368   /* If it's an ellipsis, it's easy to handle.  */
14369   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14370     {
14371       /* Consume the `...' token.  */
14372       cp_lexer_consume_token (parser->lexer);
14373       return NULL_TREE;
14374     }
14375
14376   /* Types may not be defined in exception-declarations.  */
14377   saved_message = parser->type_definition_forbidden_message;
14378   parser->type_definition_forbidden_message
14379     = "types may not be defined in exception-declarations";
14380
14381   /* Parse the type-specifier-seq.  */
14382   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14383                                 &type_specifiers);
14384   /* If it's a `)', then there is no declarator.  */
14385   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14386     declarator = NULL;
14387   else
14388     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14389                                        /*ctor_dtor_or_conv_p=*/NULL,
14390                                        /*parenthesized_p=*/NULL,
14391                                        /*member_p=*/false);
14392
14393   /* Restore the saved message.  */
14394   parser->type_definition_forbidden_message = saved_message;
14395
14396   if (!type_specifiers.any_specifiers_p)
14397     return error_mark_node;
14398
14399   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14400 }
14401
14402 /* Parse a throw-expression.
14403
14404    throw-expression:
14405      throw assignment-expression [opt]
14406
14407    Returns a THROW_EXPR representing the throw-expression.  */
14408
14409 static tree
14410 cp_parser_throw_expression (cp_parser* parser)
14411 {
14412   tree expression;
14413   cp_token* token;
14414
14415   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14416   token = cp_lexer_peek_token (parser->lexer);
14417   /* Figure out whether or not there is an assignment-expression
14418      following the "throw" keyword.  */
14419   if (token->type == CPP_COMMA
14420       || token->type == CPP_SEMICOLON
14421       || token->type == CPP_CLOSE_PAREN
14422       || token->type == CPP_CLOSE_SQUARE
14423       || token->type == CPP_CLOSE_BRACE
14424       || token->type == CPP_COLON)
14425     expression = NULL_TREE;
14426   else
14427     expression = cp_parser_assignment_expression (parser,
14428                                                   /*cast_p=*/false);
14429
14430   return build_throw (expression);
14431 }
14432
14433 /* GNU Extensions */
14434
14435 /* Parse an (optional) asm-specification.
14436
14437    asm-specification:
14438      asm ( string-literal )
14439
14440    If the asm-specification is present, returns a STRING_CST
14441    corresponding to the string-literal.  Otherwise, returns
14442    NULL_TREE.  */
14443
14444 static tree
14445 cp_parser_asm_specification_opt (cp_parser* parser)
14446 {
14447   cp_token *token;
14448   tree asm_specification;
14449
14450   /* Peek at the next token.  */
14451   token = cp_lexer_peek_token (parser->lexer);
14452   /* If the next token isn't the `asm' keyword, then there's no
14453      asm-specification.  */
14454   if (!cp_parser_is_keyword (token, RID_ASM))
14455     return NULL_TREE;
14456
14457   /* Consume the `asm' token.  */
14458   cp_lexer_consume_token (parser->lexer);
14459   /* Look for the `('.  */
14460   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14461
14462   /* Look for the string-literal.  */
14463   asm_specification = cp_parser_string_literal (parser, false, false);
14464
14465   /* Look for the `)'.  */
14466   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14467
14468   return asm_specification;
14469 }
14470
14471 /* Parse an asm-operand-list.
14472
14473    asm-operand-list:
14474      asm-operand
14475      asm-operand-list , asm-operand
14476
14477    asm-operand:
14478      string-literal ( expression )
14479      [ string-literal ] string-literal ( expression )
14480
14481    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14482    each node is the expression.  The TREE_PURPOSE is itself a
14483    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14484    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14485    is a STRING_CST for the string literal before the parenthesis.  */
14486
14487 static tree
14488 cp_parser_asm_operand_list (cp_parser* parser)
14489 {
14490   tree asm_operands = NULL_TREE;
14491
14492   while (true)
14493     {
14494       tree string_literal;
14495       tree expression;
14496       tree name;
14497
14498       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14499         {
14500           /* Consume the `[' token.  */
14501           cp_lexer_consume_token (parser->lexer);
14502           /* Read the operand name.  */
14503           name = cp_parser_identifier (parser);
14504           if (name != error_mark_node)
14505             name = build_string (IDENTIFIER_LENGTH (name),
14506                                  IDENTIFIER_POINTER (name));
14507           /* Look for the closing `]'.  */
14508           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14509         }
14510       else
14511         name = NULL_TREE;
14512       /* Look for the string-literal.  */
14513       string_literal = cp_parser_string_literal (parser, false, false);
14514
14515       /* Look for the `('.  */
14516       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14517       /* Parse the expression.  */
14518       expression = cp_parser_expression (parser, /*cast_p=*/false);
14519       /* Look for the `)'.  */
14520       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14521
14522       /* Add this operand to the list.  */
14523       asm_operands = tree_cons (build_tree_list (name, string_literal),
14524                                 expression,
14525                                 asm_operands);
14526       /* If the next token is not a `,', there are no more
14527          operands.  */
14528       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14529         break;
14530       /* Consume the `,'.  */
14531       cp_lexer_consume_token (parser->lexer);
14532     }
14533
14534   return nreverse (asm_operands);
14535 }
14536
14537 /* Parse an asm-clobber-list.
14538
14539    asm-clobber-list:
14540      string-literal
14541      asm-clobber-list , string-literal
14542
14543    Returns a TREE_LIST, indicating the clobbers in the order that they
14544    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14545
14546 static tree
14547 cp_parser_asm_clobber_list (cp_parser* parser)
14548 {
14549   tree clobbers = NULL_TREE;
14550
14551   while (true)
14552     {
14553       tree string_literal;
14554
14555       /* Look for the string literal.  */
14556       string_literal = cp_parser_string_literal (parser, false, false);
14557       /* Add it to the list.  */
14558       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14559       /* If the next token is not a `,', then the list is
14560          complete.  */
14561       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14562         break;
14563       /* Consume the `,' token.  */
14564       cp_lexer_consume_token (parser->lexer);
14565     }
14566
14567   return clobbers;
14568 }
14569
14570 /* Parse an (optional) series of attributes.
14571
14572    attributes:
14573      attributes attribute
14574
14575    attribute:
14576      __attribute__ (( attribute-list [opt] ))
14577
14578    The return value is as for cp_parser_attribute_list.  */
14579
14580 static tree
14581 cp_parser_attributes_opt (cp_parser* parser)
14582 {
14583   tree attributes = NULL_TREE;
14584
14585   while (true)
14586     {
14587       cp_token *token;
14588       tree attribute_list;
14589
14590       /* Peek at the next token.  */
14591       token = cp_lexer_peek_token (parser->lexer);
14592       /* If it's not `__attribute__', then we're done.  */
14593       if (token->keyword != RID_ATTRIBUTE)
14594         break;
14595
14596       /* Consume the `__attribute__' keyword.  */
14597       cp_lexer_consume_token (parser->lexer);
14598       /* Look for the two `(' tokens.  */
14599       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14600       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14601
14602       /* Peek at the next token.  */
14603       token = cp_lexer_peek_token (parser->lexer);
14604       if (token->type != CPP_CLOSE_PAREN)
14605         /* Parse the attribute-list.  */
14606         attribute_list = cp_parser_attribute_list (parser);
14607       else
14608         /* If the next token is a `)', then there is no attribute
14609            list.  */
14610         attribute_list = NULL;
14611
14612       /* Look for the two `)' tokens.  */
14613       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14614       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14615
14616       /* Add these new attributes to the list.  */
14617       attributes = chainon (attributes, attribute_list);
14618     }
14619
14620   return attributes;
14621 }
14622
14623 /* Parse an attribute-list.
14624
14625    attribute-list:
14626      attribute
14627      attribute-list , attribute
14628
14629    attribute:
14630      identifier
14631      identifier ( identifier )
14632      identifier ( identifier , expression-list )
14633      identifier ( expression-list )
14634
14635    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14636    to an attribute.  The TREE_PURPOSE of each node is the identifier
14637    indicating which attribute is in use.  The TREE_VALUE represents
14638    the arguments, if any.  */
14639
14640 static tree
14641 cp_parser_attribute_list (cp_parser* parser)
14642 {
14643   tree attribute_list = NULL_TREE;
14644   bool save_translate_strings_p = parser->translate_strings_p;
14645
14646   parser->translate_strings_p = false;
14647   while (true)
14648     {
14649       cp_token *token;
14650       tree identifier;
14651       tree attribute;
14652
14653       /* Look for the identifier.  We also allow keywords here; for
14654          example `__attribute__ ((const))' is legal.  */
14655       token = cp_lexer_peek_token (parser->lexer);
14656       if (token->type == CPP_NAME
14657           || token->type == CPP_KEYWORD)
14658         {
14659           tree arguments = NULL_TREE;
14660
14661           /* Consume the token.  */
14662           token = cp_lexer_consume_token (parser->lexer);
14663
14664           /* Save away the identifier that indicates which attribute
14665              this is.  */
14666           identifier = token->value;
14667           attribute = build_tree_list (identifier, NULL_TREE);
14668
14669           /* Peek at the next token.  */
14670           token = cp_lexer_peek_token (parser->lexer);
14671           /* If it's an `(', then parse the attribute arguments.  */
14672           if (token->type == CPP_OPEN_PAREN)
14673             {
14674               arguments = cp_parser_parenthesized_expression_list
14675                           (parser, true, /*cast_p=*/false,
14676                            /*non_constant_p=*/NULL);
14677               /* Save the arguments away.  */
14678               TREE_VALUE (attribute) = arguments;
14679             }
14680
14681           if (arguments != error_mark_node)
14682             {
14683               /* Add this attribute to the list.  */
14684               TREE_CHAIN (attribute) = attribute_list;
14685               attribute_list = attribute;
14686             }
14687
14688           token = cp_lexer_peek_token (parser->lexer);
14689         }
14690       /* Now, look for more attributes.  If the next token isn't a
14691          `,', we're done.  */
14692       if (token->type != CPP_COMMA)
14693         break;
14694
14695       /* Consume the comma and keep going.  */
14696       cp_lexer_consume_token (parser->lexer);
14697     }
14698   parser->translate_strings_p = save_translate_strings_p;
14699
14700   /* We built up the list in reverse order.  */
14701   return nreverse (attribute_list);
14702 }
14703
14704 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14705    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14706    current value of the PEDANTIC flag, regardless of whether or not
14707    the `__extension__' keyword is present.  The caller is responsible
14708    for restoring the value of the PEDANTIC flag.  */
14709
14710 static bool
14711 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14712 {
14713   /* Save the old value of the PEDANTIC flag.  */
14714   *saved_pedantic = pedantic;
14715
14716   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14717     {
14718       /* Consume the `__extension__' token.  */
14719       cp_lexer_consume_token (parser->lexer);
14720       /* We're not being pedantic while the `__extension__' keyword is
14721          in effect.  */
14722       pedantic = 0;
14723
14724       return true;
14725     }
14726
14727   return false;
14728 }
14729
14730 /* Parse a label declaration.
14731
14732    label-declaration:
14733      __label__ label-declarator-seq ;
14734
14735    label-declarator-seq:
14736      identifier , label-declarator-seq
14737      identifier  */
14738
14739 static void
14740 cp_parser_label_declaration (cp_parser* parser)
14741 {
14742   /* Look for the `__label__' keyword.  */
14743   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14744
14745   while (true)
14746     {
14747       tree identifier;
14748
14749       /* Look for an identifier.  */
14750       identifier = cp_parser_identifier (parser);
14751       /* If we failed, stop.  */
14752       if (identifier == error_mark_node)
14753         break;
14754       /* Declare it as a label.  */
14755       finish_label_decl (identifier);
14756       /* If the next token is a `;', stop.  */
14757       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14758         break;
14759       /* Look for the `,' separating the label declarations.  */
14760       cp_parser_require (parser, CPP_COMMA, "`,'");
14761     }
14762
14763   /* Look for the final `;'.  */
14764   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14765 }
14766
14767 /* Support Functions */
14768
14769 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14770    NAME should have one of the representations used for an
14771    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14772    is returned.  If PARSER->SCOPE is a dependent type, then a
14773    SCOPE_REF is returned.
14774
14775    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14776    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14777    was formed.  Abstractly, such entities should not be passed to this
14778    function, because they do not need to be looked up, but it is
14779    simpler to check for this special case here, rather than at the
14780    call-sites.
14781
14782    In cases not explicitly covered above, this function returns a
14783    DECL, OVERLOAD, or baselink representing the result of the lookup.
14784    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14785    is returned.
14786
14787    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14788    (e.g., "struct") that was used.  In that case bindings that do not
14789    refer to types are ignored.
14790
14791    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14792    ignored.
14793
14794    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14795    are ignored.
14796
14797    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14798    types.
14799
14800    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14801    TREE_LIST of candidates if name-lookup results in an ambiguity, and
14802    NULL_TREE otherwise.  */
14803
14804 static tree
14805 cp_parser_lookup_name (cp_parser *parser, tree name,
14806                        enum tag_types tag_type,
14807                        bool is_template,
14808                        bool is_namespace,
14809                        bool check_dependency,
14810                        tree *ambiguous_decls)
14811 {
14812   int flags = 0;
14813   tree decl;
14814   tree object_type = parser->context->object_type;
14815
14816   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14817     flags |= LOOKUP_COMPLAIN;
14818
14819   /* Assume that the lookup will be unambiguous.  */
14820   if (ambiguous_decls)
14821     *ambiguous_decls = NULL_TREE;
14822
14823   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14824      no longer valid.  Note that if we are parsing tentatively, and
14825      the parse fails, OBJECT_TYPE will be automatically restored.  */
14826   parser->context->object_type = NULL_TREE;
14827
14828   if (name == error_mark_node)
14829     return error_mark_node;
14830
14831   /* A template-id has already been resolved; there is no lookup to
14832      do.  */
14833   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14834     return name;
14835   if (BASELINK_P (name))
14836     {
14837       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14838                   == TEMPLATE_ID_EXPR);
14839       return name;
14840     }
14841
14842   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14843      it should already have been checked to make sure that the name
14844      used matches the type being destroyed.  */
14845   if (TREE_CODE (name) == BIT_NOT_EXPR)
14846     {
14847       tree type;
14848
14849       /* Figure out to which type this destructor applies.  */
14850       if (parser->scope)
14851         type = parser->scope;
14852       else if (object_type)
14853         type = object_type;
14854       else
14855         type = current_class_type;
14856       /* If that's not a class type, there is no destructor.  */
14857       if (!type || !CLASS_TYPE_P (type))
14858         return error_mark_node;
14859       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14860         lazily_declare_fn (sfk_destructor, type);
14861       if (!CLASSTYPE_DESTRUCTORS (type))
14862           return error_mark_node;
14863       /* If it was a class type, return the destructor.  */
14864       return CLASSTYPE_DESTRUCTORS (type);
14865     }
14866
14867   /* By this point, the NAME should be an ordinary identifier.  If
14868      the id-expression was a qualified name, the qualifying scope is
14869      stored in PARSER->SCOPE at this point.  */
14870   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14871
14872   /* Perform the lookup.  */
14873   if (parser->scope)
14874     {
14875       bool dependent_p;
14876
14877       if (parser->scope == error_mark_node)
14878         return error_mark_node;
14879
14880       /* If the SCOPE is dependent, the lookup must be deferred until
14881          the template is instantiated -- unless we are explicitly
14882          looking up names in uninstantiated templates.  Even then, we
14883          cannot look up the name if the scope is not a class type; it
14884          might, for example, be a template type parameter.  */
14885       dependent_p = (TYPE_P (parser->scope)
14886                      && !(parser->in_declarator_p
14887                           && currently_open_class (parser->scope))
14888                      && dependent_type_p (parser->scope));
14889       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14890            && dependent_p)
14891         {
14892           if (tag_type)
14893             {
14894               tree type;
14895
14896               /* The resolution to Core Issue 180 says that `struct
14897                  A::B' should be considered a type-name, even if `A'
14898                  is dependent.  */
14899               type = make_typename_type (parser->scope, name, tag_type,
14900                                          /*complain=*/tf_error);
14901               decl = TYPE_NAME (type);
14902             }
14903           else if (is_template
14904                    && (cp_parser_next_token_ends_template_argument_p (parser)
14905                        || cp_lexer_next_token_is (parser->lexer,
14906                                                   CPP_CLOSE_PAREN)))
14907             decl = make_unbound_class_template (parser->scope,
14908                                                 name, NULL_TREE,
14909                                                 /*complain=*/tf_error);
14910           else
14911             decl = build_qualified_name (/*type=*/NULL_TREE,
14912                                          parser->scope, name,
14913                                          is_template);
14914         }
14915       else
14916         {
14917           tree pushed_scope = NULL_TREE;
14918
14919           /* If PARSER->SCOPE is a dependent type, then it must be a
14920              class type, and we must not be checking dependencies;
14921              otherwise, we would have processed this lookup above.  So
14922              that PARSER->SCOPE is not considered a dependent base by
14923              lookup_member, we must enter the scope here.  */
14924           if (dependent_p)
14925             pushed_scope = push_scope (parser->scope);
14926           /* If the PARSER->SCOPE is a template specialization, it
14927              may be instantiated during name lookup.  In that case,
14928              errors may be issued.  Even if we rollback the current
14929              tentative parse, those errors are valid.  */
14930           decl = lookup_qualified_name (parser->scope, name,
14931                                         tag_type != none_type,
14932                                         /*complain=*/true);
14933           if (pushed_scope)
14934             pop_scope (pushed_scope);
14935         }
14936       parser->qualifying_scope = parser->scope;
14937       parser->object_scope = NULL_TREE;
14938     }
14939   else if (object_type)
14940     {
14941       tree object_decl = NULL_TREE;
14942       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14943          OBJECT_TYPE is not a class.  */
14944       if (CLASS_TYPE_P (object_type))
14945         /* If the OBJECT_TYPE is a template specialization, it may
14946            be instantiated during name lookup.  In that case, errors
14947            may be issued.  Even if we rollback the current tentative
14948            parse, those errors are valid.  */
14949         object_decl = lookup_member (object_type,
14950                                      name,
14951                                      /*protect=*/0,
14952                                      tag_type != none_type);
14953       /* Look it up in the enclosing context, too.  */
14954       decl = lookup_name_real (name, tag_type != none_type,
14955                                /*nonclass=*/0,
14956                                /*block_p=*/true, is_namespace, flags);
14957       parser->object_scope = object_type;
14958       parser->qualifying_scope = NULL_TREE;
14959       if (object_decl)
14960         decl = object_decl;
14961     }
14962   else
14963     {
14964       decl = lookup_name_real (name, tag_type != none_type,
14965                                /*nonclass=*/0,
14966                                /*block_p=*/true, is_namespace, flags);
14967       parser->qualifying_scope = NULL_TREE;
14968       parser->object_scope = NULL_TREE;
14969     }
14970
14971   /* If the lookup failed, let our caller know.  */
14972   if (!decl || decl == error_mark_node)
14973     return error_mark_node;
14974
14975   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14976   if (TREE_CODE (decl) == TREE_LIST)
14977     {
14978       if (ambiguous_decls)
14979         *ambiguous_decls = decl;
14980       /* The error message we have to print is too complicated for
14981          cp_parser_error, so we incorporate its actions directly.  */
14982       if (!cp_parser_simulate_error (parser))
14983         {
14984           error ("reference to %qD is ambiguous", name);
14985           print_candidates (decl);
14986         }
14987       return error_mark_node;
14988     }
14989
14990   gcc_assert (DECL_P (decl)
14991               || TREE_CODE (decl) == OVERLOAD
14992               || TREE_CODE (decl) == SCOPE_REF
14993               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14994               || BASELINK_P (decl));
14995
14996   /* If we have resolved the name of a member declaration, check to
14997      see if the declaration is accessible.  When the name resolves to
14998      set of overloaded functions, accessibility is checked when
14999      overload resolution is done.
15000
15001      During an explicit instantiation, access is not checked at all,
15002      as per [temp.explicit].  */
15003   if (DECL_P (decl))
15004     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15005
15006   return decl;
15007 }
15008
15009 /* Like cp_parser_lookup_name, but for use in the typical case where
15010    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15011    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
15012
15013 static tree
15014 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15015 {
15016   return cp_parser_lookup_name (parser, name,
15017                                 none_type,
15018                                 /*is_template=*/false,
15019                                 /*is_namespace=*/false,
15020                                 /*check_dependency=*/true,
15021                                 /*ambiguous_decls=*/NULL);
15022 }
15023
15024 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15025    the current context, return the TYPE_DECL.  If TAG_NAME_P is
15026    true, the DECL indicates the class being defined in a class-head,
15027    or declared in an elaborated-type-specifier.
15028
15029    Otherwise, return DECL.  */
15030
15031 static tree
15032 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15033 {
15034   /* If the TEMPLATE_DECL is being declared as part of a class-head,
15035      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15036
15037        struct A {
15038          template <typename T> struct B;
15039        };
15040
15041        template <typename T> struct A::B {};
15042
15043      Similarly, in an elaborated-type-specifier:
15044
15045        namespace N { struct X{}; }
15046
15047        struct A {
15048          template <typename T> friend struct N::X;
15049        };
15050
15051      However, if the DECL refers to a class type, and we are in
15052      the scope of the class, then the name lookup automatically
15053      finds the TYPE_DECL created by build_self_reference rather
15054      than a TEMPLATE_DECL.  For example, in:
15055
15056        template <class T> struct S {
15057          S s;
15058        };
15059
15060      there is no need to handle such case.  */
15061
15062   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15063     return DECL_TEMPLATE_RESULT (decl);
15064
15065   return decl;
15066 }
15067
15068 /* If too many, or too few, template-parameter lists apply to the
15069    declarator, issue an error message.  Returns TRUE if all went well,
15070    and FALSE otherwise.  */
15071
15072 static bool
15073 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15074                                                 cp_declarator *declarator)
15075 {
15076   unsigned num_templates;
15077
15078   /* We haven't seen any classes that involve template parameters yet.  */
15079   num_templates = 0;
15080
15081   switch (declarator->kind)
15082     {
15083     case cdk_id:
15084       if (declarator->u.id.qualifying_scope)
15085         {
15086           tree scope;
15087           tree member;
15088
15089           scope = declarator->u.id.qualifying_scope;
15090           member = declarator->u.id.unqualified_name;
15091
15092           while (scope && CLASS_TYPE_P (scope))
15093             {
15094               /* You're supposed to have one `template <...>'
15095                  for every template class, but you don't need one
15096                  for a full specialization.  For example:
15097
15098                  template <class T> struct S{};
15099                  template <> struct S<int> { void f(); };
15100                  void S<int>::f () {}
15101
15102                  is correct; there shouldn't be a `template <>' for
15103                  the definition of `S<int>::f'.  */
15104               if (CLASSTYPE_TEMPLATE_INFO (scope)
15105                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
15106                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
15107                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15108                 ++num_templates;
15109
15110               scope = TYPE_CONTEXT (scope);
15111             }
15112         }
15113       else if (TREE_CODE (declarator->u.id.unqualified_name)
15114                == TEMPLATE_ID_EXPR)
15115         /* If the DECLARATOR has the form `X<y>' then it uses one
15116            additional level of template parameters.  */
15117         ++num_templates;
15118
15119       return cp_parser_check_template_parameters (parser,
15120                                                   num_templates);
15121
15122     case cdk_function:
15123     case cdk_array:
15124     case cdk_pointer:
15125     case cdk_reference:
15126     case cdk_ptrmem:
15127       return (cp_parser_check_declarator_template_parameters
15128               (parser, declarator->declarator));
15129
15130     case cdk_error:
15131       return true;
15132
15133     default:
15134       gcc_unreachable ();
15135     }
15136   return false;
15137 }
15138
15139 /* NUM_TEMPLATES were used in the current declaration.  If that is
15140    invalid, return FALSE and issue an error messages.  Otherwise,
15141    return TRUE.  */
15142
15143 static bool
15144 cp_parser_check_template_parameters (cp_parser* parser,
15145                                      unsigned num_templates)
15146 {
15147   /* If there are more template classes than parameter lists, we have
15148      something like:
15149
15150        template <class T> void S<T>::R<T>::f ();  */
15151   if (parser->num_template_parameter_lists < num_templates)
15152     {
15153       error ("too few template-parameter-lists");
15154       return false;
15155     }
15156   /* If there are the same number of template classes and parameter
15157      lists, that's OK.  */
15158   if (parser->num_template_parameter_lists == num_templates)
15159     return true;
15160   /* If there are more, but only one more, then we are referring to a
15161      member template.  That's OK too.  */
15162   if (parser->num_template_parameter_lists == num_templates + 1)
15163       return true;
15164   /* Otherwise, there are too many template parameter lists.  We have
15165      something like:
15166
15167      template <class T> template <class U> void S::f();  */
15168   error ("too many template-parameter-lists");
15169   return false;
15170 }
15171
15172 /* Parse an optional `::' token indicating that the following name is
15173    from the global namespace.  If so, PARSER->SCOPE is set to the
15174    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15175    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15176    Returns the new value of PARSER->SCOPE, if the `::' token is
15177    present, and NULL_TREE otherwise.  */
15178
15179 static tree
15180 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15181 {
15182   cp_token *token;
15183
15184   /* Peek at the next token.  */
15185   token = cp_lexer_peek_token (parser->lexer);
15186   /* If we're looking at a `::' token then we're starting from the
15187      global namespace, not our current location.  */
15188   if (token->type == CPP_SCOPE)
15189     {
15190       /* Consume the `::' token.  */
15191       cp_lexer_consume_token (parser->lexer);
15192       /* Set the SCOPE so that we know where to start the lookup.  */
15193       parser->scope = global_namespace;
15194       parser->qualifying_scope = global_namespace;
15195       parser->object_scope = NULL_TREE;
15196
15197       return parser->scope;
15198     }
15199   else if (!current_scope_valid_p)
15200     {
15201       parser->scope = NULL_TREE;
15202       parser->qualifying_scope = NULL_TREE;
15203       parser->object_scope = NULL_TREE;
15204     }
15205
15206   return NULL_TREE;
15207 }
15208
15209 /* Returns TRUE if the upcoming token sequence is the start of a
15210    constructor declarator.  If FRIEND_P is true, the declarator is
15211    preceded by the `friend' specifier.  */
15212
15213 static bool
15214 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15215 {
15216   bool constructor_p;
15217   tree type_decl = NULL_TREE;
15218   bool nested_name_p;
15219   cp_token *next_token;
15220
15221   /* The common case is that this is not a constructor declarator, so
15222      try to avoid doing lots of work if at all possible.  It's not
15223      valid declare a constructor at function scope.  */
15224   if (at_function_scope_p ())
15225     return false;
15226   /* And only certain tokens can begin a constructor declarator.  */
15227   next_token = cp_lexer_peek_token (parser->lexer);
15228   if (next_token->type != CPP_NAME
15229       && next_token->type != CPP_SCOPE
15230       && next_token->type != CPP_NESTED_NAME_SPECIFIER
15231       && next_token->type != CPP_TEMPLATE_ID)
15232     return false;
15233
15234   /* Parse tentatively; we are going to roll back all of the tokens
15235      consumed here.  */
15236   cp_parser_parse_tentatively (parser);
15237   /* Assume that we are looking at a constructor declarator.  */
15238   constructor_p = true;
15239
15240   /* Look for the optional `::' operator.  */
15241   cp_parser_global_scope_opt (parser,
15242                               /*current_scope_valid_p=*/false);
15243   /* Look for the nested-name-specifier.  */
15244   nested_name_p
15245     = (cp_parser_nested_name_specifier_opt (parser,
15246                                             /*typename_keyword_p=*/false,
15247                                             /*check_dependency_p=*/false,
15248                                             /*type_p=*/false,
15249                                             /*is_declaration=*/false)
15250        != NULL_TREE);
15251   /* Outside of a class-specifier, there must be a
15252      nested-name-specifier.  */
15253   if (!nested_name_p &&
15254       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15255        || friend_p))
15256     constructor_p = false;
15257   /* If we still think that this might be a constructor-declarator,
15258      look for a class-name.  */
15259   if (constructor_p)
15260     {
15261       /* If we have:
15262
15263            template <typename T> struct S { S(); };
15264            template <typename T> S<T>::S ();
15265
15266          we must recognize that the nested `S' names a class.
15267          Similarly, for:
15268
15269            template <typename T> S<T>::S<T> ();
15270
15271          we must recognize that the nested `S' names a template.  */
15272       type_decl = cp_parser_class_name (parser,
15273                                         /*typename_keyword_p=*/false,
15274                                         /*template_keyword_p=*/false,
15275                                         none_type,
15276                                         /*check_dependency_p=*/false,
15277                                         /*class_head_p=*/false,
15278                                         /*is_declaration=*/false);
15279       /* If there was no class-name, then this is not a constructor.  */
15280       constructor_p = !cp_parser_error_occurred (parser);
15281     }
15282
15283   /* If we're still considering a constructor, we have to see a `(',
15284      to begin the parameter-declaration-clause, followed by either a
15285      `)', an `...', or a decl-specifier.  We need to check for a
15286      type-specifier to avoid being fooled into thinking that:
15287
15288        S::S (f) (int);
15289
15290      is a constructor.  (It is actually a function named `f' that
15291      takes one parameter (of type `int') and returns a value of type
15292      `S::S'.  */
15293   if (constructor_p
15294       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15295     {
15296       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15297           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15298           /* A parameter declaration begins with a decl-specifier,
15299              which is either the "attribute" keyword, a storage class
15300              specifier, or (usually) a type-specifier.  */
15301           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15302           && !cp_parser_storage_class_specifier_opt (parser))
15303         {
15304           tree type;
15305           tree pushed_scope = NULL_TREE;
15306           unsigned saved_num_template_parameter_lists;
15307
15308           /* Names appearing in the type-specifier should be looked up
15309              in the scope of the class.  */
15310           if (current_class_type)
15311             type = NULL_TREE;
15312           else
15313             {
15314               type = TREE_TYPE (type_decl);
15315               if (TREE_CODE (type) == TYPENAME_TYPE)
15316                 {
15317                   type = resolve_typename_type (type,
15318                                                 /*only_current_p=*/false);
15319                   if (type == error_mark_node)
15320                     {
15321                       cp_parser_abort_tentative_parse (parser);
15322                       return false;
15323                     }
15324                 }
15325               pushed_scope = push_scope (type);
15326             }
15327
15328           /* Inside the constructor parameter list, surrounding
15329              template-parameter-lists do not apply.  */
15330           saved_num_template_parameter_lists
15331             = parser->num_template_parameter_lists;
15332           parser->num_template_parameter_lists = 0;
15333
15334           /* Look for the type-specifier.  */
15335           cp_parser_type_specifier (parser,
15336                                     CP_PARSER_FLAGS_NONE,
15337                                     /*decl_specs=*/NULL,
15338                                     /*is_declarator=*/true,
15339                                     /*declares_class_or_enum=*/NULL,
15340                                     /*is_cv_qualifier=*/NULL);
15341
15342           parser->num_template_parameter_lists
15343             = saved_num_template_parameter_lists;
15344
15345           /* Leave the scope of the class.  */
15346           if (pushed_scope)
15347             pop_scope (pushed_scope);
15348
15349           constructor_p = !cp_parser_error_occurred (parser);
15350         }
15351     }
15352   else
15353     constructor_p = false;
15354   /* We did not really want to consume any tokens.  */
15355   cp_parser_abort_tentative_parse (parser);
15356
15357   return constructor_p;
15358 }
15359
15360 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15361    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15362    they must be performed once we are in the scope of the function.
15363
15364    Returns the function defined.  */
15365
15366 static tree
15367 cp_parser_function_definition_from_specifiers_and_declarator
15368   (cp_parser* parser,
15369    cp_decl_specifier_seq *decl_specifiers,
15370    tree attributes,
15371    const cp_declarator *declarator)
15372 {
15373   tree fn;
15374   bool success_p;
15375
15376   /* Begin the function-definition.  */
15377   success_p = start_function (decl_specifiers, declarator, attributes);
15378
15379   /* The things we're about to see are not directly qualified by any
15380      template headers we've seen thus far.  */
15381   reset_specialization ();
15382
15383   /* If there were names looked up in the decl-specifier-seq that we
15384      did not check, check them now.  We must wait until we are in the
15385      scope of the function to perform the checks, since the function
15386      might be a friend.  */
15387   perform_deferred_access_checks ();
15388
15389   if (!success_p)
15390     {
15391       /* Skip the entire function.  */
15392       cp_parser_skip_to_end_of_block_or_statement (parser);
15393       fn = error_mark_node;
15394     }
15395   else
15396     fn = cp_parser_function_definition_after_declarator (parser,
15397                                                          /*inline_p=*/false);
15398
15399   return fn;
15400 }
15401
15402 /* Parse the part of a function-definition that follows the
15403    declarator.  INLINE_P is TRUE iff this function is an inline
15404    function defined with a class-specifier.
15405
15406    Returns the function defined.  */
15407
15408 static tree
15409 cp_parser_function_definition_after_declarator (cp_parser* parser,
15410                                                 bool inline_p)
15411 {
15412   tree fn;
15413   bool ctor_initializer_p = false;
15414   bool saved_in_unbraced_linkage_specification_p;
15415   unsigned saved_num_template_parameter_lists;
15416
15417   /* If the next token is `return', then the code may be trying to
15418      make use of the "named return value" extension that G++ used to
15419      support.  */
15420   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15421     {
15422       /* Consume the `return' keyword.  */
15423       cp_lexer_consume_token (parser->lexer);
15424       /* Look for the identifier that indicates what value is to be
15425          returned.  */
15426       cp_parser_identifier (parser);
15427       /* Issue an error message.  */
15428       error ("named return values are no longer supported");
15429       /* Skip tokens until we reach the start of the function body.  */
15430       while (true)
15431         {
15432           cp_token *token = cp_lexer_peek_token (parser->lexer);
15433           if (token->type == CPP_OPEN_BRACE
15434               || token->type == CPP_EOF
15435               || token->type == CPP_PRAGMA_EOL)
15436             break;
15437           cp_lexer_consume_token (parser->lexer);
15438         }
15439     }
15440   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15441      anything declared inside `f'.  */
15442   saved_in_unbraced_linkage_specification_p
15443     = parser->in_unbraced_linkage_specification_p;
15444   parser->in_unbraced_linkage_specification_p = false;
15445   /* Inside the function, surrounding template-parameter-lists do not
15446      apply.  */
15447   saved_num_template_parameter_lists
15448     = parser->num_template_parameter_lists;
15449   parser->num_template_parameter_lists = 0;
15450   /* If the next token is `try', then we are looking at a
15451      function-try-block.  */
15452   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15453     ctor_initializer_p = cp_parser_function_try_block (parser);
15454   /* A function-try-block includes the function-body, so we only do
15455      this next part if we're not processing a function-try-block.  */
15456   else
15457     ctor_initializer_p
15458       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15459
15460   /* Finish the function.  */
15461   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15462                         (inline_p ? 2 : 0));
15463   /* Generate code for it, if necessary.  */
15464   expand_or_defer_fn (fn);
15465   /* Restore the saved values.  */
15466   parser->in_unbraced_linkage_specification_p
15467     = saved_in_unbraced_linkage_specification_p;
15468   parser->num_template_parameter_lists
15469     = saved_num_template_parameter_lists;
15470
15471   return fn;
15472 }
15473
15474 /* Parse a template-declaration, assuming that the `export' (and
15475    `extern') keywords, if present, has already been scanned.  MEMBER_P
15476    is as for cp_parser_template_declaration.  */
15477
15478 static void
15479 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15480 {
15481   tree decl = NULL_TREE;
15482   tree checks;
15483   tree parameter_list;
15484   bool friend_p = false;
15485   bool need_lang_pop;
15486
15487   /* Look for the `template' keyword.  */
15488   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15489     return;
15490
15491   /* And the `<'.  */
15492   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15493     return;
15494   /* [temp]
15495
15496      A template ... shall not have C linkage.  */
15497   if (current_lang_name == lang_name_c)
15498     {
15499       error ("template with C linkage");
15500       /* Give it C++ linkage to avoid confusing other parts of the
15501          front end.  */
15502       push_lang_context (lang_name_cplusplus);
15503       need_lang_pop = true;
15504     }
15505   else
15506     need_lang_pop = false;
15507
15508   /* We cannot perform access checks on the template parameter
15509      declarations until we know what is being declared, just as we
15510      cannot check the decl-specifier list.  */
15511   push_deferring_access_checks (dk_deferred);
15512
15513   /* If the next token is `>', then we have an invalid
15514      specialization.  Rather than complain about an invalid template
15515      parameter, issue an error message here.  */
15516   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15517     {
15518       cp_parser_error (parser, "invalid explicit specialization");
15519       begin_specialization ();
15520       parameter_list = NULL_TREE;
15521     }
15522   else
15523     /* Parse the template parameters.  */
15524     parameter_list = cp_parser_template_parameter_list (parser);
15525
15526   /* Get the deferred access checks from the parameter list.  These
15527      will be checked once we know what is being declared, as for a
15528      member template the checks must be performed in the scope of the
15529      class containing the member.  */
15530   checks = get_deferred_access_checks ();
15531
15532   /* Look for the `>'.  */
15533   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15534   /* We just processed one more parameter list.  */
15535   ++parser->num_template_parameter_lists;
15536   /* If the next token is `template', there are more template
15537      parameters.  */
15538   if (cp_lexer_next_token_is_keyword (parser->lexer,
15539                                       RID_TEMPLATE))
15540     cp_parser_template_declaration_after_export (parser, member_p);
15541   else
15542     {
15543       /* There are no access checks when parsing a template, as we do not
15544          know if a specialization will be a friend.  */
15545       push_deferring_access_checks (dk_no_check);
15546       decl = cp_parser_single_declaration (parser,
15547                                            checks,
15548                                            member_p,
15549                                            &friend_p);
15550       pop_deferring_access_checks ();
15551
15552       /* If this is a member template declaration, let the front
15553          end know.  */
15554       if (member_p && !friend_p && decl)
15555         {
15556           if (TREE_CODE (decl) == TYPE_DECL)
15557             cp_parser_check_access_in_redeclaration (decl);
15558
15559           decl = finish_member_template_decl (decl);
15560         }
15561       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15562         make_friend_class (current_class_type, TREE_TYPE (decl),
15563                            /*complain=*/true);
15564     }
15565   /* We are done with the current parameter list.  */
15566   --parser->num_template_parameter_lists;
15567
15568   pop_deferring_access_checks ();
15569
15570   /* Finish up.  */
15571   finish_template_decl (parameter_list);
15572
15573   /* Register member declarations.  */
15574   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15575     finish_member_declaration (decl);
15576   /* For the erroneous case of a template with C linkage, we pushed an
15577      implicit C++ linkage scope; exit that scope now.  */
15578   if (need_lang_pop)
15579     pop_lang_context ();
15580   /* If DECL is a function template, we must return to parse it later.
15581      (Even though there is no definition, there might be default
15582      arguments that need handling.)  */
15583   if (member_p && decl
15584       && (TREE_CODE (decl) == FUNCTION_DECL
15585           || DECL_FUNCTION_TEMPLATE_P (decl)))
15586     TREE_VALUE (parser->unparsed_functions_queues)
15587       = tree_cons (NULL_TREE, decl,
15588                    TREE_VALUE (parser->unparsed_functions_queues));
15589 }
15590
15591 /* Perform the deferred access checks from a template-parameter-list.
15592    CHECKS is a TREE_LIST of access checks, as returned by
15593    get_deferred_access_checks.  */
15594
15595 static void
15596 cp_parser_perform_template_parameter_access_checks (tree checks)
15597 {
15598   ++processing_template_parmlist;
15599   perform_access_checks (checks);
15600   --processing_template_parmlist;
15601 }
15602
15603 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15604    `function-definition' sequence.  MEMBER_P is true, this declaration
15605    appears in a class scope.
15606
15607    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15608    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15609
15610 static tree
15611 cp_parser_single_declaration (cp_parser* parser,
15612                               tree checks,
15613                               bool member_p,
15614                               bool* friend_p)
15615 {
15616   int declares_class_or_enum;
15617   tree decl = NULL_TREE;
15618   cp_decl_specifier_seq decl_specifiers;
15619   bool function_definition_p = false;
15620
15621   /* This function is only used when processing a template
15622      declaration.  */
15623   gcc_assert (innermost_scope_kind () == sk_template_parms
15624               || innermost_scope_kind () == sk_template_spec);
15625
15626   /* Defer access checks until we know what is being declared.  */
15627   push_deferring_access_checks (dk_deferred);
15628
15629   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15630      alternative.  */
15631   cp_parser_decl_specifier_seq (parser,
15632                                 CP_PARSER_FLAGS_OPTIONAL,
15633                                 &decl_specifiers,
15634                                 &declares_class_or_enum);
15635   if (friend_p)
15636     *friend_p = cp_parser_friend_p (&decl_specifiers);
15637
15638   /* There are no template typedefs.  */
15639   if (decl_specifiers.specs[(int) ds_typedef])
15640     {
15641       error ("template declaration of %qs", "typedef");
15642       decl = error_mark_node;
15643     }
15644
15645   /* Gather up the access checks that occurred the
15646      decl-specifier-seq.  */
15647   stop_deferring_access_checks ();
15648
15649   /* Check for the declaration of a template class.  */
15650   if (declares_class_or_enum)
15651     {
15652       if (cp_parser_declares_only_class_p (parser))
15653         {
15654           decl = shadow_tag (&decl_specifiers);
15655
15656           /* In this case:
15657
15658                struct C {
15659                  friend template <typename T> struct A<T>::B;
15660                };
15661
15662              A<T>::B will be represented by a TYPENAME_TYPE, and
15663              therefore not recognized by shadow_tag.  */
15664           if (friend_p && *friend_p
15665               && !decl
15666               && decl_specifiers.type
15667               && TYPE_P (decl_specifiers.type))
15668             decl = decl_specifiers.type;
15669
15670           if (decl && decl != error_mark_node)
15671             decl = TYPE_NAME (decl);
15672           else
15673             decl = error_mark_node;
15674
15675           /* Perform access checks for template parameters.  */
15676           cp_parser_perform_template_parameter_access_checks (checks);
15677         }
15678     }
15679   /* If it's not a template class, try for a template function.  If
15680      the next token is a `;', then this declaration does not declare
15681      anything.  But, if there were errors in the decl-specifiers, then
15682      the error might well have come from an attempted class-specifier.
15683      In that case, there's no need to warn about a missing declarator.  */
15684   if (!decl
15685       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15686           || decl_specifiers.type != error_mark_node))
15687     decl = cp_parser_init_declarator (parser,
15688                                       &decl_specifiers,
15689                                       checks,
15690                                       /*function_definition_allowed_p=*/true,
15691                                       member_p,
15692                                       declares_class_or_enum,
15693                                       &function_definition_p);
15694
15695   pop_deferring_access_checks ();
15696
15697   /* Clear any current qualification; whatever comes next is the start
15698      of something new.  */
15699   parser->scope = NULL_TREE;
15700   parser->qualifying_scope = NULL_TREE;
15701   parser->object_scope = NULL_TREE;
15702   /* Look for a trailing `;' after the declaration.  */
15703   if (!function_definition_p
15704       && (decl == error_mark_node
15705           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15706     cp_parser_skip_to_end_of_block_or_statement (parser);
15707
15708   return decl;
15709 }
15710
15711 /* Parse a cast-expression that is not the operand of a unary "&".  */
15712
15713 static tree
15714 cp_parser_simple_cast_expression (cp_parser *parser)
15715 {
15716   return cp_parser_cast_expression (parser, /*address_p=*/false,
15717                                     /*cast_p=*/false);
15718 }
15719
15720 /* Parse a functional cast to TYPE.  Returns an expression
15721    representing the cast.  */
15722
15723 static tree
15724 cp_parser_functional_cast (cp_parser* parser, tree type)
15725 {
15726   tree expression_list;
15727   tree cast;
15728
15729   expression_list
15730     = cp_parser_parenthesized_expression_list (parser, false,
15731                                                /*cast_p=*/true,
15732                                                /*non_constant_p=*/NULL);
15733
15734   cast = build_functional_cast (type, expression_list);
15735   /* [expr.const]/1: In an integral constant expression "only type
15736      conversions to integral or enumeration type can be used".  */
15737   if (TREE_CODE (type) == TYPE_DECL)
15738     type = TREE_TYPE (type);
15739   if (cast != error_mark_node && !dependent_type_p (type)
15740       && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15741     {
15742       if (cp_parser_non_integral_constant_expression
15743           (parser, "a call to a constructor"))
15744         return error_mark_node;
15745     }
15746   return cast;
15747 }
15748
15749 /* Save the tokens that make up the body of a member function defined
15750    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15751    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15752    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15753    for the member function.  */
15754
15755 static tree
15756 cp_parser_save_member_function_body (cp_parser* parser,
15757                                      cp_decl_specifier_seq *decl_specifiers,
15758                                      cp_declarator *declarator,
15759                                      tree attributes)
15760 {
15761   cp_token *first;
15762   cp_token *last;
15763   tree fn;
15764
15765   /* Create the function-declaration.  */
15766   fn = start_method (decl_specifiers, declarator, attributes);
15767   /* If something went badly wrong, bail out now.  */
15768   if (fn == error_mark_node)
15769     {
15770       /* If there's a function-body, skip it.  */
15771       if (cp_parser_token_starts_function_definition_p
15772           (cp_lexer_peek_token (parser->lexer)))
15773         cp_parser_skip_to_end_of_block_or_statement (parser);
15774       return error_mark_node;
15775     }
15776
15777   /* Remember it, if there default args to post process.  */
15778   cp_parser_save_default_args (parser, fn);
15779
15780   /* Save away the tokens that make up the body of the
15781      function.  */
15782   first = parser->lexer->next_token;
15783   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15784   /* Handle function try blocks.  */
15785   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15786     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15787   last = parser->lexer->next_token;
15788
15789   /* Save away the inline definition; we will process it when the
15790      class is complete.  */
15791   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15792   DECL_PENDING_INLINE_P (fn) = 1;
15793
15794   /* We need to know that this was defined in the class, so that
15795      friend templates are handled correctly.  */
15796   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15797
15798   /* We're done with the inline definition.  */
15799   finish_method (fn);
15800
15801   /* Add FN to the queue of functions to be parsed later.  */
15802   TREE_VALUE (parser->unparsed_functions_queues)
15803     = tree_cons (NULL_TREE, fn,
15804                  TREE_VALUE (parser->unparsed_functions_queues));
15805
15806   return fn;
15807 }
15808
15809 /* Parse a template-argument-list, as well as the trailing ">" (but
15810    not the opening ">").  See cp_parser_template_argument_list for the
15811    return value.  */
15812
15813 static tree
15814 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15815 {
15816   tree arguments;
15817   tree saved_scope;
15818   tree saved_qualifying_scope;
15819   tree saved_object_scope;
15820   bool saved_greater_than_is_operator_p;
15821   bool saved_skip_evaluation;
15822
15823   /* [temp.names]
15824
15825      When parsing a template-id, the first non-nested `>' is taken as
15826      the end of the template-argument-list rather than a greater-than
15827      operator.  */
15828   saved_greater_than_is_operator_p
15829     = parser->greater_than_is_operator_p;
15830   parser->greater_than_is_operator_p = false;
15831   /* Parsing the argument list may modify SCOPE, so we save it
15832      here.  */
15833   saved_scope = parser->scope;
15834   saved_qualifying_scope = parser->qualifying_scope;
15835   saved_object_scope = parser->object_scope;
15836   /* We need to evaluate the template arguments, even though this
15837      template-id may be nested within a "sizeof".  */
15838   saved_skip_evaluation = skip_evaluation;
15839   skip_evaluation = false;
15840   /* Parse the template-argument-list itself.  */
15841   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15842     arguments = NULL_TREE;
15843   else
15844     arguments = cp_parser_template_argument_list (parser);
15845   /* Look for the `>' that ends the template-argument-list. If we find
15846      a '>>' instead, it's probably just a typo.  */
15847   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15848     {
15849       if (!saved_greater_than_is_operator_p)
15850         {
15851           /* If we're in a nested template argument list, the '>>' has
15852             to be a typo for '> >'. We emit the error message, but we
15853             continue parsing and we push a '>' as next token, so that
15854             the argument list will be parsed correctly.  Note that the
15855             global source location is still on the token before the
15856             '>>', so we need to say explicitly where we want it.  */
15857           cp_token *token = cp_lexer_peek_token (parser->lexer);
15858           error ("%H%<>>%> should be %<> >%> "
15859                  "within a nested template argument list",
15860                  &token->location);
15861
15862           /* ??? Proper recovery should terminate two levels of
15863              template argument list here.  */
15864           token->type = CPP_GREATER;
15865         }
15866       else
15867         {
15868           /* If this is not a nested template argument list, the '>>'
15869             is a typo for '>'. Emit an error message and continue.
15870             Same deal about the token location, but here we can get it
15871             right by consuming the '>>' before issuing the diagnostic.  */
15872           cp_lexer_consume_token (parser->lexer);
15873           error ("spurious %<>>%>, use %<>%> to terminate "
15874                  "a template argument list");
15875         }
15876     }
15877   else
15878     cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15879   /* The `>' token might be a greater-than operator again now.  */
15880   parser->greater_than_is_operator_p
15881     = saved_greater_than_is_operator_p;
15882   /* Restore the SAVED_SCOPE.  */
15883   parser->scope = saved_scope;
15884   parser->qualifying_scope = saved_qualifying_scope;
15885   parser->object_scope = saved_object_scope;
15886   skip_evaluation = saved_skip_evaluation;
15887
15888   return arguments;
15889 }
15890
15891 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15892    arguments, or the body of the function have not yet been parsed,
15893    parse them now.  */
15894
15895 static void
15896 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15897 {
15898   /* If this member is a template, get the underlying
15899      FUNCTION_DECL.  */
15900   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15901     member_function = DECL_TEMPLATE_RESULT (member_function);
15902
15903   /* There should not be any class definitions in progress at this
15904      point; the bodies of members are only parsed outside of all class
15905      definitions.  */
15906   gcc_assert (parser->num_classes_being_defined == 0);
15907   /* While we're parsing the member functions we might encounter more
15908      classes.  We want to handle them right away, but we don't want
15909      them getting mixed up with functions that are currently in the
15910      queue.  */
15911   parser->unparsed_functions_queues
15912     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15913
15914   /* Make sure that any template parameters are in scope.  */
15915   maybe_begin_member_template_processing (member_function);
15916
15917   /* If the body of the function has not yet been parsed, parse it
15918      now.  */
15919   if (DECL_PENDING_INLINE_P (member_function))
15920     {
15921       tree function_scope;
15922       cp_token_cache *tokens;
15923
15924       /* The function is no longer pending; we are processing it.  */
15925       tokens = DECL_PENDING_INLINE_INFO (member_function);
15926       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15927       DECL_PENDING_INLINE_P (member_function) = 0;
15928
15929       /* If this is a local class, enter the scope of the containing
15930          function.  */
15931       function_scope = current_function_decl;
15932       if (function_scope)
15933         push_function_context_to (function_scope);
15934
15935
15936       /* Push the body of the function onto the lexer stack.  */
15937       cp_parser_push_lexer_for_tokens (parser, tokens);
15938
15939       /* Let the front end know that we going to be defining this
15940          function.  */
15941       start_preparsed_function (member_function, NULL_TREE,
15942                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15943
15944       /* Don't do access checking if it is a templated function.  */
15945       if (processing_template_decl)
15946         push_deferring_access_checks (dk_no_check);
15947
15948       /* Now, parse the body of the function.  */
15949       cp_parser_function_definition_after_declarator (parser,
15950                                                       /*inline_p=*/true);
15951
15952       if (processing_template_decl)
15953         pop_deferring_access_checks ();
15954
15955       /* Leave the scope of the containing function.  */
15956       if (function_scope)
15957         pop_function_context_from (function_scope);
15958       cp_parser_pop_lexer (parser);
15959     }
15960
15961   /* Remove any template parameters from the symbol table.  */
15962   maybe_end_member_template_processing ();
15963
15964   /* Restore the queue.  */
15965   parser->unparsed_functions_queues
15966     = TREE_CHAIN (parser->unparsed_functions_queues);
15967 }
15968
15969 /* If DECL contains any default args, remember it on the unparsed
15970    functions queue.  */
15971
15972 static void
15973 cp_parser_save_default_args (cp_parser* parser, tree decl)
15974 {
15975   tree probe;
15976
15977   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15978        probe;
15979        probe = TREE_CHAIN (probe))
15980     if (TREE_PURPOSE (probe))
15981       {
15982         TREE_PURPOSE (parser->unparsed_functions_queues)
15983           = tree_cons (current_class_type, decl,
15984                        TREE_PURPOSE (parser->unparsed_functions_queues));
15985         break;
15986       }
15987 }
15988
15989 /* FN is a FUNCTION_DECL which may contains a parameter with an
15990    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15991    assumes that the current scope is the scope in which the default
15992    argument should be processed.  */
15993
15994 static void
15995 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15996 {
15997   bool saved_local_variables_forbidden_p;
15998   tree parm;
15999
16000   /* While we're parsing the default args, we might (due to the
16001      statement expression extension) encounter more classes.  We want
16002      to handle them right away, but we don't want them getting mixed
16003      up with default args that are currently in the queue.  */
16004   parser->unparsed_functions_queues
16005     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16006
16007   /* Local variable names (and the `this' keyword) may not appear
16008      in a default argument.  */
16009   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16010   parser->local_variables_forbidden_p = true;
16011
16012   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16013        parm;
16014        parm = TREE_CHAIN (parm))
16015     {
16016       cp_token_cache *tokens;
16017       tree default_arg = TREE_PURPOSE (parm);
16018       tree parsed_arg;
16019       VEC(tree,gc) *insts;
16020       tree copy;
16021       unsigned ix;
16022
16023       if (!default_arg)
16024         continue;
16025
16026       if (TREE_CODE (default_arg) != DEFAULT_ARG)
16027         /* This can happen for a friend declaration for a function
16028            already declared with default arguments.  */
16029         continue;
16030
16031        /* Push the saved tokens for the default argument onto the parser's
16032           lexer stack.  */
16033       tokens = DEFARG_TOKENS (default_arg);
16034       cp_parser_push_lexer_for_tokens (parser, tokens);
16035
16036       /* Parse the assignment-expression.  */
16037       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16038
16039       if (!processing_template_decl)
16040         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16041
16042       TREE_PURPOSE (parm) = parsed_arg;
16043
16044       /* Update any instantiations we've already created.  */
16045       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16046            VEC_iterate (tree, insts, ix, copy); ix++)
16047         TREE_PURPOSE (copy) = parsed_arg;
16048
16049       /* If the token stream has not been completely used up, then
16050          there was extra junk after the end of the default
16051          argument.  */
16052       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16053         cp_parser_error (parser, "expected %<,%>");
16054
16055       /* Revert to the main lexer.  */
16056       cp_parser_pop_lexer (parser);
16057     }
16058
16059   /* Make sure no default arg is missing.  */
16060   check_default_args (fn);
16061
16062   /* Restore the state of local_variables_forbidden_p.  */
16063   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16064
16065   /* Restore the queue.  */
16066   parser->unparsed_functions_queues
16067     = TREE_CHAIN (parser->unparsed_functions_queues);
16068 }
16069
16070 /* Parse the operand of `sizeof' (or a similar operator).  Returns
16071    either a TYPE or an expression, depending on the form of the
16072    input.  The KEYWORD indicates which kind of expression we have
16073    encountered.  */
16074
16075 static tree
16076 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16077 {
16078   static const char *format;
16079   tree expr = NULL_TREE;
16080   const char *saved_message;
16081   bool saved_integral_constant_expression_p;
16082   bool saved_non_integral_constant_expression_p;
16083
16084   /* Initialize FORMAT the first time we get here.  */
16085   if (!format)
16086     format = "types may not be defined in '%s' expressions";
16087
16088   /* Types cannot be defined in a `sizeof' expression.  Save away the
16089      old message.  */
16090   saved_message = parser->type_definition_forbidden_message;
16091   /* And create the new one.  */
16092   parser->type_definition_forbidden_message
16093     = XNEWVEC (const char, strlen (format)
16094                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16095                + 1 /* `\0' */);
16096   sprintf ((char *) parser->type_definition_forbidden_message,
16097            format, IDENTIFIER_POINTER (ridpointers[keyword]));
16098
16099   /* The restrictions on constant-expressions do not apply inside
16100      sizeof expressions.  */
16101   saved_integral_constant_expression_p
16102     = parser->integral_constant_expression_p;
16103   saved_non_integral_constant_expression_p
16104     = parser->non_integral_constant_expression_p;
16105   parser->integral_constant_expression_p = false;
16106
16107   /* Do not actually evaluate the expression.  */
16108   ++skip_evaluation;
16109   /* If it's a `(', then we might be looking at the type-id
16110      construction.  */
16111   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16112     {
16113       tree type;
16114       bool saved_in_type_id_in_expr_p;
16115
16116       /* We can't be sure yet whether we're looking at a type-id or an
16117          expression.  */
16118       cp_parser_parse_tentatively (parser);
16119       /* Consume the `('.  */
16120       cp_lexer_consume_token (parser->lexer);
16121       /* Parse the type-id.  */
16122       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16123       parser->in_type_id_in_expr_p = true;
16124       type = cp_parser_type_id (parser);
16125       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16126       /* Now, look for the trailing `)'.  */
16127       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16128       /* If all went well, then we're done.  */
16129       if (cp_parser_parse_definitely (parser))
16130         {
16131           cp_decl_specifier_seq decl_specs;
16132
16133           /* Build a trivial decl-specifier-seq.  */
16134           clear_decl_specs (&decl_specs);
16135           decl_specs.type = type;
16136
16137           /* Call grokdeclarator to figure out what type this is.  */
16138           expr = grokdeclarator (NULL,
16139                                  &decl_specs,
16140                                  TYPENAME,
16141                                  /*initialized=*/0,
16142                                  /*attrlist=*/NULL);
16143         }
16144     }
16145
16146   /* If the type-id production did not work out, then we must be
16147      looking at the unary-expression production.  */
16148   if (!expr)
16149     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16150                                        /*cast_p=*/false);
16151   /* Go back to evaluating expressions.  */
16152   --skip_evaluation;
16153
16154   /* Free the message we created.  */
16155   free ((char *) parser->type_definition_forbidden_message);
16156   /* And restore the old one.  */
16157   parser->type_definition_forbidden_message = saved_message;
16158   parser->integral_constant_expression_p
16159     = saved_integral_constant_expression_p;
16160   parser->non_integral_constant_expression_p
16161     = saved_non_integral_constant_expression_p;
16162
16163   return expr;
16164 }
16165
16166 /* If the current declaration has no declarator, return true.  */
16167
16168 static bool
16169 cp_parser_declares_only_class_p (cp_parser *parser)
16170 {
16171   /* If the next token is a `;' or a `,' then there is no
16172      declarator.  */
16173   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16174           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16175 }
16176
16177 /* Update the DECL_SPECS to reflect the storage class indicated by
16178    KEYWORD.  */
16179
16180 static void
16181 cp_parser_set_storage_class (cp_parser *parser,
16182                              cp_decl_specifier_seq *decl_specs,
16183                              enum rid keyword)
16184 {
16185   cp_storage_class storage_class;
16186
16187   if (parser->in_unbraced_linkage_specification_p)
16188     {
16189       error ("invalid use of %qD in linkage specification",
16190              ridpointers[keyword]);
16191       return;
16192     }
16193   else if (decl_specs->storage_class != sc_none)
16194     {
16195       decl_specs->multiple_storage_classes_p = true;
16196       return;
16197     }
16198
16199   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16200       && decl_specs->specs[(int) ds_thread])
16201     {
16202       error ("%<__thread%> before %qD", ridpointers[keyword]);
16203       decl_specs->specs[(int) ds_thread] = 0;
16204     }
16205
16206   switch (keyword)
16207     {
16208     case RID_AUTO:
16209       storage_class = sc_auto;
16210       break;
16211     case RID_REGISTER:
16212       storage_class = sc_register;
16213       break;
16214     case RID_STATIC:
16215       storage_class = sc_static;
16216       break;
16217     case RID_EXTERN:
16218       storage_class = sc_extern;
16219       break;
16220     case RID_MUTABLE:
16221       storage_class = sc_mutable;
16222       break;
16223     default:
16224       gcc_unreachable ();
16225     }
16226   decl_specs->storage_class = storage_class;
16227 }
16228
16229 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
16230    is true, the type is a user-defined type; otherwise it is a
16231    built-in type specified by a keyword.  */
16232
16233 static void
16234 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16235                               tree type_spec,
16236                               bool user_defined_p)
16237 {
16238   decl_specs->any_specifiers_p = true;
16239
16240   /* If the user tries to redeclare bool or wchar_t (with, for
16241      example, in "typedef int wchar_t;") we remember that this is what
16242      happened.  In system headers, we ignore these declarations so
16243      that G++ can work with system headers that are not C++-safe.  */
16244   if (decl_specs->specs[(int) ds_typedef]
16245       && !user_defined_p
16246       && (type_spec == boolean_type_node
16247           || type_spec == wchar_type_node)
16248       && (decl_specs->type
16249           || decl_specs->specs[(int) ds_long]
16250           || decl_specs->specs[(int) ds_short]
16251           || decl_specs->specs[(int) ds_unsigned]
16252           || decl_specs->specs[(int) ds_signed]))
16253     {
16254       decl_specs->redefined_builtin_type = type_spec;
16255       if (!decl_specs->type)
16256         {
16257           decl_specs->type = type_spec;
16258           decl_specs->user_defined_type_p = false;
16259         }
16260     }
16261   else if (decl_specs->type)
16262     decl_specs->multiple_types_p = true;
16263   else
16264     {
16265       decl_specs->type = type_spec;
16266       decl_specs->user_defined_type_p = user_defined_p;
16267       decl_specs->redefined_builtin_type = NULL_TREE;
16268     }
16269 }
16270
16271 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16272    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
16273
16274 static bool
16275 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16276 {
16277   return decl_specifiers->specs[(int) ds_friend] != 0;
16278 }
16279
16280 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
16281    issue an error message indicating that TOKEN_DESC was expected.
16282
16283    Returns the token consumed, if the token had the appropriate type.
16284    Otherwise, returns NULL.  */
16285
16286 static cp_token *
16287 cp_parser_require (cp_parser* parser,
16288                    enum cpp_ttype type,
16289                    const char* token_desc)
16290 {
16291   if (cp_lexer_next_token_is (parser->lexer, type))
16292     return cp_lexer_consume_token (parser->lexer);
16293   else
16294     {
16295       /* Output the MESSAGE -- unless we're parsing tentatively.  */
16296       if (!cp_parser_simulate_error (parser))
16297         {
16298           char *message = concat ("expected ", token_desc, NULL);
16299           cp_parser_error (parser, message);
16300           free (message);
16301         }
16302       return NULL;
16303     }
16304 }
16305
16306 /* Like cp_parser_require, except that tokens will be skipped until
16307    the desired token is found.  An error message is still produced if
16308    the next token is not as expected.  */
16309
16310 static void
16311 cp_parser_skip_until_found (cp_parser* parser,
16312                             enum cpp_ttype type,
16313                             const char* token_desc)
16314 {
16315   cp_token *token;
16316   unsigned nesting_depth = 0;
16317
16318   if (cp_parser_require (parser, type, token_desc))
16319     return;
16320
16321   /* Skip tokens until the desired token is found.  */
16322   while (true)
16323     {
16324       /* Peek at the next token.  */
16325       token = cp_lexer_peek_token (parser->lexer);
16326
16327       /* If we've reached the token we want, consume it and stop.  */
16328       if (token->type == type && !nesting_depth)
16329         {
16330           cp_lexer_consume_token (parser->lexer);
16331           return;
16332         }
16333
16334       switch (token->type)
16335         {
16336         case CPP_EOF:
16337         case CPP_PRAGMA_EOL:
16338           /* If we've run out of tokens, stop.  */
16339           return;
16340
16341         case CPP_OPEN_BRACE:
16342         case CPP_OPEN_PAREN:
16343         case CPP_OPEN_SQUARE:
16344           ++nesting_depth;
16345           break;
16346
16347         case CPP_CLOSE_BRACE:
16348         case CPP_CLOSE_PAREN:
16349         case CPP_CLOSE_SQUARE:
16350           if (nesting_depth-- == 0)
16351             return;
16352           break;
16353
16354         default:
16355           break;
16356         }
16357
16358       /* Consume this token.  */
16359       cp_lexer_consume_token (parser->lexer);
16360     }
16361 }
16362
16363 /* If the next token is the indicated keyword, consume it.  Otherwise,
16364    issue an error message indicating that TOKEN_DESC was expected.
16365
16366    Returns the token consumed, if the token had the appropriate type.
16367    Otherwise, returns NULL.  */
16368
16369 static cp_token *
16370 cp_parser_require_keyword (cp_parser* parser,
16371                            enum rid keyword,
16372                            const char* token_desc)
16373 {
16374   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16375
16376   if (token && token->keyword != keyword)
16377     {
16378       dyn_string_t error_msg;
16379
16380       /* Format the error message.  */
16381       error_msg = dyn_string_new (0);
16382       dyn_string_append_cstr (error_msg, "expected ");
16383       dyn_string_append_cstr (error_msg, token_desc);
16384       cp_parser_error (parser, error_msg->s);
16385       dyn_string_delete (error_msg);
16386       return NULL;
16387     }
16388
16389   return token;
16390 }
16391
16392 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16393    function-definition.  */
16394
16395 static bool
16396 cp_parser_token_starts_function_definition_p (cp_token* token)
16397 {
16398   return (/* An ordinary function-body begins with an `{'.  */
16399           token->type == CPP_OPEN_BRACE
16400           /* A ctor-initializer begins with a `:'.  */
16401           || token->type == CPP_COLON
16402           /* A function-try-block begins with `try'.  */
16403           || token->keyword == RID_TRY
16404           /* The named return value extension begins with `return'.  */
16405           || token->keyword == RID_RETURN);
16406 }
16407
16408 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16409    definition.  */
16410
16411 static bool
16412 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16413 {
16414   cp_token *token;
16415
16416   token = cp_lexer_peek_token (parser->lexer);
16417   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16418 }
16419
16420 /* Returns TRUE iff the next token is the "," or ">" ending a
16421    template-argument.  */
16422
16423 static bool
16424 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16425 {
16426   cp_token *token;
16427
16428   token = cp_lexer_peek_token (parser->lexer);
16429   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16430 }
16431
16432 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16433    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
16434
16435 static bool
16436 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16437                                                      size_t n)
16438 {
16439   cp_token *token;
16440
16441   token = cp_lexer_peek_nth_token (parser->lexer, n);
16442   if (token->type == CPP_LESS)
16443     return true;
16444   /* Check for the sequence `<::' in the original code. It would be lexed as
16445      `[:', where `[' is a digraph, and there is no whitespace before
16446      `:'.  */
16447   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16448     {
16449       cp_token *token2;
16450       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16451       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16452         return true;
16453     }
16454   return false;
16455 }
16456
16457 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16458    or none_type otherwise.  */
16459
16460 static enum tag_types
16461 cp_parser_token_is_class_key (cp_token* token)
16462 {
16463   switch (token->keyword)
16464     {
16465     case RID_CLASS:
16466       return class_type;
16467     case RID_STRUCT:
16468       return record_type;
16469     case RID_UNION:
16470       return union_type;
16471
16472     default:
16473       return none_type;
16474     }
16475 }
16476
16477 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16478
16479 static void
16480 cp_parser_check_class_key (enum tag_types class_key, tree type)
16481 {
16482   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16483     pedwarn ("%qs tag used in naming %q#T",
16484             class_key == union_type ? "union"
16485              : class_key == record_type ? "struct" : "class",
16486              type);
16487 }
16488
16489 /* Issue an error message if DECL is redeclared with different
16490    access than its original declaration [class.access.spec/3].
16491    This applies to nested classes and nested class templates.
16492    [class.mem/1].  */
16493
16494 static void
16495 cp_parser_check_access_in_redeclaration (tree decl)
16496 {
16497   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16498     return;
16499
16500   if ((TREE_PRIVATE (decl)
16501        != (current_access_specifier == access_private_node))
16502       || (TREE_PROTECTED (decl)
16503           != (current_access_specifier == access_protected_node)))
16504     error ("%qD redeclared with different access", decl);
16505 }
16506
16507 /* Look for the `template' keyword, as a syntactic disambiguator.
16508    Return TRUE iff it is present, in which case it will be
16509    consumed.  */
16510
16511 static bool
16512 cp_parser_optional_template_keyword (cp_parser *parser)
16513 {
16514   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16515     {
16516       /* The `template' keyword can only be used within templates;
16517          outside templates the parser can always figure out what is a
16518          template and what is not.  */
16519       if (!processing_template_decl)
16520         {
16521           error ("%<template%> (as a disambiguator) is only allowed "
16522                  "within templates");
16523           /* If this part of the token stream is rescanned, the same
16524              error message would be generated.  So, we purge the token
16525              from the stream.  */
16526           cp_lexer_purge_token (parser->lexer);
16527           return false;
16528         }
16529       else
16530         {
16531           /* Consume the `template' keyword.  */
16532           cp_lexer_consume_token (parser->lexer);
16533           return true;
16534         }
16535     }
16536
16537   return false;
16538 }
16539
16540 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16541    set PARSER->SCOPE, and perform other related actions.  */
16542
16543 static void
16544 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16545 {
16546   tree value;
16547   tree check;
16548
16549   /* Get the stored value.  */
16550   value = cp_lexer_consume_token (parser->lexer)->value;
16551   /* Perform any access checks that were deferred.  */
16552   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16553     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16554   /* Set the scope from the stored value.  */
16555   parser->scope = TREE_VALUE (value);
16556   parser->qualifying_scope = TREE_TYPE (value);
16557   parser->object_scope = NULL_TREE;
16558 }
16559
16560 /* Consume tokens up through a non-nested END token.  */
16561
16562 static void
16563 cp_parser_cache_group (cp_parser *parser,
16564                        enum cpp_ttype end,
16565                        unsigned depth)
16566 {
16567   while (true)
16568     {
16569       cp_token *token;
16570
16571       /* Abort a parenthesized expression if we encounter a brace.  */
16572       if ((end == CPP_CLOSE_PAREN || depth == 0)
16573           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16574         return;
16575       /* If we've reached the end of the file, stop.  */
16576       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16577           || (end != CPP_PRAGMA_EOL
16578               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16579         return;
16580       /* Consume the next token.  */
16581       token = cp_lexer_consume_token (parser->lexer);
16582       /* See if it starts a new group.  */
16583       if (token->type == CPP_OPEN_BRACE)
16584         {
16585           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16586           if (depth == 0)
16587             return;
16588         }
16589       else if (token->type == CPP_OPEN_PAREN)
16590         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16591       else if (token->type == CPP_PRAGMA)
16592         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16593       else if (token->type == end)
16594         return;
16595     }
16596 }
16597
16598 /* Begin parsing tentatively.  We always save tokens while parsing
16599    tentatively so that if the tentative parsing fails we can restore the
16600    tokens.  */
16601
16602 static void
16603 cp_parser_parse_tentatively (cp_parser* parser)
16604 {
16605   /* Enter a new parsing context.  */
16606   parser->context = cp_parser_context_new (parser->context);
16607   /* Begin saving tokens.  */
16608   cp_lexer_save_tokens (parser->lexer);
16609   /* In order to avoid repetitive access control error messages,
16610      access checks are queued up until we are no longer parsing
16611      tentatively.  */
16612   push_deferring_access_checks (dk_deferred);
16613 }
16614
16615 /* Commit to the currently active tentative parse.  */
16616
16617 static void
16618 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16619 {
16620   cp_parser_context *context;
16621   cp_lexer *lexer;
16622
16623   /* Mark all of the levels as committed.  */
16624   lexer = parser->lexer;
16625   for (context = parser->context; context->next; context = context->next)
16626     {
16627       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16628         break;
16629       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16630       while (!cp_lexer_saving_tokens (lexer))
16631         lexer = lexer->next;
16632       cp_lexer_commit_tokens (lexer);
16633     }
16634 }
16635
16636 /* Abort the currently active tentative parse.  All consumed tokens
16637    will be rolled back, and no diagnostics will be issued.  */
16638
16639 static void
16640 cp_parser_abort_tentative_parse (cp_parser* parser)
16641 {
16642   cp_parser_simulate_error (parser);
16643   /* Now, pretend that we want to see if the construct was
16644      successfully parsed.  */
16645   cp_parser_parse_definitely (parser);
16646 }
16647
16648 /* Stop parsing tentatively.  If a parse error has occurred, restore the
16649    token stream.  Otherwise, commit to the tokens we have consumed.
16650    Returns true if no error occurred; false otherwise.  */
16651
16652 static bool
16653 cp_parser_parse_definitely (cp_parser* parser)
16654 {
16655   bool error_occurred;
16656   cp_parser_context *context;
16657
16658   /* Remember whether or not an error occurred, since we are about to
16659      destroy that information.  */
16660   error_occurred = cp_parser_error_occurred (parser);
16661   /* Remove the topmost context from the stack.  */
16662   context = parser->context;
16663   parser->context = context->next;
16664   /* If no parse errors occurred, commit to the tentative parse.  */
16665   if (!error_occurred)
16666     {
16667       /* Commit to the tokens read tentatively, unless that was
16668          already done.  */
16669       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16670         cp_lexer_commit_tokens (parser->lexer);
16671
16672       pop_to_parent_deferring_access_checks ();
16673     }
16674   /* Otherwise, if errors occurred, roll back our state so that things
16675      are just as they were before we began the tentative parse.  */
16676   else
16677     {
16678       cp_lexer_rollback_tokens (parser->lexer);
16679       pop_deferring_access_checks ();
16680     }
16681   /* Add the context to the front of the free list.  */
16682   context->next = cp_parser_context_free_list;
16683   cp_parser_context_free_list = context;
16684
16685   return !error_occurred;
16686 }
16687
16688 /* Returns true if we are parsing tentatively and are not committed to
16689    this tentative parse.  */
16690
16691 static bool
16692 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16693 {
16694   return (cp_parser_parsing_tentatively (parser)
16695           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16696 }
16697
16698 /* Returns nonzero iff an error has occurred during the most recent
16699    tentative parse.  */
16700
16701 static bool
16702 cp_parser_error_occurred (cp_parser* parser)
16703 {
16704   return (cp_parser_parsing_tentatively (parser)
16705           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16706 }
16707
16708 /* Returns nonzero if GNU extensions are allowed.  */
16709
16710 static bool
16711 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16712 {
16713   return parser->allow_gnu_extensions_p;
16714 }
16715 \f
16716 /* Objective-C++ Productions */
16717
16718
16719 /* Parse an Objective-C expression, which feeds into a primary-expression
16720    above.
16721
16722    objc-expression:
16723      objc-message-expression
16724      objc-string-literal
16725      objc-encode-expression
16726      objc-protocol-expression
16727      objc-selector-expression
16728
16729   Returns a tree representation of the expression.  */
16730
16731 static tree
16732 cp_parser_objc_expression (cp_parser* parser)
16733 {
16734   /* Try to figure out what kind of declaration is present.  */
16735   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16736
16737   switch (kwd->type)
16738     {
16739     case CPP_OPEN_SQUARE:
16740       return cp_parser_objc_message_expression (parser);
16741
16742     case CPP_OBJC_STRING:
16743       kwd = cp_lexer_consume_token (parser->lexer);
16744       return objc_build_string_object (kwd->value);
16745
16746     case CPP_KEYWORD:
16747       switch (kwd->keyword)
16748         {
16749         case RID_AT_ENCODE:
16750           return cp_parser_objc_encode_expression (parser);
16751
16752         case RID_AT_PROTOCOL:
16753           return cp_parser_objc_protocol_expression (parser);
16754
16755         case RID_AT_SELECTOR:
16756           return cp_parser_objc_selector_expression (parser);
16757
16758         default:
16759           break;
16760         }
16761     default:
16762       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16763       cp_parser_skip_to_end_of_block_or_statement (parser);
16764     }
16765
16766   return error_mark_node;
16767 }
16768
16769 /* Parse an Objective-C message expression.
16770
16771    objc-message-expression:
16772      [ objc-message-receiver objc-message-args ]
16773
16774    Returns a representation of an Objective-C message.  */
16775
16776 static tree
16777 cp_parser_objc_message_expression (cp_parser* parser)
16778 {
16779   tree receiver, messageargs;
16780
16781   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
16782   receiver = cp_parser_objc_message_receiver (parser);
16783   messageargs = cp_parser_objc_message_args (parser);
16784   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16785
16786   return objc_build_message_expr (build_tree_list (receiver, messageargs));
16787 }
16788
16789 /* Parse an objc-message-receiver.
16790
16791    objc-message-receiver:
16792      expression
16793      simple-type-specifier
16794
16795   Returns a representation of the type or expression.  */
16796
16797 static tree
16798 cp_parser_objc_message_receiver (cp_parser* parser)
16799 {
16800   tree rcv;
16801
16802   /* An Objective-C message receiver may be either (1) a type
16803      or (2) an expression.  */
16804   cp_parser_parse_tentatively (parser);
16805   rcv = cp_parser_expression (parser, false);
16806
16807   if (cp_parser_parse_definitely (parser))
16808     return rcv;
16809
16810   rcv = cp_parser_simple_type_specifier (parser,
16811                                          /*decl_specs=*/NULL,
16812                                          CP_PARSER_FLAGS_NONE);
16813
16814   return objc_get_class_reference (rcv);
16815 }
16816
16817 /* Parse the arguments and selectors comprising an Objective-C message.
16818
16819    objc-message-args:
16820      objc-selector
16821      objc-selector-args
16822      objc-selector-args , objc-comma-args
16823
16824    objc-selector-args:
16825      objc-selector [opt] : assignment-expression
16826      objc-selector-args objc-selector [opt] : assignment-expression
16827
16828    objc-comma-args:
16829      assignment-expression
16830      objc-comma-args , assignment-expression
16831
16832    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16833    selector arguments and TREE_VALUE containing a list of comma
16834    arguments.  */
16835
16836 static tree
16837 cp_parser_objc_message_args (cp_parser* parser)
16838 {
16839   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16840   bool maybe_unary_selector_p = true;
16841   cp_token *token = cp_lexer_peek_token (parser->lexer);
16842
16843   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16844     {
16845       tree selector = NULL_TREE, arg;
16846
16847       if (token->type != CPP_COLON)
16848         selector = cp_parser_objc_selector (parser);
16849
16850       /* Detect if we have a unary selector.  */
16851       if (maybe_unary_selector_p
16852           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16853         return build_tree_list (selector, NULL_TREE);
16854
16855       maybe_unary_selector_p = false;
16856       cp_parser_require (parser, CPP_COLON, "`:'");
16857       arg = cp_parser_assignment_expression (parser, false);
16858
16859       sel_args
16860         = chainon (sel_args,
16861                    build_tree_list (selector, arg));
16862
16863       token = cp_lexer_peek_token (parser->lexer);
16864     }
16865
16866   /* Handle non-selector arguments, if any. */
16867   while (token->type == CPP_COMMA)
16868     {
16869       tree arg;
16870
16871       cp_lexer_consume_token (parser->lexer);
16872       arg = cp_parser_assignment_expression (parser, false);
16873
16874       addl_args
16875         = chainon (addl_args,
16876                    build_tree_list (NULL_TREE, arg));
16877
16878       token = cp_lexer_peek_token (parser->lexer);
16879     }
16880
16881   return build_tree_list (sel_args, addl_args);
16882 }
16883
16884 /* Parse an Objective-C encode expression.
16885
16886    objc-encode-expression:
16887      @encode objc-typename
16888
16889    Returns an encoded representation of the type argument.  */
16890
16891 static tree
16892 cp_parser_objc_encode_expression (cp_parser* parser)
16893 {
16894   tree type;
16895
16896   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
16897   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16898   type = complete_type (cp_parser_type_id (parser));
16899   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16900
16901   if (!type)
16902     {
16903       error ("%<@encode%> must specify a type as an argument");
16904       return error_mark_node;
16905     }
16906
16907   return objc_build_encode_expr (type);
16908 }
16909
16910 /* Parse an Objective-C @defs expression.  */
16911
16912 static tree
16913 cp_parser_objc_defs_expression (cp_parser *parser)
16914 {
16915   tree name;
16916
16917   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
16918   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16919   name = cp_parser_identifier (parser);
16920   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16921
16922   return objc_get_class_ivars (name);
16923 }
16924
16925 /* Parse an Objective-C protocol expression.
16926
16927   objc-protocol-expression:
16928     @protocol ( identifier )
16929
16930   Returns a representation of the protocol expression.  */
16931
16932 static tree
16933 cp_parser_objc_protocol_expression (cp_parser* parser)
16934 {
16935   tree proto;
16936
16937   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
16938   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16939   proto = cp_parser_identifier (parser);
16940   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16941
16942   return objc_build_protocol_expr (proto);
16943 }
16944
16945 /* Parse an Objective-C selector expression.
16946
16947    objc-selector-expression:
16948      @selector ( objc-method-signature )
16949
16950    objc-method-signature:
16951      objc-selector
16952      objc-selector-seq
16953
16954    objc-selector-seq:
16955      objc-selector :
16956      objc-selector-seq objc-selector :
16957
16958   Returns a representation of the method selector.  */
16959
16960 static tree
16961 cp_parser_objc_selector_expression (cp_parser* parser)
16962 {
16963   tree sel_seq = NULL_TREE;
16964   bool maybe_unary_selector_p = true;
16965   cp_token *token;
16966
16967   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
16968   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16969   token = cp_lexer_peek_token (parser->lexer);
16970
16971   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16972          || token->type == CPP_SCOPE)
16973     {
16974       tree selector = NULL_TREE;
16975
16976       if (token->type != CPP_COLON
16977           || token->type == CPP_SCOPE)
16978         selector = cp_parser_objc_selector (parser);
16979
16980       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16981           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16982         {
16983           /* Detect if we have a unary selector.  */
16984           if (maybe_unary_selector_p)
16985             {
16986               sel_seq = selector;
16987               goto finish_selector;
16988             }
16989           else
16990             {
16991               cp_parser_error (parser, "expected %<:%>");
16992             }
16993         }
16994       maybe_unary_selector_p = false;
16995       token = cp_lexer_consume_token (parser->lexer);
16996
16997       if (token->type == CPP_SCOPE)
16998         {
16999           sel_seq
17000             = chainon (sel_seq,
17001                        build_tree_list (selector, NULL_TREE));
17002           sel_seq
17003             = chainon (sel_seq,
17004                        build_tree_list (NULL_TREE, NULL_TREE));
17005         }
17006       else
17007         sel_seq
17008           = chainon (sel_seq,
17009                      build_tree_list (selector, NULL_TREE));
17010
17011       token = cp_lexer_peek_token (parser->lexer);
17012     }
17013
17014  finish_selector:
17015   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17016
17017   return objc_build_selector_expr (sel_seq);
17018 }
17019
17020 /* Parse a list of identifiers.
17021
17022    objc-identifier-list:
17023      identifier
17024      objc-identifier-list , identifier
17025
17026    Returns a TREE_LIST of identifier nodes.  */
17027
17028 static tree
17029 cp_parser_objc_identifier_list (cp_parser* parser)
17030 {
17031   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17032   cp_token *sep = cp_lexer_peek_token (parser->lexer);
17033
17034   while (sep->type == CPP_COMMA)
17035     {
17036       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17037       list = chainon (list,
17038                       build_tree_list (NULL_TREE,
17039                                        cp_parser_identifier (parser)));
17040       sep = cp_lexer_peek_token (parser->lexer);
17041     }
17042
17043   return list;
17044 }
17045
17046 /* Parse an Objective-C alias declaration.
17047
17048    objc-alias-declaration:
17049      @compatibility_alias identifier identifier ;
17050
17051    This function registers the alias mapping with the Objective-C front-end.
17052    It returns nothing.  */
17053
17054 static void
17055 cp_parser_objc_alias_declaration (cp_parser* parser)
17056 {
17057   tree alias, orig;
17058
17059   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
17060   alias = cp_parser_identifier (parser);
17061   orig = cp_parser_identifier (parser);
17062   objc_declare_alias (alias, orig);
17063   cp_parser_consume_semicolon_at_end_of_statement (parser);
17064 }
17065
17066 /* Parse an Objective-C class forward-declaration.
17067
17068    objc-class-declaration:
17069      @class objc-identifier-list ;
17070
17071    The function registers the forward declarations with the Objective-C
17072    front-end.  It returns nothing.  */
17073
17074 static void
17075 cp_parser_objc_class_declaration (cp_parser* parser)
17076 {
17077   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
17078   objc_declare_class (cp_parser_objc_identifier_list (parser));
17079   cp_parser_consume_semicolon_at_end_of_statement (parser);
17080 }
17081
17082 /* Parse a list of Objective-C protocol references.
17083
17084    objc-protocol-refs-opt:
17085      objc-protocol-refs [opt]
17086
17087    objc-protocol-refs:
17088      < objc-identifier-list >
17089
17090    Returns a TREE_LIST of identifiers, if any.  */
17091
17092 static tree
17093 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17094 {
17095   tree protorefs = NULL_TREE;
17096
17097   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17098     {
17099       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
17100       protorefs = cp_parser_objc_identifier_list (parser);
17101       cp_parser_require (parser, CPP_GREATER, "`>'");
17102     }
17103
17104   return protorefs;
17105 }
17106
17107 /* Parse a Objective-C visibility specification.  */
17108
17109 static void
17110 cp_parser_objc_visibility_spec (cp_parser* parser)
17111 {
17112   cp_token *vis = cp_lexer_peek_token (parser->lexer);
17113
17114   switch (vis->keyword)
17115     {
17116     case RID_AT_PRIVATE:
17117       objc_set_visibility (2);
17118       break;
17119     case RID_AT_PROTECTED:
17120       objc_set_visibility (0);
17121       break;
17122     case RID_AT_PUBLIC:
17123       objc_set_visibility (1);
17124       break;
17125     default:
17126       return;
17127     }
17128
17129   /* Eat '@private'/'@protected'/'@public'.  */
17130   cp_lexer_consume_token (parser->lexer);
17131 }
17132
17133 /* Parse an Objective-C method type.  */
17134
17135 static void
17136 cp_parser_objc_method_type (cp_parser* parser)
17137 {
17138   objc_set_method_type
17139    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17140     ? PLUS_EXPR
17141     : MINUS_EXPR);
17142 }
17143
17144 /* Parse an Objective-C protocol qualifier.  */
17145
17146 static tree
17147 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17148 {
17149   tree quals = NULL_TREE, node;
17150   cp_token *token = cp_lexer_peek_token (parser->lexer);
17151
17152   node = token->value;
17153
17154   while (node && TREE_CODE (node) == IDENTIFIER_NODE
17155          && (node == ridpointers [(int) RID_IN]
17156              || node == ridpointers [(int) RID_OUT]
17157              || node == ridpointers [(int) RID_INOUT]
17158              || node == ridpointers [(int) RID_BYCOPY]
17159              || node == ridpointers [(int) RID_BYREF]
17160              || node == ridpointers [(int) RID_ONEWAY]))
17161     {
17162       quals = tree_cons (NULL_TREE, node, quals);
17163       cp_lexer_consume_token (parser->lexer);
17164       token = cp_lexer_peek_token (parser->lexer);
17165       node = token->value;
17166     }
17167
17168   return quals;
17169 }
17170
17171 /* Parse an Objective-C typename.  */
17172
17173 static tree
17174 cp_parser_objc_typename (cp_parser* parser)
17175 {
17176   tree typename = NULL_TREE;
17177
17178   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17179     {
17180       tree proto_quals, cp_type = NULL_TREE;
17181
17182       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17183       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17184
17185       /* An ObjC type name may consist of just protocol qualifiers, in which
17186          case the type shall default to 'id'.  */
17187       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17188         cp_type = cp_parser_type_id (parser);
17189
17190       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17191       typename = build_tree_list (proto_quals, cp_type);
17192     }
17193
17194   return typename;
17195 }
17196
17197 /* Check to see if TYPE refers to an Objective-C selector name.  */
17198
17199 static bool
17200 cp_parser_objc_selector_p (enum cpp_ttype type)
17201 {
17202   return (type == CPP_NAME || type == CPP_KEYWORD
17203           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17204           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17205           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17206           || type == CPP_XOR || type == CPP_XOR_EQ);
17207 }
17208
17209 /* Parse an Objective-C selector.  */
17210
17211 static tree
17212 cp_parser_objc_selector (cp_parser* parser)
17213 {
17214   cp_token *token = cp_lexer_consume_token (parser->lexer);
17215
17216   if (!cp_parser_objc_selector_p (token->type))
17217     {
17218       error ("invalid Objective-C++ selector name");
17219       return error_mark_node;
17220     }
17221
17222   /* C++ operator names are allowed to appear in ObjC selectors.  */
17223   switch (token->type)
17224     {
17225     case CPP_AND_AND: return get_identifier ("and");
17226     case CPP_AND_EQ: return get_identifier ("and_eq");
17227     case CPP_AND: return get_identifier ("bitand");
17228     case CPP_OR: return get_identifier ("bitor");
17229     case CPP_COMPL: return get_identifier ("compl");
17230     case CPP_NOT: return get_identifier ("not");
17231     case CPP_NOT_EQ: return get_identifier ("not_eq");
17232     case CPP_OR_OR: return get_identifier ("or");
17233     case CPP_OR_EQ: return get_identifier ("or_eq");
17234     case CPP_XOR: return get_identifier ("xor");
17235     case CPP_XOR_EQ: return get_identifier ("xor_eq");
17236     default: return token->value;
17237     }
17238 }
17239
17240 /* Parse an Objective-C params list.  */
17241
17242 static tree
17243 cp_parser_objc_method_keyword_params (cp_parser* parser)
17244 {
17245   tree params = NULL_TREE;
17246   bool maybe_unary_selector_p = true;
17247   cp_token *token = cp_lexer_peek_token (parser->lexer);
17248
17249   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17250     {
17251       tree selector = NULL_TREE, typename, identifier;
17252
17253       if (token->type != CPP_COLON)
17254         selector = cp_parser_objc_selector (parser);
17255
17256       /* Detect if we have a unary selector.  */
17257       if (maybe_unary_selector_p
17258           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17259         return selector;
17260
17261       maybe_unary_selector_p = false;
17262       cp_parser_require (parser, CPP_COLON, "`:'");
17263       typename = cp_parser_objc_typename (parser);
17264       identifier = cp_parser_identifier (parser);
17265
17266       params
17267         = chainon (params,
17268                    objc_build_keyword_decl (selector,
17269                                             typename,
17270                                             identifier));
17271
17272       token = cp_lexer_peek_token (parser->lexer);
17273     }
17274
17275   return params;
17276 }
17277
17278 /* Parse the non-keyword Objective-C params.  */
17279
17280 static tree
17281 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17282 {
17283   tree params = make_node (TREE_LIST);
17284   cp_token *token = cp_lexer_peek_token (parser->lexer);
17285   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
17286
17287   while (token->type == CPP_COMMA)
17288     {
17289       cp_parameter_declarator *parmdecl;
17290       tree parm;
17291
17292       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17293       token = cp_lexer_peek_token (parser->lexer);
17294
17295       if (token->type == CPP_ELLIPSIS)
17296         {
17297           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
17298           *ellipsisp = true;
17299           break;
17300         }
17301
17302       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17303       parm = grokdeclarator (parmdecl->declarator,
17304                              &parmdecl->decl_specifiers,
17305                              PARM, /*initialized=*/0,
17306                              /*attrlist=*/NULL);
17307
17308       chainon (params, build_tree_list (NULL_TREE, parm));
17309       token = cp_lexer_peek_token (parser->lexer);
17310     }
17311
17312   return params;
17313 }
17314
17315 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
17316
17317 static void
17318 cp_parser_objc_interstitial_code (cp_parser* parser)
17319 {
17320   cp_token *token = cp_lexer_peek_token (parser->lexer);
17321
17322   /* If the next token is `extern' and the following token is a string
17323      literal, then we have a linkage specification.  */
17324   if (token->keyword == RID_EXTERN
17325       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17326     cp_parser_linkage_specification (parser);
17327   /* Handle #pragma, if any.  */
17328   else if (token->type == CPP_PRAGMA)
17329     cp_parser_pragma (parser, pragma_external);
17330   /* Allow stray semicolons.  */
17331   else if (token->type == CPP_SEMICOLON)
17332     cp_lexer_consume_token (parser->lexer);
17333   /* Finally, try to parse a block-declaration, or a function-definition.  */
17334   else
17335     cp_parser_block_declaration (parser, /*statement_p=*/false);
17336 }
17337
17338 /* Parse a method signature.  */
17339
17340 static tree
17341 cp_parser_objc_method_signature (cp_parser* parser)
17342 {
17343   tree rettype, kwdparms, optparms;
17344   bool ellipsis = false;
17345
17346   cp_parser_objc_method_type (parser);
17347   rettype = cp_parser_objc_typename (parser);
17348   kwdparms = cp_parser_objc_method_keyword_params (parser);
17349   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17350
17351   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17352 }
17353
17354 /* Pars an Objective-C method prototype list.  */
17355
17356 static void
17357 cp_parser_objc_method_prototype_list (cp_parser* parser)
17358 {
17359   cp_token *token = cp_lexer_peek_token (parser->lexer);
17360
17361   while (token->keyword != RID_AT_END)
17362     {
17363       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17364         {
17365           objc_add_method_declaration
17366            (cp_parser_objc_method_signature (parser));
17367           cp_parser_consume_semicolon_at_end_of_statement (parser);
17368         }
17369       else
17370         /* Allow for interspersed non-ObjC++ code.  */
17371         cp_parser_objc_interstitial_code (parser);
17372
17373       token = cp_lexer_peek_token (parser->lexer);
17374     }
17375
17376   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17377   objc_finish_interface ();
17378 }
17379
17380 /* Parse an Objective-C method definition list.  */
17381
17382 static void
17383 cp_parser_objc_method_definition_list (cp_parser* parser)
17384 {
17385   cp_token *token = cp_lexer_peek_token (parser->lexer);
17386
17387   while (token->keyword != RID_AT_END)
17388     {
17389       tree meth;
17390
17391       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17392         {
17393           push_deferring_access_checks (dk_deferred);
17394           objc_start_method_definition
17395            (cp_parser_objc_method_signature (parser));
17396
17397           /* For historical reasons, we accept an optional semicolon.  */
17398           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17399             cp_lexer_consume_token (parser->lexer);
17400
17401           perform_deferred_access_checks ();
17402           stop_deferring_access_checks ();
17403           meth = cp_parser_function_definition_after_declarator (parser,
17404                                                                  false);
17405           pop_deferring_access_checks ();
17406           objc_finish_method_definition (meth);
17407         }
17408       else
17409         /* Allow for interspersed non-ObjC++ code.  */
17410         cp_parser_objc_interstitial_code (parser);
17411
17412       token = cp_lexer_peek_token (parser->lexer);
17413     }
17414
17415   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17416   objc_finish_implementation ();
17417 }
17418
17419 /* Parse Objective-C ivars.  */
17420
17421 static void
17422 cp_parser_objc_class_ivars (cp_parser* parser)
17423 {
17424   cp_token *token = cp_lexer_peek_token (parser->lexer);
17425
17426   if (token->type != CPP_OPEN_BRACE)
17427     return;     /* No ivars specified.  */
17428
17429   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
17430   token = cp_lexer_peek_token (parser->lexer);
17431
17432   while (token->type != CPP_CLOSE_BRACE)
17433     {
17434       cp_decl_specifier_seq declspecs;
17435       int decl_class_or_enum_p;
17436       tree prefix_attributes;
17437
17438       cp_parser_objc_visibility_spec (parser);
17439
17440       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17441         break;
17442
17443       cp_parser_decl_specifier_seq (parser,
17444                                     CP_PARSER_FLAGS_OPTIONAL,
17445                                     &declspecs,
17446                                     &decl_class_or_enum_p);
17447       prefix_attributes = declspecs.attributes;
17448       declspecs.attributes = NULL_TREE;
17449
17450       /* Keep going until we hit the `;' at the end of the
17451          declaration.  */
17452       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17453         {
17454           tree width = NULL_TREE, attributes, first_attribute, decl;
17455           cp_declarator *declarator = NULL;
17456           int ctor_dtor_or_conv_p;
17457
17458           /* Check for a (possibly unnamed) bitfield declaration.  */
17459           token = cp_lexer_peek_token (parser->lexer);
17460           if (token->type == CPP_COLON)
17461             goto eat_colon;
17462
17463           if (token->type == CPP_NAME
17464               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17465                   == CPP_COLON))
17466             {
17467               /* Get the name of the bitfield.  */
17468               declarator = make_id_declarator (NULL_TREE,
17469                                                cp_parser_identifier (parser),
17470                                                sfk_none);
17471
17472              eat_colon:
17473               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17474               /* Get the width of the bitfield.  */
17475               width
17476                 = cp_parser_constant_expression (parser,
17477                                                  /*allow_non_constant=*/false,
17478                                                  NULL);
17479             }
17480           else
17481             {
17482               /* Parse the declarator.  */
17483               declarator
17484                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17485                                         &ctor_dtor_or_conv_p,
17486                                         /*parenthesized_p=*/NULL,
17487                                         /*member_p=*/false);
17488             }
17489
17490           /* Look for attributes that apply to the ivar.  */
17491           attributes = cp_parser_attributes_opt (parser);
17492           /* Remember which attributes are prefix attributes and
17493              which are not.  */
17494           first_attribute = attributes;
17495           /* Combine the attributes.  */
17496           attributes = chainon (prefix_attributes, attributes);
17497
17498           if (width)
17499             {
17500               /* Create the bitfield declaration.  */
17501               decl = grokbitfield (declarator, &declspecs, width);
17502               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17503             }
17504           else
17505             decl = grokfield (declarator, &declspecs,
17506                               NULL_TREE, /*init_const_expr_p=*/false,
17507                               NULL_TREE, attributes);
17508
17509           /* Add the instance variable.  */
17510           objc_add_instance_variable (decl);
17511
17512           /* Reset PREFIX_ATTRIBUTES.  */
17513           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17514             attributes = TREE_CHAIN (attributes);
17515           if (attributes)
17516             TREE_CHAIN (attributes) = NULL_TREE;
17517
17518           token = cp_lexer_peek_token (parser->lexer);
17519
17520           if (token->type == CPP_COMMA)
17521             {
17522               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17523               continue;
17524             }
17525           break;
17526         }
17527
17528       cp_parser_consume_semicolon_at_end_of_statement (parser);
17529       token = cp_lexer_peek_token (parser->lexer);
17530     }
17531
17532   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17533   /* For historical reasons, we accept an optional semicolon.  */
17534   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17535     cp_lexer_consume_token (parser->lexer);
17536 }
17537
17538 /* Parse an Objective-C protocol declaration.  */
17539
17540 static void
17541 cp_parser_objc_protocol_declaration (cp_parser* parser)
17542 {
17543   tree proto, protorefs;
17544   cp_token *tok;
17545
17546   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17547   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17548     {
17549       error ("identifier expected after %<@protocol%>");
17550       goto finish;
17551     }
17552
17553   /* See if we have a forward declaration or a definition.  */
17554   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17555
17556   /* Try a forward declaration first.  */
17557   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17558     {
17559       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17560      finish:
17561       cp_parser_consume_semicolon_at_end_of_statement (parser);
17562     }
17563
17564   /* Ok, we got a full-fledged definition (or at least should).  */
17565   else
17566     {
17567       proto = cp_parser_identifier (parser);
17568       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17569       objc_start_protocol (proto, protorefs);
17570       cp_parser_objc_method_prototype_list (parser);
17571     }
17572 }
17573
17574 /* Parse an Objective-C superclass or category.  */
17575
17576 static void
17577 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17578                                                           tree *categ)
17579 {
17580   cp_token *next = cp_lexer_peek_token (parser->lexer);
17581
17582   *super = *categ = NULL_TREE;
17583   if (next->type == CPP_COLON)
17584     {
17585       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17586       *super = cp_parser_identifier (parser);
17587     }
17588   else if (next->type == CPP_OPEN_PAREN)
17589     {
17590       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17591       *categ = cp_parser_identifier (parser);
17592       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17593     }
17594 }
17595
17596 /* Parse an Objective-C class interface.  */
17597
17598 static void
17599 cp_parser_objc_class_interface (cp_parser* parser)
17600 {
17601   tree name, super, categ, protos;
17602
17603   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17604   name = cp_parser_identifier (parser);
17605   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17606   protos = cp_parser_objc_protocol_refs_opt (parser);
17607
17608   /* We have either a class or a category on our hands.  */
17609   if (categ)
17610     objc_start_category_interface (name, categ, protos);
17611   else
17612     {
17613       objc_start_class_interface (name, super, protos);
17614       /* Handle instance variable declarations, if any.  */
17615       cp_parser_objc_class_ivars (parser);
17616       objc_continue_interface ();
17617     }
17618
17619   cp_parser_objc_method_prototype_list (parser);
17620 }
17621
17622 /* Parse an Objective-C class implementation.  */
17623
17624 static void
17625 cp_parser_objc_class_implementation (cp_parser* parser)
17626 {
17627   tree name, super, categ;
17628
17629   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17630   name = cp_parser_identifier (parser);
17631   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17632
17633   /* We have either a class or a category on our hands.  */
17634   if (categ)
17635     objc_start_category_implementation (name, categ);
17636   else
17637     {
17638       objc_start_class_implementation (name, super);
17639       /* Handle instance variable declarations, if any.  */
17640       cp_parser_objc_class_ivars (parser);
17641       objc_continue_implementation ();
17642     }
17643
17644   cp_parser_objc_method_definition_list (parser);
17645 }
17646
17647 /* Consume the @end token and finish off the implementation.  */
17648
17649 static void
17650 cp_parser_objc_end_implementation (cp_parser* parser)
17651 {
17652   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17653   objc_finish_implementation ();
17654 }
17655
17656 /* Parse an Objective-C declaration.  */
17657
17658 static void
17659 cp_parser_objc_declaration (cp_parser* parser)
17660 {
17661   /* Try to figure out what kind of declaration is present.  */
17662   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17663
17664   switch (kwd->keyword)
17665     {
17666     case RID_AT_ALIAS:
17667       cp_parser_objc_alias_declaration (parser);
17668       break;
17669     case RID_AT_CLASS:
17670       cp_parser_objc_class_declaration (parser);
17671       break;
17672     case RID_AT_PROTOCOL:
17673       cp_parser_objc_protocol_declaration (parser);
17674       break;
17675     case RID_AT_INTERFACE:
17676       cp_parser_objc_class_interface (parser);
17677       break;
17678     case RID_AT_IMPLEMENTATION:
17679       cp_parser_objc_class_implementation (parser);
17680       break;
17681     case RID_AT_END:
17682       cp_parser_objc_end_implementation (parser);
17683       break;
17684     default:
17685       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17686       cp_parser_skip_to_end_of_block_or_statement (parser);
17687     }
17688 }
17689
17690 /* Parse an Objective-C try-catch-finally statement.
17691
17692    objc-try-catch-finally-stmt:
17693      @try compound-statement objc-catch-clause-seq [opt]
17694        objc-finally-clause [opt]
17695
17696    objc-catch-clause-seq:
17697      objc-catch-clause objc-catch-clause-seq [opt]
17698
17699    objc-catch-clause:
17700      @catch ( exception-declaration ) compound-statement
17701
17702    objc-finally-clause
17703      @finally compound-statement
17704
17705    Returns NULL_TREE.  */
17706
17707 static tree
17708 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17709   location_t location;
17710   tree stmt;
17711
17712   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17713   location = cp_lexer_peek_token (parser->lexer)->location;
17714   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17715      node, lest it get absorbed into the surrounding block.  */
17716   stmt = push_stmt_list ();
17717   cp_parser_compound_statement (parser, NULL, false);
17718   objc_begin_try_stmt (location, pop_stmt_list (stmt));
17719
17720   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17721     {
17722       cp_parameter_declarator *parmdecl;
17723       tree parm;
17724
17725       cp_lexer_consume_token (parser->lexer);
17726       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17727       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17728       parm = grokdeclarator (parmdecl->declarator,
17729                              &parmdecl->decl_specifiers,
17730                              PARM, /*initialized=*/0,
17731                              /*attrlist=*/NULL);
17732       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17733       objc_begin_catch_clause (parm);
17734       cp_parser_compound_statement (parser, NULL, false);
17735       objc_finish_catch_clause ();
17736     }
17737
17738   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17739     {
17740       cp_lexer_consume_token (parser->lexer);
17741       location = cp_lexer_peek_token (parser->lexer)->location;
17742       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17743          node, lest it get absorbed into the surrounding block.  */
17744       stmt = push_stmt_list ();
17745       cp_parser_compound_statement (parser, NULL, false);
17746       objc_build_finally_clause (location, pop_stmt_list (stmt));
17747     }
17748
17749   return objc_finish_try_stmt ();
17750 }
17751
17752 /* Parse an Objective-C synchronized statement.
17753
17754    objc-synchronized-stmt:
17755      @synchronized ( expression ) compound-statement
17756
17757    Returns NULL_TREE.  */
17758
17759 static tree
17760 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17761   location_t location;
17762   tree lock, stmt;
17763
17764   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17765
17766   location = cp_lexer_peek_token (parser->lexer)->location;
17767   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17768   lock = cp_parser_expression (parser, false);
17769   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17770
17771   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17772      node, lest it get absorbed into the surrounding block.  */
17773   stmt = push_stmt_list ();
17774   cp_parser_compound_statement (parser, NULL, false);
17775
17776   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17777 }
17778
17779 /* Parse an Objective-C throw statement.
17780
17781    objc-throw-stmt:
17782      @throw assignment-expression [opt] ;
17783
17784    Returns a constructed '@throw' statement.  */
17785
17786 static tree
17787 cp_parser_objc_throw_statement (cp_parser *parser) {
17788   tree expr = NULL_TREE;
17789
17790   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17791
17792   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17793     expr = cp_parser_assignment_expression (parser, false);
17794
17795   cp_parser_consume_semicolon_at_end_of_statement (parser);
17796
17797   return objc_build_throw_stmt (expr);
17798 }
17799
17800 /* Parse an Objective-C statement.  */
17801
17802 static tree
17803 cp_parser_objc_statement (cp_parser * parser) {
17804   /* Try to figure out what kind of declaration is present.  */
17805   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17806
17807   switch (kwd->keyword)
17808     {
17809     case RID_AT_TRY:
17810       return cp_parser_objc_try_catch_finally_statement (parser);
17811     case RID_AT_SYNCHRONIZED:
17812       return cp_parser_objc_synchronized_statement (parser);
17813     case RID_AT_THROW:
17814       return cp_parser_objc_throw_statement (parser);
17815     default:
17816       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17817       cp_parser_skip_to_end_of_block_or_statement (parser);
17818     }
17819
17820   return error_mark_node;
17821 }
17822 \f
17823 /* OpenMP 2.5 parsing routines.  */
17824
17825 /* All OpenMP clauses.  OpenMP 2.5.  */
17826 typedef enum pragma_omp_clause {
17827   PRAGMA_OMP_CLAUSE_NONE = 0,
17828
17829   PRAGMA_OMP_CLAUSE_COPYIN,
17830   PRAGMA_OMP_CLAUSE_COPYPRIVATE,
17831   PRAGMA_OMP_CLAUSE_DEFAULT,
17832   PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
17833   PRAGMA_OMP_CLAUSE_IF,
17834   PRAGMA_OMP_CLAUSE_LASTPRIVATE,
17835   PRAGMA_OMP_CLAUSE_NOWAIT,
17836   PRAGMA_OMP_CLAUSE_NUM_THREADS,
17837   PRAGMA_OMP_CLAUSE_ORDERED,
17838   PRAGMA_OMP_CLAUSE_PRIVATE,
17839   PRAGMA_OMP_CLAUSE_REDUCTION,
17840   PRAGMA_OMP_CLAUSE_SCHEDULE,
17841   PRAGMA_OMP_CLAUSE_SHARED
17842 } pragma_omp_clause;
17843
17844 /* Returns name of the next clause.
17845    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17846    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
17847    returned and the token is consumed.  */
17848
17849 static pragma_omp_clause
17850 cp_parser_omp_clause_name (cp_parser *parser)
17851 {
17852   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
17853
17854   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
17855     result = PRAGMA_OMP_CLAUSE_IF;
17856   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
17857     result = PRAGMA_OMP_CLAUSE_DEFAULT;
17858   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
17859     result = PRAGMA_OMP_CLAUSE_PRIVATE;
17860   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17861     {
17862       tree id = cp_lexer_peek_token (parser->lexer)->value;
17863       const char *p = IDENTIFIER_POINTER (id);
17864
17865       switch (p[0])
17866         {
17867         case 'c':
17868           if (!strcmp ("copyin", p))
17869             result = PRAGMA_OMP_CLAUSE_COPYIN;
17870           else if (!strcmp ("copyprivate", p))
17871             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
17872           break;
17873         case 'f':
17874           if (!strcmp ("firstprivate", p))
17875             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
17876           break;
17877         case 'l':
17878           if (!strcmp ("lastprivate", p))
17879             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
17880           break;
17881         case 'n':
17882           if (!strcmp ("nowait", p))
17883             result = PRAGMA_OMP_CLAUSE_NOWAIT;
17884           else if (!strcmp ("num_threads", p))
17885             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
17886           break;
17887         case 'o':
17888           if (!strcmp ("ordered", p))
17889             result = PRAGMA_OMP_CLAUSE_ORDERED;
17890           break;
17891         case 'r':
17892           if (!strcmp ("reduction", p))
17893             result = PRAGMA_OMP_CLAUSE_REDUCTION;
17894           break;
17895         case 's':
17896           if (!strcmp ("schedule", p))
17897             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
17898           else if (!strcmp ("shared", p))
17899             result = PRAGMA_OMP_CLAUSE_SHARED;
17900           break;
17901         }
17902     }
17903
17904   if (result != PRAGMA_OMP_CLAUSE_NONE)
17905     cp_lexer_consume_token (parser->lexer);
17906
17907   return result;
17908 }
17909
17910 /* Validate that a clause of the given type does not already exist.  */
17911
17912 static void
17913 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
17914 {
17915   tree c;
17916
17917   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
17918     if (OMP_CLAUSE_CODE (c) == code)
17919       {
17920         error ("too many %qs clauses", name);
17921         break;
17922       }
17923 }
17924
17925 /* OpenMP 2.5:
17926    variable-list:
17927      identifier
17928      variable-list , identifier
17929
17930    In addition, we match a closing parenthesis.  An opening parenthesis
17931    will have been consumed by the caller.
17932
17933    If KIND is nonzero, create the appropriate node and install the decl
17934    in OMP_CLAUSE_DECL and add the node to the head of the list.
17935
17936    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17937    return the list created.  */
17938
17939 static tree
17940 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
17941                                 tree list)
17942 {
17943   while (1)
17944     {
17945       tree name, decl;
17946
17947       name = cp_parser_id_expression (parser, /*template_p=*/false,
17948                                       /*check_dependency_p=*/true,
17949                                       /*template_p=*/NULL,
17950                                       /*declarator_p=*/false,
17951                                       /*optional_p=*/false);
17952       if (name == error_mark_node)
17953         goto skip_comma;
17954
17955       decl = cp_parser_lookup_name_simple (parser, name);
17956       if (decl == error_mark_node)
17957         cp_parser_name_lookup_error (parser, name, decl, NULL);
17958       else if (kind != 0)
17959         {
17960           tree u = build_omp_clause (kind);
17961           OMP_CLAUSE_DECL (u) = decl;
17962           OMP_CLAUSE_CHAIN (u) = list;
17963           list = u;
17964         }
17965       else
17966         list = tree_cons (decl, NULL_TREE, list);
17967
17968     get_comma:
17969       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17970         break;
17971       cp_lexer_consume_token (parser->lexer);
17972     }
17973
17974   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17975     {
17976       int ending;
17977
17978       /* Try to resync to an unnested comma.  Copied from
17979          cp_parser_parenthesized_expression_list.  */
17980     skip_comma:
17981       ending = cp_parser_skip_to_closing_parenthesis (parser,
17982                                                       /*recovering=*/true,
17983                                                       /*or_comma=*/true,
17984                                                       /*consume_paren=*/true);
17985       if (ending < 0)
17986         goto get_comma;
17987     }
17988
17989   return list;
17990 }
17991
17992 /* Similarly, but expect leading and trailing parenthesis.  This is a very
17993    common case for omp clauses.  */
17994
17995 static tree
17996 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
17997 {
17998   if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17999     return cp_parser_omp_var_list_no_open (parser, kind, list);
18000   return list;
18001 }
18002
18003 /* OpenMP 2.5:
18004    default ( shared | none ) */
18005
18006 static tree
18007 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18008 {
18009   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18010   tree c;
18011
18012   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18013     return list;
18014   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18015     {
18016       tree id = cp_lexer_peek_token (parser->lexer)->value;
18017       const char *p = IDENTIFIER_POINTER (id);
18018
18019       switch (p[0])
18020         {
18021         case 'n':
18022           if (strcmp ("none", p) != 0)
18023             goto invalid_kind;
18024           kind = OMP_CLAUSE_DEFAULT_NONE;
18025           break;
18026
18027         case 's':
18028           if (strcmp ("shared", p) != 0)
18029             goto invalid_kind;
18030           kind = OMP_CLAUSE_DEFAULT_SHARED;
18031           break;
18032
18033         default:
18034           goto invalid_kind;
18035         }
18036
18037       cp_lexer_consume_token (parser->lexer);
18038     }
18039   else
18040     {
18041     invalid_kind:
18042       cp_parser_error (parser, "expected %<none%> or %<shared%>");
18043     }
18044
18045   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18046     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18047                                            /*or_comma=*/false,
18048                                            /*consume_paren=*/true);
18049
18050   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18051     return list;
18052
18053   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18054   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18055   OMP_CLAUSE_CHAIN (c) = list;
18056   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18057
18058   return c;
18059 }
18060
18061 /* OpenMP 2.5:
18062    if ( expression ) */
18063
18064 static tree
18065 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18066 {
18067   tree t, c;
18068
18069   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18070     return list;
18071
18072   t = cp_parser_condition (parser);
18073
18074   if (t == error_mark_node
18075       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18076     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18077                                            /*or_comma=*/false,
18078                                            /*consume_paren=*/true);
18079
18080   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18081
18082   c = build_omp_clause (OMP_CLAUSE_IF);
18083   OMP_CLAUSE_IF_EXPR (c) = t;
18084   OMP_CLAUSE_CHAIN (c) = list;
18085
18086   return c;
18087 }
18088
18089 /* OpenMP 2.5:
18090    nowait */
18091
18092 static tree
18093 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18094 {
18095   tree c;
18096
18097   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18098
18099   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18100   OMP_CLAUSE_CHAIN (c) = list;
18101   return c;
18102 }
18103
18104 /* OpenMP 2.5:
18105    num_threads ( expression ) */
18106
18107 static tree
18108 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18109 {
18110   tree t, c;
18111
18112   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18113     return list;
18114
18115   t = cp_parser_expression (parser, false);
18116
18117   if (t == error_mark_node
18118       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18119     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18120                                            /*or_comma=*/false,
18121                                            /*consume_paren=*/true);
18122
18123   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18124
18125   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18126   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18127   OMP_CLAUSE_CHAIN (c) = list;
18128
18129   return c;
18130 }
18131
18132 /* OpenMP 2.5:
18133    ordered */
18134
18135 static tree
18136 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18137 {
18138   tree c;
18139
18140   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18141
18142   c = build_omp_clause (OMP_CLAUSE_ORDERED);
18143   OMP_CLAUSE_CHAIN (c) = list;
18144   return c;
18145 }
18146
18147 /* OpenMP 2.5:
18148    reduction ( reduction-operator : variable-list )
18149
18150    reduction-operator:
18151      One of: + * - & ^ | && || */
18152
18153 static tree
18154 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18155 {
18156   enum tree_code code;
18157   tree nlist, c;
18158
18159   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18160     return list;
18161
18162   switch (cp_lexer_peek_token (parser->lexer)->type)
18163     {
18164     case CPP_PLUS:
18165       code = PLUS_EXPR;
18166       break;
18167     case CPP_MULT:
18168       code = MULT_EXPR;
18169       break;
18170     case CPP_MINUS:
18171       code = MINUS_EXPR;
18172       break;
18173     case CPP_AND:
18174       code = BIT_AND_EXPR;
18175       break;
18176     case CPP_XOR:
18177       code = BIT_XOR_EXPR;
18178       break;
18179     case CPP_OR:
18180       code = BIT_IOR_EXPR;
18181       break;
18182     case CPP_AND_AND:
18183       code = TRUTH_ANDIF_EXPR;
18184       break;
18185     case CPP_OR_OR:
18186       code = TRUTH_ORIF_EXPR;
18187       break;
18188     default:
18189       cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18190     resync_fail:
18191       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18192                                              /*or_comma=*/false,
18193                                              /*consume_paren=*/true);
18194       return list;
18195     }
18196   cp_lexer_consume_token (parser->lexer);
18197
18198   if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18199     goto resync_fail;
18200
18201   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18202   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18203     OMP_CLAUSE_REDUCTION_CODE (c) = code;
18204
18205   return nlist;
18206 }
18207
18208 /* OpenMP 2.5:
18209    schedule ( schedule-kind )
18210    schedule ( schedule-kind , expression )
18211
18212    schedule-kind:
18213      static | dynamic | guided | runtime  */
18214
18215 static tree
18216 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18217 {
18218   tree c, t;
18219
18220   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18221     return list;
18222
18223   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18224
18225   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18226     {
18227       tree id = cp_lexer_peek_token (parser->lexer)->value;
18228       const char *p = IDENTIFIER_POINTER (id);
18229
18230       switch (p[0])
18231         {
18232         case 'd':
18233           if (strcmp ("dynamic", p) != 0)
18234             goto invalid_kind;
18235           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18236           break;
18237
18238         case 'g':
18239           if (strcmp ("guided", p) != 0)
18240             goto invalid_kind;
18241           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18242           break;
18243
18244         case 'r':
18245           if (strcmp ("runtime", p) != 0)
18246             goto invalid_kind;
18247           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18248           break;
18249
18250         default:
18251           goto invalid_kind;
18252         }
18253     }
18254   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18255     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18256   else
18257     goto invalid_kind;
18258   cp_lexer_consume_token (parser->lexer);
18259
18260   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18261     {
18262       cp_lexer_consume_token (parser->lexer);
18263
18264       t = cp_parser_assignment_expression (parser, false);
18265
18266       if (t == error_mark_node)
18267         goto resync_fail;
18268       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18269         error ("schedule %<runtime%> does not take "
18270                "a %<chunk_size%> parameter");
18271       else
18272         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18273
18274       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18275         goto resync_fail;
18276     }
18277   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18278     goto resync_fail;
18279
18280   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18281   OMP_CLAUSE_CHAIN (c) = list;
18282   return c;
18283
18284  invalid_kind:
18285   cp_parser_error (parser, "invalid schedule kind");
18286  resync_fail:
18287   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18288                                          /*or_comma=*/false,
18289                                          /*consume_paren=*/true);
18290   return list;
18291 }
18292
18293 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
18294    is a bitmask in MASK.  Return the list of clauses found; the result
18295    of clause default goes in *pdefault.  */
18296
18297 static tree
18298 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18299                            const char *where, cp_token *pragma_tok)
18300 {
18301   tree clauses = NULL;
18302
18303   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18304     {
18305       pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18306       const char *c_name;
18307       tree prev = clauses;
18308
18309       switch (c_kind)
18310         {
18311         case PRAGMA_OMP_CLAUSE_COPYIN:
18312           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18313           c_name = "copyin";
18314           break;
18315         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18316           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18317                                             clauses);
18318           c_name = "copyprivate";
18319           break;
18320         case PRAGMA_OMP_CLAUSE_DEFAULT:
18321           clauses = cp_parser_omp_clause_default (parser, clauses);
18322           c_name = "default";
18323           break;
18324         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18325           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18326                                             clauses);
18327           c_name = "firstprivate";
18328           break;
18329         case PRAGMA_OMP_CLAUSE_IF:
18330           clauses = cp_parser_omp_clause_if (parser, clauses);
18331           c_name = "if";
18332           break;
18333         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18334           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18335                                             clauses);
18336           c_name = "lastprivate";
18337           break;
18338         case PRAGMA_OMP_CLAUSE_NOWAIT:
18339           clauses = cp_parser_omp_clause_nowait (parser, clauses);
18340           c_name = "nowait";
18341           break;
18342         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18343           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18344           c_name = "num_threads";
18345           break;
18346         case PRAGMA_OMP_CLAUSE_ORDERED:
18347           clauses = cp_parser_omp_clause_ordered (parser, clauses);
18348           c_name = "ordered";
18349           break;
18350         case PRAGMA_OMP_CLAUSE_PRIVATE:
18351           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18352                                             clauses);
18353           c_name = "private";
18354           break;
18355         case PRAGMA_OMP_CLAUSE_REDUCTION:
18356           clauses = cp_parser_omp_clause_reduction (parser, clauses);
18357           c_name = "reduction";
18358           break;
18359         case PRAGMA_OMP_CLAUSE_SCHEDULE:
18360           clauses = cp_parser_omp_clause_schedule (parser, clauses);
18361           c_name = "schedule";
18362           break;
18363         case PRAGMA_OMP_CLAUSE_SHARED:
18364           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18365                                             clauses);
18366           c_name = "shared";
18367           break;
18368         default:
18369           cp_parser_error (parser, "expected %<#pragma omp%> clause");
18370           goto saw_error;
18371         }
18372
18373       if (((mask >> c_kind) & 1) == 0)
18374         {
18375           /* Remove the invalid clause(s) from the list to avoid
18376              confusing the rest of the compiler.  */
18377           clauses = prev;
18378           error ("%qs is not valid for %qs", c_name, where);
18379         }
18380     }
18381  saw_error:
18382   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18383   return finish_omp_clauses (clauses);
18384 }
18385
18386 /* OpenMP 2.5:
18387    structured-block:
18388      statement
18389
18390    In practice, we're also interested in adding the statement to an
18391    outer node.  So it is convenient if we work around the fact that
18392    cp_parser_statement calls add_stmt.  */
18393
18394 static unsigned
18395 cp_parser_begin_omp_structured_block (cp_parser *parser)
18396 {
18397   unsigned save = parser->in_statement;
18398
18399   /* Only move the values to IN_OMP_BLOCK if they weren't false.
18400      This preserves the "not within loop or switch" style error messages
18401      for nonsense cases like
18402         void foo() {
18403         #pragma omp single
18404           break;
18405         }
18406   */
18407   if (parser->in_statement)
18408     parser->in_statement = IN_OMP_BLOCK;
18409
18410   return save;
18411 }
18412
18413 static void
18414 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18415 {
18416   parser->in_statement = save;
18417 }
18418
18419 static tree
18420 cp_parser_omp_structured_block (cp_parser *parser)
18421 {
18422   tree stmt = begin_omp_structured_block ();
18423   unsigned int save = cp_parser_begin_omp_structured_block (parser);
18424
18425   cp_parser_statement (parser, NULL_TREE, false);
18426
18427   cp_parser_end_omp_structured_block (parser, save);
18428   return finish_omp_structured_block (stmt);
18429 }
18430
18431 /* OpenMP 2.5:
18432    # pragma omp atomic new-line
18433      expression-stmt
18434
18435    expression-stmt:
18436      x binop= expr | x++ | ++x | x-- | --x
18437    binop:
18438      +, *, -, /, &, ^, |, <<, >>
18439
18440   where x is an lvalue expression with scalar type.  */
18441
18442 static void
18443 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18444 {
18445   tree lhs, rhs;
18446   enum tree_code code;
18447
18448   cp_parser_require_pragma_eol (parser, pragma_tok);
18449
18450   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18451                                     /*cast_p=*/false);
18452   switch (TREE_CODE (lhs))
18453     {
18454     case ERROR_MARK:
18455       goto saw_error;
18456
18457     case PREINCREMENT_EXPR:
18458     case POSTINCREMENT_EXPR:
18459       lhs = TREE_OPERAND (lhs, 0);
18460       code = PLUS_EXPR;
18461       rhs = integer_one_node;
18462       break;
18463
18464     case PREDECREMENT_EXPR:
18465     case POSTDECREMENT_EXPR:
18466       lhs = TREE_OPERAND (lhs, 0);
18467       code = MINUS_EXPR;
18468       rhs = integer_one_node;
18469       break;
18470
18471     default:
18472       switch (cp_lexer_peek_token (parser->lexer)->type)
18473         {
18474         case CPP_MULT_EQ:
18475           code = MULT_EXPR;
18476           break;
18477         case CPP_DIV_EQ:
18478           code = TRUNC_DIV_EXPR;
18479           break;
18480         case CPP_PLUS_EQ:
18481           code = PLUS_EXPR;
18482           break;
18483         case CPP_MINUS_EQ:
18484           code = MINUS_EXPR;
18485           break;
18486         case CPP_LSHIFT_EQ:
18487           code = LSHIFT_EXPR;
18488           break;
18489         case CPP_RSHIFT_EQ:
18490           code = RSHIFT_EXPR;
18491           break;
18492         case CPP_AND_EQ:
18493           code = BIT_AND_EXPR;
18494           break;
18495         case CPP_OR_EQ:
18496           code = BIT_IOR_EXPR;
18497           break;
18498         case CPP_XOR_EQ:
18499           code = BIT_XOR_EXPR;
18500           break;
18501         default:
18502           cp_parser_error (parser,
18503                            "invalid operator for %<#pragma omp atomic%>");
18504           goto saw_error;
18505         }
18506       cp_lexer_consume_token (parser->lexer);
18507
18508       rhs = cp_parser_expression (parser, false);
18509       if (rhs == error_mark_node)
18510         goto saw_error;
18511       break;
18512     }
18513   finish_omp_atomic (code, lhs, rhs);
18514   cp_parser_consume_semicolon_at_end_of_statement (parser);
18515   return;
18516
18517  saw_error:
18518   cp_parser_skip_to_end_of_block_or_statement (parser);
18519 }
18520
18521
18522 /* OpenMP 2.5:
18523    # pragma omp barrier new-line  */
18524
18525 static void
18526 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18527 {
18528   cp_parser_require_pragma_eol (parser, pragma_tok);
18529   finish_omp_barrier ();
18530 }
18531
18532 /* OpenMP 2.5:
18533    # pragma omp critical [(name)] new-line
18534      structured-block  */
18535
18536 static tree
18537 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18538 {
18539   tree stmt, name = NULL;
18540
18541   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18542     {
18543       cp_lexer_consume_token (parser->lexer);
18544
18545       name = cp_parser_identifier (parser);
18546
18547       if (name == error_mark_node
18548           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18549         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18550                                                /*or_comma=*/false,
18551                                                /*consume_paren=*/true);
18552       if (name == error_mark_node)
18553         name = NULL;
18554     }
18555   cp_parser_require_pragma_eol (parser, pragma_tok);
18556
18557   stmt = cp_parser_omp_structured_block (parser);
18558   return c_finish_omp_critical (stmt, name);
18559 }
18560
18561 /* OpenMP 2.5:
18562    # pragma omp flush flush-vars[opt] new-line
18563
18564    flush-vars:
18565      ( variable-list ) */
18566
18567 static void
18568 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18569 {
18570   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18571     (void) cp_parser_omp_var_list (parser, 0, NULL);
18572   cp_parser_require_pragma_eol (parser, pragma_tok);
18573
18574   finish_omp_flush ();
18575 }
18576
18577 /* Parse the restricted form of the for statment allowed by OpenMP.  */
18578
18579 static tree
18580 cp_parser_omp_for_loop (cp_parser *parser)
18581 {
18582   tree init, cond, incr, body, decl, pre_body;
18583   location_t loc;
18584
18585   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18586     {
18587       cp_parser_error (parser, "for statement expected");
18588       return NULL;
18589     }
18590   loc = cp_lexer_consume_token (parser->lexer)->location;
18591   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18592     return NULL;
18593
18594   init = decl = NULL;
18595   pre_body = push_stmt_list ();
18596   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18597     {
18598       cp_decl_specifier_seq type_specifiers;
18599
18600       /* First, try to parse as an initialized declaration.  See
18601          cp_parser_condition, from whence the bulk of this is copied.  */
18602
18603       cp_parser_parse_tentatively (parser);
18604       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18605                                     &type_specifiers);
18606       if (!cp_parser_error_occurred (parser))
18607         {
18608           tree asm_specification, attributes;
18609           cp_declarator *declarator;
18610
18611           declarator = cp_parser_declarator (parser,
18612                                              CP_PARSER_DECLARATOR_NAMED,
18613                                              /*ctor_dtor_or_conv_p=*/NULL,
18614                                              /*parenthesized_p=*/NULL,
18615                                              /*member_p=*/false);
18616           attributes = cp_parser_attributes_opt (parser);
18617           asm_specification = cp_parser_asm_specification_opt (parser);
18618
18619           cp_parser_require (parser, CPP_EQ, "`='");
18620           if (cp_parser_parse_definitely (parser))
18621             {
18622               tree pushed_scope;
18623
18624               decl = start_decl (declarator, &type_specifiers,
18625                                  /*initialized_p=*/false, attributes,
18626                                  /*prefix_attributes=*/NULL_TREE,
18627                                  &pushed_scope);
18628
18629               init = cp_parser_assignment_expression (parser, false);
18630
18631               cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18632                               asm_specification, LOOKUP_ONLYCONVERTING);
18633
18634               if (pushed_scope)
18635                 pop_scope (pushed_scope);
18636             }
18637         }
18638       else
18639         cp_parser_abort_tentative_parse (parser);
18640
18641       /* If parsing as an initialized declaration failed, try again as
18642          a simple expression.  */
18643       if (decl == NULL)
18644         init = cp_parser_expression (parser, false);
18645     }
18646   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18647   pre_body = pop_stmt_list (pre_body);
18648
18649   cond = NULL;
18650   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18651     cond = cp_parser_condition (parser);
18652   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18653
18654   incr = NULL;
18655   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18656     incr = cp_parser_expression (parser, false);
18657
18658   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18659     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18660                                            /*or_comma=*/false,
18661                                            /*consume_paren=*/true);
18662
18663   /* Note that we saved the original contents of this flag when we entered
18664      the structured block, and so we don't need to re-save it here.  */
18665   parser->in_statement = IN_OMP_FOR;
18666
18667   /* Note that the grammar doesn't call for a structured block here,
18668      though the loop as a whole is a structured block.  */
18669   body = push_stmt_list ();
18670   cp_parser_statement (parser, NULL_TREE, false);
18671   body = pop_stmt_list (body);
18672
18673   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18674 }
18675
18676 /* OpenMP 2.5:
18677    #pragma omp for for-clause[optseq] new-line
18678      for-loop  */
18679
18680 #define OMP_FOR_CLAUSE_MASK                             \
18681         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18682         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18683         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
18684         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
18685         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
18686         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
18687         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18688
18689 static tree
18690 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18691 {
18692   tree clauses, sb, ret;
18693   unsigned int save;
18694
18695   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18696                                        "#pragma omp for", pragma_tok);
18697
18698   sb = begin_omp_structured_block ();
18699   save = cp_parser_begin_omp_structured_block (parser);
18700
18701   ret = cp_parser_omp_for_loop (parser);
18702   if (ret)
18703     OMP_FOR_CLAUSES (ret) = clauses;
18704
18705   cp_parser_end_omp_structured_block (parser, save);
18706   add_stmt (finish_omp_structured_block (sb));
18707
18708   return ret;
18709 }
18710
18711 /* OpenMP 2.5:
18712    # pragma omp master new-line
18713      structured-block  */
18714
18715 static tree
18716 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18717 {
18718   cp_parser_require_pragma_eol (parser, pragma_tok);
18719   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18720 }
18721
18722 /* OpenMP 2.5:
18723    # pragma omp ordered new-line
18724      structured-block  */
18725
18726 static tree
18727 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18728 {
18729   cp_parser_require_pragma_eol (parser, pragma_tok);
18730   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18731 }
18732
18733 /* OpenMP 2.5:
18734
18735    section-scope:
18736      { section-sequence }
18737
18738    section-sequence:
18739      section-directive[opt] structured-block
18740      section-sequence section-directive structured-block  */
18741
18742 static tree
18743 cp_parser_omp_sections_scope (cp_parser *parser)
18744 {
18745   tree stmt, substmt;
18746   bool error_suppress = false;
18747   cp_token *tok;
18748
18749   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18750     return NULL_TREE;
18751
18752   stmt = push_stmt_list ();
18753
18754   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18755     {
18756       unsigned save;
18757
18758       substmt = begin_omp_structured_block ();
18759       save = cp_parser_begin_omp_structured_block (parser);
18760
18761       while (1)
18762         {
18763           cp_parser_statement (parser, NULL_TREE, false);
18764
18765           tok = cp_lexer_peek_token (parser->lexer);
18766           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18767             break;
18768           if (tok->type == CPP_CLOSE_BRACE)
18769             break;
18770           if (tok->type == CPP_EOF)
18771             break;
18772         }
18773
18774       cp_parser_end_omp_structured_block (parser, save);
18775       substmt = finish_omp_structured_block (substmt);
18776       substmt = build1 (OMP_SECTION, void_type_node, substmt);
18777       add_stmt (substmt);
18778     }
18779
18780   while (1)
18781     {
18782       tok = cp_lexer_peek_token (parser->lexer);
18783       if (tok->type == CPP_CLOSE_BRACE)
18784         break;
18785       if (tok->type == CPP_EOF)
18786         break;
18787
18788       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18789         {
18790           cp_lexer_consume_token (parser->lexer);
18791           cp_parser_require_pragma_eol (parser, tok);
18792           error_suppress = false;
18793         }
18794       else if (!error_suppress)
18795         {
18796           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
18797           error_suppress = true;
18798         }
18799
18800       substmt = cp_parser_omp_structured_block (parser);
18801       substmt = build1 (OMP_SECTION, void_type_node, substmt);
18802       add_stmt (substmt);
18803     }
18804   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
18805
18806   substmt = pop_stmt_list (stmt);
18807
18808   stmt = make_node (OMP_SECTIONS);
18809   TREE_TYPE (stmt) = void_type_node;
18810   OMP_SECTIONS_BODY (stmt) = substmt;
18811
18812   add_stmt (stmt);
18813   return stmt;
18814 }
18815
18816 /* OpenMP 2.5:
18817    # pragma omp sections sections-clause[optseq] newline
18818      sections-scope  */
18819
18820 #define OMP_SECTIONS_CLAUSE_MASK                        \
18821         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18822         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18823         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
18824         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
18825         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18826
18827 static tree
18828 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
18829 {
18830   tree clauses, ret;
18831
18832   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
18833                                        "#pragma omp sections", pragma_tok);
18834
18835   ret = cp_parser_omp_sections_scope (parser);
18836   if (ret)
18837     OMP_SECTIONS_CLAUSES (ret) = clauses;
18838
18839   return ret;
18840 }
18841
18842 /* OpenMP 2.5:
18843    # pragma parallel parallel-clause new-line
18844    # pragma parallel for parallel-for-clause new-line
18845    # pragma parallel sections parallel-sections-clause new-line  */
18846
18847 #define OMP_PARALLEL_CLAUSE_MASK                        \
18848         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
18849         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18850         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18851         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
18852         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
18853         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
18854         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
18855         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18856
18857 static tree
18858 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
18859 {
18860   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
18861   const char *p_name = "#pragma omp parallel";
18862   tree stmt, clauses, par_clause, ws_clause, block;
18863   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
18864   unsigned int save;
18865
18866   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18867     {
18868       cp_lexer_consume_token (parser->lexer);
18869       p_kind = PRAGMA_OMP_PARALLEL_FOR;
18870       p_name = "#pragma omp parallel for";
18871       mask |= OMP_FOR_CLAUSE_MASK;
18872       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18873     }
18874   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18875     {
18876       tree id = cp_lexer_peek_token (parser->lexer)->value;
18877       const char *p = IDENTIFIER_POINTER (id);
18878       if (strcmp (p, "sections") == 0)
18879         {
18880           cp_lexer_consume_token (parser->lexer);
18881           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
18882           p_name = "#pragma omp parallel sections";
18883           mask |= OMP_SECTIONS_CLAUSE_MASK;
18884           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18885         }
18886     }
18887
18888   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
18889   block = begin_omp_parallel ();
18890   save = cp_parser_begin_omp_structured_block (parser);
18891
18892   switch (p_kind)
18893     {
18894     case PRAGMA_OMP_PARALLEL:
18895       cp_parser_already_scoped_statement (parser);
18896       par_clause = clauses;
18897       break;
18898
18899     case PRAGMA_OMP_PARALLEL_FOR:
18900       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18901       stmt = cp_parser_omp_for_loop (parser);
18902       if (stmt)
18903         OMP_FOR_CLAUSES (stmt) = ws_clause;
18904       break;
18905
18906     case PRAGMA_OMP_PARALLEL_SECTIONS:
18907       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18908       stmt = cp_parser_omp_sections_scope (parser);
18909       if (stmt)
18910         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
18911       break;
18912
18913     default:
18914       gcc_unreachable ();
18915     }
18916
18917   cp_parser_end_omp_structured_block (parser, save);
18918   stmt = finish_omp_parallel (par_clause, block);
18919   if (p_kind != PRAGMA_OMP_PARALLEL)
18920     OMP_PARALLEL_COMBINED (stmt) = 1;
18921   return stmt;
18922 }
18923
18924 /* OpenMP 2.5:
18925    # pragma omp single single-clause[optseq] new-line
18926      structured-block  */
18927
18928 #define OMP_SINGLE_CLAUSE_MASK                          \
18929         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18930         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18931         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
18932         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18933
18934 static tree
18935 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
18936 {
18937   tree stmt = make_node (OMP_SINGLE);
18938   TREE_TYPE (stmt) = void_type_node;
18939
18940   OMP_SINGLE_CLAUSES (stmt)
18941     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18942                                  "#pragma omp single", pragma_tok);
18943   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
18944
18945   return add_stmt (stmt);
18946 }
18947
18948 /* OpenMP 2.5:
18949    # pragma omp threadprivate (variable-list) */
18950
18951 static void
18952 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
18953 {
18954   tree vars;
18955
18956   vars = cp_parser_omp_var_list (parser, 0, NULL);
18957   cp_parser_require_pragma_eol (parser, pragma_tok);
18958
18959   if (!targetm.have_tls)
18960     sorry ("threadprivate variables not supported in this target");
18961
18962   finish_omp_threadprivate (vars);
18963 }
18964
18965 /* Main entry point to OpenMP statement pragmas.  */
18966
18967 static void
18968 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
18969 {
18970   tree stmt;
18971
18972   switch (pragma_tok->pragma_kind)
18973     {
18974     case PRAGMA_OMP_ATOMIC:
18975       cp_parser_omp_atomic (parser, pragma_tok);
18976       return;
18977     case PRAGMA_OMP_CRITICAL:
18978       stmt = cp_parser_omp_critical (parser, pragma_tok);
18979       break;
18980     case PRAGMA_OMP_FOR:
18981       stmt = cp_parser_omp_for (parser, pragma_tok);
18982       break;
18983     case PRAGMA_OMP_MASTER:
18984       stmt = cp_parser_omp_master (parser, pragma_tok);
18985       break;
18986     case PRAGMA_OMP_ORDERED:
18987       stmt = cp_parser_omp_ordered (parser, pragma_tok);
18988       break;
18989     case PRAGMA_OMP_PARALLEL:
18990       stmt = cp_parser_omp_parallel (parser, pragma_tok);
18991       break;
18992     case PRAGMA_OMP_SECTIONS:
18993       stmt = cp_parser_omp_sections (parser, pragma_tok);
18994       break;
18995     case PRAGMA_OMP_SINGLE:
18996       stmt = cp_parser_omp_single (parser, pragma_tok);
18997       break;
18998     default:
18999       gcc_unreachable ();
19000     }
19001
19002   if (stmt)
19003     SET_EXPR_LOCATION (stmt, pragma_tok->location);
19004 }
19005 \f
19006 /* The parser.  */
19007
19008 static GTY (()) cp_parser *the_parser;
19009
19010 \f
19011 /* Special handling for the first token or line in the file.  The first
19012    thing in the file might be #pragma GCC pch_preprocess, which loads a
19013    PCH file, which is a GC collection point.  So we need to handle this
19014    first pragma without benefit of an existing lexer structure.
19015
19016    Always returns one token to the caller in *FIRST_TOKEN.  This is
19017    either the true first token of the file, or the first token after
19018    the initial pragma.  */
19019
19020 static void
19021 cp_parser_initial_pragma (cp_token *first_token)
19022 {
19023   tree name = NULL;
19024
19025   cp_lexer_get_preprocessor_token (NULL, first_token);
19026   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19027     return;
19028
19029   cp_lexer_get_preprocessor_token (NULL, first_token);
19030   if (first_token->type == CPP_STRING)
19031     {
19032       name = first_token->value;
19033
19034       cp_lexer_get_preprocessor_token (NULL, first_token);
19035       if (first_token->type != CPP_PRAGMA_EOL)
19036         error ("junk at end of %<#pragma GCC pch_preprocess%>");
19037     }
19038   else
19039     error ("expected string literal");
19040
19041   /* Skip to the end of the pragma.  */
19042   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19043     cp_lexer_get_preprocessor_token (NULL, first_token);
19044
19045   /* Now actually load the PCH file.  */
19046   if (name)
19047     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19048
19049   /* Read one more token to return to our caller.  We have to do this
19050      after reading the PCH file in, since its pointers have to be
19051      live.  */
19052   cp_lexer_get_preprocessor_token (NULL, first_token);
19053 }
19054
19055 /* Normal parsing of a pragma token.  Here we can (and must) use the
19056    regular lexer.  */
19057
19058 static bool
19059 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19060 {
19061   cp_token *pragma_tok;
19062   unsigned int id;
19063
19064   pragma_tok = cp_lexer_consume_token (parser->lexer);
19065   gcc_assert (pragma_tok->type == CPP_PRAGMA);
19066   parser->lexer->in_pragma = true;
19067
19068   id = pragma_tok->pragma_kind;
19069   switch (id)
19070     {
19071     case PRAGMA_GCC_PCH_PREPROCESS:
19072       error ("%<#pragma GCC pch_preprocess%> must be first");
19073       break;
19074
19075     case PRAGMA_OMP_BARRIER:
19076       switch (context)
19077         {
19078         case pragma_compound:
19079           cp_parser_omp_barrier (parser, pragma_tok);
19080           return false;
19081         case pragma_stmt:
19082           error ("%<#pragma omp barrier%> may only be "
19083                  "used in compound statements");
19084           break;
19085         default:
19086           goto bad_stmt;
19087         }
19088       break;
19089
19090     case PRAGMA_OMP_FLUSH:
19091       switch (context)
19092         {
19093         case pragma_compound:
19094           cp_parser_omp_flush (parser, pragma_tok);
19095           return false;
19096         case pragma_stmt:
19097           error ("%<#pragma omp flush%> may only be "
19098                  "used in compound statements");
19099           break;
19100         default:
19101           goto bad_stmt;
19102         }
19103       break;
19104
19105     case PRAGMA_OMP_THREADPRIVATE:
19106       cp_parser_omp_threadprivate (parser, pragma_tok);
19107       return false;
19108
19109     case PRAGMA_OMP_ATOMIC:
19110     case PRAGMA_OMP_CRITICAL:
19111     case PRAGMA_OMP_FOR:
19112     case PRAGMA_OMP_MASTER:
19113     case PRAGMA_OMP_ORDERED:
19114     case PRAGMA_OMP_PARALLEL:
19115     case PRAGMA_OMP_SECTIONS:
19116     case PRAGMA_OMP_SINGLE:
19117       if (context == pragma_external)
19118         goto bad_stmt;
19119       cp_parser_omp_construct (parser, pragma_tok);
19120       return true;
19121
19122     case PRAGMA_OMP_SECTION:
19123       error ("%<#pragma omp section%> may only be used in "
19124              "%<#pragma omp sections%> construct");
19125       break;
19126
19127     default:
19128       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19129       c_invoke_pragma_handler (id);
19130       break;
19131
19132     bad_stmt:
19133       cp_parser_error (parser, "expected declaration specifiers");
19134       break;
19135     }
19136
19137   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19138   return false;
19139 }
19140
19141 /* The interface the pragma parsers have to the lexer.  */
19142
19143 enum cpp_ttype
19144 pragma_lex (tree *value)
19145 {
19146   cp_token *tok;
19147   enum cpp_ttype ret;
19148
19149   tok = cp_lexer_peek_token (the_parser->lexer);
19150
19151   ret = tok->type;
19152   *value = tok->value;
19153
19154   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19155     ret = CPP_EOF;
19156   else if (ret == CPP_STRING)
19157     *value = cp_parser_string_literal (the_parser, false, false);
19158   else
19159     {
19160       cp_lexer_consume_token (the_parser->lexer);
19161       if (ret == CPP_KEYWORD)
19162         ret = CPP_NAME;
19163     }
19164
19165   return ret;
19166 }
19167
19168 \f
19169 /* External interface.  */
19170
19171 /* Parse one entire translation unit.  */
19172
19173 void
19174 c_parse_file (void)
19175 {
19176   bool error_occurred;
19177   static bool already_called = false;
19178
19179   if (already_called)
19180     {
19181       sorry ("inter-module optimizations not implemented for C++");
19182       return;
19183     }
19184   already_called = true;
19185
19186   the_parser = cp_parser_new ();
19187   push_deferring_access_checks (flag_access_control
19188                                 ? dk_no_deferred : dk_no_check);
19189   error_occurred = cp_parser_translation_unit (the_parser);
19190   the_parser = NULL;
19191 }
19192
19193 /* This variable must be provided by every front end.  */
19194
19195 int yydebug;
19196
19197 #include "gt-cp-parser.h"