OSDN Git Service

2006-08-17 Paolo Bonzini <bonzini@gnu.org>
[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
1154   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1155   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1156
1157   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1158
1159   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1160
1161   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1162
1163   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1164
1165   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1166 };
1167
1168 /* The same as binops, but initialized by cp_parser_new so that
1169    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1170    for speed.  */
1171 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1172
1173 /* Constructors and destructors.  */
1174
1175 /* Construct a new context.  The context below this one on the stack
1176    is given by NEXT.  */
1177
1178 static cp_parser_context *
1179 cp_parser_context_new (cp_parser_context* next)
1180 {
1181   cp_parser_context *context;
1182
1183   /* Allocate the storage.  */
1184   if (cp_parser_context_free_list != NULL)
1185     {
1186       /* Pull the first entry from the free list.  */
1187       context = cp_parser_context_free_list;
1188       cp_parser_context_free_list = context->next;
1189       memset (context, 0, sizeof (*context));
1190     }
1191   else
1192     context = GGC_CNEW (cp_parser_context);
1193
1194   /* No errors have occurred yet in this context.  */
1195   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1196   /* If this is not the bottomost context, copy information that we
1197      need from the previous context.  */
1198   if (next)
1199     {
1200       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1201          expression, then we are parsing one in this context, too.  */
1202       context->object_type = next->object_type;
1203       /* Thread the stack.  */
1204       context->next = next;
1205     }
1206
1207   return context;
1208 }
1209
1210 /* The cp_parser structure represents the C++ parser.  */
1211
1212 typedef struct cp_parser GTY(())
1213 {
1214   /* The lexer from which we are obtaining tokens.  */
1215   cp_lexer *lexer;
1216
1217   /* The scope in which names should be looked up.  If NULL_TREE, then
1218      we look up names in the scope that is currently open in the
1219      source program.  If non-NULL, this is either a TYPE or
1220      NAMESPACE_DECL for the scope in which we should look.  It can
1221      also be ERROR_MARK, when we've parsed a bogus scope.
1222
1223      This value is not cleared automatically after a name is looked
1224      up, so we must be careful to clear it before starting a new look
1225      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1226      will look up `Z' in the scope of `X', rather than the current
1227      scope.)  Unfortunately, it is difficult to tell when name lookup
1228      is complete, because we sometimes peek at a token, look it up,
1229      and then decide not to consume it.   */
1230   tree scope;
1231
1232   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1233      last lookup took place.  OBJECT_SCOPE is used if an expression
1234      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1235      respectively.  QUALIFYING_SCOPE is used for an expression of the
1236      form "X::Y"; it refers to X.  */
1237   tree object_scope;
1238   tree qualifying_scope;
1239
1240   /* A stack of parsing contexts.  All but the bottom entry on the
1241      stack will be tentative contexts.
1242
1243      We parse tentatively in order to determine which construct is in
1244      use in some situations.  For example, in order to determine
1245      whether a statement is an expression-statement or a
1246      declaration-statement we parse it tentatively as a
1247      declaration-statement.  If that fails, we then reparse the same
1248      token stream as an expression-statement.  */
1249   cp_parser_context *context;
1250
1251   /* True if we are parsing GNU C++.  If this flag is not set, then
1252      GNU extensions are not recognized.  */
1253   bool allow_gnu_extensions_p;
1254
1255   /* TRUE if the `>' token should be interpreted as the greater-than
1256      operator.  FALSE if it is the end of a template-id or
1257      template-parameter-list.  */
1258   bool greater_than_is_operator_p;
1259
1260   /* TRUE if default arguments are allowed within a parameter list
1261      that starts at this point. FALSE if only a gnu extension makes
1262      them permissible.  */
1263   bool default_arg_ok_p;
1264
1265   /* TRUE if we are parsing an integral constant-expression.  See
1266      [expr.const] for a precise definition.  */
1267   bool integral_constant_expression_p;
1268
1269   /* TRUE if we are parsing an integral constant-expression -- but a
1270      non-constant expression should be permitted as well.  This flag
1271      is used when parsing an array bound so that GNU variable-length
1272      arrays are tolerated.  */
1273   bool allow_non_integral_constant_expression_p;
1274
1275   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1276      been seen that makes the expression non-constant.  */
1277   bool non_integral_constant_expression_p;
1278
1279   /* TRUE if local variable names and `this' are forbidden in the
1280      current context.  */
1281   bool local_variables_forbidden_p;
1282
1283   /* TRUE if the declaration we are parsing is part of a
1284      linkage-specification of the form `extern string-literal
1285      declaration'.  */
1286   bool in_unbraced_linkage_specification_p;
1287
1288   /* TRUE if we are presently parsing a declarator, after the
1289      direct-declarator.  */
1290   bool in_declarator_p;
1291
1292   /* TRUE if we are presently parsing a template-argument-list.  */
1293   bool in_template_argument_list_p;
1294
1295   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1296      to IN_OMP_BLOCK if parsing OpenMP structured block and
1297      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1298      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1299      iteration-statement, OpenMP block or loop within that switch.  */
1300 #define IN_SWITCH_STMT          1
1301 #define IN_ITERATION_STMT       2
1302 #define IN_OMP_BLOCK            4
1303 #define IN_OMP_FOR              8
1304   unsigned char in_statement;
1305
1306   /* TRUE if we are presently parsing the body of a switch statement.
1307      Note that this doesn't quite overlap with in_statement above.
1308      The difference relates to giving the right sets of error messages:
1309      "case not in switch" vs "break statement used with OpenMP...".  */
1310   bool in_switch_statement_p;
1311
1312   /* TRUE if we are parsing a type-id in an expression context.  In
1313      such a situation, both "type (expr)" and "type (type)" are valid
1314      alternatives.  */
1315   bool in_type_id_in_expr_p;
1316
1317   /* TRUE if we are currently in a header file where declarations are
1318      implicitly extern "C".  */
1319   bool implicit_extern_c;
1320
1321   /* TRUE if strings in expressions should be translated to the execution
1322      character set.  */
1323   bool translate_strings_p;
1324
1325   /* If non-NULL, then we are parsing a construct where new type
1326      definitions are not permitted.  The string stored here will be
1327      issued as an error message if a type is defined.  */
1328   const char *type_definition_forbidden_message;
1329
1330   /* A list of lists. The outer list is a stack, used for member
1331      functions of local classes. At each level there are two sub-list,
1332      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1333      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1334      TREE_VALUE's. The functions are chained in reverse declaration
1335      order.
1336
1337      The TREE_PURPOSE sublist contains those functions with default
1338      arguments that need post processing, and the TREE_VALUE sublist
1339      contains those functions with definitions that need post
1340      processing.
1341
1342      These lists can only be processed once the outermost class being
1343      defined is complete.  */
1344   tree unparsed_functions_queues;
1345
1346   /* The number of classes whose definitions are currently in
1347      progress.  */
1348   unsigned num_classes_being_defined;
1349
1350   /* The number of template parameter lists that apply directly to the
1351      current declaration.  */
1352   unsigned num_template_parameter_lists;
1353 } cp_parser;
1354
1355 /* Prototypes.  */
1356
1357 /* Constructors and destructors.  */
1358
1359 static cp_parser *cp_parser_new
1360   (void);
1361
1362 /* Routines to parse various constructs.
1363
1364    Those that return `tree' will return the error_mark_node (rather
1365    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1366    Sometimes, they will return an ordinary node if error-recovery was
1367    attempted, even though a parse error occurred.  So, to check
1368    whether or not a parse error occurred, you should always use
1369    cp_parser_error_occurred.  If the construct is optional (indicated
1370    either by an `_opt' in the name of the function that does the
1371    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1372    the construct is not present.  */
1373
1374 /* Lexical conventions [gram.lex]  */
1375
1376 static tree cp_parser_identifier
1377   (cp_parser *);
1378 static tree cp_parser_string_literal
1379   (cp_parser *, bool, bool);
1380
1381 /* Basic concepts [gram.basic]  */
1382
1383 static bool cp_parser_translation_unit
1384   (cp_parser *);
1385
1386 /* Expressions [gram.expr]  */
1387
1388 static tree cp_parser_primary_expression
1389   (cp_parser *, bool, bool, bool, cp_id_kind *);
1390 static tree cp_parser_id_expression
1391   (cp_parser *, bool, bool, bool *, bool, bool);
1392 static tree cp_parser_unqualified_id
1393   (cp_parser *, bool, bool, bool, bool);
1394 static tree cp_parser_nested_name_specifier_opt
1395   (cp_parser *, bool, bool, bool, bool);
1396 static tree cp_parser_nested_name_specifier
1397   (cp_parser *, bool, bool, bool, bool);
1398 static tree cp_parser_class_or_namespace_name
1399   (cp_parser *, bool, bool, bool, bool, bool);
1400 static tree cp_parser_postfix_expression
1401   (cp_parser *, bool, bool);
1402 static tree cp_parser_postfix_open_square_expression
1403   (cp_parser *, tree, bool);
1404 static tree cp_parser_postfix_dot_deref_expression
1405   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1406 static tree cp_parser_parenthesized_expression_list
1407   (cp_parser *, bool, bool, bool *);
1408 static void cp_parser_pseudo_destructor_name
1409   (cp_parser *, tree *, tree *);
1410 static tree cp_parser_unary_expression
1411   (cp_parser *, bool, bool);
1412 static enum tree_code cp_parser_unary_operator
1413   (cp_token *);
1414 static tree cp_parser_new_expression
1415   (cp_parser *);
1416 static tree cp_parser_new_placement
1417   (cp_parser *);
1418 static tree cp_parser_new_type_id
1419   (cp_parser *, tree *);
1420 static cp_declarator *cp_parser_new_declarator_opt
1421   (cp_parser *);
1422 static cp_declarator *cp_parser_direct_new_declarator
1423   (cp_parser *);
1424 static tree cp_parser_new_initializer
1425   (cp_parser *);
1426 static tree cp_parser_delete_expression
1427   (cp_parser *);
1428 static tree cp_parser_cast_expression
1429   (cp_parser *, bool, bool);
1430 static tree cp_parser_binary_expression
1431   (cp_parser *, bool);
1432 static tree cp_parser_question_colon_clause
1433   (cp_parser *, tree);
1434 static tree cp_parser_assignment_expression
1435   (cp_parser *, bool);
1436 static enum tree_code cp_parser_assignment_operator_opt
1437   (cp_parser *);
1438 static tree cp_parser_expression
1439   (cp_parser *, bool);
1440 static tree cp_parser_constant_expression
1441   (cp_parser *, bool, bool *);
1442 static tree cp_parser_builtin_offsetof
1443   (cp_parser *);
1444
1445 /* Statements [gram.stmt.stmt]  */
1446
1447 static void cp_parser_statement
1448   (cp_parser *, tree, bool);
1449 static tree cp_parser_labeled_statement
1450   (cp_parser *, tree, bool);
1451 static tree cp_parser_expression_statement
1452   (cp_parser *, tree);
1453 static tree cp_parser_compound_statement
1454   (cp_parser *, tree, bool);
1455 static void cp_parser_statement_seq_opt
1456   (cp_parser *, tree);
1457 static tree cp_parser_selection_statement
1458   (cp_parser *);
1459 static tree cp_parser_condition
1460   (cp_parser *);
1461 static tree cp_parser_iteration_statement
1462   (cp_parser *);
1463 static void cp_parser_for_init_statement
1464   (cp_parser *);
1465 static tree cp_parser_jump_statement
1466   (cp_parser *);
1467 static void cp_parser_declaration_statement
1468   (cp_parser *);
1469
1470 static tree cp_parser_implicitly_scoped_statement
1471   (cp_parser *);
1472 static void cp_parser_already_scoped_statement
1473   (cp_parser *);
1474
1475 /* Declarations [gram.dcl.dcl] */
1476
1477 static void cp_parser_declaration_seq_opt
1478   (cp_parser *);
1479 static void cp_parser_declaration
1480   (cp_parser *);
1481 static void cp_parser_block_declaration
1482   (cp_parser *, bool);
1483 static void cp_parser_simple_declaration
1484   (cp_parser *, bool);
1485 static void cp_parser_decl_specifier_seq
1486   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1487 static tree cp_parser_storage_class_specifier_opt
1488   (cp_parser *);
1489 static tree cp_parser_function_specifier_opt
1490   (cp_parser *, cp_decl_specifier_seq *);
1491 static tree cp_parser_type_specifier
1492   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1493    int *, bool *);
1494 static tree cp_parser_simple_type_specifier
1495   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1496 static tree cp_parser_type_name
1497   (cp_parser *);
1498 static tree cp_parser_elaborated_type_specifier
1499   (cp_parser *, bool, bool);
1500 static tree cp_parser_enum_specifier
1501   (cp_parser *);
1502 static void cp_parser_enumerator_list
1503   (cp_parser *, tree);
1504 static void cp_parser_enumerator_definition
1505   (cp_parser *, tree);
1506 static tree cp_parser_namespace_name
1507   (cp_parser *);
1508 static void cp_parser_namespace_definition
1509   (cp_parser *);
1510 static void cp_parser_namespace_body
1511   (cp_parser *);
1512 static tree cp_parser_qualified_namespace_specifier
1513   (cp_parser *);
1514 static void cp_parser_namespace_alias_definition
1515   (cp_parser *);
1516 static void cp_parser_using_declaration
1517   (cp_parser *);
1518 static void cp_parser_using_directive
1519   (cp_parser *);
1520 static void cp_parser_asm_definition
1521   (cp_parser *);
1522 static void cp_parser_linkage_specification
1523   (cp_parser *);
1524
1525 /* Declarators [gram.dcl.decl] */
1526
1527 static tree cp_parser_init_declarator
1528   (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
1529 static cp_declarator *cp_parser_declarator
1530   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1531 static cp_declarator *cp_parser_direct_declarator
1532   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1533 static enum tree_code cp_parser_ptr_operator
1534   (cp_parser *, tree *, cp_cv_quals *);
1535 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1536   (cp_parser *);
1537 static tree cp_parser_declarator_id
1538   (cp_parser *, bool);
1539 static tree cp_parser_type_id
1540   (cp_parser *);
1541 static void cp_parser_type_specifier_seq
1542   (cp_parser *, bool, cp_decl_specifier_seq *);
1543 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1544   (cp_parser *);
1545 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1546   (cp_parser *, bool *);
1547 static cp_parameter_declarator *cp_parser_parameter_declaration
1548   (cp_parser *, bool, bool *);
1549 static void cp_parser_function_body
1550   (cp_parser *);
1551 static tree cp_parser_initializer
1552   (cp_parser *, bool *, bool *);
1553 static tree cp_parser_initializer_clause
1554   (cp_parser *, bool *);
1555 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1556   (cp_parser *, bool *);
1557
1558 static bool cp_parser_ctor_initializer_opt_and_function_body
1559   (cp_parser *);
1560
1561 /* Classes [gram.class] */
1562
1563 static tree cp_parser_class_name
1564   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1565 static tree cp_parser_class_specifier
1566   (cp_parser *);
1567 static tree cp_parser_class_head
1568   (cp_parser *, bool *, tree *);
1569 static enum tag_types cp_parser_class_key
1570   (cp_parser *);
1571 static void cp_parser_member_specification_opt
1572   (cp_parser *);
1573 static void cp_parser_member_declaration
1574   (cp_parser *);
1575 static tree cp_parser_pure_specifier
1576   (cp_parser *);
1577 static tree cp_parser_constant_initializer
1578   (cp_parser *);
1579
1580 /* Derived classes [gram.class.derived] */
1581
1582 static tree cp_parser_base_clause
1583   (cp_parser *);
1584 static tree cp_parser_base_specifier
1585   (cp_parser *);
1586
1587 /* Special member functions [gram.special] */
1588
1589 static tree cp_parser_conversion_function_id
1590   (cp_parser *);
1591 static tree cp_parser_conversion_type_id
1592   (cp_parser *);
1593 static cp_declarator *cp_parser_conversion_declarator_opt
1594   (cp_parser *);
1595 static bool cp_parser_ctor_initializer_opt
1596   (cp_parser *);
1597 static void cp_parser_mem_initializer_list
1598   (cp_parser *);
1599 static tree cp_parser_mem_initializer
1600   (cp_parser *);
1601 static tree cp_parser_mem_initializer_id
1602   (cp_parser *);
1603
1604 /* Overloading [gram.over] */
1605
1606 static tree cp_parser_operator_function_id
1607   (cp_parser *);
1608 static tree cp_parser_operator
1609   (cp_parser *);
1610
1611 /* Templates [gram.temp] */
1612
1613 static void cp_parser_template_declaration
1614   (cp_parser *, bool);
1615 static tree cp_parser_template_parameter_list
1616   (cp_parser *);
1617 static tree cp_parser_template_parameter
1618   (cp_parser *, bool *);
1619 static tree cp_parser_type_parameter
1620   (cp_parser *);
1621 static tree cp_parser_template_id
1622   (cp_parser *, bool, bool, bool);
1623 static tree cp_parser_template_name
1624   (cp_parser *, bool, bool, bool, bool *);
1625 static tree cp_parser_template_argument_list
1626   (cp_parser *);
1627 static tree cp_parser_template_argument
1628   (cp_parser *);
1629 static void cp_parser_explicit_instantiation
1630   (cp_parser *);
1631 static void cp_parser_explicit_specialization
1632   (cp_parser *);
1633
1634 /* Exception handling [gram.exception] */
1635
1636 static tree cp_parser_try_block
1637   (cp_parser *);
1638 static bool cp_parser_function_try_block
1639   (cp_parser *);
1640 static void cp_parser_handler_seq
1641   (cp_parser *);
1642 static void cp_parser_handler
1643   (cp_parser *);
1644 static tree cp_parser_exception_declaration
1645   (cp_parser *);
1646 static tree cp_parser_throw_expression
1647   (cp_parser *);
1648 static tree cp_parser_exception_specification_opt
1649   (cp_parser *);
1650 static tree cp_parser_type_id_list
1651   (cp_parser *);
1652
1653 /* GNU Extensions */
1654
1655 static tree cp_parser_asm_specification_opt
1656   (cp_parser *);
1657 static tree cp_parser_asm_operand_list
1658   (cp_parser *);
1659 static tree cp_parser_asm_clobber_list
1660   (cp_parser *);
1661 static tree cp_parser_attributes_opt
1662   (cp_parser *);
1663 static tree cp_parser_attribute_list
1664   (cp_parser *);
1665 static bool cp_parser_extension_opt
1666   (cp_parser *, int *);
1667 static void cp_parser_label_declaration
1668   (cp_parser *);
1669
1670 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1671 static bool cp_parser_pragma
1672   (cp_parser *, enum pragma_context);
1673
1674 /* Objective-C++ Productions */
1675
1676 static tree cp_parser_objc_message_receiver
1677   (cp_parser *);
1678 static tree cp_parser_objc_message_args
1679   (cp_parser *);
1680 static tree cp_parser_objc_message_expression
1681   (cp_parser *);
1682 static tree cp_parser_objc_encode_expression
1683   (cp_parser *);
1684 static tree cp_parser_objc_defs_expression
1685   (cp_parser *);
1686 static tree cp_parser_objc_protocol_expression
1687   (cp_parser *);
1688 static tree cp_parser_objc_selector_expression
1689   (cp_parser *);
1690 static tree cp_parser_objc_expression
1691   (cp_parser *);
1692 static bool cp_parser_objc_selector_p
1693   (enum cpp_ttype);
1694 static tree cp_parser_objc_selector
1695   (cp_parser *);
1696 static tree cp_parser_objc_protocol_refs_opt
1697   (cp_parser *);
1698 static void cp_parser_objc_declaration
1699   (cp_parser *);
1700 static tree cp_parser_objc_statement
1701   (cp_parser *);
1702
1703 /* Utility Routines */
1704
1705 static tree cp_parser_lookup_name
1706   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1707 static tree cp_parser_lookup_name_simple
1708   (cp_parser *, tree);
1709 static tree cp_parser_maybe_treat_template_as_class
1710   (tree, bool);
1711 static bool cp_parser_check_declarator_template_parameters
1712   (cp_parser *, cp_declarator *);
1713 static bool cp_parser_check_template_parameters
1714   (cp_parser *, unsigned);
1715 static tree cp_parser_simple_cast_expression
1716   (cp_parser *);
1717 static tree cp_parser_global_scope_opt
1718   (cp_parser *, bool);
1719 static bool cp_parser_constructor_declarator_p
1720   (cp_parser *, bool);
1721 static tree cp_parser_function_definition_from_specifiers_and_declarator
1722   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1723 static tree cp_parser_function_definition_after_declarator
1724   (cp_parser *, bool);
1725 static void cp_parser_template_declaration_after_export
1726   (cp_parser *, bool);
1727 static void cp_parser_perform_template_parameter_access_checks
1728   (tree);
1729 static tree cp_parser_single_declaration
1730   (cp_parser *, tree, bool, bool *);
1731 static tree cp_parser_functional_cast
1732   (cp_parser *, tree);
1733 static tree cp_parser_save_member_function_body
1734   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1735 static tree cp_parser_enclosed_template_argument_list
1736   (cp_parser *);
1737 static void cp_parser_save_default_args
1738   (cp_parser *, tree);
1739 static void cp_parser_late_parsing_for_member
1740   (cp_parser *, tree);
1741 static void cp_parser_late_parsing_default_args
1742   (cp_parser *, tree);
1743 static tree cp_parser_sizeof_operand
1744   (cp_parser *, enum rid);
1745 static bool cp_parser_declares_only_class_p
1746   (cp_parser *);
1747 static void cp_parser_set_storage_class
1748   (cp_parser *, cp_decl_specifier_seq *, enum rid);
1749 static void cp_parser_set_decl_spec_type
1750   (cp_decl_specifier_seq *, tree, bool);
1751 static bool cp_parser_friend_p
1752   (const cp_decl_specifier_seq *);
1753 static cp_token *cp_parser_require
1754   (cp_parser *, enum cpp_ttype, const char *);
1755 static cp_token *cp_parser_require_keyword
1756   (cp_parser *, enum rid, const char *);
1757 static bool cp_parser_token_starts_function_definition_p
1758   (cp_token *);
1759 static bool cp_parser_next_token_starts_class_definition_p
1760   (cp_parser *);
1761 static bool cp_parser_next_token_ends_template_argument_p
1762   (cp_parser *);
1763 static bool cp_parser_nth_token_starts_template_argument_list_p
1764   (cp_parser *, size_t);
1765 static enum tag_types cp_parser_token_is_class_key
1766   (cp_token *);
1767 static void cp_parser_check_class_key
1768   (enum tag_types, tree type);
1769 static void cp_parser_check_access_in_redeclaration
1770   (tree type);
1771 static bool cp_parser_optional_template_keyword
1772   (cp_parser *);
1773 static void cp_parser_pre_parsed_nested_name_specifier
1774   (cp_parser *);
1775 static void cp_parser_cache_group
1776   (cp_parser *, enum cpp_ttype, unsigned);
1777 static void cp_parser_parse_tentatively
1778   (cp_parser *);
1779 static void cp_parser_commit_to_tentative_parse
1780   (cp_parser *);
1781 static void cp_parser_abort_tentative_parse
1782   (cp_parser *);
1783 static bool cp_parser_parse_definitely
1784   (cp_parser *);
1785 static inline bool cp_parser_parsing_tentatively
1786   (cp_parser *);
1787 static bool cp_parser_uncommitted_to_tentative_parse_p
1788   (cp_parser *);
1789 static void cp_parser_error
1790   (cp_parser *, const char *);
1791 static void cp_parser_name_lookup_error
1792   (cp_parser *, tree, tree, const char *);
1793 static bool cp_parser_simulate_error
1794   (cp_parser *);
1795 static void cp_parser_check_type_definition
1796   (cp_parser *);
1797 static void cp_parser_check_for_definition_in_return_type
1798   (cp_declarator *, tree);
1799 static void cp_parser_check_for_invalid_template_id
1800   (cp_parser *, tree);
1801 static bool cp_parser_non_integral_constant_expression
1802   (cp_parser *, const char *);
1803 static void cp_parser_diagnose_invalid_type_name
1804   (cp_parser *, tree, tree);
1805 static bool cp_parser_parse_and_diagnose_invalid_type_name
1806   (cp_parser *);
1807 static int cp_parser_skip_to_closing_parenthesis
1808   (cp_parser *, bool, bool, bool);
1809 static void cp_parser_skip_to_end_of_statement
1810   (cp_parser *);
1811 static void cp_parser_consume_semicolon_at_end_of_statement
1812   (cp_parser *);
1813 static void cp_parser_skip_to_end_of_block_or_statement
1814   (cp_parser *);
1815 static void cp_parser_skip_to_closing_brace
1816   (cp_parser *);
1817 static void cp_parser_skip_until_found
1818   (cp_parser *, enum cpp_ttype, const char *);
1819 static void cp_parser_skip_to_pragma_eol
1820   (cp_parser*, cp_token *);
1821 static bool cp_parser_error_occurred
1822   (cp_parser *);
1823 static bool cp_parser_allow_gnu_extensions_p
1824   (cp_parser *);
1825 static bool cp_parser_is_string_literal
1826   (cp_token *);
1827 static bool cp_parser_is_keyword
1828   (cp_token *, enum rid);
1829 static tree cp_parser_make_typename_type
1830   (cp_parser *, tree, tree);
1831
1832 /* Returns nonzero if we are parsing tentatively.  */
1833
1834 static inline bool
1835 cp_parser_parsing_tentatively (cp_parser* parser)
1836 {
1837   return parser->context->next != NULL;
1838 }
1839
1840 /* Returns nonzero if TOKEN is a string literal.  */
1841
1842 static bool
1843 cp_parser_is_string_literal (cp_token* token)
1844 {
1845   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1846 }
1847
1848 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1849
1850 static bool
1851 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1852 {
1853   return token->keyword == keyword;
1854 }
1855
1856 /* If not parsing tentatively, issue a diagnostic of the form
1857       FILE:LINE: MESSAGE before TOKEN
1858    where TOKEN is the next token in the input stream.  MESSAGE
1859    (specified by the caller) is usually of the form "expected
1860    OTHER-TOKEN".  */
1861
1862 static void
1863 cp_parser_error (cp_parser* parser, const char* message)
1864 {
1865   if (!cp_parser_simulate_error (parser))
1866     {
1867       cp_token *token = cp_lexer_peek_token (parser->lexer);
1868       /* This diagnostic makes more sense if it is tagged to the line
1869          of the token we just peeked at.  */
1870       cp_lexer_set_source_position_from_token (token);
1871
1872       if (token->type == CPP_PRAGMA)
1873         {
1874           error ("%<#pragma%> is not allowed here");
1875           cp_parser_skip_to_pragma_eol (parser, token);
1876           return;
1877         }
1878
1879       c_parse_error (message,
1880                      /* Because c_parser_error does not understand
1881                         CPP_KEYWORD, keywords are treated like
1882                         identifiers.  */
1883                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1884                      token->value);
1885     }
1886 }
1887
1888 /* Issue an error about name-lookup failing.  NAME is the
1889    IDENTIFIER_NODE DECL is the result of
1890    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1891    the thing that we hoped to find.  */
1892
1893 static void
1894 cp_parser_name_lookup_error (cp_parser* parser,
1895                              tree name,
1896                              tree decl,
1897                              const char* desired)
1898 {
1899   /* If name lookup completely failed, tell the user that NAME was not
1900      declared.  */
1901   if (decl == error_mark_node)
1902     {
1903       if (parser->scope && parser->scope != global_namespace)
1904         error ("%<%D::%D%> has not been declared",
1905                parser->scope, name);
1906       else if (parser->scope == global_namespace)
1907         error ("%<::%D%> has not been declared", name);
1908       else if (parser->object_scope
1909                && !CLASS_TYPE_P (parser->object_scope))
1910         error ("request for member %qD in non-class type %qT",
1911                name, parser->object_scope);
1912       else if (parser->object_scope)
1913         error ("%<%T::%D%> has not been declared",
1914                parser->object_scope, name);
1915       else
1916         error ("%qD has not been declared", name);
1917     }
1918   else if (parser->scope && parser->scope != global_namespace)
1919     error ("%<%D::%D%> %s", parser->scope, name, desired);
1920   else if (parser->scope == global_namespace)
1921     error ("%<::%D%> %s", name, desired);
1922   else
1923     error ("%qD %s", name, desired);
1924 }
1925
1926 /* If we are parsing tentatively, remember that an error has occurred
1927    during this tentative parse.  Returns true if the error was
1928    simulated; false if a message should be issued by the caller.  */
1929
1930 static bool
1931 cp_parser_simulate_error (cp_parser* parser)
1932 {
1933   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1934     {
1935       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1936       return true;
1937     }
1938   return false;
1939 }
1940
1941 /* Check for repeated decl-specifiers.  */
1942
1943 static void
1944 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
1945 {
1946   cp_decl_spec ds;
1947
1948   for (ds = ds_first; ds != ds_last; ++ds)
1949     {
1950       unsigned count = decl_specs->specs[(int)ds];
1951       if (count < 2)
1952         continue;
1953       /* The "long" specifier is a special case because of "long long".  */
1954       if (ds == ds_long)
1955         {
1956           if (count > 2)
1957             error ("%<long long long%> is too long for GCC");
1958           else if (pedantic && !in_system_header && warn_long_long)
1959             pedwarn ("ISO C++ does not support %<long long%>");
1960         }
1961       else if (count > 1)
1962         {
1963           static const char *const decl_spec_names[] = {
1964             "signed",
1965             "unsigned",
1966             "short",
1967             "long",
1968             "const",
1969             "volatile",
1970             "restrict",
1971             "inline",
1972             "virtual",
1973             "explicit",
1974             "friend",
1975             "typedef",
1976             "__complex",
1977             "__thread"
1978           };
1979           error ("duplicate %qs", decl_spec_names[(int)ds]);
1980         }
1981     }
1982 }
1983
1984 /* This function is called when a type is defined.  If type
1985    definitions are forbidden at this point, an error message is
1986    issued.  */
1987
1988 static void
1989 cp_parser_check_type_definition (cp_parser* parser)
1990 {
1991   /* If types are forbidden here, issue a message.  */
1992   if (parser->type_definition_forbidden_message)
1993     /* Use `%s' to print the string in case there are any escape
1994        characters in the message.  */
1995     error ("%s", parser->type_definition_forbidden_message);
1996 }
1997
1998 /* This function is called when the DECLARATOR is processed.  The TYPE
1999    was a type defined in the decl-specifiers.  If it is invalid to
2000    define a type in the decl-specifiers for DECLARATOR, an error is
2001    issued.  */
2002
2003 static void
2004 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2005                                                tree type)
2006 {
2007   /* [dcl.fct] forbids type definitions in return types.
2008      Unfortunately, it's not easy to know whether or not we are
2009      processing a return type until after the fact.  */
2010   while (declarator
2011          && (declarator->kind == cdk_pointer
2012              || declarator->kind == cdk_reference
2013              || declarator->kind == cdk_ptrmem))
2014     declarator = declarator->declarator;
2015   if (declarator
2016       && declarator->kind == cdk_function)
2017     {
2018       error ("new types may not be defined in a return type");
2019       inform ("(perhaps a semicolon is missing after the definition of %qT)",
2020               type);
2021     }
2022 }
2023
2024 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2025    "<" in any valid C++ program.  If the next token is indeed "<",
2026    issue a message warning the user about what appears to be an
2027    invalid attempt to form a template-id.  */
2028
2029 static void
2030 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2031                                          tree type)
2032 {
2033   cp_token_position start = 0;
2034
2035   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2036     {
2037       if (TYPE_P (type))
2038         error ("%qT is not a template", type);
2039       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2040         error ("%qE is not a template", type);
2041       else
2042         error ("invalid template-id");
2043       /* Remember the location of the invalid "<".  */
2044       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2045         start = cp_lexer_token_position (parser->lexer, true);
2046       /* Consume the "<".  */
2047       cp_lexer_consume_token (parser->lexer);
2048       /* Parse the template arguments.  */
2049       cp_parser_enclosed_template_argument_list (parser);
2050       /* Permanently remove the invalid template arguments so that
2051          this error message is not issued again.  */
2052       if (start)
2053         cp_lexer_purge_tokens_after (parser->lexer, start);
2054     }
2055 }
2056
2057 /* If parsing an integral constant-expression, issue an error message
2058    about the fact that THING appeared and return true.  Otherwise,
2059    return false.  In either case, set
2060    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2061
2062 static bool
2063 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2064                                             const char *thing)
2065 {
2066   parser->non_integral_constant_expression_p = true;
2067   if (parser->integral_constant_expression_p)
2068     {
2069       if (!parser->allow_non_integral_constant_expression_p)
2070         {
2071           error ("%s cannot appear in a constant-expression", thing);
2072           return true;
2073         }
2074     }
2075   return false;
2076 }
2077
2078 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2079    qualifying scope (or NULL, if none) for ID.  This function commits
2080    to the current active tentative parse, if any.  (Otherwise, the
2081    problematic construct might be encountered again later, resulting
2082    in duplicate error messages.)  */
2083
2084 static void
2085 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2086 {
2087   tree decl, old_scope;
2088   /* Try to lookup the identifier.  */
2089   old_scope = parser->scope;
2090   parser->scope = scope;
2091   decl = cp_parser_lookup_name_simple (parser, id);
2092   parser->scope = old_scope;
2093   /* If the lookup found a template-name, it means that the user forgot
2094   to specify an argument list. Emit a useful error message.  */
2095   if (TREE_CODE (decl) == TEMPLATE_DECL)
2096     error ("invalid use of template-name %qE without an argument list",
2097       decl);
2098   else if (!parser->scope)
2099     {
2100       /* Issue an error message.  */
2101       error ("%qE does not name a type", id);
2102       /* If we're in a template class, it's possible that the user was
2103          referring to a type from a base class.  For example:
2104
2105            template <typename T> struct A { typedef T X; };
2106            template <typename T> struct B : public A<T> { X x; };
2107
2108          The user should have said "typename A<T>::X".  */
2109       if (processing_template_decl && current_class_type
2110           && TYPE_BINFO (current_class_type))
2111         {
2112           tree b;
2113
2114           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2115                b;
2116                b = TREE_CHAIN (b))
2117             {
2118               tree base_type = BINFO_TYPE (b);
2119               if (CLASS_TYPE_P (base_type)
2120                   && dependent_type_p (base_type))
2121                 {
2122                   tree field;
2123                   /* Go from a particular instantiation of the
2124                      template (which will have an empty TYPE_FIELDs),
2125                      to the main version.  */
2126                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2127                   for (field = TYPE_FIELDS (base_type);
2128                        field;
2129                        field = TREE_CHAIN (field))
2130                     if (TREE_CODE (field) == TYPE_DECL
2131                         && DECL_NAME (field) == id)
2132                       {
2133                         inform ("(perhaps %<typename %T::%E%> was intended)",
2134                                 BINFO_TYPE (b), id);
2135                         break;
2136                       }
2137                   if (field)
2138                     break;
2139                 }
2140             }
2141         }
2142     }
2143   /* Here we diagnose qualified-ids where the scope is actually correct,
2144      but the identifier does not resolve to a valid type name.  */
2145   else if (parser->scope != error_mark_node)
2146     {
2147       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2148         error ("%qE in namespace %qE does not name a type",
2149                id, parser->scope);
2150       else if (TYPE_P (parser->scope))
2151         error ("%qE in class %qT does not name a type", id, parser->scope);
2152       else
2153         gcc_unreachable ();
2154     }
2155   cp_parser_commit_to_tentative_parse (parser);
2156 }
2157
2158 /* Check for a common situation where a type-name should be present,
2159    but is not, and issue a sensible error message.  Returns true if an
2160    invalid type-name was detected.
2161
2162    The situation handled by this function are variable declarations of the
2163    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2164    Usually, `ID' should name a type, but if we got here it means that it
2165    does not. We try to emit the best possible error message depending on
2166    how exactly the id-expression looks like.  */
2167
2168 static bool
2169 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2170 {
2171   tree id;
2172
2173   cp_parser_parse_tentatively (parser);
2174   id = cp_parser_id_expression (parser,
2175                                 /*template_keyword_p=*/false,
2176                                 /*check_dependency_p=*/true,
2177                                 /*template_p=*/NULL,
2178                                 /*declarator_p=*/true,
2179                                 /*optional_p=*/false);
2180   /* After the id-expression, there should be a plain identifier,
2181      otherwise this is not a simple variable declaration. Also, if
2182      the scope is dependent, we cannot do much.  */
2183   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2184       || (parser->scope && TYPE_P (parser->scope)
2185           && dependent_type_p (parser->scope)))
2186     {
2187       cp_parser_abort_tentative_parse (parser);
2188       return false;
2189     }
2190   if (!cp_parser_parse_definitely (parser)
2191       || TREE_CODE (id) != IDENTIFIER_NODE)
2192     return false;
2193
2194   /* Emit a diagnostic for the invalid type.  */
2195   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2196   /* Skip to the end of the declaration; there's no point in
2197      trying to process it.  */
2198   cp_parser_skip_to_end_of_block_or_statement (parser);
2199   return true;
2200 }
2201
2202 /* Consume tokens up to, and including, the next non-nested closing `)'.
2203    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2204    are doing error recovery. Returns -1 if OR_COMMA is true and we
2205    found an unnested comma.  */
2206
2207 static int
2208 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2209                                        bool recovering,
2210                                        bool or_comma,
2211                                        bool consume_paren)
2212 {
2213   unsigned paren_depth = 0;
2214   unsigned brace_depth = 0;
2215
2216   if (recovering && !or_comma
2217       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2218     return 0;
2219
2220   while (true)
2221     {
2222       cp_token * token = cp_lexer_peek_token (parser->lexer);
2223
2224       switch (token->type)
2225         {
2226         case CPP_EOF:
2227         case CPP_PRAGMA_EOL:
2228           /* If we've run out of tokens, then there is no closing `)'.  */
2229           return 0;
2230
2231         case CPP_SEMICOLON:
2232           /* This matches the processing in skip_to_end_of_statement.  */
2233           if (!brace_depth)
2234             return 0;
2235           break;
2236
2237         case CPP_OPEN_BRACE:
2238           ++brace_depth;
2239           break;
2240         case CPP_CLOSE_BRACE:
2241           if (!brace_depth--)
2242             return 0;
2243           break;
2244
2245         case CPP_COMMA:
2246           if (recovering && or_comma && !brace_depth && !paren_depth)
2247             return -1;
2248           break;
2249
2250         case CPP_OPEN_PAREN:
2251           if (!brace_depth)
2252             ++paren_depth;
2253           break;
2254
2255         case CPP_CLOSE_PAREN:
2256           if (!brace_depth && !paren_depth--)
2257             {
2258               if (consume_paren)
2259                 cp_lexer_consume_token (parser->lexer);
2260               return 1;
2261             }
2262           break;
2263
2264         default:
2265           break;
2266         }
2267
2268       /* Consume the token.  */
2269       cp_lexer_consume_token (parser->lexer);
2270     }
2271 }
2272
2273 /* Consume tokens until we reach the end of the current statement.
2274    Normally, that will be just before consuming a `;'.  However, if a
2275    non-nested `}' comes first, then we stop before consuming that.  */
2276
2277 static void
2278 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2279 {
2280   unsigned nesting_depth = 0;
2281
2282   while (true)
2283     {
2284       cp_token *token = cp_lexer_peek_token (parser->lexer);
2285
2286       switch (token->type)
2287         {
2288         case CPP_EOF:
2289         case CPP_PRAGMA_EOL:
2290           /* If we've run out of tokens, stop.  */
2291           return;
2292
2293         case CPP_SEMICOLON:
2294           /* If the next token is a `;', we have reached the end of the
2295              statement.  */
2296           if (!nesting_depth)
2297             return;
2298           break;
2299
2300         case CPP_CLOSE_BRACE:
2301           /* If this is a non-nested '}', stop before consuming it.
2302              That way, when confronted with something like:
2303
2304                { 3 + }
2305
2306              we stop before consuming the closing '}', even though we
2307              have not yet reached a `;'.  */
2308           if (nesting_depth == 0)
2309             return;
2310
2311           /* If it is the closing '}' for a block that we have
2312              scanned, stop -- but only after consuming the token.
2313              That way given:
2314
2315                 void f g () { ... }
2316                 typedef int I;
2317
2318              we will stop after the body of the erroneously declared
2319              function, but before consuming the following `typedef'
2320              declaration.  */
2321           if (--nesting_depth == 0)
2322             {
2323               cp_lexer_consume_token (parser->lexer);
2324               return;
2325             }
2326
2327         case CPP_OPEN_BRACE:
2328           ++nesting_depth;
2329           break;
2330
2331         default:
2332           break;
2333         }
2334
2335       /* Consume the token.  */
2336       cp_lexer_consume_token (parser->lexer);
2337     }
2338 }
2339
2340 /* This function is called at the end of a statement or declaration.
2341    If the next token is a semicolon, it is consumed; otherwise, error
2342    recovery is attempted.  */
2343
2344 static void
2345 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2346 {
2347   /* Look for the trailing `;'.  */
2348   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2349     {
2350       /* If there is additional (erroneous) input, skip to the end of
2351          the statement.  */
2352       cp_parser_skip_to_end_of_statement (parser);
2353       /* If the next token is now a `;', consume it.  */
2354       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2355         cp_lexer_consume_token (parser->lexer);
2356     }
2357 }
2358
2359 /* Skip tokens until we have consumed an entire block, or until we
2360    have consumed a non-nested `;'.  */
2361
2362 static void
2363 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2364 {
2365   int nesting_depth = 0;
2366
2367   while (nesting_depth >= 0)
2368     {
2369       cp_token *token = cp_lexer_peek_token (parser->lexer);
2370
2371       switch (token->type)
2372         {
2373         case CPP_EOF:
2374         case CPP_PRAGMA_EOL:
2375           /* If we've run out of tokens, stop.  */
2376           return;
2377
2378         case CPP_SEMICOLON:
2379           /* Stop if this is an unnested ';'. */
2380           if (!nesting_depth)
2381             nesting_depth = -1;
2382           break;
2383
2384         case CPP_CLOSE_BRACE:
2385           /* Stop if this is an unnested '}', or closes the outermost
2386              nesting level.  */
2387           nesting_depth--;
2388           if (!nesting_depth)
2389             nesting_depth = -1;
2390           break;
2391
2392         case CPP_OPEN_BRACE:
2393           /* Nest. */
2394           nesting_depth++;
2395           break;
2396
2397         default:
2398           break;
2399         }
2400
2401       /* Consume the token.  */
2402       cp_lexer_consume_token (parser->lexer);
2403     }
2404 }
2405
2406 /* Skip tokens until a non-nested closing curly brace is the next
2407    token.  */
2408
2409 static void
2410 cp_parser_skip_to_closing_brace (cp_parser *parser)
2411 {
2412   unsigned nesting_depth = 0;
2413
2414   while (true)
2415     {
2416       cp_token *token = cp_lexer_peek_token (parser->lexer);
2417
2418       switch (token->type)
2419         {
2420         case CPP_EOF:
2421         case CPP_PRAGMA_EOL:
2422           /* If we've run out of tokens, stop.  */
2423           return;
2424
2425         case CPP_CLOSE_BRACE:
2426           /* If the next token is a non-nested `}', then we have reached
2427              the end of the current block.  */
2428           if (nesting_depth-- == 0)
2429             return;
2430           break;
2431
2432         case CPP_OPEN_BRACE:
2433           /* If it the next token is a `{', then we are entering a new
2434              block.  Consume the entire block.  */
2435           ++nesting_depth;
2436           break;
2437
2438         default:
2439           break;
2440         }
2441
2442       /* Consume the token.  */
2443       cp_lexer_consume_token (parser->lexer);
2444     }
2445 }
2446
2447 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2448    parameter is the PRAGMA token, allowing us to purge the entire pragma
2449    sequence.  */
2450
2451 static void
2452 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2453 {
2454   cp_token *token;
2455
2456   parser->lexer->in_pragma = false;
2457
2458   do
2459     token = cp_lexer_consume_token (parser->lexer);
2460   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2461
2462   /* Ensure that the pragma is not parsed again.  */
2463   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2464 }
2465
2466 /* Require pragma end of line, resyncing with it as necessary.  The
2467    arguments are as for cp_parser_skip_to_pragma_eol.  */
2468
2469 static void
2470 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2471 {
2472   parser->lexer->in_pragma = false;
2473   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2474     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2475 }
2476
2477 /* This is a simple wrapper around make_typename_type. When the id is
2478    an unresolved identifier node, we can provide a superior diagnostic
2479    using cp_parser_diagnose_invalid_type_name.  */
2480
2481 static tree
2482 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2483 {
2484   tree result;
2485   if (TREE_CODE (id) == IDENTIFIER_NODE)
2486     {
2487       result = make_typename_type (scope, id, typename_type,
2488                                    /*complain=*/tf_none);
2489       if (result == error_mark_node)
2490         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2491       return result;
2492     }
2493   return make_typename_type (scope, id, typename_type, tf_error);
2494 }
2495
2496
2497 /* Create a new C++ parser.  */
2498
2499 static cp_parser *
2500 cp_parser_new (void)
2501 {
2502   cp_parser *parser;
2503   cp_lexer *lexer;
2504   unsigned i;
2505
2506   /* cp_lexer_new_main is called before calling ggc_alloc because
2507      cp_lexer_new_main might load a PCH file.  */
2508   lexer = cp_lexer_new_main ();
2509
2510   /* Initialize the binops_by_token so that we can get the tree
2511      directly from the token.  */
2512   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2513     binops_by_token[binops[i].token_type] = binops[i];
2514
2515   parser = GGC_CNEW (cp_parser);
2516   parser->lexer = lexer;
2517   parser->context = cp_parser_context_new (NULL);
2518
2519   /* For now, we always accept GNU extensions.  */
2520   parser->allow_gnu_extensions_p = 1;
2521
2522   /* The `>' token is a greater-than operator, not the end of a
2523      template-id.  */
2524   parser->greater_than_is_operator_p = true;
2525
2526   parser->default_arg_ok_p = true;
2527
2528   /* We are not parsing a constant-expression.  */
2529   parser->integral_constant_expression_p = false;
2530   parser->allow_non_integral_constant_expression_p = false;
2531   parser->non_integral_constant_expression_p = false;
2532
2533   /* Local variable names are not forbidden.  */
2534   parser->local_variables_forbidden_p = false;
2535
2536   /* We are not processing an `extern "C"' declaration.  */
2537   parser->in_unbraced_linkage_specification_p = false;
2538
2539   /* We are not processing a declarator.  */
2540   parser->in_declarator_p = false;
2541
2542   /* We are not processing a template-argument-list.  */
2543   parser->in_template_argument_list_p = false;
2544
2545   /* We are not in an iteration statement.  */
2546   parser->in_statement = 0;
2547
2548   /* We are not in a switch statement.  */
2549   parser->in_switch_statement_p = false;
2550
2551   /* We are not parsing a type-id inside an expression.  */
2552   parser->in_type_id_in_expr_p = false;
2553
2554   /* Declarations aren't implicitly extern "C".  */
2555   parser->implicit_extern_c = false;
2556
2557   /* String literals should be translated to the execution character set.  */
2558   parser->translate_strings_p = true;
2559
2560   /* The unparsed function queue is empty.  */
2561   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2562
2563   /* There are no classes being defined.  */
2564   parser->num_classes_being_defined = 0;
2565
2566   /* No template parameters apply.  */
2567   parser->num_template_parameter_lists = 0;
2568
2569   return parser;
2570 }
2571
2572 /* Create a cp_lexer structure which will emit the tokens in CACHE
2573    and push it onto the parser's lexer stack.  This is used for delayed
2574    parsing of in-class method bodies and default arguments, and should
2575    not be confused with tentative parsing.  */
2576 static void
2577 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2578 {
2579   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2580   lexer->next = parser->lexer;
2581   parser->lexer = lexer;
2582
2583   /* Move the current source position to that of the first token in the
2584      new lexer.  */
2585   cp_lexer_set_source_position_from_token (lexer->next_token);
2586 }
2587
2588 /* Pop the top lexer off the parser stack.  This is never used for the
2589    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2590 static void
2591 cp_parser_pop_lexer (cp_parser *parser)
2592 {
2593   cp_lexer *lexer = parser->lexer;
2594   parser->lexer = lexer->next;
2595   cp_lexer_destroy (lexer);
2596
2597   /* Put the current source position back where it was before this
2598      lexer was pushed.  */
2599   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2600 }
2601
2602 /* Lexical conventions [gram.lex]  */
2603
2604 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2605    identifier.  */
2606
2607 static tree
2608 cp_parser_identifier (cp_parser* parser)
2609 {
2610   cp_token *token;
2611
2612   /* Look for the identifier.  */
2613   token = cp_parser_require (parser, CPP_NAME, "identifier");
2614   /* Return the value.  */
2615   return token ? token->value : error_mark_node;
2616 }
2617
2618 /* Parse a sequence of adjacent string constants.  Returns a
2619    TREE_STRING representing the combined, nul-terminated string
2620    constant.  If TRANSLATE is true, translate the string to the
2621    execution character set.  If WIDE_OK is true, a wide string is
2622    invalid here.
2623
2624    C++98 [lex.string] says that if a narrow string literal token is
2625    adjacent to a wide string literal token, the behavior is undefined.
2626    However, C99 6.4.5p4 says that this results in a wide string literal.
2627    We follow C99 here, for consistency with the C front end.
2628
2629    This code is largely lifted from lex_string() in c-lex.c.
2630
2631    FUTURE: ObjC++ will need to handle @-strings here.  */
2632 static tree
2633 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2634 {
2635   tree value;
2636   bool wide = false;
2637   size_t count;
2638   struct obstack str_ob;
2639   cpp_string str, istr, *strs;
2640   cp_token *tok;
2641
2642   tok = cp_lexer_peek_token (parser->lexer);
2643   if (!cp_parser_is_string_literal (tok))
2644     {
2645       cp_parser_error (parser, "expected string-literal");
2646       return error_mark_node;
2647     }
2648
2649   /* Try to avoid the overhead of creating and destroying an obstack
2650      for the common case of just one string.  */
2651   if (!cp_parser_is_string_literal
2652       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2653     {
2654       cp_lexer_consume_token (parser->lexer);
2655
2656       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2657       str.len = TREE_STRING_LENGTH (tok->value);
2658       count = 1;
2659       if (tok->type == CPP_WSTRING)
2660         wide = true;
2661
2662       strs = &str;
2663     }
2664   else
2665     {
2666       gcc_obstack_init (&str_ob);
2667       count = 0;
2668
2669       do
2670         {
2671           cp_lexer_consume_token (parser->lexer);
2672           count++;
2673           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2674           str.len = TREE_STRING_LENGTH (tok->value);
2675           if (tok->type == CPP_WSTRING)
2676             wide = true;
2677
2678           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2679
2680           tok = cp_lexer_peek_token (parser->lexer);
2681         }
2682       while (cp_parser_is_string_literal (tok));
2683
2684       strs = (cpp_string *) obstack_finish (&str_ob);
2685     }
2686
2687   if (wide && !wide_ok)
2688     {
2689       cp_parser_error (parser, "a wide string is invalid in this context");
2690       wide = false;
2691     }
2692
2693   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2694       (parse_in, strs, count, &istr, wide))
2695     {
2696       value = build_string (istr.len, (char *)istr.text);
2697       free ((void *)istr.text);
2698
2699       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2700       value = fix_string_type (value);
2701     }
2702   else
2703     /* cpp_interpret_string has issued an error.  */
2704     value = error_mark_node;
2705
2706   if (count > 1)
2707     obstack_free (&str_ob, 0);
2708
2709   return value;
2710 }
2711
2712
2713 /* Basic concepts [gram.basic]  */
2714
2715 /* Parse a translation-unit.
2716
2717    translation-unit:
2718      declaration-seq [opt]
2719
2720    Returns TRUE if all went well.  */
2721
2722 static bool
2723 cp_parser_translation_unit (cp_parser* parser)
2724 {
2725   /* The address of the first non-permanent object on the declarator
2726      obstack.  */
2727   static void *declarator_obstack_base;
2728
2729   bool success;
2730
2731   /* Create the declarator obstack, if necessary.  */
2732   if (!cp_error_declarator)
2733     {
2734       gcc_obstack_init (&declarator_obstack);
2735       /* Create the error declarator.  */
2736       cp_error_declarator = make_declarator (cdk_error);
2737       /* Create the empty parameter list.  */
2738       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2739       /* Remember where the base of the declarator obstack lies.  */
2740       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2741     }
2742
2743   cp_parser_declaration_seq_opt (parser);
2744
2745   /* If there are no tokens left then all went well.  */
2746   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2747     {
2748       /* Get rid of the token array; we don't need it any more.  */
2749       cp_lexer_destroy (parser->lexer);
2750       parser->lexer = NULL;
2751
2752       /* This file might have been a context that's implicitly extern
2753          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2754       if (parser->implicit_extern_c)
2755         {
2756           pop_lang_context ();
2757           parser->implicit_extern_c = false;
2758         }
2759
2760       /* Finish up.  */
2761       finish_translation_unit ();
2762
2763       success = true;
2764     }
2765   else
2766     {
2767       cp_parser_error (parser, "expected declaration");
2768       success = false;
2769     }
2770
2771   /* Make sure the declarator obstack was fully cleaned up.  */
2772   gcc_assert (obstack_next_free (&declarator_obstack)
2773               == declarator_obstack_base);
2774
2775   /* All went well.  */
2776   return success;
2777 }
2778
2779 /* Expressions [gram.expr] */
2780
2781 /* Parse a primary-expression.
2782
2783    primary-expression:
2784      literal
2785      this
2786      ( expression )
2787      id-expression
2788
2789    GNU Extensions:
2790
2791    primary-expression:
2792      ( compound-statement )
2793      __builtin_va_arg ( assignment-expression , type-id )
2794      __builtin_offsetof ( type-id , offsetof-expression )
2795
2796    Objective-C++ Extension:
2797
2798    primary-expression:
2799      objc-expression
2800
2801    literal:
2802      __null
2803
2804    ADDRESS_P is true iff this expression was immediately preceded by
2805    "&" and therefore might denote a pointer-to-member.  CAST_P is true
2806    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
2807    true iff this expression is a template argument.
2808
2809    Returns a representation of the expression.  Upon return, *IDK
2810    indicates what kind of id-expression (if any) was present.  */
2811
2812 static tree
2813 cp_parser_primary_expression (cp_parser *parser,
2814                               bool address_p,
2815                               bool cast_p,
2816                               bool template_arg_p,
2817                               cp_id_kind *idk)
2818 {
2819   cp_token *token;
2820
2821   /* Assume the primary expression is not an id-expression.  */
2822   *idk = CP_ID_KIND_NONE;
2823
2824   /* Peek at the next token.  */
2825   token = cp_lexer_peek_token (parser->lexer);
2826   switch (token->type)
2827     {
2828       /* literal:
2829            integer-literal
2830            character-literal
2831            floating-literal
2832            string-literal
2833            boolean-literal  */
2834     case CPP_CHAR:
2835     case CPP_WCHAR:
2836     case CPP_NUMBER:
2837       token = cp_lexer_consume_token (parser->lexer);
2838       /* Floating-point literals are only allowed in an integral
2839          constant expression if they are cast to an integral or
2840          enumeration type.  */
2841       if (TREE_CODE (token->value) == REAL_CST
2842           && parser->integral_constant_expression_p
2843           && pedantic)
2844         {
2845           /* CAST_P will be set even in invalid code like "int(2.7 +
2846              ...)".   Therefore, we have to check that the next token
2847              is sure to end the cast.  */
2848           if (cast_p)
2849             {
2850               cp_token *next_token;
2851
2852               next_token = cp_lexer_peek_token (parser->lexer);
2853               if (/* The comma at the end of an
2854                      enumerator-definition.  */
2855                   next_token->type != CPP_COMMA
2856                   /* The curly brace at the end of an enum-specifier.  */
2857                   && next_token->type != CPP_CLOSE_BRACE
2858                   /* The end of a statement.  */
2859                   && next_token->type != CPP_SEMICOLON
2860                   /* The end of the cast-expression.  */
2861                   && next_token->type != CPP_CLOSE_PAREN
2862                   /* The end of an array bound.  */
2863                   && next_token->type != CPP_CLOSE_SQUARE
2864                   /* The closing ">" in a template-argument-list.  */
2865                   && (next_token->type != CPP_GREATER
2866                       || parser->greater_than_is_operator_p))
2867                 cast_p = false;
2868             }
2869
2870           /* If we are within a cast, then the constraint that the
2871              cast is to an integral or enumeration type will be
2872              checked at that point.  If we are not within a cast, then
2873              this code is invalid.  */
2874           if (!cast_p)
2875             cp_parser_non_integral_constant_expression
2876               (parser, "floating-point literal");
2877         }
2878       return token->value;
2879
2880     case CPP_STRING:
2881     case CPP_WSTRING:
2882       /* ??? Should wide strings be allowed when parser->translate_strings_p
2883          is false (i.e. in attributes)?  If not, we can kill the third
2884          argument to cp_parser_string_literal.  */
2885       return cp_parser_string_literal (parser,
2886                                        parser->translate_strings_p,
2887                                        true);
2888
2889     case CPP_OPEN_PAREN:
2890       {
2891         tree expr;
2892         bool saved_greater_than_is_operator_p;
2893
2894         /* Consume the `('.  */
2895         cp_lexer_consume_token (parser->lexer);
2896         /* Within a parenthesized expression, a `>' token is always
2897            the greater-than operator.  */
2898         saved_greater_than_is_operator_p
2899           = parser->greater_than_is_operator_p;
2900         parser->greater_than_is_operator_p = true;
2901         /* If we see `( { ' then we are looking at the beginning of
2902            a GNU statement-expression.  */
2903         if (cp_parser_allow_gnu_extensions_p (parser)
2904             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2905           {
2906             /* Statement-expressions are not allowed by the standard.  */
2907             if (pedantic)
2908               pedwarn ("ISO C++ forbids braced-groups within expressions");
2909
2910             /* And they're not allowed outside of a function-body; you
2911                cannot, for example, write:
2912
2913                  int i = ({ int j = 3; j + 1; });
2914
2915                at class or namespace scope.  */
2916             if (!at_function_scope_p ())
2917               error ("statement-expressions are allowed only inside functions");
2918             /* Start the statement-expression.  */
2919             expr = begin_stmt_expr ();
2920             /* Parse the compound-statement.  */
2921             cp_parser_compound_statement (parser, expr, false);
2922             /* Finish up.  */
2923             expr = finish_stmt_expr (expr, false);
2924           }
2925         else
2926           {
2927             /* Parse the parenthesized expression.  */
2928             expr = cp_parser_expression (parser, cast_p);
2929             /* Let the front end know that this expression was
2930                enclosed in parentheses. This matters in case, for
2931                example, the expression is of the form `A::B', since
2932                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2933                not.  */
2934             finish_parenthesized_expr (expr);
2935           }
2936         /* The `>' token might be the end of a template-id or
2937            template-parameter-list now.  */
2938         parser->greater_than_is_operator_p
2939           = saved_greater_than_is_operator_p;
2940         /* Consume the `)'.  */
2941         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2942           cp_parser_skip_to_end_of_statement (parser);
2943
2944         return expr;
2945       }
2946
2947     case CPP_KEYWORD:
2948       switch (token->keyword)
2949         {
2950           /* These two are the boolean literals.  */
2951         case RID_TRUE:
2952           cp_lexer_consume_token (parser->lexer);
2953           return boolean_true_node;
2954         case RID_FALSE:
2955           cp_lexer_consume_token (parser->lexer);
2956           return boolean_false_node;
2957
2958           /* The `__null' literal.  */
2959         case RID_NULL:
2960           cp_lexer_consume_token (parser->lexer);
2961           return null_node;
2962
2963           /* Recognize the `this' keyword.  */
2964         case RID_THIS:
2965           cp_lexer_consume_token (parser->lexer);
2966           if (parser->local_variables_forbidden_p)
2967             {
2968               error ("%<this%> may not be used in this context");
2969               return error_mark_node;
2970             }
2971           /* Pointers cannot appear in constant-expressions.  */
2972           if (cp_parser_non_integral_constant_expression (parser,
2973                                                           "`this'"))
2974             return error_mark_node;
2975           return finish_this_expr ();
2976
2977           /* The `operator' keyword can be the beginning of an
2978              id-expression.  */
2979         case RID_OPERATOR:
2980           goto id_expression;
2981
2982         case RID_FUNCTION_NAME:
2983         case RID_PRETTY_FUNCTION_NAME:
2984         case RID_C99_FUNCTION_NAME:
2985           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2986              __func__ are the names of variables -- but they are
2987              treated specially.  Therefore, they are handled here,
2988              rather than relying on the generic id-expression logic
2989              below.  Grammatically, these names are id-expressions.
2990
2991              Consume the token.  */
2992           token = cp_lexer_consume_token (parser->lexer);
2993           /* Look up the name.  */
2994           return finish_fname (token->value);
2995
2996         case RID_VA_ARG:
2997           {
2998             tree expression;
2999             tree type;
3000
3001             /* The `__builtin_va_arg' construct is used to handle
3002                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3003             cp_lexer_consume_token (parser->lexer);
3004             /* Look for the opening `('.  */
3005             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3006             /* Now, parse the assignment-expression.  */
3007             expression = cp_parser_assignment_expression (parser,
3008                                                           /*cast_p=*/false);
3009             /* Look for the `,'.  */
3010             cp_parser_require (parser, CPP_COMMA, "`,'");
3011             /* Parse the type-id.  */
3012             type = cp_parser_type_id (parser);
3013             /* Look for the closing `)'.  */
3014             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3015             /* Using `va_arg' in a constant-expression is not
3016                allowed.  */
3017             if (cp_parser_non_integral_constant_expression (parser,
3018                                                             "`va_arg'"))
3019               return error_mark_node;
3020             return build_x_va_arg (expression, type);
3021           }
3022
3023         case RID_OFFSETOF:
3024           return cp_parser_builtin_offsetof (parser);
3025
3026           /* Objective-C++ expressions.  */
3027         case RID_AT_ENCODE:
3028         case RID_AT_PROTOCOL:
3029         case RID_AT_SELECTOR:
3030           return cp_parser_objc_expression (parser);
3031
3032         default:
3033           cp_parser_error (parser, "expected primary-expression");
3034           return error_mark_node;
3035         }
3036
3037       /* An id-expression can start with either an identifier, a
3038          `::' as the beginning of a qualified-id, or the "operator"
3039          keyword.  */
3040     case CPP_NAME:
3041     case CPP_SCOPE:
3042     case CPP_TEMPLATE_ID:
3043     case CPP_NESTED_NAME_SPECIFIER:
3044       {
3045         tree id_expression;
3046         tree decl;
3047         const char *error_msg;
3048         bool template_p;
3049         bool done;
3050
3051       id_expression:
3052         /* Parse the id-expression.  */
3053         id_expression
3054           = cp_parser_id_expression (parser,
3055                                      /*template_keyword_p=*/false,
3056                                      /*check_dependency_p=*/true,
3057                                      &template_p,
3058                                      /*declarator_p=*/false,
3059                                      /*optional_p=*/false);
3060         if (id_expression == error_mark_node)
3061           return error_mark_node;
3062         token = cp_lexer_peek_token (parser->lexer);
3063         done = (token->type != CPP_OPEN_SQUARE
3064                 && token->type != CPP_OPEN_PAREN
3065                 && token->type != CPP_DOT
3066                 && token->type != CPP_DEREF
3067                 && token->type != CPP_PLUS_PLUS
3068                 && token->type != CPP_MINUS_MINUS);
3069         /* If we have a template-id, then no further lookup is
3070            required.  If the template-id was for a template-class, we
3071            will sometimes have a TYPE_DECL at this point.  */
3072         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3073                  || TREE_CODE (id_expression) == TYPE_DECL)
3074           decl = id_expression;
3075         /* Look up the name.  */
3076         else
3077           {
3078             tree ambiguous_decls;
3079
3080             decl = cp_parser_lookup_name (parser, id_expression,
3081                                           none_type,
3082                                           template_p,
3083                                           /*is_namespace=*/false,
3084                                           /*check_dependency=*/true,
3085                                           &ambiguous_decls);
3086             /* If the lookup was ambiguous, an error will already have
3087                been issued.  */
3088             if (ambiguous_decls)
3089               return error_mark_node;
3090
3091             /* In Objective-C++, an instance variable (ivar) may be preferred
3092                to whatever cp_parser_lookup_name() found.  */
3093             decl = objc_lookup_ivar (decl, id_expression);
3094
3095             /* If name lookup gives us a SCOPE_REF, then the
3096                qualifying scope was dependent.  */
3097             if (TREE_CODE (decl) == SCOPE_REF)
3098               return decl;
3099             /* Check to see if DECL is a local variable in a context
3100                where that is forbidden.  */
3101             if (parser->local_variables_forbidden_p
3102                 && local_variable_p (decl))
3103               {
3104                 /* It might be that we only found DECL because we are
3105                    trying to be generous with pre-ISO scoping rules.
3106                    For example, consider:
3107
3108                      int i;
3109                      void g() {
3110                        for (int i = 0; i < 10; ++i) {}
3111                        extern void f(int j = i);
3112                      }
3113
3114                    Here, name look up will originally find the out
3115                    of scope `i'.  We need to issue a warning message,
3116                    but then use the global `i'.  */
3117                 decl = check_for_out_of_scope_variable (decl);
3118                 if (local_variable_p (decl))
3119                   {
3120                     error ("local variable %qD may not appear in this context",
3121                            decl);
3122                     return error_mark_node;
3123                   }
3124               }
3125           }
3126
3127         decl = (finish_id_expression
3128                 (id_expression, decl, parser->scope,
3129                  idk,
3130                  parser->integral_constant_expression_p,
3131                  parser->allow_non_integral_constant_expression_p,
3132                  &parser->non_integral_constant_expression_p,
3133                  template_p, done, address_p,
3134                  template_arg_p,
3135                  &error_msg));
3136         if (error_msg)
3137           cp_parser_error (parser, error_msg);
3138         return decl;
3139       }
3140
3141       /* Anything else is an error.  */
3142     default:
3143       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3144       if (c_dialect_objc ()
3145           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3146         return cp_parser_objc_expression (parser);
3147
3148       cp_parser_error (parser, "expected primary-expression");
3149       return error_mark_node;
3150     }
3151 }
3152
3153 /* Parse an id-expression.
3154
3155    id-expression:
3156      unqualified-id
3157      qualified-id
3158
3159    qualified-id:
3160      :: [opt] nested-name-specifier template [opt] unqualified-id
3161      :: identifier
3162      :: operator-function-id
3163      :: template-id
3164
3165    Return a representation of the unqualified portion of the
3166    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3167    a `::' or nested-name-specifier.
3168
3169    Often, if the id-expression was a qualified-id, the caller will
3170    want to make a SCOPE_REF to represent the qualified-id.  This
3171    function does not do this in order to avoid wastefully creating
3172    SCOPE_REFs when they are not required.
3173
3174    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3175    `template' keyword.
3176
3177    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3178    uninstantiated templates.
3179
3180    If *TEMPLATE_P is non-NULL, it is set to true iff the
3181    `template' keyword is used to explicitly indicate that the entity
3182    named is a template.
3183
3184    If DECLARATOR_P is true, the id-expression is appearing as part of
3185    a declarator, rather than as part of an expression.  */
3186
3187 static tree
3188 cp_parser_id_expression (cp_parser *parser,
3189                          bool template_keyword_p,
3190                          bool check_dependency_p,
3191                          bool *template_p,
3192                          bool declarator_p,
3193                          bool optional_p)
3194 {
3195   bool global_scope_p;
3196   bool nested_name_specifier_p;
3197
3198   /* Assume the `template' keyword was not used.  */
3199   if (template_p)
3200     *template_p = template_keyword_p;
3201
3202   /* Look for the optional `::' operator.  */
3203   global_scope_p
3204     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3205        != NULL_TREE);
3206   /* Look for the optional nested-name-specifier.  */
3207   nested_name_specifier_p
3208     = (cp_parser_nested_name_specifier_opt (parser,
3209                                             /*typename_keyword_p=*/false,
3210                                             check_dependency_p,
3211                                             /*type_p=*/false,
3212                                             declarator_p)
3213        != NULL_TREE);
3214   /* If there is a nested-name-specifier, then we are looking at
3215      the first qualified-id production.  */
3216   if (nested_name_specifier_p)
3217     {
3218       tree saved_scope;
3219       tree saved_object_scope;
3220       tree saved_qualifying_scope;
3221       tree unqualified_id;
3222       bool is_template;
3223
3224       /* See if the next token is the `template' keyword.  */
3225       if (!template_p)
3226         template_p = &is_template;
3227       *template_p = cp_parser_optional_template_keyword (parser);
3228       /* Name lookup we do during the processing of the
3229          unqualified-id might obliterate SCOPE.  */
3230       saved_scope = parser->scope;
3231       saved_object_scope = parser->object_scope;
3232       saved_qualifying_scope = parser->qualifying_scope;
3233       /* Process the final unqualified-id.  */
3234       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3235                                                  check_dependency_p,
3236                                                  declarator_p,
3237                                                  /*optional_p=*/false);
3238       /* Restore the SAVED_SCOPE for our caller.  */
3239       parser->scope = saved_scope;
3240       parser->object_scope = saved_object_scope;
3241       parser->qualifying_scope = saved_qualifying_scope;
3242
3243       return unqualified_id;
3244     }
3245   /* Otherwise, if we are in global scope, then we are looking at one
3246      of the other qualified-id productions.  */
3247   else if (global_scope_p)
3248     {
3249       cp_token *token;
3250       tree id;
3251
3252       /* Peek at the next token.  */
3253       token = cp_lexer_peek_token (parser->lexer);
3254
3255       /* If it's an identifier, and the next token is not a "<", then
3256          we can avoid the template-id case.  This is an optimization
3257          for this common case.  */
3258       if (token->type == CPP_NAME
3259           && !cp_parser_nth_token_starts_template_argument_list_p
3260                (parser, 2))
3261         return cp_parser_identifier (parser);
3262
3263       cp_parser_parse_tentatively (parser);
3264       /* Try a template-id.  */
3265       id = cp_parser_template_id (parser,
3266                                   /*template_keyword_p=*/false,
3267                                   /*check_dependency_p=*/true,
3268                                   declarator_p);
3269       /* If that worked, we're done.  */
3270       if (cp_parser_parse_definitely (parser))
3271         return id;
3272
3273       /* Peek at the next token.  (Changes in the token buffer may
3274          have invalidated the pointer obtained above.)  */
3275       token = cp_lexer_peek_token (parser->lexer);
3276
3277       switch (token->type)
3278         {
3279         case CPP_NAME:
3280           return cp_parser_identifier (parser);
3281
3282         case CPP_KEYWORD:
3283           if (token->keyword == RID_OPERATOR)
3284             return cp_parser_operator_function_id (parser);
3285           /* Fall through.  */
3286
3287         default:
3288           cp_parser_error (parser, "expected id-expression");
3289           return error_mark_node;
3290         }
3291     }
3292   else
3293     return cp_parser_unqualified_id (parser, template_keyword_p,
3294                                      /*check_dependency_p=*/true,
3295                                      declarator_p,
3296                                      optional_p);
3297 }
3298
3299 /* Parse an unqualified-id.
3300
3301    unqualified-id:
3302      identifier
3303      operator-function-id
3304      conversion-function-id
3305      ~ class-name
3306      template-id
3307
3308    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3309    keyword, in a construct like `A::template ...'.
3310
3311    Returns a representation of unqualified-id.  For the `identifier'
3312    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3313    production a BIT_NOT_EXPR is returned; the operand of the
3314    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3315    other productions, see the documentation accompanying the
3316    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3317    names are looked up in uninstantiated templates.  If DECLARATOR_P
3318    is true, the unqualified-id is appearing as part of a declarator,
3319    rather than as part of an expression.  */
3320
3321 static tree
3322 cp_parser_unqualified_id (cp_parser* parser,
3323                           bool template_keyword_p,
3324                           bool check_dependency_p,
3325                           bool declarator_p,
3326                           bool optional_p)
3327 {
3328   cp_token *token;
3329
3330   /* Peek at the next token.  */
3331   token = cp_lexer_peek_token (parser->lexer);
3332
3333   switch (token->type)
3334     {
3335     case CPP_NAME:
3336       {
3337         tree id;
3338
3339         /* We don't know yet whether or not this will be a
3340            template-id.  */
3341         cp_parser_parse_tentatively (parser);
3342         /* Try a template-id.  */
3343         id = cp_parser_template_id (parser, template_keyword_p,
3344                                     check_dependency_p,
3345                                     declarator_p);
3346         /* If it worked, we're done.  */
3347         if (cp_parser_parse_definitely (parser))
3348           return id;
3349         /* Otherwise, it's an ordinary identifier.  */
3350         return cp_parser_identifier (parser);
3351       }
3352
3353     case CPP_TEMPLATE_ID:
3354       return cp_parser_template_id (parser, template_keyword_p,
3355                                     check_dependency_p,
3356                                     declarator_p);
3357
3358     case CPP_COMPL:
3359       {
3360         tree type_decl;
3361         tree qualifying_scope;
3362         tree object_scope;
3363         tree scope;
3364         bool done;
3365
3366         /* Consume the `~' token.  */
3367         cp_lexer_consume_token (parser->lexer);
3368         /* Parse the class-name.  The standard, as written, seems to
3369            say that:
3370
3371              template <typename T> struct S { ~S (); };
3372              template <typename T> S<T>::~S() {}
3373
3374            is invalid, since `~' must be followed by a class-name, but
3375            `S<T>' is dependent, and so not known to be a class.
3376            That's not right; we need to look in uninstantiated
3377            templates.  A further complication arises from:
3378
3379              template <typename T> void f(T t) {
3380                t.T::~T();
3381              }
3382
3383            Here, it is not possible to look up `T' in the scope of `T'
3384            itself.  We must look in both the current scope, and the
3385            scope of the containing complete expression.
3386
3387            Yet another issue is:
3388
3389              struct S {
3390                int S;
3391                ~S();
3392              };
3393
3394              S::~S() {}
3395
3396            The standard does not seem to say that the `S' in `~S'
3397            should refer to the type `S' and not the data member
3398            `S::S'.  */
3399
3400         /* DR 244 says that we look up the name after the "~" in the
3401            same scope as we looked up the qualifying name.  That idea
3402            isn't fully worked out; it's more complicated than that.  */
3403         scope = parser->scope;
3404         object_scope = parser->object_scope;
3405         qualifying_scope = parser->qualifying_scope;
3406
3407         /* Check for invalid scopes.  */
3408         if (scope == error_mark_node)
3409           {
3410             cp_parser_skip_to_end_of_statement (parser);
3411             return error_mark_node;
3412           }
3413         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3414           {
3415             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3416               error ("scope %qT before %<~%> is not a class-name", scope);
3417             cp_parser_skip_to_end_of_statement (parser);
3418             return error_mark_node;
3419           }
3420         gcc_assert (!scope || TYPE_P (scope));
3421
3422         /* If the name is of the form "X::~X" it's OK.  */
3423         token = cp_lexer_peek_token (parser->lexer);
3424         if (scope
3425             && token->type == CPP_NAME
3426             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3427                 == CPP_OPEN_PAREN)
3428             && constructor_name_p (token->value, scope))
3429           {
3430             cp_lexer_consume_token (parser->lexer);
3431             return build_nt (BIT_NOT_EXPR, scope);
3432           }
3433
3434         /* If there was an explicit qualification (S::~T), first look
3435            in the scope given by the qualification (i.e., S).  */
3436         done = false;
3437         type_decl = NULL_TREE;
3438         if (scope)
3439           {
3440             cp_parser_parse_tentatively (parser);
3441             type_decl = cp_parser_class_name (parser,
3442                                               /*typename_keyword_p=*/false,
3443                                               /*template_keyword_p=*/false,
3444                                               none_type,
3445                                               /*check_dependency=*/false,
3446                                               /*class_head_p=*/false,
3447                                               declarator_p);
3448             if (cp_parser_parse_definitely (parser))
3449               done = true;
3450           }
3451         /* In "N::S::~S", look in "N" as well.  */
3452         if (!done && scope && qualifying_scope)
3453           {
3454             cp_parser_parse_tentatively (parser);
3455             parser->scope = qualifying_scope;
3456             parser->object_scope = NULL_TREE;
3457             parser->qualifying_scope = NULL_TREE;
3458             type_decl
3459               = cp_parser_class_name (parser,
3460                                       /*typename_keyword_p=*/false,
3461                                       /*template_keyword_p=*/false,
3462                                       none_type,
3463                                       /*check_dependency=*/false,
3464                                       /*class_head_p=*/false,
3465                                       declarator_p);
3466             if (cp_parser_parse_definitely (parser))
3467               done = true;
3468           }
3469         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3470         else if (!done && object_scope)
3471           {
3472             cp_parser_parse_tentatively (parser);
3473             parser->scope = object_scope;
3474             parser->object_scope = NULL_TREE;
3475             parser->qualifying_scope = NULL_TREE;
3476             type_decl
3477               = cp_parser_class_name (parser,
3478                                       /*typename_keyword_p=*/false,
3479                                       /*template_keyword_p=*/false,
3480                                       none_type,
3481                                       /*check_dependency=*/false,
3482                                       /*class_head_p=*/false,
3483                                       declarator_p);
3484             if (cp_parser_parse_definitely (parser))
3485               done = true;
3486           }
3487         /* Look in the surrounding context.  */
3488         if (!done)
3489           {
3490             parser->scope = NULL_TREE;
3491             parser->object_scope = NULL_TREE;
3492             parser->qualifying_scope = NULL_TREE;
3493             type_decl
3494               = cp_parser_class_name (parser,
3495                                       /*typename_keyword_p=*/false,
3496                                       /*template_keyword_p=*/false,
3497                                       none_type,
3498                                       /*check_dependency=*/false,
3499                                       /*class_head_p=*/false,
3500                                       declarator_p);
3501           }
3502         /* If an error occurred, assume that the name of the
3503            destructor is the same as the name of the qualifying
3504            class.  That allows us to keep parsing after running
3505            into ill-formed destructor names.  */
3506         if (type_decl == error_mark_node && scope)
3507           return build_nt (BIT_NOT_EXPR, scope);
3508         else if (type_decl == error_mark_node)
3509           return error_mark_node;
3510
3511         /* Check that destructor name and scope match.  */
3512         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3513           {
3514             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3515               error ("declaration of %<~%T%> as member of %qT",
3516                      type_decl, scope);
3517             return error_mark_node;
3518           }
3519
3520         /* [class.dtor]
3521
3522            A typedef-name that names a class shall not be used as the
3523            identifier in the declarator for a destructor declaration.  */
3524         if (declarator_p
3525             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3526             && !DECL_SELF_REFERENCE_P (type_decl)
3527             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3528           error ("typedef-name %qD used as destructor declarator",
3529                  type_decl);
3530
3531         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3532       }
3533
3534     case CPP_KEYWORD:
3535       if (token->keyword == RID_OPERATOR)
3536         {
3537           tree id;
3538
3539           /* This could be a template-id, so we try that first.  */
3540           cp_parser_parse_tentatively (parser);
3541           /* Try a template-id.  */
3542           id = cp_parser_template_id (parser, template_keyword_p,
3543                                       /*check_dependency_p=*/true,
3544                                       declarator_p);
3545           /* If that worked, we're done.  */
3546           if (cp_parser_parse_definitely (parser))
3547             return id;
3548           /* We still don't know whether we're looking at an
3549              operator-function-id or a conversion-function-id.  */
3550           cp_parser_parse_tentatively (parser);
3551           /* Try an operator-function-id.  */
3552           id = cp_parser_operator_function_id (parser);
3553           /* If that didn't work, try a conversion-function-id.  */
3554           if (!cp_parser_parse_definitely (parser))
3555             id = cp_parser_conversion_function_id (parser);
3556
3557           return id;
3558         }
3559       /* Fall through.  */
3560
3561     default:
3562       if (optional_p)
3563         return NULL_TREE;
3564       cp_parser_error (parser, "expected unqualified-id");
3565       return error_mark_node;
3566     }
3567 }
3568
3569 /* Parse an (optional) nested-name-specifier.
3570
3571    nested-name-specifier:
3572      class-or-namespace-name :: nested-name-specifier [opt]
3573      class-or-namespace-name :: template nested-name-specifier [opt]
3574
3575    PARSER->SCOPE should be set appropriately before this function is
3576    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3577    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3578    in name lookups.
3579
3580    Sets PARSER->SCOPE to the class (TYPE) or namespace
3581    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3582    it unchanged if there is no nested-name-specifier.  Returns the new
3583    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3584
3585    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3586    part of a declaration and/or decl-specifier.  */
3587
3588 static tree
3589 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3590                                      bool typename_keyword_p,
3591                                      bool check_dependency_p,
3592                                      bool type_p,
3593                                      bool is_declaration)
3594 {
3595   bool success = false;
3596   cp_token_position start = 0;
3597   cp_token *token;
3598
3599   /* Remember where the nested-name-specifier starts.  */
3600   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3601     {
3602       start = cp_lexer_token_position (parser->lexer, false);
3603       push_deferring_access_checks (dk_deferred);
3604     }
3605
3606   while (true)
3607     {
3608       tree new_scope;
3609       tree old_scope;
3610       tree saved_qualifying_scope;
3611       bool template_keyword_p;
3612
3613       /* Spot cases that cannot be the beginning of a
3614          nested-name-specifier.  */
3615       token = cp_lexer_peek_token (parser->lexer);
3616
3617       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3618          the already parsed nested-name-specifier.  */
3619       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3620         {
3621           /* Grab the nested-name-specifier and continue the loop.  */
3622           cp_parser_pre_parsed_nested_name_specifier (parser);
3623           success = true;
3624           continue;
3625         }
3626
3627       /* Spot cases that cannot be the beginning of a
3628          nested-name-specifier.  On the second and subsequent times
3629          through the loop, we look for the `template' keyword.  */
3630       if (success && token->keyword == RID_TEMPLATE)
3631         ;
3632       /* A template-id can start a nested-name-specifier.  */
3633       else if (token->type == CPP_TEMPLATE_ID)
3634         ;
3635       else
3636         {
3637           /* If the next token is not an identifier, then it is
3638              definitely not a class-or-namespace-name.  */
3639           if (token->type != CPP_NAME)
3640             break;
3641           /* If the following token is neither a `<' (to begin a
3642              template-id), nor a `::', then we are not looking at a
3643              nested-name-specifier.  */
3644           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3645           if (token->type != CPP_SCOPE
3646               && !cp_parser_nth_token_starts_template_argument_list_p
3647                   (parser, 2))
3648             break;
3649         }
3650
3651       /* The nested-name-specifier is optional, so we parse
3652          tentatively.  */
3653       cp_parser_parse_tentatively (parser);
3654
3655       /* Look for the optional `template' keyword, if this isn't the
3656          first time through the loop.  */
3657       if (success)
3658         template_keyword_p = cp_parser_optional_template_keyword (parser);
3659       else
3660         template_keyword_p = false;
3661
3662       /* Save the old scope since the name lookup we are about to do
3663          might destroy it.  */
3664       old_scope = parser->scope;
3665       saved_qualifying_scope = parser->qualifying_scope;
3666       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3667          look up names in "X<T>::I" in order to determine that "Y" is
3668          a template.  So, if we have a typename at this point, we make
3669          an effort to look through it.  */
3670       if (is_declaration
3671           && !typename_keyword_p
3672           && parser->scope
3673           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3674         parser->scope = resolve_typename_type (parser->scope,
3675                                                /*only_current_p=*/false);
3676       /* Parse the qualifying entity.  */
3677       new_scope
3678         = cp_parser_class_or_namespace_name (parser,
3679                                              typename_keyword_p,
3680                                              template_keyword_p,
3681                                              check_dependency_p,
3682                                              type_p,
3683                                              is_declaration);
3684       /* Look for the `::' token.  */
3685       cp_parser_require (parser, CPP_SCOPE, "`::'");
3686
3687       /* If we found what we wanted, we keep going; otherwise, we're
3688          done.  */
3689       if (!cp_parser_parse_definitely (parser))
3690         {
3691           bool error_p = false;
3692
3693           /* Restore the OLD_SCOPE since it was valid before the
3694              failed attempt at finding the last
3695              class-or-namespace-name.  */
3696           parser->scope = old_scope;
3697           parser->qualifying_scope = saved_qualifying_scope;
3698           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3699             break;
3700           /* If the next token is an identifier, and the one after
3701              that is a `::', then any valid interpretation would have
3702              found a class-or-namespace-name.  */
3703           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3704                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3705                      == CPP_SCOPE)
3706                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3707                      != CPP_COMPL))
3708             {
3709               token = cp_lexer_consume_token (parser->lexer);
3710               if (!error_p)
3711                 {
3712                   if (!token->ambiguous_p)
3713                     {
3714                       tree decl;
3715                       tree ambiguous_decls;
3716
3717                       decl = cp_parser_lookup_name (parser, token->value,
3718                                                     none_type,
3719                                                     /*is_template=*/false,
3720                                                     /*is_namespace=*/false,
3721                                                     /*check_dependency=*/true,
3722                                                     &ambiguous_decls);
3723                       if (TREE_CODE (decl) == TEMPLATE_DECL)
3724                         error ("%qD used without template parameters", decl);
3725                       else if (ambiguous_decls)
3726                         {
3727                           error ("reference to %qD is ambiguous",
3728                                  token->value);
3729                           print_candidates (ambiguous_decls);
3730                           decl = error_mark_node;
3731                         }
3732                       else
3733                         cp_parser_name_lookup_error
3734                           (parser, token->value, decl,
3735                            "is not a class or namespace");
3736                     }
3737                   parser->scope = error_mark_node;
3738                   error_p = true;
3739                   /* Treat this as a successful nested-name-specifier
3740                      due to:
3741
3742                      [basic.lookup.qual]
3743
3744                      If the name found is not a class-name (clause
3745                      _class_) or namespace-name (_namespace.def_), the
3746                      program is ill-formed.  */
3747                   success = true;
3748                 }
3749               cp_lexer_consume_token (parser->lexer);
3750             }
3751           break;
3752         }
3753       /* We've found one valid nested-name-specifier.  */
3754       success = true;
3755       /* Name lookup always gives us a DECL.  */
3756       if (TREE_CODE (new_scope) == TYPE_DECL)
3757         new_scope = TREE_TYPE (new_scope);
3758       /* Uses of "template" must be followed by actual templates.  */
3759       if (template_keyword_p
3760           && !(CLASS_TYPE_P (new_scope)
3761                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3762                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3763                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
3764           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3765                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3766                    == TEMPLATE_ID_EXPR)))
3767         pedwarn (TYPE_P (new_scope)
3768                  ? "%qT is not a template"
3769                  : "%qD is not a template",
3770                  new_scope);
3771       /* If it is a class scope, try to complete it; we are about to
3772          be looking up names inside the class.  */
3773       if (TYPE_P (new_scope)
3774           /* Since checking types for dependency can be expensive,
3775              avoid doing it if the type is already complete.  */
3776           && !COMPLETE_TYPE_P (new_scope)
3777           /* Do not try to complete dependent types.  */
3778           && !dependent_type_p (new_scope))
3779         new_scope = complete_type (new_scope);
3780       /* Make sure we look in the right scope the next time through
3781          the loop.  */
3782       parser->scope = new_scope;
3783     }
3784
3785   /* If parsing tentatively, replace the sequence of tokens that makes
3786      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3787      token.  That way, should we re-parse the token stream, we will
3788      not have to repeat the effort required to do the parse, nor will
3789      we issue duplicate error messages.  */
3790   if (success && start)
3791     {
3792       cp_token *token;
3793       tree access_checks;
3794
3795       token = cp_lexer_token_at (parser->lexer, start);
3796       /* Reset the contents of the START token.  */
3797       token->type = CPP_NESTED_NAME_SPECIFIER;
3798       /* Retrieve any deferred checks.  Do not pop this access checks yet
3799          so the memory will not be reclaimed during token replacing below.  */
3800       access_checks = get_deferred_access_checks ();
3801       token->value = build_tree_list (copy_list (access_checks),
3802                                       parser->scope);
3803       TREE_TYPE (token->value) = parser->qualifying_scope;
3804       token->keyword = RID_MAX;
3805
3806       /* Purge all subsequent tokens.  */
3807       cp_lexer_purge_tokens_after (parser->lexer, start);
3808     }
3809
3810   if (start)
3811     pop_to_parent_deferring_access_checks ();
3812
3813   return success ? parser->scope : NULL_TREE;
3814 }
3815
3816 /* Parse a nested-name-specifier.  See
3817    cp_parser_nested_name_specifier_opt for details.  This function
3818    behaves identically, except that it will an issue an error if no
3819    nested-name-specifier is present.  */
3820
3821 static tree
3822 cp_parser_nested_name_specifier (cp_parser *parser,
3823                                  bool typename_keyword_p,
3824                                  bool check_dependency_p,
3825                                  bool type_p,
3826                                  bool is_declaration)
3827 {
3828   tree scope;
3829
3830   /* Look for the nested-name-specifier.  */
3831   scope = cp_parser_nested_name_specifier_opt (parser,
3832                                                typename_keyword_p,
3833                                                check_dependency_p,
3834                                                type_p,
3835                                                is_declaration);
3836   /* If it was not present, issue an error message.  */
3837   if (!scope)
3838     {
3839       cp_parser_error (parser, "expected nested-name-specifier");
3840       parser->scope = NULL_TREE;
3841     }
3842
3843   return scope;
3844 }
3845
3846 /* Parse a class-or-namespace-name.
3847
3848    class-or-namespace-name:
3849      class-name
3850      namespace-name
3851
3852    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3853    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3854    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3855    TYPE_P is TRUE iff the next name should be taken as a class-name,
3856    even the same name is declared to be another entity in the same
3857    scope.
3858
3859    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3860    specified by the class-or-namespace-name.  If neither is found the
3861    ERROR_MARK_NODE is returned.  */
3862
3863 static tree
3864 cp_parser_class_or_namespace_name (cp_parser *parser,
3865                                    bool typename_keyword_p,
3866                                    bool template_keyword_p,
3867                                    bool check_dependency_p,
3868                                    bool type_p,
3869                                    bool is_declaration)
3870 {
3871   tree saved_scope;
3872   tree saved_qualifying_scope;
3873   tree saved_object_scope;
3874   tree scope;
3875   bool only_class_p;
3876
3877   /* Before we try to parse the class-name, we must save away the
3878      current PARSER->SCOPE since cp_parser_class_name will destroy
3879      it.  */
3880   saved_scope = parser->scope;
3881   saved_qualifying_scope = parser->qualifying_scope;
3882   saved_object_scope = parser->object_scope;
3883   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3884      there is no need to look for a namespace-name.  */
3885   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3886   if (!only_class_p)
3887     cp_parser_parse_tentatively (parser);
3888   scope = cp_parser_class_name (parser,
3889                                 typename_keyword_p,
3890                                 template_keyword_p,
3891                                 type_p ? class_type : none_type,
3892                                 check_dependency_p,
3893                                 /*class_head_p=*/false,
3894                                 is_declaration);
3895   /* If that didn't work, try for a namespace-name.  */
3896   if (!only_class_p && !cp_parser_parse_definitely (parser))
3897     {
3898       /* Restore the saved scope.  */
3899       parser->scope = saved_scope;
3900       parser->qualifying_scope = saved_qualifying_scope;
3901       parser->object_scope = saved_object_scope;
3902       /* If we are not looking at an identifier followed by the scope
3903          resolution operator, then this is not part of a
3904          nested-name-specifier.  (Note that this function is only used
3905          to parse the components of a nested-name-specifier.)  */
3906       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3907           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3908         return error_mark_node;
3909       scope = cp_parser_namespace_name (parser);
3910     }
3911
3912   return scope;
3913 }
3914
3915 /* Parse a postfix-expression.
3916
3917    postfix-expression:
3918      primary-expression
3919      postfix-expression [ expression ]
3920      postfix-expression ( expression-list [opt] )
3921      simple-type-specifier ( expression-list [opt] )
3922      typename :: [opt] nested-name-specifier identifier
3923        ( expression-list [opt] )
3924      typename :: [opt] nested-name-specifier template [opt] template-id
3925        ( expression-list [opt] )
3926      postfix-expression . template [opt] id-expression
3927      postfix-expression -> template [opt] id-expression
3928      postfix-expression . pseudo-destructor-name
3929      postfix-expression -> pseudo-destructor-name
3930      postfix-expression ++
3931      postfix-expression --
3932      dynamic_cast < type-id > ( expression )
3933      static_cast < type-id > ( expression )
3934      reinterpret_cast < type-id > ( expression )
3935      const_cast < type-id > ( expression )
3936      typeid ( expression )
3937      typeid ( type-id )
3938
3939    GNU Extension:
3940
3941    postfix-expression:
3942      ( type-id ) { initializer-list , [opt] }
3943
3944    This extension is a GNU version of the C99 compound-literal
3945    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3946    but they are essentially the same concept.)
3947
3948    If ADDRESS_P is true, the postfix expression is the operand of the
3949    `&' operator.  CAST_P is true if this expression is the target of a
3950    cast.
3951
3952    Returns a representation of the expression.  */
3953
3954 static tree
3955 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3956 {
3957   cp_token *token;
3958   enum rid keyword;
3959   cp_id_kind idk = CP_ID_KIND_NONE;
3960   tree postfix_expression = NULL_TREE;
3961
3962   /* Peek at the next token.  */
3963   token = cp_lexer_peek_token (parser->lexer);
3964   /* Some of the productions are determined by keywords.  */
3965   keyword = token->keyword;
3966   switch (keyword)
3967     {
3968     case RID_DYNCAST:
3969     case RID_STATCAST:
3970     case RID_REINTCAST:
3971     case RID_CONSTCAST:
3972       {
3973         tree type;
3974         tree expression;
3975         const char *saved_message;
3976
3977         /* All of these can be handled in the same way from the point
3978            of view of parsing.  Begin by consuming the token
3979            identifying the cast.  */
3980         cp_lexer_consume_token (parser->lexer);
3981
3982         /* New types cannot be defined in the cast.  */
3983         saved_message = parser->type_definition_forbidden_message;
3984         parser->type_definition_forbidden_message
3985           = "types may not be defined in casts";
3986
3987         /* Look for the opening `<'.  */
3988         cp_parser_require (parser, CPP_LESS, "`<'");
3989         /* Parse the type to which we are casting.  */
3990         type = cp_parser_type_id (parser);
3991         /* Look for the closing `>'.  */
3992         cp_parser_require (parser, CPP_GREATER, "`>'");
3993         /* Restore the old message.  */
3994         parser->type_definition_forbidden_message = saved_message;
3995
3996         /* And the expression which is being cast.  */
3997         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3998         expression = cp_parser_expression (parser, /*cast_p=*/true);
3999         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4000
4001         /* Only type conversions to integral or enumeration types
4002            can be used in constant-expressions.  */
4003         if (parser->integral_constant_expression_p
4004             && !dependent_type_p (type)
4005             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4006             && (cp_parser_non_integral_constant_expression
4007                 (parser,
4008                  "a cast to a type other than an integral or "
4009                  "enumeration type")))
4010           return error_mark_node;
4011
4012         switch (keyword)
4013           {
4014           case RID_DYNCAST:
4015             postfix_expression
4016               = build_dynamic_cast (type, expression);
4017             break;
4018           case RID_STATCAST:
4019             postfix_expression
4020               = build_static_cast (type, expression);
4021             break;
4022           case RID_REINTCAST:
4023             postfix_expression
4024               = build_reinterpret_cast (type, expression);
4025             break;
4026           case RID_CONSTCAST:
4027             postfix_expression
4028               = build_const_cast (type, expression);
4029             break;
4030           default:
4031             gcc_unreachable ();
4032           }
4033       }
4034       break;
4035
4036     case RID_TYPEID:
4037       {
4038         tree type;
4039         const char *saved_message;
4040         bool saved_in_type_id_in_expr_p;
4041
4042         /* Consume the `typeid' token.  */
4043         cp_lexer_consume_token (parser->lexer);
4044         /* Look for the `(' token.  */
4045         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4046         /* Types cannot be defined in a `typeid' expression.  */
4047         saved_message = parser->type_definition_forbidden_message;
4048         parser->type_definition_forbidden_message
4049           = "types may not be defined in a `typeid\' expression";
4050         /* We can't be sure yet whether we're looking at a type-id or an
4051            expression.  */
4052         cp_parser_parse_tentatively (parser);
4053         /* Try a type-id first.  */
4054         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4055         parser->in_type_id_in_expr_p = true;
4056         type = cp_parser_type_id (parser);
4057         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4058         /* Look for the `)' token.  Otherwise, we can't be sure that
4059            we're not looking at an expression: consider `typeid (int
4060            (3))', for example.  */
4061         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4062         /* If all went well, simply lookup the type-id.  */
4063         if (cp_parser_parse_definitely (parser))
4064           postfix_expression = get_typeid (type);
4065         /* Otherwise, fall back to the expression variant.  */
4066         else
4067           {
4068             tree expression;
4069
4070             /* Look for an expression.  */
4071             expression = cp_parser_expression (parser, /*cast_p=*/false);
4072             /* Compute its typeid.  */
4073             postfix_expression = build_typeid (expression);
4074             /* Look for the `)' token.  */
4075             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4076           }
4077         /* `typeid' may not appear in an integral constant expression.  */
4078         if (cp_parser_non_integral_constant_expression(parser,
4079                                                        "`typeid' operator"))
4080           return error_mark_node;
4081         /* Restore the saved message.  */
4082         parser->type_definition_forbidden_message = saved_message;
4083       }
4084       break;
4085
4086     case RID_TYPENAME:
4087       {
4088         tree type;
4089         /* The syntax permitted here is the same permitted for an
4090            elaborated-type-specifier.  */
4091         type = cp_parser_elaborated_type_specifier (parser,
4092                                                     /*is_friend=*/false,
4093                                                     /*is_declaration=*/false);
4094         postfix_expression = cp_parser_functional_cast (parser, type);
4095       }
4096       break;
4097
4098     default:
4099       {
4100         tree type;
4101
4102         /* If the next thing is a simple-type-specifier, we may be
4103            looking at a functional cast.  We could also be looking at
4104            an id-expression.  So, we try the functional cast, and if
4105            that doesn't work we fall back to the primary-expression.  */
4106         cp_parser_parse_tentatively (parser);
4107         /* Look for the simple-type-specifier.  */
4108         type = cp_parser_simple_type_specifier (parser,
4109                                                 /*decl_specs=*/NULL,
4110                                                 CP_PARSER_FLAGS_NONE);
4111         /* Parse the cast itself.  */
4112         if (!cp_parser_error_occurred (parser))
4113           postfix_expression
4114             = cp_parser_functional_cast (parser, type);
4115         /* If that worked, we're done.  */
4116         if (cp_parser_parse_definitely (parser))
4117           break;
4118
4119         /* If the functional-cast didn't work out, try a
4120            compound-literal.  */
4121         if (cp_parser_allow_gnu_extensions_p (parser)
4122             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4123           {
4124             VEC(constructor_elt,gc) *initializer_list = NULL;
4125             bool saved_in_type_id_in_expr_p;
4126
4127             cp_parser_parse_tentatively (parser);
4128             /* Consume the `('.  */
4129             cp_lexer_consume_token (parser->lexer);
4130             /* Parse the type.  */
4131             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4132             parser->in_type_id_in_expr_p = true;
4133             type = cp_parser_type_id (parser);
4134             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4135             /* Look for the `)'.  */
4136             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4137             /* Look for the `{'.  */
4138             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4139             /* If things aren't going well, there's no need to
4140                keep going.  */
4141             if (!cp_parser_error_occurred (parser))
4142               {
4143                 bool non_constant_p;
4144                 /* Parse the initializer-list.  */
4145                 initializer_list
4146                   = cp_parser_initializer_list (parser, &non_constant_p);
4147                 /* Allow a trailing `,'.  */
4148                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4149                   cp_lexer_consume_token (parser->lexer);
4150                 /* Look for the final `}'.  */
4151                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4152               }
4153             /* If that worked, we're definitely looking at a
4154                compound-literal expression.  */
4155             if (cp_parser_parse_definitely (parser))
4156               {
4157                 /* Warn the user that a compound literal is not
4158                    allowed in standard C++.  */
4159                 if (pedantic)
4160                   pedwarn ("ISO C++ forbids compound-literals");
4161                 /* Form the representation of the compound-literal.  */
4162                 postfix_expression
4163                   = finish_compound_literal (type, initializer_list);
4164                 break;
4165               }
4166           }
4167
4168         /* It must be a primary-expression.  */
4169         postfix_expression
4170           = cp_parser_primary_expression (parser, address_p, cast_p,
4171                                           /*template_arg_p=*/false,
4172                                           &idk);
4173       }
4174       break;
4175     }
4176
4177   /* Keep looping until the postfix-expression is complete.  */
4178   while (true)
4179     {
4180       if (idk == CP_ID_KIND_UNQUALIFIED
4181           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4182           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4183         /* It is not a Koenig lookup function call.  */
4184         postfix_expression
4185           = unqualified_name_lookup_error (postfix_expression);
4186
4187       /* Peek at the next token.  */
4188       token = cp_lexer_peek_token (parser->lexer);
4189
4190       switch (token->type)
4191         {
4192         case CPP_OPEN_SQUARE:
4193           postfix_expression
4194             = cp_parser_postfix_open_square_expression (parser,
4195                                                         postfix_expression,
4196                                                         false);
4197           idk = CP_ID_KIND_NONE;
4198           break;
4199
4200         case CPP_OPEN_PAREN:
4201           /* postfix-expression ( expression-list [opt] ) */
4202           {
4203             bool koenig_p;
4204             bool is_builtin_constant_p;
4205             bool saved_integral_constant_expression_p = false;
4206             bool saved_non_integral_constant_expression_p = false;
4207             tree args;
4208
4209             is_builtin_constant_p
4210               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4211             if (is_builtin_constant_p)
4212               {
4213                 /* The whole point of __builtin_constant_p is to allow
4214                    non-constant expressions to appear as arguments.  */
4215                 saved_integral_constant_expression_p
4216                   = parser->integral_constant_expression_p;
4217                 saved_non_integral_constant_expression_p
4218                   = parser->non_integral_constant_expression_p;
4219                 parser->integral_constant_expression_p = false;
4220               }
4221             args = (cp_parser_parenthesized_expression_list
4222                     (parser, /*is_attribute_list=*/false,
4223                      /*cast_p=*/false,
4224                      /*non_constant_p=*/NULL));
4225             if (is_builtin_constant_p)
4226               {
4227                 parser->integral_constant_expression_p
4228                   = saved_integral_constant_expression_p;
4229                 parser->non_integral_constant_expression_p
4230                   = saved_non_integral_constant_expression_p;
4231               }
4232
4233             if (args == error_mark_node)
4234               {
4235                 postfix_expression = error_mark_node;
4236                 break;
4237               }
4238
4239             /* Function calls are not permitted in
4240                constant-expressions.  */
4241             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4242                 && cp_parser_non_integral_constant_expression (parser,
4243                                                                "a function call"))
4244               {
4245                 postfix_expression = error_mark_node;
4246                 break;
4247               }
4248
4249             koenig_p = false;
4250             if (idk == CP_ID_KIND_UNQUALIFIED)
4251               {
4252                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4253                   {
4254                     if (args)
4255                       {
4256                         koenig_p = true;
4257                         postfix_expression
4258                           = perform_koenig_lookup (postfix_expression, args);
4259                       }
4260                     else
4261                       postfix_expression
4262                         = unqualified_fn_lookup_error (postfix_expression);
4263                   }
4264                 /* We do not perform argument-dependent lookup if
4265                    normal lookup finds a non-function, in accordance
4266                    with the expected resolution of DR 218.  */
4267                 else if (args && is_overloaded_fn (postfix_expression))
4268                   {
4269                     tree fn = get_first_fn (postfix_expression);
4270
4271                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4272                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4273
4274                     /* Only do argument dependent lookup if regular
4275                        lookup does not find a set of member functions.
4276                        [basic.lookup.koenig]/2a  */
4277                     if (!DECL_FUNCTION_MEMBER_P (fn))
4278                       {
4279                         koenig_p = true;
4280                         postfix_expression
4281                           = perform_koenig_lookup (postfix_expression, args);
4282                       }
4283                   }
4284               }
4285
4286             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4287               {
4288                 tree instance = TREE_OPERAND (postfix_expression, 0);
4289                 tree fn = TREE_OPERAND (postfix_expression, 1);
4290
4291                 if (processing_template_decl
4292                     && (type_dependent_expression_p (instance)
4293                         || (!BASELINK_P (fn)
4294                             && TREE_CODE (fn) != FIELD_DECL)
4295                         || type_dependent_expression_p (fn)
4296                         || any_type_dependent_arguments_p (args)))
4297                   {
4298                     postfix_expression
4299                       = build_min_nt (CALL_EXPR, postfix_expression,
4300                                       args, NULL_TREE);
4301                     break;
4302                   }
4303
4304                 if (BASELINK_P (fn))
4305                   postfix_expression
4306                     = (build_new_method_call
4307                        (instance, fn, args, NULL_TREE,
4308                         (idk == CP_ID_KIND_QUALIFIED
4309                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4310                         /*fn_p=*/NULL));
4311                 else
4312                   postfix_expression
4313                     = finish_call_expr (postfix_expression, args,
4314                                         /*disallow_virtual=*/false,
4315                                         /*koenig_p=*/false);
4316               }
4317             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4318                      || TREE_CODE (postfix_expression) == MEMBER_REF
4319                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4320               postfix_expression = (build_offset_ref_call_from_tree
4321                                     (postfix_expression, args));
4322             else if (idk == CP_ID_KIND_QUALIFIED)
4323               /* A call to a static class member, or a namespace-scope
4324                  function.  */
4325               postfix_expression
4326                 = finish_call_expr (postfix_expression, args,
4327                                     /*disallow_virtual=*/true,
4328                                     koenig_p);
4329             else
4330               /* All other function calls.  */
4331               postfix_expression
4332                 = finish_call_expr (postfix_expression, args,
4333                                     /*disallow_virtual=*/false,
4334                                     koenig_p);
4335
4336             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4337             idk = CP_ID_KIND_NONE;
4338           }
4339           break;
4340
4341         case CPP_DOT:
4342         case CPP_DEREF:
4343           /* postfix-expression . template [opt] id-expression
4344              postfix-expression . pseudo-destructor-name
4345              postfix-expression -> template [opt] id-expression
4346              postfix-expression -> pseudo-destructor-name */
4347
4348           /* Consume the `.' or `->' operator.  */
4349           cp_lexer_consume_token (parser->lexer);
4350
4351           postfix_expression
4352             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4353                                                       postfix_expression,
4354                                                       false, &idk);
4355           break;
4356
4357         case CPP_PLUS_PLUS:
4358           /* postfix-expression ++  */
4359           /* Consume the `++' token.  */
4360           cp_lexer_consume_token (parser->lexer);
4361           /* Generate a representation for the complete expression.  */
4362           postfix_expression
4363             = finish_increment_expr (postfix_expression,
4364                                      POSTINCREMENT_EXPR);
4365           /* Increments may not appear in constant-expressions.  */
4366           if (cp_parser_non_integral_constant_expression (parser,
4367                                                           "an increment"))
4368             postfix_expression = error_mark_node;
4369           idk = CP_ID_KIND_NONE;
4370           break;
4371
4372         case CPP_MINUS_MINUS:
4373           /* postfix-expression -- */
4374           /* Consume the `--' token.  */
4375           cp_lexer_consume_token (parser->lexer);
4376           /* Generate a representation for the complete expression.  */
4377           postfix_expression
4378             = finish_increment_expr (postfix_expression,
4379                                      POSTDECREMENT_EXPR);
4380           /* Decrements may not appear in constant-expressions.  */
4381           if (cp_parser_non_integral_constant_expression (parser,
4382                                                           "a decrement"))
4383             postfix_expression = error_mark_node;
4384           idk = CP_ID_KIND_NONE;
4385           break;
4386
4387         default:
4388           return postfix_expression;
4389         }
4390     }
4391
4392   /* We should never get here.  */
4393   gcc_unreachable ();
4394   return error_mark_node;
4395 }
4396
4397 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4398    by cp_parser_builtin_offsetof.  We're looking for
4399
4400      postfix-expression [ expression ]
4401
4402    FOR_OFFSETOF is set if we're being called in that context, which
4403    changes how we deal with integer constant expressions.  */
4404
4405 static tree
4406 cp_parser_postfix_open_square_expression (cp_parser *parser,
4407                                           tree postfix_expression,
4408                                           bool for_offsetof)
4409 {
4410   tree index;
4411
4412   /* Consume the `[' token.  */
4413   cp_lexer_consume_token (parser->lexer);
4414
4415   /* Parse the index expression.  */
4416   /* ??? For offsetof, there is a question of what to allow here.  If
4417      offsetof is not being used in an integral constant expression context,
4418      then we *could* get the right answer by computing the value at runtime.
4419      If we are in an integral constant expression context, then we might
4420      could accept any constant expression; hard to say without analysis.
4421      Rather than open the barn door too wide right away, allow only integer
4422      constant expressions here.  */
4423   if (for_offsetof)
4424     index = cp_parser_constant_expression (parser, false, NULL);
4425   else
4426     index = cp_parser_expression (parser, /*cast_p=*/false);
4427
4428   /* Look for the closing `]'.  */
4429   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4430
4431   /* Build the ARRAY_REF.  */
4432   postfix_expression = grok_array_decl (postfix_expression, index);
4433
4434   /* When not doing offsetof, array references are not permitted in
4435      constant-expressions.  */
4436   if (!for_offsetof
4437       && (cp_parser_non_integral_constant_expression
4438           (parser, "an array reference")))
4439     postfix_expression = error_mark_node;
4440
4441   return postfix_expression;
4442 }
4443
4444 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4445    by cp_parser_builtin_offsetof.  We're looking for
4446
4447      postfix-expression . template [opt] id-expression
4448      postfix-expression . pseudo-destructor-name
4449      postfix-expression -> template [opt] id-expression
4450      postfix-expression -> pseudo-destructor-name
4451
4452    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4453    limits what of the above we'll actually accept, but nevermind.
4454    TOKEN_TYPE is the "." or "->" token, which will already have been
4455    removed from the stream.  */
4456
4457 static tree
4458 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4459                                         enum cpp_ttype token_type,
4460                                         tree postfix_expression,
4461                                         bool for_offsetof, cp_id_kind *idk)
4462 {
4463   tree name;
4464   bool dependent_p;
4465   bool pseudo_destructor_p;
4466   tree scope = NULL_TREE;
4467
4468   /* If this is a `->' operator, dereference the pointer.  */
4469   if (token_type == CPP_DEREF)
4470     postfix_expression = build_x_arrow (postfix_expression);
4471   /* Check to see whether or not the expression is type-dependent.  */
4472   dependent_p = type_dependent_expression_p (postfix_expression);
4473   /* The identifier following the `->' or `.' is not qualified.  */
4474   parser->scope = NULL_TREE;
4475   parser->qualifying_scope = NULL_TREE;
4476   parser->object_scope = NULL_TREE;
4477   *idk = CP_ID_KIND_NONE;
4478   /* Enter the scope corresponding to the type of the object
4479      given by the POSTFIX_EXPRESSION.  */
4480   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4481     {
4482       scope = TREE_TYPE (postfix_expression);
4483       /* According to the standard, no expression should ever have
4484          reference type.  Unfortunately, we do not currently match
4485          the standard in this respect in that our internal representation
4486          of an expression may have reference type even when the standard
4487          says it does not.  Therefore, we have to manually obtain the
4488          underlying type here.  */
4489       scope = non_reference (scope);
4490       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4491       if (scope == unknown_type_node)
4492         {
4493           error ("%qE does not have class type", postfix_expression);
4494           scope = NULL_TREE;
4495         }
4496       else
4497         scope = complete_type_or_else (scope, NULL_TREE);
4498       /* Let the name lookup machinery know that we are processing a
4499          class member access expression.  */
4500       parser->context->object_type = scope;
4501       /* If something went wrong, we want to be able to discern that case,
4502          as opposed to the case where there was no SCOPE due to the type
4503          of expression being dependent.  */
4504       if (!scope)
4505         scope = error_mark_node;
4506       /* If the SCOPE was erroneous, make the various semantic analysis
4507          functions exit quickly -- and without issuing additional error
4508          messages.  */
4509       if (scope == error_mark_node)
4510         postfix_expression = error_mark_node;
4511     }
4512
4513   /* Assume this expression is not a pseudo-destructor access.  */
4514   pseudo_destructor_p = false;
4515
4516   /* If the SCOPE is a scalar type, then, if this is a valid program,
4517      we must be looking at a pseudo-destructor-name.  */
4518   if (scope && SCALAR_TYPE_P (scope))
4519     {
4520       tree s;
4521       tree type;
4522
4523       cp_parser_parse_tentatively (parser);
4524       /* Parse the pseudo-destructor-name.  */
4525       s = NULL_TREE;
4526       cp_parser_pseudo_destructor_name (parser, &s, &type);
4527       if (cp_parser_parse_definitely (parser))
4528         {
4529           pseudo_destructor_p = true;
4530           postfix_expression
4531             = finish_pseudo_destructor_expr (postfix_expression,
4532                                              s, TREE_TYPE (type));
4533         }
4534     }
4535
4536   if (!pseudo_destructor_p)
4537     {
4538       /* If the SCOPE is not a scalar type, we are looking at an
4539          ordinary class member access expression, rather than a
4540          pseudo-destructor-name.  */
4541       bool template_p;
4542       /* Parse the id-expression.  */
4543       name = (cp_parser_id_expression
4544               (parser,
4545                cp_parser_optional_template_keyword (parser),
4546                /*check_dependency_p=*/true,
4547                &template_p,
4548                /*declarator_p=*/false,
4549                /*optional_p=*/false));
4550       /* In general, build a SCOPE_REF if the member name is qualified.
4551          However, if the name was not dependent and has already been
4552          resolved; there is no need to build the SCOPE_REF.  For example;
4553
4554              struct X { void f(); };
4555              template <typename T> void f(T* t) { t->X::f(); }
4556
4557          Even though "t" is dependent, "X::f" is not and has been resolved
4558          to a BASELINK; there is no need to include scope information.  */
4559
4560       /* But we do need to remember that there was an explicit scope for
4561          virtual function calls.  */
4562       if (parser->scope)
4563         *idk = CP_ID_KIND_QUALIFIED;
4564
4565       /* If the name is a template-id that names a type, we will get a
4566          TYPE_DECL here.  That is invalid code.  */
4567       if (TREE_CODE (name) == TYPE_DECL)
4568         {
4569           error ("invalid use of %qD", name);
4570           postfix_expression = error_mark_node;
4571         }
4572       else
4573         {
4574           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4575             {
4576               name = build_qualified_name (/*type=*/NULL_TREE,
4577                                            parser->scope,
4578                                            name,
4579                                            template_p);
4580               parser->scope = NULL_TREE;
4581               parser->qualifying_scope = NULL_TREE;
4582               parser->object_scope = NULL_TREE;
4583             }
4584           if (scope && name && BASELINK_P (name))
4585             adjust_result_of_qualified_name_lookup
4586               (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4587           postfix_expression
4588             = finish_class_member_access_expr (postfix_expression, name,
4589                                                template_p);
4590         }
4591     }
4592
4593   /* We no longer need to look up names in the scope of the object on
4594      the left-hand side of the `.' or `->' operator.  */
4595   parser->context->object_type = NULL_TREE;
4596
4597   /* Outside of offsetof, these operators may not appear in
4598      constant-expressions.  */
4599   if (!for_offsetof
4600       && (cp_parser_non_integral_constant_expression
4601           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4602     postfix_expression = error_mark_node;
4603
4604   return postfix_expression;
4605 }
4606
4607 /* Parse a parenthesized expression-list.
4608
4609    expression-list:
4610      assignment-expression
4611      expression-list, assignment-expression
4612
4613    attribute-list:
4614      expression-list
4615      identifier
4616      identifier, expression-list
4617
4618    CAST_P is true if this expression is the target of a cast.
4619
4620    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4621    representation of an assignment-expression.  Note that a TREE_LIST
4622    is returned even if there is only a single expression in the list.
4623    error_mark_node is returned if the ( and or ) are
4624    missing. NULL_TREE is returned on no expressions. The parentheses
4625    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4626    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4627    indicates whether or not all of the expressions in the list were
4628    constant.  */
4629
4630 static tree
4631 cp_parser_parenthesized_expression_list (cp_parser* parser,
4632                                          bool is_attribute_list,
4633                                          bool cast_p,
4634                                          bool *non_constant_p)
4635 {
4636   tree expression_list = NULL_TREE;
4637   bool fold_expr_p = is_attribute_list;
4638   tree identifier = NULL_TREE;
4639
4640   /* Assume all the expressions will be constant.  */
4641   if (non_constant_p)
4642     *non_constant_p = false;
4643
4644   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4645     return error_mark_node;
4646
4647   /* Consume expressions until there are no more.  */
4648   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4649     while (true)
4650       {
4651         tree expr;
4652
4653         /* At the beginning of attribute lists, check to see if the
4654            next token is an identifier.  */
4655         if (is_attribute_list
4656             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4657           {
4658             cp_token *token;
4659
4660             /* Consume the identifier.  */
4661             token = cp_lexer_consume_token (parser->lexer);
4662             /* Save the identifier.  */
4663             identifier = token->value;
4664           }
4665         else
4666           {
4667             /* Parse the next assignment-expression.  */
4668             if (non_constant_p)
4669               {
4670                 bool expr_non_constant_p;
4671                 expr = (cp_parser_constant_expression
4672                         (parser, /*allow_non_constant_p=*/true,
4673                          &expr_non_constant_p));
4674                 if (expr_non_constant_p)
4675                   *non_constant_p = true;
4676               }
4677             else
4678               expr = cp_parser_assignment_expression (parser, cast_p);
4679
4680             if (fold_expr_p)
4681               expr = fold_non_dependent_expr (expr);
4682
4683              /* Add it to the list.  We add error_mark_node
4684                 expressions to the list, so that we can still tell if
4685                 the correct form for a parenthesized expression-list
4686                 is found. That gives better errors.  */
4687             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4688
4689             if (expr == error_mark_node)
4690               goto skip_comma;
4691           }
4692
4693         /* After the first item, attribute lists look the same as
4694            expression lists.  */
4695         is_attribute_list = false;
4696
4697       get_comma:;
4698         /* If the next token isn't a `,', then we are done.  */
4699         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4700           break;
4701
4702         /* Otherwise, consume the `,' and keep going.  */
4703         cp_lexer_consume_token (parser->lexer);
4704       }
4705
4706   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4707     {
4708       int ending;
4709
4710     skip_comma:;
4711       /* We try and resync to an unnested comma, as that will give the
4712          user better diagnostics.  */
4713       ending = cp_parser_skip_to_closing_parenthesis (parser,
4714                                                       /*recovering=*/true,
4715                                                       /*or_comma=*/true,
4716                                                       /*consume_paren=*/true);
4717       if (ending < 0)
4718         goto get_comma;
4719       if (!ending)
4720         return error_mark_node;
4721     }
4722
4723   /* We built up the list in reverse order so we must reverse it now.  */
4724   expression_list = nreverse (expression_list);
4725   if (identifier)
4726     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4727
4728   return expression_list;
4729 }
4730
4731 /* Parse a pseudo-destructor-name.
4732
4733    pseudo-destructor-name:
4734      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4735      :: [opt] nested-name-specifier template template-id :: ~ type-name
4736      :: [opt] nested-name-specifier [opt] ~ type-name
4737
4738    If either of the first two productions is used, sets *SCOPE to the
4739    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4740    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4741    or ERROR_MARK_NODE if the parse fails.  */
4742
4743 static void
4744 cp_parser_pseudo_destructor_name (cp_parser* parser,
4745                                   tree* scope,
4746                                   tree* type)
4747 {
4748   bool nested_name_specifier_p;
4749
4750   /* Assume that things will not work out.  */
4751   *type = error_mark_node;
4752
4753   /* Look for the optional `::' operator.  */
4754   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4755   /* Look for the optional nested-name-specifier.  */
4756   nested_name_specifier_p
4757     = (cp_parser_nested_name_specifier_opt (parser,
4758                                             /*typename_keyword_p=*/false,
4759                                             /*check_dependency_p=*/true,
4760                                             /*type_p=*/false,
4761                                             /*is_declaration=*/true)
4762        != NULL_TREE);
4763   /* Now, if we saw a nested-name-specifier, we might be doing the
4764      second production.  */
4765   if (nested_name_specifier_p
4766       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4767     {
4768       /* Consume the `template' keyword.  */
4769       cp_lexer_consume_token (parser->lexer);
4770       /* Parse the template-id.  */
4771       cp_parser_template_id (parser,
4772                              /*template_keyword_p=*/true,
4773                              /*check_dependency_p=*/false,
4774                              /*is_declaration=*/true);
4775       /* Look for the `::' token.  */
4776       cp_parser_require (parser, CPP_SCOPE, "`::'");
4777     }
4778   /* If the next token is not a `~', then there might be some
4779      additional qualification.  */
4780   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4781     {
4782       /* Look for the type-name.  */
4783       *scope = TREE_TYPE (cp_parser_type_name (parser));
4784
4785       if (*scope == error_mark_node)
4786         return;
4787
4788       /* If we don't have ::~, then something has gone wrong.  Since
4789          the only caller of this function is looking for something
4790          after `.' or `->' after a scalar type, most likely the
4791          program is trying to get a member of a non-aggregate
4792          type.  */
4793       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4794           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4795         {
4796           cp_parser_error (parser, "request for member of non-aggregate type");
4797           return;
4798         }
4799
4800       /* Look for the `::' token.  */
4801       cp_parser_require (parser, CPP_SCOPE, "`::'");
4802     }
4803   else
4804     *scope = NULL_TREE;
4805
4806   /* Look for the `~'.  */
4807   cp_parser_require (parser, CPP_COMPL, "`~'");
4808   /* Look for the type-name again.  We are not responsible for
4809      checking that it matches the first type-name.  */
4810   *type = cp_parser_type_name (parser);
4811 }
4812
4813 /* Parse a unary-expression.
4814
4815    unary-expression:
4816      postfix-expression
4817      ++ cast-expression
4818      -- cast-expression
4819      unary-operator cast-expression
4820      sizeof unary-expression
4821      sizeof ( type-id )
4822      new-expression
4823      delete-expression
4824
4825    GNU Extensions:
4826
4827    unary-expression:
4828      __extension__ cast-expression
4829      __alignof__ unary-expression
4830      __alignof__ ( type-id )
4831      __real__ cast-expression
4832      __imag__ cast-expression
4833      && identifier
4834
4835    ADDRESS_P is true iff the unary-expression is appearing as the
4836    operand of the `&' operator.   CAST_P is true if this expression is
4837    the target of a cast.
4838
4839    Returns a representation of the expression.  */
4840
4841 static tree
4842 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4843 {
4844   cp_token *token;
4845   enum tree_code unary_operator;
4846
4847   /* Peek at the next token.  */
4848   token = cp_lexer_peek_token (parser->lexer);
4849   /* Some keywords give away the kind of expression.  */
4850   if (token->type == CPP_KEYWORD)
4851     {
4852       enum rid keyword = token->keyword;
4853
4854       switch (keyword)
4855         {
4856         case RID_ALIGNOF:
4857         case RID_SIZEOF:
4858           {
4859             tree operand;
4860             enum tree_code op;
4861
4862             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4863             /* Consume the token.  */
4864             cp_lexer_consume_token (parser->lexer);
4865             /* Parse the operand.  */
4866             operand = cp_parser_sizeof_operand (parser, keyword);
4867
4868             if (TYPE_P (operand))
4869               return cxx_sizeof_or_alignof_type (operand, op, true);
4870             else
4871               return cxx_sizeof_or_alignof_expr (operand, op);
4872           }
4873
4874         case RID_NEW:
4875           return cp_parser_new_expression (parser);
4876
4877         case RID_DELETE:
4878           return cp_parser_delete_expression (parser);
4879
4880         case RID_EXTENSION:
4881           {
4882             /* The saved value of the PEDANTIC flag.  */
4883             int saved_pedantic;
4884             tree expr;
4885
4886             /* Save away the PEDANTIC flag.  */
4887             cp_parser_extension_opt (parser, &saved_pedantic);
4888             /* Parse the cast-expression.  */
4889             expr = cp_parser_simple_cast_expression (parser);
4890             /* Restore the PEDANTIC flag.  */
4891             pedantic = saved_pedantic;
4892
4893             return expr;
4894           }
4895
4896         case RID_REALPART:
4897         case RID_IMAGPART:
4898           {
4899             tree expression;
4900
4901             /* Consume the `__real__' or `__imag__' token.  */
4902             cp_lexer_consume_token (parser->lexer);
4903             /* Parse the cast-expression.  */
4904             expression = cp_parser_simple_cast_expression (parser);
4905             /* Create the complete representation.  */
4906             return build_x_unary_op ((keyword == RID_REALPART
4907                                       ? REALPART_EXPR : IMAGPART_EXPR),
4908                                      expression);
4909           }
4910           break;
4911
4912         default:
4913           break;
4914         }
4915     }
4916
4917   /* Look for the `:: new' and `:: delete', which also signal the
4918      beginning of a new-expression, or delete-expression,
4919      respectively.  If the next token is `::', then it might be one of
4920      these.  */
4921   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4922     {
4923       enum rid keyword;
4924
4925       /* See if the token after the `::' is one of the keywords in
4926          which we're interested.  */
4927       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4928       /* If it's `new', we have a new-expression.  */
4929       if (keyword == RID_NEW)
4930         return cp_parser_new_expression (parser);
4931       /* Similarly, for `delete'.  */
4932       else if (keyword == RID_DELETE)
4933         return cp_parser_delete_expression (parser);
4934     }
4935
4936   /* Look for a unary operator.  */
4937   unary_operator = cp_parser_unary_operator (token);
4938   /* The `++' and `--' operators can be handled similarly, even though
4939      they are not technically unary-operators in the grammar.  */
4940   if (unary_operator == ERROR_MARK)
4941     {
4942       if (token->type == CPP_PLUS_PLUS)
4943         unary_operator = PREINCREMENT_EXPR;
4944       else if (token->type == CPP_MINUS_MINUS)
4945         unary_operator = PREDECREMENT_EXPR;
4946       /* Handle the GNU address-of-label extension.  */
4947       else if (cp_parser_allow_gnu_extensions_p (parser)
4948                && token->type == CPP_AND_AND)
4949         {
4950           tree identifier;
4951
4952           /* Consume the '&&' token.  */
4953           cp_lexer_consume_token (parser->lexer);
4954           /* Look for the identifier.  */
4955           identifier = cp_parser_identifier (parser);
4956           /* Create an expression representing the address.  */
4957           return finish_label_address_expr (identifier);
4958         }
4959     }
4960   if (unary_operator != ERROR_MARK)
4961     {
4962       tree cast_expression;
4963       tree expression = error_mark_node;
4964       const char *non_constant_p = NULL;
4965
4966       /* Consume the operator token.  */
4967       token = cp_lexer_consume_token (parser->lexer);
4968       /* Parse the cast-expression.  */
4969       cast_expression
4970         = cp_parser_cast_expression (parser,
4971                                      unary_operator == ADDR_EXPR,
4972                                      /*cast_p=*/false);
4973       /* Now, build an appropriate representation.  */
4974       switch (unary_operator)
4975         {
4976         case INDIRECT_REF:
4977           non_constant_p = "`*'";
4978           expression = build_x_indirect_ref (cast_expression, "unary *");
4979           break;
4980
4981         case ADDR_EXPR:
4982           non_constant_p = "`&'";
4983           /* Fall through.  */
4984         case BIT_NOT_EXPR:
4985           expression = build_x_unary_op (unary_operator, cast_expression);
4986           break;
4987
4988         case PREINCREMENT_EXPR:
4989         case PREDECREMENT_EXPR:
4990           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4991                             ? "`++'" : "`--'");
4992           /* Fall through.  */
4993         case UNARY_PLUS_EXPR:
4994         case NEGATE_EXPR:
4995         case TRUTH_NOT_EXPR:
4996           expression = finish_unary_op_expr (unary_operator, cast_expression);
4997           break;
4998
4999         default:
5000           gcc_unreachable ();
5001         }
5002
5003       if (non_constant_p
5004           && cp_parser_non_integral_constant_expression (parser,
5005                                                          non_constant_p))
5006         expression = error_mark_node;
5007
5008       return expression;
5009     }
5010
5011   return cp_parser_postfix_expression (parser, address_p, cast_p);
5012 }
5013
5014 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5015    unary-operator, the corresponding tree code is returned.  */
5016
5017 static enum tree_code
5018 cp_parser_unary_operator (cp_token* token)
5019 {
5020   switch (token->type)
5021     {
5022     case CPP_MULT:
5023       return INDIRECT_REF;
5024
5025     case CPP_AND:
5026       return ADDR_EXPR;
5027
5028     case CPP_PLUS:
5029       return UNARY_PLUS_EXPR;
5030
5031     case CPP_MINUS:
5032       return NEGATE_EXPR;
5033
5034     case CPP_NOT:
5035       return TRUTH_NOT_EXPR;
5036
5037     case CPP_COMPL:
5038       return BIT_NOT_EXPR;
5039
5040     default:
5041       return ERROR_MARK;
5042     }
5043 }
5044
5045 /* Parse a new-expression.
5046
5047    new-expression:
5048      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5049      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5050
5051    Returns a representation of the expression.  */
5052
5053 static tree
5054 cp_parser_new_expression (cp_parser* parser)
5055 {
5056   bool global_scope_p;
5057   tree placement;
5058   tree type;
5059   tree initializer;
5060   tree nelts;
5061
5062   /* Look for the optional `::' operator.  */
5063   global_scope_p
5064     = (cp_parser_global_scope_opt (parser,
5065                                    /*current_scope_valid_p=*/false)
5066        != NULL_TREE);
5067   /* Look for the `new' operator.  */
5068   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5069   /* There's no easy way to tell a new-placement from the
5070      `( type-id )' construct.  */
5071   cp_parser_parse_tentatively (parser);
5072   /* Look for a new-placement.  */
5073   placement = cp_parser_new_placement (parser);
5074   /* If that didn't work out, there's no new-placement.  */
5075   if (!cp_parser_parse_definitely (parser))
5076     placement = NULL_TREE;
5077
5078   /* If the next token is a `(', then we have a parenthesized
5079      type-id.  */
5080   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5081     {
5082       /* Consume the `('.  */
5083       cp_lexer_consume_token (parser->lexer);
5084       /* Parse the type-id.  */
5085       type = cp_parser_type_id (parser);
5086       /* Look for the closing `)'.  */
5087       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5088       /* There should not be a direct-new-declarator in this production,
5089          but GCC used to allowed this, so we check and emit a sensible error
5090          message for this case.  */
5091       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5092         {
5093           error ("array bound forbidden after parenthesized type-id");
5094           inform ("try removing the parentheses around the type-id");
5095           cp_parser_direct_new_declarator (parser);
5096         }
5097       nelts = NULL_TREE;
5098     }
5099   /* Otherwise, there must be a new-type-id.  */
5100   else
5101     type = cp_parser_new_type_id (parser, &nelts);
5102
5103   /* If the next token is a `(', then we have a new-initializer.  */
5104   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5105     initializer = cp_parser_new_initializer (parser);
5106   else
5107     initializer = NULL_TREE;
5108
5109   /* A new-expression may not appear in an integral constant
5110      expression.  */
5111   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5112     return error_mark_node;
5113
5114   /* Create a representation of the new-expression.  */
5115   return build_new (placement, type, nelts, initializer, global_scope_p);
5116 }
5117
5118 /* Parse a new-placement.
5119
5120    new-placement:
5121      ( expression-list )
5122
5123    Returns the same representation as for an expression-list.  */
5124
5125 static tree
5126 cp_parser_new_placement (cp_parser* parser)
5127 {
5128   tree expression_list;
5129
5130   /* Parse the expression-list.  */
5131   expression_list = (cp_parser_parenthesized_expression_list
5132                      (parser, false, /*cast_p=*/false,
5133                       /*non_constant_p=*/NULL));
5134
5135   return expression_list;
5136 }
5137
5138 /* Parse a new-type-id.
5139
5140    new-type-id:
5141      type-specifier-seq new-declarator [opt]
5142
5143    Returns the TYPE allocated.  If the new-type-id indicates an array
5144    type, *NELTS is set to the number of elements in the last array
5145    bound; the TYPE will not include the last array bound.  */
5146
5147 static tree
5148 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5149 {
5150   cp_decl_specifier_seq type_specifier_seq;
5151   cp_declarator *new_declarator;
5152   cp_declarator *declarator;
5153   cp_declarator *outer_declarator;
5154   const char *saved_message;
5155   tree type;
5156
5157   /* The type-specifier sequence must not contain type definitions.
5158      (It cannot contain declarations of new types either, but if they
5159      are not definitions we will catch that because they are not
5160      complete.)  */
5161   saved_message = parser->type_definition_forbidden_message;
5162   parser->type_definition_forbidden_message
5163     = "types may not be defined in a new-type-id";
5164   /* Parse the type-specifier-seq.  */
5165   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5166                                 &type_specifier_seq);
5167   /* Restore the old message.  */
5168   parser->type_definition_forbidden_message = saved_message;
5169   /* Parse the new-declarator.  */
5170   new_declarator = cp_parser_new_declarator_opt (parser);
5171
5172   /* Determine the number of elements in the last array dimension, if
5173      any.  */
5174   *nelts = NULL_TREE;
5175   /* Skip down to the last array dimension.  */
5176   declarator = new_declarator;
5177   outer_declarator = NULL;
5178   while (declarator && (declarator->kind == cdk_pointer
5179                         || declarator->kind == cdk_ptrmem))
5180     {
5181       outer_declarator = declarator;
5182       declarator = declarator->declarator;
5183     }
5184   while (declarator
5185          && declarator->kind == cdk_array
5186          && declarator->declarator
5187          && declarator->declarator->kind == cdk_array)
5188     {
5189       outer_declarator = declarator;
5190       declarator = declarator->declarator;
5191     }
5192
5193   if (declarator && declarator->kind == cdk_array)
5194     {
5195       *nelts = declarator->u.array.bounds;
5196       if (*nelts == error_mark_node)
5197         *nelts = integer_one_node;
5198
5199       if (outer_declarator)
5200         outer_declarator->declarator = declarator->declarator;
5201       else
5202         new_declarator = NULL;
5203     }
5204
5205   type = groktypename (&type_specifier_seq, new_declarator);
5206   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5207     {
5208       *nelts = array_type_nelts_top (type);
5209       type = TREE_TYPE (type);
5210     }
5211   return type;
5212 }
5213
5214 /* Parse an (optional) new-declarator.
5215
5216    new-declarator:
5217      ptr-operator new-declarator [opt]
5218      direct-new-declarator
5219
5220    Returns the declarator.  */
5221
5222 static cp_declarator *
5223 cp_parser_new_declarator_opt (cp_parser* parser)
5224 {
5225   enum tree_code code;
5226   tree type;
5227   cp_cv_quals cv_quals;
5228
5229   /* We don't know if there's a ptr-operator next, or not.  */
5230   cp_parser_parse_tentatively (parser);
5231   /* Look for a ptr-operator.  */
5232   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5233   /* If that worked, look for more new-declarators.  */
5234   if (cp_parser_parse_definitely (parser))
5235     {
5236       cp_declarator *declarator;
5237
5238       /* Parse another optional declarator.  */
5239       declarator = cp_parser_new_declarator_opt (parser);
5240
5241       /* Create the representation of the declarator.  */
5242       if (type)
5243         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5244       else if (code == INDIRECT_REF)
5245         declarator = make_pointer_declarator (cv_quals, declarator);
5246       else
5247         declarator = make_reference_declarator (cv_quals, declarator);
5248
5249       return declarator;
5250     }
5251
5252   /* If the next token is a `[', there is a direct-new-declarator.  */
5253   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5254     return cp_parser_direct_new_declarator (parser);
5255
5256   return NULL;
5257 }
5258
5259 /* Parse a direct-new-declarator.
5260
5261    direct-new-declarator:
5262      [ expression ]
5263      direct-new-declarator [constant-expression]
5264
5265    */
5266
5267 static cp_declarator *
5268 cp_parser_direct_new_declarator (cp_parser* parser)
5269 {
5270   cp_declarator *declarator = NULL;
5271
5272   while (true)
5273     {
5274       tree expression;
5275
5276       /* Look for the opening `['.  */
5277       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5278       /* The first expression is not required to be constant.  */
5279       if (!declarator)
5280         {
5281           expression = cp_parser_expression (parser, /*cast_p=*/false);
5282           /* The standard requires that the expression have integral
5283              type.  DR 74 adds enumeration types.  We believe that the
5284              real intent is that these expressions be handled like the
5285              expression in a `switch' condition, which also allows
5286              classes with a single conversion to integral or
5287              enumeration type.  */
5288           if (!processing_template_decl)
5289             {
5290               expression
5291                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5292                                               expression,
5293                                               /*complain=*/true);
5294               if (!expression)
5295                 {
5296                   error ("expression in new-declarator must have integral "
5297                          "or enumeration type");
5298                   expression = error_mark_node;
5299                 }
5300             }
5301         }
5302       /* But all the other expressions must be.  */
5303       else
5304         expression
5305           = cp_parser_constant_expression (parser,
5306                                            /*allow_non_constant=*/false,
5307                                            NULL);
5308       /* Look for the closing `]'.  */
5309       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5310
5311       /* Add this bound to the declarator.  */
5312       declarator = make_array_declarator (declarator, expression);
5313
5314       /* If the next token is not a `[', then there are no more
5315          bounds.  */
5316       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5317         break;
5318     }
5319
5320   return declarator;
5321 }
5322
5323 /* Parse a new-initializer.
5324
5325    new-initializer:
5326      ( expression-list [opt] )
5327
5328    Returns a representation of the expression-list.  If there is no
5329    expression-list, VOID_ZERO_NODE is returned.  */
5330
5331 static tree
5332 cp_parser_new_initializer (cp_parser* parser)
5333 {
5334   tree expression_list;
5335
5336   expression_list = (cp_parser_parenthesized_expression_list
5337                      (parser, false, /*cast_p=*/false,
5338                       /*non_constant_p=*/NULL));
5339   if (!expression_list)
5340     expression_list = void_zero_node;
5341
5342   return expression_list;
5343 }
5344
5345 /* Parse a delete-expression.
5346
5347    delete-expression:
5348      :: [opt] delete cast-expression
5349      :: [opt] delete [ ] cast-expression
5350
5351    Returns a representation of the expression.  */
5352
5353 static tree
5354 cp_parser_delete_expression (cp_parser* parser)
5355 {
5356   bool global_scope_p;
5357   bool array_p;
5358   tree expression;
5359
5360   /* Look for the optional `::' operator.  */
5361   global_scope_p
5362     = (cp_parser_global_scope_opt (parser,
5363                                    /*current_scope_valid_p=*/false)
5364        != NULL_TREE);
5365   /* Look for the `delete' keyword.  */
5366   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5367   /* See if the array syntax is in use.  */
5368   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5369     {
5370       /* Consume the `[' token.  */
5371       cp_lexer_consume_token (parser->lexer);
5372       /* Look for the `]' token.  */
5373       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5374       /* Remember that this is the `[]' construct.  */
5375       array_p = true;
5376     }
5377   else
5378     array_p = false;
5379
5380   /* Parse the cast-expression.  */
5381   expression = cp_parser_simple_cast_expression (parser);
5382
5383   /* A delete-expression may not appear in an integral constant
5384      expression.  */
5385   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5386     return error_mark_node;
5387
5388   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5389 }
5390
5391 /* Parse a cast-expression.
5392
5393    cast-expression:
5394      unary-expression
5395      ( type-id ) cast-expression
5396
5397    ADDRESS_P is true iff the unary-expression is appearing as the
5398    operand of the `&' operator.   CAST_P is true if this expression is
5399    the target of a cast.
5400
5401    Returns a representation of the expression.  */
5402
5403 static tree
5404 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5405 {
5406   /* If it's a `(', then we might be looking at a cast.  */
5407   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5408     {
5409       tree type = NULL_TREE;
5410       tree expr = NULL_TREE;
5411       bool compound_literal_p;
5412       const char *saved_message;
5413
5414       /* There's no way to know yet whether or not this is a cast.
5415          For example, `(int (3))' is a unary-expression, while `(int)
5416          3' is a cast.  So, we resort to parsing tentatively.  */
5417       cp_parser_parse_tentatively (parser);
5418       /* Types may not be defined in a cast.  */
5419       saved_message = parser->type_definition_forbidden_message;
5420       parser->type_definition_forbidden_message
5421         = "types may not be defined in casts";
5422       /* Consume the `('.  */
5423       cp_lexer_consume_token (parser->lexer);
5424       /* A very tricky bit is that `(struct S) { 3 }' is a
5425          compound-literal (which we permit in C++ as an extension).
5426          But, that construct is not a cast-expression -- it is a
5427          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5428          is legal; if the compound-literal were a cast-expression,
5429          you'd need an extra set of parentheses.)  But, if we parse
5430          the type-id, and it happens to be a class-specifier, then we
5431          will commit to the parse at that point, because we cannot
5432          undo the action that is done when creating a new class.  So,
5433          then we cannot back up and do a postfix-expression.
5434
5435          Therefore, we scan ahead to the closing `)', and check to see
5436          if the token after the `)' is a `{'.  If so, we are not
5437          looking at a cast-expression.
5438
5439          Save tokens so that we can put them back.  */
5440       cp_lexer_save_tokens (parser->lexer);
5441       /* Skip tokens until the next token is a closing parenthesis.
5442          If we find the closing `)', and the next token is a `{', then
5443          we are looking at a compound-literal.  */
5444       compound_literal_p
5445         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5446                                                   /*consume_paren=*/true)
5447            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5448       /* Roll back the tokens we skipped.  */
5449       cp_lexer_rollback_tokens (parser->lexer);
5450       /* If we were looking at a compound-literal, simulate an error
5451          so that the call to cp_parser_parse_definitely below will
5452          fail.  */
5453       if (compound_literal_p)
5454         cp_parser_simulate_error (parser);
5455       else
5456         {
5457           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5458           parser->in_type_id_in_expr_p = true;
5459           /* Look for the type-id.  */
5460           type = cp_parser_type_id (parser);
5461           /* Look for the closing `)'.  */
5462           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5463           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5464         }
5465
5466       /* Restore the saved message.  */
5467       parser->type_definition_forbidden_message = saved_message;
5468
5469       /* If ok so far, parse the dependent expression. We cannot be
5470          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5471          ctor of T, but looks like a cast to function returning T
5472          without a dependent expression.  */
5473       if (!cp_parser_error_occurred (parser))
5474         expr = cp_parser_cast_expression (parser,
5475                                           /*address_p=*/false,
5476                                           /*cast_p=*/true);
5477
5478       if (cp_parser_parse_definitely (parser))
5479         {
5480           /* Warn about old-style casts, if so requested.  */
5481           if (warn_old_style_cast
5482               && !in_system_header
5483               && !VOID_TYPE_P (type)
5484               && current_lang_name != lang_name_c)
5485             warning (OPT_Wold_style_cast, "use of old-style cast");
5486
5487           /* Only type conversions to integral or enumeration types
5488              can be used in constant-expressions.  */
5489           if (parser->integral_constant_expression_p
5490               && !dependent_type_p (type)
5491               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5492               && (cp_parser_non_integral_constant_expression
5493                   (parser,
5494                    "a cast to a type other than an integral or "
5495                    "enumeration type")))
5496             return error_mark_node;
5497
5498           /* Perform the cast.  */
5499           expr = build_c_cast (type, expr);
5500           return expr;
5501         }
5502     }
5503
5504   /* If we get here, then it's not a cast, so it must be a
5505      unary-expression.  */
5506   return cp_parser_unary_expression (parser, address_p, cast_p);
5507 }
5508
5509 /* Parse a binary expression of the general form:
5510
5511    pm-expression:
5512      cast-expression
5513      pm-expression .* cast-expression
5514      pm-expression ->* cast-expression
5515
5516    multiplicative-expression:
5517      pm-expression
5518      multiplicative-expression * pm-expression
5519      multiplicative-expression / pm-expression
5520      multiplicative-expression % pm-expression
5521
5522    additive-expression:
5523      multiplicative-expression
5524      additive-expression + multiplicative-expression
5525      additive-expression - multiplicative-expression
5526
5527    shift-expression:
5528      additive-expression
5529      shift-expression << additive-expression
5530      shift-expression >> additive-expression
5531
5532    relational-expression:
5533      shift-expression
5534      relational-expression < shift-expression
5535      relational-expression > shift-expression
5536      relational-expression <= shift-expression
5537      relational-expression >= shift-expression
5538
5539   GNU Extension:
5540
5541    relational-expression:
5542      relational-expression <? shift-expression
5543      relational-expression >? shift-expression
5544
5545    equality-expression:
5546      relational-expression
5547      equality-expression == relational-expression
5548      equality-expression != relational-expression
5549
5550    and-expression:
5551      equality-expression
5552      and-expression & equality-expression
5553
5554    exclusive-or-expression:
5555      and-expression
5556      exclusive-or-expression ^ and-expression
5557
5558    inclusive-or-expression:
5559      exclusive-or-expression
5560      inclusive-or-expression | exclusive-or-expression
5561
5562    logical-and-expression:
5563      inclusive-or-expression
5564      logical-and-expression && inclusive-or-expression
5565
5566    logical-or-expression:
5567      logical-and-expression
5568      logical-or-expression || logical-and-expression
5569
5570    All these are implemented with a single function like:
5571
5572    binary-expression:
5573      simple-cast-expression
5574      binary-expression <token> binary-expression
5575
5576    CAST_P is true if this expression is the target of a cast.
5577
5578    The binops_by_token map is used to get the tree codes for each <token> type.
5579    binary-expressions are associated according to a precedence table.  */
5580
5581 #define TOKEN_PRECEDENCE(token) \
5582   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5583    ? PREC_NOT_OPERATOR \
5584    : binops_by_token[token->type].prec)
5585
5586 static tree
5587 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5588 {
5589   cp_parser_expression_stack stack;
5590   cp_parser_expression_stack_entry *sp = &stack[0];
5591   tree lhs, rhs;
5592   cp_token *token;
5593   enum tree_code tree_type;
5594   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5595   bool overloaded_p;
5596
5597   /* Parse the first expression.  */
5598   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5599
5600   for (;;)
5601     {
5602       /* Get an operator token.  */
5603       token = cp_lexer_peek_token (parser->lexer);
5604
5605       new_prec = TOKEN_PRECEDENCE (token);
5606
5607       /* Popping an entry off the stack means we completed a subexpression:
5608          - either we found a token which is not an operator (`>' where it is not
5609            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5610            will happen repeatedly;
5611          - or, we found an operator which has lower priority.  This is the case
5612            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5613            parsing `3 * 4'.  */
5614       if (new_prec <= prec)
5615         {
5616           if (sp == stack)
5617             break;
5618           else
5619             goto pop;
5620         }
5621
5622      get_rhs:
5623       tree_type = binops_by_token[token->type].tree_type;
5624
5625       /* We used the operator token.  */
5626       cp_lexer_consume_token (parser->lexer);
5627
5628       /* Extract another operand.  It may be the RHS of this expression
5629          or the LHS of a new, higher priority expression.  */
5630       rhs = cp_parser_simple_cast_expression (parser);
5631
5632       /* Get another operator token.  Look up its precedence to avoid
5633          building a useless (immediately popped) stack entry for common
5634          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5635       token = cp_lexer_peek_token (parser->lexer);
5636       lookahead_prec = TOKEN_PRECEDENCE (token);
5637       if (lookahead_prec > new_prec)
5638         {
5639           /* ... and prepare to parse the RHS of the new, higher priority
5640              expression.  Since precedence levels on the stack are
5641              monotonically increasing, we do not have to care about
5642              stack overflows.  */
5643           sp->prec = prec;
5644           sp->tree_type = tree_type;
5645           sp->lhs = lhs;
5646           sp++;
5647           lhs = rhs;
5648           prec = new_prec;
5649           new_prec = lookahead_prec;
5650           goto get_rhs;
5651
5652          pop:
5653           /* If the stack is not empty, we have parsed into LHS the right side
5654              (`4' in the example above) of an expression we had suspended.
5655              We can use the information on the stack to recover the LHS (`3')
5656              from the stack together with the tree code (`MULT_EXPR'), and
5657              the precedence of the higher level subexpression
5658              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5659              which will be used to actually build the additive expression.  */
5660           --sp;
5661           prec = sp->prec;
5662           tree_type = sp->tree_type;
5663           rhs = lhs;
5664           lhs = sp->lhs;
5665         }
5666
5667       overloaded_p = false;
5668       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5669
5670       /* If the binary operator required the use of an overloaded operator,
5671          then this expression cannot be an integral constant-expression.
5672          An overloaded operator can be used even if both operands are
5673          otherwise permissible in an integral constant-expression if at
5674          least one of the operands is of enumeration type.  */
5675
5676       if (overloaded_p
5677           && (cp_parser_non_integral_constant_expression
5678               (parser, "calls to overloaded operators")))
5679         return error_mark_node;
5680     }
5681
5682   return lhs;
5683 }
5684
5685
5686 /* Parse the `? expression : assignment-expression' part of a
5687    conditional-expression.  The LOGICAL_OR_EXPR is the
5688    logical-or-expression that started the conditional-expression.
5689    Returns a representation of the entire conditional-expression.
5690
5691    This routine is used by cp_parser_assignment_expression.
5692
5693      ? expression : assignment-expression
5694
5695    GNU Extensions:
5696
5697      ? : assignment-expression */
5698
5699 static tree
5700 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5701 {
5702   tree expr;
5703   tree assignment_expr;
5704
5705   /* Consume the `?' token.  */
5706   cp_lexer_consume_token (parser->lexer);
5707   if (cp_parser_allow_gnu_extensions_p (parser)
5708       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5709     /* Implicit true clause.  */
5710     expr = NULL_TREE;
5711   else
5712     /* Parse the expression.  */
5713     expr = cp_parser_expression (parser, /*cast_p=*/false);
5714
5715   /* The next token should be a `:'.  */
5716   cp_parser_require (parser, CPP_COLON, "`:'");
5717   /* Parse the assignment-expression.  */
5718   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5719
5720   /* Build the conditional-expression.  */
5721   return build_x_conditional_expr (logical_or_expr,
5722                                    expr,
5723                                    assignment_expr);
5724 }
5725
5726 /* Parse an assignment-expression.
5727
5728    assignment-expression:
5729      conditional-expression
5730      logical-or-expression assignment-operator assignment_expression
5731      throw-expression
5732
5733    CAST_P is true if this expression is the target of a cast.
5734
5735    Returns a representation for the expression.  */
5736
5737 static tree
5738 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5739 {
5740   tree expr;
5741
5742   /* If the next token is the `throw' keyword, then we're looking at
5743      a throw-expression.  */
5744   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5745     expr = cp_parser_throw_expression (parser);
5746   /* Otherwise, it must be that we are looking at a
5747      logical-or-expression.  */
5748   else
5749     {
5750       /* Parse the binary expressions (logical-or-expression).  */
5751       expr = cp_parser_binary_expression (parser, cast_p);
5752       /* If the next token is a `?' then we're actually looking at a
5753          conditional-expression.  */
5754       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5755         return cp_parser_question_colon_clause (parser, expr);
5756       else
5757         {
5758           enum tree_code assignment_operator;
5759
5760           /* If it's an assignment-operator, we're using the second
5761              production.  */
5762           assignment_operator
5763             = cp_parser_assignment_operator_opt (parser);
5764           if (assignment_operator != ERROR_MARK)
5765             {
5766               tree rhs;
5767
5768               /* Parse the right-hand side of the assignment.  */
5769               rhs = cp_parser_assignment_expression (parser, cast_p);
5770               /* An assignment may not appear in a
5771                  constant-expression.  */
5772               if (cp_parser_non_integral_constant_expression (parser,
5773                                                               "an assignment"))
5774                 return error_mark_node;
5775               /* Build the assignment expression.  */
5776               expr = build_x_modify_expr (expr,
5777                                           assignment_operator,
5778                                           rhs);
5779             }
5780         }
5781     }
5782
5783   return expr;
5784 }
5785
5786 /* Parse an (optional) assignment-operator.
5787
5788    assignment-operator: one of
5789      = *= /= %= += -= >>= <<= &= ^= |=
5790
5791    GNU Extension:
5792
5793    assignment-operator: one of
5794      <?= >?=
5795
5796    If the next token is an assignment operator, the corresponding tree
5797    code is returned, and the token is consumed.  For example, for
5798    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5799    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5800    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5801    operator, ERROR_MARK is returned.  */
5802
5803 static enum tree_code
5804 cp_parser_assignment_operator_opt (cp_parser* parser)
5805 {
5806   enum tree_code op;
5807   cp_token *token;
5808
5809   /* Peek at the next toen.  */
5810   token = cp_lexer_peek_token (parser->lexer);
5811
5812   switch (token->type)
5813     {
5814     case CPP_EQ:
5815       op = NOP_EXPR;
5816       break;
5817
5818     case CPP_MULT_EQ:
5819       op = MULT_EXPR;
5820       break;
5821
5822     case CPP_DIV_EQ:
5823       op = TRUNC_DIV_EXPR;
5824       break;
5825
5826     case CPP_MOD_EQ:
5827       op = TRUNC_MOD_EXPR;
5828       break;
5829
5830     case CPP_PLUS_EQ:
5831       op = PLUS_EXPR;
5832       break;
5833
5834     case CPP_MINUS_EQ:
5835       op = MINUS_EXPR;
5836       break;
5837
5838     case CPP_RSHIFT_EQ:
5839       op = RSHIFT_EXPR;
5840       break;
5841
5842     case CPP_LSHIFT_EQ:
5843       op = LSHIFT_EXPR;
5844       break;
5845
5846     case CPP_AND_EQ:
5847       op = BIT_AND_EXPR;
5848       break;
5849
5850     case CPP_XOR_EQ:
5851       op = BIT_XOR_EXPR;
5852       break;
5853
5854     case CPP_OR_EQ:
5855       op = BIT_IOR_EXPR;
5856       break;
5857
5858     default:
5859       /* Nothing else is an assignment operator.  */
5860       op = ERROR_MARK;
5861     }
5862
5863   /* If it was an assignment operator, consume it.  */
5864   if (op != ERROR_MARK)
5865     cp_lexer_consume_token (parser->lexer);
5866
5867   return op;
5868 }
5869
5870 /* Parse an expression.
5871
5872    expression:
5873      assignment-expression
5874      expression , assignment-expression
5875
5876    CAST_P is true if this expression is the target of a cast.
5877
5878    Returns a representation of the expression.  */
5879
5880 static tree
5881 cp_parser_expression (cp_parser* parser, bool cast_p)
5882 {
5883   tree expression = NULL_TREE;
5884
5885   while (true)
5886     {
5887       tree assignment_expression;
5888
5889       /* Parse the next assignment-expression.  */
5890       assignment_expression
5891         = cp_parser_assignment_expression (parser, cast_p);
5892       /* If this is the first assignment-expression, we can just
5893          save it away.  */
5894       if (!expression)
5895         expression = assignment_expression;
5896       else
5897         expression = build_x_compound_expr (expression,
5898                                             assignment_expression);
5899       /* If the next token is not a comma, then we are done with the
5900          expression.  */
5901       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5902         break;
5903       /* Consume the `,'.  */
5904       cp_lexer_consume_token (parser->lexer);
5905       /* A comma operator cannot appear in a constant-expression.  */
5906       if (cp_parser_non_integral_constant_expression (parser,
5907                                                       "a comma operator"))
5908         expression = error_mark_node;
5909     }
5910
5911   return expression;
5912 }
5913
5914 /* Parse a constant-expression.
5915
5916    constant-expression:
5917      conditional-expression
5918
5919   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5920   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5921   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5922   is false, NON_CONSTANT_P should be NULL.  */
5923
5924 static tree
5925 cp_parser_constant_expression (cp_parser* parser,
5926                                bool allow_non_constant_p,
5927                                bool *non_constant_p)
5928 {
5929   bool saved_integral_constant_expression_p;
5930   bool saved_allow_non_integral_constant_expression_p;
5931   bool saved_non_integral_constant_expression_p;
5932   tree expression;
5933
5934   /* It might seem that we could simply parse the
5935      conditional-expression, and then check to see if it were
5936      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5937      one that the compiler can figure out is constant, possibly after
5938      doing some simplifications or optimizations.  The standard has a
5939      precise definition of constant-expression, and we must honor
5940      that, even though it is somewhat more restrictive.
5941
5942      For example:
5943
5944        int i[(2, 3)];
5945
5946      is not a legal declaration, because `(2, 3)' is not a
5947      constant-expression.  The `,' operator is forbidden in a
5948      constant-expression.  However, GCC's constant-folding machinery
5949      will fold this operation to an INTEGER_CST for `3'.  */
5950
5951   /* Save the old settings.  */
5952   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5953   saved_allow_non_integral_constant_expression_p
5954     = parser->allow_non_integral_constant_expression_p;
5955   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5956   /* We are now parsing a constant-expression.  */
5957   parser->integral_constant_expression_p = true;
5958   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5959   parser->non_integral_constant_expression_p = false;
5960   /* Although the grammar says "conditional-expression", we parse an
5961      "assignment-expression", which also permits "throw-expression"
5962      and the use of assignment operators.  In the case that
5963      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5964      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5965      actually essential that we look for an assignment-expression.
5966      For example, cp_parser_initializer_clauses uses this function to
5967      determine whether a particular assignment-expression is in fact
5968      constant.  */
5969   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5970   /* Restore the old settings.  */
5971   parser->integral_constant_expression_p
5972     = saved_integral_constant_expression_p;
5973   parser->allow_non_integral_constant_expression_p
5974     = saved_allow_non_integral_constant_expression_p;
5975   if (allow_non_constant_p)
5976     *non_constant_p = parser->non_integral_constant_expression_p;
5977   else if (parser->non_integral_constant_expression_p)
5978     expression = error_mark_node;
5979   parser->non_integral_constant_expression_p
5980     = saved_non_integral_constant_expression_p;
5981
5982   return expression;
5983 }
5984
5985 /* Parse __builtin_offsetof.
5986
5987    offsetof-expression:
5988      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5989
5990    offsetof-member-designator:
5991      id-expression
5992      | offsetof-member-designator "." id-expression
5993      | offsetof-member-designator "[" expression "]"  */
5994
5995 static tree
5996 cp_parser_builtin_offsetof (cp_parser *parser)
5997 {
5998   int save_ice_p, save_non_ice_p;
5999   tree type, expr;
6000   cp_id_kind dummy;
6001
6002   /* We're about to accept non-integral-constant things, but will
6003      definitely yield an integral constant expression.  Save and
6004      restore these values around our local parsing.  */
6005   save_ice_p = parser->integral_constant_expression_p;
6006   save_non_ice_p = parser->non_integral_constant_expression_p;
6007
6008   /* Consume the "__builtin_offsetof" token.  */
6009   cp_lexer_consume_token (parser->lexer);
6010   /* Consume the opening `('.  */
6011   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6012   /* Parse the type-id.  */
6013   type = cp_parser_type_id (parser);
6014   /* Look for the `,'.  */
6015   cp_parser_require (parser, CPP_COMMA, "`,'");
6016
6017   /* Build the (type *)null that begins the traditional offsetof macro.  */
6018   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6019
6020   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6021   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6022                                                  true, &dummy);
6023   while (true)
6024     {
6025       cp_token *token = cp_lexer_peek_token (parser->lexer);
6026       switch (token->type)
6027         {
6028         case CPP_OPEN_SQUARE:
6029           /* offsetof-member-designator "[" expression "]" */
6030           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6031           break;
6032
6033         case CPP_DOT:
6034           /* offsetof-member-designator "." identifier */
6035           cp_lexer_consume_token (parser->lexer);
6036           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6037                                                          true, &dummy);
6038           break;
6039
6040         case CPP_CLOSE_PAREN:
6041           /* Consume the ")" token.  */
6042           cp_lexer_consume_token (parser->lexer);
6043           goto success;
6044
6045         default:
6046           /* Error.  We know the following require will fail, but
6047              that gives the proper error message.  */
6048           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6049           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6050           expr = error_mark_node;
6051           goto failure;
6052         }
6053     }
6054
6055  success:
6056   /* If we're processing a template, we can't finish the semantics yet.
6057      Otherwise we can fold the entire expression now.  */
6058   if (processing_template_decl)
6059     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6060   else
6061     expr = finish_offsetof (expr);
6062
6063  failure:
6064   parser->integral_constant_expression_p = save_ice_p;
6065   parser->non_integral_constant_expression_p = save_non_ice_p;
6066
6067   return expr;
6068 }
6069
6070 /* Statements [gram.stmt.stmt]  */
6071
6072 /* Parse a statement.
6073
6074    statement:
6075      labeled-statement
6076      expression-statement
6077      compound-statement
6078      selection-statement
6079      iteration-statement
6080      jump-statement
6081      declaration-statement
6082      try-block
6083
6084   IN_COMPOUND is true when the statement is nested inside a
6085   cp_parser_compound_statement; this matters for certain pragmas.  */
6086
6087 static void
6088 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6089                      bool in_compound)
6090 {
6091   tree statement;
6092   cp_token *token;
6093   location_t statement_location;
6094
6095  restart:
6096   /* There is no statement yet.  */
6097   statement = NULL_TREE;
6098   /* Peek at the next token.  */
6099   token = cp_lexer_peek_token (parser->lexer);
6100   /* Remember the location of the first token in the statement.  */
6101   statement_location = token->location;
6102   /* If this is a keyword, then that will often determine what kind of
6103      statement we have.  */
6104   if (token->type == CPP_KEYWORD)
6105     {
6106       enum rid keyword = token->keyword;
6107
6108       switch (keyword)
6109         {
6110         case RID_CASE:
6111         case RID_DEFAULT:
6112           statement = cp_parser_labeled_statement (parser, in_statement_expr,
6113                                                    in_compound);
6114           break;
6115
6116         case RID_IF:
6117         case RID_SWITCH:
6118           statement = cp_parser_selection_statement (parser);
6119           break;
6120
6121         case RID_WHILE:
6122         case RID_DO:
6123         case RID_FOR:
6124           statement = cp_parser_iteration_statement (parser);
6125           break;
6126
6127         case RID_BREAK:
6128         case RID_CONTINUE:
6129         case RID_RETURN:
6130         case RID_GOTO:
6131           statement = cp_parser_jump_statement (parser);
6132           break;
6133
6134           /* Objective-C++ exception-handling constructs.  */
6135         case RID_AT_TRY:
6136         case RID_AT_CATCH:
6137         case RID_AT_FINALLY:
6138         case RID_AT_SYNCHRONIZED:
6139         case RID_AT_THROW:
6140           statement = cp_parser_objc_statement (parser);
6141           break;
6142
6143         case RID_TRY:
6144           statement = cp_parser_try_block (parser);
6145           break;
6146
6147         default:
6148           /* It might be a keyword like `int' that can start a
6149              declaration-statement.  */
6150           break;
6151         }
6152     }
6153   else if (token->type == CPP_NAME)
6154     {
6155       /* If the next token is a `:', then we are looking at a
6156          labeled-statement.  */
6157       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6158       if (token->type == CPP_COLON)
6159         statement = cp_parser_labeled_statement (parser, in_statement_expr,
6160                                                  in_compound);
6161     }
6162   /* Anything that starts with a `{' must be a compound-statement.  */
6163   else if (token->type == CPP_OPEN_BRACE)
6164     statement = cp_parser_compound_statement (parser, NULL, false);
6165   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6166      a statement all its own.  */
6167   else if (token->type == CPP_PRAGMA)
6168     {
6169       /* Only certain OpenMP pragmas are attached to statements, and thus
6170          are considered statements themselves.  All others are not.  In
6171          the context of a compound, accept the pragma as a "statement" and
6172          return so that we can check for a close brace.  Otherwise we
6173          require a real statement and must go back and read one.  */
6174       if (in_compound)
6175         cp_parser_pragma (parser, pragma_compound);
6176       else if (!cp_parser_pragma (parser, pragma_stmt))
6177         goto restart;
6178       return;
6179     }
6180   else if (token->type == CPP_EOF)
6181     {
6182       cp_parser_error (parser, "expected statement");
6183       return;
6184     }
6185
6186   /* Everything else must be a declaration-statement or an
6187      expression-statement.  Try for the declaration-statement
6188      first, unless we are looking at a `;', in which case we know that
6189      we have an expression-statement.  */
6190   if (!statement)
6191     {
6192       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6193         {
6194           cp_parser_parse_tentatively (parser);
6195           /* Try to parse the declaration-statement.  */
6196           cp_parser_declaration_statement (parser);
6197           /* If that worked, we're done.  */
6198           if (cp_parser_parse_definitely (parser))
6199             return;
6200         }
6201       /* Look for an expression-statement instead.  */
6202       statement = cp_parser_expression_statement (parser, in_statement_expr);
6203     }
6204
6205   /* Set the line number for the statement.  */
6206   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6207     SET_EXPR_LOCATION (statement, statement_location);
6208 }
6209
6210 /* Parse a labeled-statement.
6211
6212    labeled-statement:
6213      identifier : statement
6214      case constant-expression : statement
6215      default : statement
6216
6217    GNU Extension:
6218
6219    labeled-statement:
6220      case constant-expression ... constant-expression : statement
6221
6222    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6223    For an ordinary label, returns a LABEL_EXPR.
6224
6225    IN_COMPOUND is as for cp_parser_statement: true when we're nested
6226    inside a compound.  */
6227
6228 static tree
6229 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6230                              bool in_compound)
6231 {
6232   cp_token *token;
6233   tree statement = error_mark_node;
6234
6235   /* The next token should be an identifier.  */
6236   token = cp_lexer_peek_token (parser->lexer);
6237   if (token->type != CPP_NAME
6238       && token->type != CPP_KEYWORD)
6239     {
6240       cp_parser_error (parser, "expected labeled-statement");
6241       return error_mark_node;
6242     }
6243
6244   switch (token->keyword)
6245     {
6246     case RID_CASE:
6247       {
6248         tree expr, expr_hi;
6249         cp_token *ellipsis;
6250
6251         /* Consume the `case' token.  */
6252         cp_lexer_consume_token (parser->lexer);
6253         /* Parse the constant-expression.  */
6254         expr = cp_parser_constant_expression (parser,
6255                                               /*allow_non_constant_p=*/false,
6256                                               NULL);
6257
6258         ellipsis = cp_lexer_peek_token (parser->lexer);
6259         if (ellipsis->type == CPP_ELLIPSIS)
6260           {
6261             /* Consume the `...' token.  */
6262             cp_lexer_consume_token (parser->lexer);
6263             expr_hi =
6264               cp_parser_constant_expression (parser,
6265                                              /*allow_non_constant_p=*/false,
6266                                              NULL);
6267             /* We don't need to emit warnings here, as the common code
6268                will do this for us.  */
6269           }
6270         else
6271           expr_hi = NULL_TREE;
6272
6273         if (parser->in_switch_statement_p)
6274           statement = finish_case_label (expr, expr_hi);
6275         else
6276           error ("case label %qE not within a switch statement", expr);
6277       }
6278       break;
6279
6280     case RID_DEFAULT:
6281       /* Consume the `default' token.  */
6282       cp_lexer_consume_token (parser->lexer);
6283
6284       if (parser->in_switch_statement_p)
6285         statement = finish_case_label (NULL_TREE, NULL_TREE);
6286       else
6287         error ("case label not within a switch statement");
6288       break;
6289
6290     default:
6291       /* Anything else must be an ordinary label.  */
6292       statement = finish_label_stmt (cp_parser_identifier (parser));
6293       break;
6294     }
6295
6296   /* Require the `:' token.  */
6297   cp_parser_require (parser, CPP_COLON, "`:'");
6298   /* Parse the labeled statement.  */
6299   cp_parser_statement (parser, in_statement_expr, in_compound);
6300
6301   /* Return the label, in the case of a `case' or `default' label.  */
6302   return statement;
6303 }
6304
6305 /* Parse an expression-statement.
6306
6307    expression-statement:
6308      expression [opt] ;
6309
6310    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6311    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6312    indicates whether this expression-statement is part of an
6313    expression statement.  */
6314
6315 static tree
6316 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6317 {
6318   tree statement = NULL_TREE;
6319
6320   /* If the next token is a ';', then there is no expression
6321      statement.  */
6322   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6323     statement = cp_parser_expression (parser, /*cast_p=*/false);
6324
6325   /* Consume the final `;'.  */
6326   cp_parser_consume_semicolon_at_end_of_statement (parser);
6327
6328   if (in_statement_expr
6329       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6330     /* This is the final expression statement of a statement
6331        expression.  */
6332     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6333   else if (statement)
6334     statement = finish_expr_stmt (statement);
6335   else
6336     finish_stmt ();
6337
6338   return statement;
6339 }
6340
6341 /* Parse a compound-statement.
6342
6343    compound-statement:
6344      { statement-seq [opt] }
6345
6346    Returns a tree representing the statement.  */
6347
6348 static tree
6349 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6350                               bool in_try)
6351 {
6352   tree compound_stmt;
6353
6354   /* Consume the `{'.  */
6355   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6356     return error_mark_node;
6357   /* Begin the compound-statement.  */
6358   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6359   /* Parse an (optional) statement-seq.  */
6360   cp_parser_statement_seq_opt (parser, in_statement_expr);
6361   /* Finish the compound-statement.  */
6362   finish_compound_stmt (compound_stmt);
6363   /* Consume the `}'.  */
6364   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6365
6366   return compound_stmt;
6367 }
6368
6369 /* Parse an (optional) statement-seq.
6370
6371    statement-seq:
6372      statement
6373      statement-seq [opt] statement  */
6374
6375 static void
6376 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6377 {
6378   /* Scan statements until there aren't any more.  */
6379   while (true)
6380     {
6381       cp_token *token = cp_lexer_peek_token (parser->lexer);
6382
6383       /* If we're looking at a `}', then we've run out of statements.  */
6384       if (token->type == CPP_CLOSE_BRACE
6385           || token->type == CPP_EOF
6386           || token->type == CPP_PRAGMA_EOL)
6387         break;
6388
6389       /* Parse the statement.  */
6390       cp_parser_statement (parser, in_statement_expr, true);
6391     }
6392 }
6393
6394 /* Parse a selection-statement.
6395
6396    selection-statement:
6397      if ( condition ) statement
6398      if ( condition ) statement else statement
6399      switch ( condition ) statement
6400
6401    Returns the new IF_STMT or SWITCH_STMT.  */
6402
6403 static tree
6404 cp_parser_selection_statement (cp_parser* parser)
6405 {
6406   cp_token *token;
6407   enum rid keyword;
6408
6409   /* Peek at the next token.  */
6410   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6411
6412   /* See what kind of keyword it is.  */
6413   keyword = token->keyword;
6414   switch (keyword)
6415     {
6416     case RID_IF:
6417     case RID_SWITCH:
6418       {
6419         tree statement;
6420         tree condition;
6421
6422         /* Look for the `('.  */
6423         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6424           {
6425             cp_parser_skip_to_end_of_statement (parser);
6426             return error_mark_node;
6427           }
6428
6429         /* Begin the selection-statement.  */
6430         if (keyword == RID_IF)
6431           statement = begin_if_stmt ();
6432         else
6433           statement = begin_switch_stmt ();
6434
6435         /* Parse the condition.  */
6436         condition = cp_parser_condition (parser);
6437         /* Look for the `)'.  */
6438         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6439           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6440                                                  /*consume_paren=*/true);
6441
6442         if (keyword == RID_IF)
6443           {
6444             /* Add the condition.  */
6445             finish_if_stmt_cond (condition, statement);
6446
6447             /* Parse the then-clause.  */
6448             cp_parser_implicitly_scoped_statement (parser);
6449             finish_then_clause (statement);
6450
6451             /* If the next token is `else', parse the else-clause.  */
6452             if (cp_lexer_next_token_is_keyword (parser->lexer,
6453                                                 RID_ELSE))
6454               {
6455                 /* Consume the `else' keyword.  */
6456                 cp_lexer_consume_token (parser->lexer);
6457                 begin_else_clause (statement);
6458                 /* Parse the else-clause.  */
6459                 cp_parser_implicitly_scoped_statement (parser);
6460                 finish_else_clause (statement);
6461               }
6462
6463             /* Now we're all done with the if-statement.  */
6464             finish_if_stmt (statement);
6465           }
6466         else
6467           {
6468             bool in_switch_statement_p;
6469             unsigned char in_statement;
6470
6471             /* Add the condition.  */
6472             finish_switch_cond (condition, statement);
6473
6474             /* Parse the body of the switch-statement.  */
6475             in_switch_statement_p = parser->in_switch_statement_p;
6476             in_statement = parser->in_statement;
6477             parser->in_switch_statement_p = true;
6478             parser->in_statement |= IN_SWITCH_STMT;
6479             cp_parser_implicitly_scoped_statement (parser);
6480             parser->in_switch_statement_p = in_switch_statement_p;
6481             parser->in_statement = in_statement;
6482
6483             /* Now we're all done with the switch-statement.  */
6484             finish_switch_stmt (statement);
6485           }
6486
6487         return statement;
6488       }
6489       break;
6490
6491     default:
6492       cp_parser_error (parser, "expected selection-statement");
6493       return error_mark_node;
6494     }
6495 }
6496
6497 /* Parse a condition.
6498
6499    condition:
6500      expression
6501      type-specifier-seq declarator = assignment-expression
6502
6503    GNU Extension:
6504
6505    condition:
6506      type-specifier-seq declarator asm-specification [opt]
6507        attributes [opt] = assignment-expression
6508
6509    Returns the expression that should be tested.  */
6510
6511 static tree
6512 cp_parser_condition (cp_parser* parser)
6513 {
6514   cp_decl_specifier_seq type_specifiers;
6515   const char *saved_message;
6516
6517   /* Try the declaration first.  */
6518   cp_parser_parse_tentatively (parser);
6519   /* New types are not allowed in the type-specifier-seq for a
6520      condition.  */
6521   saved_message = parser->type_definition_forbidden_message;
6522   parser->type_definition_forbidden_message
6523     = "types may not be defined in conditions";
6524   /* Parse the type-specifier-seq.  */
6525   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6526                                 &type_specifiers);
6527   /* Restore the saved message.  */
6528   parser->type_definition_forbidden_message = saved_message;
6529   /* If all is well, we might be looking at a declaration.  */
6530   if (!cp_parser_error_occurred (parser))
6531     {
6532       tree decl;
6533       tree asm_specification;
6534       tree attributes;
6535       cp_declarator *declarator;
6536       tree initializer = NULL_TREE;
6537
6538       /* Parse the declarator.  */
6539       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6540                                          /*ctor_dtor_or_conv_p=*/NULL,
6541                                          /*parenthesized_p=*/NULL,
6542                                          /*member_p=*/false);
6543       /* Parse the attributes.  */
6544       attributes = cp_parser_attributes_opt (parser);
6545       /* Parse the asm-specification.  */
6546       asm_specification = cp_parser_asm_specification_opt (parser);
6547       /* If the next token is not an `=', then we might still be
6548          looking at an expression.  For example:
6549
6550            if (A(a).x)
6551
6552          looks like a decl-specifier-seq and a declarator -- but then
6553          there is no `=', so this is an expression.  */
6554       cp_parser_require (parser, CPP_EQ, "`='");
6555       /* If we did see an `=', then we are looking at a declaration
6556          for sure.  */
6557       if (cp_parser_parse_definitely (parser))
6558         {
6559           tree pushed_scope;
6560           bool non_constant_p;
6561
6562           /* Create the declaration.  */
6563           decl = start_decl (declarator, &type_specifiers,
6564                              /*initialized_p=*/true,
6565                              attributes, /*prefix_attributes=*/NULL_TREE,
6566                              &pushed_scope);
6567           /* Parse the assignment-expression.  */
6568           initializer
6569             = cp_parser_constant_expression (parser,
6570                                              /*allow_non_constant_p=*/true,
6571                                              &non_constant_p);
6572           if (!non_constant_p)
6573             initializer = fold_non_dependent_expr (initializer);
6574
6575           /* Process the initializer.  */
6576           cp_finish_decl (decl,
6577                           initializer, !non_constant_p,
6578                           asm_specification,
6579                           LOOKUP_ONLYCONVERTING);
6580
6581           if (pushed_scope)
6582             pop_scope (pushed_scope);
6583
6584           return convert_from_reference (decl);
6585         }
6586     }
6587   /* If we didn't even get past the declarator successfully, we are
6588      definitely not looking at a declaration.  */
6589   else
6590     cp_parser_abort_tentative_parse (parser);
6591
6592   /* Otherwise, we are looking at an expression.  */
6593   return cp_parser_expression (parser, /*cast_p=*/false);
6594 }
6595
6596 /* Parse an iteration-statement.
6597
6598    iteration-statement:
6599      while ( condition ) statement
6600      do statement while ( expression ) ;
6601      for ( for-init-statement condition [opt] ; expression [opt] )
6602        statement
6603
6604    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6605
6606 static tree
6607 cp_parser_iteration_statement (cp_parser* parser)
6608 {
6609   cp_token *token;
6610   enum rid keyword;
6611   tree statement;
6612   unsigned char in_statement;
6613
6614   /* Peek at the next token.  */
6615   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6616   if (!token)
6617     return error_mark_node;
6618
6619   /* Remember whether or not we are already within an iteration
6620      statement.  */
6621   in_statement = parser->in_statement;
6622
6623   /* See what kind of keyword it is.  */
6624   keyword = token->keyword;
6625   switch (keyword)
6626     {
6627     case RID_WHILE:
6628       {
6629         tree condition;
6630
6631         /* Begin the while-statement.  */
6632         statement = begin_while_stmt ();
6633         /* Look for the `('.  */
6634         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6635         /* Parse the condition.  */
6636         condition = cp_parser_condition (parser);
6637         finish_while_stmt_cond (condition, statement);
6638         /* Look for the `)'.  */
6639         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6640         /* Parse the dependent statement.  */
6641         parser->in_statement = IN_ITERATION_STMT;
6642         cp_parser_already_scoped_statement (parser);
6643         parser->in_statement = in_statement;
6644         /* We're done with the while-statement.  */
6645         finish_while_stmt (statement);
6646       }
6647       break;
6648
6649     case RID_DO:
6650       {
6651         tree expression;
6652
6653         /* Begin the do-statement.  */
6654         statement = begin_do_stmt ();
6655         /* Parse the body of the do-statement.  */
6656         parser->in_statement = IN_ITERATION_STMT;
6657         cp_parser_implicitly_scoped_statement (parser);
6658         parser->in_statement = in_statement;
6659         finish_do_body (statement);
6660         /* Look for the `while' keyword.  */
6661         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6662         /* Look for the `('.  */
6663         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6664         /* Parse the expression.  */
6665         expression = cp_parser_expression (parser, /*cast_p=*/false);
6666         /* We're done with the do-statement.  */
6667         finish_do_stmt (expression, statement);
6668         /* Look for the `)'.  */
6669         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6670         /* Look for the `;'.  */
6671         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6672       }
6673       break;
6674
6675     case RID_FOR:
6676       {
6677         tree condition = NULL_TREE;
6678         tree expression = NULL_TREE;
6679
6680         /* Begin the for-statement.  */
6681         statement = begin_for_stmt ();
6682         /* Look for the `('.  */
6683         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6684         /* Parse the initialization.  */
6685         cp_parser_for_init_statement (parser);
6686         finish_for_init_stmt (statement);
6687
6688         /* If there's a condition, process it.  */
6689         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6690           condition = cp_parser_condition (parser);
6691         finish_for_cond (condition, statement);
6692         /* Look for the `;'.  */
6693         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6694
6695         /* If there's an expression, process it.  */
6696         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6697           expression = cp_parser_expression (parser, /*cast_p=*/false);
6698         finish_for_expr (expression, statement);
6699         /* Look for the `)'.  */
6700         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6701
6702         /* Parse the body of the for-statement.  */
6703         parser->in_statement = IN_ITERATION_STMT;
6704         cp_parser_already_scoped_statement (parser);
6705         parser->in_statement = in_statement;
6706
6707         /* We're done with the for-statement.  */
6708         finish_for_stmt (statement);
6709       }
6710       break;
6711
6712     default:
6713       cp_parser_error (parser, "expected iteration-statement");
6714       statement = error_mark_node;
6715       break;
6716     }
6717
6718   return statement;
6719 }
6720
6721 /* Parse a for-init-statement.
6722
6723    for-init-statement:
6724      expression-statement
6725      simple-declaration  */
6726
6727 static void
6728 cp_parser_for_init_statement (cp_parser* parser)
6729 {
6730   /* If the next token is a `;', then we have an empty
6731      expression-statement.  Grammatically, this is also a
6732      simple-declaration, but an invalid one, because it does not
6733      declare anything.  Therefore, if we did not handle this case
6734      specially, we would issue an error message about an invalid
6735      declaration.  */
6736   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6737     {
6738       /* We're going to speculatively look for a declaration, falling back
6739          to an expression, if necessary.  */
6740       cp_parser_parse_tentatively (parser);
6741       /* Parse the declaration.  */
6742       cp_parser_simple_declaration (parser,
6743                                     /*function_definition_allowed_p=*/false);
6744       /* If the tentative parse failed, then we shall need to look for an
6745          expression-statement.  */
6746       if (cp_parser_parse_definitely (parser))
6747         return;
6748     }
6749
6750   cp_parser_expression_statement (parser, false);
6751 }
6752
6753 /* Parse a jump-statement.
6754
6755    jump-statement:
6756      break ;
6757      continue ;
6758      return expression [opt] ;
6759      goto identifier ;
6760
6761    GNU extension:
6762
6763    jump-statement:
6764      goto * expression ;
6765
6766    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6767
6768 static tree
6769 cp_parser_jump_statement (cp_parser* parser)
6770 {
6771   tree statement = error_mark_node;
6772   cp_token *token;
6773   enum rid keyword;
6774
6775   /* Peek at the next token.  */
6776   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6777   if (!token)
6778     return error_mark_node;
6779
6780   /* See what kind of keyword it is.  */
6781   keyword = token->keyword;
6782   switch (keyword)
6783     {
6784     case RID_BREAK:
6785       switch (parser->in_statement)
6786         {
6787         case 0:
6788           error ("break statement not within loop or switch");
6789           break;
6790         default:
6791           gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6792                       || parser->in_statement == IN_ITERATION_STMT);
6793           statement = finish_break_stmt ();
6794           break;
6795         case IN_OMP_BLOCK:
6796           error ("invalid exit from OpenMP structured block");
6797           break;
6798         case IN_OMP_FOR:
6799           error ("break statement used with OpenMP for loop");
6800           break;
6801         }
6802       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6803       break;
6804
6805     case RID_CONTINUE:
6806       switch (parser->in_statement & ~IN_SWITCH_STMT)
6807         {
6808         case 0:
6809           error ("continue statement not within a loop");
6810           break;
6811         case IN_ITERATION_STMT:
6812         case IN_OMP_FOR:
6813           statement = finish_continue_stmt ();
6814           break;
6815         case IN_OMP_BLOCK:
6816           error ("invalid exit from OpenMP structured block");
6817           break;
6818         default:
6819           gcc_unreachable ();
6820         }
6821       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6822       break;
6823
6824     case RID_RETURN:
6825       {
6826         tree expr;
6827
6828         /* If the next token is a `;', then there is no
6829            expression.  */
6830         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6831           expr = cp_parser_expression (parser, /*cast_p=*/false);
6832         else
6833           expr = NULL_TREE;
6834         /* Build the return-statement.  */
6835         statement = finish_return_stmt (expr);
6836         /* Look for the final `;'.  */
6837         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6838       }
6839       break;
6840
6841     case RID_GOTO:
6842       /* Create the goto-statement.  */
6843       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6844         {
6845           /* Issue a warning about this use of a GNU extension.  */
6846           if (pedantic)
6847             pedwarn ("ISO C++ forbids computed gotos");
6848           /* Consume the '*' token.  */
6849           cp_lexer_consume_token (parser->lexer);
6850           /* Parse the dependent expression.  */
6851           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6852         }
6853       else
6854         finish_goto_stmt (cp_parser_identifier (parser));
6855       /* Look for the final `;'.  */
6856       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6857       break;
6858
6859     default:
6860       cp_parser_error (parser, "expected jump-statement");
6861       break;
6862     }
6863
6864   return statement;
6865 }
6866
6867 /* Parse a declaration-statement.
6868
6869    declaration-statement:
6870      block-declaration  */
6871
6872 static void
6873 cp_parser_declaration_statement (cp_parser* parser)
6874 {
6875   void *p;
6876
6877   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6878   p = obstack_alloc (&declarator_obstack, 0);
6879
6880  /* Parse the block-declaration.  */
6881   cp_parser_block_declaration (parser, /*statement_p=*/true);
6882
6883   /* Free any declarators allocated.  */
6884   obstack_free (&declarator_obstack, p);
6885
6886   /* Finish off the statement.  */
6887   finish_stmt ();
6888 }
6889
6890 /* Some dependent statements (like `if (cond) statement'), are
6891    implicitly in their own scope.  In other words, if the statement is
6892    a single statement (as opposed to a compound-statement), it is
6893    none-the-less treated as if it were enclosed in braces.  Any
6894    declarations appearing in the dependent statement are out of scope
6895    after control passes that point.  This function parses a statement,
6896    but ensures that is in its own scope, even if it is not a
6897    compound-statement.
6898
6899    Returns the new statement.  */
6900
6901 static tree
6902 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6903 {
6904   tree statement;
6905
6906   /* Mark if () ; with a special NOP_EXPR.  */
6907   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6908     {
6909       cp_lexer_consume_token (parser->lexer);
6910       statement = add_stmt (build_empty_stmt ());
6911     }
6912   /* if a compound is opened, we simply parse the statement directly.  */
6913   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6914     statement = cp_parser_compound_statement (parser, NULL, false);
6915   /* If the token is not a `{', then we must take special action.  */
6916   else
6917     {
6918       /* Create a compound-statement.  */
6919       statement = begin_compound_stmt (0);
6920       /* Parse the dependent-statement.  */
6921       cp_parser_statement (parser, NULL_TREE, false);
6922       /* Finish the dummy compound-statement.  */
6923       finish_compound_stmt (statement);
6924     }
6925
6926   /* Return the statement.  */
6927   return statement;
6928 }
6929
6930 /* For some dependent statements (like `while (cond) statement'), we
6931    have already created a scope.  Therefore, even if the dependent
6932    statement is a compound-statement, we do not want to create another
6933    scope.  */
6934
6935 static void
6936 cp_parser_already_scoped_statement (cp_parser* parser)
6937 {
6938   /* If the token is a `{', then we must take special action.  */
6939   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6940     cp_parser_statement (parser, NULL_TREE, false);
6941   else
6942     {
6943       /* Avoid calling cp_parser_compound_statement, so that we
6944          don't create a new scope.  Do everything else by hand.  */
6945       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6946       cp_parser_statement_seq_opt (parser, NULL_TREE);
6947       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6948     }
6949 }
6950
6951 /* Declarations [gram.dcl.dcl] */
6952
6953 /* Parse an optional declaration-sequence.
6954
6955    declaration-seq:
6956      declaration
6957      declaration-seq declaration  */
6958
6959 static void
6960 cp_parser_declaration_seq_opt (cp_parser* parser)
6961 {
6962   while (true)
6963     {
6964       cp_token *token;
6965
6966       token = cp_lexer_peek_token (parser->lexer);
6967
6968       if (token->type == CPP_CLOSE_BRACE
6969           || token->type == CPP_EOF
6970           || token->type == CPP_PRAGMA_EOL)
6971         break;
6972
6973       if (token->type == CPP_SEMICOLON)
6974         {
6975           /* A declaration consisting of a single semicolon is
6976              invalid.  Allow it unless we're being pedantic.  */
6977           cp_lexer_consume_token (parser->lexer);
6978           if (pedantic && !in_system_header)
6979             pedwarn ("extra %<;%>");
6980           continue;
6981         }
6982
6983       /* If we're entering or exiting a region that's implicitly
6984          extern "C", modify the lang context appropriately.  */
6985       if (!parser->implicit_extern_c && token->implicit_extern_c)
6986         {
6987           push_lang_context (lang_name_c);
6988           parser->implicit_extern_c = true;
6989         }
6990       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6991         {
6992           pop_lang_context ();
6993           parser->implicit_extern_c = false;
6994         }
6995
6996       if (token->type == CPP_PRAGMA)
6997         {
6998           /* A top-level declaration can consist solely of a #pragma.
6999              A nested declaration cannot, so this is done here and not
7000              in cp_parser_declaration.  (A #pragma at block scope is
7001              handled in cp_parser_statement.)  */
7002           cp_parser_pragma (parser, pragma_external);
7003           continue;
7004         }
7005
7006       /* Parse the declaration itself.  */
7007       cp_parser_declaration (parser);
7008     }
7009 }
7010
7011 /* Parse a declaration.
7012
7013    declaration:
7014      block-declaration
7015      function-definition
7016      template-declaration
7017      explicit-instantiation
7018      explicit-specialization
7019      linkage-specification
7020      namespace-definition
7021
7022    GNU extension:
7023
7024    declaration:
7025       __extension__ declaration */
7026
7027 static void
7028 cp_parser_declaration (cp_parser* parser)
7029 {
7030   cp_token token1;
7031   cp_token token2;
7032   int saved_pedantic;
7033   void *p;
7034
7035   /* Check for the `__extension__' keyword.  */
7036   if (cp_parser_extension_opt (parser, &saved_pedantic))
7037     {
7038       /* Parse the qualified declaration.  */
7039       cp_parser_declaration (parser);
7040       /* Restore the PEDANTIC flag.  */
7041       pedantic = saved_pedantic;
7042
7043       return;
7044     }
7045
7046   /* Try to figure out what kind of declaration is present.  */
7047   token1 = *cp_lexer_peek_token (parser->lexer);
7048
7049   if (token1.type != CPP_EOF)
7050     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7051   else
7052     {
7053       token2.type = CPP_EOF;
7054       token2.keyword = RID_MAX;
7055     }
7056
7057   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7058   p = obstack_alloc (&declarator_obstack, 0);
7059
7060   /* If the next token is `extern' and the following token is a string
7061      literal, then we have a linkage specification.  */
7062   if (token1.keyword == RID_EXTERN
7063       && cp_parser_is_string_literal (&token2))
7064     cp_parser_linkage_specification (parser);
7065   /* If the next token is `template', then we have either a template
7066      declaration, an explicit instantiation, or an explicit
7067      specialization.  */
7068   else if (token1.keyword == RID_TEMPLATE)
7069     {
7070       /* `template <>' indicates a template specialization.  */
7071       if (token2.type == CPP_LESS
7072           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7073         cp_parser_explicit_specialization (parser);
7074       /* `template <' indicates a template declaration.  */
7075       else if (token2.type == CPP_LESS)
7076         cp_parser_template_declaration (parser, /*member_p=*/false);
7077       /* Anything else must be an explicit instantiation.  */
7078       else
7079         cp_parser_explicit_instantiation (parser);
7080     }
7081   /* If the next token is `export', then we have a template
7082      declaration.  */
7083   else if (token1.keyword == RID_EXPORT)
7084     cp_parser_template_declaration (parser, /*member_p=*/false);
7085   /* If the next token is `extern', 'static' or 'inline' and the one
7086      after that is `template', we have a GNU extended explicit
7087      instantiation directive.  */
7088   else if (cp_parser_allow_gnu_extensions_p (parser)
7089            && (token1.keyword == RID_EXTERN
7090                || token1.keyword == RID_STATIC
7091                || token1.keyword == RID_INLINE)
7092            && token2.keyword == RID_TEMPLATE)
7093     cp_parser_explicit_instantiation (parser);
7094   /* If the next token is `namespace', check for a named or unnamed
7095      namespace definition.  */
7096   else if (token1.keyword == RID_NAMESPACE
7097            && (/* A named namespace definition.  */
7098                (token2.type == CPP_NAME
7099                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7100                     != CPP_EQ))
7101                /* An unnamed namespace definition.  */
7102                || token2.type == CPP_OPEN_BRACE
7103                || token2.keyword == RID_ATTRIBUTE))
7104     cp_parser_namespace_definition (parser);
7105   /* Objective-C++ declaration/definition.  */
7106   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7107     cp_parser_objc_declaration (parser);
7108   /* We must have either a block declaration or a function
7109      definition.  */
7110   else
7111     /* Try to parse a block-declaration, or a function-definition.  */
7112     cp_parser_block_declaration (parser, /*statement_p=*/false);
7113
7114   /* Free any declarators allocated.  */
7115   obstack_free (&declarator_obstack, p);
7116 }
7117
7118 /* Parse a block-declaration.
7119
7120    block-declaration:
7121      simple-declaration
7122      asm-definition
7123      namespace-alias-definition
7124      using-declaration
7125      using-directive
7126
7127    GNU Extension:
7128
7129    block-declaration:
7130      __extension__ block-declaration
7131      label-declaration
7132
7133    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7134    part of a declaration-statement.  */
7135
7136 static void
7137 cp_parser_block_declaration (cp_parser *parser,
7138                              bool      statement_p)
7139 {
7140   cp_token *token1;
7141   int saved_pedantic;
7142
7143   /* Check for the `__extension__' keyword.  */
7144   if (cp_parser_extension_opt (parser, &saved_pedantic))
7145     {
7146       /* Parse the qualified declaration.  */
7147       cp_parser_block_declaration (parser, statement_p);
7148       /* Restore the PEDANTIC flag.  */
7149       pedantic = saved_pedantic;
7150
7151       return;
7152     }
7153
7154   /* Peek at the next token to figure out which kind of declaration is
7155      present.  */
7156   token1 = cp_lexer_peek_token (parser->lexer);
7157
7158   /* If the next keyword is `asm', we have an asm-definition.  */
7159   if (token1->keyword == RID_ASM)
7160     {
7161       if (statement_p)
7162         cp_parser_commit_to_tentative_parse (parser);
7163       cp_parser_asm_definition (parser);
7164     }
7165   /* If the next keyword is `namespace', we have a
7166      namespace-alias-definition.  */
7167   else if (token1->keyword == RID_NAMESPACE)
7168     cp_parser_namespace_alias_definition (parser);
7169   /* If the next keyword is `using', we have either a
7170      using-declaration or a using-directive.  */
7171   else if (token1->keyword == RID_USING)
7172     {
7173       cp_token *token2;
7174
7175       if (statement_p)
7176         cp_parser_commit_to_tentative_parse (parser);
7177       /* If the token after `using' is `namespace', then we have a
7178          using-directive.  */
7179       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7180       if (token2->keyword == RID_NAMESPACE)
7181         cp_parser_using_directive (parser);
7182       /* Otherwise, it's a using-declaration.  */
7183       else
7184         cp_parser_using_declaration (parser);
7185     }
7186   /* If the next keyword is `__label__' we have a label declaration.  */
7187   else if (token1->keyword == RID_LABEL)
7188     {
7189       if (statement_p)
7190         cp_parser_commit_to_tentative_parse (parser);
7191       cp_parser_label_declaration (parser);
7192     }
7193   /* Anything else must be a simple-declaration.  */
7194   else
7195     cp_parser_simple_declaration (parser, !statement_p);
7196 }
7197
7198 /* Parse a simple-declaration.
7199
7200    simple-declaration:
7201      decl-specifier-seq [opt] init-declarator-list [opt] ;
7202
7203    init-declarator-list:
7204      init-declarator
7205      init-declarator-list , init-declarator
7206
7207    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7208    function-definition as a simple-declaration.  */
7209
7210 static void
7211 cp_parser_simple_declaration (cp_parser* parser,
7212                               bool function_definition_allowed_p)
7213 {
7214   cp_decl_specifier_seq decl_specifiers;
7215   int declares_class_or_enum;
7216   bool saw_declarator;
7217
7218   /* Defer access checks until we know what is being declared; the
7219      checks for names appearing in the decl-specifier-seq should be
7220      done as if we were in the scope of the thing being declared.  */
7221   push_deferring_access_checks (dk_deferred);
7222
7223   /* Parse the decl-specifier-seq.  We have to keep track of whether
7224      or not the decl-specifier-seq declares a named class or
7225      enumeration type, since that is the only case in which the
7226      init-declarator-list is allowed to be empty.
7227
7228      [dcl.dcl]
7229
7230      In a simple-declaration, the optional init-declarator-list can be
7231      omitted only when declaring a class or enumeration, that is when
7232      the decl-specifier-seq contains either a class-specifier, an
7233      elaborated-type-specifier, or an enum-specifier.  */
7234   cp_parser_decl_specifier_seq (parser,
7235                                 CP_PARSER_FLAGS_OPTIONAL,
7236                                 &decl_specifiers,
7237                                 &declares_class_or_enum);
7238   /* We no longer need to defer access checks.  */
7239   stop_deferring_access_checks ();
7240
7241   /* In a block scope, a valid declaration must always have a
7242      decl-specifier-seq.  By not trying to parse declarators, we can
7243      resolve the declaration/expression ambiguity more quickly.  */
7244   if (!function_definition_allowed_p
7245       && !decl_specifiers.any_specifiers_p)
7246     {
7247       cp_parser_error (parser, "expected declaration");
7248       goto done;
7249     }
7250
7251   /* If the next two tokens are both identifiers, the code is
7252      erroneous. The usual cause of this situation is code like:
7253
7254        T t;
7255
7256      where "T" should name a type -- but does not.  */
7257   if (!decl_specifiers.type
7258       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7259     {
7260       /* If parsing tentatively, we should commit; we really are
7261          looking at a declaration.  */
7262       cp_parser_commit_to_tentative_parse (parser);
7263       /* Give up.  */
7264       goto done;
7265     }
7266
7267   /* If we have seen at least one decl-specifier, and the next token
7268      is not a parenthesis, then we must be looking at a declaration.
7269      (After "int (" we might be looking at a functional cast.)  */
7270   if (decl_specifiers.any_specifiers_p
7271       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7272     cp_parser_commit_to_tentative_parse (parser);
7273
7274   /* Keep going until we hit the `;' at the end of the simple
7275      declaration.  */
7276   saw_declarator = false;
7277   while (cp_lexer_next_token_is_not (parser->lexer,
7278                                      CPP_SEMICOLON))
7279     {
7280       cp_token *token;
7281       bool function_definition_p;
7282       tree decl;
7283
7284       if (saw_declarator)
7285         {
7286           /* If we are processing next declarator, coma is expected */
7287           token = cp_lexer_peek_token (parser->lexer);
7288           gcc_assert (token->type == CPP_COMMA);
7289           cp_lexer_consume_token (parser->lexer);
7290         }
7291       else
7292         saw_declarator = true;
7293
7294       /* Parse the init-declarator.  */
7295       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7296                                         /*checks=*/NULL_TREE,
7297                                         function_definition_allowed_p,
7298                                         /*member_p=*/false,
7299                                         declares_class_or_enum,
7300                                         &function_definition_p);
7301       /* If an error occurred while parsing tentatively, exit quickly.
7302          (That usually happens when in the body of a function; each
7303          statement is treated as a declaration-statement until proven
7304          otherwise.)  */
7305       if (cp_parser_error_occurred (parser))
7306         goto done;
7307       /* Handle function definitions specially.  */
7308       if (function_definition_p)
7309         {
7310           /* If the next token is a `,', then we are probably
7311              processing something like:
7312
7313                void f() {}, *p;
7314
7315              which is erroneous.  */
7316           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7317             error ("mixing declarations and function-definitions is forbidden");
7318           /* Otherwise, we're done with the list of declarators.  */
7319           else
7320             {
7321               pop_deferring_access_checks ();
7322               return;
7323             }
7324         }
7325       /* The next token should be either a `,' or a `;'.  */
7326       token = cp_lexer_peek_token (parser->lexer);
7327       /* If it's a `,', there are more declarators to come.  */
7328       if (token->type == CPP_COMMA)
7329         /* will be consumed next time around */;
7330       /* If it's a `;', we are done.  */
7331       else if (token->type == CPP_SEMICOLON)
7332         break;
7333       /* Anything else is an error.  */
7334       else
7335         {
7336           /* If we have already issued an error message we don't need
7337              to issue another one.  */
7338           if (decl != error_mark_node
7339               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7340             cp_parser_error (parser, "expected %<,%> or %<;%>");
7341           /* Skip tokens until we reach the end of the statement.  */
7342           cp_parser_skip_to_end_of_statement (parser);
7343           /* If the next token is now a `;', consume it.  */
7344           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7345             cp_lexer_consume_token (parser->lexer);
7346           goto done;
7347         }
7348       /* After the first time around, a function-definition is not
7349          allowed -- even if it was OK at first.  For example:
7350
7351            int i, f() {}
7352
7353          is not valid.  */
7354       function_definition_allowed_p = false;
7355     }
7356
7357   /* Issue an error message if no declarators are present, and the
7358      decl-specifier-seq does not itself declare a class or
7359      enumeration.  */
7360   if (!saw_declarator)
7361     {
7362       if (cp_parser_declares_only_class_p (parser))
7363         shadow_tag (&decl_specifiers);
7364       /* Perform any deferred access checks.  */
7365       perform_deferred_access_checks ();
7366     }
7367
7368   /* Consume the `;'.  */
7369   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7370
7371  done:
7372   pop_deferring_access_checks ();
7373 }
7374
7375 /* Parse a decl-specifier-seq.
7376
7377    decl-specifier-seq:
7378      decl-specifier-seq [opt] decl-specifier
7379
7380    decl-specifier:
7381      storage-class-specifier
7382      type-specifier
7383      function-specifier
7384      friend
7385      typedef
7386
7387    GNU Extension:
7388
7389    decl-specifier:
7390      attributes
7391
7392    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7393
7394    The parser flags FLAGS is used to control type-specifier parsing.
7395
7396    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7397    flags:
7398
7399      1: one of the decl-specifiers is an elaborated-type-specifier
7400         (i.e., a type declaration)
7401      2: one of the decl-specifiers is an enum-specifier or a
7402         class-specifier (i.e., a type definition)
7403
7404    */
7405
7406 static void
7407 cp_parser_decl_specifier_seq (cp_parser* parser,
7408                               cp_parser_flags flags,
7409                               cp_decl_specifier_seq *decl_specs,
7410                               int* declares_class_or_enum)
7411 {
7412   bool constructor_possible_p = !parser->in_declarator_p;
7413
7414   /* Clear DECL_SPECS.  */
7415   clear_decl_specs (decl_specs);
7416
7417   /* Assume no class or enumeration type is declared.  */
7418   *declares_class_or_enum = 0;
7419
7420   /* Keep reading specifiers until there are no more to read.  */
7421   while (true)
7422     {
7423       bool constructor_p;
7424       bool found_decl_spec;
7425       cp_token *token;
7426
7427       /* Peek at the next token.  */
7428       token = cp_lexer_peek_token (parser->lexer);
7429       /* Handle attributes.  */
7430       if (token->keyword == RID_ATTRIBUTE)
7431         {
7432           /* Parse the attributes.  */
7433           decl_specs->attributes
7434             = chainon (decl_specs->attributes,
7435                        cp_parser_attributes_opt (parser));
7436           continue;
7437         }
7438       /* Assume we will find a decl-specifier keyword.  */
7439       found_decl_spec = true;
7440       /* If the next token is an appropriate keyword, we can simply
7441          add it to the list.  */
7442       switch (token->keyword)
7443         {
7444           /* decl-specifier:
7445                friend  */
7446         case RID_FRIEND:
7447           if (!at_class_scope_p ())
7448             {
7449               error ("%<friend%> used outside of class");
7450               cp_lexer_purge_token (parser->lexer);
7451             }
7452           else
7453             {
7454               ++decl_specs->specs[(int) ds_friend];
7455               /* Consume the token.  */
7456               cp_lexer_consume_token (parser->lexer);
7457             }
7458           break;
7459
7460           /* function-specifier:
7461                inline
7462                virtual
7463                explicit  */
7464         case RID_INLINE:
7465         case RID_VIRTUAL:
7466         case RID_EXPLICIT:
7467           cp_parser_function_specifier_opt (parser, decl_specs);
7468           break;
7469
7470           /* decl-specifier:
7471                typedef  */
7472         case RID_TYPEDEF:
7473           ++decl_specs->specs[(int) ds_typedef];
7474           /* Consume the token.  */
7475           cp_lexer_consume_token (parser->lexer);
7476           /* A constructor declarator cannot appear in a typedef.  */
7477           constructor_possible_p = false;
7478           /* The "typedef" keyword can only occur in a declaration; we
7479              may as well commit at this point.  */
7480           cp_parser_commit_to_tentative_parse (parser);
7481           break;
7482
7483           /* storage-class-specifier:
7484                auto
7485                register
7486                static
7487                extern
7488                mutable
7489
7490              GNU Extension:
7491                thread  */
7492         case RID_AUTO:
7493         case RID_REGISTER:
7494         case RID_STATIC:
7495         case RID_EXTERN:
7496         case RID_MUTABLE:
7497           /* Consume the token.  */
7498           cp_lexer_consume_token (parser->lexer);
7499           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7500           break;
7501         case RID_THREAD:
7502           /* Consume the token.  */
7503           cp_lexer_consume_token (parser->lexer);
7504           ++decl_specs->specs[(int) ds_thread];
7505           break;
7506
7507         default:
7508           /* We did not yet find a decl-specifier yet.  */
7509           found_decl_spec = false;
7510           break;
7511         }
7512
7513       /* Constructors are a special case.  The `S' in `S()' is not a
7514          decl-specifier; it is the beginning of the declarator.  */
7515       constructor_p
7516         = (!found_decl_spec
7517            && constructor_possible_p
7518            && (cp_parser_constructor_declarator_p
7519                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7520
7521       /* If we don't have a DECL_SPEC yet, then we must be looking at
7522          a type-specifier.  */
7523       if (!found_decl_spec && !constructor_p)
7524         {
7525           int decl_spec_declares_class_or_enum;
7526           bool is_cv_qualifier;
7527           tree type_spec;
7528
7529           type_spec
7530             = cp_parser_type_specifier (parser, flags,
7531                                         decl_specs,
7532                                         /*is_declaration=*/true,
7533                                         &decl_spec_declares_class_or_enum,
7534                                         &is_cv_qualifier);
7535
7536           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7537
7538           /* If this type-specifier referenced a user-defined type
7539              (a typedef, class-name, etc.), then we can't allow any
7540              more such type-specifiers henceforth.
7541
7542              [dcl.spec]
7543
7544              The longest sequence of decl-specifiers that could
7545              possibly be a type name is taken as the
7546              decl-specifier-seq of a declaration.  The sequence shall
7547              be self-consistent as described below.
7548
7549              [dcl.type]
7550
7551              As a general rule, at most one type-specifier is allowed
7552              in the complete decl-specifier-seq of a declaration.  The
7553              only exceptions are the following:
7554
7555              -- const or volatile can be combined with any other
7556                 type-specifier.
7557
7558              -- signed or unsigned can be combined with char, long,
7559                 short, or int.
7560
7561              -- ..
7562
7563              Example:
7564
7565                typedef char* Pc;
7566                void g (const int Pc);
7567
7568              Here, Pc is *not* part of the decl-specifier seq; it's
7569              the declarator.  Therefore, once we see a type-specifier
7570              (other than a cv-qualifier), we forbid any additional
7571              user-defined types.  We *do* still allow things like `int
7572              int' to be considered a decl-specifier-seq, and issue the
7573              error message later.  */
7574           if (type_spec && !is_cv_qualifier)
7575             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7576           /* A constructor declarator cannot follow a type-specifier.  */
7577           if (type_spec)
7578             {
7579               constructor_possible_p = false;
7580               found_decl_spec = true;
7581             }
7582         }
7583
7584       /* If we still do not have a DECL_SPEC, then there are no more
7585          decl-specifiers.  */
7586       if (!found_decl_spec)
7587         break;
7588
7589       decl_specs->any_specifiers_p = true;
7590       /* After we see one decl-specifier, further decl-specifiers are
7591          always optional.  */
7592       flags |= CP_PARSER_FLAGS_OPTIONAL;
7593     }
7594
7595   cp_parser_check_decl_spec (decl_specs);
7596
7597   /* Don't allow a friend specifier with a class definition.  */
7598   if (decl_specs->specs[(int) ds_friend] != 0
7599       && (*declares_class_or_enum & 2))
7600     error ("class definition may not be declared a friend");
7601 }
7602
7603 /* Parse an (optional) storage-class-specifier.
7604
7605    storage-class-specifier:
7606      auto
7607      register
7608      static
7609      extern
7610      mutable
7611
7612    GNU Extension:
7613
7614    storage-class-specifier:
7615      thread
7616
7617    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7618
7619 static tree
7620 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7621 {
7622   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7623     {
7624     case RID_AUTO:
7625     case RID_REGISTER:
7626     case RID_STATIC:
7627     case RID_EXTERN:
7628     case RID_MUTABLE:
7629     case RID_THREAD:
7630       /* Consume the token.  */
7631       return cp_lexer_consume_token (parser->lexer)->value;
7632
7633     default:
7634       return NULL_TREE;
7635     }
7636 }
7637
7638 /* Parse an (optional) function-specifier.
7639
7640    function-specifier:
7641      inline
7642      virtual
7643      explicit
7644
7645    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7646    Updates DECL_SPECS, if it is non-NULL.  */
7647
7648 static tree
7649 cp_parser_function_specifier_opt (cp_parser* parser,
7650                                   cp_decl_specifier_seq *decl_specs)
7651 {
7652   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7653     {
7654     case RID_INLINE:
7655       if (decl_specs)
7656         ++decl_specs->specs[(int) ds_inline];
7657       break;
7658
7659     case RID_VIRTUAL:
7660       /* 14.5.2.3 [temp.mem]
7661
7662          A member function template shall not be virtual.  */
7663       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7664         error ("templates may not be %<virtual%>");
7665       else if (decl_specs)
7666         ++decl_specs->specs[(int) ds_virtual];
7667       break;
7668
7669     case RID_EXPLICIT:
7670       if (decl_specs)
7671         ++decl_specs->specs[(int) ds_explicit];
7672       break;
7673
7674     default:
7675       return NULL_TREE;
7676     }
7677
7678   /* Consume the token.  */
7679   return cp_lexer_consume_token (parser->lexer)->value;
7680 }
7681
7682 /* Parse a linkage-specification.
7683
7684    linkage-specification:
7685      extern string-literal { declaration-seq [opt] }
7686      extern string-literal declaration  */
7687
7688 static void
7689 cp_parser_linkage_specification (cp_parser* parser)
7690 {
7691   tree linkage;
7692
7693   /* Look for the `extern' keyword.  */
7694   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7695
7696   /* Look for the string-literal.  */
7697   linkage = cp_parser_string_literal (parser, false, false);
7698
7699   /* Transform the literal into an identifier.  If the literal is a
7700      wide-character string, or contains embedded NULs, then we can't
7701      handle it as the user wants.  */
7702   if (strlen (TREE_STRING_POINTER (linkage))
7703       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7704     {
7705       cp_parser_error (parser, "invalid linkage-specification");
7706       /* Assume C++ linkage.  */
7707       linkage = lang_name_cplusplus;
7708     }
7709   else
7710     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7711
7712   /* We're now using the new linkage.  */
7713   push_lang_context (linkage);
7714
7715   /* If the next token is a `{', then we're using the first
7716      production.  */
7717   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7718     {
7719       /* Consume the `{' token.  */
7720       cp_lexer_consume_token (parser->lexer);
7721       /* Parse the declarations.  */
7722       cp_parser_declaration_seq_opt (parser);
7723       /* Look for the closing `}'.  */
7724       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7725     }
7726   /* Otherwise, there's just one declaration.  */
7727   else
7728     {
7729       bool saved_in_unbraced_linkage_specification_p;
7730
7731       saved_in_unbraced_linkage_specification_p
7732         = parser->in_unbraced_linkage_specification_p;
7733       parser->in_unbraced_linkage_specification_p = true;
7734       cp_parser_declaration (parser);
7735       parser->in_unbraced_linkage_specification_p
7736         = saved_in_unbraced_linkage_specification_p;
7737     }
7738
7739   /* We're done with the linkage-specification.  */
7740   pop_lang_context ();
7741 }
7742
7743 /* Special member functions [gram.special] */
7744
7745 /* Parse a conversion-function-id.
7746
7747    conversion-function-id:
7748      operator conversion-type-id
7749
7750    Returns an IDENTIFIER_NODE representing the operator.  */
7751
7752 static tree
7753 cp_parser_conversion_function_id (cp_parser* parser)
7754 {
7755   tree type;
7756   tree saved_scope;
7757   tree saved_qualifying_scope;
7758   tree saved_object_scope;
7759   tree pushed_scope = NULL_TREE;
7760
7761   /* Look for the `operator' token.  */
7762   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7763     return error_mark_node;
7764   /* When we parse the conversion-type-id, the current scope will be
7765      reset.  However, we need that information in able to look up the
7766      conversion function later, so we save it here.  */
7767   saved_scope = parser->scope;
7768   saved_qualifying_scope = parser->qualifying_scope;
7769   saved_object_scope = parser->object_scope;
7770   /* We must enter the scope of the class so that the names of
7771      entities declared within the class are available in the
7772      conversion-type-id.  For example, consider:
7773
7774        struct S {
7775          typedef int I;
7776          operator I();
7777        };
7778
7779        S::operator I() { ... }
7780
7781      In order to see that `I' is a type-name in the definition, we
7782      must be in the scope of `S'.  */
7783   if (saved_scope)
7784     pushed_scope = push_scope (saved_scope);
7785   /* Parse the conversion-type-id.  */
7786   type = cp_parser_conversion_type_id (parser);
7787   /* Leave the scope of the class, if any.  */
7788   if (pushed_scope)
7789     pop_scope (pushed_scope);
7790   /* Restore the saved scope.  */
7791   parser->scope = saved_scope;
7792   parser->qualifying_scope = saved_qualifying_scope;
7793   parser->object_scope = saved_object_scope;
7794   /* If the TYPE is invalid, indicate failure.  */
7795   if (type == error_mark_node)
7796     return error_mark_node;
7797   return mangle_conv_op_name_for_type (type);
7798 }
7799
7800 /* Parse a conversion-type-id:
7801
7802    conversion-type-id:
7803      type-specifier-seq conversion-declarator [opt]
7804
7805    Returns the TYPE specified.  */
7806
7807 static tree
7808 cp_parser_conversion_type_id (cp_parser* parser)
7809 {
7810   tree attributes;
7811   cp_decl_specifier_seq type_specifiers;
7812   cp_declarator *declarator;
7813   tree type_specified;
7814
7815   /* Parse the attributes.  */
7816   attributes = cp_parser_attributes_opt (parser);
7817   /* Parse the type-specifiers.  */
7818   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7819                                 &type_specifiers);
7820   /* If that didn't work, stop.  */
7821   if (type_specifiers.type == error_mark_node)
7822     return error_mark_node;
7823   /* Parse the conversion-declarator.  */
7824   declarator = cp_parser_conversion_declarator_opt (parser);
7825
7826   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7827                                     /*initialized=*/0, &attributes);
7828   if (attributes)
7829     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7830   return type_specified;
7831 }
7832
7833 /* Parse an (optional) conversion-declarator.
7834
7835    conversion-declarator:
7836      ptr-operator conversion-declarator [opt]
7837
7838    */
7839
7840 static cp_declarator *
7841 cp_parser_conversion_declarator_opt (cp_parser* parser)
7842 {
7843   enum tree_code code;
7844   tree class_type;
7845   cp_cv_quals cv_quals;
7846
7847   /* We don't know if there's a ptr-operator next, or not.  */
7848   cp_parser_parse_tentatively (parser);
7849   /* Try the ptr-operator.  */
7850   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7851   /* If it worked, look for more conversion-declarators.  */
7852   if (cp_parser_parse_definitely (parser))
7853     {
7854       cp_declarator *declarator;
7855
7856       /* Parse another optional declarator.  */
7857       declarator = cp_parser_conversion_declarator_opt (parser);
7858
7859       /* Create the representation of the declarator.  */
7860       if (class_type)
7861         declarator = make_ptrmem_declarator (cv_quals, class_type,
7862                                              declarator);
7863       else if (code == INDIRECT_REF)
7864         declarator = make_pointer_declarator (cv_quals, declarator);
7865       else
7866         declarator = make_reference_declarator (cv_quals, declarator);
7867
7868       return declarator;
7869    }
7870
7871   return NULL;
7872 }
7873
7874 /* Parse an (optional) ctor-initializer.
7875
7876    ctor-initializer:
7877      : mem-initializer-list
7878
7879    Returns TRUE iff the ctor-initializer was actually present.  */
7880
7881 static bool
7882 cp_parser_ctor_initializer_opt (cp_parser* parser)
7883 {
7884   /* If the next token is not a `:', then there is no
7885      ctor-initializer.  */
7886   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7887     {
7888       /* Do default initialization of any bases and members.  */
7889       if (DECL_CONSTRUCTOR_P (current_function_decl))
7890         finish_mem_initializers (NULL_TREE);
7891
7892       return false;
7893     }
7894
7895   /* Consume the `:' token.  */
7896   cp_lexer_consume_token (parser->lexer);
7897   /* And the mem-initializer-list.  */
7898   cp_parser_mem_initializer_list (parser);
7899
7900   return true;
7901 }
7902
7903 /* Parse a mem-initializer-list.
7904
7905    mem-initializer-list:
7906      mem-initializer
7907      mem-initializer , mem-initializer-list  */
7908
7909 static void
7910 cp_parser_mem_initializer_list (cp_parser* parser)
7911 {
7912   tree mem_initializer_list = NULL_TREE;
7913
7914   /* Let the semantic analysis code know that we are starting the
7915      mem-initializer-list.  */
7916   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7917     error ("only constructors take base initializers");
7918
7919   /* Loop through the list.  */
7920   while (true)
7921     {
7922       tree mem_initializer;
7923
7924       /* Parse the mem-initializer.  */
7925       mem_initializer = cp_parser_mem_initializer (parser);
7926       /* Add it to the list, unless it was erroneous.  */
7927       if (mem_initializer != error_mark_node)
7928         {
7929           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7930           mem_initializer_list = mem_initializer;
7931         }
7932       /* If the next token is not a `,', we're done.  */
7933       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7934         break;
7935       /* Consume the `,' token.  */
7936       cp_lexer_consume_token (parser->lexer);
7937     }
7938
7939   /* Perform semantic analysis.  */
7940   if (DECL_CONSTRUCTOR_P (current_function_decl))
7941     finish_mem_initializers (mem_initializer_list);
7942 }
7943
7944 /* Parse a mem-initializer.
7945
7946    mem-initializer:
7947      mem-initializer-id ( expression-list [opt] )
7948
7949    GNU extension:
7950
7951    mem-initializer:
7952      ( expression-list [opt] )
7953
7954    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7955    class) or FIELD_DECL (for a non-static data member) to initialize;
7956    the TREE_VALUE is the expression-list.  An empty initialization
7957    list is represented by void_list_node.  */
7958
7959 static tree
7960 cp_parser_mem_initializer (cp_parser* parser)
7961 {
7962   tree mem_initializer_id;
7963   tree expression_list;
7964   tree member;
7965
7966   /* Find out what is being initialized.  */
7967   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7968     {
7969       pedwarn ("anachronistic old-style base class initializer");
7970       mem_initializer_id = NULL_TREE;
7971     }
7972   else
7973     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7974   member = expand_member_init (mem_initializer_id);
7975   if (member && !DECL_P (member))
7976     in_base_initializer = 1;
7977
7978   expression_list
7979     = cp_parser_parenthesized_expression_list (parser, false,
7980                                                /*cast_p=*/false,
7981                                                /*non_constant_p=*/NULL);
7982   if (expression_list == error_mark_node)
7983     return error_mark_node;
7984   if (!expression_list)
7985     expression_list = void_type_node;
7986
7987   in_base_initializer = 0;
7988
7989   return member ? build_tree_list (member, expression_list) : error_mark_node;
7990 }
7991
7992 /* Parse a mem-initializer-id.
7993
7994    mem-initializer-id:
7995      :: [opt] nested-name-specifier [opt] class-name
7996      identifier
7997
7998    Returns a TYPE indicating the class to be initializer for the first
7999    production.  Returns an IDENTIFIER_NODE indicating the data member
8000    to be initialized for the second production.  */
8001
8002 static tree
8003 cp_parser_mem_initializer_id (cp_parser* parser)
8004 {
8005   bool global_scope_p;
8006   bool nested_name_specifier_p;
8007   bool template_p = false;
8008   tree id;
8009
8010   /* `typename' is not allowed in this context ([temp.res]).  */
8011   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8012     {
8013       error ("keyword %<typename%> not allowed in this context (a qualified "
8014              "member initializer is implicitly a type)");
8015       cp_lexer_consume_token (parser->lexer);
8016     }
8017   /* Look for the optional `::' operator.  */
8018   global_scope_p
8019     = (cp_parser_global_scope_opt (parser,
8020                                    /*current_scope_valid_p=*/false)
8021        != NULL_TREE);
8022   /* Look for the optional nested-name-specifier.  The simplest way to
8023      implement:
8024
8025        [temp.res]
8026
8027        The keyword `typename' is not permitted in a base-specifier or
8028        mem-initializer; in these contexts a qualified name that
8029        depends on a template-parameter is implicitly assumed to be a
8030        type name.
8031
8032      is to assume that we have seen the `typename' keyword at this
8033      point.  */
8034   nested_name_specifier_p
8035     = (cp_parser_nested_name_specifier_opt (parser,
8036                                             /*typename_keyword_p=*/true,
8037                                             /*check_dependency_p=*/true,
8038                                             /*type_p=*/true,
8039                                             /*is_declaration=*/true)
8040        != NULL_TREE);
8041   if (nested_name_specifier_p)
8042     template_p = cp_parser_optional_template_keyword (parser);
8043   /* If there is a `::' operator or a nested-name-specifier, then we
8044      are definitely looking for a class-name.  */
8045   if (global_scope_p || nested_name_specifier_p)
8046     return cp_parser_class_name (parser,
8047                                  /*typename_keyword_p=*/true,
8048                                  /*template_keyword_p=*/template_p,
8049                                  none_type,
8050                                  /*check_dependency_p=*/true,
8051                                  /*class_head_p=*/false,
8052                                  /*is_declaration=*/true);
8053   /* Otherwise, we could also be looking for an ordinary identifier.  */
8054   cp_parser_parse_tentatively (parser);
8055   /* Try a class-name.  */
8056   id = cp_parser_class_name (parser,
8057                              /*typename_keyword_p=*/true,
8058                              /*template_keyword_p=*/false,
8059                              none_type,
8060                              /*check_dependency_p=*/true,
8061                              /*class_head_p=*/false,
8062                              /*is_declaration=*/true);
8063   /* If we found one, we're done.  */
8064   if (cp_parser_parse_definitely (parser))
8065     return id;
8066   /* Otherwise, look for an ordinary identifier.  */
8067   return cp_parser_identifier (parser);
8068 }
8069
8070 /* Overloading [gram.over] */
8071
8072 /* Parse an operator-function-id.
8073
8074    operator-function-id:
8075      operator operator
8076
8077    Returns an IDENTIFIER_NODE for the operator which is a
8078    human-readable spelling of the identifier, e.g., `operator +'.  */
8079
8080 static tree
8081 cp_parser_operator_function_id (cp_parser* parser)
8082 {
8083   /* Look for the `operator' keyword.  */
8084   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8085     return error_mark_node;
8086   /* And then the name of the operator itself.  */
8087   return cp_parser_operator (parser);
8088 }
8089
8090 /* Parse an operator.
8091
8092    operator:
8093      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8094      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8095      || ++ -- , ->* -> () []
8096
8097    GNU Extensions:
8098
8099    operator:
8100      <? >? <?= >?=
8101
8102    Returns an IDENTIFIER_NODE for the operator which is a
8103    human-readable spelling of the identifier, e.g., `operator +'.  */
8104
8105 static tree
8106 cp_parser_operator (cp_parser* parser)
8107 {
8108   tree id = NULL_TREE;
8109   cp_token *token;
8110
8111   /* Peek at the next token.  */
8112   token = cp_lexer_peek_token (parser->lexer);
8113   /* Figure out which operator we have.  */
8114   switch (token->type)
8115     {
8116     case CPP_KEYWORD:
8117       {
8118         enum tree_code op;
8119
8120         /* The keyword should be either `new' or `delete'.  */
8121         if (token->keyword == RID_NEW)
8122           op = NEW_EXPR;
8123         else if (token->keyword == RID_DELETE)
8124           op = DELETE_EXPR;
8125         else
8126           break;
8127
8128         /* Consume the `new' or `delete' token.  */
8129         cp_lexer_consume_token (parser->lexer);
8130
8131         /* Peek at the next token.  */
8132         token = cp_lexer_peek_token (parser->lexer);
8133         /* If it's a `[' token then this is the array variant of the
8134            operator.  */
8135         if (token->type == CPP_OPEN_SQUARE)
8136           {
8137             /* Consume the `[' token.  */
8138             cp_lexer_consume_token (parser->lexer);
8139             /* Look for the `]' token.  */
8140             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8141             id = ansi_opname (op == NEW_EXPR
8142                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8143           }
8144         /* Otherwise, we have the non-array variant.  */
8145         else
8146           id = ansi_opname (op);
8147
8148         return id;
8149       }
8150
8151     case CPP_PLUS:
8152       id = ansi_opname (PLUS_EXPR);
8153       break;
8154
8155     case CPP_MINUS:
8156       id = ansi_opname (MINUS_EXPR);
8157       break;
8158
8159     case CPP_MULT:
8160       id = ansi_opname (MULT_EXPR);
8161       break;
8162
8163     case CPP_DIV:
8164       id = ansi_opname (TRUNC_DIV_EXPR);
8165       break;
8166
8167     case CPP_MOD:
8168       id = ansi_opname (TRUNC_MOD_EXPR);
8169       break;
8170
8171     case CPP_XOR:
8172       id = ansi_opname (BIT_XOR_EXPR);
8173       break;
8174
8175     case CPP_AND:
8176       id = ansi_opname (BIT_AND_EXPR);
8177       break;
8178
8179     case CPP_OR:
8180       id = ansi_opname (BIT_IOR_EXPR);
8181       break;
8182
8183     case CPP_COMPL:
8184       id = ansi_opname (BIT_NOT_EXPR);
8185       break;
8186
8187     case CPP_NOT:
8188       id = ansi_opname (TRUTH_NOT_EXPR);
8189       break;
8190
8191     case CPP_EQ:
8192       id = ansi_assopname (NOP_EXPR);
8193       break;
8194
8195     case CPP_LESS:
8196       id = ansi_opname (LT_EXPR);
8197       break;
8198
8199     case CPP_GREATER:
8200       id = ansi_opname (GT_EXPR);
8201       break;
8202
8203     case CPP_PLUS_EQ:
8204       id = ansi_assopname (PLUS_EXPR);
8205       break;
8206
8207     case CPP_MINUS_EQ:
8208       id = ansi_assopname (MINUS_EXPR);
8209       break;
8210
8211     case CPP_MULT_EQ:
8212       id = ansi_assopname (MULT_EXPR);
8213       break;
8214
8215     case CPP_DIV_EQ:
8216       id = ansi_assopname (TRUNC_DIV_EXPR);
8217       break;
8218
8219     case CPP_MOD_EQ:
8220       id = ansi_assopname (TRUNC_MOD_EXPR);
8221       break;
8222
8223     case CPP_XOR_EQ:
8224       id = ansi_assopname (BIT_XOR_EXPR);
8225       break;
8226
8227     case CPP_AND_EQ:
8228       id = ansi_assopname (BIT_AND_EXPR);
8229       break;
8230
8231     case CPP_OR_EQ:
8232       id = ansi_assopname (BIT_IOR_EXPR);
8233       break;
8234
8235     case CPP_LSHIFT:
8236       id = ansi_opname (LSHIFT_EXPR);
8237       break;
8238
8239     case CPP_RSHIFT:
8240       id = ansi_opname (RSHIFT_EXPR);
8241       break;
8242
8243     case CPP_LSHIFT_EQ:
8244       id = ansi_assopname (LSHIFT_EXPR);
8245       break;
8246
8247     case CPP_RSHIFT_EQ:
8248       id = ansi_assopname (RSHIFT_EXPR);
8249       break;
8250
8251     case CPP_EQ_EQ:
8252       id = ansi_opname (EQ_EXPR);
8253       break;
8254
8255     case CPP_NOT_EQ:
8256       id = ansi_opname (NE_EXPR);
8257       break;
8258
8259     case CPP_LESS_EQ:
8260       id = ansi_opname (LE_EXPR);
8261       break;
8262
8263     case CPP_GREATER_EQ:
8264       id = ansi_opname (GE_EXPR);
8265       break;
8266
8267     case CPP_AND_AND:
8268       id = ansi_opname (TRUTH_ANDIF_EXPR);
8269       break;
8270
8271     case CPP_OR_OR:
8272       id = ansi_opname (TRUTH_ORIF_EXPR);
8273       break;
8274
8275     case CPP_PLUS_PLUS:
8276       id = ansi_opname (POSTINCREMENT_EXPR);
8277       break;
8278
8279     case CPP_MINUS_MINUS:
8280       id = ansi_opname (PREDECREMENT_EXPR);
8281       break;
8282
8283     case CPP_COMMA:
8284       id = ansi_opname (COMPOUND_EXPR);
8285       break;
8286
8287     case CPP_DEREF_STAR:
8288       id = ansi_opname (MEMBER_REF);
8289       break;
8290
8291     case CPP_DEREF:
8292       id = ansi_opname (COMPONENT_REF);
8293       break;
8294
8295     case CPP_OPEN_PAREN:
8296       /* Consume the `('.  */
8297       cp_lexer_consume_token (parser->lexer);
8298       /* Look for the matching `)'.  */
8299       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8300       return ansi_opname (CALL_EXPR);
8301
8302     case CPP_OPEN_SQUARE:
8303       /* Consume the `['.  */
8304       cp_lexer_consume_token (parser->lexer);
8305       /* Look for the matching `]'.  */
8306       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8307       return ansi_opname (ARRAY_REF);
8308
8309     default:
8310       /* Anything else is an error.  */
8311       break;
8312     }
8313
8314   /* If we have selected an identifier, we need to consume the
8315      operator token.  */
8316   if (id)
8317     cp_lexer_consume_token (parser->lexer);
8318   /* Otherwise, no valid operator name was present.  */
8319   else
8320     {
8321       cp_parser_error (parser, "expected operator");
8322       id = error_mark_node;
8323     }
8324
8325   return id;
8326 }
8327
8328 /* Parse a template-declaration.
8329
8330    template-declaration:
8331      export [opt] template < template-parameter-list > declaration
8332
8333    If MEMBER_P is TRUE, this template-declaration occurs within a
8334    class-specifier.
8335
8336    The grammar rule given by the standard isn't correct.  What
8337    is really meant is:
8338
8339    template-declaration:
8340      export [opt] template-parameter-list-seq
8341        decl-specifier-seq [opt] init-declarator [opt] ;
8342      export [opt] template-parameter-list-seq
8343        function-definition
8344
8345    template-parameter-list-seq:
8346      template-parameter-list-seq [opt]
8347      template < template-parameter-list >  */
8348
8349 static void
8350 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8351 {
8352   /* Check for `export'.  */
8353   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8354     {
8355       /* Consume the `export' token.  */
8356       cp_lexer_consume_token (parser->lexer);
8357       /* Warn that we do not support `export'.  */
8358       warning (0, "keyword %<export%> not implemented, and will be ignored");
8359     }
8360
8361   cp_parser_template_declaration_after_export (parser, member_p);
8362 }
8363
8364 /* Parse a template-parameter-list.
8365
8366    template-parameter-list:
8367      template-parameter
8368      template-parameter-list , template-parameter
8369
8370    Returns a TREE_LIST.  Each node represents a template parameter.
8371    The nodes are connected via their TREE_CHAINs.  */
8372
8373 static tree
8374 cp_parser_template_parameter_list (cp_parser* parser)
8375 {
8376   tree parameter_list = NULL_TREE;
8377
8378   begin_template_parm_list ();
8379   while (true)
8380     {
8381       tree parameter;
8382       cp_token *token;
8383       bool is_non_type;
8384
8385       /* Parse the template-parameter.  */
8386       parameter = cp_parser_template_parameter (parser, &is_non_type);
8387       /* Add it to the list.  */
8388       if (parameter != error_mark_node)
8389         parameter_list = process_template_parm (parameter_list,
8390                                                 parameter,
8391                                                 is_non_type);
8392       /* Peek at the next token.  */
8393       token = cp_lexer_peek_token (parser->lexer);
8394       /* If it's not a `,', we're done.  */
8395       if (token->type != CPP_COMMA)
8396         break;
8397       /* Otherwise, consume the `,' token.  */
8398       cp_lexer_consume_token (parser->lexer);
8399     }
8400
8401   return end_template_parm_list (parameter_list);
8402 }
8403
8404 /* Parse a template-parameter.
8405
8406    template-parameter:
8407      type-parameter
8408      parameter-declaration
8409
8410    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8411    the parameter.  The TREE_PURPOSE is the default value, if any.
8412    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8413    iff this parameter is a non-type parameter.  */
8414
8415 static tree
8416 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8417 {
8418   cp_token *token;
8419   cp_parameter_declarator *parameter_declarator;
8420   tree parm;
8421
8422   /* Assume it is a type parameter or a template parameter.  */
8423   *is_non_type = false;
8424   /* Peek at the next token.  */
8425   token = cp_lexer_peek_token (parser->lexer);
8426   /* If it is `class' or `template', we have a type-parameter.  */
8427   if (token->keyword == RID_TEMPLATE)
8428     return cp_parser_type_parameter (parser);
8429   /* If it is `class' or `typename' we do not know yet whether it is a
8430      type parameter or a non-type parameter.  Consider:
8431
8432        template <typename T, typename T::X X> ...
8433
8434      or:
8435
8436        template <class C, class D*> ...
8437
8438      Here, the first parameter is a type parameter, and the second is
8439      a non-type parameter.  We can tell by looking at the token after
8440      the identifier -- if it is a `,', `=', or `>' then we have a type
8441      parameter.  */
8442   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8443     {
8444       /* Peek at the token after `class' or `typename'.  */
8445       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8446       /* If it's an identifier, skip it.  */
8447       if (token->type == CPP_NAME)
8448         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8449       /* Now, see if the token looks like the end of a template
8450          parameter.  */
8451       if (token->type == CPP_COMMA
8452           || token->type == CPP_EQ
8453           || token->type == CPP_GREATER)
8454         return cp_parser_type_parameter (parser);
8455     }
8456
8457   /* Otherwise, it is a non-type parameter.
8458
8459      [temp.param]
8460
8461      When parsing a default template-argument for a non-type
8462      template-parameter, the first non-nested `>' is taken as the end
8463      of the template parameter-list rather than a greater-than
8464      operator.  */
8465   *is_non_type = true;
8466   parameter_declarator
8467      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8468                                         /*parenthesized_p=*/NULL);
8469   parm = grokdeclarator (parameter_declarator->declarator,
8470                          &parameter_declarator->decl_specifiers,
8471                          PARM, /*initialized=*/0,
8472                          /*attrlist=*/NULL);
8473   if (parm == error_mark_node)
8474     return error_mark_node;
8475   return build_tree_list (parameter_declarator->default_argument, parm);
8476 }
8477
8478 /* Parse a type-parameter.
8479
8480    type-parameter:
8481      class identifier [opt]
8482      class identifier [opt] = type-id
8483      typename identifier [opt]
8484      typename identifier [opt] = type-id
8485      template < template-parameter-list > class identifier [opt]
8486      template < template-parameter-list > class identifier [opt]
8487        = id-expression
8488
8489    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8490    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8491    the declaration of the parameter.  */
8492
8493 static tree
8494 cp_parser_type_parameter (cp_parser* parser)
8495 {
8496   cp_token *token;
8497   tree parameter;
8498
8499   /* Look for a keyword to tell us what kind of parameter this is.  */
8500   token = cp_parser_require (parser, CPP_KEYWORD,
8501                              "`class', `typename', or `template'");
8502   if (!token)
8503     return error_mark_node;
8504
8505   switch (token->keyword)
8506     {
8507     case RID_CLASS:
8508     case RID_TYPENAME:
8509       {
8510         tree identifier;
8511         tree default_argument;
8512
8513         /* If the next token is an identifier, then it names the
8514            parameter.  */
8515         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8516           identifier = cp_parser_identifier (parser);
8517         else
8518           identifier = NULL_TREE;
8519
8520         /* Create the parameter.  */
8521         parameter = finish_template_type_parm (class_type_node, identifier);
8522
8523         /* If the next token is an `=', we have a default argument.  */
8524         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8525           {
8526             /* Consume the `=' token.  */
8527             cp_lexer_consume_token (parser->lexer);
8528             /* Parse the default-argument.  */
8529             push_deferring_access_checks (dk_no_deferred);
8530             default_argument = cp_parser_type_id (parser);
8531             pop_deferring_access_checks ();
8532           }
8533         else
8534           default_argument = NULL_TREE;
8535
8536         /* Create the combined representation of the parameter and the
8537            default argument.  */
8538         parameter = build_tree_list (default_argument, parameter);
8539       }
8540       break;
8541
8542     case RID_TEMPLATE:
8543       {
8544         tree parameter_list;
8545         tree identifier;
8546         tree default_argument;
8547
8548         /* Look for the `<'.  */
8549         cp_parser_require (parser, CPP_LESS, "`<'");
8550         /* Parse the template-parameter-list.  */
8551         parameter_list = cp_parser_template_parameter_list (parser);
8552         /* Look for the `>'.  */
8553         cp_parser_require (parser, CPP_GREATER, "`>'");
8554         /* Look for the `class' keyword.  */
8555         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8556         /* If the next token is an `=', then there is a
8557            default-argument.  If the next token is a `>', we are at
8558            the end of the parameter-list.  If the next token is a `,',
8559            then we are at the end of this parameter.  */
8560         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8561             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8562             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8563           {
8564             identifier = cp_parser_identifier (parser);
8565             /* Treat invalid names as if the parameter were nameless.  */
8566             if (identifier == error_mark_node)
8567               identifier = NULL_TREE;
8568           }
8569         else
8570           identifier = NULL_TREE;
8571
8572         /* Create the template parameter.  */
8573         parameter = finish_template_template_parm (class_type_node,
8574                                                    identifier);
8575
8576         /* If the next token is an `=', then there is a
8577            default-argument.  */
8578         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8579           {
8580             bool is_template;
8581
8582             /* Consume the `='.  */
8583             cp_lexer_consume_token (parser->lexer);
8584             /* Parse the id-expression.  */
8585             push_deferring_access_checks (dk_no_deferred);
8586             default_argument
8587               = cp_parser_id_expression (parser,
8588                                          /*template_keyword_p=*/false,
8589                                          /*check_dependency_p=*/true,
8590                                          /*template_p=*/&is_template,
8591                                          /*declarator_p=*/false,
8592                                          /*optional_p=*/false);
8593             if (TREE_CODE (default_argument) == TYPE_DECL)
8594               /* If the id-expression was a template-id that refers to
8595                  a template-class, we already have the declaration here,
8596                  so no further lookup is needed.  */
8597                  ;
8598             else
8599               /* Look up the name.  */
8600               default_argument
8601                 = cp_parser_lookup_name (parser, default_argument,
8602                                          none_type,
8603                                          /*is_template=*/is_template,
8604                                          /*is_namespace=*/false,
8605                                          /*check_dependency=*/true,
8606                                          /*ambiguous_decls=*/NULL);
8607             /* See if the default argument is valid.  */
8608             default_argument
8609               = check_template_template_default_arg (default_argument);
8610             pop_deferring_access_checks ();
8611           }
8612         else
8613           default_argument = NULL_TREE;
8614
8615         /* Create the combined representation of the parameter and the
8616            default argument.  */
8617         parameter = build_tree_list (default_argument, parameter);
8618       }
8619       break;
8620
8621     default:
8622       gcc_unreachable ();
8623       break;
8624     }
8625
8626   return parameter;
8627 }
8628
8629 /* Parse a template-id.
8630
8631    template-id:
8632      template-name < template-argument-list [opt] >
8633
8634    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8635    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8636    returned.  Otherwise, if the template-name names a function, or set
8637    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8638    names a class, returns a TYPE_DECL for the specialization.
8639
8640    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8641    uninstantiated templates.  */
8642
8643 static tree
8644 cp_parser_template_id (cp_parser *parser,
8645                        bool template_keyword_p,
8646                        bool check_dependency_p,
8647                        bool is_declaration)
8648 {
8649   tree template;
8650   tree arguments;
8651   tree template_id;
8652   cp_token_position start_of_id = 0;
8653   tree access_check = NULL_TREE;
8654   cp_token *next_token, *next_token_2;
8655   bool is_identifier;
8656
8657   /* If the next token corresponds to a template-id, there is no need
8658      to reparse it.  */
8659   next_token = cp_lexer_peek_token (parser->lexer);
8660   if (next_token->type == CPP_TEMPLATE_ID)
8661     {
8662       tree value;
8663       tree check;
8664
8665       /* Get the stored value.  */
8666       value = cp_lexer_consume_token (parser->lexer)->value;
8667       /* Perform any access checks that were deferred.  */
8668       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8669         perform_or_defer_access_check (TREE_PURPOSE (check),
8670                                        TREE_VALUE (check));
8671       /* Return the stored value.  */
8672       return TREE_VALUE (value);
8673     }
8674
8675   /* Avoid performing name lookup if there is no possibility of
8676      finding a template-id.  */
8677   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8678       || (next_token->type == CPP_NAME
8679           && !cp_parser_nth_token_starts_template_argument_list_p
8680                (parser, 2)))
8681     {
8682       cp_parser_error (parser, "expected template-id");
8683       return error_mark_node;
8684     }
8685
8686   /* Remember where the template-id starts.  */
8687   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8688     start_of_id = cp_lexer_token_position (parser->lexer, false);
8689
8690   push_deferring_access_checks (dk_deferred);
8691
8692   /* Parse the template-name.  */
8693   is_identifier = false;
8694   template = cp_parser_template_name (parser, template_keyword_p,
8695                                       check_dependency_p,
8696                                       is_declaration,
8697                                       &is_identifier);
8698   if (template == error_mark_node || is_identifier)
8699     {
8700       pop_deferring_access_checks ();
8701       return template;
8702     }
8703
8704   /* If we find the sequence `[:' after a template-name, it's probably
8705      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8706      parse correctly the argument list.  */
8707   next_token = cp_lexer_peek_token (parser->lexer);
8708   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8709   if (next_token->type == CPP_OPEN_SQUARE
8710       && next_token->flags & DIGRAPH
8711       && next_token_2->type == CPP_COLON
8712       && !(next_token_2->flags & PREV_WHITE))
8713     {
8714       cp_parser_parse_tentatively (parser);
8715       /* Change `:' into `::'.  */
8716       next_token_2->type = CPP_SCOPE;
8717       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8718          CPP_LESS.  */
8719       cp_lexer_consume_token (parser->lexer);
8720       /* Parse the arguments.  */
8721       arguments = cp_parser_enclosed_template_argument_list (parser);
8722       if (!cp_parser_parse_definitely (parser))
8723         {
8724           /* If we couldn't parse an argument list, then we revert our changes
8725              and return simply an error. Maybe this is not a template-id
8726              after all.  */
8727           next_token_2->type = CPP_COLON;
8728           cp_parser_error (parser, "expected %<<%>");
8729           pop_deferring_access_checks ();
8730           return error_mark_node;
8731         }
8732       /* Otherwise, emit an error about the invalid digraph, but continue
8733          parsing because we got our argument list.  */
8734       pedwarn ("%<<::%> cannot begin a template-argument list");
8735       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8736               "between %<<%> and %<::%>");
8737       if (!flag_permissive)
8738         {
8739           static bool hint;
8740           if (!hint)
8741             {
8742               inform ("(if you use -fpermissive G++ will accept your code)");
8743               hint = true;
8744             }
8745         }
8746     }
8747   else
8748     {
8749       /* Look for the `<' that starts the template-argument-list.  */
8750       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8751         {
8752           pop_deferring_access_checks ();
8753           return error_mark_node;
8754         }
8755       /* Parse the arguments.  */
8756       arguments = cp_parser_enclosed_template_argument_list (parser);
8757     }
8758
8759   /* Build a representation of the specialization.  */
8760   if (TREE_CODE (template) == IDENTIFIER_NODE)
8761     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8762   else if (DECL_CLASS_TEMPLATE_P (template)
8763            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8764     {
8765       bool entering_scope;
8766       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8767          template (rather than some instantiation thereof) only if
8768          is not nested within some other construct.  For example, in
8769          "template <typename T> void f(T) { A<T>::", A<T> is just an
8770          instantiation of A.  */
8771       entering_scope = (template_parm_scope_p ()
8772                         && cp_lexer_next_token_is (parser->lexer,
8773                                                    CPP_SCOPE));
8774       template_id
8775         = finish_template_type (template, arguments, entering_scope);
8776     }
8777   else
8778     {
8779       /* If it's not a class-template or a template-template, it should be
8780          a function-template.  */
8781       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8782                    || TREE_CODE (template) == OVERLOAD
8783                    || BASELINK_P (template)));
8784
8785       template_id = lookup_template_function (template, arguments);
8786     }
8787
8788   /* Retrieve any deferred checks.  Do not pop this access checks yet
8789      so the memory will not be reclaimed during token replacing below.  */
8790   access_check = get_deferred_access_checks ();
8791
8792   /* If parsing tentatively, replace the sequence of tokens that makes
8793      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8794      should we re-parse the token stream, we will not have to repeat
8795      the effort required to do the parse, nor will we issue duplicate
8796      error messages about problems during instantiation of the
8797      template.  */
8798   if (start_of_id)
8799     {
8800       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8801
8802       /* Reset the contents of the START_OF_ID token.  */
8803       token->type = CPP_TEMPLATE_ID;
8804       token->value = build_tree_list (access_check, template_id);
8805       token->keyword = RID_MAX;
8806
8807       /* Purge all subsequent tokens.  */
8808       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8809
8810       /* ??? Can we actually assume that, if template_id ==
8811          error_mark_node, we will have issued a diagnostic to the
8812          user, as opposed to simply marking the tentative parse as
8813          failed?  */
8814       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8815         error ("parse error in template argument list");
8816     }
8817
8818   pop_deferring_access_checks ();
8819   return template_id;
8820 }
8821
8822 /* Parse a template-name.
8823
8824    template-name:
8825      identifier
8826
8827    The standard should actually say:
8828
8829    template-name:
8830      identifier
8831      operator-function-id
8832
8833    A defect report has been filed about this issue.
8834
8835    A conversion-function-id cannot be a template name because they cannot
8836    be part of a template-id. In fact, looking at this code:
8837
8838    a.operator K<int>()
8839
8840    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8841    It is impossible to call a templated conversion-function-id with an
8842    explicit argument list, since the only allowed template parameter is
8843    the type to which it is converting.
8844
8845    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8846    `template' keyword, in a construction like:
8847
8848      T::template f<3>()
8849
8850    In that case `f' is taken to be a template-name, even though there
8851    is no way of knowing for sure.
8852
8853    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8854    name refers to a set of overloaded functions, at least one of which
8855    is a template, or an IDENTIFIER_NODE with the name of the template,
8856    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8857    names are looked up inside uninstantiated templates.  */
8858
8859 static tree
8860 cp_parser_template_name (cp_parser* parser,
8861                          bool template_keyword_p,
8862                          bool check_dependency_p,
8863                          bool is_declaration,
8864                          bool *is_identifier)
8865 {
8866   tree identifier;
8867   tree decl;
8868   tree fns;
8869
8870   /* If the next token is `operator', then we have either an
8871      operator-function-id or a conversion-function-id.  */
8872   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8873     {
8874       /* We don't know whether we're looking at an
8875          operator-function-id or a conversion-function-id.  */
8876       cp_parser_parse_tentatively (parser);
8877       /* Try an operator-function-id.  */
8878       identifier = cp_parser_operator_function_id (parser);
8879       /* If that didn't work, try a conversion-function-id.  */
8880       if (!cp_parser_parse_definitely (parser))
8881         {
8882           cp_parser_error (parser, "expected template-name");
8883           return error_mark_node;
8884         }
8885     }
8886   /* Look for the identifier.  */
8887   else
8888     identifier = cp_parser_identifier (parser);
8889
8890   /* If we didn't find an identifier, we don't have a template-id.  */
8891   if (identifier == error_mark_node)
8892     return error_mark_node;
8893
8894   /* If the name immediately followed the `template' keyword, then it
8895      is a template-name.  However, if the next token is not `<', then
8896      we do not treat it as a template-name, since it is not being used
8897      as part of a template-id.  This enables us to handle constructs
8898      like:
8899
8900        template <typename T> struct S { S(); };
8901        template <typename T> S<T>::S();
8902
8903      correctly.  We would treat `S' as a template -- if it were `S<T>'
8904      -- but we do not if there is no `<'.  */
8905
8906   if (processing_template_decl
8907       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8908     {
8909       /* In a declaration, in a dependent context, we pretend that the
8910          "template" keyword was present in order to improve error
8911          recovery.  For example, given:
8912
8913            template <typename T> void f(T::X<int>);
8914
8915          we want to treat "X<int>" as a template-id.  */
8916       if (is_declaration
8917           && !template_keyword_p
8918           && parser->scope && TYPE_P (parser->scope)
8919           && check_dependency_p
8920           && dependent_type_p (parser->scope)
8921           /* Do not do this for dtors (or ctors), since they never
8922              need the template keyword before their name.  */
8923           && !constructor_name_p (identifier, parser->scope))
8924         {
8925           cp_token_position start = 0;
8926
8927           /* Explain what went wrong.  */
8928           error ("non-template %qD used as template", identifier);
8929           inform ("use %<%T::template %D%> to indicate that it is a template",
8930                   parser->scope, identifier);
8931           /* If parsing tentatively, find the location of the "<" token.  */
8932           if (cp_parser_simulate_error (parser))
8933             start = cp_lexer_token_position (parser->lexer, true);
8934           /* Parse the template arguments so that we can issue error
8935              messages about them.  */
8936           cp_lexer_consume_token (parser->lexer);
8937           cp_parser_enclosed_template_argument_list (parser);
8938           /* Skip tokens until we find a good place from which to
8939              continue parsing.  */
8940           cp_parser_skip_to_closing_parenthesis (parser,
8941                                                  /*recovering=*/true,
8942                                                  /*or_comma=*/true,
8943                                                  /*consume_paren=*/false);
8944           /* If parsing tentatively, permanently remove the
8945              template argument list.  That will prevent duplicate
8946              error messages from being issued about the missing
8947              "template" keyword.  */
8948           if (start)
8949             cp_lexer_purge_tokens_after (parser->lexer, start);
8950           if (is_identifier)
8951             *is_identifier = true;
8952           return identifier;
8953         }
8954
8955       /* If the "template" keyword is present, then there is generally
8956          no point in doing name-lookup, so we just return IDENTIFIER.
8957          But, if the qualifying scope is non-dependent then we can
8958          (and must) do name-lookup normally.  */
8959       if (template_keyword_p
8960           && (!parser->scope
8961               || (TYPE_P (parser->scope)
8962                   && dependent_type_p (parser->scope))))
8963         return identifier;
8964     }
8965
8966   /* Look up the name.  */
8967   decl = cp_parser_lookup_name (parser, identifier,
8968                                 none_type,
8969                                 /*is_template=*/false,
8970                                 /*is_namespace=*/false,
8971                                 check_dependency_p,
8972                                 /*ambiguous_decls=*/NULL);
8973   decl = maybe_get_template_decl_from_type_decl (decl);
8974
8975   /* If DECL is a template, then the name was a template-name.  */
8976   if (TREE_CODE (decl) == TEMPLATE_DECL)
8977     ;
8978   else
8979     {
8980       tree fn = NULL_TREE;
8981
8982       /* The standard does not explicitly indicate whether a name that
8983          names a set of overloaded declarations, some of which are
8984          templates, is a template-name.  However, such a name should
8985          be a template-name; otherwise, there is no way to form a
8986          template-id for the overloaded templates.  */
8987       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8988       if (TREE_CODE (fns) == OVERLOAD)
8989         for (fn = fns; fn; fn = OVL_NEXT (fn))
8990           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8991             break;
8992
8993       if (!fn)
8994         {
8995           /* The name does not name a template.  */
8996           cp_parser_error (parser, "expected template-name");
8997           return error_mark_node;
8998         }
8999     }
9000
9001   /* If DECL is dependent, and refers to a function, then just return
9002      its name; we will look it up again during template instantiation.  */
9003   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9004     {
9005       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9006       if (TYPE_P (scope) && dependent_type_p (scope))
9007         return identifier;
9008     }
9009
9010   return decl;
9011 }
9012
9013 /* Parse a template-argument-list.
9014
9015    template-argument-list:
9016      template-argument
9017      template-argument-list , template-argument
9018
9019    Returns a TREE_VEC containing the arguments.  */
9020
9021 static tree
9022 cp_parser_template_argument_list (cp_parser* parser)
9023 {
9024   tree fixed_args[10];
9025   unsigned n_args = 0;
9026   unsigned alloced = 10;
9027   tree *arg_ary = fixed_args;
9028   tree vec;
9029   bool saved_in_template_argument_list_p;
9030   bool saved_ice_p;
9031   bool saved_non_ice_p;
9032
9033   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9034   parser->in_template_argument_list_p = true;
9035   /* Even if the template-id appears in an integral
9036      constant-expression, the contents of the argument list do
9037      not.  */
9038   saved_ice_p = parser->integral_constant_expression_p;
9039   parser->integral_constant_expression_p = false;
9040   saved_non_ice_p = parser->non_integral_constant_expression_p;
9041   parser->non_integral_constant_expression_p = false;
9042   /* Parse the arguments.  */
9043   do
9044     {
9045       tree argument;
9046
9047       if (n_args)
9048         /* Consume the comma.  */
9049         cp_lexer_consume_token (parser->lexer);
9050
9051       /* Parse the template-argument.  */
9052       argument = cp_parser_template_argument (parser);
9053       if (n_args == alloced)
9054         {
9055           alloced *= 2;
9056
9057           if (arg_ary == fixed_args)
9058             {
9059               arg_ary = XNEWVEC (tree, alloced);
9060               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9061             }
9062           else
9063             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9064         }
9065       arg_ary[n_args++] = argument;
9066     }
9067   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9068
9069   vec = make_tree_vec (n_args);
9070
9071   while (n_args--)
9072     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9073
9074   if (arg_ary != fixed_args)
9075     free (arg_ary);
9076   parser->non_integral_constant_expression_p = saved_non_ice_p;
9077   parser->integral_constant_expression_p = saved_ice_p;
9078   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9079   return vec;
9080 }
9081
9082 /* Parse a template-argument.
9083
9084    template-argument:
9085      assignment-expression
9086      type-id
9087      id-expression
9088
9089    The representation is that of an assignment-expression, type-id, or
9090    id-expression -- except that the qualified id-expression is
9091    evaluated, so that the value returned is either a DECL or an
9092    OVERLOAD.
9093
9094    Although the standard says "assignment-expression", it forbids
9095    throw-expressions or assignments in the template argument.
9096    Therefore, we use "conditional-expression" instead.  */
9097
9098 static tree
9099 cp_parser_template_argument (cp_parser* parser)
9100 {
9101   tree argument;
9102   bool template_p;
9103   bool address_p;
9104   bool maybe_type_id = false;
9105   cp_token *token;
9106   cp_id_kind idk;
9107
9108   /* There's really no way to know what we're looking at, so we just
9109      try each alternative in order.
9110
9111        [temp.arg]
9112
9113        In a template-argument, an ambiguity between a type-id and an
9114        expression is resolved to a type-id, regardless of the form of
9115        the corresponding template-parameter.
9116
9117      Therefore, we try a type-id first.  */
9118   cp_parser_parse_tentatively (parser);
9119   argument = cp_parser_type_id (parser);
9120   /* If there was no error parsing the type-id but the next token is a '>>',
9121      we probably found a typo for '> >'. But there are type-id which are
9122      also valid expressions. For instance:
9123
9124      struct X { int operator >> (int); };
9125      template <int V> struct Foo {};
9126      Foo<X () >> 5> r;
9127
9128      Here 'X()' is a valid type-id of a function type, but the user just
9129      wanted to write the expression "X() >> 5". Thus, we remember that we
9130      found a valid type-id, but we still try to parse the argument as an
9131      expression to see what happens.  */
9132   if (!cp_parser_error_occurred (parser)
9133       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9134     {
9135       maybe_type_id = true;
9136       cp_parser_abort_tentative_parse (parser);
9137     }
9138   else
9139     {
9140       /* If the next token isn't a `,' or a `>', then this argument wasn't
9141       really finished. This means that the argument is not a valid
9142       type-id.  */
9143       if (!cp_parser_next_token_ends_template_argument_p (parser))
9144         cp_parser_error (parser, "expected template-argument");
9145       /* If that worked, we're done.  */
9146       if (cp_parser_parse_definitely (parser))
9147         return argument;
9148     }
9149   /* We're still not sure what the argument will be.  */
9150   cp_parser_parse_tentatively (parser);
9151   /* Try a template.  */
9152   argument = cp_parser_id_expression (parser,
9153                                       /*template_keyword_p=*/false,
9154                                       /*check_dependency_p=*/true,
9155                                       &template_p,
9156                                       /*declarator_p=*/false,
9157                                       /*optional_p=*/false);
9158   /* If the next token isn't a `,' or a `>', then this argument wasn't
9159      really finished.  */
9160   if (!cp_parser_next_token_ends_template_argument_p (parser))
9161     cp_parser_error (parser, "expected template-argument");
9162   if (!cp_parser_error_occurred (parser))
9163     {
9164       /* Figure out what is being referred to.  If the id-expression
9165          was for a class template specialization, then we will have a
9166          TYPE_DECL at this point.  There is no need to do name lookup
9167          at this point in that case.  */
9168       if (TREE_CODE (argument) != TYPE_DECL)
9169         argument = cp_parser_lookup_name (parser, argument,
9170                                           none_type,
9171                                           /*is_template=*/template_p,
9172                                           /*is_namespace=*/false,
9173                                           /*check_dependency=*/true,
9174                                           /*ambiguous_decls=*/NULL);
9175       if (TREE_CODE (argument) != TEMPLATE_DECL
9176           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9177         cp_parser_error (parser, "expected template-name");
9178     }
9179   if (cp_parser_parse_definitely (parser))
9180     return argument;
9181   /* It must be a non-type argument.  There permitted cases are given
9182      in [temp.arg.nontype]:
9183
9184      -- an integral constant-expression of integral or enumeration
9185         type; or
9186
9187      -- the name of a non-type template-parameter; or
9188
9189      -- the name of an object or function with external linkage...
9190
9191      -- the address of an object or function with external linkage...
9192
9193      -- a pointer to member...  */
9194   /* Look for a non-type template parameter.  */
9195   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9196     {
9197       cp_parser_parse_tentatively (parser);
9198       argument = cp_parser_primary_expression (parser,
9199                                                /*adress_p=*/false,
9200                                                /*cast_p=*/false,
9201                                                /*template_arg_p=*/true,
9202                                                &idk);
9203       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9204           || !cp_parser_next_token_ends_template_argument_p (parser))
9205         cp_parser_simulate_error (parser);
9206       if (cp_parser_parse_definitely (parser))
9207         return argument;
9208     }
9209
9210   /* If the next token is "&", the argument must be the address of an
9211      object or function with external linkage.  */
9212   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9213   if (address_p)
9214     cp_lexer_consume_token (parser->lexer);
9215   /* See if we might have an id-expression.  */
9216   token = cp_lexer_peek_token (parser->lexer);
9217   if (token->type == CPP_NAME
9218       || token->keyword == RID_OPERATOR
9219       || token->type == CPP_SCOPE
9220       || token->type == CPP_TEMPLATE_ID
9221       || token->type == CPP_NESTED_NAME_SPECIFIER)
9222     {
9223       cp_parser_parse_tentatively (parser);
9224       argument = cp_parser_primary_expression (parser,
9225                                                address_p,
9226                                                /*cast_p=*/false,
9227                                                /*template_arg_p=*/true,
9228                                                &idk);
9229       if (cp_parser_error_occurred (parser)
9230           || !cp_parser_next_token_ends_template_argument_p (parser))
9231         cp_parser_abort_tentative_parse (parser);
9232       else
9233         {
9234           if (TREE_CODE (argument) == INDIRECT_REF)
9235             {
9236               gcc_assert (REFERENCE_REF_P (argument));
9237               argument = TREE_OPERAND (argument, 0);
9238             }
9239
9240           if (TREE_CODE (argument) == BASELINK)
9241             /* We don't need the information about what class was used
9242                to name the overloaded functions.  */
9243             argument = BASELINK_FUNCTIONS (argument);
9244
9245           if (TREE_CODE (argument) == VAR_DECL)
9246             {
9247               /* A variable without external linkage might still be a
9248                  valid constant-expression, so no error is issued here
9249                  if the external-linkage check fails.  */
9250               if (!DECL_EXTERNAL_LINKAGE_P (argument))
9251                 cp_parser_simulate_error (parser);
9252             }
9253           else if (is_overloaded_fn (argument))
9254             /* All overloaded functions are allowed; if the external
9255                linkage test does not pass, an error will be issued
9256                later.  */
9257             ;
9258           else if (address_p
9259                    && (TREE_CODE (argument) == OFFSET_REF
9260                        || TREE_CODE (argument) == SCOPE_REF))
9261             /* A pointer-to-member.  */
9262             ;
9263           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9264             ;
9265           else
9266             cp_parser_simulate_error (parser);
9267
9268           if (cp_parser_parse_definitely (parser))
9269             {
9270               if (address_p)
9271                 argument = build_x_unary_op (ADDR_EXPR, argument);
9272               return argument;
9273             }
9274         }
9275     }
9276   /* If the argument started with "&", there are no other valid
9277      alternatives at this point.  */
9278   if (address_p)
9279     {
9280       cp_parser_error (parser, "invalid non-type template argument");
9281       return error_mark_node;
9282     }
9283
9284   /* If the argument wasn't successfully parsed as a type-id followed
9285      by '>>', the argument can only be a constant expression now.
9286      Otherwise, we try parsing the constant-expression tentatively,
9287      because the argument could really be a type-id.  */
9288   if (maybe_type_id)
9289     cp_parser_parse_tentatively (parser);
9290   argument = cp_parser_constant_expression (parser,
9291                                             /*allow_non_constant_p=*/false,
9292                                             /*non_constant_p=*/NULL);
9293   argument = fold_non_dependent_expr (argument);
9294   if (!maybe_type_id)
9295     return argument;
9296   if (!cp_parser_next_token_ends_template_argument_p (parser))
9297     cp_parser_error (parser, "expected template-argument");
9298   if (cp_parser_parse_definitely (parser))
9299     return argument;
9300   /* We did our best to parse the argument as a non type-id, but that
9301      was the only alternative that matched (albeit with a '>' after
9302      it). We can assume it's just a typo from the user, and a
9303      diagnostic will then be issued.  */
9304   return cp_parser_type_id (parser);
9305 }
9306
9307 /* Parse an explicit-instantiation.
9308
9309    explicit-instantiation:
9310      template declaration
9311
9312    Although the standard says `declaration', what it really means is:
9313
9314    explicit-instantiation:
9315      template decl-specifier-seq [opt] declarator [opt] ;
9316
9317    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9318    supposed to be allowed.  A defect report has been filed about this
9319    issue.
9320
9321    GNU Extension:
9322
9323    explicit-instantiation:
9324      storage-class-specifier template
9325        decl-specifier-seq [opt] declarator [opt] ;
9326      function-specifier template
9327        decl-specifier-seq [opt] declarator [opt] ;  */
9328
9329 static void
9330 cp_parser_explicit_instantiation (cp_parser* parser)
9331 {
9332   int declares_class_or_enum;
9333   cp_decl_specifier_seq decl_specifiers;
9334   tree extension_specifier = NULL_TREE;
9335
9336   /* Look for an (optional) storage-class-specifier or
9337      function-specifier.  */
9338   if (cp_parser_allow_gnu_extensions_p (parser))
9339     {
9340       extension_specifier
9341         = cp_parser_storage_class_specifier_opt (parser);
9342       if (!extension_specifier)
9343         extension_specifier
9344           = cp_parser_function_specifier_opt (parser,
9345                                               /*decl_specs=*/NULL);
9346     }
9347
9348   /* Look for the `template' keyword.  */
9349   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9350   /* Let the front end know that we are processing an explicit
9351      instantiation.  */
9352   begin_explicit_instantiation ();
9353   /* [temp.explicit] says that we are supposed to ignore access
9354      control while processing explicit instantiation directives.  */
9355   push_deferring_access_checks (dk_no_check);
9356   /* Parse a decl-specifier-seq.  */
9357   cp_parser_decl_specifier_seq (parser,
9358                                 CP_PARSER_FLAGS_OPTIONAL,
9359                                 &decl_specifiers,
9360                                 &declares_class_or_enum);
9361   /* If there was exactly one decl-specifier, and it declared a class,
9362      and there's no declarator, then we have an explicit type
9363      instantiation.  */
9364   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9365     {
9366       tree type;
9367
9368       type = check_tag_decl (&decl_specifiers);
9369       /* Turn access control back on for names used during
9370          template instantiation.  */
9371       pop_deferring_access_checks ();
9372       if (type)
9373         do_type_instantiation (type, extension_specifier,
9374                                /*complain=*/tf_error);
9375     }
9376   else
9377     {
9378       cp_declarator *declarator;
9379       tree decl;
9380
9381       /* Parse the declarator.  */
9382       declarator
9383         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9384                                 /*ctor_dtor_or_conv_p=*/NULL,
9385                                 /*parenthesized_p=*/NULL,
9386                                 /*member_p=*/false);
9387       if (declares_class_or_enum & 2)
9388         cp_parser_check_for_definition_in_return_type (declarator,
9389                                                        decl_specifiers.type);
9390       if (declarator != cp_error_declarator)
9391         {
9392           decl = grokdeclarator (declarator, &decl_specifiers,
9393                                  NORMAL, 0, &decl_specifiers.attributes);
9394           /* Turn access control back on for names used during
9395              template instantiation.  */
9396           pop_deferring_access_checks ();
9397           /* Do the explicit instantiation.  */
9398           do_decl_instantiation (decl, extension_specifier);
9399         }
9400       else
9401         {
9402           pop_deferring_access_checks ();
9403           /* Skip the body of the explicit instantiation.  */
9404           cp_parser_skip_to_end_of_statement (parser);
9405         }
9406     }
9407   /* We're done with the instantiation.  */
9408   end_explicit_instantiation ();
9409
9410   cp_parser_consume_semicolon_at_end_of_statement (parser);
9411 }
9412
9413 /* Parse an explicit-specialization.
9414
9415    explicit-specialization:
9416      template < > declaration
9417
9418    Although the standard says `declaration', what it really means is:
9419
9420    explicit-specialization:
9421      template <> decl-specifier [opt] init-declarator [opt] ;
9422      template <> function-definition
9423      template <> explicit-specialization
9424      template <> template-declaration  */
9425
9426 static void
9427 cp_parser_explicit_specialization (cp_parser* parser)
9428 {
9429   bool need_lang_pop;
9430   /* Look for the `template' keyword.  */
9431   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9432   /* Look for the `<'.  */
9433   cp_parser_require (parser, CPP_LESS, "`<'");
9434   /* Look for the `>'.  */
9435   cp_parser_require (parser, CPP_GREATER, "`>'");
9436   /* We have processed another parameter list.  */
9437   ++parser->num_template_parameter_lists;
9438   /* [temp]
9439
9440      A template ... explicit specialization ... shall not have C
9441      linkage.  */
9442   if (current_lang_name == lang_name_c)
9443     {
9444       error ("template specialization with C linkage");
9445       /* Give it C++ linkage to avoid confusing other parts of the
9446          front end.  */
9447       push_lang_context (lang_name_cplusplus);
9448       need_lang_pop = true;
9449     }
9450   else
9451     need_lang_pop = false;
9452   /* Let the front end know that we are beginning a specialization.  */
9453   begin_specialization ();
9454   /* If the next keyword is `template', we need to figure out whether
9455      or not we're looking a template-declaration.  */
9456   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9457     {
9458       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9459           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9460         cp_parser_template_declaration_after_export (parser,
9461                                                      /*member_p=*/false);
9462       else
9463         cp_parser_explicit_specialization (parser);
9464     }
9465   else
9466     /* Parse the dependent declaration.  */
9467     cp_parser_single_declaration (parser,
9468                                   /*checks=*/NULL_TREE,
9469                                   /*member_p=*/false,
9470                                   /*friend_p=*/NULL);
9471   /* We're done with the specialization.  */
9472   end_specialization ();
9473   /* For the erroneous case of a template with C linkage, we pushed an
9474      implicit C++ linkage scope; exit that scope now.  */
9475   if (need_lang_pop)
9476     pop_lang_context ();
9477   /* We're done with this parameter list.  */
9478   --parser->num_template_parameter_lists;
9479 }
9480
9481 /* Parse a type-specifier.
9482
9483    type-specifier:
9484      simple-type-specifier
9485      class-specifier
9486      enum-specifier
9487      elaborated-type-specifier
9488      cv-qualifier
9489
9490    GNU Extension:
9491
9492    type-specifier:
9493      __complex__
9494
9495    Returns a representation of the type-specifier.  For a
9496    class-specifier, enum-specifier, or elaborated-type-specifier, a
9497    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9498
9499    The parser flags FLAGS is used to control type-specifier parsing.
9500
9501    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9502    in a decl-specifier-seq.
9503
9504    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9505    class-specifier, enum-specifier, or elaborated-type-specifier, then
9506    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9507    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9508    zero.
9509
9510    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9511    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9512    is set to FALSE.  */
9513
9514 static tree
9515 cp_parser_type_specifier (cp_parser* parser,
9516                           cp_parser_flags flags,
9517                           cp_decl_specifier_seq *decl_specs,
9518                           bool is_declaration,
9519                           int* declares_class_or_enum,
9520                           bool* is_cv_qualifier)
9521 {
9522   tree type_spec = NULL_TREE;
9523   cp_token *token;
9524   enum rid keyword;
9525   cp_decl_spec ds = ds_last;
9526
9527   /* Assume this type-specifier does not declare a new type.  */
9528   if (declares_class_or_enum)
9529     *declares_class_or_enum = 0;
9530   /* And that it does not specify a cv-qualifier.  */
9531   if (is_cv_qualifier)
9532     *is_cv_qualifier = false;
9533   /* Peek at the next token.  */
9534   token = cp_lexer_peek_token (parser->lexer);
9535
9536   /* If we're looking at a keyword, we can use that to guide the
9537      production we choose.  */
9538   keyword = token->keyword;
9539   switch (keyword)
9540     {
9541     case RID_ENUM:
9542       /* Look for the enum-specifier.  */
9543       type_spec = cp_parser_enum_specifier (parser);
9544       /* If that worked, we're done.  */
9545       if (type_spec)
9546         {
9547           if (declares_class_or_enum)
9548             *declares_class_or_enum = 2;
9549           if (decl_specs)
9550             cp_parser_set_decl_spec_type (decl_specs,
9551                                           type_spec,
9552                                           /*user_defined_p=*/true);
9553           return type_spec;
9554         }
9555       else
9556         goto elaborated_type_specifier;
9557
9558       /* Any of these indicate either a class-specifier, or an
9559          elaborated-type-specifier.  */
9560     case RID_CLASS:
9561     case RID_STRUCT:
9562     case RID_UNION:
9563       /* Parse tentatively so that we can back up if we don't find a
9564          class-specifier.  */
9565       cp_parser_parse_tentatively (parser);
9566       /* Look for the class-specifier.  */
9567       type_spec = cp_parser_class_specifier (parser);
9568       /* If that worked, we're done.  */
9569       if (cp_parser_parse_definitely (parser))
9570         {
9571           if (declares_class_or_enum)
9572             *declares_class_or_enum = 2;
9573           if (decl_specs)
9574             cp_parser_set_decl_spec_type (decl_specs,
9575                                           type_spec,
9576                                           /*user_defined_p=*/true);
9577           return type_spec;
9578         }
9579
9580       /* Fall through.  */
9581     elaborated_type_specifier:
9582       /* We're declaring (not defining) a class or enum.  */
9583       if (declares_class_or_enum)
9584         *declares_class_or_enum = 1;
9585
9586       /* Fall through.  */
9587     case RID_TYPENAME:
9588       /* Look for an elaborated-type-specifier.  */
9589       type_spec
9590         = (cp_parser_elaborated_type_specifier
9591            (parser,
9592             decl_specs && decl_specs->specs[(int) ds_friend],
9593             is_declaration));
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     case RID_CONST:
9601       ds = ds_const;
9602       if (is_cv_qualifier)
9603         *is_cv_qualifier = true;
9604       break;
9605
9606     case RID_VOLATILE:
9607       ds = ds_volatile;
9608       if (is_cv_qualifier)
9609         *is_cv_qualifier = true;
9610       break;
9611
9612     case RID_RESTRICT:
9613       ds = ds_restrict;
9614       if (is_cv_qualifier)
9615         *is_cv_qualifier = true;
9616       break;
9617
9618     case RID_COMPLEX:
9619       /* The `__complex__' keyword is a GNU extension.  */
9620       ds = ds_complex;
9621       break;
9622
9623     default:
9624       break;
9625     }
9626
9627   /* Handle simple keywords.  */
9628   if (ds != ds_last)
9629     {
9630       if (decl_specs)
9631         {
9632           ++decl_specs->specs[(int)ds];
9633           decl_specs->any_specifiers_p = true;
9634         }
9635       return cp_lexer_consume_token (parser->lexer)->value;
9636     }
9637
9638   /* If we do not already have a type-specifier, assume we are looking
9639      at a simple-type-specifier.  */
9640   type_spec = cp_parser_simple_type_specifier (parser,
9641                                                decl_specs,
9642                                                flags);
9643
9644   /* If we didn't find a type-specifier, and a type-specifier was not
9645      optional in this context, issue an error message.  */
9646   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9647     {
9648       cp_parser_error (parser, "expected type specifier");
9649       return error_mark_node;
9650     }
9651
9652   return type_spec;
9653 }
9654
9655 /* Parse a simple-type-specifier.
9656
9657    simple-type-specifier:
9658      :: [opt] nested-name-specifier [opt] type-name
9659      :: [opt] nested-name-specifier template template-id
9660      char
9661      wchar_t
9662      bool
9663      short
9664      int
9665      long
9666      signed
9667      unsigned
9668      float
9669      double
9670      void
9671
9672    GNU Extension:
9673
9674    simple-type-specifier:
9675      __typeof__ unary-expression
9676      __typeof__ ( type-id )
9677
9678    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9679    appropriately updated.  */
9680
9681 static tree
9682 cp_parser_simple_type_specifier (cp_parser* parser,
9683                                  cp_decl_specifier_seq *decl_specs,
9684                                  cp_parser_flags flags)
9685 {
9686   tree type = NULL_TREE;
9687   cp_token *token;
9688
9689   /* Peek at the next token.  */
9690   token = cp_lexer_peek_token (parser->lexer);
9691
9692   /* If we're looking at a keyword, things are easy.  */
9693   switch (token->keyword)
9694     {
9695     case RID_CHAR:
9696       if (decl_specs)
9697         decl_specs->explicit_char_p = true;
9698       type = char_type_node;
9699       break;
9700     case RID_WCHAR:
9701       type = wchar_type_node;
9702       break;
9703     case RID_BOOL:
9704       type = boolean_type_node;
9705       break;
9706     case RID_SHORT:
9707       if (decl_specs)
9708         ++decl_specs->specs[(int) ds_short];
9709       type = short_integer_type_node;
9710       break;
9711     case RID_INT:
9712       if (decl_specs)
9713         decl_specs->explicit_int_p = true;
9714       type = integer_type_node;
9715       break;
9716     case RID_LONG:
9717       if (decl_specs)
9718         ++decl_specs->specs[(int) ds_long];
9719       type = long_integer_type_node;
9720       break;
9721     case RID_SIGNED:
9722       if (decl_specs)
9723         ++decl_specs->specs[(int) ds_signed];
9724       type = integer_type_node;
9725       break;
9726     case RID_UNSIGNED:
9727       if (decl_specs)
9728         ++decl_specs->specs[(int) ds_unsigned];
9729       type = unsigned_type_node;
9730       break;
9731     case RID_FLOAT:
9732       type = float_type_node;
9733       break;
9734     case RID_DOUBLE:
9735       type = double_type_node;
9736       break;
9737     case RID_VOID:
9738       type = void_type_node;
9739       break;
9740
9741     case RID_TYPEOF:
9742       /* Consume the `typeof' token.  */
9743       cp_lexer_consume_token (parser->lexer);
9744       /* Parse the operand to `typeof'.  */
9745       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9746       /* If it is not already a TYPE, take its type.  */
9747       if (!TYPE_P (type))
9748         type = finish_typeof (type);
9749
9750       if (decl_specs)
9751         cp_parser_set_decl_spec_type (decl_specs, type,
9752                                       /*user_defined_p=*/true);
9753
9754       return type;
9755
9756     default:
9757       break;
9758     }
9759
9760   /* If the type-specifier was for a built-in type, we're done.  */
9761   if (type)
9762     {
9763       tree id;
9764
9765       /* Record the type.  */
9766       if (decl_specs
9767           && (token->keyword != RID_SIGNED
9768               && token->keyword != RID_UNSIGNED
9769               && token->keyword != RID_SHORT
9770               && token->keyword != RID_LONG))
9771         cp_parser_set_decl_spec_type (decl_specs,
9772                                       type,
9773                                       /*user_defined=*/false);
9774       if (decl_specs)
9775         decl_specs->any_specifiers_p = true;
9776
9777       /* Consume the token.  */
9778       id = cp_lexer_consume_token (parser->lexer)->value;
9779
9780       /* There is no valid C++ program where a non-template type is
9781          followed by a "<".  That usually indicates that the user thought
9782          that the type was a template.  */
9783       cp_parser_check_for_invalid_template_id (parser, type);
9784
9785       return TYPE_NAME (type);
9786     }
9787
9788   /* The type-specifier must be a user-defined type.  */
9789   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9790     {
9791       bool qualified_p;
9792       bool global_p;
9793
9794       /* Don't gobble tokens or issue error messages if this is an
9795          optional type-specifier.  */
9796       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9797         cp_parser_parse_tentatively (parser);
9798
9799       /* Look for the optional `::' operator.  */
9800       global_p
9801         = (cp_parser_global_scope_opt (parser,
9802                                        /*current_scope_valid_p=*/false)
9803            != NULL_TREE);
9804       /* Look for the nested-name specifier.  */
9805       qualified_p
9806         = (cp_parser_nested_name_specifier_opt (parser,
9807                                                 /*typename_keyword_p=*/false,
9808                                                 /*check_dependency_p=*/true,
9809                                                 /*type_p=*/false,
9810                                                 /*is_declaration=*/false)
9811            != NULL_TREE);
9812       /* If we have seen a nested-name-specifier, and the next token
9813          is `template', then we are using the template-id production.  */
9814       if (parser->scope
9815           && cp_parser_optional_template_keyword (parser))
9816         {
9817           /* Look for the template-id.  */
9818           type = cp_parser_template_id (parser,
9819                                         /*template_keyword_p=*/true,
9820                                         /*check_dependency_p=*/true,
9821                                         /*is_declaration=*/false);
9822           /* If the template-id did not name a type, we are out of
9823              luck.  */
9824           if (TREE_CODE (type) != TYPE_DECL)
9825             {
9826               cp_parser_error (parser, "expected template-id for type");
9827               type = NULL_TREE;
9828             }
9829         }
9830       /* Otherwise, look for a type-name.  */
9831       else
9832         type = cp_parser_type_name (parser);
9833       /* Keep track of all name-lookups performed in class scopes.  */
9834       if (type
9835           && !global_p
9836           && !qualified_p
9837           && TREE_CODE (type) == TYPE_DECL
9838           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9839         maybe_note_name_used_in_class (DECL_NAME (type), type);
9840       /* If it didn't work out, we don't have a TYPE.  */
9841       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9842           && !cp_parser_parse_definitely (parser))
9843         type = NULL_TREE;
9844       if (type && decl_specs)
9845         cp_parser_set_decl_spec_type (decl_specs, type,
9846                                       /*user_defined=*/true);
9847     }
9848
9849   /* If we didn't get a type-name, issue an error message.  */
9850   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9851     {
9852       cp_parser_error (parser, "expected type-name");
9853       return error_mark_node;
9854     }
9855
9856   /* There is no valid C++ program where a non-template type is
9857      followed by a "<".  That usually indicates that the user thought
9858      that the type was a template.  */
9859   if (type && type != error_mark_node)
9860     {
9861       /* As a last-ditch effort, see if TYPE is an Objective-C type.
9862          If it is, then the '<'...'>' enclose protocol names rather than
9863          template arguments, and so everything is fine.  */
9864       if (c_dialect_objc ()
9865           && (objc_is_id (type) || objc_is_class_name (type)))
9866         {
9867           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9868           tree qual_type = objc_get_protocol_qualified_type (type, protos);
9869
9870           /* Clobber the "unqualified" type previously entered into
9871              DECL_SPECS with the new, improved protocol-qualified version.  */
9872           if (decl_specs)
9873             decl_specs->type = qual_type;
9874
9875           return qual_type;
9876         }
9877
9878       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9879     }
9880
9881   return type;
9882 }
9883
9884 /* Parse a type-name.
9885
9886    type-name:
9887      class-name
9888      enum-name
9889      typedef-name
9890
9891    enum-name:
9892      identifier
9893
9894    typedef-name:
9895      identifier
9896
9897    Returns a TYPE_DECL for the type.  */
9898
9899 static tree
9900 cp_parser_type_name (cp_parser* parser)
9901 {
9902   tree type_decl;
9903   tree identifier;
9904
9905   /* We can't know yet whether it is a class-name or not.  */
9906   cp_parser_parse_tentatively (parser);
9907   /* Try a class-name.  */
9908   type_decl = cp_parser_class_name (parser,
9909                                     /*typename_keyword_p=*/false,
9910                                     /*template_keyword_p=*/false,
9911                                     none_type,
9912                                     /*check_dependency_p=*/true,
9913                                     /*class_head_p=*/false,
9914                                     /*is_declaration=*/false);
9915   /* If it's not a class-name, keep looking.  */
9916   if (!cp_parser_parse_definitely (parser))
9917     {
9918       /* It must be a typedef-name or an enum-name.  */
9919       identifier = cp_parser_identifier (parser);
9920       if (identifier == error_mark_node)
9921         return error_mark_node;
9922
9923       /* Look up the type-name.  */
9924       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9925
9926       if (TREE_CODE (type_decl) != TYPE_DECL
9927           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9928         {
9929           /* See if this is an Objective-C type.  */
9930           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9931           tree type = objc_get_protocol_qualified_type (identifier, protos);
9932           if (type)
9933             type_decl = TYPE_NAME (type);
9934         }
9935
9936       /* Issue an error if we did not find a type-name.  */
9937       if (TREE_CODE (type_decl) != TYPE_DECL)
9938         {
9939           if (!cp_parser_simulate_error (parser))
9940             cp_parser_name_lookup_error (parser, identifier, type_decl,
9941                                          "is not a type");
9942           type_decl = error_mark_node;
9943         }
9944       /* Remember that the name was used in the definition of the
9945          current class so that we can check later to see if the
9946          meaning would have been different after the class was
9947          entirely defined.  */
9948       else if (type_decl != error_mark_node
9949                && !parser->scope)
9950         maybe_note_name_used_in_class (identifier, type_decl);
9951     }
9952
9953   return type_decl;
9954 }
9955
9956
9957 /* Parse an elaborated-type-specifier.  Note that the grammar given
9958    here incorporates the resolution to DR68.
9959
9960    elaborated-type-specifier:
9961      class-key :: [opt] nested-name-specifier [opt] identifier
9962      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9963      enum :: [opt] nested-name-specifier [opt] identifier
9964      typename :: [opt] nested-name-specifier identifier
9965      typename :: [opt] nested-name-specifier template [opt]
9966        template-id
9967
9968    GNU extension:
9969
9970    elaborated-type-specifier:
9971      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9972      class-key attributes :: [opt] nested-name-specifier [opt]
9973                template [opt] template-id
9974      enum attributes :: [opt] nested-name-specifier [opt] identifier
9975
9976    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9977    declared `friend'.  If IS_DECLARATION is TRUE, then this
9978    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9979    something is being declared.
9980
9981    Returns the TYPE specified.  */
9982
9983 static tree
9984 cp_parser_elaborated_type_specifier (cp_parser* parser,
9985                                      bool is_friend,
9986                                      bool is_declaration)
9987 {
9988   enum tag_types tag_type;
9989   tree identifier;
9990   tree type = NULL_TREE;
9991   tree attributes = NULL_TREE;
9992
9993   /* See if we're looking at the `enum' keyword.  */
9994   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9995     {
9996       /* Consume the `enum' token.  */
9997       cp_lexer_consume_token (parser->lexer);
9998       /* Remember that it's an enumeration type.  */
9999       tag_type = enum_type;
10000       /* Parse the attributes.  */
10001       attributes = cp_parser_attributes_opt (parser);
10002     }
10003   /* Or, it might be `typename'.  */
10004   else if (cp_lexer_next_token_is_keyword (parser->lexer,
10005                                            RID_TYPENAME))
10006     {
10007       /* Consume the `typename' token.  */
10008       cp_lexer_consume_token (parser->lexer);
10009       /* Remember that it's a `typename' type.  */
10010       tag_type = typename_type;
10011       /* The `typename' keyword is only allowed in templates.  */
10012       if (!processing_template_decl)
10013         pedwarn ("using %<typename%> outside of template");
10014     }
10015   /* Otherwise it must be a class-key.  */
10016   else
10017     {
10018       tag_type = cp_parser_class_key (parser);
10019       if (tag_type == none_type)
10020         return error_mark_node;
10021       /* Parse the attributes.  */
10022       attributes = cp_parser_attributes_opt (parser);
10023     }
10024
10025   /* Look for the `::' operator.  */
10026   cp_parser_global_scope_opt (parser,
10027                               /*current_scope_valid_p=*/false);
10028   /* Look for the nested-name-specifier.  */
10029   if (tag_type == typename_type)
10030     {
10031       if (!cp_parser_nested_name_specifier (parser,
10032                                            /*typename_keyword_p=*/true,
10033                                            /*check_dependency_p=*/true,
10034                                            /*type_p=*/true,
10035                                             is_declaration))
10036         return error_mark_node;
10037     }
10038   else
10039     /* Even though `typename' is not present, the proposed resolution
10040        to Core Issue 180 says that in `class A<T>::B', `B' should be
10041        considered a type-name, even if `A<T>' is dependent.  */
10042     cp_parser_nested_name_specifier_opt (parser,
10043                                          /*typename_keyword_p=*/true,
10044                                          /*check_dependency_p=*/true,
10045                                          /*type_p=*/true,
10046                                          is_declaration);
10047   /* For everything but enumeration types, consider a template-id.  */
10048   /* For an enumeration type, consider only a plain identifier.  */
10049   if (tag_type != enum_type)
10050     {
10051       bool template_p = false;
10052       tree decl;
10053
10054       /* Allow the `template' keyword.  */
10055       template_p = cp_parser_optional_template_keyword (parser);
10056       /* If we didn't see `template', we don't know if there's a
10057          template-id or not.  */
10058       if (!template_p)
10059         cp_parser_parse_tentatively (parser);
10060       /* Parse the template-id.  */
10061       decl = cp_parser_template_id (parser, template_p,
10062                                     /*check_dependency_p=*/true,
10063                                     is_declaration);
10064       /* If we didn't find a template-id, look for an ordinary
10065          identifier.  */
10066       if (!template_p && !cp_parser_parse_definitely (parser))
10067         ;
10068       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10069          in effect, then we must assume that, upon instantiation, the
10070          template will correspond to a class.  */
10071       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10072                && tag_type == typename_type)
10073         type = make_typename_type (parser->scope, decl,
10074                                    typename_type,
10075                                    /*complain=*/tf_error);
10076       else
10077         type = TREE_TYPE (decl);
10078     }
10079
10080   if (!type)
10081     {
10082       identifier = cp_parser_identifier (parser);
10083
10084       if (identifier == error_mark_node)
10085         {
10086           parser->scope = NULL_TREE;
10087           return error_mark_node;
10088         }
10089
10090       /* For a `typename', we needn't call xref_tag.  */
10091       if (tag_type == typename_type
10092           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10093         return cp_parser_make_typename_type (parser, parser->scope,
10094                                              identifier);
10095       /* Look up a qualified name in the usual way.  */
10096       if (parser->scope)
10097         {
10098           tree decl;
10099
10100           decl = cp_parser_lookup_name (parser, identifier,
10101                                         tag_type,
10102                                         /*is_template=*/false,
10103                                         /*is_namespace=*/false,
10104                                         /*check_dependency=*/true,
10105                                         /*ambiguous_decls=*/NULL);
10106
10107           /* If we are parsing friend declaration, DECL may be a
10108              TEMPLATE_DECL tree node here.  However, we need to check
10109              whether this TEMPLATE_DECL results in valid code.  Consider
10110              the following example:
10111
10112                namespace N {
10113                  template <class T> class C {};
10114                }
10115                class X {
10116                  template <class T> friend class N::C; // #1, valid code
10117                };
10118                template <class T> class Y {
10119                  friend class N::C;                    // #2, invalid code
10120                };
10121
10122              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10123              name lookup of `N::C'.  We see that friend declaration must
10124              be template for the code to be valid.  Note that
10125              processing_template_decl does not work here since it is
10126              always 1 for the above two cases.  */
10127
10128           decl = (cp_parser_maybe_treat_template_as_class
10129                   (decl, /*tag_name_p=*/is_friend
10130                          && parser->num_template_parameter_lists));
10131
10132           if (TREE_CODE (decl) != TYPE_DECL)
10133             {
10134               cp_parser_diagnose_invalid_type_name (parser,
10135                                                     parser->scope,
10136                                                     identifier);
10137               return error_mark_node;
10138             }
10139
10140           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10141             check_elaborated_type_specifier
10142               (tag_type, decl,
10143                (parser->num_template_parameter_lists
10144                 || DECL_SELF_REFERENCE_P (decl)));
10145
10146           type = TREE_TYPE (decl);
10147         }
10148       else
10149         {
10150           /* An elaborated-type-specifier sometimes introduces a new type and
10151              sometimes names an existing type.  Normally, the rule is that it
10152              introduces a new type only if there is not an existing type of
10153              the same name already in scope.  For example, given:
10154
10155                struct S {};
10156                void f() { struct S s; }
10157
10158              the `struct S' in the body of `f' is the same `struct S' as in
10159              the global scope; the existing definition is used.  However, if
10160              there were no global declaration, this would introduce a new
10161              local class named `S'.
10162
10163              An exception to this rule applies to the following code:
10164
10165                namespace N { struct S; }
10166
10167              Here, the elaborated-type-specifier names a new type
10168              unconditionally; even if there is already an `S' in the
10169              containing scope this declaration names a new type.
10170              This exception only applies if the elaborated-type-specifier
10171              forms the complete declaration:
10172
10173                [class.name]
10174
10175                A declaration consisting solely of `class-key identifier ;' is
10176                either a redeclaration of the name in the current scope or a
10177                forward declaration of the identifier as a class name.  It
10178                introduces the name into the current scope.
10179
10180              We are in this situation precisely when the next token is a `;'.
10181
10182              An exception to the exception is that a `friend' declaration does
10183              *not* name a new type; i.e., given:
10184
10185                struct S { friend struct T; };
10186
10187              `T' is not a new type in the scope of `S'.
10188
10189              Also, `new struct S' or `sizeof (struct S)' never results in the
10190              definition of a new type; a new type can only be declared in a
10191              declaration context.  */
10192
10193           tag_scope ts;
10194           bool template_p;
10195
10196           if (is_friend)
10197             /* Friends have special name lookup rules.  */
10198             ts = ts_within_enclosing_non_class;
10199           else if (is_declaration
10200                    && cp_lexer_next_token_is (parser->lexer,
10201                                               CPP_SEMICOLON))
10202             /* This is a `class-key identifier ;' */
10203             ts = ts_current;
10204           else
10205             ts = ts_global;
10206
10207           template_p =
10208             (parser->num_template_parameter_lists
10209              && (cp_parser_next_token_starts_class_definition_p (parser)
10210                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10211           /* An unqualified name was used to reference this type, so
10212              there were no qualifying templates.  */
10213           if (!cp_parser_check_template_parameters (parser,
10214                                                     /*num_templates=*/0))
10215             return error_mark_node;
10216           type = xref_tag (tag_type, identifier, ts, template_p);
10217         }
10218     }
10219
10220   if (type == error_mark_node)
10221     return error_mark_node;
10222
10223   /* Allow attributes on forward declarations of classes.  */
10224   if (attributes)
10225     {
10226       if (TREE_CODE (type) == TYPENAME_TYPE)
10227         warning (OPT_Wattributes,
10228                  "attributes ignored on uninstantiated type");
10229       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10230                && ! processing_explicit_instantiation)
10231         warning (OPT_Wattributes,
10232                  "attributes ignored on template instantiation");
10233       else if (is_declaration && cp_parser_declares_only_class_p (parser))
10234         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10235       else
10236         warning (OPT_Wattributes,
10237                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10238     }
10239
10240   if (tag_type != enum_type)
10241     cp_parser_check_class_key (tag_type, type);
10242
10243   /* A "<" cannot follow an elaborated type specifier.  If that
10244      happens, the user was probably trying to form a template-id.  */
10245   cp_parser_check_for_invalid_template_id (parser, type);
10246
10247   return type;
10248 }
10249
10250 /* Parse an enum-specifier.
10251
10252    enum-specifier:
10253      enum identifier [opt] { enumerator-list [opt] }
10254
10255    GNU Extensions:
10256      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10257        attributes[opt]
10258
10259    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10260    if the token stream isn't an enum-specifier after all.  */
10261
10262 static tree
10263 cp_parser_enum_specifier (cp_parser* parser)
10264 {
10265   tree identifier;
10266   tree type;
10267   tree attributes;
10268
10269   /* Parse tentatively so that we can back up if we don't find a
10270      enum-specifier.  */
10271   cp_parser_parse_tentatively (parser);
10272
10273   /* Caller guarantees that the current token is 'enum', an identifier
10274      possibly follows, and the token after that is an opening brace.
10275      If we don't have an identifier, fabricate an anonymous name for
10276      the enumeration being defined.  */
10277   cp_lexer_consume_token (parser->lexer);
10278
10279   attributes = cp_parser_attributes_opt (parser);
10280
10281   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10282     identifier = cp_parser_identifier (parser);
10283   else
10284     identifier = make_anon_name ();
10285
10286   /* Look for the `{' but don't consume it yet.  */
10287   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10288     cp_parser_simulate_error (parser);
10289
10290   if (!cp_parser_parse_definitely (parser))
10291     return NULL_TREE;
10292
10293   /* Issue an error message if type-definitions are forbidden here.  */
10294   cp_parser_check_type_definition (parser);
10295
10296   /* Create the new type.  We do this before consuming the opening brace
10297      so the enum will be recorded as being on the line of its tag (or the
10298      'enum' keyword, if there is no tag).  */
10299   type = start_enum (identifier);
10300
10301   /* Consume the opening brace.  */
10302   cp_lexer_consume_token (parser->lexer);
10303
10304   if (type == error_mark_node)
10305     {
10306       cp_parser_skip_to_end_of_block_or_statement (parser);
10307       return error_mark_node;
10308     }
10309
10310   /* If the next token is not '}', then there are some enumerators.  */
10311   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10312     cp_parser_enumerator_list (parser, type);
10313
10314   /* Consume the final '}'.  */
10315   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10316
10317   /* Look for trailing attributes to apply to this enumeration, and
10318      apply them if appropriate.  */
10319   if (cp_parser_allow_gnu_extensions_p (parser))
10320     {
10321       tree trailing_attr = cp_parser_attributes_opt (parser);
10322       cplus_decl_attributes (&type,
10323                              trailing_attr,
10324                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10325     }
10326
10327   /* Finish up the enumeration.  */
10328   finish_enum (type);
10329
10330   return type;
10331 }
10332
10333 /* Parse an enumerator-list.  The enumerators all have the indicated
10334    TYPE.
10335
10336    enumerator-list:
10337      enumerator-definition
10338      enumerator-list , enumerator-definition  */
10339
10340 static void
10341 cp_parser_enumerator_list (cp_parser* parser, tree type)
10342 {
10343   while (true)
10344     {
10345       /* Parse an enumerator-definition.  */
10346       cp_parser_enumerator_definition (parser, type);
10347
10348       /* If the next token is not a ',', we've reached the end of
10349          the list.  */
10350       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10351         break;
10352       /* Otherwise, consume the `,' and keep going.  */
10353       cp_lexer_consume_token (parser->lexer);
10354       /* If the next token is a `}', there is a trailing comma.  */
10355       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10356         {
10357           if (pedantic && !in_system_header)
10358             pedwarn ("comma at end of enumerator list");
10359           break;
10360         }
10361     }
10362 }
10363
10364 /* Parse an enumerator-definition.  The enumerator has the indicated
10365    TYPE.
10366
10367    enumerator-definition:
10368      enumerator
10369      enumerator = constant-expression
10370
10371    enumerator:
10372      identifier  */
10373
10374 static void
10375 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10376 {
10377   tree identifier;
10378   tree value;
10379
10380   /* Look for the identifier.  */
10381   identifier = cp_parser_identifier (parser);
10382   if (identifier == error_mark_node)
10383     return;
10384
10385   /* If the next token is an '=', then there is an explicit value.  */
10386   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10387     {
10388       /* Consume the `=' token.  */
10389       cp_lexer_consume_token (parser->lexer);
10390       /* Parse the value.  */
10391       value = cp_parser_constant_expression (parser,
10392                                              /*allow_non_constant_p=*/false,
10393                                              NULL);
10394     }
10395   else
10396     value = NULL_TREE;
10397
10398   /* Create the enumerator.  */
10399   build_enumerator (identifier, value, type);
10400 }
10401
10402 /* Parse a namespace-name.
10403
10404    namespace-name:
10405      original-namespace-name
10406      namespace-alias
10407
10408    Returns the NAMESPACE_DECL for the namespace.  */
10409
10410 static tree
10411 cp_parser_namespace_name (cp_parser* parser)
10412 {
10413   tree identifier;
10414   tree namespace_decl;
10415
10416   /* Get the name of the namespace.  */
10417   identifier = cp_parser_identifier (parser);
10418   if (identifier == error_mark_node)
10419     return error_mark_node;
10420
10421   /* Look up the identifier in the currently active scope.  Look only
10422      for namespaces, due to:
10423
10424        [basic.lookup.udir]
10425
10426        When looking up a namespace-name in a using-directive or alias
10427        definition, only namespace names are considered.
10428
10429      And:
10430
10431        [basic.lookup.qual]
10432
10433        During the lookup of a name preceding the :: scope resolution
10434        operator, object, function, and enumerator names are ignored.
10435
10436      (Note that cp_parser_class_or_namespace_name only calls this
10437      function if the token after the name is the scope resolution
10438      operator.)  */
10439   namespace_decl = cp_parser_lookup_name (parser, identifier,
10440                                           none_type,
10441                                           /*is_template=*/false,
10442                                           /*is_namespace=*/true,
10443                                           /*check_dependency=*/true,
10444                                           /*ambiguous_decls=*/NULL);
10445   /* If it's not a namespace, issue an error.  */
10446   if (namespace_decl == error_mark_node
10447       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10448     {
10449       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10450         error ("%qD is not a namespace-name", identifier);
10451       cp_parser_error (parser, "expected namespace-name");
10452       namespace_decl = error_mark_node;
10453     }
10454
10455   return namespace_decl;
10456 }
10457
10458 /* Parse a namespace-definition.
10459
10460    namespace-definition:
10461      named-namespace-definition
10462      unnamed-namespace-definition
10463
10464    named-namespace-definition:
10465      original-namespace-definition
10466      extension-namespace-definition
10467
10468    original-namespace-definition:
10469      namespace identifier { namespace-body }
10470
10471    extension-namespace-definition:
10472      namespace original-namespace-name { namespace-body }
10473
10474    unnamed-namespace-definition:
10475      namespace { namespace-body } */
10476
10477 static void
10478 cp_parser_namespace_definition (cp_parser* parser)
10479 {
10480   tree identifier, attribs;
10481
10482   /* Look for the `namespace' keyword.  */
10483   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10484
10485   /* Get the name of the namespace.  We do not attempt to distinguish
10486      between an original-namespace-definition and an
10487      extension-namespace-definition at this point.  The semantic
10488      analysis routines are responsible for that.  */
10489   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10490     identifier = cp_parser_identifier (parser);
10491   else
10492     identifier = NULL_TREE;
10493
10494   /* Parse any specified attributes.  */
10495   attribs = cp_parser_attributes_opt (parser);
10496
10497   /* Look for the `{' to start the namespace.  */
10498   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10499   /* Start the namespace.  */
10500   push_namespace_with_attribs (identifier, attribs);
10501   /* Parse the body of the namespace.  */
10502   cp_parser_namespace_body (parser);
10503   /* Finish the namespace.  */
10504   pop_namespace ();
10505   /* Look for the final `}'.  */
10506   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10507 }
10508
10509 /* Parse a namespace-body.
10510
10511    namespace-body:
10512      declaration-seq [opt]  */
10513
10514 static void
10515 cp_parser_namespace_body (cp_parser* parser)
10516 {
10517   cp_parser_declaration_seq_opt (parser);
10518 }
10519
10520 /* Parse a namespace-alias-definition.
10521
10522    namespace-alias-definition:
10523      namespace identifier = qualified-namespace-specifier ;  */
10524
10525 static void
10526 cp_parser_namespace_alias_definition (cp_parser* parser)
10527 {
10528   tree identifier;
10529   tree namespace_specifier;
10530
10531   /* Look for the `namespace' keyword.  */
10532   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10533   /* Look for the identifier.  */
10534   identifier = cp_parser_identifier (parser);
10535   if (identifier == error_mark_node)
10536     return;
10537   /* Look for the `=' token.  */
10538   cp_parser_require (parser, CPP_EQ, "`='");
10539   /* Look for the qualified-namespace-specifier.  */
10540   namespace_specifier
10541     = cp_parser_qualified_namespace_specifier (parser);
10542   /* Look for the `;' token.  */
10543   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10544
10545   /* Register the alias in the symbol table.  */
10546   do_namespace_alias (identifier, namespace_specifier);
10547 }
10548
10549 /* Parse a qualified-namespace-specifier.
10550
10551    qualified-namespace-specifier:
10552      :: [opt] nested-name-specifier [opt] namespace-name
10553
10554    Returns a NAMESPACE_DECL corresponding to the specified
10555    namespace.  */
10556
10557 static tree
10558 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10559 {
10560   /* Look for the optional `::'.  */
10561   cp_parser_global_scope_opt (parser,
10562                               /*current_scope_valid_p=*/false);
10563
10564   /* Look for the optional nested-name-specifier.  */
10565   cp_parser_nested_name_specifier_opt (parser,
10566                                        /*typename_keyword_p=*/false,
10567                                        /*check_dependency_p=*/true,
10568                                        /*type_p=*/false,
10569                                        /*is_declaration=*/true);
10570
10571   return cp_parser_namespace_name (parser);
10572 }
10573
10574 /* Parse a using-declaration.
10575
10576    using-declaration:
10577      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10578      using :: unqualified-id ;  */
10579
10580 static void
10581 cp_parser_using_declaration (cp_parser* parser)
10582 {
10583   cp_token *token;
10584   bool typename_p = false;
10585   bool global_scope_p;
10586   tree decl;
10587   tree identifier;
10588   tree qscope;
10589
10590   /* Look for the `using' keyword.  */
10591   cp_parser_require_keyword (parser, RID_USING, "`using'");
10592
10593   /* Peek at the next token.  */
10594   token = cp_lexer_peek_token (parser->lexer);
10595   /* See if it's `typename'.  */
10596   if (token->keyword == RID_TYPENAME)
10597     {
10598       /* Remember that we've seen it.  */
10599       typename_p = true;
10600       /* Consume the `typename' token.  */
10601       cp_lexer_consume_token (parser->lexer);
10602     }
10603
10604   /* Look for the optional global scope qualification.  */
10605   global_scope_p
10606     = (cp_parser_global_scope_opt (parser,
10607                                    /*current_scope_valid_p=*/false)
10608        != NULL_TREE);
10609
10610   /* If we saw `typename', or didn't see `::', then there must be a
10611      nested-name-specifier present.  */
10612   if (typename_p || !global_scope_p)
10613     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10614                                               /*check_dependency_p=*/true,
10615                                               /*type_p=*/false,
10616                                               /*is_declaration=*/true);
10617   /* Otherwise, we could be in either of the two productions.  In that
10618      case, treat the nested-name-specifier as optional.  */
10619   else
10620     qscope = cp_parser_nested_name_specifier_opt (parser,
10621                                                   /*typename_keyword_p=*/false,
10622                                                   /*check_dependency_p=*/true,
10623                                                   /*type_p=*/false,
10624                                                   /*is_declaration=*/true);
10625   if (!qscope)
10626     qscope = global_namespace;
10627
10628   /* Parse the unqualified-id.  */
10629   identifier = cp_parser_unqualified_id (parser,
10630                                          /*template_keyword_p=*/false,
10631                                          /*check_dependency_p=*/true,
10632                                          /*declarator_p=*/true,
10633                                          /*optional_p=*/false);
10634
10635   /* The function we call to handle a using-declaration is different
10636      depending on what scope we are in.  */
10637   if (qscope == error_mark_node || identifier == error_mark_node)
10638     ;
10639   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10640            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10641     /* [namespace.udecl]
10642
10643        A using declaration shall not name a template-id.  */
10644     error ("a template-id may not appear in a using-declaration");
10645   else
10646     {
10647       if (at_class_scope_p ())
10648         {
10649           /* Create the USING_DECL.  */
10650           decl = do_class_using_decl (parser->scope, identifier);
10651           /* Add it to the list of members in this class.  */
10652           finish_member_declaration (decl);
10653         }
10654       else
10655         {
10656           decl = cp_parser_lookup_name_simple (parser, identifier);
10657           if (decl == error_mark_node)
10658             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10659           else if (!at_namespace_scope_p ())
10660             do_local_using_decl (decl, qscope, identifier);
10661           else
10662             do_toplevel_using_decl (decl, qscope, identifier);
10663         }
10664     }
10665
10666   /* Look for the final `;'.  */
10667   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10668 }
10669
10670 /* Parse a using-directive.
10671
10672    using-directive:
10673      using namespace :: [opt] nested-name-specifier [opt]
10674        namespace-name ;  */
10675
10676 static void
10677 cp_parser_using_directive (cp_parser* parser)
10678 {
10679   tree namespace_decl;
10680   tree attribs;
10681
10682   /* Look for the `using' keyword.  */
10683   cp_parser_require_keyword (parser, RID_USING, "`using'");
10684   /* And the `namespace' keyword.  */
10685   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10686   /* Look for the optional `::' operator.  */
10687   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10688   /* And the optional nested-name-specifier.  */
10689   cp_parser_nested_name_specifier_opt (parser,
10690                                        /*typename_keyword_p=*/false,
10691                                        /*check_dependency_p=*/true,
10692                                        /*type_p=*/false,
10693                                        /*is_declaration=*/true);
10694   /* Get the namespace being used.  */
10695   namespace_decl = cp_parser_namespace_name (parser);
10696   /* And any specified attributes.  */
10697   attribs = cp_parser_attributes_opt (parser);
10698   /* Update the symbol table.  */
10699   parse_using_directive (namespace_decl, attribs);
10700   /* Look for the final `;'.  */
10701   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10702 }
10703
10704 /* Parse an asm-definition.
10705
10706    asm-definition:
10707      asm ( string-literal ) ;
10708
10709    GNU Extension:
10710
10711    asm-definition:
10712      asm volatile [opt] ( string-literal ) ;
10713      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10714      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10715                           : asm-operand-list [opt] ) ;
10716      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10717                           : asm-operand-list [opt]
10718                           : asm-operand-list [opt] ) ;  */
10719
10720 static void
10721 cp_parser_asm_definition (cp_parser* parser)
10722 {
10723   tree string;
10724   tree outputs = NULL_TREE;
10725   tree inputs = NULL_TREE;
10726   tree clobbers = NULL_TREE;
10727   tree asm_stmt;
10728   bool volatile_p = false;
10729   bool extended_p = false;
10730
10731   /* Look for the `asm' keyword.  */
10732   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10733   /* See if the next token is `volatile'.  */
10734   if (cp_parser_allow_gnu_extensions_p (parser)
10735       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10736     {
10737       /* Remember that we saw the `volatile' keyword.  */
10738       volatile_p = true;
10739       /* Consume the token.  */
10740       cp_lexer_consume_token (parser->lexer);
10741     }
10742   /* Look for the opening `('.  */
10743   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10744     return;
10745   /* Look for the string.  */
10746   string = cp_parser_string_literal (parser, false, false);
10747   if (string == error_mark_node)
10748     {
10749       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10750                                              /*consume_paren=*/true);
10751       return;
10752     }
10753
10754   /* If we're allowing GNU extensions, check for the extended assembly
10755      syntax.  Unfortunately, the `:' tokens need not be separated by
10756      a space in C, and so, for compatibility, we tolerate that here
10757      too.  Doing that means that we have to treat the `::' operator as
10758      two `:' tokens.  */
10759   if (cp_parser_allow_gnu_extensions_p (parser)
10760       && at_function_scope_p ()
10761       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10762           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10763     {
10764       bool inputs_p = false;
10765       bool clobbers_p = false;
10766
10767       /* The extended syntax was used.  */
10768       extended_p = true;
10769
10770       /* Look for outputs.  */
10771       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10772         {
10773           /* Consume the `:'.  */
10774           cp_lexer_consume_token (parser->lexer);
10775           /* Parse the output-operands.  */
10776           if (cp_lexer_next_token_is_not (parser->lexer,
10777                                           CPP_COLON)
10778               && cp_lexer_next_token_is_not (parser->lexer,
10779                                              CPP_SCOPE)
10780               && cp_lexer_next_token_is_not (parser->lexer,
10781                                              CPP_CLOSE_PAREN))
10782             outputs = cp_parser_asm_operand_list (parser);
10783         }
10784       /* If the next token is `::', there are no outputs, and the
10785          next token is the beginning of the inputs.  */
10786       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10787         /* The inputs are coming next.  */
10788         inputs_p = true;
10789
10790       /* Look for inputs.  */
10791       if (inputs_p
10792           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10793         {
10794           /* Consume the `:' or `::'.  */
10795           cp_lexer_consume_token (parser->lexer);
10796           /* Parse the output-operands.  */
10797           if (cp_lexer_next_token_is_not (parser->lexer,
10798                                           CPP_COLON)
10799               && cp_lexer_next_token_is_not (parser->lexer,
10800                                              CPP_CLOSE_PAREN))
10801             inputs = cp_parser_asm_operand_list (parser);
10802         }
10803       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10804         /* The clobbers are coming next.  */
10805         clobbers_p = true;
10806
10807       /* Look for clobbers.  */
10808       if (clobbers_p
10809           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10810         {
10811           /* Consume the `:' or `::'.  */
10812           cp_lexer_consume_token (parser->lexer);
10813           /* Parse the clobbers.  */
10814           if (cp_lexer_next_token_is_not (parser->lexer,
10815                                           CPP_CLOSE_PAREN))
10816             clobbers = cp_parser_asm_clobber_list (parser);
10817         }
10818     }
10819   /* Look for the closing `)'.  */
10820   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10821     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10822                                            /*consume_paren=*/true);
10823   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10824
10825   /* Create the ASM_EXPR.  */
10826   if (at_function_scope_p ())
10827     {
10828       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10829                                   inputs, clobbers);
10830       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10831       if (!extended_p)
10832         {
10833           tree temp = asm_stmt;
10834           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10835             temp = TREE_OPERAND (temp, 0);
10836
10837           ASM_INPUT_P (temp) = 1;
10838         }
10839     }
10840   else
10841     cgraph_add_asm_node (string);
10842 }
10843
10844 /* Declarators [gram.dcl.decl] */
10845
10846 /* Parse an init-declarator.
10847
10848    init-declarator:
10849      declarator initializer [opt]
10850
10851    GNU Extension:
10852
10853    init-declarator:
10854      declarator asm-specification [opt] attributes [opt] initializer [opt]
10855
10856    function-definition:
10857      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10858        function-body
10859      decl-specifier-seq [opt] declarator function-try-block
10860
10861    GNU Extension:
10862
10863    function-definition:
10864      __extension__ function-definition
10865
10866    The DECL_SPECIFIERS apply to this declarator.  Returns a
10867    representation of the entity declared.  If MEMBER_P is TRUE, then
10868    this declarator appears in a class scope.  The new DECL created by
10869    this declarator is returned.
10870
10871    The CHECKS are access checks that should be performed once we know
10872    what entity is being declared (and, therefore, what classes have
10873    befriended it).
10874
10875    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10876    for a function-definition here as well.  If the declarator is a
10877    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10878    be TRUE upon return.  By that point, the function-definition will
10879    have been completely parsed.
10880
10881    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10882    is FALSE.  */
10883
10884 static tree
10885 cp_parser_init_declarator (cp_parser* parser,
10886                            cp_decl_specifier_seq *decl_specifiers,
10887                            tree checks,
10888                            bool function_definition_allowed_p,
10889                            bool member_p,
10890                            int declares_class_or_enum,
10891                            bool* function_definition_p)
10892 {
10893   cp_token *token;
10894   cp_declarator *declarator;
10895   tree prefix_attributes;
10896   tree attributes;
10897   tree asm_specification;
10898   tree initializer;
10899   tree decl = NULL_TREE;
10900   tree scope;
10901   bool is_initialized;
10902   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
10903      initialized with "= ..", CPP_OPEN_PAREN if initialized with
10904      "(...)".  */
10905   enum cpp_ttype initialization_kind;
10906   bool is_parenthesized_init = false;
10907   bool is_non_constant_init;
10908   int ctor_dtor_or_conv_p;
10909   bool friend_p;
10910   tree pushed_scope = NULL;
10911
10912   /* Gather the attributes that were provided with the
10913      decl-specifiers.  */
10914   prefix_attributes = decl_specifiers->attributes;
10915
10916   /* Assume that this is not the declarator for a function
10917      definition.  */
10918   if (function_definition_p)
10919     *function_definition_p = false;
10920
10921   /* Defer access checks while parsing the declarator; we cannot know
10922      what names are accessible until we know what is being
10923      declared.  */
10924   resume_deferring_access_checks ();
10925
10926   /* Parse the declarator.  */
10927   declarator
10928     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10929                             &ctor_dtor_or_conv_p,
10930                             /*parenthesized_p=*/NULL,
10931                             /*member_p=*/false);
10932   /* Gather up the deferred checks.  */
10933   stop_deferring_access_checks ();
10934
10935   /* If the DECLARATOR was erroneous, there's no need to go
10936      further.  */
10937   if (declarator == cp_error_declarator)
10938     return error_mark_node;
10939
10940   if (declares_class_or_enum & 2)
10941     cp_parser_check_for_definition_in_return_type (declarator,
10942                                                    decl_specifiers->type);
10943
10944   /* Figure out what scope the entity declared by the DECLARATOR is
10945      located in.  `grokdeclarator' sometimes changes the scope, so
10946      we compute it now.  */
10947   scope = get_scope_of_declarator (declarator);
10948
10949   /* If we're allowing GNU extensions, look for an asm-specification
10950      and attributes.  */
10951   if (cp_parser_allow_gnu_extensions_p (parser))
10952     {
10953       /* Look for an asm-specification.  */
10954       asm_specification = cp_parser_asm_specification_opt (parser);
10955       /* And attributes.  */
10956       attributes = cp_parser_attributes_opt (parser);
10957     }
10958   else
10959     {
10960       asm_specification = NULL_TREE;
10961       attributes = NULL_TREE;
10962     }
10963
10964   /* Peek at the next token.  */
10965   token = cp_lexer_peek_token (parser->lexer);
10966   /* Check to see if the token indicates the start of a
10967      function-definition.  */
10968   if (cp_parser_token_starts_function_definition_p (token))
10969     {
10970       if (!function_definition_allowed_p)
10971         {
10972           /* If a function-definition should not appear here, issue an
10973              error message.  */
10974           cp_parser_error (parser,
10975                            "a function-definition is not allowed here");
10976           return error_mark_node;
10977         }
10978       else
10979         {
10980           /* Neither attributes nor an asm-specification are allowed
10981              on a function-definition.  */
10982           if (asm_specification)
10983             error ("an asm-specification is not allowed on a function-definition");
10984           if (attributes)
10985             error ("attributes are not allowed on a function-definition");
10986           /* This is a function-definition.  */
10987           *function_definition_p = true;
10988
10989           /* Parse the function definition.  */
10990           if (member_p)
10991             decl = cp_parser_save_member_function_body (parser,
10992                                                         decl_specifiers,
10993                                                         declarator,
10994                                                         prefix_attributes);
10995           else
10996             decl
10997               = (cp_parser_function_definition_from_specifiers_and_declarator
10998                  (parser, decl_specifiers, prefix_attributes, declarator));
10999
11000           return decl;
11001         }
11002     }
11003
11004   /* [dcl.dcl]
11005
11006      Only in function declarations for constructors, destructors, and
11007      type conversions can the decl-specifier-seq be omitted.
11008
11009      We explicitly postpone this check past the point where we handle
11010      function-definitions because we tolerate function-definitions
11011      that are missing their return types in some modes.  */
11012   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11013     {
11014       cp_parser_error (parser,
11015                        "expected constructor, destructor, or type conversion");
11016       return error_mark_node;
11017     }
11018
11019   /* An `=' or an `(' indicates an initializer.  */
11020   if (token->type == CPP_EQ
11021       || token->type == CPP_OPEN_PAREN)
11022     {
11023       is_initialized = true;
11024       initialization_kind = token->type;
11025     }
11026   else
11027     {
11028       /* If the init-declarator isn't initialized and isn't followed by a
11029          `,' or `;', it's not a valid init-declarator.  */
11030       if (token->type != CPP_COMMA
11031           && token->type != CPP_SEMICOLON)
11032         {
11033           cp_parser_error (parser, "expected initializer");
11034           return error_mark_node;
11035         }
11036       is_initialized = false;
11037       initialization_kind = CPP_EOF;
11038     }
11039
11040   /* Because start_decl has side-effects, we should only call it if we
11041      know we're going ahead.  By this point, we know that we cannot
11042      possibly be looking at any other construct.  */
11043   cp_parser_commit_to_tentative_parse (parser);
11044
11045   /* If the decl specifiers were bad, issue an error now that we're
11046      sure this was intended to be a declarator.  Then continue
11047      declaring the variable(s), as int, to try to cut down on further
11048      errors.  */
11049   if (decl_specifiers->any_specifiers_p
11050       && decl_specifiers->type == error_mark_node)
11051     {
11052       cp_parser_error (parser, "invalid type in declaration");
11053       decl_specifiers->type = integer_type_node;
11054     }
11055
11056   /* Check to see whether or not this declaration is a friend.  */
11057   friend_p = cp_parser_friend_p (decl_specifiers);
11058
11059   /* Check that the number of template-parameter-lists is OK.  */
11060   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11061     return error_mark_node;
11062
11063   /* Enter the newly declared entry in the symbol table.  If we're
11064      processing a declaration in a class-specifier, we wait until
11065      after processing the initializer.  */
11066   if (!member_p)
11067     {
11068       if (parser->in_unbraced_linkage_specification_p)
11069         decl_specifiers->storage_class = sc_extern;
11070       decl = start_decl (declarator, decl_specifiers,
11071                          is_initialized, attributes, prefix_attributes,
11072                          &pushed_scope);
11073     }
11074   else if (scope)
11075     /* Enter the SCOPE.  That way unqualified names appearing in the
11076        initializer will be looked up in SCOPE.  */
11077     pushed_scope = push_scope (scope);
11078
11079   /* Perform deferred access control checks, now that we know in which
11080      SCOPE the declared entity resides.  */
11081   if (!member_p && decl)
11082     {
11083       tree saved_current_function_decl = NULL_TREE;
11084
11085       /* If the entity being declared is a function, pretend that we
11086          are in its scope.  If it is a `friend', it may have access to
11087          things that would not otherwise be accessible.  */
11088       if (TREE_CODE (decl) == FUNCTION_DECL)
11089         {
11090           saved_current_function_decl = current_function_decl;
11091           current_function_decl = decl;
11092         }
11093
11094       /* Perform access checks for template parameters.  */
11095       cp_parser_perform_template_parameter_access_checks (checks);
11096
11097       /* Perform the access control checks for the declarator and the
11098          the decl-specifiers.  */
11099       perform_deferred_access_checks ();
11100
11101       /* Restore the saved value.  */
11102       if (TREE_CODE (decl) == FUNCTION_DECL)
11103         current_function_decl = saved_current_function_decl;
11104     }
11105
11106   /* Parse the initializer.  */
11107   initializer = NULL_TREE;
11108   is_parenthesized_init = false;
11109   is_non_constant_init = true;
11110   if (is_initialized)
11111     {
11112       if (declarator->kind == cdk_function
11113           && declarator->declarator->kind == cdk_id
11114           && initialization_kind == CPP_EQ)
11115         initializer = cp_parser_pure_specifier (parser);
11116       else
11117         initializer = cp_parser_initializer (parser,
11118                                              &is_parenthesized_init,
11119                                              &is_non_constant_init);
11120     }
11121
11122   /* The old parser allows attributes to appear after a parenthesized
11123      initializer.  Mark Mitchell proposed removing this functionality
11124      on the GCC mailing lists on 2002-08-13.  This parser accepts the
11125      attributes -- but ignores them.  */
11126   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11127     if (cp_parser_attributes_opt (parser))
11128       warning (OPT_Wattributes,
11129                "attributes after parenthesized initializer ignored");
11130
11131   /* For an in-class declaration, use `grokfield' to create the
11132      declaration.  */
11133   if (member_p)
11134     {
11135       if (pushed_scope)
11136         {
11137           pop_scope (pushed_scope);
11138           pushed_scope = false;
11139         }
11140       decl = grokfield (declarator, decl_specifiers,
11141                         initializer, !is_non_constant_init,
11142                         /*asmspec=*/NULL_TREE,
11143                         prefix_attributes);
11144       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11145         cp_parser_save_default_args (parser, decl);
11146     }
11147
11148   /* Finish processing the declaration.  But, skip friend
11149      declarations.  */
11150   if (!friend_p && decl && decl != error_mark_node)
11151     {
11152       cp_finish_decl (decl,
11153                       initializer, !is_non_constant_init,
11154                       asm_specification,
11155                       /* If the initializer is in parentheses, then this is
11156                          a direct-initialization, which means that an
11157                          `explicit' constructor is OK.  Otherwise, an
11158                          `explicit' constructor cannot be used.  */
11159                       ((is_parenthesized_init || !is_initialized)
11160                      ? 0 : LOOKUP_ONLYCONVERTING));
11161     }
11162   if (!friend_p && pushed_scope)
11163     pop_scope (pushed_scope);
11164
11165   return decl;
11166 }
11167
11168 /* Parse a declarator.
11169
11170    declarator:
11171      direct-declarator
11172      ptr-operator declarator
11173
11174    abstract-declarator:
11175      ptr-operator abstract-declarator [opt]
11176      direct-abstract-declarator
11177
11178    GNU Extensions:
11179
11180    declarator:
11181      attributes [opt] direct-declarator
11182      attributes [opt] ptr-operator declarator
11183
11184    abstract-declarator:
11185      attributes [opt] ptr-operator abstract-declarator [opt]
11186      attributes [opt] direct-abstract-declarator
11187
11188    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11189    detect constructor, destructor or conversion operators. It is set
11190    to -1 if the declarator is a name, and +1 if it is a
11191    function. Otherwise it is set to zero. Usually you just want to
11192    test for >0, but internally the negative value is used.
11193
11194    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11195    a decl-specifier-seq unless it declares a constructor, destructor,
11196    or conversion.  It might seem that we could check this condition in
11197    semantic analysis, rather than parsing, but that makes it difficult
11198    to handle something like `f()'.  We want to notice that there are
11199    no decl-specifiers, and therefore realize that this is an
11200    expression, not a declaration.)
11201
11202    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11203    the declarator is a direct-declarator of the form "(...)".
11204
11205    MEMBER_P is true iff this declarator is a member-declarator.  */
11206
11207 static cp_declarator *
11208 cp_parser_declarator (cp_parser* parser,
11209                       cp_parser_declarator_kind dcl_kind,
11210                       int* ctor_dtor_or_conv_p,
11211                       bool* parenthesized_p,
11212                       bool member_p)
11213 {
11214   cp_token *token;
11215   cp_declarator *declarator;
11216   enum tree_code code;
11217   cp_cv_quals cv_quals;
11218   tree class_type;
11219   tree attributes = NULL_TREE;
11220
11221   /* Assume this is not a constructor, destructor, or type-conversion
11222      operator.  */
11223   if (ctor_dtor_or_conv_p)
11224     *ctor_dtor_or_conv_p = 0;
11225
11226   if (cp_parser_allow_gnu_extensions_p (parser))
11227     attributes = cp_parser_attributes_opt (parser);
11228
11229   /* Peek at the next token.  */
11230   token = cp_lexer_peek_token (parser->lexer);
11231
11232   /* Check for the ptr-operator production.  */
11233   cp_parser_parse_tentatively (parser);
11234   /* Parse the ptr-operator.  */
11235   code = cp_parser_ptr_operator (parser,
11236                                  &class_type,
11237                                  &cv_quals);
11238   /* If that worked, then we have a ptr-operator.  */
11239   if (cp_parser_parse_definitely (parser))
11240     {
11241       /* If a ptr-operator was found, then this declarator was not
11242          parenthesized.  */
11243       if (parenthesized_p)
11244         *parenthesized_p = true;
11245       /* The dependent declarator is optional if we are parsing an
11246          abstract-declarator.  */
11247       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11248         cp_parser_parse_tentatively (parser);
11249
11250       /* Parse the dependent declarator.  */
11251       declarator = cp_parser_declarator (parser, dcl_kind,
11252                                          /*ctor_dtor_or_conv_p=*/NULL,
11253                                          /*parenthesized_p=*/NULL,
11254                                          /*member_p=*/false);
11255
11256       /* If we are parsing an abstract-declarator, we must handle the
11257          case where the dependent declarator is absent.  */
11258       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11259           && !cp_parser_parse_definitely (parser))
11260         declarator = NULL;
11261
11262       /* Build the representation of the ptr-operator.  */
11263       if (class_type)
11264         declarator = make_ptrmem_declarator (cv_quals,
11265                                              class_type,
11266                                              declarator);
11267       else if (code == INDIRECT_REF)
11268         declarator = make_pointer_declarator (cv_quals, declarator);
11269       else
11270         declarator = make_reference_declarator (cv_quals, declarator);
11271     }
11272   /* Everything else is a direct-declarator.  */
11273   else
11274     {
11275       if (parenthesized_p)
11276         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11277                                                    CPP_OPEN_PAREN);
11278       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11279                                                 ctor_dtor_or_conv_p,
11280                                                 member_p);
11281     }
11282
11283   if (attributes && declarator && declarator != cp_error_declarator)
11284     declarator->attributes = attributes;
11285
11286   return declarator;
11287 }
11288
11289 /* Parse a direct-declarator or direct-abstract-declarator.
11290
11291    direct-declarator:
11292      declarator-id
11293      direct-declarator ( parameter-declaration-clause )
11294        cv-qualifier-seq [opt]
11295        exception-specification [opt]
11296      direct-declarator [ constant-expression [opt] ]
11297      ( declarator )
11298
11299    direct-abstract-declarator:
11300      direct-abstract-declarator [opt]
11301        ( parameter-declaration-clause )
11302        cv-qualifier-seq [opt]
11303        exception-specification [opt]
11304      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11305      ( abstract-declarator )
11306
11307    Returns a representation of the declarator.  DCL_KIND is
11308    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11309    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11310    we are parsing a direct-declarator.  It is
11311    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11312    of ambiguity we prefer an abstract declarator, as per
11313    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11314    cp_parser_declarator.  */
11315
11316 static cp_declarator *
11317 cp_parser_direct_declarator (cp_parser* parser,
11318                              cp_parser_declarator_kind dcl_kind,
11319                              int* ctor_dtor_or_conv_p,
11320                              bool member_p)
11321 {
11322   cp_token *token;
11323   cp_declarator *declarator = NULL;
11324   tree scope = NULL_TREE;
11325   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11326   bool saved_in_declarator_p = parser->in_declarator_p;
11327   bool first = true;
11328   tree pushed_scope = NULL_TREE;
11329
11330   while (true)
11331     {
11332       /* Peek at the next token.  */
11333       token = cp_lexer_peek_token (parser->lexer);
11334       if (token->type == CPP_OPEN_PAREN)
11335         {
11336           /* This is either a parameter-declaration-clause, or a
11337              parenthesized declarator. When we know we are parsing a
11338              named declarator, it must be a parenthesized declarator
11339              if FIRST is true. For instance, `(int)' is a
11340              parameter-declaration-clause, with an omitted
11341              direct-abstract-declarator. But `((*))', is a
11342              parenthesized abstract declarator. Finally, when T is a
11343              template parameter `(T)' is a
11344              parameter-declaration-clause, and not a parenthesized
11345              named declarator.
11346
11347              We first try and parse a parameter-declaration-clause,
11348              and then try a nested declarator (if FIRST is true).
11349
11350              It is not an error for it not to be a
11351              parameter-declaration-clause, even when FIRST is
11352              false. Consider,
11353
11354                int i (int);
11355                int i (3);
11356
11357              The first is the declaration of a function while the
11358              second is a the definition of a variable, including its
11359              initializer.
11360
11361              Having seen only the parenthesis, we cannot know which of
11362              these two alternatives should be selected.  Even more
11363              complex are examples like:
11364
11365                int i (int (a));
11366                int i (int (3));
11367
11368              The former is a function-declaration; the latter is a
11369              variable initialization.
11370
11371              Thus again, we try a parameter-declaration-clause, and if
11372              that fails, we back out and return.  */
11373
11374           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11375             {
11376               cp_parameter_declarator *params;
11377               unsigned saved_num_template_parameter_lists;
11378
11379               /* In a member-declarator, the only valid interpretation
11380                  of a parenthesis is the start of a
11381                  parameter-declaration-clause.  (It is invalid to
11382                  initialize a static data member with a parenthesized
11383                  initializer; only the "=" form of initialization is
11384                  permitted.)  */
11385               if (!member_p)
11386                 cp_parser_parse_tentatively (parser);
11387
11388               /* Consume the `('.  */
11389               cp_lexer_consume_token (parser->lexer);
11390               if (first)
11391                 {
11392                   /* If this is going to be an abstract declarator, we're
11393                      in a declarator and we can't have default args.  */
11394                   parser->default_arg_ok_p = false;
11395                   parser->in_declarator_p = true;
11396                 }
11397
11398               /* Inside the function parameter list, surrounding
11399                  template-parameter-lists do not apply.  */
11400               saved_num_template_parameter_lists
11401                 = parser->num_template_parameter_lists;
11402               parser->num_template_parameter_lists = 0;
11403
11404               /* Parse the parameter-declaration-clause.  */
11405               params = cp_parser_parameter_declaration_clause (parser);
11406
11407               parser->num_template_parameter_lists
11408                 = saved_num_template_parameter_lists;
11409
11410               /* If all went well, parse the cv-qualifier-seq and the
11411                  exception-specification.  */
11412               if (member_p || cp_parser_parse_definitely (parser))
11413                 {
11414                   cp_cv_quals cv_quals;
11415                   tree exception_specification;
11416
11417                   if (ctor_dtor_or_conv_p)
11418                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11419                   first = false;
11420                   /* Consume the `)'.  */
11421                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11422
11423                   /* Parse the cv-qualifier-seq.  */
11424                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11425                   /* And the exception-specification.  */
11426                   exception_specification
11427                     = cp_parser_exception_specification_opt (parser);
11428
11429                   /* Create the function-declarator.  */
11430                   declarator = make_call_declarator (declarator,
11431                                                      params,
11432                                                      cv_quals,
11433                                                      exception_specification);
11434                   /* Any subsequent parameter lists are to do with
11435                      return type, so are not those of the declared
11436                      function.  */
11437                   parser->default_arg_ok_p = false;
11438
11439                   /* Repeat the main loop.  */
11440                   continue;
11441                 }
11442             }
11443
11444           /* If this is the first, we can try a parenthesized
11445              declarator.  */
11446           if (first)
11447             {
11448               bool saved_in_type_id_in_expr_p;
11449
11450               parser->default_arg_ok_p = saved_default_arg_ok_p;
11451               parser->in_declarator_p = saved_in_declarator_p;
11452
11453               /* Consume the `('.  */
11454               cp_lexer_consume_token (parser->lexer);
11455               /* Parse the nested declarator.  */
11456               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11457               parser->in_type_id_in_expr_p = true;
11458               declarator
11459                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11460                                         /*parenthesized_p=*/NULL,
11461                                         member_p);
11462               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11463               first = false;
11464               /* Expect a `)'.  */
11465               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11466                 declarator = cp_error_declarator;
11467               if (declarator == cp_error_declarator)
11468                 break;
11469
11470               goto handle_declarator;
11471             }
11472           /* Otherwise, we must be done.  */
11473           else
11474             break;
11475         }
11476       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11477                && token->type == CPP_OPEN_SQUARE)
11478         {
11479           /* Parse an array-declarator.  */
11480           tree bounds;
11481
11482           if (ctor_dtor_or_conv_p)
11483             *ctor_dtor_or_conv_p = 0;
11484
11485           first = false;
11486           parser->default_arg_ok_p = false;
11487           parser->in_declarator_p = true;
11488           /* Consume the `['.  */
11489           cp_lexer_consume_token (parser->lexer);
11490           /* Peek at the next token.  */
11491           token = cp_lexer_peek_token (parser->lexer);
11492           /* If the next token is `]', then there is no
11493              constant-expression.  */
11494           if (token->type != CPP_CLOSE_SQUARE)
11495             {
11496               bool non_constant_p;
11497
11498               bounds
11499                 = cp_parser_constant_expression (parser,
11500                                                  /*allow_non_constant=*/true,
11501                                                  &non_constant_p);
11502               if (!non_constant_p)
11503                 bounds = fold_non_dependent_expr (bounds);
11504               /* Normally, the array bound must be an integral constant
11505                  expression.  However, as an extension, we allow VLAs
11506                  in function scopes.  */
11507               else if (!at_function_scope_p ())
11508                 {
11509                   error ("array bound is not an integer constant");
11510                   bounds = error_mark_node;
11511                 }
11512             }
11513           else
11514             bounds = NULL_TREE;
11515           /* Look for the closing `]'.  */
11516           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11517             {
11518               declarator = cp_error_declarator;
11519               break;
11520             }
11521
11522           declarator = make_array_declarator (declarator, bounds);
11523         }
11524       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11525         {
11526           tree qualifying_scope;
11527           tree unqualified_name;
11528           special_function_kind sfk;
11529           bool abstract_ok;
11530
11531           /* Parse a declarator-id */
11532           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11533           if (abstract_ok)
11534             cp_parser_parse_tentatively (parser);
11535           unqualified_name
11536             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11537           qualifying_scope = parser->scope;
11538           if (abstract_ok)
11539             {
11540               if (!cp_parser_parse_definitely (parser))
11541                 unqualified_name = error_mark_node;
11542               else if (unqualified_name
11543                        && (qualifying_scope
11544                            || (TREE_CODE (unqualified_name)
11545                                != IDENTIFIER_NODE)))
11546                 {
11547                   cp_parser_error (parser, "expected unqualified-id");
11548                   unqualified_name = error_mark_node;
11549                 }
11550             }
11551
11552           if (!unqualified_name)
11553             return NULL;
11554           if (unqualified_name == error_mark_node)
11555             {
11556               declarator = cp_error_declarator;
11557               break;
11558             }
11559
11560           if (qualifying_scope && at_namespace_scope_p ()
11561               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11562             {
11563               /* In the declaration of a member of a template class
11564                  outside of the class itself, the SCOPE will sometimes
11565                  be a TYPENAME_TYPE.  For example, given:
11566
11567                  template <typename T>
11568                  int S<T>::R::i = 3;
11569
11570                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11571                  this context, we must resolve S<T>::R to an ordinary
11572                  type, rather than a typename type.
11573
11574                  The reason we normally avoid resolving TYPENAME_TYPEs
11575                  is that a specialization of `S' might render
11576                  `S<T>::R' not a type.  However, if `S' is
11577                  specialized, then this `i' will not be used, so there
11578                  is no harm in resolving the types here.  */
11579               tree type;
11580
11581               /* Resolve the TYPENAME_TYPE.  */
11582               type = resolve_typename_type (qualifying_scope,
11583                                             /*only_current_p=*/false);
11584               /* If that failed, the declarator is invalid.  */
11585               if (type == error_mark_node)
11586                 error ("%<%T::%D%> is not a type",
11587                        TYPE_CONTEXT (qualifying_scope),
11588                        TYPE_IDENTIFIER (qualifying_scope));
11589               qualifying_scope = type;
11590             }
11591
11592           sfk = sfk_none;
11593           if (unqualified_name)
11594             {
11595               tree class_type;
11596
11597               if (qualifying_scope
11598                   && CLASS_TYPE_P (qualifying_scope))
11599                 class_type = qualifying_scope;
11600               else
11601                 class_type = current_class_type;
11602
11603               if (TREE_CODE (unqualified_name) == TYPE_DECL)
11604                 {
11605                   tree name_type = TREE_TYPE (unqualified_name);
11606                   if (class_type && same_type_p (name_type, class_type))
11607                     {
11608                       if (qualifying_scope
11609                           && CLASSTYPE_USE_TEMPLATE (name_type))
11610                         {
11611                           error ("invalid use of constructor as a template");
11612                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11613                                   "name the constructor in a qualified name",
11614                                   class_type,
11615                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11616                                   class_type, name_type);
11617                           declarator = cp_error_declarator;
11618                           break;
11619                         }
11620                       else
11621                         unqualified_name = constructor_name (class_type);
11622                     }
11623                   else
11624                     {
11625                       /* We do not attempt to print the declarator
11626                          here because we do not have enough
11627                          information about its original syntactic
11628                          form.  */
11629                       cp_parser_error (parser, "invalid declarator");
11630                       declarator = cp_error_declarator;
11631                       break;
11632                     }
11633                 }
11634
11635               if (class_type)
11636                 {
11637                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11638                     sfk = sfk_destructor;
11639                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11640                     sfk = sfk_conversion;
11641                   else if (/* There's no way to declare a constructor
11642                               for an anonymous type, even if the type
11643                               got a name for linkage purposes.  */
11644                            !TYPE_WAS_ANONYMOUS (class_type)
11645                            && constructor_name_p (unqualified_name,
11646                                                   class_type))
11647                     {
11648                       unqualified_name = constructor_name (class_type);
11649                       sfk = sfk_constructor;
11650                     }
11651
11652                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
11653                     *ctor_dtor_or_conv_p = -1;
11654                 }
11655             }
11656           declarator = make_id_declarator (qualifying_scope,
11657                                            unqualified_name,
11658                                            sfk);
11659           declarator->id_loc = token->location;
11660
11661         handle_declarator:;
11662           scope = get_scope_of_declarator (declarator);
11663           if (scope)
11664             /* Any names that appear after the declarator-id for a
11665                member are looked up in the containing scope.  */
11666             pushed_scope = push_scope (scope);
11667           parser->in_declarator_p = true;
11668           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11669               || (declarator && declarator->kind == cdk_id))
11670             /* Default args are only allowed on function
11671                declarations.  */
11672             parser->default_arg_ok_p = saved_default_arg_ok_p;
11673           else
11674             parser->default_arg_ok_p = false;
11675
11676           first = false;
11677         }
11678       /* We're done.  */
11679       else
11680         break;
11681     }
11682
11683   /* For an abstract declarator, we might wind up with nothing at this
11684      point.  That's an error; the declarator is not optional.  */
11685   if (!declarator)
11686     cp_parser_error (parser, "expected declarator");
11687
11688   /* If we entered a scope, we must exit it now.  */
11689   if (pushed_scope)
11690     pop_scope (pushed_scope);
11691
11692   parser->default_arg_ok_p = saved_default_arg_ok_p;
11693   parser->in_declarator_p = saved_in_declarator_p;
11694
11695   return declarator;
11696 }
11697
11698 /* Parse a ptr-operator.
11699
11700    ptr-operator:
11701      * cv-qualifier-seq [opt]
11702      &
11703      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11704
11705    GNU Extension:
11706
11707    ptr-operator:
11708      & cv-qualifier-seq [opt]
11709
11710    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11711    Returns ADDR_EXPR if a reference was used.  In the case of a
11712    pointer-to-member, *TYPE is filled in with the TYPE containing the
11713    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11714    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11715    ERROR_MARK if an error occurred.  */
11716
11717 static enum tree_code
11718 cp_parser_ptr_operator (cp_parser* parser,
11719                         tree* type,
11720                         cp_cv_quals *cv_quals)
11721 {
11722   enum tree_code code = ERROR_MARK;
11723   cp_token *token;
11724
11725   /* Assume that it's not a pointer-to-member.  */
11726   *type = NULL_TREE;
11727   /* And that there are no cv-qualifiers.  */
11728   *cv_quals = TYPE_UNQUALIFIED;
11729
11730   /* Peek at the next token.  */
11731   token = cp_lexer_peek_token (parser->lexer);
11732   /* If it's a `*' or `&' we have a pointer or reference.  */
11733   if (token->type == CPP_MULT || token->type == CPP_AND)
11734     {
11735       /* Remember which ptr-operator we were processing.  */
11736       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11737
11738       /* Consume the `*' or `&'.  */
11739       cp_lexer_consume_token (parser->lexer);
11740
11741       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11742          `&', if we are allowing GNU extensions.  (The only qualifier
11743          that can legally appear after `&' is `restrict', but that is
11744          enforced during semantic analysis.  */
11745       if (code == INDIRECT_REF
11746           || cp_parser_allow_gnu_extensions_p (parser))
11747         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11748     }
11749   else
11750     {
11751       /* Try the pointer-to-member case.  */
11752       cp_parser_parse_tentatively (parser);
11753       /* Look for the optional `::' operator.  */
11754       cp_parser_global_scope_opt (parser,
11755                                   /*current_scope_valid_p=*/false);
11756       /* Look for the nested-name specifier.  */
11757       cp_parser_nested_name_specifier (parser,
11758                                        /*typename_keyword_p=*/false,
11759                                        /*check_dependency_p=*/true,
11760                                        /*type_p=*/false,
11761                                        /*is_declaration=*/false);
11762       /* If we found it, and the next token is a `*', then we are
11763          indeed looking at a pointer-to-member operator.  */
11764       if (!cp_parser_error_occurred (parser)
11765           && cp_parser_require (parser, CPP_MULT, "`*'"))
11766         {
11767           /* Indicate that the `*' operator was used.  */
11768           code = INDIRECT_REF;
11769
11770           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11771             error ("%qD is a namespace", parser->scope);
11772           else
11773             {
11774               /* The type of which the member is a member is given by the
11775                  current SCOPE.  */
11776               *type = parser->scope;
11777               /* The next name will not be qualified.  */
11778               parser->scope = NULL_TREE;
11779               parser->qualifying_scope = NULL_TREE;
11780               parser->object_scope = NULL_TREE;
11781               /* Look for the optional cv-qualifier-seq.  */
11782               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11783             }
11784         }
11785       /* If that didn't work we don't have a ptr-operator.  */
11786       if (!cp_parser_parse_definitely (parser))
11787         cp_parser_error (parser, "expected ptr-operator");
11788     }
11789
11790   return code;
11791 }
11792
11793 /* Parse an (optional) cv-qualifier-seq.
11794
11795    cv-qualifier-seq:
11796      cv-qualifier cv-qualifier-seq [opt]
11797
11798    cv-qualifier:
11799      const
11800      volatile
11801
11802    GNU Extension:
11803
11804    cv-qualifier:
11805      __restrict__
11806
11807    Returns a bitmask representing the cv-qualifiers.  */
11808
11809 static cp_cv_quals
11810 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11811 {
11812   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11813
11814   while (true)
11815     {
11816       cp_token *token;
11817       cp_cv_quals cv_qualifier;
11818
11819       /* Peek at the next token.  */
11820       token = cp_lexer_peek_token (parser->lexer);
11821       /* See if it's a cv-qualifier.  */
11822       switch (token->keyword)
11823         {
11824         case RID_CONST:
11825           cv_qualifier = TYPE_QUAL_CONST;
11826           break;
11827
11828         case RID_VOLATILE:
11829           cv_qualifier = TYPE_QUAL_VOLATILE;
11830           break;
11831
11832         case RID_RESTRICT:
11833           cv_qualifier = TYPE_QUAL_RESTRICT;
11834           break;
11835
11836         default:
11837           cv_qualifier = TYPE_UNQUALIFIED;
11838           break;
11839         }
11840
11841       if (!cv_qualifier)
11842         break;
11843
11844       if (cv_quals & cv_qualifier)
11845         {
11846           error ("duplicate cv-qualifier");
11847           cp_lexer_purge_token (parser->lexer);
11848         }
11849       else
11850         {
11851           cp_lexer_consume_token (parser->lexer);
11852           cv_quals |= cv_qualifier;
11853         }
11854     }
11855
11856   return cv_quals;
11857 }
11858
11859 /* Parse a declarator-id.
11860
11861    declarator-id:
11862      id-expression
11863      :: [opt] nested-name-specifier [opt] type-name
11864
11865    In the `id-expression' case, the value returned is as for
11866    cp_parser_id_expression if the id-expression was an unqualified-id.
11867    If the id-expression was a qualified-id, then a SCOPE_REF is
11868    returned.  The first operand is the scope (either a NAMESPACE_DECL
11869    or TREE_TYPE), but the second is still just a representation of an
11870    unqualified-id.  */
11871
11872 static tree
11873 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
11874 {
11875   tree id;
11876   /* The expression must be an id-expression.  Assume that qualified
11877      names are the names of types so that:
11878
11879        template <class T>
11880        int S<T>::R::i = 3;
11881
11882      will work; we must treat `S<T>::R' as the name of a type.
11883      Similarly, assume that qualified names are templates, where
11884      required, so that:
11885
11886        template <class T>
11887        int S<T>::R<T>::i = 3;
11888
11889      will work, too.  */
11890   id = cp_parser_id_expression (parser,
11891                                 /*template_keyword_p=*/false,
11892                                 /*check_dependency_p=*/false,
11893                                 /*template_p=*/NULL,
11894                                 /*declarator_p=*/true,
11895                                 optional_p);
11896   if (id && BASELINK_P (id))
11897     id = BASELINK_FUNCTIONS (id);
11898   return id;
11899 }
11900
11901 /* Parse a type-id.
11902
11903    type-id:
11904      type-specifier-seq abstract-declarator [opt]
11905
11906    Returns the TYPE specified.  */
11907
11908 static tree
11909 cp_parser_type_id (cp_parser* parser)
11910 {
11911   cp_decl_specifier_seq type_specifier_seq;
11912   cp_declarator *abstract_declarator;
11913
11914   /* Parse the type-specifier-seq.  */
11915   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11916                                 &type_specifier_seq);
11917   if (type_specifier_seq.type == error_mark_node)
11918     return error_mark_node;
11919
11920   /* There might or might not be an abstract declarator.  */
11921   cp_parser_parse_tentatively (parser);
11922   /* Look for the declarator.  */
11923   abstract_declarator
11924     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11925                             /*parenthesized_p=*/NULL,
11926                             /*member_p=*/false);
11927   /* Check to see if there really was a declarator.  */
11928   if (!cp_parser_parse_definitely (parser))
11929     abstract_declarator = NULL;
11930
11931   return groktypename (&type_specifier_seq, abstract_declarator);
11932 }
11933
11934 /* Parse a type-specifier-seq.
11935
11936    type-specifier-seq:
11937      type-specifier type-specifier-seq [opt]
11938
11939    GNU extension:
11940
11941    type-specifier-seq:
11942      attributes type-specifier-seq [opt]
11943
11944    If IS_CONDITION is true, we are at the start of a "condition",
11945    e.g., we've just seen "if (".
11946
11947    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11948
11949 static void
11950 cp_parser_type_specifier_seq (cp_parser* parser,
11951                               bool is_condition,
11952                               cp_decl_specifier_seq *type_specifier_seq)
11953 {
11954   bool seen_type_specifier = false;
11955   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11956
11957   /* Clear the TYPE_SPECIFIER_SEQ.  */
11958   clear_decl_specs (type_specifier_seq);
11959
11960   /* Parse the type-specifiers and attributes.  */
11961   while (true)
11962     {
11963       tree type_specifier;
11964       bool is_cv_qualifier;
11965
11966       /* Check for attributes first.  */
11967       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11968         {
11969           type_specifier_seq->attributes =
11970             chainon (type_specifier_seq->attributes,
11971                      cp_parser_attributes_opt (parser));
11972           continue;
11973         }
11974
11975       /* Look for the type-specifier.  */
11976       type_specifier = cp_parser_type_specifier (parser,
11977                                                  flags,
11978                                                  type_specifier_seq,
11979                                                  /*is_declaration=*/false,
11980                                                  NULL,
11981                                                  &is_cv_qualifier);
11982       if (!type_specifier)
11983         {
11984           /* If the first type-specifier could not be found, this is not a
11985              type-specifier-seq at all.  */
11986           if (!seen_type_specifier)
11987             {
11988               cp_parser_error (parser, "expected type-specifier");
11989               type_specifier_seq->type = error_mark_node;
11990               return;
11991             }
11992           /* If subsequent type-specifiers could not be found, the
11993              type-specifier-seq is complete.  */
11994           break;
11995         }
11996
11997       seen_type_specifier = true;
11998       /* The standard says that a condition can be:
11999
12000             type-specifier-seq declarator = assignment-expression
12001
12002          However, given:
12003
12004            struct S {};
12005            if (int S = ...)
12006
12007          we should treat the "S" as a declarator, not as a
12008          type-specifier.  The standard doesn't say that explicitly for
12009          type-specifier-seq, but it does say that for
12010          decl-specifier-seq in an ordinary declaration.  Perhaps it
12011          would be clearer just to allow a decl-specifier-seq here, and
12012          then add a semantic restriction that if any decl-specifiers
12013          that are not type-specifiers appear, the program is invalid.  */
12014       if (is_condition && !is_cv_qualifier)
12015         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12016     }
12017
12018   cp_parser_check_decl_spec (type_specifier_seq);
12019 }
12020
12021 /* Parse a parameter-declaration-clause.
12022
12023    parameter-declaration-clause:
12024      parameter-declaration-list [opt] ... [opt]
12025      parameter-declaration-list , ...
12026
12027    Returns a representation for the parameter declarations.  A return
12028    value of NULL indicates a parameter-declaration-clause consisting
12029    only of an ellipsis.  */
12030
12031 static cp_parameter_declarator *
12032 cp_parser_parameter_declaration_clause (cp_parser* parser)
12033 {
12034   cp_parameter_declarator *parameters;
12035   cp_token *token;
12036   bool ellipsis_p;
12037   bool is_error;
12038
12039   /* Peek at the next token.  */
12040   token = cp_lexer_peek_token (parser->lexer);
12041   /* Check for trivial parameter-declaration-clauses.  */
12042   if (token->type == CPP_ELLIPSIS)
12043     {
12044       /* Consume the `...' token.  */
12045       cp_lexer_consume_token (parser->lexer);
12046       return NULL;
12047     }
12048   else if (token->type == CPP_CLOSE_PAREN)
12049     /* There are no parameters.  */
12050     {
12051 #ifndef NO_IMPLICIT_EXTERN_C
12052       if (in_system_header && current_class_type == NULL
12053           && current_lang_name == lang_name_c)
12054         return NULL;
12055       else
12056 #endif
12057         return no_parameters;
12058     }
12059   /* Check for `(void)', too, which is a special case.  */
12060   else if (token->keyword == RID_VOID
12061            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12062                == CPP_CLOSE_PAREN))
12063     {
12064       /* Consume the `void' token.  */
12065       cp_lexer_consume_token (parser->lexer);
12066       /* There are no parameters.  */
12067       return no_parameters;
12068     }
12069
12070   /* Parse the parameter-declaration-list.  */
12071   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12072   /* If a parse error occurred while parsing the
12073      parameter-declaration-list, then the entire
12074      parameter-declaration-clause is erroneous.  */
12075   if (is_error)
12076     return NULL;
12077
12078   /* Peek at the next token.  */
12079   token = cp_lexer_peek_token (parser->lexer);
12080   /* If it's a `,', the clause should terminate with an ellipsis.  */
12081   if (token->type == CPP_COMMA)
12082     {
12083       /* Consume the `,'.  */
12084       cp_lexer_consume_token (parser->lexer);
12085       /* Expect an ellipsis.  */
12086       ellipsis_p
12087         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12088     }
12089   /* It might also be `...' if the optional trailing `,' was
12090      omitted.  */
12091   else if (token->type == CPP_ELLIPSIS)
12092     {
12093       /* Consume the `...' token.  */
12094       cp_lexer_consume_token (parser->lexer);
12095       /* And remember that we saw it.  */
12096       ellipsis_p = true;
12097     }
12098   else
12099     ellipsis_p = false;
12100
12101   /* Finish the parameter list.  */
12102   if (parameters && ellipsis_p)
12103     parameters->ellipsis_p = true;
12104
12105   return parameters;
12106 }
12107
12108 /* Parse a parameter-declaration-list.
12109
12110    parameter-declaration-list:
12111      parameter-declaration
12112      parameter-declaration-list , parameter-declaration
12113
12114    Returns a representation of the parameter-declaration-list, as for
12115    cp_parser_parameter_declaration_clause.  However, the
12116    `void_list_node' is never appended to the list.  Upon return,
12117    *IS_ERROR will be true iff an error occurred.  */
12118
12119 static cp_parameter_declarator *
12120 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12121 {
12122   cp_parameter_declarator *parameters = NULL;
12123   cp_parameter_declarator **tail = &parameters;
12124   bool saved_in_unbraced_linkage_specification_p;
12125
12126   /* Assume all will go well.  */
12127   *is_error = false;
12128   /* The special considerations that apply to a function within an
12129      unbraced linkage specifications do not apply to the parameters
12130      to the function.  */
12131   saved_in_unbraced_linkage_specification_p 
12132     = parser->in_unbraced_linkage_specification_p;
12133   parser->in_unbraced_linkage_specification_p = false;
12134
12135   /* Look for more parameters.  */
12136   while (true)
12137     {
12138       cp_parameter_declarator *parameter;
12139       bool parenthesized_p;
12140       /* Parse the parameter.  */
12141       parameter
12142         = cp_parser_parameter_declaration (parser,
12143                                            /*template_parm_p=*/false,
12144                                            &parenthesized_p);
12145
12146       /* If a parse error occurred parsing the parameter declaration,
12147          then the entire parameter-declaration-list is erroneous.  */
12148       if (!parameter)
12149         {
12150           *is_error = true;
12151           parameters = NULL;
12152           break;
12153         }
12154       /* Add the new parameter to the list.  */
12155       *tail = parameter;
12156       tail = &parameter->next;
12157
12158       /* Peek at the next token.  */
12159       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12160           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12161           /* These are for Objective-C++ */
12162           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12163           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12164         /* The parameter-declaration-list is complete.  */
12165         break;
12166       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12167         {
12168           cp_token *token;
12169
12170           /* Peek at the next token.  */
12171           token = cp_lexer_peek_nth_token (parser->lexer, 2);
12172           /* If it's an ellipsis, then the list is complete.  */
12173           if (token->type == CPP_ELLIPSIS)
12174             break;
12175           /* Otherwise, there must be more parameters.  Consume the
12176              `,'.  */
12177           cp_lexer_consume_token (parser->lexer);
12178           /* When parsing something like:
12179
12180                 int i(float f, double d)
12181
12182              we can tell after seeing the declaration for "f" that we
12183              are not looking at an initialization of a variable "i",
12184              but rather at the declaration of a function "i".
12185
12186              Due to the fact that the parsing of template arguments
12187              (as specified to a template-id) requires backtracking we
12188              cannot use this technique when inside a template argument
12189              list.  */
12190           if (!parser->in_template_argument_list_p
12191               && !parser->in_type_id_in_expr_p
12192               && cp_parser_uncommitted_to_tentative_parse_p (parser)
12193               /* However, a parameter-declaration of the form
12194                  "foat(f)" (which is a valid declaration of a
12195                  parameter "f") can also be interpreted as an
12196                  expression (the conversion of "f" to "float").  */
12197               && !parenthesized_p)
12198             cp_parser_commit_to_tentative_parse (parser);
12199         }
12200       else
12201         {
12202           cp_parser_error (parser, "expected %<,%> or %<...%>");
12203           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12204             cp_parser_skip_to_closing_parenthesis (parser,
12205                                                    /*recovering=*/true,
12206                                                    /*or_comma=*/false,
12207                                                    /*consume_paren=*/false);
12208           break;
12209         }
12210     }
12211
12212   parser->in_unbraced_linkage_specification_p
12213     = saved_in_unbraced_linkage_specification_p;
12214
12215   return parameters;
12216 }
12217
12218 /* Parse a parameter declaration.
12219
12220    parameter-declaration:
12221      decl-specifier-seq declarator
12222      decl-specifier-seq declarator = assignment-expression
12223      decl-specifier-seq abstract-declarator [opt]
12224      decl-specifier-seq abstract-declarator [opt] = assignment-expression
12225
12226    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12227    declares a template parameter.  (In that case, a non-nested `>'
12228    token encountered during the parsing of the assignment-expression
12229    is not interpreted as a greater-than operator.)
12230
12231    Returns a representation of the parameter, or NULL if an error
12232    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12233    true iff the declarator is of the form "(p)".  */
12234
12235 static cp_parameter_declarator *
12236 cp_parser_parameter_declaration (cp_parser *parser,
12237                                  bool template_parm_p,
12238                                  bool *parenthesized_p)
12239 {
12240   int declares_class_or_enum;
12241   bool greater_than_is_operator_p;
12242   cp_decl_specifier_seq decl_specifiers;
12243   cp_declarator *declarator;
12244   tree default_argument;
12245   cp_token *token;
12246   const char *saved_message;
12247
12248   /* In a template parameter, `>' is not an operator.
12249
12250      [temp.param]
12251
12252      When parsing a default template-argument for a non-type
12253      template-parameter, the first non-nested `>' is taken as the end
12254      of the template parameter-list rather than a greater-than
12255      operator.  */
12256   greater_than_is_operator_p = !template_parm_p;
12257
12258   /* Type definitions may not appear in parameter types.  */
12259   saved_message = parser->type_definition_forbidden_message;
12260   parser->type_definition_forbidden_message
12261     = "types may not be defined in parameter types";
12262
12263   /* Parse the declaration-specifiers.  */
12264   cp_parser_decl_specifier_seq (parser,
12265                                 CP_PARSER_FLAGS_NONE,
12266                                 &decl_specifiers,
12267                                 &declares_class_or_enum);
12268   /* If an error occurred, there's no reason to attempt to parse the
12269      rest of the declaration.  */
12270   if (cp_parser_error_occurred (parser))
12271     {
12272       parser->type_definition_forbidden_message = saved_message;
12273       return NULL;
12274     }
12275
12276   /* Peek at the next token.  */
12277   token = cp_lexer_peek_token (parser->lexer);
12278   /* If the next token is a `)', `,', `=', `>', or `...', then there
12279      is no declarator.  */
12280   if (token->type == CPP_CLOSE_PAREN
12281       || token->type == CPP_COMMA
12282       || token->type == CPP_EQ
12283       || token->type == CPP_ELLIPSIS
12284       || token->type == CPP_GREATER)
12285     {
12286       declarator = NULL;
12287       if (parenthesized_p)
12288         *parenthesized_p = false;
12289     }
12290   /* Otherwise, there should be a declarator.  */
12291   else
12292     {
12293       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12294       parser->default_arg_ok_p = false;
12295
12296       /* After seeing a decl-specifier-seq, if the next token is not a
12297          "(", there is no possibility that the code is a valid
12298          expression.  Therefore, if parsing tentatively, we commit at
12299          this point.  */
12300       if (!parser->in_template_argument_list_p
12301           /* In an expression context, having seen:
12302
12303                (int((char ...
12304
12305              we cannot be sure whether we are looking at a
12306              function-type (taking a "char" as a parameter) or a cast
12307              of some object of type "char" to "int".  */
12308           && !parser->in_type_id_in_expr_p
12309           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12310           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12311         cp_parser_commit_to_tentative_parse (parser);
12312       /* Parse the declarator.  */
12313       declarator = cp_parser_declarator (parser,
12314                                          CP_PARSER_DECLARATOR_EITHER,
12315                                          /*ctor_dtor_or_conv_p=*/NULL,
12316                                          parenthesized_p,
12317                                          /*member_p=*/false);
12318       parser->default_arg_ok_p = saved_default_arg_ok_p;
12319       /* After the declarator, allow more attributes.  */
12320       decl_specifiers.attributes
12321         = chainon (decl_specifiers.attributes,
12322                    cp_parser_attributes_opt (parser));
12323     }
12324
12325   /* The restriction on defining new types applies only to the type
12326      of the parameter, not to the default argument.  */
12327   parser->type_definition_forbidden_message = saved_message;
12328
12329   /* If the next token is `=', then process a default argument.  */
12330   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12331     {
12332       bool saved_greater_than_is_operator_p;
12333       /* Consume the `='.  */
12334       cp_lexer_consume_token (parser->lexer);
12335
12336       /* If we are defining a class, then the tokens that make up the
12337          default argument must be saved and processed later.  */
12338       if (!template_parm_p && at_class_scope_p ()
12339           && TYPE_BEING_DEFINED (current_class_type))
12340         {
12341           unsigned depth = 0;
12342           cp_token *first_token;
12343           cp_token *token;
12344
12345           /* Add tokens until we have processed the entire default
12346              argument.  We add the range [first_token, token).  */
12347           first_token = cp_lexer_peek_token (parser->lexer);
12348           while (true)
12349             {
12350               bool done = false;
12351
12352               /* Peek at the next token.  */
12353               token = cp_lexer_peek_token (parser->lexer);
12354               /* What we do depends on what token we have.  */
12355               switch (token->type)
12356                 {
12357                   /* In valid code, a default argument must be
12358                      immediately followed by a `,' `)', or `...'.  */
12359                 case CPP_COMMA:
12360                 case CPP_CLOSE_PAREN:
12361                 case CPP_ELLIPSIS:
12362                   /* If we run into a non-nested `;', `}', or `]',
12363                      then the code is invalid -- but the default
12364                      argument is certainly over.  */
12365                 case CPP_SEMICOLON:
12366                 case CPP_CLOSE_BRACE:
12367                 case CPP_CLOSE_SQUARE:
12368                   if (depth == 0)
12369                     done = true;
12370                   /* Update DEPTH, if necessary.  */
12371                   else if (token->type == CPP_CLOSE_PAREN
12372                            || token->type == CPP_CLOSE_BRACE
12373                            || token->type == CPP_CLOSE_SQUARE)
12374                     --depth;
12375                   break;
12376
12377                 case CPP_OPEN_PAREN:
12378                 case CPP_OPEN_SQUARE:
12379                 case CPP_OPEN_BRACE:
12380                   ++depth;
12381                   break;
12382
12383                 case CPP_GREATER:
12384                   /* If we see a non-nested `>', and `>' is not an
12385                      operator, then it marks the end of the default
12386                      argument.  */
12387                   if (!depth && !greater_than_is_operator_p)
12388                     done = true;
12389                   break;
12390
12391                   /* If we run out of tokens, issue an error message.  */
12392                 case CPP_EOF:
12393                 case CPP_PRAGMA_EOL:
12394                   error ("file ends in default argument");
12395                   done = true;
12396                   break;
12397
12398                 case CPP_NAME:
12399                 case CPP_SCOPE:
12400                   /* In these cases, we should look for template-ids.
12401                      For example, if the default argument is
12402                      `X<int, double>()', we need to do name lookup to
12403                      figure out whether or not `X' is a template; if
12404                      so, the `,' does not end the default argument.
12405
12406                      That is not yet done.  */
12407                   break;
12408
12409                 default:
12410                   break;
12411                 }
12412
12413               /* If we've reached the end, stop.  */
12414               if (done)
12415                 break;
12416
12417               /* Add the token to the token block.  */
12418               token = cp_lexer_consume_token (parser->lexer);
12419             }
12420
12421           /* Create a DEFAULT_ARG to represented the unparsed default
12422              argument.  */
12423           default_argument = make_node (DEFAULT_ARG);
12424           DEFARG_TOKENS (default_argument)
12425             = cp_token_cache_new (first_token, token);
12426           DEFARG_INSTANTIATIONS (default_argument) = NULL;
12427         }
12428       /* Outside of a class definition, we can just parse the
12429          assignment-expression.  */
12430       else
12431         {
12432           bool saved_local_variables_forbidden_p;
12433
12434           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12435              set correctly.  */
12436           saved_greater_than_is_operator_p
12437             = parser->greater_than_is_operator_p;
12438           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12439           /* Local variable names (and the `this' keyword) may not
12440              appear in a default argument.  */
12441           saved_local_variables_forbidden_p
12442             = parser->local_variables_forbidden_p;
12443           parser->local_variables_forbidden_p = true;
12444           /* The default argument expression may cause implicitly
12445              defined member functions to be synthesized, which will
12446              result in garbage collection.  We must treat this
12447              situation as if we were within the body of function so as
12448              to avoid collecting live data on the stack.  */
12449           ++function_depth;
12450           /* Parse the assignment-expression.  */
12451           if (template_parm_p)
12452             push_deferring_access_checks (dk_no_deferred);
12453           default_argument
12454             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12455           if (template_parm_p)
12456             pop_deferring_access_checks ();
12457           /* Restore saved state.  */
12458           --function_depth;
12459           parser->greater_than_is_operator_p
12460             = saved_greater_than_is_operator_p;
12461           parser->local_variables_forbidden_p
12462             = saved_local_variables_forbidden_p;
12463         }
12464       if (!parser->default_arg_ok_p)
12465         {
12466           if (!flag_pedantic_errors)
12467             warning (0, "deprecated use of default argument for parameter of non-function");
12468           else
12469             {
12470               error ("default arguments are only permitted for function parameters");
12471               default_argument = NULL_TREE;
12472             }
12473         }
12474     }
12475   else
12476     default_argument = NULL_TREE;
12477
12478   return make_parameter_declarator (&decl_specifiers,
12479                                     declarator,
12480                                     default_argument);
12481 }
12482
12483 /* Parse a function-body.
12484
12485    function-body:
12486      compound_statement  */
12487
12488 static void
12489 cp_parser_function_body (cp_parser *parser)
12490 {
12491   cp_parser_compound_statement (parser, NULL, false);
12492 }
12493
12494 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12495    true if a ctor-initializer was present.  */
12496
12497 static bool
12498 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12499 {
12500   tree body;
12501   bool ctor_initializer_p;
12502
12503   /* Begin the function body.  */
12504   body = begin_function_body ();
12505   /* Parse the optional ctor-initializer.  */
12506   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12507   /* Parse the function-body.  */
12508   cp_parser_function_body (parser);
12509   /* Finish the function body.  */
12510   finish_function_body (body);
12511
12512   return ctor_initializer_p;
12513 }
12514
12515 /* Parse an initializer.
12516
12517    initializer:
12518      = initializer-clause
12519      ( expression-list )
12520
12521    Returns an expression representing the initializer.  If no
12522    initializer is present, NULL_TREE is returned.
12523
12524    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12525    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12526    set to FALSE if there is no initializer present.  If there is an
12527    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12528    is set to true; otherwise it is set to false.  */
12529
12530 static tree
12531 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12532                        bool* non_constant_p)
12533 {
12534   cp_token *token;
12535   tree init;
12536
12537   /* Peek at the next token.  */
12538   token = cp_lexer_peek_token (parser->lexer);
12539
12540   /* Let our caller know whether or not this initializer was
12541      parenthesized.  */
12542   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12543   /* Assume that the initializer is constant.  */
12544   *non_constant_p = false;
12545
12546   if (token->type == CPP_EQ)
12547     {
12548       /* Consume the `='.  */
12549       cp_lexer_consume_token (parser->lexer);
12550       /* Parse the initializer-clause.  */
12551       init = cp_parser_initializer_clause (parser, non_constant_p);
12552     }
12553   else if (token->type == CPP_OPEN_PAREN)
12554     init = cp_parser_parenthesized_expression_list (parser, false,
12555                                                     /*cast_p=*/false,
12556                                                     non_constant_p);
12557   else
12558     {
12559       /* Anything else is an error.  */
12560       cp_parser_error (parser, "expected initializer");
12561       init = error_mark_node;
12562     }
12563
12564   return init;
12565 }
12566
12567 /* Parse an initializer-clause.
12568
12569    initializer-clause:
12570      assignment-expression
12571      { initializer-list , [opt] }
12572      { }
12573
12574    Returns an expression representing the initializer.
12575
12576    If the `assignment-expression' production is used the value
12577    returned is simply a representation for the expression.
12578
12579    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12580    the elements of the initializer-list (or NULL, if the last
12581    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12582    NULL_TREE.  There is no way to detect whether or not the optional
12583    trailing `,' was provided.  NON_CONSTANT_P is as for
12584    cp_parser_initializer.  */
12585
12586 static tree
12587 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12588 {
12589   tree initializer;
12590
12591   /* Assume the expression is constant.  */
12592   *non_constant_p = false;
12593
12594   /* If it is not a `{', then we are looking at an
12595      assignment-expression.  */
12596   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12597     {
12598       initializer
12599         = cp_parser_constant_expression (parser,
12600                                         /*allow_non_constant_p=*/true,
12601                                         non_constant_p);
12602       if (!*non_constant_p)
12603         initializer = fold_non_dependent_expr (initializer);
12604     }
12605   else
12606     {
12607       /* Consume the `{' token.  */
12608       cp_lexer_consume_token (parser->lexer);
12609       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12610       initializer = make_node (CONSTRUCTOR);
12611       /* If it's not a `}', then there is a non-trivial initializer.  */
12612       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12613         {
12614           /* Parse the initializer list.  */
12615           CONSTRUCTOR_ELTS (initializer)
12616             = cp_parser_initializer_list (parser, non_constant_p);
12617           /* A trailing `,' token is allowed.  */
12618           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12619             cp_lexer_consume_token (parser->lexer);
12620         }
12621       /* Now, there should be a trailing `}'.  */
12622       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12623     }
12624
12625   return initializer;
12626 }
12627
12628 /* Parse an initializer-list.
12629
12630    initializer-list:
12631      initializer-clause
12632      initializer-list , initializer-clause
12633
12634    GNU Extension:
12635
12636    initializer-list:
12637      identifier : initializer-clause
12638      initializer-list, identifier : initializer-clause
12639
12640    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12641    for the initializer.  If the INDEX of the elt is non-NULL, it is the
12642    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12643    as for cp_parser_initializer.  */
12644
12645 static VEC(constructor_elt,gc) *
12646 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12647 {
12648   VEC(constructor_elt,gc) *v = NULL;
12649
12650   /* Assume all of the expressions are constant.  */
12651   *non_constant_p = false;
12652
12653   /* Parse the rest of the list.  */
12654   while (true)
12655     {
12656       cp_token *token;
12657       tree identifier;
12658       tree initializer;
12659       bool clause_non_constant_p;
12660
12661       /* If the next token is an identifier and the following one is a
12662          colon, we are looking at the GNU designated-initializer
12663          syntax.  */
12664       if (cp_parser_allow_gnu_extensions_p (parser)
12665           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12666           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12667         {
12668           /* Consume the identifier.  */
12669           identifier = cp_lexer_consume_token (parser->lexer)->value;
12670           /* Consume the `:'.  */
12671           cp_lexer_consume_token (parser->lexer);
12672         }
12673       else
12674         identifier = NULL_TREE;
12675
12676       /* Parse the initializer.  */
12677       initializer = cp_parser_initializer_clause (parser,
12678                                                   &clause_non_constant_p);
12679       /* If any clause is non-constant, so is the entire initializer.  */
12680       if (clause_non_constant_p)
12681         *non_constant_p = true;
12682
12683       /* Add it to the vector.  */
12684       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12685
12686       /* If the next token is not a comma, we have reached the end of
12687          the list.  */
12688       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12689         break;
12690
12691       /* Peek at the next token.  */
12692       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12693       /* If the next token is a `}', then we're still done.  An
12694          initializer-clause can have a trailing `,' after the
12695          initializer-list and before the closing `}'.  */
12696       if (token->type == CPP_CLOSE_BRACE)
12697         break;
12698
12699       /* Consume the `,' token.  */
12700       cp_lexer_consume_token (parser->lexer);
12701     }
12702
12703   return v;
12704 }
12705
12706 /* Classes [gram.class] */
12707
12708 /* Parse a class-name.
12709
12710    class-name:
12711      identifier
12712      template-id
12713
12714    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12715    to indicate that names looked up in dependent types should be
12716    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12717    keyword has been used to indicate that the name that appears next
12718    is a template.  TAG_TYPE indicates the explicit tag given before
12719    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12720    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12721    is the class being defined in a class-head.
12722
12723    Returns the TYPE_DECL representing the class.  */
12724
12725 static tree
12726 cp_parser_class_name (cp_parser *parser,
12727                       bool typename_keyword_p,
12728                       bool template_keyword_p,
12729                       enum tag_types tag_type,
12730                       bool check_dependency_p,
12731                       bool class_head_p,
12732                       bool is_declaration)
12733 {
12734   tree decl;
12735   tree scope;
12736   bool typename_p;
12737   cp_token *token;
12738
12739   /* All class-names start with an identifier.  */
12740   token = cp_lexer_peek_token (parser->lexer);
12741   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12742     {
12743       cp_parser_error (parser, "expected class-name");
12744       return error_mark_node;
12745     }
12746
12747   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12748      to a template-id, so we save it here.  */
12749   scope = parser->scope;
12750   if (scope == error_mark_node)
12751     return error_mark_node;
12752
12753   /* Any name names a type if we're following the `typename' keyword
12754      in a qualified name where the enclosing scope is type-dependent.  */
12755   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12756                 && dependent_type_p (scope));
12757   /* Handle the common case (an identifier, but not a template-id)
12758      efficiently.  */
12759   if (token->type == CPP_NAME
12760       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12761     {
12762       cp_token *identifier_token;
12763       tree identifier;
12764       bool ambiguous_p;
12765
12766       /* Look for the identifier.  */
12767       identifier_token = cp_lexer_peek_token (parser->lexer);
12768       ambiguous_p = identifier_token->ambiguous_p;
12769       identifier = cp_parser_identifier (parser);
12770       /* If the next token isn't an identifier, we are certainly not
12771          looking at a class-name.  */
12772       if (identifier == error_mark_node)
12773         decl = error_mark_node;
12774       /* If we know this is a type-name, there's no need to look it
12775          up.  */
12776       else if (typename_p)
12777         decl = identifier;
12778       else
12779         {
12780           tree ambiguous_decls;
12781           /* If we already know that this lookup is ambiguous, then
12782              we've already issued an error message; there's no reason
12783              to check again.  */
12784           if (ambiguous_p)
12785             {
12786               cp_parser_simulate_error (parser);
12787               return error_mark_node;
12788             }
12789           /* If the next token is a `::', then the name must be a type
12790              name.
12791
12792              [basic.lookup.qual]
12793
12794              During the lookup for a name preceding the :: scope
12795              resolution operator, object, function, and enumerator
12796              names are ignored.  */
12797           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12798             tag_type = typename_type;
12799           /* Look up the name.  */
12800           decl = cp_parser_lookup_name (parser, identifier,
12801                                         tag_type,
12802                                         /*is_template=*/false,
12803                                         /*is_namespace=*/false,
12804                                         check_dependency_p,
12805                                         &ambiguous_decls);
12806           if (ambiguous_decls)
12807             {
12808               error ("reference to %qD is ambiguous", identifier);
12809               print_candidates (ambiguous_decls);
12810               if (cp_parser_parsing_tentatively (parser))
12811                 {
12812                   identifier_token->ambiguous_p = true;
12813                   cp_parser_simulate_error (parser);
12814                 }
12815               return error_mark_node;
12816             }
12817         }
12818     }
12819   else
12820     {
12821       /* Try a template-id.  */
12822       decl = cp_parser_template_id (parser, template_keyword_p,
12823                                     check_dependency_p,
12824                                     is_declaration);
12825       if (decl == error_mark_node)
12826         return error_mark_node;
12827     }
12828
12829   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12830
12831   /* If this is a typename, create a TYPENAME_TYPE.  */
12832   if (typename_p && decl != error_mark_node)
12833     {
12834       decl = make_typename_type (scope, decl, typename_type,
12835                                  /*complain=*/tf_error);
12836       if (decl != error_mark_node)
12837         decl = TYPE_NAME (decl);
12838     }
12839
12840   /* Check to see that it is really the name of a class.  */
12841   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12842       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12843       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12844     /* Situations like this:
12845
12846          template <typename T> struct A {
12847            typename T::template X<int>::I i;
12848          };
12849
12850        are problematic.  Is `T::template X<int>' a class-name?  The
12851        standard does not seem to be definitive, but there is no other
12852        valid interpretation of the following `::'.  Therefore, those
12853        names are considered class-names.  */
12854     {
12855       decl = make_typename_type (scope, decl, tag_type, tf_error);
12856       if (decl != error_mark_node)
12857         decl = TYPE_NAME (decl);
12858     }
12859   else if (TREE_CODE (decl) != TYPE_DECL
12860            || TREE_TYPE (decl) == error_mark_node
12861            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12862     decl = error_mark_node;
12863
12864   if (decl == error_mark_node)
12865     cp_parser_error (parser, "expected class-name");
12866
12867   return decl;
12868 }
12869
12870 /* Parse a class-specifier.
12871
12872    class-specifier:
12873      class-head { member-specification [opt] }
12874
12875    Returns the TREE_TYPE representing the class.  */
12876
12877 static tree
12878 cp_parser_class_specifier (cp_parser* parser)
12879 {
12880   cp_token *token;
12881   tree type;
12882   tree attributes = NULL_TREE;
12883   int has_trailing_semicolon;
12884   bool nested_name_specifier_p;
12885   unsigned saved_num_template_parameter_lists;
12886   tree old_scope = NULL_TREE;
12887   tree scope = NULL_TREE;
12888
12889   push_deferring_access_checks (dk_no_deferred);
12890
12891   /* Parse the class-head.  */
12892   type = cp_parser_class_head (parser,
12893                                &nested_name_specifier_p,
12894                                &attributes);
12895   /* If the class-head was a semantic disaster, skip the entire body
12896      of the class.  */
12897   if (!type)
12898     {
12899       cp_parser_skip_to_end_of_block_or_statement (parser);
12900       pop_deferring_access_checks ();
12901       return error_mark_node;
12902     }
12903
12904   /* Look for the `{'.  */
12905   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12906     {
12907       pop_deferring_access_checks ();
12908       return error_mark_node;
12909     }
12910
12911   /* Issue an error message if type-definitions are forbidden here.  */
12912   cp_parser_check_type_definition (parser);
12913   /* Remember that we are defining one more class.  */
12914   ++parser->num_classes_being_defined;
12915   /* Inside the class, surrounding template-parameter-lists do not
12916      apply.  */
12917   saved_num_template_parameter_lists
12918     = parser->num_template_parameter_lists;
12919   parser->num_template_parameter_lists = 0;
12920
12921   /* Start the class.  */
12922   if (nested_name_specifier_p)
12923     {
12924       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12925       old_scope = push_inner_scope (scope);
12926     }
12927   type = begin_class_definition (type, attributes);
12928
12929   if (type == error_mark_node)
12930     /* If the type is erroneous, skip the entire body of the class.  */
12931     cp_parser_skip_to_closing_brace (parser);
12932   else
12933     /* Parse the member-specification.  */
12934     cp_parser_member_specification_opt (parser);
12935
12936   /* Look for the trailing `}'.  */
12937   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12938   /* We get better error messages by noticing a common problem: a
12939      missing trailing `;'.  */
12940   token = cp_lexer_peek_token (parser->lexer);
12941   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12942   /* Look for trailing attributes to apply to this class.  */
12943   if (cp_parser_allow_gnu_extensions_p (parser))
12944     attributes = cp_parser_attributes_opt (parser);
12945   if (type != error_mark_node)
12946     type = finish_struct (type, attributes);
12947   if (nested_name_specifier_p)
12948     pop_inner_scope (old_scope, scope);
12949   /* If this class is not itself within the scope of another class,
12950      then we need to parse the bodies of all of the queued function
12951      definitions.  Note that the queued functions defined in a class
12952      are not always processed immediately following the
12953      class-specifier for that class.  Consider:
12954
12955        struct A {
12956          struct B { void f() { sizeof (A); } };
12957        };
12958
12959      If `f' were processed before the processing of `A' were
12960      completed, there would be no way to compute the size of `A'.
12961      Note that the nesting we are interested in here is lexical --
12962      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12963      for:
12964
12965        struct A { struct B; };
12966        struct A::B { void f() { } };
12967
12968      there is no need to delay the parsing of `A::B::f'.  */
12969   if (--parser->num_classes_being_defined == 0)
12970     {
12971       tree queue_entry;
12972       tree fn;
12973       tree class_type = NULL_TREE;
12974       tree pushed_scope = NULL_TREE;
12975
12976       /* In a first pass, parse default arguments to the functions.
12977          Then, in a second pass, parse the bodies of the functions.
12978          This two-phased approach handles cases like:
12979
12980             struct S {
12981               void f() { g(); }
12982               void g(int i = 3);
12983             };
12984
12985          */
12986       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12987              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12988            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12989            TREE_PURPOSE (parser->unparsed_functions_queues)
12990              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12991         {
12992           fn = TREE_VALUE (queue_entry);
12993           /* If there are default arguments that have not yet been processed,
12994              take care of them now.  */
12995           if (class_type != TREE_PURPOSE (queue_entry))
12996             {
12997               if (pushed_scope)
12998                 pop_scope (pushed_scope);
12999               class_type = TREE_PURPOSE (queue_entry);
13000               pushed_scope = push_scope (class_type);
13001             }
13002           /* Make sure that any template parameters are in scope.  */
13003           maybe_begin_member_template_processing (fn);
13004           /* Parse the default argument expressions.  */
13005           cp_parser_late_parsing_default_args (parser, fn);
13006           /* Remove any template parameters from the symbol table.  */
13007           maybe_end_member_template_processing ();
13008         }
13009       if (pushed_scope)
13010         pop_scope (pushed_scope);
13011       /* Now parse the body of the functions.  */
13012       for (TREE_VALUE (parser->unparsed_functions_queues)
13013              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13014            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13015            TREE_VALUE (parser->unparsed_functions_queues)
13016              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13017         {
13018           /* Figure out which function we need to process.  */
13019           fn = TREE_VALUE (queue_entry);
13020           /* Parse the function.  */
13021           cp_parser_late_parsing_for_member (parser, fn);
13022         }
13023     }
13024
13025   /* Put back any saved access checks.  */
13026   pop_deferring_access_checks ();
13027
13028   /* Restore the count of active template-parameter-lists.  */
13029   parser->num_template_parameter_lists
13030     = saved_num_template_parameter_lists;
13031
13032   return type;
13033 }
13034
13035 /* Parse a class-head.
13036
13037    class-head:
13038      class-key identifier [opt] base-clause [opt]
13039      class-key nested-name-specifier identifier base-clause [opt]
13040      class-key nested-name-specifier [opt] template-id
13041        base-clause [opt]
13042
13043    GNU Extensions:
13044      class-key attributes identifier [opt] base-clause [opt]
13045      class-key attributes nested-name-specifier identifier base-clause [opt]
13046      class-key attributes nested-name-specifier [opt] template-id
13047        base-clause [opt]
13048
13049    Returns the TYPE of the indicated class.  Sets
13050    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13051    involving a nested-name-specifier was used, and FALSE otherwise.
13052
13053    Returns error_mark_node if this is not a class-head.
13054
13055    Returns NULL_TREE if the class-head is syntactically valid, but
13056    semantically invalid in a way that means we should skip the entire
13057    body of the class.  */
13058
13059 static tree
13060 cp_parser_class_head (cp_parser* parser,
13061                       bool* nested_name_specifier_p,
13062                       tree *attributes_p)
13063 {
13064   tree nested_name_specifier;
13065   enum tag_types class_key;
13066   tree id = NULL_TREE;
13067   tree type = NULL_TREE;
13068   tree attributes;
13069   bool template_id_p = false;
13070   bool qualified_p = false;
13071   bool invalid_nested_name_p = false;
13072   bool invalid_explicit_specialization_p = false;
13073   tree pushed_scope = NULL_TREE;
13074   unsigned num_templates;
13075   tree bases;
13076
13077   /* Assume no nested-name-specifier will be present.  */
13078   *nested_name_specifier_p = false;
13079   /* Assume no template parameter lists will be used in defining the
13080      type.  */
13081   num_templates = 0;
13082
13083   /* Look for the class-key.  */
13084   class_key = cp_parser_class_key (parser);
13085   if (class_key == none_type)
13086     return error_mark_node;
13087
13088   /* Parse the attributes.  */
13089   attributes = cp_parser_attributes_opt (parser);
13090
13091   /* If the next token is `::', that is invalid -- but sometimes
13092      people do try to write:
13093
13094        struct ::S {};
13095
13096      Handle this gracefully by accepting the extra qualifier, and then
13097      issuing an error about it later if this really is a
13098      class-head.  If it turns out just to be an elaborated type
13099      specifier, remain silent.  */
13100   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13101     qualified_p = true;
13102
13103   push_deferring_access_checks (dk_no_check);
13104
13105   /* Determine the name of the class.  Begin by looking for an
13106      optional nested-name-specifier.  */
13107   nested_name_specifier
13108     = cp_parser_nested_name_specifier_opt (parser,
13109                                            /*typename_keyword_p=*/false,
13110                                            /*check_dependency_p=*/false,
13111                                            /*type_p=*/false,
13112                                            /*is_declaration=*/false);
13113   /* If there was a nested-name-specifier, then there *must* be an
13114      identifier.  */
13115   if (nested_name_specifier)
13116     {
13117       /* Although the grammar says `identifier', it really means
13118          `class-name' or `template-name'.  You are only allowed to
13119          define a class that has already been declared with this
13120          syntax.
13121
13122          The proposed resolution for Core Issue 180 says that wherever
13123          you see `class T::X' you should treat `X' as a type-name.
13124
13125          It is OK to define an inaccessible class; for example:
13126
13127            class A { class B; };
13128            class A::B {};
13129
13130          We do not know if we will see a class-name, or a
13131          template-name.  We look for a class-name first, in case the
13132          class-name is a template-id; if we looked for the
13133          template-name first we would stop after the template-name.  */
13134       cp_parser_parse_tentatively (parser);
13135       type = cp_parser_class_name (parser,
13136                                    /*typename_keyword_p=*/false,
13137                                    /*template_keyword_p=*/false,
13138                                    class_type,
13139                                    /*check_dependency_p=*/false,
13140                                    /*class_head_p=*/true,
13141                                    /*is_declaration=*/false);
13142       /* If that didn't work, ignore the nested-name-specifier.  */
13143       if (!cp_parser_parse_definitely (parser))
13144         {
13145           invalid_nested_name_p = true;
13146           id = cp_parser_identifier (parser);
13147           if (id == error_mark_node)
13148             id = NULL_TREE;
13149         }
13150       /* If we could not find a corresponding TYPE, treat this
13151          declaration like an unqualified declaration.  */
13152       if (type == error_mark_node)
13153         nested_name_specifier = NULL_TREE;
13154       /* Otherwise, count the number of templates used in TYPE and its
13155          containing scopes.  */
13156       else
13157         {
13158           tree scope;
13159
13160           for (scope = TREE_TYPE (type);
13161                scope && TREE_CODE (scope) != NAMESPACE_DECL;
13162                scope = (TYPE_P (scope)
13163                         ? TYPE_CONTEXT (scope)
13164                         : DECL_CONTEXT (scope)))
13165             if (TYPE_P (scope)
13166                 && CLASS_TYPE_P (scope)
13167                 && CLASSTYPE_TEMPLATE_INFO (scope)
13168                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13169                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13170               ++num_templates;
13171         }
13172     }
13173   /* Otherwise, the identifier is optional.  */
13174   else
13175     {
13176       /* We don't know whether what comes next is a template-id,
13177          an identifier, or nothing at all.  */
13178       cp_parser_parse_tentatively (parser);
13179       /* Check for a template-id.  */
13180       id = cp_parser_template_id (parser,
13181                                   /*template_keyword_p=*/false,
13182                                   /*check_dependency_p=*/true,
13183                                   /*is_declaration=*/true);
13184       /* If that didn't work, it could still be an identifier.  */
13185       if (!cp_parser_parse_definitely (parser))
13186         {
13187           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13188             id = cp_parser_identifier (parser);
13189           else
13190             id = NULL_TREE;
13191         }
13192       else
13193         {
13194           template_id_p = true;
13195           ++num_templates;
13196         }
13197     }
13198
13199   pop_deferring_access_checks ();
13200
13201   if (id)
13202     cp_parser_check_for_invalid_template_id (parser, id);
13203
13204   /* If it's not a `:' or a `{' then we can't really be looking at a
13205      class-head, since a class-head only appears as part of a
13206      class-specifier.  We have to detect this situation before calling
13207      xref_tag, since that has irreversible side-effects.  */
13208   if (!cp_parser_next_token_starts_class_definition_p (parser))
13209     {
13210       cp_parser_error (parser, "expected %<{%> or %<:%>");
13211       return error_mark_node;
13212     }
13213
13214   /* At this point, we're going ahead with the class-specifier, even
13215      if some other problem occurs.  */
13216   cp_parser_commit_to_tentative_parse (parser);
13217   /* Issue the error about the overly-qualified name now.  */
13218   if (qualified_p)
13219     cp_parser_error (parser,
13220                      "global qualification of class name is invalid");
13221   else if (invalid_nested_name_p)
13222     cp_parser_error (parser,
13223                      "qualified name does not name a class");
13224   else if (nested_name_specifier)
13225     {
13226       tree scope;
13227
13228       /* Reject typedef-names in class heads.  */
13229       if (!DECL_IMPLICIT_TYPEDEF_P (type))
13230         {
13231           error ("invalid class name in declaration of %qD", type);
13232           type = NULL_TREE;
13233           goto done;
13234         }
13235
13236       /* Figure out in what scope the declaration is being placed.  */
13237       scope = current_scope ();
13238       /* If that scope does not contain the scope in which the
13239          class was originally declared, the program is invalid.  */
13240       if (scope && !is_ancestor (scope, nested_name_specifier))
13241         {
13242           error ("declaration of %qD in %qD which does not enclose %qD",
13243                  type, scope, nested_name_specifier);
13244           type = NULL_TREE;
13245           goto done;
13246         }
13247       /* [dcl.meaning]
13248
13249          A declarator-id shall not be qualified exception of the
13250          definition of a ... nested class outside of its class
13251          ... [or] a the definition or explicit instantiation of a
13252          class member of a namespace outside of its namespace.  */
13253       if (scope == nested_name_specifier)
13254         {
13255           pedwarn ("extra qualification ignored");
13256           nested_name_specifier = NULL_TREE;
13257           num_templates = 0;
13258         }
13259     }
13260   /* An explicit-specialization must be preceded by "template <>".  If
13261      it is not, try to recover gracefully.  */
13262   if (at_namespace_scope_p ()
13263       && parser->num_template_parameter_lists == 0
13264       && template_id_p)
13265     {
13266       error ("an explicit specialization must be preceded by %<template <>%>");
13267       invalid_explicit_specialization_p = true;
13268       /* Take the same action that would have been taken by
13269          cp_parser_explicit_specialization.  */
13270       ++parser->num_template_parameter_lists;
13271       begin_specialization ();
13272     }
13273   /* There must be no "return" statements between this point and the
13274      end of this function; set "type "to the correct return value and
13275      use "goto done;" to return.  */
13276   /* Make sure that the right number of template parameters were
13277      present.  */
13278   if (!cp_parser_check_template_parameters (parser, num_templates))
13279     {
13280       /* If something went wrong, there is no point in even trying to
13281          process the class-definition.  */
13282       type = NULL_TREE;
13283       goto done;
13284     }
13285
13286   /* Look up the type.  */
13287   if (template_id_p)
13288     {
13289       type = TREE_TYPE (id);
13290       maybe_process_partial_specialization (type);
13291       if (nested_name_specifier)
13292         pushed_scope = push_scope (nested_name_specifier);
13293     }
13294   else if (nested_name_specifier)
13295     {
13296       tree class_type;
13297
13298       /* Given:
13299
13300             template <typename T> struct S { struct T };
13301             template <typename T> struct S<T>::T { };
13302
13303          we will get a TYPENAME_TYPE when processing the definition of
13304          `S::T'.  We need to resolve it to the actual type before we
13305          try to define it.  */
13306       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13307         {
13308           class_type = resolve_typename_type (TREE_TYPE (type),
13309                                               /*only_current_p=*/false);
13310           if (class_type != error_mark_node)
13311             type = TYPE_NAME (class_type);
13312           else
13313             {
13314               cp_parser_error (parser, "could not resolve typename type");
13315               type = error_mark_node;
13316             }
13317         }
13318
13319       maybe_process_partial_specialization (TREE_TYPE (type));
13320       class_type = current_class_type;
13321       /* Enter the scope indicated by the nested-name-specifier.  */
13322       pushed_scope = push_scope (nested_name_specifier);
13323       /* Get the canonical version of this type.  */
13324       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13325       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13326           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13327         {
13328           type = push_template_decl (type);
13329           if (type == error_mark_node)
13330             {
13331               type = NULL_TREE;
13332               goto done;
13333             }
13334         }
13335
13336       type = TREE_TYPE (type);
13337       *nested_name_specifier_p = true;
13338     }
13339   else      /* The name is not a nested name.  */
13340     {
13341       /* If the class was unnamed, create a dummy name.  */
13342       if (!id)
13343         id = make_anon_name ();
13344       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13345                        parser->num_template_parameter_lists);
13346     }
13347
13348   /* Indicate whether this class was declared as a `class' or as a
13349      `struct'.  */
13350   if (TREE_CODE (type) == RECORD_TYPE)
13351     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13352   cp_parser_check_class_key (class_key, type);
13353
13354   /* If this type was already complete, and we see another definition,
13355      that's an error.  */
13356   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13357     {
13358       error ("redefinition of %q#T", type);
13359       error ("previous definition of %q+#T", type);
13360       type = NULL_TREE;
13361       goto done;
13362     }
13363
13364   /* We will have entered the scope containing the class; the names of
13365      base classes should be looked up in that context.  For example:
13366
13367        struct A { struct B {}; struct C; };
13368        struct A::C : B {};
13369
13370      is valid.  */
13371   bases = NULL_TREE;
13372
13373   /* Get the list of base-classes, if there is one.  */
13374   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13375     bases = cp_parser_base_clause (parser);
13376
13377   /* Process the base classes.  */
13378   xref_basetypes (type, bases);
13379
13380  done:
13381   /* Leave the scope given by the nested-name-specifier.  We will
13382      enter the class scope itself while processing the members.  */
13383   if (pushed_scope)
13384     pop_scope (pushed_scope);
13385
13386   if (invalid_explicit_specialization_p)
13387     {
13388       end_specialization ();
13389       --parser->num_template_parameter_lists;
13390     }
13391   *attributes_p = attributes;
13392   return type;
13393 }
13394
13395 /* Parse a class-key.
13396
13397    class-key:
13398      class
13399      struct
13400      union
13401
13402    Returns the kind of class-key specified, or none_type to indicate
13403    error.  */
13404
13405 static enum tag_types
13406 cp_parser_class_key (cp_parser* parser)
13407 {
13408   cp_token *token;
13409   enum tag_types tag_type;
13410
13411   /* Look for the class-key.  */
13412   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13413   if (!token)
13414     return none_type;
13415
13416   /* Check to see if the TOKEN is a class-key.  */
13417   tag_type = cp_parser_token_is_class_key (token);
13418   if (!tag_type)
13419     cp_parser_error (parser, "expected class-key");
13420   return tag_type;
13421 }
13422
13423 /* Parse an (optional) member-specification.
13424
13425    member-specification:
13426      member-declaration member-specification [opt]
13427      access-specifier : member-specification [opt]  */
13428
13429 static void
13430 cp_parser_member_specification_opt (cp_parser* parser)
13431 {
13432   while (true)
13433     {
13434       cp_token *token;
13435       enum rid keyword;
13436
13437       /* Peek at the next token.  */
13438       token = cp_lexer_peek_token (parser->lexer);
13439       /* If it's a `}', or EOF then we've seen all the members.  */
13440       if (token->type == CPP_CLOSE_BRACE
13441           || token->type == CPP_EOF
13442           || token->type == CPP_PRAGMA_EOL)
13443         break;
13444
13445       /* See if this token is a keyword.  */
13446       keyword = token->keyword;
13447       switch (keyword)
13448         {
13449         case RID_PUBLIC:
13450         case RID_PROTECTED:
13451         case RID_PRIVATE:
13452           /* Consume the access-specifier.  */
13453           cp_lexer_consume_token (parser->lexer);
13454           /* Remember which access-specifier is active.  */
13455           current_access_specifier = token->value;
13456           /* Look for the `:'.  */
13457           cp_parser_require (parser, CPP_COLON, "`:'");
13458           break;
13459
13460         default:
13461           /* Accept #pragmas at class scope.  */
13462           if (token->type == CPP_PRAGMA)
13463             {
13464               cp_parser_pragma (parser, pragma_external);
13465               break;
13466             }
13467
13468           /* Otherwise, the next construction must be a
13469              member-declaration.  */
13470           cp_parser_member_declaration (parser);
13471         }
13472     }
13473 }
13474
13475 /* Parse a member-declaration.
13476
13477    member-declaration:
13478      decl-specifier-seq [opt] member-declarator-list [opt] ;
13479      function-definition ; [opt]
13480      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13481      using-declaration
13482      template-declaration
13483
13484    member-declarator-list:
13485      member-declarator
13486      member-declarator-list , member-declarator
13487
13488    member-declarator:
13489      declarator pure-specifier [opt]
13490      declarator constant-initializer [opt]
13491      identifier [opt] : constant-expression
13492
13493    GNU Extensions:
13494
13495    member-declaration:
13496      __extension__ member-declaration
13497
13498    member-declarator:
13499      declarator attributes [opt] pure-specifier [opt]
13500      declarator attributes [opt] constant-initializer [opt]
13501      identifier [opt] attributes [opt] : constant-expression  */
13502
13503 static void
13504 cp_parser_member_declaration (cp_parser* parser)
13505 {
13506   cp_decl_specifier_seq decl_specifiers;
13507   tree prefix_attributes;
13508   tree decl;
13509   int declares_class_or_enum;
13510   bool friend_p;
13511   cp_token *token;
13512   int saved_pedantic;
13513
13514   /* Check for the `__extension__' keyword.  */
13515   if (cp_parser_extension_opt (parser, &saved_pedantic))
13516     {
13517       /* Recurse.  */
13518       cp_parser_member_declaration (parser);
13519       /* Restore the old value of the PEDANTIC flag.  */
13520       pedantic = saved_pedantic;
13521
13522       return;
13523     }
13524
13525   /* Check for a template-declaration.  */
13526   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13527     {
13528       /* An explicit specialization here is an error condition, and we
13529          expect the specialization handler to detect and report this.  */
13530       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13531           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13532         cp_parser_explicit_specialization (parser);
13533       else
13534         cp_parser_template_declaration (parser, /*member_p=*/true);
13535
13536       return;
13537     }
13538
13539   /* Check for a using-declaration.  */
13540   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13541     {
13542       /* Parse the using-declaration.  */
13543       cp_parser_using_declaration (parser);
13544
13545       return;
13546     }
13547
13548   /* Check for @defs.  */
13549   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13550     {
13551       tree ivar, member;
13552       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13553       ivar = ivar_chains;
13554       while (ivar)
13555         {
13556           member = ivar;
13557           ivar = TREE_CHAIN (member);
13558           TREE_CHAIN (member) = NULL_TREE;
13559           finish_member_declaration (member);
13560         }
13561       return;
13562     }
13563
13564   /* Parse the decl-specifier-seq.  */
13565   cp_parser_decl_specifier_seq (parser,
13566                                 CP_PARSER_FLAGS_OPTIONAL,
13567                                 &decl_specifiers,
13568                                 &declares_class_or_enum);
13569   prefix_attributes = decl_specifiers.attributes;
13570   decl_specifiers.attributes = NULL_TREE;
13571   /* Check for an invalid type-name.  */
13572   if (!decl_specifiers.type
13573       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13574     return;
13575   /* If there is no declarator, then the decl-specifier-seq should
13576      specify a type.  */
13577   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13578     {
13579       /* If there was no decl-specifier-seq, and the next token is a
13580          `;', then we have something like:
13581
13582            struct S { ; };
13583
13584          [class.mem]
13585
13586          Each member-declaration shall declare at least one member
13587          name of the class.  */
13588       if (!decl_specifiers.any_specifiers_p)
13589         {
13590           cp_token *token = cp_lexer_peek_token (parser->lexer);
13591           if (pedantic && !token->in_system_header)
13592             pedwarn ("%Hextra %<;%>", &token->location);
13593         }
13594       else
13595         {
13596           tree type;
13597
13598           /* See if this declaration is a friend.  */
13599           friend_p = cp_parser_friend_p (&decl_specifiers);
13600           /* If there were decl-specifiers, check to see if there was
13601              a class-declaration.  */
13602           type = check_tag_decl (&decl_specifiers);
13603           /* Nested classes have already been added to the class, but
13604              a `friend' needs to be explicitly registered.  */
13605           if (friend_p)
13606             {
13607               /* If the `friend' keyword was present, the friend must
13608                  be introduced with a class-key.  */
13609                if (!declares_class_or_enum)
13610                  error ("a class-key must be used when declaring a friend");
13611                /* In this case:
13612
13613                     template <typename T> struct A {
13614                       friend struct A<T>::B;
13615                     };
13616
13617                   A<T>::B will be represented by a TYPENAME_TYPE, and
13618                   therefore not recognized by check_tag_decl.  */
13619                if (!type
13620                    && decl_specifiers.type
13621                    && TYPE_P (decl_specifiers.type))
13622                  type = decl_specifiers.type;
13623                if (!type || !TYPE_P (type))
13624                  error ("friend declaration does not name a class or "
13625                         "function");
13626                else
13627                  make_friend_class (current_class_type, type,
13628                                     /*complain=*/true);
13629             }
13630           /* If there is no TYPE, an error message will already have
13631              been issued.  */
13632           else if (!type || type == error_mark_node)
13633             ;
13634           /* An anonymous aggregate has to be handled specially; such
13635              a declaration really declares a data member (with a
13636              particular type), as opposed to a nested class.  */
13637           else if (ANON_AGGR_TYPE_P (type))
13638             {
13639               /* Remove constructors and such from TYPE, now that we
13640                  know it is an anonymous aggregate.  */
13641               fixup_anonymous_aggr (type);
13642               /* And make the corresponding data member.  */
13643               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13644               /* Add it to the class.  */
13645               finish_member_declaration (decl);
13646             }
13647           else
13648             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13649         }
13650     }
13651   else
13652     {
13653       /* See if these declarations will be friends.  */
13654       friend_p = cp_parser_friend_p (&decl_specifiers);
13655
13656       /* Keep going until we hit the `;' at the end of the
13657          declaration.  */
13658       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13659         {
13660           tree attributes = NULL_TREE;
13661           tree first_attribute;
13662
13663           /* Peek at the next token.  */
13664           token = cp_lexer_peek_token (parser->lexer);
13665
13666           /* Check for a bitfield declaration.  */
13667           if (token->type == CPP_COLON
13668               || (token->type == CPP_NAME
13669                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13670                   == CPP_COLON))
13671             {
13672               tree identifier;
13673               tree width;
13674
13675               /* Get the name of the bitfield.  Note that we cannot just
13676                  check TOKEN here because it may have been invalidated by
13677                  the call to cp_lexer_peek_nth_token above.  */
13678               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13679                 identifier = cp_parser_identifier (parser);
13680               else
13681                 identifier = NULL_TREE;
13682
13683               /* Consume the `:' token.  */
13684               cp_lexer_consume_token (parser->lexer);
13685               /* Get the width of the bitfield.  */
13686               width
13687                 = cp_parser_constant_expression (parser,
13688                                                  /*allow_non_constant=*/false,
13689                                                  NULL);
13690
13691               /* Look for attributes that apply to the bitfield.  */
13692               attributes = cp_parser_attributes_opt (parser);
13693               /* Remember which attributes are prefix attributes and
13694                  which are not.  */
13695               first_attribute = attributes;
13696               /* Combine the attributes.  */
13697               attributes = chainon (prefix_attributes, attributes);
13698
13699               /* Create the bitfield declaration.  */
13700               decl = grokbitfield (identifier
13701                                    ? make_id_declarator (NULL_TREE,
13702                                                          identifier,
13703                                                          sfk_none)
13704                                    : NULL,
13705                                    &decl_specifiers,
13706                                    width);
13707               /* Apply the attributes.  */
13708               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13709             }
13710           else
13711             {
13712               cp_declarator *declarator;
13713               tree initializer;
13714               tree asm_specification;
13715               int ctor_dtor_or_conv_p;
13716
13717               /* Parse the declarator.  */
13718               declarator
13719                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13720                                         &ctor_dtor_or_conv_p,
13721                                         /*parenthesized_p=*/NULL,
13722                                         /*member_p=*/true);
13723
13724               /* If something went wrong parsing the declarator, make sure
13725                  that we at least consume some tokens.  */
13726               if (declarator == cp_error_declarator)
13727                 {
13728                   /* Skip to the end of the statement.  */
13729                   cp_parser_skip_to_end_of_statement (parser);
13730                   /* If the next token is not a semicolon, that is
13731                      probably because we just skipped over the body of
13732                      a function.  So, we consume a semicolon if
13733                      present, but do not issue an error message if it
13734                      is not present.  */
13735                   if (cp_lexer_next_token_is (parser->lexer,
13736                                               CPP_SEMICOLON))
13737                     cp_lexer_consume_token (parser->lexer);
13738                   return;
13739                 }
13740
13741               if (declares_class_or_enum & 2)
13742                 cp_parser_check_for_definition_in_return_type
13743                   (declarator, decl_specifiers.type);
13744
13745               /* Look for an asm-specification.  */
13746               asm_specification = cp_parser_asm_specification_opt (parser);
13747               /* Look for attributes that apply to the declaration.  */
13748               attributes = cp_parser_attributes_opt (parser);
13749               /* Remember which attributes are prefix attributes and
13750                  which are not.  */
13751               first_attribute = attributes;
13752               /* Combine the attributes.  */
13753               attributes = chainon (prefix_attributes, attributes);
13754
13755               /* If it's an `=', then we have a constant-initializer or a
13756                  pure-specifier.  It is not correct to parse the
13757                  initializer before registering the member declaration
13758                  since the member declaration should be in scope while
13759                  its initializer is processed.  However, the rest of the
13760                  front end does not yet provide an interface that allows
13761                  us to handle this correctly.  */
13762               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13763                 {
13764                   /* In [class.mem]:
13765
13766                      A pure-specifier shall be used only in the declaration of
13767                      a virtual function.
13768
13769                      A member-declarator can contain a constant-initializer
13770                      only if it declares a static member of integral or
13771                      enumeration type.
13772
13773                      Therefore, if the DECLARATOR is for a function, we look
13774                      for a pure-specifier; otherwise, we look for a
13775                      constant-initializer.  When we call `grokfield', it will
13776                      perform more stringent semantics checks.  */
13777                   if (declarator->kind == cdk_function
13778                       && declarator->declarator->kind == cdk_id)
13779                     initializer = cp_parser_pure_specifier (parser);
13780                   else
13781                     /* Parse the initializer.  */
13782                     initializer = cp_parser_constant_initializer (parser);
13783                 }
13784               /* Otherwise, there is no initializer.  */
13785               else
13786                 initializer = NULL_TREE;
13787
13788               /* See if we are probably looking at a function
13789                  definition.  We are certainly not looking at a
13790                  member-declarator.  Calling `grokfield' has
13791                  side-effects, so we must not do it unless we are sure
13792                  that we are looking at a member-declarator.  */
13793               if (cp_parser_token_starts_function_definition_p
13794                   (cp_lexer_peek_token (parser->lexer)))
13795                 {
13796                   /* The grammar does not allow a pure-specifier to be
13797                      used when a member function is defined.  (It is
13798                      possible that this fact is an oversight in the
13799                      standard, since a pure function may be defined
13800                      outside of the class-specifier.  */
13801                   if (initializer)
13802                     error ("pure-specifier on function-definition");
13803                   decl = cp_parser_save_member_function_body (parser,
13804                                                               &decl_specifiers,
13805                                                               declarator,
13806                                                               attributes);
13807                   /* If the member was not a friend, declare it here.  */
13808                   if (!friend_p)
13809                     finish_member_declaration (decl);
13810                   /* Peek at the next token.  */
13811                   token = cp_lexer_peek_token (parser->lexer);
13812                   /* If the next token is a semicolon, consume it.  */
13813                   if (token->type == CPP_SEMICOLON)
13814                     cp_lexer_consume_token (parser->lexer);
13815                   return;
13816                 }
13817               else
13818                 /* Create the declaration.  */
13819                 decl = grokfield (declarator, &decl_specifiers,
13820                                   initializer, /*init_const_expr_p=*/true,
13821                                   asm_specification,
13822                                   attributes);
13823             }
13824
13825           /* Reset PREFIX_ATTRIBUTES.  */
13826           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13827             attributes = TREE_CHAIN (attributes);
13828           if (attributes)
13829             TREE_CHAIN (attributes) = NULL_TREE;
13830
13831           /* If there is any qualification still in effect, clear it
13832              now; we will be starting fresh with the next declarator.  */
13833           parser->scope = NULL_TREE;
13834           parser->qualifying_scope = NULL_TREE;
13835           parser->object_scope = NULL_TREE;
13836           /* If it's a `,', then there are more declarators.  */
13837           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13838             cp_lexer_consume_token (parser->lexer);
13839           /* If the next token isn't a `;', then we have a parse error.  */
13840           else if (cp_lexer_next_token_is_not (parser->lexer,
13841                                                CPP_SEMICOLON))
13842             {
13843               cp_parser_error (parser, "expected %<;%>");
13844               /* Skip tokens until we find a `;'.  */
13845               cp_parser_skip_to_end_of_statement (parser);
13846
13847               break;
13848             }
13849
13850           if (decl)
13851             {
13852               /* Add DECL to the list of members.  */
13853               if (!friend_p)
13854                 finish_member_declaration (decl);
13855
13856               if (TREE_CODE (decl) == FUNCTION_DECL)
13857                 cp_parser_save_default_args (parser, decl);
13858             }
13859         }
13860     }
13861
13862   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13863 }
13864
13865 /* Parse a pure-specifier.
13866
13867    pure-specifier:
13868      = 0
13869
13870    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13871    Otherwise, ERROR_MARK_NODE is returned.  */
13872
13873 static tree
13874 cp_parser_pure_specifier (cp_parser* parser)
13875 {
13876   cp_token *token;
13877
13878   /* Look for the `=' token.  */
13879   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13880     return error_mark_node;
13881   /* Look for the `0' token.  */
13882   token = cp_lexer_consume_token (parser->lexer);
13883   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
13884   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
13885     {
13886       cp_parser_error (parser,
13887                        "invalid pure specifier (only `= 0' is allowed)");
13888       cp_parser_skip_to_end_of_statement (parser);
13889       return error_mark_node;
13890     }
13891   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
13892     {
13893       error ("templates may not be %<virtual%>");
13894       return error_mark_node;
13895     }
13896
13897   return integer_zero_node;
13898 }
13899
13900 /* Parse a constant-initializer.
13901
13902    constant-initializer:
13903      = constant-expression
13904
13905    Returns a representation of the constant-expression.  */
13906
13907 static tree
13908 cp_parser_constant_initializer (cp_parser* parser)
13909 {
13910   /* Look for the `=' token.  */
13911   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13912     return error_mark_node;
13913
13914   /* It is invalid to write:
13915
13916        struct S { static const int i = { 7 }; };
13917
13918      */
13919   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13920     {
13921       cp_parser_error (parser,
13922                        "a brace-enclosed initializer is not allowed here");
13923       /* Consume the opening brace.  */
13924       cp_lexer_consume_token (parser->lexer);
13925       /* Skip the initializer.  */
13926       cp_parser_skip_to_closing_brace (parser);
13927       /* Look for the trailing `}'.  */
13928       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13929
13930       return error_mark_node;
13931     }
13932
13933   return cp_parser_constant_expression (parser,
13934                                         /*allow_non_constant=*/false,
13935                                         NULL);
13936 }
13937
13938 /* Derived classes [gram.class.derived] */
13939
13940 /* Parse a base-clause.
13941
13942    base-clause:
13943      : base-specifier-list
13944
13945    base-specifier-list:
13946      base-specifier
13947      base-specifier-list , base-specifier
13948
13949    Returns a TREE_LIST representing the base-classes, in the order in
13950    which they were declared.  The representation of each node is as
13951    described by cp_parser_base_specifier.
13952
13953    In the case that no bases are specified, this function will return
13954    NULL_TREE, not ERROR_MARK_NODE.  */
13955
13956 static tree
13957 cp_parser_base_clause (cp_parser* parser)
13958 {
13959   tree bases = NULL_TREE;
13960
13961   /* Look for the `:' that begins the list.  */
13962   cp_parser_require (parser, CPP_COLON, "`:'");
13963
13964   /* Scan the base-specifier-list.  */
13965   while (true)
13966     {
13967       cp_token *token;
13968       tree base;
13969
13970       /* Look for the base-specifier.  */
13971       base = cp_parser_base_specifier (parser);
13972       /* Add BASE to the front of the list.  */
13973       if (base != error_mark_node)
13974         {
13975           TREE_CHAIN (base) = bases;
13976           bases = base;
13977         }
13978       /* Peek at the next token.  */
13979       token = cp_lexer_peek_token (parser->lexer);
13980       /* If it's not a comma, then the list is complete.  */
13981       if (token->type != CPP_COMMA)
13982         break;
13983       /* Consume the `,'.  */
13984       cp_lexer_consume_token (parser->lexer);
13985     }
13986
13987   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13988      base class had a qualified name.  However, the next name that
13989      appears is certainly not qualified.  */
13990   parser->scope = NULL_TREE;
13991   parser->qualifying_scope = NULL_TREE;
13992   parser->object_scope = NULL_TREE;
13993
13994   return nreverse (bases);
13995 }
13996
13997 /* Parse a base-specifier.
13998
13999    base-specifier:
14000      :: [opt] nested-name-specifier [opt] class-name
14001      virtual access-specifier [opt] :: [opt] nested-name-specifier
14002        [opt] class-name
14003      access-specifier virtual [opt] :: [opt] nested-name-specifier
14004        [opt] class-name
14005
14006    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
14007    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14008    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
14009    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
14010
14011 static tree
14012 cp_parser_base_specifier (cp_parser* parser)
14013 {
14014   cp_token *token;
14015   bool done = false;
14016   bool virtual_p = false;
14017   bool duplicate_virtual_error_issued_p = false;
14018   bool duplicate_access_error_issued_p = false;
14019   bool class_scope_p, template_p;
14020   tree access = access_default_node;
14021   tree type;
14022
14023   /* Process the optional `virtual' and `access-specifier'.  */
14024   while (!done)
14025     {
14026       /* Peek at the next token.  */
14027       token = cp_lexer_peek_token (parser->lexer);
14028       /* Process `virtual'.  */
14029       switch (token->keyword)
14030         {
14031         case RID_VIRTUAL:
14032           /* If `virtual' appears more than once, issue an error.  */
14033           if (virtual_p && !duplicate_virtual_error_issued_p)
14034             {
14035               cp_parser_error (parser,
14036                                "%<virtual%> specified more than once in base-specified");
14037               duplicate_virtual_error_issued_p = true;
14038             }
14039
14040           virtual_p = true;
14041
14042           /* Consume the `virtual' token.  */
14043           cp_lexer_consume_token (parser->lexer);
14044
14045           break;
14046
14047         case RID_PUBLIC:
14048         case RID_PROTECTED:
14049         case RID_PRIVATE:
14050           /* If more than one access specifier appears, issue an
14051              error.  */
14052           if (access != access_default_node
14053               && !duplicate_access_error_issued_p)
14054             {
14055               cp_parser_error (parser,
14056                                "more than one access specifier in base-specified");
14057               duplicate_access_error_issued_p = true;
14058             }
14059
14060           access = ridpointers[(int) token->keyword];
14061
14062           /* Consume the access-specifier.  */
14063           cp_lexer_consume_token (parser->lexer);
14064
14065           break;
14066
14067         default:
14068           done = true;
14069           break;
14070         }
14071     }
14072   /* It is not uncommon to see programs mechanically, erroneously, use
14073      the 'typename' keyword to denote (dependent) qualified types
14074      as base classes.  */
14075   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14076     {
14077       if (!processing_template_decl)
14078         error ("keyword %<typename%> not allowed outside of templates");
14079       else
14080         error ("keyword %<typename%> not allowed in this context "
14081                "(the base class is implicitly a type)");
14082       cp_lexer_consume_token (parser->lexer);
14083     }
14084
14085   /* Look for the optional `::' operator.  */
14086   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14087   /* Look for the nested-name-specifier.  The simplest way to
14088      implement:
14089
14090        [temp.res]
14091
14092        The keyword `typename' is not permitted in a base-specifier or
14093        mem-initializer; in these contexts a qualified name that
14094        depends on a template-parameter is implicitly assumed to be a
14095        type name.
14096
14097      is to pretend that we have seen the `typename' keyword at this
14098      point.  */
14099   cp_parser_nested_name_specifier_opt (parser,
14100                                        /*typename_keyword_p=*/true,
14101                                        /*check_dependency_p=*/true,
14102                                        typename_type,
14103                                        /*is_declaration=*/true);
14104   /* If the base class is given by a qualified name, assume that names
14105      we see are type names or templates, as appropriate.  */
14106   class_scope_p = (parser->scope && TYPE_P (parser->scope));
14107   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14108
14109   /* Finally, look for the class-name.  */
14110   type = cp_parser_class_name (parser,
14111                                class_scope_p,
14112                                template_p,
14113                                typename_type,
14114                                /*check_dependency_p=*/true,
14115                                /*class_head_p=*/false,
14116                                /*is_declaration=*/true);
14117
14118   if (type == error_mark_node)
14119     return error_mark_node;
14120
14121   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14122 }
14123
14124 /* Exception handling [gram.exception] */
14125
14126 /* Parse an (optional) exception-specification.
14127
14128    exception-specification:
14129      throw ( type-id-list [opt] )
14130
14131    Returns a TREE_LIST representing the exception-specification.  The
14132    TREE_VALUE of each node is a type.  */
14133
14134 static tree
14135 cp_parser_exception_specification_opt (cp_parser* parser)
14136 {
14137   cp_token *token;
14138   tree type_id_list;
14139
14140   /* Peek at the next token.  */
14141   token = cp_lexer_peek_token (parser->lexer);
14142   /* If it's not `throw', then there's no exception-specification.  */
14143   if (!cp_parser_is_keyword (token, RID_THROW))
14144     return NULL_TREE;
14145
14146   /* Consume the `throw'.  */
14147   cp_lexer_consume_token (parser->lexer);
14148
14149   /* Look for the `('.  */
14150   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14151
14152   /* Peek at the next token.  */
14153   token = cp_lexer_peek_token (parser->lexer);
14154   /* If it's not a `)', then there is a type-id-list.  */
14155   if (token->type != CPP_CLOSE_PAREN)
14156     {
14157       const char *saved_message;
14158
14159       /* Types may not be defined in an exception-specification.  */
14160       saved_message = parser->type_definition_forbidden_message;
14161       parser->type_definition_forbidden_message
14162         = "types may not be defined in an exception-specification";
14163       /* Parse the type-id-list.  */
14164       type_id_list = cp_parser_type_id_list (parser);
14165       /* Restore the saved message.  */
14166       parser->type_definition_forbidden_message = saved_message;
14167     }
14168   else
14169     type_id_list = empty_except_spec;
14170
14171   /* Look for the `)'.  */
14172   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14173
14174   return type_id_list;
14175 }
14176
14177 /* Parse an (optional) type-id-list.
14178
14179    type-id-list:
14180      type-id
14181      type-id-list , type-id
14182
14183    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14184    in the order that the types were presented.  */
14185
14186 static tree
14187 cp_parser_type_id_list (cp_parser* parser)
14188 {
14189   tree types = NULL_TREE;
14190
14191   while (true)
14192     {
14193       cp_token *token;
14194       tree type;
14195
14196       /* Get the next type-id.  */
14197       type = cp_parser_type_id (parser);
14198       /* Add it to the list.  */
14199       types = add_exception_specifier (types, type, /*complain=*/1);
14200       /* Peek at the next token.  */
14201       token = cp_lexer_peek_token (parser->lexer);
14202       /* If it is not a `,', we are done.  */
14203       if (token->type != CPP_COMMA)
14204         break;
14205       /* Consume the `,'.  */
14206       cp_lexer_consume_token (parser->lexer);
14207     }
14208
14209   return nreverse (types);
14210 }
14211
14212 /* Parse a try-block.
14213
14214    try-block:
14215      try compound-statement handler-seq  */
14216
14217 static tree
14218 cp_parser_try_block (cp_parser* parser)
14219 {
14220   tree try_block;
14221
14222   cp_parser_require_keyword (parser, RID_TRY, "`try'");
14223   try_block = begin_try_block ();
14224   cp_parser_compound_statement (parser, NULL, true);
14225   finish_try_block (try_block);
14226   cp_parser_handler_seq (parser);
14227   finish_handler_sequence (try_block);
14228
14229   return try_block;
14230 }
14231
14232 /* Parse a function-try-block.
14233
14234    function-try-block:
14235      try ctor-initializer [opt] function-body handler-seq  */
14236
14237 static bool
14238 cp_parser_function_try_block (cp_parser* parser)
14239 {
14240   tree compound_stmt;
14241   tree try_block;
14242   bool ctor_initializer_p;
14243
14244   /* Look for the `try' keyword.  */
14245   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14246     return false;
14247   /* Let the rest of the front-end know where we are.  */
14248   try_block = begin_function_try_block (&compound_stmt);
14249   /* Parse the function-body.  */
14250   ctor_initializer_p
14251     = cp_parser_ctor_initializer_opt_and_function_body (parser);
14252   /* We're done with the `try' part.  */
14253   finish_function_try_block (try_block);
14254   /* Parse the handlers.  */
14255   cp_parser_handler_seq (parser);
14256   /* We're done with the handlers.  */
14257   finish_function_handler_sequence (try_block, compound_stmt);
14258
14259   return ctor_initializer_p;
14260 }
14261
14262 /* Parse a handler-seq.
14263
14264    handler-seq:
14265      handler handler-seq [opt]  */
14266
14267 static void
14268 cp_parser_handler_seq (cp_parser* parser)
14269 {
14270   while (true)
14271     {
14272       cp_token *token;
14273
14274       /* Parse the handler.  */
14275       cp_parser_handler (parser);
14276       /* Peek at the next token.  */
14277       token = cp_lexer_peek_token (parser->lexer);
14278       /* If it's not `catch' then there are no more handlers.  */
14279       if (!cp_parser_is_keyword (token, RID_CATCH))
14280         break;
14281     }
14282 }
14283
14284 /* Parse a handler.
14285
14286    handler:
14287      catch ( exception-declaration ) compound-statement  */
14288
14289 static void
14290 cp_parser_handler (cp_parser* parser)
14291 {
14292   tree handler;
14293   tree declaration;
14294
14295   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14296   handler = begin_handler ();
14297   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14298   declaration = cp_parser_exception_declaration (parser);
14299   finish_handler_parms (declaration, handler);
14300   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14301   cp_parser_compound_statement (parser, NULL, false);
14302   finish_handler (handler);
14303 }
14304
14305 /* Parse an exception-declaration.
14306
14307    exception-declaration:
14308      type-specifier-seq declarator
14309      type-specifier-seq abstract-declarator
14310      type-specifier-seq
14311      ...
14312
14313    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14314    ellipsis variant is used.  */
14315
14316 static tree
14317 cp_parser_exception_declaration (cp_parser* parser)
14318 {
14319   cp_decl_specifier_seq type_specifiers;
14320   cp_declarator *declarator;
14321   const char *saved_message;
14322
14323   /* If it's an ellipsis, it's easy to handle.  */
14324   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14325     {
14326       /* Consume the `...' token.  */
14327       cp_lexer_consume_token (parser->lexer);
14328       return NULL_TREE;
14329     }
14330
14331   /* Types may not be defined in exception-declarations.  */
14332   saved_message = parser->type_definition_forbidden_message;
14333   parser->type_definition_forbidden_message
14334     = "types may not be defined in exception-declarations";
14335
14336   /* Parse the type-specifier-seq.  */
14337   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14338                                 &type_specifiers);
14339   /* If it's a `)', then there is no declarator.  */
14340   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14341     declarator = NULL;
14342   else
14343     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14344                                        /*ctor_dtor_or_conv_p=*/NULL,
14345                                        /*parenthesized_p=*/NULL,
14346                                        /*member_p=*/false);
14347
14348   /* Restore the saved message.  */
14349   parser->type_definition_forbidden_message = saved_message;
14350
14351   if (!type_specifiers.any_specifiers_p)
14352     return error_mark_node;
14353
14354   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14355 }
14356
14357 /* Parse a throw-expression.
14358
14359    throw-expression:
14360      throw assignment-expression [opt]
14361
14362    Returns a THROW_EXPR representing the throw-expression.  */
14363
14364 static tree
14365 cp_parser_throw_expression (cp_parser* parser)
14366 {
14367   tree expression;
14368   cp_token* token;
14369
14370   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14371   token = cp_lexer_peek_token (parser->lexer);
14372   /* Figure out whether or not there is an assignment-expression
14373      following the "throw" keyword.  */
14374   if (token->type == CPP_COMMA
14375       || token->type == CPP_SEMICOLON
14376       || token->type == CPP_CLOSE_PAREN
14377       || token->type == CPP_CLOSE_SQUARE
14378       || token->type == CPP_CLOSE_BRACE
14379       || token->type == CPP_COLON)
14380     expression = NULL_TREE;
14381   else
14382     expression = cp_parser_assignment_expression (parser,
14383                                                   /*cast_p=*/false);
14384
14385   return build_throw (expression);
14386 }
14387
14388 /* GNU Extensions */
14389
14390 /* Parse an (optional) asm-specification.
14391
14392    asm-specification:
14393      asm ( string-literal )
14394
14395    If the asm-specification is present, returns a STRING_CST
14396    corresponding to the string-literal.  Otherwise, returns
14397    NULL_TREE.  */
14398
14399 static tree
14400 cp_parser_asm_specification_opt (cp_parser* parser)
14401 {
14402   cp_token *token;
14403   tree asm_specification;
14404
14405   /* Peek at the next token.  */
14406   token = cp_lexer_peek_token (parser->lexer);
14407   /* If the next token isn't the `asm' keyword, then there's no
14408      asm-specification.  */
14409   if (!cp_parser_is_keyword (token, RID_ASM))
14410     return NULL_TREE;
14411
14412   /* Consume the `asm' token.  */
14413   cp_lexer_consume_token (parser->lexer);
14414   /* Look for the `('.  */
14415   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14416
14417   /* Look for the string-literal.  */
14418   asm_specification = cp_parser_string_literal (parser, false, false);
14419
14420   /* Look for the `)'.  */
14421   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14422
14423   return asm_specification;
14424 }
14425
14426 /* Parse an asm-operand-list.
14427
14428    asm-operand-list:
14429      asm-operand
14430      asm-operand-list , asm-operand
14431
14432    asm-operand:
14433      string-literal ( expression )
14434      [ string-literal ] string-literal ( expression )
14435
14436    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14437    each node is the expression.  The TREE_PURPOSE is itself a
14438    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14439    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14440    is a STRING_CST for the string literal before the parenthesis.  */
14441
14442 static tree
14443 cp_parser_asm_operand_list (cp_parser* parser)
14444 {
14445   tree asm_operands = NULL_TREE;
14446
14447   while (true)
14448     {
14449       tree string_literal;
14450       tree expression;
14451       tree name;
14452
14453       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14454         {
14455           /* Consume the `[' token.  */
14456           cp_lexer_consume_token (parser->lexer);
14457           /* Read the operand name.  */
14458           name = cp_parser_identifier (parser);
14459           if (name != error_mark_node)
14460             name = build_string (IDENTIFIER_LENGTH (name),
14461                                  IDENTIFIER_POINTER (name));
14462           /* Look for the closing `]'.  */
14463           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14464         }
14465       else
14466         name = NULL_TREE;
14467       /* Look for the string-literal.  */
14468       string_literal = cp_parser_string_literal (parser, false, false);
14469
14470       /* Look for the `('.  */
14471       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14472       /* Parse the expression.  */
14473       expression = cp_parser_expression (parser, /*cast_p=*/false);
14474       /* Look for the `)'.  */
14475       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14476
14477       /* Add this operand to the list.  */
14478       asm_operands = tree_cons (build_tree_list (name, string_literal),
14479                                 expression,
14480                                 asm_operands);
14481       /* If the next token is not a `,', there are no more
14482          operands.  */
14483       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14484         break;
14485       /* Consume the `,'.  */
14486       cp_lexer_consume_token (parser->lexer);
14487     }
14488
14489   return nreverse (asm_operands);
14490 }
14491
14492 /* Parse an asm-clobber-list.
14493
14494    asm-clobber-list:
14495      string-literal
14496      asm-clobber-list , string-literal
14497
14498    Returns a TREE_LIST, indicating the clobbers in the order that they
14499    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14500
14501 static tree
14502 cp_parser_asm_clobber_list (cp_parser* parser)
14503 {
14504   tree clobbers = NULL_TREE;
14505
14506   while (true)
14507     {
14508       tree string_literal;
14509
14510       /* Look for the string literal.  */
14511       string_literal = cp_parser_string_literal (parser, false, false);
14512       /* Add it to the list.  */
14513       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14514       /* If the next token is not a `,', then the list is
14515          complete.  */
14516       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14517         break;
14518       /* Consume the `,' token.  */
14519       cp_lexer_consume_token (parser->lexer);
14520     }
14521
14522   return clobbers;
14523 }
14524
14525 /* Parse an (optional) series of attributes.
14526
14527    attributes:
14528      attributes attribute
14529
14530    attribute:
14531      __attribute__ (( attribute-list [opt] ))
14532
14533    The return value is as for cp_parser_attribute_list.  */
14534
14535 static tree
14536 cp_parser_attributes_opt (cp_parser* parser)
14537 {
14538   tree attributes = NULL_TREE;
14539
14540   while (true)
14541     {
14542       cp_token *token;
14543       tree attribute_list;
14544
14545       /* Peek at the next token.  */
14546       token = cp_lexer_peek_token (parser->lexer);
14547       /* If it's not `__attribute__', then we're done.  */
14548       if (token->keyword != RID_ATTRIBUTE)
14549         break;
14550
14551       /* Consume the `__attribute__' keyword.  */
14552       cp_lexer_consume_token (parser->lexer);
14553       /* Look for the two `(' tokens.  */
14554       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14555       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14556
14557       /* Peek at the next token.  */
14558       token = cp_lexer_peek_token (parser->lexer);
14559       if (token->type != CPP_CLOSE_PAREN)
14560         /* Parse the attribute-list.  */
14561         attribute_list = cp_parser_attribute_list (parser);
14562       else
14563         /* If the next token is a `)', then there is no attribute
14564            list.  */
14565         attribute_list = NULL;
14566
14567       /* Look for the two `)' tokens.  */
14568       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14569       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14570
14571       /* Add these new attributes to the list.  */
14572       attributes = chainon (attributes, attribute_list);
14573     }
14574
14575   return attributes;
14576 }
14577
14578 /* Parse an attribute-list.
14579
14580    attribute-list:
14581      attribute
14582      attribute-list , attribute
14583
14584    attribute:
14585      identifier
14586      identifier ( identifier )
14587      identifier ( identifier , expression-list )
14588      identifier ( expression-list )
14589
14590    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14591    to an attribute.  The TREE_PURPOSE of each node is the identifier
14592    indicating which attribute is in use.  The TREE_VALUE represents
14593    the arguments, if any.  */
14594
14595 static tree
14596 cp_parser_attribute_list (cp_parser* parser)
14597 {
14598   tree attribute_list = NULL_TREE;
14599   bool save_translate_strings_p = parser->translate_strings_p;
14600
14601   parser->translate_strings_p = false;
14602   while (true)
14603     {
14604       cp_token *token;
14605       tree identifier;
14606       tree attribute;
14607
14608       /* Look for the identifier.  We also allow keywords here; for
14609          example `__attribute__ ((const))' is legal.  */
14610       token = cp_lexer_peek_token (parser->lexer);
14611       if (token->type == CPP_NAME
14612           || token->type == CPP_KEYWORD)
14613         {
14614           tree arguments = NULL_TREE;
14615
14616           /* Consume the token.  */
14617           token = cp_lexer_consume_token (parser->lexer);
14618
14619           /* Save away the identifier that indicates which attribute
14620              this is.  */
14621           identifier = token->value;
14622           attribute = build_tree_list (identifier, NULL_TREE);
14623
14624           /* Peek at the next token.  */
14625           token = cp_lexer_peek_token (parser->lexer);
14626           /* If it's an `(', then parse the attribute arguments.  */
14627           if (token->type == CPP_OPEN_PAREN)
14628             {
14629               arguments = cp_parser_parenthesized_expression_list
14630                           (parser, true, /*cast_p=*/false,
14631                            /*non_constant_p=*/NULL);
14632               /* Save the arguments away.  */
14633               TREE_VALUE (attribute) = arguments;
14634             }
14635
14636           if (arguments != error_mark_node)
14637             {
14638               /* Add this attribute to the list.  */
14639               TREE_CHAIN (attribute) = attribute_list;
14640               attribute_list = attribute;
14641             }
14642
14643           token = cp_lexer_peek_token (parser->lexer);
14644         }
14645       /* Now, look for more attributes.  If the next token isn't a
14646          `,', we're done.  */
14647       if (token->type != CPP_COMMA)
14648         break;
14649
14650       /* Consume the comma and keep going.  */
14651       cp_lexer_consume_token (parser->lexer);
14652     }
14653   parser->translate_strings_p = save_translate_strings_p;
14654
14655   /* We built up the list in reverse order.  */
14656   return nreverse (attribute_list);
14657 }
14658
14659 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14660    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14661    current value of the PEDANTIC flag, regardless of whether or not
14662    the `__extension__' keyword is present.  The caller is responsible
14663    for restoring the value of the PEDANTIC flag.  */
14664
14665 static bool
14666 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14667 {
14668   /* Save the old value of the PEDANTIC flag.  */
14669   *saved_pedantic = pedantic;
14670
14671   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14672     {
14673       /* Consume the `__extension__' token.  */
14674       cp_lexer_consume_token (parser->lexer);
14675       /* We're not being pedantic while the `__extension__' keyword is
14676          in effect.  */
14677       pedantic = 0;
14678
14679       return true;
14680     }
14681
14682   return false;
14683 }
14684
14685 /* Parse a label declaration.
14686
14687    label-declaration:
14688      __label__ label-declarator-seq ;
14689
14690    label-declarator-seq:
14691      identifier , label-declarator-seq
14692      identifier  */
14693
14694 static void
14695 cp_parser_label_declaration (cp_parser* parser)
14696 {
14697   /* Look for the `__label__' keyword.  */
14698   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14699
14700   while (true)
14701     {
14702       tree identifier;
14703
14704       /* Look for an identifier.  */
14705       identifier = cp_parser_identifier (parser);
14706       /* If we failed, stop.  */
14707       if (identifier == error_mark_node)
14708         break;
14709       /* Declare it as a label.  */
14710       finish_label_decl (identifier);
14711       /* If the next token is a `;', stop.  */
14712       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14713         break;
14714       /* Look for the `,' separating the label declarations.  */
14715       cp_parser_require (parser, CPP_COMMA, "`,'");
14716     }
14717
14718   /* Look for the final `;'.  */
14719   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14720 }
14721
14722 /* Support Functions */
14723
14724 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14725    NAME should have one of the representations used for an
14726    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14727    is returned.  If PARSER->SCOPE is a dependent type, then a
14728    SCOPE_REF is returned.
14729
14730    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14731    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14732    was formed.  Abstractly, such entities should not be passed to this
14733    function, because they do not need to be looked up, but it is
14734    simpler to check for this special case here, rather than at the
14735    call-sites.
14736
14737    In cases not explicitly covered above, this function returns a
14738    DECL, OVERLOAD, or baselink representing the result of the lookup.
14739    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14740    is returned.
14741
14742    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14743    (e.g., "struct") that was used.  In that case bindings that do not
14744    refer to types are ignored.
14745
14746    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14747    ignored.
14748
14749    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14750    are ignored.
14751
14752    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14753    types.
14754
14755    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14756    TREE_LIST of candidates if name-lookup results in an ambiguity, and
14757    NULL_TREE otherwise.  */
14758
14759 static tree
14760 cp_parser_lookup_name (cp_parser *parser, tree name,
14761                        enum tag_types tag_type,
14762                        bool is_template,
14763                        bool is_namespace,
14764                        bool check_dependency,
14765                        tree *ambiguous_decls)
14766 {
14767   int flags = 0;
14768   tree decl;
14769   tree object_type = parser->context->object_type;
14770
14771   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14772     flags |= LOOKUP_COMPLAIN;
14773
14774   /* Assume that the lookup will be unambiguous.  */
14775   if (ambiguous_decls)
14776     *ambiguous_decls = NULL_TREE;
14777
14778   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14779      no longer valid.  Note that if we are parsing tentatively, and
14780      the parse fails, OBJECT_TYPE will be automatically restored.  */
14781   parser->context->object_type = NULL_TREE;
14782
14783   if (name == error_mark_node)
14784     return error_mark_node;
14785
14786   /* A template-id has already been resolved; there is no lookup to
14787      do.  */
14788   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14789     return name;
14790   if (BASELINK_P (name))
14791     {
14792       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14793                   == TEMPLATE_ID_EXPR);
14794       return name;
14795     }
14796
14797   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14798      it should already have been checked to make sure that the name
14799      used matches the type being destroyed.  */
14800   if (TREE_CODE (name) == BIT_NOT_EXPR)
14801     {
14802       tree type;
14803
14804       /* Figure out to which type this destructor applies.  */
14805       if (parser->scope)
14806         type = parser->scope;
14807       else if (object_type)
14808         type = object_type;
14809       else
14810         type = current_class_type;
14811       /* If that's not a class type, there is no destructor.  */
14812       if (!type || !CLASS_TYPE_P (type))
14813         return error_mark_node;
14814       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14815         lazily_declare_fn (sfk_destructor, type);
14816       if (!CLASSTYPE_DESTRUCTORS (type))
14817           return error_mark_node;
14818       /* If it was a class type, return the destructor.  */
14819       return CLASSTYPE_DESTRUCTORS (type);
14820     }
14821
14822   /* By this point, the NAME should be an ordinary identifier.  If
14823      the id-expression was a qualified name, the qualifying scope is
14824      stored in PARSER->SCOPE at this point.  */
14825   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14826
14827   /* Perform the lookup.  */
14828   if (parser->scope)
14829     {
14830       bool dependent_p;
14831
14832       if (parser->scope == error_mark_node)
14833         return error_mark_node;
14834
14835       /* If the SCOPE is dependent, the lookup must be deferred until
14836          the template is instantiated -- unless we are explicitly
14837          looking up names in uninstantiated templates.  Even then, we
14838          cannot look up the name if the scope is not a class type; it
14839          might, for example, be a template type parameter.  */
14840       dependent_p = (TYPE_P (parser->scope)
14841                      && !(parser->in_declarator_p
14842                           && currently_open_class (parser->scope))
14843                      && dependent_type_p (parser->scope));
14844       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14845            && dependent_p)
14846         {
14847           if (tag_type)
14848             {
14849               tree type;
14850
14851               /* The resolution to Core Issue 180 says that `struct
14852                  A::B' should be considered a type-name, even if `A'
14853                  is dependent.  */
14854               type = make_typename_type (parser->scope, name, tag_type,
14855                                          /*complain=*/tf_error);
14856               decl = TYPE_NAME (type);
14857             }
14858           else if (is_template
14859                    && (cp_parser_next_token_ends_template_argument_p (parser)
14860                        || cp_lexer_next_token_is (parser->lexer,
14861                                                   CPP_CLOSE_PAREN)))
14862             decl = make_unbound_class_template (parser->scope,
14863                                                 name, NULL_TREE,
14864                                                 /*complain=*/tf_error);
14865           else
14866             decl = build_qualified_name (/*type=*/NULL_TREE,
14867                                          parser->scope, name,
14868                                          is_template);
14869         }
14870       else
14871         {
14872           tree pushed_scope = NULL_TREE;
14873
14874           /* If PARSER->SCOPE is a dependent type, then it must be a
14875              class type, and we must not be checking dependencies;
14876              otherwise, we would have processed this lookup above.  So
14877              that PARSER->SCOPE is not considered a dependent base by
14878              lookup_member, we must enter the scope here.  */
14879           if (dependent_p)
14880             pushed_scope = push_scope (parser->scope);
14881           /* If the PARSER->SCOPE is a template specialization, it
14882              may be instantiated during name lookup.  In that case,
14883              errors may be issued.  Even if we rollback the current
14884              tentative parse, those errors are valid.  */
14885           decl = lookup_qualified_name (parser->scope, name,
14886                                         tag_type != none_type,
14887                                         /*complain=*/true);
14888           if (pushed_scope)
14889             pop_scope (pushed_scope);
14890         }
14891       parser->qualifying_scope = parser->scope;
14892       parser->object_scope = NULL_TREE;
14893     }
14894   else if (object_type)
14895     {
14896       tree object_decl = NULL_TREE;
14897       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14898          OBJECT_TYPE is not a class.  */
14899       if (CLASS_TYPE_P (object_type))
14900         /* If the OBJECT_TYPE is a template specialization, it may
14901            be instantiated during name lookup.  In that case, errors
14902            may be issued.  Even if we rollback the current tentative
14903            parse, those errors are valid.  */
14904         object_decl = lookup_member (object_type,
14905                                      name,
14906                                      /*protect=*/0,
14907                                      tag_type != none_type);
14908       /* Look it up in the enclosing context, too.  */
14909       decl = lookup_name_real (name, tag_type != none_type,
14910                                /*nonclass=*/0,
14911                                /*block_p=*/true, is_namespace, flags);
14912       parser->object_scope = object_type;
14913       parser->qualifying_scope = NULL_TREE;
14914       if (object_decl)
14915         decl = object_decl;
14916     }
14917   else
14918     {
14919       decl = lookup_name_real (name, tag_type != none_type,
14920                                /*nonclass=*/0,
14921                                /*block_p=*/true, is_namespace, flags);
14922       parser->qualifying_scope = NULL_TREE;
14923       parser->object_scope = NULL_TREE;
14924     }
14925
14926   /* If the lookup failed, let our caller know.  */
14927   if (!decl || decl == error_mark_node)
14928     return error_mark_node;
14929
14930   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14931   if (TREE_CODE (decl) == TREE_LIST)
14932     {
14933       if (ambiguous_decls)
14934         *ambiguous_decls = decl;
14935       /* The error message we have to print is too complicated for
14936          cp_parser_error, so we incorporate its actions directly.  */
14937       if (!cp_parser_simulate_error (parser))
14938         {
14939           error ("reference to %qD is ambiguous", name);
14940           print_candidates (decl);
14941         }
14942       return error_mark_node;
14943     }
14944
14945   gcc_assert (DECL_P (decl)
14946               || TREE_CODE (decl) == OVERLOAD
14947               || TREE_CODE (decl) == SCOPE_REF
14948               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14949               || BASELINK_P (decl));
14950
14951   /* If we have resolved the name of a member declaration, check to
14952      see if the declaration is accessible.  When the name resolves to
14953      set of overloaded functions, accessibility is checked when
14954      overload resolution is done.
14955
14956      During an explicit instantiation, access is not checked at all,
14957      as per [temp.explicit].  */
14958   if (DECL_P (decl))
14959     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14960
14961   return decl;
14962 }
14963
14964 /* Like cp_parser_lookup_name, but for use in the typical case where
14965    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14966    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14967
14968 static tree
14969 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14970 {
14971   return cp_parser_lookup_name (parser, name,
14972                                 none_type,
14973                                 /*is_template=*/false,
14974                                 /*is_namespace=*/false,
14975                                 /*check_dependency=*/true,
14976                                 /*ambiguous_decls=*/NULL);
14977 }
14978
14979 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14980    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14981    true, the DECL indicates the class being defined in a class-head,
14982    or declared in an elaborated-type-specifier.
14983
14984    Otherwise, return DECL.  */
14985
14986 static tree
14987 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14988 {
14989   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14990      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14991
14992        struct A {
14993          template <typename T> struct B;
14994        };
14995
14996        template <typename T> struct A::B {};
14997
14998      Similarly, in an elaborated-type-specifier:
14999
15000        namespace N { struct X{}; }
15001
15002        struct A {
15003          template <typename T> friend struct N::X;
15004        };
15005
15006      However, if the DECL refers to a class type, and we are in
15007      the scope of the class, then the name lookup automatically
15008      finds the TYPE_DECL created by build_self_reference rather
15009      than a TEMPLATE_DECL.  For example, in:
15010
15011        template <class T> struct S {
15012          S s;
15013        };
15014
15015      there is no need to handle such case.  */
15016
15017   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15018     return DECL_TEMPLATE_RESULT (decl);
15019
15020   return decl;
15021 }
15022
15023 /* If too many, or too few, template-parameter lists apply to the
15024    declarator, issue an error message.  Returns TRUE if all went well,
15025    and FALSE otherwise.  */
15026
15027 static bool
15028 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15029                                                 cp_declarator *declarator)
15030 {
15031   unsigned num_templates;
15032
15033   /* We haven't seen any classes that involve template parameters yet.  */
15034   num_templates = 0;
15035
15036   switch (declarator->kind)
15037     {
15038     case cdk_id:
15039       if (declarator->u.id.qualifying_scope)
15040         {
15041           tree scope;
15042           tree member;
15043
15044           scope = declarator->u.id.qualifying_scope;
15045           member = declarator->u.id.unqualified_name;
15046
15047           while (scope && CLASS_TYPE_P (scope))
15048             {
15049               /* You're supposed to have one `template <...>'
15050                  for every template class, but you don't need one
15051                  for a full specialization.  For example:
15052
15053                  template <class T> struct S{};
15054                  template <> struct S<int> { void f(); };
15055                  void S<int>::f () {}
15056
15057                  is correct; there shouldn't be a `template <>' for
15058                  the definition of `S<int>::f'.  */
15059               if (CLASSTYPE_TEMPLATE_INFO (scope)
15060                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
15061                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
15062                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15063                 ++num_templates;
15064
15065               scope = TYPE_CONTEXT (scope);
15066             }
15067         }
15068       else if (TREE_CODE (declarator->u.id.unqualified_name)
15069                == TEMPLATE_ID_EXPR)
15070         /* If the DECLARATOR has the form `X<y>' then it uses one
15071            additional level of template parameters.  */
15072         ++num_templates;
15073
15074       return cp_parser_check_template_parameters (parser,
15075                                                   num_templates);
15076
15077     case cdk_function:
15078     case cdk_array:
15079     case cdk_pointer:
15080     case cdk_reference:
15081     case cdk_ptrmem:
15082       return (cp_parser_check_declarator_template_parameters
15083               (parser, declarator->declarator));
15084
15085     case cdk_error:
15086       return true;
15087
15088     default:
15089       gcc_unreachable ();
15090     }
15091   return false;
15092 }
15093
15094 /* NUM_TEMPLATES were used in the current declaration.  If that is
15095    invalid, return FALSE and issue an error messages.  Otherwise,
15096    return TRUE.  */
15097
15098 static bool
15099 cp_parser_check_template_parameters (cp_parser* parser,
15100                                      unsigned num_templates)
15101 {
15102   /* If there are more template classes than parameter lists, we have
15103      something like:
15104
15105        template <class T> void S<T>::R<T>::f ();  */
15106   if (parser->num_template_parameter_lists < num_templates)
15107     {
15108       error ("too few template-parameter-lists");
15109       return false;
15110     }
15111   /* If there are the same number of template classes and parameter
15112      lists, that's OK.  */
15113   if (parser->num_template_parameter_lists == num_templates)
15114     return true;
15115   /* If there are more, but only one more, then we are referring to a
15116      member template.  That's OK too.  */
15117   if (parser->num_template_parameter_lists == num_templates + 1)
15118       return true;
15119   /* Otherwise, there are too many template parameter lists.  We have
15120      something like:
15121
15122      template <class T> template <class U> void S::f();  */
15123   error ("too many template-parameter-lists");
15124   return false;
15125 }
15126
15127 /* Parse an optional `::' token indicating that the following name is
15128    from the global namespace.  If so, PARSER->SCOPE is set to the
15129    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15130    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15131    Returns the new value of PARSER->SCOPE, if the `::' token is
15132    present, and NULL_TREE otherwise.  */
15133
15134 static tree
15135 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15136 {
15137   cp_token *token;
15138
15139   /* Peek at the next token.  */
15140   token = cp_lexer_peek_token (parser->lexer);
15141   /* If we're looking at a `::' token then we're starting from the
15142      global namespace, not our current location.  */
15143   if (token->type == CPP_SCOPE)
15144     {
15145       /* Consume the `::' token.  */
15146       cp_lexer_consume_token (parser->lexer);
15147       /* Set the SCOPE so that we know where to start the lookup.  */
15148       parser->scope = global_namespace;
15149       parser->qualifying_scope = global_namespace;
15150       parser->object_scope = NULL_TREE;
15151
15152       return parser->scope;
15153     }
15154   else if (!current_scope_valid_p)
15155     {
15156       parser->scope = NULL_TREE;
15157       parser->qualifying_scope = NULL_TREE;
15158       parser->object_scope = NULL_TREE;
15159     }
15160
15161   return NULL_TREE;
15162 }
15163
15164 /* Returns TRUE if the upcoming token sequence is the start of a
15165    constructor declarator.  If FRIEND_P is true, the declarator is
15166    preceded by the `friend' specifier.  */
15167
15168 static bool
15169 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15170 {
15171   bool constructor_p;
15172   tree type_decl = NULL_TREE;
15173   bool nested_name_p;
15174   cp_token *next_token;
15175
15176   /* The common case is that this is not a constructor declarator, so
15177      try to avoid doing lots of work if at all possible.  It's not
15178      valid declare a constructor at function scope.  */
15179   if (at_function_scope_p ())
15180     return false;
15181   /* And only certain tokens can begin a constructor declarator.  */
15182   next_token = cp_lexer_peek_token (parser->lexer);
15183   if (next_token->type != CPP_NAME
15184       && next_token->type != CPP_SCOPE
15185       && next_token->type != CPP_NESTED_NAME_SPECIFIER
15186       && next_token->type != CPP_TEMPLATE_ID)
15187     return false;
15188
15189   /* Parse tentatively; we are going to roll back all of the tokens
15190      consumed here.  */
15191   cp_parser_parse_tentatively (parser);
15192   /* Assume that we are looking at a constructor declarator.  */
15193   constructor_p = true;
15194
15195   /* Look for the optional `::' operator.  */
15196   cp_parser_global_scope_opt (parser,
15197                               /*current_scope_valid_p=*/false);
15198   /* Look for the nested-name-specifier.  */
15199   nested_name_p
15200     = (cp_parser_nested_name_specifier_opt (parser,
15201                                             /*typename_keyword_p=*/false,
15202                                             /*check_dependency_p=*/false,
15203                                             /*type_p=*/false,
15204                                             /*is_declaration=*/false)
15205        != NULL_TREE);
15206   /* Outside of a class-specifier, there must be a
15207      nested-name-specifier.  */
15208   if (!nested_name_p &&
15209       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15210        || friend_p))
15211     constructor_p = false;
15212   /* If we still think that this might be a constructor-declarator,
15213      look for a class-name.  */
15214   if (constructor_p)
15215     {
15216       /* If we have:
15217
15218            template <typename T> struct S { S(); };
15219            template <typename T> S<T>::S ();
15220
15221          we must recognize that the nested `S' names a class.
15222          Similarly, for:
15223
15224            template <typename T> S<T>::S<T> ();
15225
15226          we must recognize that the nested `S' names a template.  */
15227       type_decl = cp_parser_class_name (parser,
15228                                         /*typename_keyword_p=*/false,
15229                                         /*template_keyword_p=*/false,
15230                                         none_type,
15231                                         /*check_dependency_p=*/false,
15232                                         /*class_head_p=*/false,
15233                                         /*is_declaration=*/false);
15234       /* If there was no class-name, then this is not a constructor.  */
15235       constructor_p = !cp_parser_error_occurred (parser);
15236     }
15237
15238   /* If we're still considering a constructor, we have to see a `(',
15239      to begin the parameter-declaration-clause, followed by either a
15240      `)', an `...', or a decl-specifier.  We need to check for a
15241      type-specifier to avoid being fooled into thinking that:
15242
15243        S::S (f) (int);
15244
15245      is a constructor.  (It is actually a function named `f' that
15246      takes one parameter (of type `int') and returns a value of type
15247      `S::S'.  */
15248   if (constructor_p
15249       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15250     {
15251       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15252           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15253           /* A parameter declaration begins with a decl-specifier,
15254              which is either the "attribute" keyword, a storage class
15255              specifier, or (usually) a type-specifier.  */
15256           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15257           && !cp_parser_storage_class_specifier_opt (parser))
15258         {
15259           tree type;
15260           tree pushed_scope = NULL_TREE;
15261           unsigned saved_num_template_parameter_lists;
15262
15263           /* Names appearing in the type-specifier should be looked up
15264              in the scope of the class.  */
15265           if (current_class_type)
15266             type = NULL_TREE;
15267           else
15268             {
15269               type = TREE_TYPE (type_decl);
15270               if (TREE_CODE (type) == TYPENAME_TYPE)
15271                 {
15272                   type = resolve_typename_type (type,
15273                                                 /*only_current_p=*/false);
15274                   if (type == error_mark_node)
15275                     {
15276                       cp_parser_abort_tentative_parse (parser);
15277                       return false;
15278                     }
15279                 }
15280               pushed_scope = push_scope (type);
15281             }
15282
15283           /* Inside the constructor parameter list, surrounding
15284              template-parameter-lists do not apply.  */
15285           saved_num_template_parameter_lists
15286             = parser->num_template_parameter_lists;
15287           parser->num_template_parameter_lists = 0;
15288
15289           /* Look for the type-specifier.  */
15290           cp_parser_type_specifier (parser,
15291                                     CP_PARSER_FLAGS_NONE,
15292                                     /*decl_specs=*/NULL,
15293                                     /*is_declarator=*/true,
15294                                     /*declares_class_or_enum=*/NULL,
15295                                     /*is_cv_qualifier=*/NULL);
15296
15297           parser->num_template_parameter_lists
15298             = saved_num_template_parameter_lists;
15299
15300           /* Leave the scope of the class.  */
15301           if (pushed_scope)
15302             pop_scope (pushed_scope);
15303
15304           constructor_p = !cp_parser_error_occurred (parser);
15305         }
15306     }
15307   else
15308     constructor_p = false;
15309   /* We did not really want to consume any tokens.  */
15310   cp_parser_abort_tentative_parse (parser);
15311
15312   return constructor_p;
15313 }
15314
15315 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15316    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15317    they must be performed once we are in the scope of the function.
15318
15319    Returns the function defined.  */
15320
15321 static tree
15322 cp_parser_function_definition_from_specifiers_and_declarator
15323   (cp_parser* parser,
15324    cp_decl_specifier_seq *decl_specifiers,
15325    tree attributes,
15326    const cp_declarator *declarator)
15327 {
15328   tree fn;
15329   bool success_p;
15330
15331   /* Begin the function-definition.  */
15332   success_p = start_function (decl_specifiers, declarator, attributes);
15333
15334   /* The things we're about to see are not directly qualified by any
15335      template headers we've seen thus far.  */
15336   reset_specialization ();
15337
15338   /* If there were names looked up in the decl-specifier-seq that we
15339      did not check, check them now.  We must wait until we are in the
15340      scope of the function to perform the checks, since the function
15341      might be a friend.  */
15342   perform_deferred_access_checks ();
15343
15344   if (!success_p)
15345     {
15346       /* Skip the entire function.  */
15347       cp_parser_skip_to_end_of_block_or_statement (parser);
15348       fn = error_mark_node;
15349     }
15350   else
15351     fn = cp_parser_function_definition_after_declarator (parser,
15352                                                          /*inline_p=*/false);
15353
15354   return fn;
15355 }
15356
15357 /* Parse the part of a function-definition that follows the
15358    declarator.  INLINE_P is TRUE iff this function is an inline
15359    function defined with a class-specifier.
15360
15361    Returns the function defined.  */
15362
15363 static tree
15364 cp_parser_function_definition_after_declarator (cp_parser* parser,
15365                                                 bool inline_p)
15366 {
15367   tree fn;
15368   bool ctor_initializer_p = false;
15369   bool saved_in_unbraced_linkage_specification_p;
15370   unsigned saved_num_template_parameter_lists;
15371
15372   /* If the next token is `return', then the code may be trying to
15373      make use of the "named return value" extension that G++ used to
15374      support.  */
15375   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15376     {
15377       /* Consume the `return' keyword.  */
15378       cp_lexer_consume_token (parser->lexer);
15379       /* Look for the identifier that indicates what value is to be
15380          returned.  */
15381       cp_parser_identifier (parser);
15382       /* Issue an error message.  */
15383       error ("named return values are no longer supported");
15384       /* Skip tokens until we reach the start of the function body.  */
15385       while (true)
15386         {
15387           cp_token *token = cp_lexer_peek_token (parser->lexer);
15388           if (token->type == CPP_OPEN_BRACE
15389               || token->type == CPP_EOF
15390               || token->type == CPP_PRAGMA_EOL)
15391             break;
15392           cp_lexer_consume_token (parser->lexer);
15393         }
15394     }
15395   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15396      anything declared inside `f'.  */
15397   saved_in_unbraced_linkage_specification_p
15398     = parser->in_unbraced_linkage_specification_p;
15399   parser->in_unbraced_linkage_specification_p = false;
15400   /* Inside the function, surrounding template-parameter-lists do not
15401      apply.  */
15402   saved_num_template_parameter_lists
15403     = parser->num_template_parameter_lists;
15404   parser->num_template_parameter_lists = 0;
15405   /* If the next token is `try', then we are looking at a
15406      function-try-block.  */
15407   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15408     ctor_initializer_p = cp_parser_function_try_block (parser);
15409   /* A function-try-block includes the function-body, so we only do
15410      this next part if we're not processing a function-try-block.  */
15411   else
15412     ctor_initializer_p
15413       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15414
15415   /* Finish the function.  */
15416   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15417                         (inline_p ? 2 : 0));
15418   /* Generate code for it, if necessary.  */
15419   expand_or_defer_fn (fn);
15420   /* Restore the saved values.  */
15421   parser->in_unbraced_linkage_specification_p
15422     = saved_in_unbraced_linkage_specification_p;
15423   parser->num_template_parameter_lists
15424     = saved_num_template_parameter_lists;
15425
15426   return fn;
15427 }
15428
15429 /* Parse a template-declaration, assuming that the `export' (and
15430    `extern') keywords, if present, has already been scanned.  MEMBER_P
15431    is as for cp_parser_template_declaration.  */
15432
15433 static void
15434 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15435 {
15436   tree decl = NULL_TREE;
15437   tree checks;
15438   tree parameter_list;
15439   bool friend_p = false;
15440   bool need_lang_pop;
15441
15442   /* Look for the `template' keyword.  */
15443   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15444     return;
15445
15446   /* And the `<'.  */
15447   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15448     return;
15449   /* [temp]
15450
15451      A template ... shall not have C linkage.  */
15452   if (current_lang_name == lang_name_c)
15453     {
15454       error ("template with C linkage");
15455       /* Give it C++ linkage to avoid confusing other parts of the
15456          front end.  */
15457       push_lang_context (lang_name_cplusplus);
15458       need_lang_pop = true;
15459     }
15460   else
15461     need_lang_pop = false;
15462
15463   /* We cannot perform access checks on the template parameter
15464      declarations until we know what is being declared, just as we
15465      cannot check the decl-specifier list.  */
15466   push_deferring_access_checks (dk_deferred);
15467
15468   /* If the next token is `>', then we have an invalid
15469      specialization.  Rather than complain about an invalid template
15470      parameter, issue an error message here.  */
15471   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15472     {
15473       cp_parser_error (parser, "invalid explicit specialization");
15474       begin_specialization ();
15475       parameter_list = NULL_TREE;
15476     }
15477   else
15478     /* Parse the template parameters.  */
15479     parameter_list = cp_parser_template_parameter_list (parser);
15480
15481   /* Get the deferred access checks from the parameter list.  These
15482      will be checked once we know what is being declared, as for a
15483      member template the checks must be performed in the scope of the
15484      class containing the member.  */
15485   checks = get_deferred_access_checks ();
15486
15487   /* Look for the `>'.  */
15488   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15489   /* We just processed one more parameter list.  */
15490   ++parser->num_template_parameter_lists;
15491   /* If the next token is `template', there are more template
15492      parameters.  */
15493   if (cp_lexer_next_token_is_keyword (parser->lexer,
15494                                       RID_TEMPLATE))
15495     cp_parser_template_declaration_after_export (parser, member_p);
15496   else
15497     {
15498       /* There are no access checks when parsing a template, as we do not
15499          know if a specialization will be a friend.  */
15500       push_deferring_access_checks (dk_no_check);
15501       decl = cp_parser_single_declaration (parser,
15502                                            checks,
15503                                            member_p,
15504                                            &friend_p);
15505       pop_deferring_access_checks ();
15506
15507       /* If this is a member template declaration, let the front
15508          end know.  */
15509       if (member_p && !friend_p && decl)
15510         {
15511           if (TREE_CODE (decl) == TYPE_DECL)
15512             cp_parser_check_access_in_redeclaration (decl);
15513
15514           decl = finish_member_template_decl (decl);
15515         }
15516       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15517         make_friend_class (current_class_type, TREE_TYPE (decl),
15518                            /*complain=*/true);
15519     }
15520   /* We are done with the current parameter list.  */
15521   --parser->num_template_parameter_lists;
15522
15523   pop_deferring_access_checks ();
15524
15525   /* Finish up.  */
15526   finish_template_decl (parameter_list);
15527
15528   /* Register member declarations.  */
15529   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15530     finish_member_declaration (decl);
15531   /* For the erroneous case of a template with C linkage, we pushed an
15532      implicit C++ linkage scope; exit that scope now.  */
15533   if (need_lang_pop)
15534     pop_lang_context ();
15535   /* If DECL is a function template, we must return to parse it later.
15536      (Even though there is no definition, there might be default
15537      arguments that need handling.)  */
15538   if (member_p && decl
15539       && (TREE_CODE (decl) == FUNCTION_DECL
15540           || DECL_FUNCTION_TEMPLATE_P (decl)))
15541     TREE_VALUE (parser->unparsed_functions_queues)
15542       = tree_cons (NULL_TREE, decl,
15543                    TREE_VALUE (parser->unparsed_functions_queues));
15544 }
15545
15546 /* Perform the deferred access checks from a template-parameter-list.
15547    CHECKS is a TREE_LIST of access checks, as returned by
15548    get_deferred_access_checks.  */
15549
15550 static void
15551 cp_parser_perform_template_parameter_access_checks (tree checks)
15552 {
15553   ++processing_template_parmlist;
15554   perform_access_checks (checks);
15555   --processing_template_parmlist;
15556 }
15557
15558 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15559    `function-definition' sequence.  MEMBER_P is true, this declaration
15560    appears in a class scope.
15561
15562    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15563    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15564
15565 static tree
15566 cp_parser_single_declaration (cp_parser* parser,
15567                               tree checks,
15568                               bool member_p,
15569                               bool* friend_p)
15570 {
15571   int declares_class_or_enum;
15572   tree decl = NULL_TREE;
15573   cp_decl_specifier_seq decl_specifiers;
15574   bool function_definition_p = false;
15575
15576   /* This function is only used when processing a template
15577      declaration.  */
15578   gcc_assert (innermost_scope_kind () == sk_template_parms
15579               || innermost_scope_kind () == sk_template_spec);
15580
15581   /* Defer access checks until we know what is being declared.  */
15582   push_deferring_access_checks (dk_deferred);
15583
15584   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15585      alternative.  */
15586   cp_parser_decl_specifier_seq (parser,
15587                                 CP_PARSER_FLAGS_OPTIONAL,
15588                                 &decl_specifiers,
15589                                 &declares_class_or_enum);
15590   if (friend_p)
15591     *friend_p = cp_parser_friend_p (&decl_specifiers);
15592
15593   /* There are no template typedefs.  */
15594   if (decl_specifiers.specs[(int) ds_typedef])
15595     {
15596       error ("template declaration of %qs", "typedef");
15597       decl = error_mark_node;
15598     }
15599
15600   /* Gather up the access checks that occurred the
15601      decl-specifier-seq.  */
15602   stop_deferring_access_checks ();
15603
15604   /* Check for the declaration of a template class.  */
15605   if (declares_class_or_enum)
15606     {
15607       if (cp_parser_declares_only_class_p (parser))
15608         {
15609           decl = shadow_tag (&decl_specifiers);
15610
15611           /* In this case:
15612
15613                struct C {
15614                  friend template <typename T> struct A<T>::B;
15615                };
15616
15617              A<T>::B will be represented by a TYPENAME_TYPE, and
15618              therefore not recognized by shadow_tag.  */
15619           if (friend_p && *friend_p
15620               && !decl
15621               && decl_specifiers.type
15622               && TYPE_P (decl_specifiers.type))
15623             decl = decl_specifiers.type;
15624
15625           if (decl && decl != error_mark_node)
15626             decl = TYPE_NAME (decl);
15627           else
15628             decl = error_mark_node;
15629
15630           /* Perform access checks for template parameters.  */
15631           cp_parser_perform_template_parameter_access_checks (checks);
15632         }
15633     }
15634   /* If it's not a template class, try for a template function.  If
15635      the next token is a `;', then this declaration does not declare
15636      anything.  But, if there were errors in the decl-specifiers, then
15637      the error might well have come from an attempted class-specifier.
15638      In that case, there's no need to warn about a missing declarator.  */
15639   if (!decl
15640       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15641           || decl_specifiers.type != error_mark_node))
15642     decl = cp_parser_init_declarator (parser,
15643                                       &decl_specifiers,
15644                                       checks,
15645                                       /*function_definition_allowed_p=*/true,
15646                                       member_p,
15647                                       declares_class_or_enum,
15648                                       &function_definition_p);
15649
15650   pop_deferring_access_checks ();
15651
15652   /* Clear any current qualification; whatever comes next is the start
15653      of something new.  */
15654   parser->scope = NULL_TREE;
15655   parser->qualifying_scope = NULL_TREE;
15656   parser->object_scope = NULL_TREE;
15657   /* Look for a trailing `;' after the declaration.  */
15658   if (!function_definition_p
15659       && (decl == error_mark_node
15660           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15661     cp_parser_skip_to_end_of_block_or_statement (parser);
15662
15663   return decl;
15664 }
15665
15666 /* Parse a cast-expression that is not the operand of a unary "&".  */
15667
15668 static tree
15669 cp_parser_simple_cast_expression (cp_parser *parser)
15670 {
15671   return cp_parser_cast_expression (parser, /*address_p=*/false,
15672                                     /*cast_p=*/false);
15673 }
15674
15675 /* Parse a functional cast to TYPE.  Returns an expression
15676    representing the cast.  */
15677
15678 static tree
15679 cp_parser_functional_cast (cp_parser* parser, tree type)
15680 {
15681   tree expression_list;
15682   tree cast;
15683
15684   expression_list
15685     = cp_parser_parenthesized_expression_list (parser, false,
15686                                                /*cast_p=*/true,
15687                                                /*non_constant_p=*/NULL);
15688
15689   cast = build_functional_cast (type, expression_list);
15690   /* [expr.const]/1: In an integral constant expression "only type
15691      conversions to integral or enumeration type can be used".  */
15692   if (TREE_CODE (type) == TYPE_DECL)
15693     type = TREE_TYPE (type);
15694   if (cast != error_mark_node && !dependent_type_p (type)
15695       && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15696     {
15697       if (cp_parser_non_integral_constant_expression
15698           (parser, "a call to a constructor"))
15699         return error_mark_node;
15700     }
15701   return cast;
15702 }
15703
15704 /* Save the tokens that make up the body of a member function defined
15705    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15706    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15707    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15708    for the member function.  */
15709
15710 static tree
15711 cp_parser_save_member_function_body (cp_parser* parser,
15712                                      cp_decl_specifier_seq *decl_specifiers,
15713                                      cp_declarator *declarator,
15714                                      tree attributes)
15715 {
15716   cp_token *first;
15717   cp_token *last;
15718   tree fn;
15719
15720   /* Create the function-declaration.  */
15721   fn = start_method (decl_specifiers, declarator, attributes);
15722   /* If something went badly wrong, bail out now.  */
15723   if (fn == error_mark_node)
15724     {
15725       /* If there's a function-body, skip it.  */
15726       if (cp_parser_token_starts_function_definition_p
15727           (cp_lexer_peek_token (parser->lexer)))
15728         cp_parser_skip_to_end_of_block_or_statement (parser);
15729       return error_mark_node;
15730     }
15731
15732   /* Remember it, if there default args to post process.  */
15733   cp_parser_save_default_args (parser, fn);
15734
15735   /* Save away the tokens that make up the body of the
15736      function.  */
15737   first = parser->lexer->next_token;
15738   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15739   /* Handle function try blocks.  */
15740   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15741     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15742   last = parser->lexer->next_token;
15743
15744   /* Save away the inline definition; we will process it when the
15745      class is complete.  */
15746   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15747   DECL_PENDING_INLINE_P (fn) = 1;
15748
15749   /* We need to know that this was defined in the class, so that
15750      friend templates are handled correctly.  */
15751   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15752
15753   /* We're done with the inline definition.  */
15754   finish_method (fn);
15755
15756   /* Add FN to the queue of functions to be parsed later.  */
15757   TREE_VALUE (parser->unparsed_functions_queues)
15758     = tree_cons (NULL_TREE, fn,
15759                  TREE_VALUE (parser->unparsed_functions_queues));
15760
15761   return fn;
15762 }
15763
15764 /* Parse a template-argument-list, as well as the trailing ">" (but
15765    not the opening ">").  See cp_parser_template_argument_list for the
15766    return value.  */
15767
15768 static tree
15769 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15770 {
15771   tree arguments;
15772   tree saved_scope;
15773   tree saved_qualifying_scope;
15774   tree saved_object_scope;
15775   bool saved_greater_than_is_operator_p;
15776   bool saved_skip_evaluation;
15777
15778   /* [temp.names]
15779
15780      When parsing a template-id, the first non-nested `>' is taken as
15781      the end of the template-argument-list rather than a greater-than
15782      operator.  */
15783   saved_greater_than_is_operator_p
15784     = parser->greater_than_is_operator_p;
15785   parser->greater_than_is_operator_p = false;
15786   /* Parsing the argument list may modify SCOPE, so we save it
15787      here.  */
15788   saved_scope = parser->scope;
15789   saved_qualifying_scope = parser->qualifying_scope;
15790   saved_object_scope = parser->object_scope;
15791   /* We need to evaluate the template arguments, even though this
15792      template-id may be nested within a "sizeof".  */
15793   saved_skip_evaluation = skip_evaluation;
15794   skip_evaluation = false;
15795   /* Parse the template-argument-list itself.  */
15796   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15797     arguments = NULL_TREE;
15798   else
15799     arguments = cp_parser_template_argument_list (parser);
15800   /* Look for the `>' that ends the template-argument-list. If we find
15801      a '>>' instead, it's probably just a typo.  */
15802   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15803     {
15804       if (!saved_greater_than_is_operator_p)
15805         {
15806           /* If we're in a nested template argument list, the '>>' has
15807             to be a typo for '> >'. We emit the error message, but we
15808             continue parsing and we push a '>' as next token, so that
15809             the argument list will be parsed correctly.  Note that the
15810             global source location is still on the token before the
15811             '>>', so we need to say explicitly where we want it.  */
15812           cp_token *token = cp_lexer_peek_token (parser->lexer);
15813           error ("%H%<>>%> should be %<> >%> "
15814                  "within a nested template argument list",
15815                  &token->location);
15816
15817           /* ??? Proper recovery should terminate two levels of
15818              template argument list here.  */
15819           token->type = CPP_GREATER;
15820         }
15821       else
15822         {
15823           /* If this is not a nested template argument list, the '>>'
15824             is a typo for '>'. Emit an error message and continue.
15825             Same deal about the token location, but here we can get it
15826             right by consuming the '>>' before issuing the diagnostic.  */
15827           cp_lexer_consume_token (parser->lexer);
15828           error ("spurious %<>>%>, use %<>%> to terminate "
15829                  "a template argument list");
15830         }
15831     }
15832   else
15833     cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15834   /* The `>' token might be a greater-than operator again now.  */
15835   parser->greater_than_is_operator_p
15836     = saved_greater_than_is_operator_p;
15837   /* Restore the SAVED_SCOPE.  */
15838   parser->scope = saved_scope;
15839   parser->qualifying_scope = saved_qualifying_scope;
15840   parser->object_scope = saved_object_scope;
15841   skip_evaluation = saved_skip_evaluation;
15842
15843   return arguments;
15844 }
15845
15846 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15847    arguments, or the body of the function have not yet been parsed,
15848    parse them now.  */
15849
15850 static void
15851 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15852 {
15853   /* If this member is a template, get the underlying
15854      FUNCTION_DECL.  */
15855   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15856     member_function = DECL_TEMPLATE_RESULT (member_function);
15857
15858   /* There should not be any class definitions in progress at this
15859      point; the bodies of members are only parsed outside of all class
15860      definitions.  */
15861   gcc_assert (parser->num_classes_being_defined == 0);
15862   /* While we're parsing the member functions we might encounter more
15863      classes.  We want to handle them right away, but we don't want
15864      them getting mixed up with functions that are currently in the
15865      queue.  */
15866   parser->unparsed_functions_queues
15867     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15868
15869   /* Make sure that any template parameters are in scope.  */
15870   maybe_begin_member_template_processing (member_function);
15871
15872   /* If the body of the function has not yet been parsed, parse it
15873      now.  */
15874   if (DECL_PENDING_INLINE_P (member_function))
15875     {
15876       tree function_scope;
15877       cp_token_cache *tokens;
15878
15879       /* The function is no longer pending; we are processing it.  */
15880       tokens = DECL_PENDING_INLINE_INFO (member_function);
15881       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15882       DECL_PENDING_INLINE_P (member_function) = 0;
15883
15884       /* If this is a local class, enter the scope of the containing
15885          function.  */
15886       function_scope = current_function_decl;
15887       if (function_scope)
15888         push_function_context_to (function_scope);
15889
15890
15891       /* Push the body of the function onto the lexer stack.  */
15892       cp_parser_push_lexer_for_tokens (parser, tokens);
15893
15894       /* Let the front end know that we going to be defining this
15895          function.  */
15896       start_preparsed_function (member_function, NULL_TREE,
15897                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15898
15899       /* Don't do access checking if it is a templated function.  */
15900       if (processing_template_decl)
15901         push_deferring_access_checks (dk_no_check);
15902
15903       /* Now, parse the body of the function.  */
15904       cp_parser_function_definition_after_declarator (parser,
15905                                                       /*inline_p=*/true);
15906
15907       if (processing_template_decl)
15908         pop_deferring_access_checks ();
15909
15910       /* Leave the scope of the containing function.  */
15911       if (function_scope)
15912         pop_function_context_from (function_scope);
15913       cp_parser_pop_lexer (parser);
15914     }
15915
15916   /* Remove any template parameters from the symbol table.  */
15917   maybe_end_member_template_processing ();
15918
15919   /* Restore the queue.  */
15920   parser->unparsed_functions_queues
15921     = TREE_CHAIN (parser->unparsed_functions_queues);
15922 }
15923
15924 /* If DECL contains any default args, remember it on the unparsed
15925    functions queue.  */
15926
15927 static void
15928 cp_parser_save_default_args (cp_parser* parser, tree decl)
15929 {
15930   tree probe;
15931
15932   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15933        probe;
15934        probe = TREE_CHAIN (probe))
15935     if (TREE_PURPOSE (probe))
15936       {
15937         TREE_PURPOSE (parser->unparsed_functions_queues)
15938           = tree_cons (current_class_type, decl,
15939                        TREE_PURPOSE (parser->unparsed_functions_queues));
15940         break;
15941       }
15942 }
15943
15944 /* FN is a FUNCTION_DECL which may contains a parameter with an
15945    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15946    assumes that the current scope is the scope in which the default
15947    argument should be processed.  */
15948
15949 static void
15950 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15951 {
15952   bool saved_local_variables_forbidden_p;
15953   tree parm;
15954
15955   /* While we're parsing the default args, we might (due to the
15956      statement expression extension) encounter more classes.  We want
15957      to handle them right away, but we don't want them getting mixed
15958      up with default args that are currently in the queue.  */
15959   parser->unparsed_functions_queues
15960     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15961
15962   /* Local variable names (and the `this' keyword) may not appear
15963      in a default argument.  */
15964   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15965   parser->local_variables_forbidden_p = true;
15966
15967   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15968        parm;
15969        parm = TREE_CHAIN (parm))
15970     {
15971       cp_token_cache *tokens;
15972       tree default_arg = TREE_PURPOSE (parm);
15973       tree parsed_arg;
15974       VEC(tree,gc) *insts;
15975       tree copy;
15976       unsigned ix;
15977
15978       if (!default_arg)
15979         continue;
15980
15981       if (TREE_CODE (default_arg) != DEFAULT_ARG)
15982         /* This can happen for a friend declaration for a function
15983            already declared with default arguments.  */
15984         continue;
15985
15986        /* Push the saved tokens for the default argument onto the parser's
15987           lexer stack.  */
15988       tokens = DEFARG_TOKENS (default_arg);
15989       cp_parser_push_lexer_for_tokens (parser, tokens);
15990
15991       /* Parse the assignment-expression.  */
15992       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15993
15994       if (!processing_template_decl)
15995         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
15996
15997       TREE_PURPOSE (parm) = parsed_arg;
15998
15999       /* Update any instantiations we've already created.  */
16000       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16001            VEC_iterate (tree, insts, ix, copy); ix++)
16002         TREE_PURPOSE (copy) = parsed_arg;
16003
16004       /* If the token stream has not been completely used up, then
16005          there was extra junk after the end of the default
16006          argument.  */
16007       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16008         cp_parser_error (parser, "expected %<,%>");
16009
16010       /* Revert to the main lexer.  */
16011       cp_parser_pop_lexer (parser);
16012     }
16013
16014   /* Make sure no default arg is missing.  */
16015   check_default_args (fn);
16016
16017   /* Restore the state of local_variables_forbidden_p.  */
16018   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16019
16020   /* Restore the queue.  */
16021   parser->unparsed_functions_queues
16022     = TREE_CHAIN (parser->unparsed_functions_queues);
16023 }
16024
16025 /* Parse the operand of `sizeof' (or a similar operator).  Returns
16026    either a TYPE or an expression, depending on the form of the
16027    input.  The KEYWORD indicates which kind of expression we have
16028    encountered.  */
16029
16030 static tree
16031 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16032 {
16033   static const char *format;
16034   tree expr = NULL_TREE;
16035   const char *saved_message;
16036   bool saved_integral_constant_expression_p;
16037   bool saved_non_integral_constant_expression_p;
16038
16039   /* Initialize FORMAT the first time we get here.  */
16040   if (!format)
16041     format = "types may not be defined in '%s' expressions";
16042
16043   /* Types cannot be defined in a `sizeof' expression.  Save away the
16044      old message.  */
16045   saved_message = parser->type_definition_forbidden_message;
16046   /* And create the new one.  */
16047   parser->type_definition_forbidden_message
16048     = XNEWVEC (const char, strlen (format)
16049                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16050                + 1 /* `\0' */);
16051   sprintf ((char *) parser->type_definition_forbidden_message,
16052            format, IDENTIFIER_POINTER (ridpointers[keyword]));
16053
16054   /* The restrictions on constant-expressions do not apply inside
16055      sizeof expressions.  */
16056   saved_integral_constant_expression_p
16057     = parser->integral_constant_expression_p;
16058   saved_non_integral_constant_expression_p
16059     = parser->non_integral_constant_expression_p;
16060   parser->integral_constant_expression_p = false;
16061
16062   /* Do not actually evaluate the expression.  */
16063   ++skip_evaluation;
16064   /* If it's a `(', then we might be looking at the type-id
16065      construction.  */
16066   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16067     {
16068       tree type;
16069       bool saved_in_type_id_in_expr_p;
16070
16071       /* We can't be sure yet whether we're looking at a type-id or an
16072          expression.  */
16073       cp_parser_parse_tentatively (parser);
16074       /* Consume the `('.  */
16075       cp_lexer_consume_token (parser->lexer);
16076       /* Parse the type-id.  */
16077       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16078       parser->in_type_id_in_expr_p = true;
16079       type = cp_parser_type_id (parser);
16080       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16081       /* Now, look for the trailing `)'.  */
16082       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16083       /* If all went well, then we're done.  */
16084       if (cp_parser_parse_definitely (parser))
16085         {
16086           cp_decl_specifier_seq decl_specs;
16087
16088           /* Build a trivial decl-specifier-seq.  */
16089           clear_decl_specs (&decl_specs);
16090           decl_specs.type = type;
16091
16092           /* Call grokdeclarator to figure out what type this is.  */
16093           expr = grokdeclarator (NULL,
16094                                  &decl_specs,
16095                                  TYPENAME,
16096                                  /*initialized=*/0,
16097                                  /*attrlist=*/NULL);
16098         }
16099     }
16100
16101   /* If the type-id production did not work out, then we must be
16102      looking at the unary-expression production.  */
16103   if (!expr)
16104     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16105                                        /*cast_p=*/false);
16106   /* Go back to evaluating expressions.  */
16107   --skip_evaluation;
16108
16109   /* Free the message we created.  */
16110   free ((char *) parser->type_definition_forbidden_message);
16111   /* And restore the old one.  */
16112   parser->type_definition_forbidden_message = saved_message;
16113   parser->integral_constant_expression_p
16114     = saved_integral_constant_expression_p;
16115   parser->non_integral_constant_expression_p
16116     = saved_non_integral_constant_expression_p;
16117
16118   return expr;
16119 }
16120
16121 /* If the current declaration has no declarator, return true.  */
16122
16123 static bool
16124 cp_parser_declares_only_class_p (cp_parser *parser)
16125 {
16126   /* If the next token is a `;' or a `,' then there is no
16127      declarator.  */
16128   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16129           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16130 }
16131
16132 /* Update the DECL_SPECS to reflect the storage class indicated by
16133    KEYWORD.  */
16134
16135 static void
16136 cp_parser_set_storage_class (cp_parser *parser,
16137                              cp_decl_specifier_seq *decl_specs,
16138                              enum rid keyword)
16139 {
16140   cp_storage_class storage_class;
16141
16142   if (parser->in_unbraced_linkage_specification_p)
16143     {
16144       error ("invalid use of %qD in linkage specification",
16145              ridpointers[keyword]);
16146       return;
16147     }
16148   else if (decl_specs->storage_class != sc_none)
16149     {
16150       decl_specs->multiple_storage_classes_p = true;
16151       return;
16152     }
16153
16154   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16155       && decl_specs->specs[(int) ds_thread])
16156     {
16157       error ("%<__thread%> before %qD", ridpointers[keyword]);
16158       decl_specs->specs[(int) ds_thread] = 0;
16159     }
16160
16161   switch (keyword)
16162     {
16163     case RID_AUTO:
16164       storage_class = sc_auto;
16165       break;
16166     case RID_REGISTER:
16167       storage_class = sc_register;
16168       break;
16169     case RID_STATIC:
16170       storage_class = sc_static;
16171       break;
16172     case RID_EXTERN:
16173       storage_class = sc_extern;
16174       break;
16175     case RID_MUTABLE:
16176       storage_class = sc_mutable;
16177       break;
16178     default:
16179       gcc_unreachable ();
16180     }
16181   decl_specs->storage_class = storage_class;
16182 }
16183
16184 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
16185    is true, the type is a user-defined type; otherwise it is a
16186    built-in type specified by a keyword.  */
16187
16188 static void
16189 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16190                               tree type_spec,
16191                               bool user_defined_p)
16192 {
16193   decl_specs->any_specifiers_p = true;
16194
16195   /* If the user tries to redeclare bool or wchar_t (with, for
16196      example, in "typedef int wchar_t;") we remember that this is what
16197      happened.  In system headers, we ignore these declarations so
16198      that G++ can work with system headers that are not C++-safe.  */
16199   if (decl_specs->specs[(int) ds_typedef]
16200       && !user_defined_p
16201       && (type_spec == boolean_type_node
16202           || type_spec == wchar_type_node)
16203       && (decl_specs->type
16204           || decl_specs->specs[(int) ds_long]
16205           || decl_specs->specs[(int) ds_short]
16206           || decl_specs->specs[(int) ds_unsigned]
16207           || decl_specs->specs[(int) ds_signed]))
16208     {
16209       decl_specs->redefined_builtin_type = type_spec;
16210       if (!decl_specs->type)
16211         {
16212           decl_specs->type = type_spec;
16213           decl_specs->user_defined_type_p = false;
16214         }
16215     }
16216   else if (decl_specs->type)
16217     decl_specs->multiple_types_p = true;
16218   else
16219     {
16220       decl_specs->type = type_spec;
16221       decl_specs->user_defined_type_p = user_defined_p;
16222       decl_specs->redefined_builtin_type = NULL_TREE;
16223     }
16224 }
16225
16226 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16227    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
16228
16229 static bool
16230 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16231 {
16232   return decl_specifiers->specs[(int) ds_friend] != 0;
16233 }
16234
16235 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
16236    issue an error message indicating that TOKEN_DESC was expected.
16237
16238    Returns the token consumed, if the token had the appropriate type.
16239    Otherwise, returns NULL.  */
16240
16241 static cp_token *
16242 cp_parser_require (cp_parser* parser,
16243                    enum cpp_ttype type,
16244                    const char* token_desc)
16245 {
16246   if (cp_lexer_next_token_is (parser->lexer, type))
16247     return cp_lexer_consume_token (parser->lexer);
16248   else
16249     {
16250       /* Output the MESSAGE -- unless we're parsing tentatively.  */
16251       if (!cp_parser_simulate_error (parser))
16252         {
16253           char *message = concat ("expected ", token_desc, NULL);
16254           cp_parser_error (parser, message);
16255           free (message);
16256         }
16257       return NULL;
16258     }
16259 }
16260
16261 /* Like cp_parser_require, except that tokens will be skipped until
16262    the desired token is found.  An error message is still produced if
16263    the next token is not as expected.  */
16264
16265 static void
16266 cp_parser_skip_until_found (cp_parser* parser,
16267                             enum cpp_ttype type,
16268                             const char* token_desc)
16269 {
16270   cp_token *token;
16271   unsigned nesting_depth = 0;
16272
16273   if (cp_parser_require (parser, type, token_desc))
16274     return;
16275
16276   /* Skip tokens until the desired token is found.  */
16277   while (true)
16278     {
16279       /* Peek at the next token.  */
16280       token = cp_lexer_peek_token (parser->lexer);
16281
16282       /* If we've reached the token we want, consume it and stop.  */
16283       if (token->type == type && !nesting_depth)
16284         {
16285           cp_lexer_consume_token (parser->lexer);
16286           return;
16287         }
16288
16289       switch (token->type)
16290         {
16291         case CPP_EOF:
16292         case CPP_PRAGMA_EOL:
16293           /* If we've run out of tokens, stop.  */
16294           return;
16295
16296         case CPP_OPEN_BRACE:
16297         case CPP_OPEN_PAREN:
16298         case CPP_OPEN_SQUARE:
16299           ++nesting_depth;
16300           break;
16301
16302         case CPP_CLOSE_BRACE:
16303         case CPP_CLOSE_PAREN:
16304         case CPP_CLOSE_SQUARE:
16305           if (nesting_depth-- == 0)
16306             return;
16307           break;
16308
16309         default:
16310           break;
16311         }
16312
16313       /* Consume this token.  */
16314       cp_lexer_consume_token (parser->lexer);
16315     }
16316 }
16317
16318 /* If the next token is the indicated keyword, consume it.  Otherwise,
16319    issue an error message indicating that TOKEN_DESC was expected.
16320
16321    Returns the token consumed, if the token had the appropriate type.
16322    Otherwise, returns NULL.  */
16323
16324 static cp_token *
16325 cp_parser_require_keyword (cp_parser* parser,
16326                            enum rid keyword,
16327                            const char* token_desc)
16328 {
16329   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16330
16331   if (token && token->keyword != keyword)
16332     {
16333       dyn_string_t error_msg;
16334
16335       /* Format the error message.  */
16336       error_msg = dyn_string_new (0);
16337       dyn_string_append_cstr (error_msg, "expected ");
16338       dyn_string_append_cstr (error_msg, token_desc);
16339       cp_parser_error (parser, error_msg->s);
16340       dyn_string_delete (error_msg);
16341       return NULL;
16342     }
16343
16344   return token;
16345 }
16346
16347 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16348    function-definition.  */
16349
16350 static bool
16351 cp_parser_token_starts_function_definition_p (cp_token* token)
16352 {
16353   return (/* An ordinary function-body begins with an `{'.  */
16354           token->type == CPP_OPEN_BRACE
16355           /* A ctor-initializer begins with a `:'.  */
16356           || token->type == CPP_COLON
16357           /* A function-try-block begins with `try'.  */
16358           || token->keyword == RID_TRY
16359           /* The named return value extension begins with `return'.  */
16360           || token->keyword == RID_RETURN);
16361 }
16362
16363 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16364    definition.  */
16365
16366 static bool
16367 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16368 {
16369   cp_token *token;
16370
16371   token = cp_lexer_peek_token (parser->lexer);
16372   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16373 }
16374
16375 /* Returns TRUE iff the next token is the "," or ">" ending a
16376    template-argument.  */
16377
16378 static bool
16379 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16380 {
16381   cp_token *token;
16382
16383   token = cp_lexer_peek_token (parser->lexer);
16384   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16385 }
16386
16387 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16388    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
16389
16390 static bool
16391 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16392                                                      size_t n)
16393 {
16394   cp_token *token;
16395
16396   token = cp_lexer_peek_nth_token (parser->lexer, n);
16397   if (token->type == CPP_LESS)
16398     return true;
16399   /* Check for the sequence `<::' in the original code. It would be lexed as
16400      `[:', where `[' is a digraph, and there is no whitespace before
16401      `:'.  */
16402   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16403     {
16404       cp_token *token2;
16405       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16406       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16407         return true;
16408     }
16409   return false;
16410 }
16411
16412 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16413    or none_type otherwise.  */
16414
16415 static enum tag_types
16416 cp_parser_token_is_class_key (cp_token* token)
16417 {
16418   switch (token->keyword)
16419     {
16420     case RID_CLASS:
16421       return class_type;
16422     case RID_STRUCT:
16423       return record_type;
16424     case RID_UNION:
16425       return union_type;
16426
16427     default:
16428       return none_type;
16429     }
16430 }
16431
16432 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16433
16434 static void
16435 cp_parser_check_class_key (enum tag_types class_key, tree type)
16436 {
16437   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16438     pedwarn ("%qs tag used in naming %q#T",
16439             class_key == union_type ? "union"
16440              : class_key == record_type ? "struct" : "class",
16441              type);
16442 }
16443
16444 /* Issue an error message if DECL is redeclared with different
16445    access than its original declaration [class.access.spec/3].
16446    This applies to nested classes and nested class templates.
16447    [class.mem/1].  */
16448
16449 static void
16450 cp_parser_check_access_in_redeclaration (tree decl)
16451 {
16452   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16453     return;
16454
16455   if ((TREE_PRIVATE (decl)
16456        != (current_access_specifier == access_private_node))
16457       || (TREE_PROTECTED (decl)
16458           != (current_access_specifier == access_protected_node)))
16459     error ("%qD redeclared with different access", decl);
16460 }
16461
16462 /* Look for the `template' keyword, as a syntactic disambiguator.
16463    Return TRUE iff it is present, in which case it will be
16464    consumed.  */
16465
16466 static bool
16467 cp_parser_optional_template_keyword (cp_parser *parser)
16468 {
16469   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16470     {
16471       /* The `template' keyword can only be used within templates;
16472          outside templates the parser can always figure out what is a
16473          template and what is not.  */
16474       if (!processing_template_decl)
16475         {
16476           error ("%<template%> (as a disambiguator) is only allowed "
16477                  "within templates");
16478           /* If this part of the token stream is rescanned, the same
16479              error message would be generated.  So, we purge the token
16480              from the stream.  */
16481           cp_lexer_purge_token (parser->lexer);
16482           return false;
16483         }
16484       else
16485         {
16486           /* Consume the `template' keyword.  */
16487           cp_lexer_consume_token (parser->lexer);
16488           return true;
16489         }
16490     }
16491
16492   return false;
16493 }
16494
16495 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16496    set PARSER->SCOPE, and perform other related actions.  */
16497
16498 static void
16499 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16500 {
16501   tree value;
16502   tree check;
16503
16504   /* Get the stored value.  */
16505   value = cp_lexer_consume_token (parser->lexer)->value;
16506   /* Perform any access checks that were deferred.  */
16507   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16508     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16509   /* Set the scope from the stored value.  */
16510   parser->scope = TREE_VALUE (value);
16511   parser->qualifying_scope = TREE_TYPE (value);
16512   parser->object_scope = NULL_TREE;
16513 }
16514
16515 /* Consume tokens up through a non-nested END token.  */
16516
16517 static void
16518 cp_parser_cache_group (cp_parser *parser,
16519                        enum cpp_ttype end,
16520                        unsigned depth)
16521 {
16522   while (true)
16523     {
16524       cp_token *token;
16525
16526       /* Abort a parenthesized expression if we encounter a brace.  */
16527       if ((end == CPP_CLOSE_PAREN || depth == 0)
16528           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16529         return;
16530       /* If we've reached the end of the file, stop.  */
16531       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16532           || (end != CPP_PRAGMA_EOL
16533               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16534         return;
16535       /* Consume the next token.  */
16536       token = cp_lexer_consume_token (parser->lexer);
16537       /* See if it starts a new group.  */
16538       if (token->type == CPP_OPEN_BRACE)
16539         {
16540           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16541           if (depth == 0)
16542             return;
16543         }
16544       else if (token->type == CPP_OPEN_PAREN)
16545         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16546       else if (token->type == CPP_PRAGMA)
16547         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16548       else if (token->type == end)
16549         return;
16550     }
16551 }
16552
16553 /* Begin parsing tentatively.  We always save tokens while parsing
16554    tentatively so that if the tentative parsing fails we can restore the
16555    tokens.  */
16556
16557 static void
16558 cp_parser_parse_tentatively (cp_parser* parser)
16559 {
16560   /* Enter a new parsing context.  */
16561   parser->context = cp_parser_context_new (parser->context);
16562   /* Begin saving tokens.  */
16563   cp_lexer_save_tokens (parser->lexer);
16564   /* In order to avoid repetitive access control error messages,
16565      access checks are queued up until we are no longer parsing
16566      tentatively.  */
16567   push_deferring_access_checks (dk_deferred);
16568 }
16569
16570 /* Commit to the currently active tentative parse.  */
16571
16572 static void
16573 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16574 {
16575   cp_parser_context *context;
16576   cp_lexer *lexer;
16577
16578   /* Mark all of the levels as committed.  */
16579   lexer = parser->lexer;
16580   for (context = parser->context; context->next; context = context->next)
16581     {
16582       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16583         break;
16584       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16585       while (!cp_lexer_saving_tokens (lexer))
16586         lexer = lexer->next;
16587       cp_lexer_commit_tokens (lexer);
16588     }
16589 }
16590
16591 /* Abort the currently active tentative parse.  All consumed tokens
16592    will be rolled back, and no diagnostics will be issued.  */
16593
16594 static void
16595 cp_parser_abort_tentative_parse (cp_parser* parser)
16596 {
16597   cp_parser_simulate_error (parser);
16598   /* Now, pretend that we want to see if the construct was
16599      successfully parsed.  */
16600   cp_parser_parse_definitely (parser);
16601 }
16602
16603 /* Stop parsing tentatively.  If a parse error has occurred, restore the
16604    token stream.  Otherwise, commit to the tokens we have consumed.
16605    Returns true if no error occurred; false otherwise.  */
16606
16607 static bool
16608 cp_parser_parse_definitely (cp_parser* parser)
16609 {
16610   bool error_occurred;
16611   cp_parser_context *context;
16612
16613   /* Remember whether or not an error occurred, since we are about to
16614      destroy that information.  */
16615   error_occurred = cp_parser_error_occurred (parser);
16616   /* Remove the topmost context from the stack.  */
16617   context = parser->context;
16618   parser->context = context->next;
16619   /* If no parse errors occurred, commit to the tentative parse.  */
16620   if (!error_occurred)
16621     {
16622       /* Commit to the tokens read tentatively, unless that was
16623          already done.  */
16624       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16625         cp_lexer_commit_tokens (parser->lexer);
16626
16627       pop_to_parent_deferring_access_checks ();
16628     }
16629   /* Otherwise, if errors occurred, roll back our state so that things
16630      are just as they were before we began the tentative parse.  */
16631   else
16632     {
16633       cp_lexer_rollback_tokens (parser->lexer);
16634       pop_deferring_access_checks ();
16635     }
16636   /* Add the context to the front of the free list.  */
16637   context->next = cp_parser_context_free_list;
16638   cp_parser_context_free_list = context;
16639
16640   return !error_occurred;
16641 }
16642
16643 /* Returns true if we are parsing tentatively and are not committed to
16644    this tentative parse.  */
16645
16646 static bool
16647 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16648 {
16649   return (cp_parser_parsing_tentatively (parser)
16650           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16651 }
16652
16653 /* Returns nonzero iff an error has occurred during the most recent
16654    tentative parse.  */
16655
16656 static bool
16657 cp_parser_error_occurred (cp_parser* parser)
16658 {
16659   return (cp_parser_parsing_tentatively (parser)
16660           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16661 }
16662
16663 /* Returns nonzero if GNU extensions are allowed.  */
16664
16665 static bool
16666 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16667 {
16668   return parser->allow_gnu_extensions_p;
16669 }
16670 \f
16671 /* Objective-C++ Productions */
16672
16673
16674 /* Parse an Objective-C expression, which feeds into a primary-expression
16675    above.
16676
16677    objc-expression:
16678      objc-message-expression
16679      objc-string-literal
16680      objc-encode-expression
16681      objc-protocol-expression
16682      objc-selector-expression
16683
16684   Returns a tree representation of the expression.  */
16685
16686 static tree
16687 cp_parser_objc_expression (cp_parser* parser)
16688 {
16689   /* Try to figure out what kind of declaration is present.  */
16690   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16691
16692   switch (kwd->type)
16693     {
16694     case CPP_OPEN_SQUARE:
16695       return cp_parser_objc_message_expression (parser);
16696
16697     case CPP_OBJC_STRING:
16698       kwd = cp_lexer_consume_token (parser->lexer);
16699       return objc_build_string_object (kwd->value);
16700
16701     case CPP_KEYWORD:
16702       switch (kwd->keyword)
16703         {
16704         case RID_AT_ENCODE:
16705           return cp_parser_objc_encode_expression (parser);
16706
16707         case RID_AT_PROTOCOL:
16708           return cp_parser_objc_protocol_expression (parser);
16709
16710         case RID_AT_SELECTOR:
16711           return cp_parser_objc_selector_expression (parser);
16712
16713         default:
16714           break;
16715         }
16716     default:
16717       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16718       cp_parser_skip_to_end_of_block_or_statement (parser);
16719     }
16720
16721   return error_mark_node;
16722 }
16723
16724 /* Parse an Objective-C message expression.
16725
16726    objc-message-expression:
16727      [ objc-message-receiver objc-message-args ]
16728
16729    Returns a representation of an Objective-C message.  */
16730
16731 static tree
16732 cp_parser_objc_message_expression (cp_parser* parser)
16733 {
16734   tree receiver, messageargs;
16735
16736   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
16737   receiver = cp_parser_objc_message_receiver (parser);
16738   messageargs = cp_parser_objc_message_args (parser);
16739   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16740
16741   return objc_build_message_expr (build_tree_list (receiver, messageargs));
16742 }
16743
16744 /* Parse an objc-message-receiver.
16745
16746    objc-message-receiver:
16747      expression
16748      simple-type-specifier
16749
16750   Returns a representation of the type or expression.  */
16751
16752 static tree
16753 cp_parser_objc_message_receiver (cp_parser* parser)
16754 {
16755   tree rcv;
16756
16757   /* An Objective-C message receiver may be either (1) a type
16758      or (2) an expression.  */
16759   cp_parser_parse_tentatively (parser);
16760   rcv = cp_parser_expression (parser, false);
16761
16762   if (cp_parser_parse_definitely (parser))
16763     return rcv;
16764
16765   rcv = cp_parser_simple_type_specifier (parser,
16766                                          /*decl_specs=*/NULL,
16767                                          CP_PARSER_FLAGS_NONE);
16768
16769   return objc_get_class_reference (rcv);
16770 }
16771
16772 /* Parse the arguments and selectors comprising an Objective-C message.
16773
16774    objc-message-args:
16775      objc-selector
16776      objc-selector-args
16777      objc-selector-args , objc-comma-args
16778
16779    objc-selector-args:
16780      objc-selector [opt] : assignment-expression
16781      objc-selector-args objc-selector [opt] : assignment-expression
16782
16783    objc-comma-args:
16784      assignment-expression
16785      objc-comma-args , assignment-expression
16786
16787    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16788    selector arguments and TREE_VALUE containing a list of comma
16789    arguments.  */
16790
16791 static tree
16792 cp_parser_objc_message_args (cp_parser* parser)
16793 {
16794   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16795   bool maybe_unary_selector_p = true;
16796   cp_token *token = cp_lexer_peek_token (parser->lexer);
16797
16798   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16799     {
16800       tree selector = NULL_TREE, arg;
16801
16802       if (token->type != CPP_COLON)
16803         selector = cp_parser_objc_selector (parser);
16804
16805       /* Detect if we have a unary selector.  */
16806       if (maybe_unary_selector_p
16807           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16808         return build_tree_list (selector, NULL_TREE);
16809
16810       maybe_unary_selector_p = false;
16811       cp_parser_require (parser, CPP_COLON, "`:'");
16812       arg = cp_parser_assignment_expression (parser, false);
16813
16814       sel_args
16815         = chainon (sel_args,
16816                    build_tree_list (selector, arg));
16817
16818       token = cp_lexer_peek_token (parser->lexer);
16819     }
16820
16821   /* Handle non-selector arguments, if any. */
16822   while (token->type == CPP_COMMA)
16823     {
16824       tree arg;
16825
16826       cp_lexer_consume_token (parser->lexer);
16827       arg = cp_parser_assignment_expression (parser, false);
16828
16829       addl_args
16830         = chainon (addl_args,
16831                    build_tree_list (NULL_TREE, arg));
16832
16833       token = cp_lexer_peek_token (parser->lexer);
16834     }
16835
16836   return build_tree_list (sel_args, addl_args);
16837 }
16838
16839 /* Parse an Objective-C encode expression.
16840
16841    objc-encode-expression:
16842      @encode objc-typename
16843
16844    Returns an encoded representation of the type argument.  */
16845
16846 static tree
16847 cp_parser_objc_encode_expression (cp_parser* parser)
16848 {
16849   tree type;
16850
16851   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
16852   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16853   type = complete_type (cp_parser_type_id (parser));
16854   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16855
16856   if (!type)
16857     {
16858       error ("%<@encode%> must specify a type as an argument");
16859       return error_mark_node;
16860     }
16861
16862   return objc_build_encode_expr (type);
16863 }
16864
16865 /* Parse an Objective-C @defs expression.  */
16866
16867 static tree
16868 cp_parser_objc_defs_expression (cp_parser *parser)
16869 {
16870   tree name;
16871
16872   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
16873   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16874   name = cp_parser_identifier (parser);
16875   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16876
16877   return objc_get_class_ivars (name);
16878 }
16879
16880 /* Parse an Objective-C protocol expression.
16881
16882   objc-protocol-expression:
16883     @protocol ( identifier )
16884
16885   Returns a representation of the protocol expression.  */
16886
16887 static tree
16888 cp_parser_objc_protocol_expression (cp_parser* parser)
16889 {
16890   tree proto;
16891
16892   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
16893   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16894   proto = cp_parser_identifier (parser);
16895   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16896
16897   return objc_build_protocol_expr (proto);
16898 }
16899
16900 /* Parse an Objective-C selector expression.
16901
16902    objc-selector-expression:
16903      @selector ( objc-method-signature )
16904
16905    objc-method-signature:
16906      objc-selector
16907      objc-selector-seq
16908
16909    objc-selector-seq:
16910      objc-selector :
16911      objc-selector-seq objc-selector :
16912
16913   Returns a representation of the method selector.  */
16914
16915 static tree
16916 cp_parser_objc_selector_expression (cp_parser* parser)
16917 {
16918   tree sel_seq = NULL_TREE;
16919   bool maybe_unary_selector_p = true;
16920   cp_token *token;
16921
16922   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
16923   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16924   token = cp_lexer_peek_token (parser->lexer);
16925
16926   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16927          || token->type == CPP_SCOPE)
16928     {
16929       tree selector = NULL_TREE;
16930
16931       if (token->type != CPP_COLON
16932           || token->type == CPP_SCOPE)
16933         selector = cp_parser_objc_selector (parser);
16934
16935       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16936           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16937         {
16938           /* Detect if we have a unary selector.  */
16939           if (maybe_unary_selector_p)
16940             {
16941               sel_seq = selector;
16942               goto finish_selector;
16943             }
16944           else
16945             {
16946               cp_parser_error (parser, "expected %<:%>");
16947             }
16948         }
16949       maybe_unary_selector_p = false;
16950       token = cp_lexer_consume_token (parser->lexer);
16951
16952       if (token->type == CPP_SCOPE)
16953         {
16954           sel_seq
16955             = chainon (sel_seq,
16956                        build_tree_list (selector, NULL_TREE));
16957           sel_seq
16958             = chainon (sel_seq,
16959                        build_tree_list (NULL_TREE, NULL_TREE));
16960         }
16961       else
16962         sel_seq
16963           = chainon (sel_seq,
16964                      build_tree_list (selector, NULL_TREE));
16965
16966       token = cp_lexer_peek_token (parser->lexer);
16967     }
16968
16969  finish_selector:
16970   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16971
16972   return objc_build_selector_expr (sel_seq);
16973 }
16974
16975 /* Parse a list of identifiers.
16976
16977    objc-identifier-list:
16978      identifier
16979      objc-identifier-list , identifier
16980
16981    Returns a TREE_LIST of identifier nodes.  */
16982
16983 static tree
16984 cp_parser_objc_identifier_list (cp_parser* parser)
16985 {
16986   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16987   cp_token *sep = cp_lexer_peek_token (parser->lexer);
16988
16989   while (sep->type == CPP_COMMA)
16990     {
16991       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16992       list = chainon (list,
16993                       build_tree_list (NULL_TREE,
16994                                        cp_parser_identifier (parser)));
16995       sep = cp_lexer_peek_token (parser->lexer);
16996     }
16997
16998   return list;
16999 }
17000
17001 /* Parse an Objective-C alias declaration.
17002
17003    objc-alias-declaration:
17004      @compatibility_alias identifier identifier ;
17005
17006    This function registers the alias mapping with the Objective-C front-end.
17007    It returns nothing.  */
17008
17009 static void
17010 cp_parser_objc_alias_declaration (cp_parser* parser)
17011 {
17012   tree alias, orig;
17013
17014   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
17015   alias = cp_parser_identifier (parser);
17016   orig = cp_parser_identifier (parser);
17017   objc_declare_alias (alias, orig);
17018   cp_parser_consume_semicolon_at_end_of_statement (parser);
17019 }
17020
17021 /* Parse an Objective-C class forward-declaration.
17022
17023    objc-class-declaration:
17024      @class objc-identifier-list ;
17025
17026    The function registers the forward declarations with the Objective-C
17027    front-end.  It returns nothing.  */
17028
17029 static void
17030 cp_parser_objc_class_declaration (cp_parser* parser)
17031 {
17032   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
17033   objc_declare_class (cp_parser_objc_identifier_list (parser));
17034   cp_parser_consume_semicolon_at_end_of_statement (parser);
17035 }
17036
17037 /* Parse a list of Objective-C protocol references.
17038
17039    objc-protocol-refs-opt:
17040      objc-protocol-refs [opt]
17041
17042    objc-protocol-refs:
17043      < objc-identifier-list >
17044
17045    Returns a TREE_LIST of identifiers, if any.  */
17046
17047 static tree
17048 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17049 {
17050   tree protorefs = NULL_TREE;
17051
17052   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17053     {
17054       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
17055       protorefs = cp_parser_objc_identifier_list (parser);
17056       cp_parser_require (parser, CPP_GREATER, "`>'");
17057     }
17058
17059   return protorefs;
17060 }
17061
17062 /* Parse a Objective-C visibility specification.  */
17063
17064 static void
17065 cp_parser_objc_visibility_spec (cp_parser* parser)
17066 {
17067   cp_token *vis = cp_lexer_peek_token (parser->lexer);
17068
17069   switch (vis->keyword)
17070     {
17071     case RID_AT_PRIVATE:
17072       objc_set_visibility (2);
17073       break;
17074     case RID_AT_PROTECTED:
17075       objc_set_visibility (0);
17076       break;
17077     case RID_AT_PUBLIC:
17078       objc_set_visibility (1);
17079       break;
17080     default:
17081       return;
17082     }
17083
17084   /* Eat '@private'/'@protected'/'@public'.  */
17085   cp_lexer_consume_token (parser->lexer);
17086 }
17087
17088 /* Parse an Objective-C method type.  */
17089
17090 static void
17091 cp_parser_objc_method_type (cp_parser* parser)
17092 {
17093   objc_set_method_type
17094    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17095     ? PLUS_EXPR
17096     : MINUS_EXPR);
17097 }
17098
17099 /* Parse an Objective-C protocol qualifier.  */
17100
17101 static tree
17102 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17103 {
17104   tree quals = NULL_TREE, node;
17105   cp_token *token = cp_lexer_peek_token (parser->lexer);
17106
17107   node = token->value;
17108
17109   while (node && TREE_CODE (node) == IDENTIFIER_NODE
17110          && (node == ridpointers [(int) RID_IN]
17111              || node == ridpointers [(int) RID_OUT]
17112              || node == ridpointers [(int) RID_INOUT]
17113              || node == ridpointers [(int) RID_BYCOPY]
17114              || node == ridpointers [(int) RID_BYREF]
17115              || node == ridpointers [(int) RID_ONEWAY]))
17116     {
17117       quals = tree_cons (NULL_TREE, node, quals);
17118       cp_lexer_consume_token (parser->lexer);
17119       token = cp_lexer_peek_token (parser->lexer);
17120       node = token->value;
17121     }
17122
17123   return quals;
17124 }
17125
17126 /* Parse an Objective-C typename.  */
17127
17128 static tree
17129 cp_parser_objc_typename (cp_parser* parser)
17130 {
17131   tree typename = NULL_TREE;
17132
17133   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17134     {
17135       tree proto_quals, cp_type = NULL_TREE;
17136
17137       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17138       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17139
17140       /* An ObjC type name may consist of just protocol qualifiers, in which
17141          case the type shall default to 'id'.  */
17142       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17143         cp_type = cp_parser_type_id (parser);
17144
17145       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17146       typename = build_tree_list (proto_quals, cp_type);
17147     }
17148
17149   return typename;
17150 }
17151
17152 /* Check to see if TYPE refers to an Objective-C selector name.  */
17153
17154 static bool
17155 cp_parser_objc_selector_p (enum cpp_ttype type)
17156 {
17157   return (type == CPP_NAME || type == CPP_KEYWORD
17158           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17159           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17160           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17161           || type == CPP_XOR || type == CPP_XOR_EQ);
17162 }
17163
17164 /* Parse an Objective-C selector.  */
17165
17166 static tree
17167 cp_parser_objc_selector (cp_parser* parser)
17168 {
17169   cp_token *token = cp_lexer_consume_token (parser->lexer);
17170
17171   if (!cp_parser_objc_selector_p (token->type))
17172     {
17173       error ("invalid Objective-C++ selector name");
17174       return error_mark_node;
17175     }
17176
17177   /* C++ operator names are allowed to appear in ObjC selectors.  */
17178   switch (token->type)
17179     {
17180     case CPP_AND_AND: return get_identifier ("and");
17181     case CPP_AND_EQ: return get_identifier ("and_eq");
17182     case CPP_AND: return get_identifier ("bitand");
17183     case CPP_OR: return get_identifier ("bitor");
17184     case CPP_COMPL: return get_identifier ("compl");
17185     case CPP_NOT: return get_identifier ("not");
17186     case CPP_NOT_EQ: return get_identifier ("not_eq");
17187     case CPP_OR_OR: return get_identifier ("or");
17188     case CPP_OR_EQ: return get_identifier ("or_eq");
17189     case CPP_XOR: return get_identifier ("xor");
17190     case CPP_XOR_EQ: return get_identifier ("xor_eq");
17191     default: return token->value;
17192     }
17193 }
17194
17195 /* Parse an Objective-C params list.  */
17196
17197 static tree
17198 cp_parser_objc_method_keyword_params (cp_parser* parser)
17199 {
17200   tree params = NULL_TREE;
17201   bool maybe_unary_selector_p = true;
17202   cp_token *token = cp_lexer_peek_token (parser->lexer);
17203
17204   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17205     {
17206       tree selector = NULL_TREE, typename, identifier;
17207
17208       if (token->type != CPP_COLON)
17209         selector = cp_parser_objc_selector (parser);
17210
17211       /* Detect if we have a unary selector.  */
17212       if (maybe_unary_selector_p
17213           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17214         return selector;
17215
17216       maybe_unary_selector_p = false;
17217       cp_parser_require (parser, CPP_COLON, "`:'");
17218       typename = cp_parser_objc_typename (parser);
17219       identifier = cp_parser_identifier (parser);
17220
17221       params
17222         = chainon (params,
17223                    objc_build_keyword_decl (selector,
17224                                             typename,
17225                                             identifier));
17226
17227       token = cp_lexer_peek_token (parser->lexer);
17228     }
17229
17230   return params;
17231 }
17232
17233 /* Parse the non-keyword Objective-C params.  */
17234
17235 static tree
17236 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17237 {
17238   tree params = make_node (TREE_LIST);
17239   cp_token *token = cp_lexer_peek_token (parser->lexer);
17240   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
17241
17242   while (token->type == CPP_COMMA)
17243     {
17244       cp_parameter_declarator *parmdecl;
17245       tree parm;
17246
17247       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17248       token = cp_lexer_peek_token (parser->lexer);
17249
17250       if (token->type == CPP_ELLIPSIS)
17251         {
17252           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
17253           *ellipsisp = true;
17254           break;
17255         }
17256
17257       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17258       parm = grokdeclarator (parmdecl->declarator,
17259                              &parmdecl->decl_specifiers,
17260                              PARM, /*initialized=*/0,
17261                              /*attrlist=*/NULL);
17262
17263       chainon (params, build_tree_list (NULL_TREE, parm));
17264       token = cp_lexer_peek_token (parser->lexer);
17265     }
17266
17267   return params;
17268 }
17269
17270 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
17271
17272 static void
17273 cp_parser_objc_interstitial_code (cp_parser* parser)
17274 {
17275   cp_token *token = cp_lexer_peek_token (parser->lexer);
17276
17277   /* If the next token is `extern' and the following token is a string
17278      literal, then we have a linkage specification.  */
17279   if (token->keyword == RID_EXTERN
17280       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17281     cp_parser_linkage_specification (parser);
17282   /* Handle #pragma, if any.  */
17283   else if (token->type == CPP_PRAGMA)
17284     cp_parser_pragma (parser, pragma_external);
17285   /* Allow stray semicolons.  */
17286   else if (token->type == CPP_SEMICOLON)
17287     cp_lexer_consume_token (parser->lexer);
17288   /* Finally, try to parse a block-declaration, or a function-definition.  */
17289   else
17290     cp_parser_block_declaration (parser, /*statement_p=*/false);
17291 }
17292
17293 /* Parse a method signature.  */
17294
17295 static tree
17296 cp_parser_objc_method_signature (cp_parser* parser)
17297 {
17298   tree rettype, kwdparms, optparms;
17299   bool ellipsis = false;
17300
17301   cp_parser_objc_method_type (parser);
17302   rettype = cp_parser_objc_typename (parser);
17303   kwdparms = cp_parser_objc_method_keyword_params (parser);
17304   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17305
17306   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17307 }
17308
17309 /* Pars an Objective-C method prototype list.  */
17310
17311 static void
17312 cp_parser_objc_method_prototype_list (cp_parser* parser)
17313 {
17314   cp_token *token = cp_lexer_peek_token (parser->lexer);
17315
17316   while (token->keyword != RID_AT_END)
17317     {
17318       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17319         {
17320           objc_add_method_declaration
17321            (cp_parser_objc_method_signature (parser));
17322           cp_parser_consume_semicolon_at_end_of_statement (parser);
17323         }
17324       else
17325         /* Allow for interspersed non-ObjC++ code.  */
17326         cp_parser_objc_interstitial_code (parser);
17327
17328       token = cp_lexer_peek_token (parser->lexer);
17329     }
17330
17331   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17332   objc_finish_interface ();
17333 }
17334
17335 /* Parse an Objective-C method definition list.  */
17336
17337 static void
17338 cp_parser_objc_method_definition_list (cp_parser* parser)
17339 {
17340   cp_token *token = cp_lexer_peek_token (parser->lexer);
17341
17342   while (token->keyword != RID_AT_END)
17343     {
17344       tree meth;
17345
17346       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17347         {
17348           push_deferring_access_checks (dk_deferred);
17349           objc_start_method_definition
17350            (cp_parser_objc_method_signature (parser));
17351
17352           /* For historical reasons, we accept an optional semicolon.  */
17353           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17354             cp_lexer_consume_token (parser->lexer);
17355
17356           perform_deferred_access_checks ();
17357           stop_deferring_access_checks ();
17358           meth = cp_parser_function_definition_after_declarator (parser,
17359                                                                  false);
17360           pop_deferring_access_checks ();
17361           objc_finish_method_definition (meth);
17362         }
17363       else
17364         /* Allow for interspersed non-ObjC++ code.  */
17365         cp_parser_objc_interstitial_code (parser);
17366
17367       token = cp_lexer_peek_token (parser->lexer);
17368     }
17369
17370   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17371   objc_finish_implementation ();
17372 }
17373
17374 /* Parse Objective-C ivars.  */
17375
17376 static void
17377 cp_parser_objc_class_ivars (cp_parser* parser)
17378 {
17379   cp_token *token = cp_lexer_peek_token (parser->lexer);
17380
17381   if (token->type != CPP_OPEN_BRACE)
17382     return;     /* No ivars specified.  */
17383
17384   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
17385   token = cp_lexer_peek_token (parser->lexer);
17386
17387   while (token->type != CPP_CLOSE_BRACE)
17388     {
17389       cp_decl_specifier_seq declspecs;
17390       int decl_class_or_enum_p;
17391       tree prefix_attributes;
17392
17393       cp_parser_objc_visibility_spec (parser);
17394
17395       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17396         break;
17397
17398       cp_parser_decl_specifier_seq (parser,
17399                                     CP_PARSER_FLAGS_OPTIONAL,
17400                                     &declspecs,
17401                                     &decl_class_or_enum_p);
17402       prefix_attributes = declspecs.attributes;
17403       declspecs.attributes = NULL_TREE;
17404
17405       /* Keep going until we hit the `;' at the end of the
17406          declaration.  */
17407       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17408         {
17409           tree width = NULL_TREE, attributes, first_attribute, decl;
17410           cp_declarator *declarator = NULL;
17411           int ctor_dtor_or_conv_p;
17412
17413           /* Check for a (possibly unnamed) bitfield declaration.  */
17414           token = cp_lexer_peek_token (parser->lexer);
17415           if (token->type == CPP_COLON)
17416             goto eat_colon;
17417
17418           if (token->type == CPP_NAME
17419               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17420                   == CPP_COLON))
17421             {
17422               /* Get the name of the bitfield.  */
17423               declarator = make_id_declarator (NULL_TREE,
17424                                                cp_parser_identifier (parser),
17425                                                sfk_none);
17426
17427              eat_colon:
17428               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17429               /* Get the width of the bitfield.  */
17430               width
17431                 = cp_parser_constant_expression (parser,
17432                                                  /*allow_non_constant=*/false,
17433                                                  NULL);
17434             }
17435           else
17436             {
17437               /* Parse the declarator.  */
17438               declarator
17439                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17440                                         &ctor_dtor_or_conv_p,
17441                                         /*parenthesized_p=*/NULL,
17442                                         /*member_p=*/false);
17443             }
17444
17445           /* Look for attributes that apply to the ivar.  */
17446           attributes = cp_parser_attributes_opt (parser);
17447           /* Remember which attributes are prefix attributes and
17448              which are not.  */
17449           first_attribute = attributes;
17450           /* Combine the attributes.  */
17451           attributes = chainon (prefix_attributes, attributes);
17452
17453           if (width)
17454             {
17455               /* Create the bitfield declaration.  */
17456               decl = grokbitfield (declarator, &declspecs, width);
17457               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17458             }
17459           else
17460             decl = grokfield (declarator, &declspecs,
17461                               NULL_TREE, /*init_const_expr_p=*/false,
17462                               NULL_TREE, attributes);
17463
17464           /* Add the instance variable.  */
17465           objc_add_instance_variable (decl);
17466
17467           /* Reset PREFIX_ATTRIBUTES.  */
17468           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17469             attributes = TREE_CHAIN (attributes);
17470           if (attributes)
17471             TREE_CHAIN (attributes) = NULL_TREE;
17472
17473           token = cp_lexer_peek_token (parser->lexer);
17474
17475           if (token->type == CPP_COMMA)
17476             {
17477               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17478               continue;
17479             }
17480           break;
17481         }
17482
17483       cp_parser_consume_semicolon_at_end_of_statement (parser);
17484       token = cp_lexer_peek_token (parser->lexer);
17485     }
17486
17487   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17488   /* For historical reasons, we accept an optional semicolon.  */
17489   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17490     cp_lexer_consume_token (parser->lexer);
17491 }
17492
17493 /* Parse an Objective-C protocol declaration.  */
17494
17495 static void
17496 cp_parser_objc_protocol_declaration (cp_parser* parser)
17497 {
17498   tree proto, protorefs;
17499   cp_token *tok;
17500
17501   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17502   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17503     {
17504       error ("identifier expected after %<@protocol%>");
17505       goto finish;
17506     }
17507
17508   /* See if we have a forward declaration or a definition.  */
17509   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17510
17511   /* Try a forward declaration first.  */
17512   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17513     {
17514       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17515      finish:
17516       cp_parser_consume_semicolon_at_end_of_statement (parser);
17517     }
17518
17519   /* Ok, we got a full-fledged definition (or at least should).  */
17520   else
17521     {
17522       proto = cp_parser_identifier (parser);
17523       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17524       objc_start_protocol (proto, protorefs);
17525       cp_parser_objc_method_prototype_list (parser);
17526     }
17527 }
17528
17529 /* Parse an Objective-C superclass or category.  */
17530
17531 static void
17532 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17533                                                           tree *categ)
17534 {
17535   cp_token *next = cp_lexer_peek_token (parser->lexer);
17536
17537   *super = *categ = NULL_TREE;
17538   if (next->type == CPP_COLON)
17539     {
17540       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17541       *super = cp_parser_identifier (parser);
17542     }
17543   else if (next->type == CPP_OPEN_PAREN)
17544     {
17545       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17546       *categ = cp_parser_identifier (parser);
17547       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17548     }
17549 }
17550
17551 /* Parse an Objective-C class interface.  */
17552
17553 static void
17554 cp_parser_objc_class_interface (cp_parser* parser)
17555 {
17556   tree name, super, categ, protos;
17557
17558   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17559   name = cp_parser_identifier (parser);
17560   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17561   protos = cp_parser_objc_protocol_refs_opt (parser);
17562
17563   /* We have either a class or a category on our hands.  */
17564   if (categ)
17565     objc_start_category_interface (name, categ, protos);
17566   else
17567     {
17568       objc_start_class_interface (name, super, protos);
17569       /* Handle instance variable declarations, if any.  */
17570       cp_parser_objc_class_ivars (parser);
17571       objc_continue_interface ();
17572     }
17573
17574   cp_parser_objc_method_prototype_list (parser);
17575 }
17576
17577 /* Parse an Objective-C class implementation.  */
17578
17579 static void
17580 cp_parser_objc_class_implementation (cp_parser* parser)
17581 {
17582   tree name, super, categ;
17583
17584   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17585   name = cp_parser_identifier (parser);
17586   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17587
17588   /* We have either a class or a category on our hands.  */
17589   if (categ)
17590     objc_start_category_implementation (name, categ);
17591   else
17592     {
17593       objc_start_class_implementation (name, super);
17594       /* Handle instance variable declarations, if any.  */
17595       cp_parser_objc_class_ivars (parser);
17596       objc_continue_implementation ();
17597     }
17598
17599   cp_parser_objc_method_definition_list (parser);
17600 }
17601
17602 /* Consume the @end token and finish off the implementation.  */
17603
17604 static void
17605 cp_parser_objc_end_implementation (cp_parser* parser)
17606 {
17607   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17608   objc_finish_implementation ();
17609 }
17610
17611 /* Parse an Objective-C declaration.  */
17612
17613 static void
17614 cp_parser_objc_declaration (cp_parser* parser)
17615 {
17616   /* Try to figure out what kind of declaration is present.  */
17617   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17618
17619   switch (kwd->keyword)
17620     {
17621     case RID_AT_ALIAS:
17622       cp_parser_objc_alias_declaration (parser);
17623       break;
17624     case RID_AT_CLASS:
17625       cp_parser_objc_class_declaration (parser);
17626       break;
17627     case RID_AT_PROTOCOL:
17628       cp_parser_objc_protocol_declaration (parser);
17629       break;
17630     case RID_AT_INTERFACE:
17631       cp_parser_objc_class_interface (parser);
17632       break;
17633     case RID_AT_IMPLEMENTATION:
17634       cp_parser_objc_class_implementation (parser);
17635       break;
17636     case RID_AT_END:
17637       cp_parser_objc_end_implementation (parser);
17638       break;
17639     default:
17640       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17641       cp_parser_skip_to_end_of_block_or_statement (parser);
17642     }
17643 }
17644
17645 /* Parse an Objective-C try-catch-finally statement.
17646
17647    objc-try-catch-finally-stmt:
17648      @try compound-statement objc-catch-clause-seq [opt]
17649        objc-finally-clause [opt]
17650
17651    objc-catch-clause-seq:
17652      objc-catch-clause objc-catch-clause-seq [opt]
17653
17654    objc-catch-clause:
17655      @catch ( exception-declaration ) compound-statement
17656
17657    objc-finally-clause
17658      @finally compound-statement
17659
17660    Returns NULL_TREE.  */
17661
17662 static tree
17663 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17664   location_t location;
17665   tree stmt;
17666
17667   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17668   location = cp_lexer_peek_token (parser->lexer)->location;
17669   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17670      node, lest it get absorbed into the surrounding block.  */
17671   stmt = push_stmt_list ();
17672   cp_parser_compound_statement (parser, NULL, false);
17673   objc_begin_try_stmt (location, pop_stmt_list (stmt));
17674
17675   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17676     {
17677       cp_parameter_declarator *parmdecl;
17678       tree parm;
17679
17680       cp_lexer_consume_token (parser->lexer);
17681       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17682       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17683       parm = grokdeclarator (parmdecl->declarator,
17684                              &parmdecl->decl_specifiers,
17685                              PARM, /*initialized=*/0,
17686                              /*attrlist=*/NULL);
17687       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17688       objc_begin_catch_clause (parm);
17689       cp_parser_compound_statement (parser, NULL, false);
17690       objc_finish_catch_clause ();
17691     }
17692
17693   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17694     {
17695       cp_lexer_consume_token (parser->lexer);
17696       location = cp_lexer_peek_token (parser->lexer)->location;
17697       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17698          node, lest it get absorbed into the surrounding block.  */
17699       stmt = push_stmt_list ();
17700       cp_parser_compound_statement (parser, NULL, false);
17701       objc_build_finally_clause (location, pop_stmt_list (stmt));
17702     }
17703
17704   return objc_finish_try_stmt ();
17705 }
17706
17707 /* Parse an Objective-C synchronized statement.
17708
17709    objc-synchronized-stmt:
17710      @synchronized ( expression ) compound-statement
17711
17712    Returns NULL_TREE.  */
17713
17714 static tree
17715 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17716   location_t location;
17717   tree lock, stmt;
17718
17719   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17720
17721   location = cp_lexer_peek_token (parser->lexer)->location;
17722   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17723   lock = cp_parser_expression (parser, false);
17724   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17725
17726   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17727      node, lest it get absorbed into the surrounding block.  */
17728   stmt = push_stmt_list ();
17729   cp_parser_compound_statement (parser, NULL, false);
17730
17731   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17732 }
17733
17734 /* Parse an Objective-C throw statement.
17735
17736    objc-throw-stmt:
17737      @throw assignment-expression [opt] ;
17738
17739    Returns a constructed '@throw' statement.  */
17740
17741 static tree
17742 cp_parser_objc_throw_statement (cp_parser *parser) {
17743   tree expr = NULL_TREE;
17744
17745   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17746
17747   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17748     expr = cp_parser_assignment_expression (parser, false);
17749
17750   cp_parser_consume_semicolon_at_end_of_statement (parser);
17751
17752   return objc_build_throw_stmt (expr);
17753 }
17754
17755 /* Parse an Objective-C statement.  */
17756
17757 static tree
17758 cp_parser_objc_statement (cp_parser * parser) {
17759   /* Try to figure out what kind of declaration is present.  */
17760   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17761
17762   switch (kwd->keyword)
17763     {
17764     case RID_AT_TRY:
17765       return cp_parser_objc_try_catch_finally_statement (parser);
17766     case RID_AT_SYNCHRONIZED:
17767       return cp_parser_objc_synchronized_statement (parser);
17768     case RID_AT_THROW:
17769       return cp_parser_objc_throw_statement (parser);
17770     default:
17771       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17772       cp_parser_skip_to_end_of_block_or_statement (parser);
17773     }
17774
17775   return error_mark_node;
17776 }
17777 \f
17778 /* OpenMP 2.5 parsing routines.  */
17779
17780 /* All OpenMP clauses.  OpenMP 2.5.  */
17781 typedef enum pragma_omp_clause {
17782   PRAGMA_OMP_CLAUSE_NONE = 0,
17783
17784   PRAGMA_OMP_CLAUSE_COPYIN,
17785   PRAGMA_OMP_CLAUSE_COPYPRIVATE,
17786   PRAGMA_OMP_CLAUSE_DEFAULT,
17787   PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
17788   PRAGMA_OMP_CLAUSE_IF,
17789   PRAGMA_OMP_CLAUSE_LASTPRIVATE,
17790   PRAGMA_OMP_CLAUSE_NOWAIT,
17791   PRAGMA_OMP_CLAUSE_NUM_THREADS,
17792   PRAGMA_OMP_CLAUSE_ORDERED,
17793   PRAGMA_OMP_CLAUSE_PRIVATE,
17794   PRAGMA_OMP_CLAUSE_REDUCTION,
17795   PRAGMA_OMP_CLAUSE_SCHEDULE,
17796   PRAGMA_OMP_CLAUSE_SHARED
17797 } pragma_omp_clause;
17798
17799 /* Returns name of the next clause.
17800    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17801    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
17802    returned and the token is consumed.  */
17803
17804 static pragma_omp_clause
17805 cp_parser_omp_clause_name (cp_parser *parser)
17806 {
17807   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
17808
17809   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
17810     result = PRAGMA_OMP_CLAUSE_IF;
17811   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
17812     result = PRAGMA_OMP_CLAUSE_DEFAULT;
17813   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
17814     result = PRAGMA_OMP_CLAUSE_PRIVATE;
17815   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17816     {
17817       tree id = cp_lexer_peek_token (parser->lexer)->value;
17818       const char *p = IDENTIFIER_POINTER (id);
17819
17820       switch (p[0])
17821         {
17822         case 'c':
17823           if (!strcmp ("copyin", p))
17824             result = PRAGMA_OMP_CLAUSE_COPYIN;
17825           else if (!strcmp ("copyprivate", p))
17826             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
17827           break;
17828         case 'f':
17829           if (!strcmp ("firstprivate", p))
17830             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
17831           break;
17832         case 'l':
17833           if (!strcmp ("lastprivate", p))
17834             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
17835           break;
17836         case 'n':
17837           if (!strcmp ("nowait", p))
17838             result = PRAGMA_OMP_CLAUSE_NOWAIT;
17839           else if (!strcmp ("num_threads", p))
17840             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
17841           break;
17842         case 'o':
17843           if (!strcmp ("ordered", p))
17844             result = PRAGMA_OMP_CLAUSE_ORDERED;
17845           break;
17846         case 'r':
17847           if (!strcmp ("reduction", p))
17848             result = PRAGMA_OMP_CLAUSE_REDUCTION;
17849           break;
17850         case 's':
17851           if (!strcmp ("schedule", p))
17852             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
17853           else if (!strcmp ("shared", p))
17854             result = PRAGMA_OMP_CLAUSE_SHARED;
17855           break;
17856         }
17857     }
17858
17859   if (result != PRAGMA_OMP_CLAUSE_NONE)
17860     cp_lexer_consume_token (parser->lexer);
17861
17862   return result;
17863 }
17864
17865 /* Validate that a clause of the given type does not already exist.  */
17866
17867 static void
17868 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
17869 {
17870   tree c;
17871
17872   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
17873     if (OMP_CLAUSE_CODE (c) == code)
17874       {
17875         error ("too many %qs clauses", name);
17876         break;
17877       }
17878 }
17879
17880 /* OpenMP 2.5:
17881    variable-list:
17882      identifier
17883      variable-list , identifier
17884
17885    In addition, we match a closing parenthesis.  An opening parenthesis
17886    will have been consumed by the caller.
17887
17888    If KIND is nonzero, create the appropriate node and install the decl
17889    in OMP_CLAUSE_DECL and add the node to the head of the list.
17890
17891    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17892    return the list created.  */
17893
17894 static tree
17895 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
17896                                 tree list)
17897 {
17898   while (1)
17899     {
17900       tree name, decl;
17901
17902       name = cp_parser_id_expression (parser, /*template_p=*/false,
17903                                       /*check_dependency_p=*/true,
17904                                       /*template_p=*/NULL,
17905                                       /*declarator_p=*/false,
17906                                       /*optional_p=*/false);
17907       if (name == error_mark_node)
17908         goto skip_comma;
17909
17910       decl = cp_parser_lookup_name_simple (parser, name);
17911       if (decl == error_mark_node)
17912         cp_parser_name_lookup_error (parser, name, decl, NULL);
17913       else if (kind != 0)
17914         {
17915           tree u = build_omp_clause (kind);
17916           OMP_CLAUSE_DECL (u) = decl;
17917           OMP_CLAUSE_CHAIN (u) = list;
17918           list = u;
17919         }
17920       else
17921         list = tree_cons (decl, NULL_TREE, list);
17922
17923     get_comma:
17924       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17925         break;
17926       cp_lexer_consume_token (parser->lexer);
17927     }
17928
17929   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17930     {
17931       int ending;
17932
17933       /* Try to resync to an unnested comma.  Copied from
17934          cp_parser_parenthesized_expression_list.  */
17935     skip_comma:
17936       ending = cp_parser_skip_to_closing_parenthesis (parser,
17937                                                       /*recovering=*/true,
17938                                                       /*or_comma=*/true,
17939                                                       /*consume_paren=*/true);
17940       if (ending < 0)
17941         goto get_comma;
17942     }
17943
17944   return list;
17945 }
17946
17947 /* Similarly, but expect leading and trailing parenthesis.  This is a very
17948    common case for omp clauses.  */
17949
17950 static tree
17951 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
17952 {
17953   if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17954     return cp_parser_omp_var_list_no_open (parser, kind, list);
17955   return list;
17956 }
17957
17958 /* OpenMP 2.5:
17959    default ( shared | none ) */
17960
17961 static tree
17962 cp_parser_omp_clause_default (cp_parser *parser, tree list)
17963 {
17964   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
17965   tree c;
17966
17967   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17968     return list;
17969   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17970     {
17971       tree id = cp_lexer_peek_token (parser->lexer)->value;
17972       const char *p = IDENTIFIER_POINTER (id);
17973
17974       switch (p[0])
17975         {
17976         case 'n':
17977           if (strcmp ("none", p) != 0)
17978             goto invalid_kind;
17979           kind = OMP_CLAUSE_DEFAULT_NONE;
17980           break;
17981
17982         case 's':
17983           if (strcmp ("shared", p) != 0)
17984             goto invalid_kind;
17985           kind = OMP_CLAUSE_DEFAULT_SHARED;
17986           break;
17987
17988         default:
17989           goto invalid_kind;
17990         }
17991
17992       cp_lexer_consume_token (parser->lexer);
17993     }
17994   else
17995     {
17996     invalid_kind:
17997       cp_parser_error (parser, "expected %<none%> or %<shared%>");
17998     }
17999
18000   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18001     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18002                                            /*or_comma=*/false,
18003                                            /*consume_paren=*/true);
18004
18005   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18006     return list;
18007
18008   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18009   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18010   OMP_CLAUSE_CHAIN (c) = list;
18011   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18012
18013   return c;
18014 }
18015
18016 /* OpenMP 2.5:
18017    if ( expression ) */
18018
18019 static tree
18020 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18021 {
18022   tree t, c;
18023
18024   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18025     return list;
18026
18027   t = cp_parser_condition (parser);
18028
18029   if (t == error_mark_node
18030       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18031     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18032                                            /*or_comma=*/false,
18033                                            /*consume_paren=*/true);
18034
18035   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18036
18037   c = build_omp_clause (OMP_CLAUSE_IF);
18038   OMP_CLAUSE_IF_EXPR (c) = t;
18039   OMP_CLAUSE_CHAIN (c) = list;
18040
18041   return c;
18042 }
18043
18044 /* OpenMP 2.5:
18045    nowait */
18046
18047 static tree
18048 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18049 {
18050   tree c;
18051
18052   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18053
18054   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18055   OMP_CLAUSE_CHAIN (c) = list;
18056   return c;
18057 }
18058
18059 /* OpenMP 2.5:
18060    num_threads ( expression ) */
18061
18062 static tree
18063 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18064 {
18065   tree t, c;
18066
18067   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18068     return list;
18069
18070   t = cp_parser_expression (parser, false);
18071
18072   if (t == error_mark_node
18073       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18074     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18075                                            /*or_comma=*/false,
18076                                            /*consume_paren=*/true);
18077
18078   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18079
18080   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18081   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18082   OMP_CLAUSE_CHAIN (c) = list;
18083
18084   return c;
18085 }
18086
18087 /* OpenMP 2.5:
18088    ordered */
18089
18090 static tree
18091 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18092 {
18093   tree c;
18094
18095   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18096
18097   c = build_omp_clause (OMP_CLAUSE_ORDERED);
18098   OMP_CLAUSE_CHAIN (c) = list;
18099   return c;
18100 }
18101
18102 /* OpenMP 2.5:
18103    reduction ( reduction-operator : variable-list )
18104
18105    reduction-operator:
18106      One of: + * - & ^ | && || */
18107
18108 static tree
18109 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18110 {
18111   enum tree_code code;
18112   tree nlist, c;
18113
18114   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18115     return list;
18116
18117   switch (cp_lexer_peek_token (parser->lexer)->type)
18118     {
18119     case CPP_PLUS:
18120       code = PLUS_EXPR;
18121       break;
18122     case CPP_MULT:
18123       code = MULT_EXPR;
18124       break;
18125     case CPP_MINUS:
18126       code = MINUS_EXPR;
18127       break;
18128     case CPP_AND:
18129       code = BIT_AND_EXPR;
18130       break;
18131     case CPP_XOR:
18132       code = BIT_XOR_EXPR;
18133       break;
18134     case CPP_OR:
18135       code = BIT_IOR_EXPR;
18136       break;
18137     case CPP_AND_AND:
18138       code = TRUTH_ANDIF_EXPR;
18139       break;
18140     case CPP_OR_OR:
18141       code = TRUTH_ORIF_EXPR;
18142       break;
18143     default:
18144       cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18145     resync_fail:
18146       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18147                                              /*or_comma=*/false,
18148                                              /*consume_paren=*/true);
18149       return list;
18150     }
18151   cp_lexer_consume_token (parser->lexer);
18152
18153   if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18154     goto resync_fail;
18155
18156   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18157   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18158     OMP_CLAUSE_REDUCTION_CODE (c) = code;
18159
18160   return nlist;
18161 }
18162
18163 /* OpenMP 2.5:
18164    schedule ( schedule-kind )
18165    schedule ( schedule-kind , expression )
18166
18167    schedule-kind:
18168      static | dynamic | guided | runtime  */
18169
18170 static tree
18171 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18172 {
18173   tree c, t;
18174
18175   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18176     return list;
18177
18178   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18179
18180   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18181     {
18182       tree id = cp_lexer_peek_token (parser->lexer)->value;
18183       const char *p = IDENTIFIER_POINTER (id);
18184
18185       switch (p[0])
18186         {
18187         case 'd':
18188           if (strcmp ("dynamic", p) != 0)
18189             goto invalid_kind;
18190           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18191           break;
18192
18193         case 'g':
18194           if (strcmp ("guided", p) != 0)
18195             goto invalid_kind;
18196           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18197           break;
18198
18199         case 'r':
18200           if (strcmp ("runtime", p) != 0)
18201             goto invalid_kind;
18202           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18203           break;
18204
18205         default:
18206           goto invalid_kind;
18207         }
18208     }
18209   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18210     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18211   else
18212     goto invalid_kind;
18213   cp_lexer_consume_token (parser->lexer);
18214
18215   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18216     {
18217       cp_lexer_consume_token (parser->lexer);
18218
18219       t = cp_parser_assignment_expression (parser, false);
18220
18221       if (t == error_mark_node)
18222         goto resync_fail;
18223       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18224         error ("schedule %<runtime%> does not take "
18225                "a %<chunk_size%> parameter");
18226       else
18227         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18228
18229       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18230         goto resync_fail;
18231     }
18232   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18233     goto resync_fail;
18234
18235   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18236   OMP_CLAUSE_CHAIN (c) = list;
18237   return c;
18238
18239  invalid_kind:
18240   cp_parser_error (parser, "invalid schedule kind");
18241  resync_fail:
18242   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18243                                          /*or_comma=*/false,
18244                                          /*consume_paren=*/true);
18245   return list;
18246 }
18247
18248 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
18249    is a bitmask in MASK.  Return the list of clauses found; the result
18250    of clause default goes in *pdefault.  */
18251
18252 static tree
18253 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18254                            const char *where, cp_token *pragma_tok)
18255 {
18256   tree clauses = NULL;
18257
18258   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18259     {
18260       pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18261       const char *c_name;
18262       tree prev = clauses;
18263
18264       switch (c_kind)
18265         {
18266         case PRAGMA_OMP_CLAUSE_COPYIN:
18267           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18268           c_name = "copyin";
18269           break;
18270         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18271           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18272                                             clauses);
18273           c_name = "copyprivate";
18274           break;
18275         case PRAGMA_OMP_CLAUSE_DEFAULT:
18276           clauses = cp_parser_omp_clause_default (parser, clauses);
18277           c_name = "default";
18278           break;
18279         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18280           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18281                                             clauses);
18282           c_name = "firstprivate";
18283           break;
18284         case PRAGMA_OMP_CLAUSE_IF:
18285           clauses = cp_parser_omp_clause_if (parser, clauses);
18286           c_name = "if";
18287           break;
18288         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18289           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18290                                             clauses);
18291           c_name = "lastprivate";
18292           break;
18293         case PRAGMA_OMP_CLAUSE_NOWAIT:
18294           clauses = cp_parser_omp_clause_nowait (parser, clauses);
18295           c_name = "nowait";
18296           break;
18297         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18298           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18299           c_name = "num_threads";
18300           break;
18301         case PRAGMA_OMP_CLAUSE_ORDERED:
18302           clauses = cp_parser_omp_clause_ordered (parser, clauses);
18303           c_name = "ordered";
18304           break;
18305         case PRAGMA_OMP_CLAUSE_PRIVATE:
18306           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18307                                             clauses);
18308           c_name = "private";
18309           break;
18310         case PRAGMA_OMP_CLAUSE_REDUCTION:
18311           clauses = cp_parser_omp_clause_reduction (parser, clauses);
18312           c_name = "reduction";
18313           break;
18314         case PRAGMA_OMP_CLAUSE_SCHEDULE:
18315           clauses = cp_parser_omp_clause_schedule (parser, clauses);
18316           c_name = "schedule";
18317           break;
18318         case PRAGMA_OMP_CLAUSE_SHARED:
18319           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18320                                             clauses);
18321           c_name = "shared";
18322           break;
18323         default:
18324           cp_parser_error (parser, "expected %<#pragma omp%> clause");
18325           goto saw_error;
18326         }
18327
18328       if (((mask >> c_kind) & 1) == 0)
18329         {
18330           /* Remove the invalid clause(s) from the list to avoid
18331              confusing the rest of the compiler.  */
18332           clauses = prev;
18333           error ("%qs is not valid for %qs", c_name, where);
18334         }
18335     }
18336  saw_error:
18337   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18338   return finish_omp_clauses (clauses);
18339 }
18340
18341 /* OpenMP 2.5:
18342    structured-block:
18343      statement
18344
18345    In practice, we're also interested in adding the statement to an
18346    outer node.  So it is convenient if we work around the fact that
18347    cp_parser_statement calls add_stmt.  */
18348
18349 static unsigned
18350 cp_parser_begin_omp_structured_block (cp_parser *parser)
18351 {
18352   unsigned save = parser->in_statement;
18353
18354   /* Only move the values to IN_OMP_BLOCK if they weren't false.
18355      This preserves the "not within loop or switch" style error messages
18356      for nonsense cases like
18357         void foo() {
18358         #pragma omp single
18359           break;
18360         }
18361   */
18362   if (parser->in_statement)
18363     parser->in_statement = IN_OMP_BLOCK;
18364
18365   return save;
18366 }
18367
18368 static void
18369 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18370 {
18371   parser->in_statement = save;
18372 }
18373
18374 static tree
18375 cp_parser_omp_structured_block (cp_parser *parser)
18376 {
18377   tree stmt = begin_omp_structured_block ();
18378   unsigned int save = cp_parser_begin_omp_structured_block (parser);
18379
18380   cp_parser_statement (parser, NULL_TREE, false);
18381
18382   cp_parser_end_omp_structured_block (parser, save);
18383   return finish_omp_structured_block (stmt);
18384 }
18385
18386 /* OpenMP 2.5:
18387    # pragma omp atomic new-line
18388      expression-stmt
18389
18390    expression-stmt:
18391      x binop= expr | x++ | ++x | x-- | --x
18392    binop:
18393      +, *, -, /, &, ^, |, <<, >>
18394
18395   where x is an lvalue expression with scalar type.  */
18396
18397 static void
18398 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18399 {
18400   tree lhs, rhs;
18401   enum tree_code code;
18402
18403   cp_parser_require_pragma_eol (parser, pragma_tok);
18404
18405   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18406                                     /*cast_p=*/false);
18407   switch (TREE_CODE (lhs))
18408     {
18409     case ERROR_MARK:
18410       goto saw_error;
18411
18412     case PREINCREMENT_EXPR:
18413     case POSTINCREMENT_EXPR:
18414       lhs = TREE_OPERAND (lhs, 0);
18415       code = PLUS_EXPR;
18416       rhs = integer_one_node;
18417       break;
18418
18419     case PREDECREMENT_EXPR:
18420     case POSTDECREMENT_EXPR:
18421       lhs = TREE_OPERAND (lhs, 0);
18422       code = MINUS_EXPR;
18423       rhs = integer_one_node;
18424       break;
18425
18426     default:
18427       switch (cp_lexer_peek_token (parser->lexer)->type)
18428         {
18429         case CPP_MULT_EQ:
18430           code = MULT_EXPR;
18431           break;
18432         case CPP_DIV_EQ:
18433           code = TRUNC_DIV_EXPR;
18434           break;
18435         case CPP_PLUS_EQ:
18436           code = PLUS_EXPR;
18437           break;
18438         case CPP_MINUS_EQ:
18439           code = MINUS_EXPR;
18440           break;
18441         case CPP_LSHIFT_EQ:
18442           code = LSHIFT_EXPR;
18443           break;
18444         case CPP_RSHIFT_EQ:
18445           code = RSHIFT_EXPR;
18446           break;
18447         case CPP_AND_EQ:
18448           code = BIT_AND_EXPR;
18449           break;
18450         case CPP_OR_EQ:
18451           code = BIT_IOR_EXPR;
18452           break;
18453         case CPP_XOR_EQ:
18454           code = BIT_XOR_EXPR;
18455           break;
18456         default:
18457           cp_parser_error (parser,
18458                            "invalid operator for %<#pragma omp atomic%>");
18459           goto saw_error;
18460         }
18461       cp_lexer_consume_token (parser->lexer);
18462
18463       rhs = cp_parser_expression (parser, false);
18464       if (rhs == error_mark_node)
18465         goto saw_error;
18466       break;
18467     }
18468   finish_omp_atomic (code, lhs, rhs);
18469   cp_parser_consume_semicolon_at_end_of_statement (parser);
18470   return;
18471
18472  saw_error:
18473   cp_parser_skip_to_end_of_block_or_statement (parser);
18474 }
18475
18476
18477 /* OpenMP 2.5:
18478    # pragma omp barrier new-line  */
18479
18480 static void
18481 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18482 {
18483   cp_parser_require_pragma_eol (parser, pragma_tok);
18484   finish_omp_barrier ();
18485 }
18486
18487 /* OpenMP 2.5:
18488    # pragma omp critical [(name)] new-line
18489      structured-block  */
18490
18491 static tree
18492 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18493 {
18494   tree stmt, name = NULL;
18495
18496   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18497     {
18498       cp_lexer_consume_token (parser->lexer);
18499
18500       name = cp_parser_identifier (parser);
18501
18502       if (name == error_mark_node
18503           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18504         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18505                                                /*or_comma=*/false,
18506                                                /*consume_paren=*/true);
18507       if (name == error_mark_node)
18508         name = NULL;
18509     }
18510   cp_parser_require_pragma_eol (parser, pragma_tok);
18511
18512   stmt = cp_parser_omp_structured_block (parser);
18513   return c_finish_omp_critical (stmt, name);
18514 }
18515
18516 /* OpenMP 2.5:
18517    # pragma omp flush flush-vars[opt] new-line
18518
18519    flush-vars:
18520      ( variable-list ) */
18521
18522 static void
18523 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18524 {
18525   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18526     (void) cp_parser_omp_var_list (parser, 0, NULL);
18527   cp_parser_require_pragma_eol (parser, pragma_tok);
18528
18529   finish_omp_flush ();
18530 }
18531
18532 /* Parse the restricted form of the for statment allowed by OpenMP.  */
18533
18534 static tree
18535 cp_parser_omp_for_loop (cp_parser *parser)
18536 {
18537   tree init, cond, incr, body, decl, pre_body;
18538   location_t loc;
18539
18540   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18541     {
18542       cp_parser_error (parser, "for statement expected");
18543       return NULL;
18544     }
18545   loc = cp_lexer_consume_token (parser->lexer)->location;
18546   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18547     return NULL;
18548
18549   init = decl = NULL;
18550   pre_body = push_stmt_list ();
18551   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18552     {
18553       cp_decl_specifier_seq type_specifiers;
18554
18555       /* First, try to parse as an initialized declaration.  See
18556          cp_parser_condition, from whence the bulk of this is copied.  */
18557
18558       cp_parser_parse_tentatively (parser);
18559       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18560                                     &type_specifiers);
18561       if (!cp_parser_error_occurred (parser))
18562         {
18563           tree asm_specification, attributes;
18564           cp_declarator *declarator;
18565
18566           declarator = cp_parser_declarator (parser,
18567                                              CP_PARSER_DECLARATOR_NAMED,
18568                                              /*ctor_dtor_or_conv_p=*/NULL,
18569                                              /*parenthesized_p=*/NULL,
18570                                              /*member_p=*/false);
18571           attributes = cp_parser_attributes_opt (parser);
18572           asm_specification = cp_parser_asm_specification_opt (parser);
18573
18574           cp_parser_require (parser, CPP_EQ, "`='");
18575           if (cp_parser_parse_definitely (parser))
18576             {
18577               tree pushed_scope;
18578
18579               decl = start_decl (declarator, &type_specifiers,
18580                                  /*initialized_p=*/false, attributes,
18581                                  /*prefix_attributes=*/NULL_TREE,
18582                                  &pushed_scope);
18583
18584               init = cp_parser_assignment_expression (parser, false);
18585
18586               cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18587                               asm_specification, LOOKUP_ONLYCONVERTING);
18588
18589               if (pushed_scope)
18590                 pop_scope (pushed_scope);
18591             }
18592         }
18593       else
18594         cp_parser_abort_tentative_parse (parser);
18595
18596       /* If parsing as an initialized declaration failed, try again as
18597          a simple expression.  */
18598       if (decl == NULL)
18599         init = cp_parser_expression (parser, false);
18600     }
18601   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18602   pre_body = pop_stmt_list (pre_body);
18603
18604   cond = NULL;
18605   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18606     cond = cp_parser_condition (parser);
18607   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18608
18609   incr = NULL;
18610   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18611     incr = cp_parser_expression (parser, false);
18612
18613   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18614     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18615                                            /*or_comma=*/false,
18616                                            /*consume_paren=*/true);
18617
18618   /* Note that we saved the original contents of this flag when we entered
18619      the structured block, and so we don't need to re-save it here.  */
18620   parser->in_statement = IN_OMP_FOR;
18621
18622   /* Note that the grammar doesn't call for a structured block here,
18623      though the loop as a whole is a structured block.  */
18624   body = push_stmt_list ();
18625   cp_parser_statement (parser, NULL_TREE, false);
18626   body = pop_stmt_list (body);
18627
18628   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18629 }
18630
18631 /* OpenMP 2.5:
18632    #pragma omp for for-clause[optseq] new-line
18633      for-loop  */
18634
18635 #define OMP_FOR_CLAUSE_MASK                             \
18636         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18637         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18638         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
18639         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
18640         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
18641         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
18642         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18643
18644 static tree
18645 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18646 {
18647   tree clauses, sb, ret;
18648   unsigned int save;
18649
18650   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18651                                        "#pragma omp for", pragma_tok);
18652
18653   sb = begin_omp_structured_block ();
18654   save = cp_parser_begin_omp_structured_block (parser);
18655
18656   ret = cp_parser_omp_for_loop (parser);
18657   if (ret)
18658     OMP_FOR_CLAUSES (ret) = clauses;
18659
18660   cp_parser_end_omp_structured_block (parser, save);
18661   add_stmt (finish_omp_structured_block (sb));
18662
18663   return ret;
18664 }
18665
18666 /* OpenMP 2.5:
18667    # pragma omp master new-line
18668      structured-block  */
18669
18670 static tree
18671 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18672 {
18673   cp_parser_require_pragma_eol (parser, pragma_tok);
18674   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18675 }
18676
18677 /* OpenMP 2.5:
18678    # pragma omp ordered new-line
18679      structured-block  */
18680
18681 static tree
18682 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18683 {
18684   cp_parser_require_pragma_eol (parser, pragma_tok);
18685   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18686 }
18687
18688 /* OpenMP 2.5:
18689
18690    section-scope:
18691      { section-sequence }
18692
18693    section-sequence:
18694      section-directive[opt] structured-block
18695      section-sequence section-directive structured-block  */
18696
18697 static tree
18698 cp_parser_omp_sections_scope (cp_parser *parser)
18699 {
18700   tree stmt, substmt;
18701   bool error_suppress = false;
18702   cp_token *tok;
18703
18704   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18705     return NULL_TREE;
18706
18707   stmt = push_stmt_list ();
18708
18709   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18710     {
18711       unsigned save;
18712
18713       substmt = begin_omp_structured_block ();
18714       save = cp_parser_begin_omp_structured_block (parser);
18715
18716       while (1)
18717         {
18718           cp_parser_statement (parser, NULL_TREE, false);
18719
18720           tok = cp_lexer_peek_token (parser->lexer);
18721           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18722             break;
18723           if (tok->type == CPP_CLOSE_BRACE)
18724             break;
18725           if (tok->type == CPP_EOF)
18726             break;
18727         }
18728
18729       cp_parser_end_omp_structured_block (parser, save);
18730       substmt = finish_omp_structured_block (substmt);
18731       substmt = build1 (OMP_SECTION, void_type_node, substmt);
18732       add_stmt (substmt);
18733     }
18734
18735   while (1)
18736     {
18737       tok = cp_lexer_peek_token (parser->lexer);
18738       if (tok->type == CPP_CLOSE_BRACE)
18739         break;
18740       if (tok->type == CPP_EOF)
18741         break;
18742
18743       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18744         {
18745           cp_lexer_consume_token (parser->lexer);
18746           cp_parser_require_pragma_eol (parser, tok);
18747           error_suppress = false;
18748         }
18749       else if (!error_suppress)
18750         {
18751           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
18752           error_suppress = true;
18753         }
18754
18755       substmt = cp_parser_omp_structured_block (parser);
18756       substmt = build1 (OMP_SECTION, void_type_node, substmt);
18757       add_stmt (substmt);
18758     }
18759   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
18760
18761   substmt = pop_stmt_list (stmt);
18762
18763   stmt = make_node (OMP_SECTIONS);
18764   TREE_TYPE (stmt) = void_type_node;
18765   OMP_SECTIONS_BODY (stmt) = substmt;
18766
18767   add_stmt (stmt);
18768   return stmt;
18769 }
18770
18771 /* OpenMP 2.5:
18772    # pragma omp sections sections-clause[optseq] newline
18773      sections-scope  */
18774
18775 #define OMP_SECTIONS_CLAUSE_MASK                        \
18776         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18777         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18778         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
18779         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
18780         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18781
18782 static tree
18783 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
18784 {
18785   tree clauses, ret;
18786
18787   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
18788                                        "#pragma omp sections", pragma_tok);
18789
18790   ret = cp_parser_omp_sections_scope (parser);
18791   if (ret)
18792     OMP_SECTIONS_CLAUSES (ret) = clauses;
18793
18794   return ret;
18795 }
18796
18797 /* OpenMP 2.5:
18798    # pragma parallel parallel-clause new-line
18799    # pragma parallel for parallel-for-clause new-line
18800    # pragma parallel sections parallel-sections-clause new-line  */
18801
18802 #define OMP_PARALLEL_CLAUSE_MASK                        \
18803         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
18804         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18805         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18806         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
18807         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
18808         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
18809         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
18810         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18811
18812 static tree
18813 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
18814 {
18815   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
18816   const char *p_name = "#pragma omp parallel";
18817   tree stmt, clauses, par_clause, ws_clause, block;
18818   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
18819   unsigned int save;
18820
18821   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18822     {
18823       cp_lexer_consume_token (parser->lexer);
18824       p_kind = PRAGMA_OMP_PARALLEL_FOR;
18825       p_name = "#pragma omp parallel for";
18826       mask |= OMP_FOR_CLAUSE_MASK;
18827       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18828     }
18829   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18830     {
18831       tree id = cp_lexer_peek_token (parser->lexer)->value;
18832       const char *p = IDENTIFIER_POINTER (id);
18833       if (strcmp (p, "sections") == 0)
18834         {
18835           cp_lexer_consume_token (parser->lexer);
18836           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
18837           p_name = "#pragma omp parallel sections";
18838           mask |= OMP_SECTIONS_CLAUSE_MASK;
18839           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18840         }
18841     }
18842
18843   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
18844   block = begin_omp_parallel ();
18845   save = cp_parser_begin_omp_structured_block (parser);
18846
18847   switch (p_kind)
18848     {
18849     case PRAGMA_OMP_PARALLEL:
18850       cp_parser_already_scoped_statement (parser);
18851       par_clause = clauses;
18852       break;
18853
18854     case PRAGMA_OMP_PARALLEL_FOR:
18855       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18856       stmt = cp_parser_omp_for_loop (parser);
18857       if (stmt)
18858         OMP_FOR_CLAUSES (stmt) = ws_clause;
18859       break;
18860
18861     case PRAGMA_OMP_PARALLEL_SECTIONS:
18862       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18863       stmt = cp_parser_omp_sections_scope (parser);
18864       if (stmt)
18865         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
18866       break;
18867
18868     default:
18869       gcc_unreachable ();
18870     }
18871
18872   cp_parser_end_omp_structured_block (parser, save);
18873   stmt = finish_omp_parallel (par_clause, block);
18874   if (p_kind != PRAGMA_OMP_PARALLEL)
18875     OMP_PARALLEL_COMBINED (stmt) = 1;
18876   return stmt;
18877 }
18878
18879 /* OpenMP 2.5:
18880    # pragma omp single single-clause[optseq] new-line
18881      structured-block  */
18882
18883 #define OMP_SINGLE_CLAUSE_MASK                          \
18884         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18885         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18886         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
18887         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18888
18889 static tree
18890 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
18891 {
18892   tree stmt = make_node (OMP_SINGLE);
18893   TREE_TYPE (stmt) = void_type_node;
18894
18895   OMP_SINGLE_CLAUSES (stmt)
18896     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18897                                  "#pragma omp single", pragma_tok);
18898   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
18899
18900   return add_stmt (stmt);
18901 }
18902
18903 /* OpenMP 2.5:
18904    # pragma omp threadprivate (variable-list) */
18905
18906 static void
18907 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
18908 {
18909   tree vars;
18910
18911   vars = cp_parser_omp_var_list (parser, 0, NULL);
18912   cp_parser_require_pragma_eol (parser, pragma_tok);
18913
18914   if (!targetm.have_tls)
18915     sorry ("threadprivate variables not supported in this target");
18916
18917   finish_omp_threadprivate (vars);
18918 }
18919
18920 /* Main entry point to OpenMP statement pragmas.  */
18921
18922 static void
18923 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
18924 {
18925   tree stmt;
18926
18927   switch (pragma_tok->pragma_kind)
18928     {
18929     case PRAGMA_OMP_ATOMIC:
18930       cp_parser_omp_atomic (parser, pragma_tok);
18931       return;
18932     case PRAGMA_OMP_CRITICAL:
18933       stmt = cp_parser_omp_critical (parser, pragma_tok);
18934       break;
18935     case PRAGMA_OMP_FOR:
18936       stmt = cp_parser_omp_for (parser, pragma_tok);
18937       break;
18938     case PRAGMA_OMP_MASTER:
18939       stmt = cp_parser_omp_master (parser, pragma_tok);
18940       break;
18941     case PRAGMA_OMP_ORDERED:
18942       stmt = cp_parser_omp_ordered (parser, pragma_tok);
18943       break;
18944     case PRAGMA_OMP_PARALLEL:
18945       stmt = cp_parser_omp_parallel (parser, pragma_tok);
18946       break;
18947     case PRAGMA_OMP_SECTIONS:
18948       stmt = cp_parser_omp_sections (parser, pragma_tok);
18949       break;
18950     case PRAGMA_OMP_SINGLE:
18951       stmt = cp_parser_omp_single (parser, pragma_tok);
18952       break;
18953     default:
18954       gcc_unreachable ();
18955     }
18956
18957   if (stmt)
18958     SET_EXPR_LOCATION (stmt, pragma_tok->location);
18959 }
18960 \f
18961 /* The parser.  */
18962
18963 static GTY (()) cp_parser *the_parser;
18964
18965 \f
18966 /* Special handling for the first token or line in the file.  The first
18967    thing in the file might be #pragma GCC pch_preprocess, which loads a
18968    PCH file, which is a GC collection point.  So we need to handle this
18969    first pragma without benefit of an existing lexer structure.
18970
18971    Always returns one token to the caller in *FIRST_TOKEN.  This is
18972    either the true first token of the file, or the first token after
18973    the initial pragma.  */
18974
18975 static void
18976 cp_parser_initial_pragma (cp_token *first_token)
18977 {
18978   tree name = NULL;
18979
18980   cp_lexer_get_preprocessor_token (NULL, first_token);
18981   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
18982     return;
18983
18984   cp_lexer_get_preprocessor_token (NULL, first_token);
18985   if (first_token->type == CPP_STRING)
18986     {
18987       name = first_token->value;
18988
18989       cp_lexer_get_preprocessor_token (NULL, first_token);
18990       if (first_token->type != CPP_PRAGMA_EOL)
18991         error ("junk at end of %<#pragma GCC pch_preprocess%>");
18992     }
18993   else
18994     error ("expected string literal");
18995
18996   /* Skip to the end of the pragma.  */
18997   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
18998     cp_lexer_get_preprocessor_token (NULL, first_token);
18999
19000   /* Now actually load the PCH file.  */
19001   if (name)
19002     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19003
19004   /* Read one more token to return to our caller.  We have to do this
19005      after reading the PCH file in, since its pointers have to be
19006      live.  */
19007   cp_lexer_get_preprocessor_token (NULL, first_token);
19008 }
19009
19010 /* Normal parsing of a pragma token.  Here we can (and must) use the
19011    regular lexer.  */
19012
19013 static bool
19014 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19015 {
19016   cp_token *pragma_tok;
19017   unsigned int id;
19018
19019   pragma_tok = cp_lexer_consume_token (parser->lexer);
19020   gcc_assert (pragma_tok->type == CPP_PRAGMA);
19021   parser->lexer->in_pragma = true;
19022
19023   id = pragma_tok->pragma_kind;
19024   switch (id)
19025     {
19026     case PRAGMA_GCC_PCH_PREPROCESS:
19027       error ("%<#pragma GCC pch_preprocess%> must be first");
19028       break;
19029
19030     case PRAGMA_OMP_BARRIER:
19031       switch (context)
19032         {
19033         case pragma_compound:
19034           cp_parser_omp_barrier (parser, pragma_tok);
19035           return false;
19036         case pragma_stmt:
19037           error ("%<#pragma omp barrier%> may only be "
19038                  "used in compound statements");
19039           break;
19040         default:
19041           goto bad_stmt;
19042         }
19043       break;
19044
19045     case PRAGMA_OMP_FLUSH:
19046       switch (context)
19047         {
19048         case pragma_compound:
19049           cp_parser_omp_flush (parser, pragma_tok);
19050           return false;
19051         case pragma_stmt:
19052           error ("%<#pragma omp flush%> may only be "
19053                  "used in compound statements");
19054           break;
19055         default:
19056           goto bad_stmt;
19057         }
19058       break;
19059
19060     case PRAGMA_OMP_THREADPRIVATE:
19061       cp_parser_omp_threadprivate (parser, pragma_tok);
19062       return false;
19063
19064     case PRAGMA_OMP_ATOMIC:
19065     case PRAGMA_OMP_CRITICAL:
19066     case PRAGMA_OMP_FOR:
19067     case PRAGMA_OMP_MASTER:
19068     case PRAGMA_OMP_ORDERED:
19069     case PRAGMA_OMP_PARALLEL:
19070     case PRAGMA_OMP_SECTIONS:
19071     case PRAGMA_OMP_SINGLE:
19072       if (context == pragma_external)
19073         goto bad_stmt;
19074       cp_parser_omp_construct (parser, pragma_tok);
19075       return true;
19076
19077     case PRAGMA_OMP_SECTION:
19078       error ("%<#pragma omp section%> may only be used in "
19079              "%<#pragma omp sections%> construct");
19080       break;
19081
19082     default:
19083       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19084       c_invoke_pragma_handler (id);
19085       break;
19086
19087     bad_stmt:
19088       cp_parser_error (parser, "expected declaration specifiers");
19089       break;
19090     }
19091
19092   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19093   return false;
19094 }
19095
19096 /* The interface the pragma parsers have to the lexer.  */
19097
19098 enum cpp_ttype
19099 pragma_lex (tree *value)
19100 {
19101   cp_token *tok;
19102   enum cpp_ttype ret;
19103
19104   tok = cp_lexer_peek_token (the_parser->lexer);
19105
19106   ret = tok->type;
19107   *value = tok->value;
19108
19109   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19110     ret = CPP_EOF;
19111   else if (ret == CPP_STRING)
19112     *value = cp_parser_string_literal (the_parser, false, false);
19113   else
19114     {
19115       cp_lexer_consume_token (the_parser->lexer);
19116       if (ret == CPP_KEYWORD)
19117         ret = CPP_NAME;
19118     }
19119
19120   return ret;
19121 }
19122
19123 \f
19124 /* External interface.  */
19125
19126 /* Parse one entire translation unit.  */
19127
19128 void
19129 c_parse_file (void)
19130 {
19131   bool error_occurred;
19132   static bool already_called = false;
19133
19134   if (already_called)
19135     {
19136       sorry ("inter-module optimizations not implemented for C++");
19137       return;
19138     }
19139   already_called = true;
19140
19141   the_parser = cp_parser_new ();
19142   push_deferring_access_checks (flag_access_control
19143                                 ? dk_no_deferred : dk_no_check);
19144   error_occurred = cp_parser_translation_unit (the_parser);
19145   the_parser = NULL;
19146 }
19147
19148 /* This variable must be provided by every front end.  */
19149
19150 int yydebug;
19151
19152 #include "gt-cp-parser.h"