OSDN Git Service

2006-09-02 Lee Millward <lee.millward@codesourcery.com>
[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 input file stack index at which this token was found.  */
70   unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
71   /* The value associated with this token, if any.  */
72   tree value;
73   /* The location at which this token was found.  */
74   location_t location;
75 } cp_token;
76
77 /* We use a stack of token pointer for saving token sets.  */
78 typedef struct cp_token *cp_token_position;
79 DEF_VEC_P (cp_token_position);
80 DEF_VEC_ALLOC_P (cp_token_position,heap);
81
82 static const cp_token eof_token =
83 {
84   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, NULL_TREE,
85 #if USE_MAPPED_LOCATION
86   0
87 #else
88   {0, 0}
89 #endif
90 };
91
92 /* The cp_lexer structure represents the C++ lexer.  It is responsible
93    for managing the token stream from the preprocessor and supplying
94    it to the parser.  Tokens are never added to the cp_lexer after
95    it is created.  */
96
97 typedef struct cp_lexer GTY (())
98 {
99   /* The memory allocated for the buffer.  NULL if this lexer does not
100      own the token buffer.  */
101   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
102   /* If the lexer owns the buffer, this is the number of tokens in the
103      buffer.  */
104   size_t buffer_length;
105
106   /* A pointer just past the last available token.  The tokens
107      in this lexer are [buffer, last_token).  */
108   cp_token_position GTY ((skip)) last_token;
109
110   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
111      no more available tokens.  */
112   cp_token_position GTY ((skip)) next_token;
113
114   /* A stack indicating positions at which cp_lexer_save_tokens was
115      called.  The top entry is the most recent position at which we
116      began saving tokens.  If the stack is non-empty, we are saving
117      tokens.  */
118   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
119
120   /* The next lexer in a linked list of lexers.  */
121   struct cp_lexer *next;
122
123   /* True if we should output debugging information.  */
124   bool debugging_p;
125
126   /* True if we're in the context of parsing a pragma, and should not
127      increment past the end-of-line marker.  */
128   bool in_pragma;
129 } cp_lexer;
130
131 /* cp_token_cache is a range of tokens.  There is no need to represent
132    allocate heap memory for it, since tokens are never removed from the
133    lexer's array.  There is also no need for the GC to walk through
134    a cp_token_cache, since everything in here is referenced through
135    a lexer.  */
136
137 typedef struct cp_token_cache GTY(())
138 {
139   /* The beginning of the token range.  */
140   cp_token * GTY((skip)) first;
141
142   /* Points immediately after the last token in the range.  */
143   cp_token * GTY ((skip)) last;
144 } cp_token_cache;
145
146 /* Prototypes.  */
147
148 static cp_lexer *cp_lexer_new_main
149   (void);
150 static cp_lexer *cp_lexer_new_from_tokens
151   (cp_token_cache *tokens);
152 static void cp_lexer_destroy
153   (cp_lexer *);
154 static int cp_lexer_saving_tokens
155   (const cp_lexer *);
156 static cp_token_position cp_lexer_token_position
157   (cp_lexer *, bool);
158 static cp_token *cp_lexer_token_at
159   (cp_lexer *, cp_token_position);
160 static void cp_lexer_get_preprocessor_token
161   (cp_lexer *, cp_token *);
162 static inline cp_token *cp_lexer_peek_token
163   (cp_lexer *);
164 static cp_token *cp_lexer_peek_nth_token
165   (cp_lexer *, size_t);
166 static inline bool cp_lexer_next_token_is
167   (cp_lexer *, enum cpp_ttype);
168 static bool cp_lexer_next_token_is_not
169   (cp_lexer *, enum cpp_ttype);
170 static bool cp_lexer_next_token_is_keyword
171   (cp_lexer *, enum rid);
172 static cp_token *cp_lexer_consume_token
173   (cp_lexer *);
174 static void cp_lexer_purge_token
175   (cp_lexer *);
176 static void cp_lexer_purge_tokens_after
177   (cp_lexer *, cp_token_position);
178 static void cp_lexer_save_tokens
179   (cp_lexer *);
180 static void cp_lexer_commit_tokens
181   (cp_lexer *);
182 static void cp_lexer_rollback_tokens
183   (cp_lexer *);
184 #ifdef ENABLE_CHECKING
185 static void cp_lexer_print_token
186   (FILE *, cp_token *);
187 static inline bool cp_lexer_debugging_p
188   (cp_lexer *);
189 static void cp_lexer_start_debugging
190   (cp_lexer *) ATTRIBUTE_UNUSED;
191 static void cp_lexer_stop_debugging
192   (cp_lexer *) ATTRIBUTE_UNUSED;
193 #else
194 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
195    about passing NULL to functions that require non-NULL arguments
196    (fputs, fprintf).  It will never be used, so all we need is a value
197    of the right type that's guaranteed not to be NULL.  */
198 #define cp_lexer_debug_stream stdout
199 #define cp_lexer_print_token(str, tok) (void) 0
200 #define cp_lexer_debugging_p(lexer) 0
201 #endif /* ENABLE_CHECKING */
202
203 static cp_token_cache *cp_token_cache_new
204   (cp_token *, cp_token *);
205
206 static void cp_parser_initial_pragma
207   (cp_token *);
208
209 /* Manifest constants.  */
210 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
211 #define CP_SAVED_TOKEN_STACK 5
212
213 /* A token type for keywords, as opposed to ordinary identifiers.  */
214 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
215
216 /* A token type for template-ids.  If a template-id is processed while
217    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
218    the value of the CPP_TEMPLATE_ID is whatever was returned by
219    cp_parser_template_id.  */
220 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
221
222 /* A token type for nested-name-specifiers.  If a
223    nested-name-specifier is processed while parsing tentatively, it is
224    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
225    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
226    cp_parser_nested_name_specifier_opt.  */
227 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
228
229 /* A token type for tokens that are not tokens at all; these are used
230    to represent slots in the array where there used to be a token
231    that has now been deleted.  */
232 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
233
234 /* The number of token types, including C++-specific ones.  */
235 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
236
237 /* Variables.  */
238
239 #ifdef ENABLE_CHECKING
240 /* The stream to which debugging output should be written.  */
241 static FILE *cp_lexer_debug_stream;
242 #endif /* ENABLE_CHECKING */
243
244 /* Create a new main C++ lexer, the lexer that gets tokens from the
245    preprocessor.  */
246
247 static cp_lexer *
248 cp_lexer_new_main (void)
249 {
250   cp_token first_token;
251   cp_lexer *lexer;
252   cp_token *pos;
253   size_t alloc;
254   size_t space;
255   cp_token *buffer;
256
257   /* It's possible that parsing the first pragma will load a PCH file,
258      which is a GC collection point.  So we have to do that before
259      allocating any memory.  */
260   cp_parser_initial_pragma (&first_token);
261
262   /* Tell c_lex_with_flags not to merge string constants.  */
263   c_lex_return_raw_strings = true;
264
265   c_common_no_more_pch ();
266
267   /* Allocate the memory.  */
268   lexer = GGC_CNEW (cp_lexer);
269
270 #ifdef ENABLE_CHECKING
271   /* Initially we are not debugging.  */
272   lexer->debugging_p = false;
273 #endif /* ENABLE_CHECKING */
274   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
275                                    CP_SAVED_TOKEN_STACK);
276
277   /* Create the buffer.  */
278   alloc = CP_LEXER_BUFFER_SIZE;
279   buffer = GGC_NEWVEC (cp_token, alloc);
280
281   /* Put the first token in the buffer.  */
282   space = alloc;
283   pos = buffer;
284   *pos = first_token;
285
286   /* Get the remaining tokens from the preprocessor.  */
287   while (pos->type != CPP_EOF)
288     {
289       pos++;
290       if (!--space)
291         {
292           space = alloc;
293           alloc *= 2;
294           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
295           pos = buffer + space;
296         }
297       cp_lexer_get_preprocessor_token (lexer, pos);
298     }
299   lexer->buffer = buffer;
300   lexer->buffer_length = alloc - space;
301   lexer->last_token = pos;
302   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
303
304   /* Subsequent preprocessor diagnostics should use compiler
305      diagnostic functions to get the compiler source location.  */
306   cpp_get_options (parse_in)->client_diagnostic = true;
307   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
308
309   gcc_assert (lexer->next_token->type != CPP_PURGED);
310   return lexer;
311 }
312
313 /* Create a new lexer whose token stream is primed with the tokens in
314    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
315
316 static cp_lexer *
317 cp_lexer_new_from_tokens (cp_token_cache *cache)
318 {
319   cp_token *first = cache->first;
320   cp_token *last = cache->last;
321   cp_lexer *lexer = GGC_CNEW (cp_lexer);
322
323   /* We do not own the buffer.  */
324   lexer->buffer = NULL;
325   lexer->buffer_length = 0;
326   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
327   lexer->last_token = last;
328
329   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
330                                    CP_SAVED_TOKEN_STACK);
331
332 #ifdef ENABLE_CHECKING
333   /* Initially we are not debugging.  */
334   lexer->debugging_p = false;
335 #endif
336
337   gcc_assert (lexer->next_token->type != CPP_PURGED);
338   return lexer;
339 }
340
341 /* Frees all resources associated with LEXER.  */
342
343 static void
344 cp_lexer_destroy (cp_lexer *lexer)
345 {
346   if (lexer->buffer)
347     ggc_free (lexer->buffer);
348   VEC_free (cp_token_position, heap, lexer->saved_tokens);
349   ggc_free (lexer);
350 }
351
352 /* Returns nonzero if debugging information should be output.  */
353
354 #ifdef ENABLE_CHECKING
355
356 static inline bool
357 cp_lexer_debugging_p (cp_lexer *lexer)
358 {
359   return lexer->debugging_p;
360 }
361
362 #endif /* ENABLE_CHECKING */
363
364 static inline cp_token_position
365 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
366 {
367   gcc_assert (!previous_p || lexer->next_token != &eof_token);
368
369   return lexer->next_token - previous_p;
370 }
371
372 static inline cp_token *
373 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
374 {
375   return pos;
376 }
377
378 /* nonzero if we are presently saving tokens.  */
379
380 static inline int
381 cp_lexer_saving_tokens (const cp_lexer* lexer)
382 {
383   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
384 }
385
386 /* Store the next token from the preprocessor in *TOKEN.  Return true
387    if we reach EOF.  */
388
389 static void
390 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
391                                  cp_token *token)
392 {
393   static int is_extern_c = 0;
394
395    /* Get a new token from the preprocessor.  */
396   token->type
397     = c_lex_with_flags (&token->value, &token->location, &token->flags);
398   token->input_file_stack_index = input_file_stack_tick;
399   token->keyword = RID_MAX;
400   token->pragma_kind = PRAGMA_NONE;
401   token->in_system_header = in_system_header;
402
403   /* On some systems, some header files are surrounded by an
404      implicit extern "C" block.  Set a flag in the token if it
405      comes from such a header.  */
406   is_extern_c += pending_lang_change;
407   pending_lang_change = 0;
408   token->implicit_extern_c = is_extern_c > 0;
409
410   /* Check to see if this token is a keyword.  */
411   if (token->type == CPP_NAME)
412     {
413       if (C_IS_RESERVED_WORD (token->value))
414         {
415           /* Mark this token as a keyword.  */
416           token->type = CPP_KEYWORD;
417           /* Record which keyword.  */
418           token->keyword = C_RID_CODE (token->value);
419           /* Update the value.  Some keywords are mapped to particular
420              entities, rather than simply having the value of the
421              corresponding IDENTIFIER_NODE.  For example, `__const' is
422              mapped to `const'.  */
423           token->value = ridpointers[token->keyword];
424         }
425       else
426         {
427           token->ambiguous_p = false;
428           token->keyword = RID_MAX;
429         }
430     }
431   /* Handle Objective-C++ keywords.  */
432   else if (token->type == CPP_AT_NAME)
433     {
434       token->type = CPP_KEYWORD;
435       switch (C_RID_CODE (token->value))
436         {
437         /* Map 'class' to '@class', 'private' to '@private', etc.  */
438         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
439         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
440         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
441         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
442         case RID_THROW: token->keyword = RID_AT_THROW; break;
443         case RID_TRY: token->keyword = RID_AT_TRY; break;
444         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
445         default: token->keyword = C_RID_CODE (token->value);
446         }
447     }
448   else if (token->type == CPP_PRAGMA)
449     {
450       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
451       token->pragma_kind = TREE_INT_CST_LOW (token->value);
452       token->value = NULL;
453     }
454 }
455
456 /* Update the globals input_location and in_system_header and the
457    input file stack from TOKEN.  */
458 static inline void
459 cp_lexer_set_source_position_from_token (cp_token *token)
460 {
461   if (token->type != CPP_EOF)
462     {
463       input_location = token->location;
464       in_system_header = token->in_system_header;
465       restore_input_file_stack (token->input_file_stack_index);
466     }
467 }
468
469 /* Return a pointer to the next token in the token stream, but do not
470    consume it.  */
471
472 static inline cp_token *
473 cp_lexer_peek_token (cp_lexer *lexer)
474 {
475   if (cp_lexer_debugging_p (lexer))
476     {
477       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
478       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
479       putc ('\n', cp_lexer_debug_stream);
480     }
481   return lexer->next_token;
482 }
483
484 /* Return true if the next token has the indicated TYPE.  */
485
486 static inline bool
487 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
488 {
489   return cp_lexer_peek_token (lexer)->type == type;
490 }
491
492 /* Return true if the next token does not have the indicated TYPE.  */
493
494 static inline bool
495 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
496 {
497   return !cp_lexer_next_token_is (lexer, type);
498 }
499
500 /* Return true if the next token is the indicated KEYWORD.  */
501
502 static inline bool
503 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
504 {
505   return cp_lexer_peek_token (lexer)->keyword == keyword;
506 }
507
508 /* Return a pointer to the Nth token in the token stream.  If N is 1,
509    then this is precisely equivalent to cp_lexer_peek_token (except
510    that it is not inline).  One would like to disallow that case, but
511    there is one case (cp_parser_nth_token_starts_template_id) where
512    the caller passes a variable for N and it might be 1.  */
513
514 static cp_token *
515 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
516 {
517   cp_token *token;
518
519   /* N is 1-based, not zero-based.  */
520   gcc_assert (n > 0);
521
522   if (cp_lexer_debugging_p (lexer))
523     fprintf (cp_lexer_debug_stream,
524              "cp_lexer: peeking ahead %ld at token: ", (long)n);
525
526   --n;
527   token = lexer->next_token;
528   gcc_assert (!n || token != &eof_token);
529   while (n != 0)
530     {
531       ++token;
532       if (token == lexer->last_token)
533         {
534           token = (cp_token *)&eof_token;
535           break;
536         }
537
538       if (token->type != CPP_PURGED)
539         --n;
540     }
541
542   if (cp_lexer_debugging_p (lexer))
543     {
544       cp_lexer_print_token (cp_lexer_debug_stream, token);
545       putc ('\n', cp_lexer_debug_stream);
546     }
547
548   return token;
549 }
550
551 /* Return the next token, and advance the lexer's next_token pointer
552    to point to the next non-purged token.  */
553
554 static cp_token *
555 cp_lexer_consume_token (cp_lexer* lexer)
556 {
557   cp_token *token = lexer->next_token;
558
559   gcc_assert (token != &eof_token);
560   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
561
562   do
563     {
564       lexer->next_token++;
565       if (lexer->next_token == lexer->last_token)
566         {
567           lexer->next_token = (cp_token *)&eof_token;
568           break;
569         }
570
571     }
572   while (lexer->next_token->type == CPP_PURGED);
573
574   cp_lexer_set_source_position_from_token (token);
575
576   /* Provide debugging output.  */
577   if (cp_lexer_debugging_p (lexer))
578     {
579       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
580       cp_lexer_print_token (cp_lexer_debug_stream, token);
581       putc ('\n', cp_lexer_debug_stream);
582     }
583
584   return token;
585 }
586
587 /* Permanently remove the next token from the token stream, and
588    advance the next_token pointer to refer to the next non-purged
589    token.  */
590
591 static void
592 cp_lexer_purge_token (cp_lexer *lexer)
593 {
594   cp_token *tok = lexer->next_token;
595
596   gcc_assert (tok != &eof_token);
597   tok->type = CPP_PURGED;
598   tok->location = UNKNOWN_LOCATION;
599   tok->value = NULL_TREE;
600   tok->keyword = RID_MAX;
601
602   do
603     {
604       tok++;
605       if (tok == lexer->last_token)
606         {
607           tok = (cp_token *)&eof_token;
608           break;
609         }
610     }
611   while (tok->type == CPP_PURGED);
612   lexer->next_token = tok;
613 }
614
615 /* Permanently remove all tokens after TOK, up to, but not
616    including, the token that will be returned next by
617    cp_lexer_peek_token.  */
618
619 static void
620 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
621 {
622   cp_token *peek = lexer->next_token;
623
624   if (peek == &eof_token)
625     peek = lexer->last_token;
626
627   gcc_assert (tok < peek);
628
629   for ( tok += 1; tok != peek; tok += 1)
630     {
631       tok->type = CPP_PURGED;
632       tok->location = UNKNOWN_LOCATION;
633       tok->value = NULL_TREE;
634       tok->keyword = RID_MAX;
635     }
636 }
637
638 /* Begin saving tokens.  All tokens consumed after this point will be
639    preserved.  */
640
641 static void
642 cp_lexer_save_tokens (cp_lexer* lexer)
643 {
644   /* Provide debugging output.  */
645   if (cp_lexer_debugging_p (lexer))
646     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
647
648   VEC_safe_push (cp_token_position, heap,
649                  lexer->saved_tokens, lexer->next_token);
650 }
651
652 /* Commit to the portion of the token stream most recently saved.  */
653
654 static void
655 cp_lexer_commit_tokens (cp_lexer* lexer)
656 {
657   /* Provide debugging output.  */
658   if (cp_lexer_debugging_p (lexer))
659     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
660
661   VEC_pop (cp_token_position, lexer->saved_tokens);
662 }
663
664 /* Return all tokens saved since the last call to cp_lexer_save_tokens
665    to the token stream.  Stop saving tokens.  */
666
667 static void
668 cp_lexer_rollback_tokens (cp_lexer* lexer)
669 {
670   /* Provide debugging output.  */
671   if (cp_lexer_debugging_p (lexer))
672     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
673
674   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
675 }
676
677 /* Print a representation of the TOKEN on the STREAM.  */
678
679 #ifdef ENABLE_CHECKING
680
681 static void
682 cp_lexer_print_token (FILE * stream, cp_token *token)
683 {
684   /* We don't use cpp_type2name here because the parser defines
685      a few tokens of its own.  */
686   static const char *const token_names[] = {
687     /* cpplib-defined token types */
688 #define OP(e, s) #e,
689 #define TK(e, s) #e,
690     TTYPE_TABLE
691 #undef OP
692 #undef TK
693     /* C++ parser token types - see "Manifest constants", above.  */
694     "KEYWORD",
695     "TEMPLATE_ID",
696     "NESTED_NAME_SPECIFIER",
697     "PURGED"
698   };
699
700   /* If we have a name for the token, print it out.  Otherwise, we
701      simply give the numeric code.  */
702   gcc_assert (token->type < ARRAY_SIZE(token_names));
703   fputs (token_names[token->type], stream);
704
705   /* For some tokens, print the associated data.  */
706   switch (token->type)
707     {
708     case CPP_KEYWORD:
709       /* Some keywords have a value that is not an IDENTIFIER_NODE.
710          For example, `struct' is mapped to an INTEGER_CST.  */
711       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
712         break;
713       /* else fall through */
714     case CPP_NAME:
715       fputs (IDENTIFIER_POINTER (token->value), stream);
716       break;
717
718     case CPP_STRING:
719     case CPP_WSTRING:
720       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
721       break;
722
723     default:
724       break;
725     }
726 }
727
728 /* Start emitting debugging information.  */
729
730 static void
731 cp_lexer_start_debugging (cp_lexer* lexer)
732 {
733   lexer->debugging_p = true;
734 }
735
736 /* Stop emitting debugging information.  */
737
738 static void
739 cp_lexer_stop_debugging (cp_lexer* lexer)
740 {
741   lexer->debugging_p = false;
742 }
743
744 #endif /* ENABLE_CHECKING */
745
746 /* Create a new cp_token_cache, representing a range of tokens.  */
747
748 static cp_token_cache *
749 cp_token_cache_new (cp_token *first, cp_token *last)
750 {
751   cp_token_cache *cache = GGC_NEW (cp_token_cache);
752   cache->first = first;
753   cache->last = last;
754   return cache;
755 }
756
757 \f
758 /* Decl-specifiers.  */
759
760 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
761
762 static void
763 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
764 {
765   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
766 }
767
768 /* Declarators.  */
769
770 /* Nothing other than the parser should be creating declarators;
771    declarators are a semi-syntactic representation of C++ entities.
772    Other parts of the front end that need to create entities (like
773    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
774
775 static cp_declarator *make_call_declarator
776   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
777 static cp_declarator *make_array_declarator
778   (cp_declarator *, tree);
779 static cp_declarator *make_pointer_declarator
780   (cp_cv_quals, cp_declarator *);
781 static cp_declarator *make_reference_declarator
782   (cp_cv_quals, cp_declarator *);
783 static cp_parameter_declarator *make_parameter_declarator
784   (cp_decl_specifier_seq *, cp_declarator *, tree);
785 static cp_declarator *make_ptrmem_declarator
786   (cp_cv_quals, tree, cp_declarator *);
787
788 /* An erroneous declarator.  */
789 static cp_declarator *cp_error_declarator;
790
791 /* The obstack on which declarators and related data structures are
792    allocated.  */
793 static struct obstack declarator_obstack;
794
795 /* Alloc BYTES from the declarator memory pool.  */
796
797 static inline void *
798 alloc_declarator (size_t bytes)
799 {
800   return obstack_alloc (&declarator_obstack, bytes);
801 }
802
803 /* Allocate a declarator of the indicated KIND.  Clear fields that are
804    common to all declarators.  */
805
806 static cp_declarator *
807 make_declarator (cp_declarator_kind kind)
808 {
809   cp_declarator *declarator;
810
811   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
812   declarator->kind = kind;
813   declarator->attributes = NULL_TREE;
814   declarator->declarator = NULL;
815
816   return declarator;
817 }
818
819 /* Make a declarator for a generalized identifier.  If
820    QUALIFYING_SCOPE is non-NULL, the identifier is
821    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
822    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
823    is, if any.   */
824
825 static cp_declarator *
826 make_id_declarator (tree qualifying_scope, tree unqualified_name,
827                     special_function_kind sfk)
828 {
829   cp_declarator *declarator;
830
831   /* It is valid to write:
832
833        class C { void f(); };
834        typedef C D;
835        void D::f();
836
837      The standard is not clear about whether `typedef const C D' is
838      legal; as of 2002-09-15 the committee is considering that
839      question.  EDG 3.0 allows that syntax.  Therefore, we do as
840      well.  */
841   if (qualifying_scope && TYPE_P (qualifying_scope))
842     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
843
844   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
845               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
846               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
847
848   declarator = make_declarator (cdk_id);
849   declarator->u.id.qualifying_scope = qualifying_scope;
850   declarator->u.id.unqualified_name = unqualified_name;
851   declarator->u.id.sfk = sfk;
852
853   return declarator;
854 }
855
856 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
857    of modifiers such as const or volatile to apply to the pointer
858    type, represented as identifiers.  */
859
860 cp_declarator *
861 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
862 {
863   cp_declarator *declarator;
864
865   declarator = make_declarator (cdk_pointer);
866   declarator->declarator = target;
867   declarator->u.pointer.qualifiers = cv_qualifiers;
868   declarator->u.pointer.class_type = NULL_TREE;
869
870   return declarator;
871 }
872
873 /* Like make_pointer_declarator -- but for references.  */
874
875 cp_declarator *
876 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
877 {
878   cp_declarator *declarator;
879
880   declarator = make_declarator (cdk_reference);
881   declarator->declarator = target;
882   declarator->u.pointer.qualifiers = cv_qualifiers;
883   declarator->u.pointer.class_type = NULL_TREE;
884
885   return declarator;
886 }
887
888 /* Like make_pointer_declarator -- but for a pointer to a non-static
889    member of CLASS_TYPE.  */
890
891 cp_declarator *
892 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
893                         cp_declarator *pointee)
894 {
895   cp_declarator *declarator;
896
897   declarator = make_declarator (cdk_ptrmem);
898   declarator->declarator = pointee;
899   declarator->u.pointer.qualifiers = cv_qualifiers;
900   declarator->u.pointer.class_type = class_type;
901
902   return declarator;
903 }
904
905 /* Make a declarator for the function given by TARGET, with the
906    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
907    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
908    indicates what exceptions can be thrown.  */
909
910 cp_declarator *
911 make_call_declarator (cp_declarator *target,
912                       cp_parameter_declarator *parms,
913                       cp_cv_quals cv_qualifiers,
914                       tree exception_specification)
915 {
916   cp_declarator *declarator;
917
918   declarator = make_declarator (cdk_function);
919   declarator->declarator = target;
920   declarator->u.function.parameters = parms;
921   declarator->u.function.qualifiers = cv_qualifiers;
922   declarator->u.function.exception_specification = exception_specification;
923
924   return declarator;
925 }
926
927 /* Make a declarator for an array of BOUNDS elements, each of which is
928    defined by ELEMENT.  */
929
930 cp_declarator *
931 make_array_declarator (cp_declarator *element, tree bounds)
932 {
933   cp_declarator *declarator;
934
935   declarator = make_declarator (cdk_array);
936   declarator->declarator = element;
937   declarator->u.array.bounds = bounds;
938
939   return declarator;
940 }
941
942 cp_parameter_declarator *no_parameters;
943
944 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
945    DECLARATOR and DEFAULT_ARGUMENT.  */
946
947 cp_parameter_declarator *
948 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
949                            cp_declarator *declarator,
950                            tree default_argument)
951 {
952   cp_parameter_declarator *parameter;
953
954   parameter = ((cp_parameter_declarator *)
955                alloc_declarator (sizeof (cp_parameter_declarator)));
956   parameter->next = NULL;
957   if (decl_specifiers)
958     parameter->decl_specifiers = *decl_specifiers;
959   else
960     clear_decl_specs (&parameter->decl_specifiers);
961   parameter->declarator = declarator;
962   parameter->default_argument = default_argument;
963   parameter->ellipsis_p = false;
964
965   return parameter;
966 }
967
968 /* The parser.  */
969
970 /* Overview
971    --------
972
973    A cp_parser parses the token stream as specified by the C++
974    grammar.  Its job is purely parsing, not semantic analysis.  For
975    example, the parser breaks the token stream into declarators,
976    expressions, statements, and other similar syntactic constructs.
977    It does not check that the types of the expressions on either side
978    of an assignment-statement are compatible, or that a function is
979    not declared with a parameter of type `void'.
980
981    The parser invokes routines elsewhere in the compiler to perform
982    semantic analysis and to build up the abstract syntax tree for the
983    code processed.
984
985    The parser (and the template instantiation code, which is, in a
986    way, a close relative of parsing) are the only parts of the
987    compiler that should be calling push_scope and pop_scope, or
988    related functions.  The parser (and template instantiation code)
989    keeps track of what scope is presently active; everything else
990    should simply honor that.  (The code that generates static
991    initializers may also need to set the scope, in order to check
992    access control correctly when emitting the initializers.)
993
994    Methodology
995    -----------
996
997    The parser is of the standard recursive-descent variety.  Upcoming
998    tokens in the token stream are examined in order to determine which
999    production to use when parsing a non-terminal.  Some C++ constructs
1000    require arbitrary look ahead to disambiguate.  For example, it is
1001    impossible, in the general case, to tell whether a statement is an
1002    expression or declaration without scanning the entire statement.
1003    Therefore, the parser is capable of "parsing tentatively."  When the
1004    parser is not sure what construct comes next, it enters this mode.
1005    Then, while we attempt to parse the construct, the parser queues up
1006    error messages, rather than issuing them immediately, and saves the
1007    tokens it consumes.  If the construct is parsed successfully, the
1008    parser "commits", i.e., it issues any queued error messages and
1009    the tokens that were being preserved are permanently discarded.
1010    If, however, the construct is not parsed successfully, the parser
1011    rolls back its state completely so that it can resume parsing using
1012    a different alternative.
1013
1014    Future Improvements
1015    -------------------
1016
1017    The performance of the parser could probably be improved substantially.
1018    We could often eliminate the need to parse tentatively by looking ahead
1019    a little bit.  In some places, this approach might not entirely eliminate
1020    the need to parse tentatively, but it might still speed up the average
1021    case.  */
1022
1023 /* Flags that are passed to some parsing functions.  These values can
1024    be bitwise-ored together.  */
1025
1026 typedef enum cp_parser_flags
1027 {
1028   /* No flags.  */
1029   CP_PARSER_FLAGS_NONE = 0x0,
1030   /* The construct is optional.  If it is not present, then no error
1031      should be issued.  */
1032   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1033   /* When parsing a type-specifier, do not allow user-defined types.  */
1034   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1035 } cp_parser_flags;
1036
1037 /* The different kinds of declarators we want to parse.  */
1038
1039 typedef enum cp_parser_declarator_kind
1040 {
1041   /* We want an abstract declarator.  */
1042   CP_PARSER_DECLARATOR_ABSTRACT,
1043   /* We want a named declarator.  */
1044   CP_PARSER_DECLARATOR_NAMED,
1045   /* We don't mind, but the name must be an unqualified-id.  */
1046   CP_PARSER_DECLARATOR_EITHER
1047 } cp_parser_declarator_kind;
1048
1049 /* The precedence values used to parse binary expressions.  The minimum value
1050    of PREC must be 1, because zero is reserved to quickly discriminate
1051    binary operators from other tokens.  */
1052
1053 enum cp_parser_prec
1054 {
1055   PREC_NOT_OPERATOR,
1056   PREC_LOGICAL_OR_EXPRESSION,
1057   PREC_LOGICAL_AND_EXPRESSION,
1058   PREC_INCLUSIVE_OR_EXPRESSION,
1059   PREC_EXCLUSIVE_OR_EXPRESSION,
1060   PREC_AND_EXPRESSION,
1061   PREC_EQUALITY_EXPRESSION,
1062   PREC_RELATIONAL_EXPRESSION,
1063   PREC_SHIFT_EXPRESSION,
1064   PREC_ADDITIVE_EXPRESSION,
1065   PREC_MULTIPLICATIVE_EXPRESSION,
1066   PREC_PM_EXPRESSION,
1067   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1068 };
1069
1070 /* A mapping from a token type to a corresponding tree node type, with a
1071    precedence value.  */
1072
1073 typedef struct cp_parser_binary_operations_map_node
1074 {
1075   /* The token type.  */
1076   enum cpp_ttype token_type;
1077   /* The corresponding tree code.  */
1078   enum tree_code tree_type;
1079   /* The precedence of this operator.  */
1080   enum cp_parser_prec prec;
1081 } cp_parser_binary_operations_map_node;
1082
1083 /* The status of a tentative parse.  */
1084
1085 typedef enum cp_parser_status_kind
1086 {
1087   /* No errors have occurred.  */
1088   CP_PARSER_STATUS_KIND_NO_ERROR,
1089   /* An error has occurred.  */
1090   CP_PARSER_STATUS_KIND_ERROR,
1091   /* We are committed to this tentative parse, whether or not an error
1092      has occurred.  */
1093   CP_PARSER_STATUS_KIND_COMMITTED
1094 } cp_parser_status_kind;
1095
1096 typedef struct cp_parser_expression_stack_entry
1097 {
1098   tree lhs;
1099   enum tree_code tree_type;
1100   int prec;
1101 } cp_parser_expression_stack_entry;
1102
1103 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1104    entries because precedence levels on the stack are monotonically
1105    increasing.  */
1106 typedef struct cp_parser_expression_stack_entry
1107   cp_parser_expression_stack[NUM_PREC_VALUES];
1108
1109 /* Context that is saved and restored when parsing tentatively.  */
1110 typedef struct cp_parser_context GTY (())
1111 {
1112   /* If this is a tentative parsing context, the status of the
1113      tentative parse.  */
1114   enum cp_parser_status_kind status;
1115   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1116      that are looked up in this context must be looked up both in the
1117      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1118      the context of the containing expression.  */
1119   tree object_type;
1120
1121   /* The next parsing context in the stack.  */
1122   struct cp_parser_context *next;
1123 } cp_parser_context;
1124
1125 /* Prototypes.  */
1126
1127 /* Constructors and destructors.  */
1128
1129 static cp_parser_context *cp_parser_context_new
1130   (cp_parser_context *);
1131
1132 /* Class variables.  */
1133
1134 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1135
1136 /* The operator-precedence table used by cp_parser_binary_expression.
1137    Transformed into an associative array (binops_by_token) by
1138    cp_parser_new.  */
1139
1140 static const cp_parser_binary_operations_map_node binops[] = {
1141   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1142   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1143
1144   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1145   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1146   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1147
1148   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1149   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1150
1151   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1152   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1153
1154   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1155   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1156   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1157   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1158
1159   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1160   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1161
1162   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1163
1164   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1165
1166   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1167
1168   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1169
1170   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1171 };
1172
1173 /* The same as binops, but initialized by cp_parser_new so that
1174    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1175    for speed.  */
1176 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1177
1178 /* Constructors and destructors.  */
1179
1180 /* Construct a new context.  The context below this one on the stack
1181    is given by NEXT.  */
1182
1183 static cp_parser_context *
1184 cp_parser_context_new (cp_parser_context* next)
1185 {
1186   cp_parser_context *context;
1187
1188   /* Allocate the storage.  */
1189   if (cp_parser_context_free_list != NULL)
1190     {
1191       /* Pull the first entry from the free list.  */
1192       context = cp_parser_context_free_list;
1193       cp_parser_context_free_list = context->next;
1194       memset (context, 0, sizeof (*context));
1195     }
1196   else
1197     context = GGC_CNEW (cp_parser_context);
1198
1199   /* No errors have occurred yet in this context.  */
1200   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1201   /* If this is not the bottomost context, copy information that we
1202      need from the previous context.  */
1203   if (next)
1204     {
1205       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1206          expression, then we are parsing one in this context, too.  */
1207       context->object_type = next->object_type;
1208       /* Thread the stack.  */
1209       context->next = next;
1210     }
1211
1212   return context;
1213 }
1214
1215 /* The cp_parser structure represents the C++ parser.  */
1216
1217 typedef struct cp_parser GTY(())
1218 {
1219   /* The lexer from which we are obtaining tokens.  */
1220   cp_lexer *lexer;
1221
1222   /* The scope in which names should be looked up.  If NULL_TREE, then
1223      we look up names in the scope that is currently open in the
1224      source program.  If non-NULL, this is either a TYPE or
1225      NAMESPACE_DECL for the scope in which we should look.  It can
1226      also be ERROR_MARK, when we've parsed a bogus scope.
1227
1228      This value is not cleared automatically after a name is looked
1229      up, so we must be careful to clear it before starting a new look
1230      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1231      will look up `Z' in the scope of `X', rather than the current
1232      scope.)  Unfortunately, it is difficult to tell when name lookup
1233      is complete, because we sometimes peek at a token, look it up,
1234      and then decide not to consume it.   */
1235   tree scope;
1236
1237   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1238      last lookup took place.  OBJECT_SCOPE is used if an expression
1239      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1240      respectively.  QUALIFYING_SCOPE is used for an expression of the
1241      form "X::Y"; it refers to X.  */
1242   tree object_scope;
1243   tree qualifying_scope;
1244
1245   /* A stack of parsing contexts.  All but the bottom entry on the
1246      stack will be tentative contexts.
1247
1248      We parse tentatively in order to determine which construct is in
1249      use in some situations.  For example, in order to determine
1250      whether a statement is an expression-statement or a
1251      declaration-statement we parse it tentatively as a
1252      declaration-statement.  If that fails, we then reparse the same
1253      token stream as an expression-statement.  */
1254   cp_parser_context *context;
1255
1256   /* True if we are parsing GNU C++.  If this flag is not set, then
1257      GNU extensions are not recognized.  */
1258   bool allow_gnu_extensions_p;
1259
1260   /* TRUE if the `>' token should be interpreted as the greater-than
1261      operator.  FALSE if it is the end of a template-id or
1262      template-parameter-list.  */
1263   bool greater_than_is_operator_p;
1264
1265   /* TRUE if default arguments are allowed within a parameter list
1266      that starts at this point. FALSE if only a gnu extension makes
1267      them permissible.  */
1268   bool default_arg_ok_p;
1269
1270   /* TRUE if we are parsing an integral constant-expression.  See
1271      [expr.const] for a precise definition.  */
1272   bool integral_constant_expression_p;
1273
1274   /* TRUE if we are parsing an integral constant-expression -- but a
1275      non-constant expression should be permitted as well.  This flag
1276      is used when parsing an array bound so that GNU variable-length
1277      arrays are tolerated.  */
1278   bool allow_non_integral_constant_expression_p;
1279
1280   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1281      been seen that makes the expression non-constant.  */
1282   bool non_integral_constant_expression_p;
1283
1284   /* TRUE if local variable names and `this' are forbidden in the
1285      current context.  */
1286   bool local_variables_forbidden_p;
1287
1288   /* TRUE if the declaration we are parsing is part of a
1289      linkage-specification of the form `extern string-literal
1290      declaration'.  */
1291   bool in_unbraced_linkage_specification_p;
1292
1293   /* TRUE if we are presently parsing a declarator, after the
1294      direct-declarator.  */
1295   bool in_declarator_p;
1296
1297   /* TRUE if we are presently parsing a template-argument-list.  */
1298   bool in_template_argument_list_p;
1299
1300   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1301      to IN_OMP_BLOCK if parsing OpenMP structured block and
1302      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1303      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1304      iteration-statement, OpenMP block or loop within that switch.  */
1305 #define IN_SWITCH_STMT          1
1306 #define IN_ITERATION_STMT       2
1307 #define IN_OMP_BLOCK            4
1308 #define IN_OMP_FOR              8
1309   unsigned char in_statement;
1310
1311   /* TRUE if we are presently parsing the body of a switch statement.
1312      Note that this doesn't quite overlap with in_statement above.
1313      The difference relates to giving the right sets of error messages:
1314      "case not in switch" vs "break statement used with OpenMP...".  */
1315   bool in_switch_statement_p;
1316
1317   /* TRUE if we are parsing a type-id in an expression context.  In
1318      such a situation, both "type (expr)" and "type (type)" are valid
1319      alternatives.  */
1320   bool in_type_id_in_expr_p;
1321
1322   /* TRUE if we are currently in a header file where declarations are
1323      implicitly extern "C".  */
1324   bool implicit_extern_c;
1325
1326   /* TRUE if strings in expressions should be translated to the execution
1327      character set.  */
1328   bool translate_strings_p;
1329
1330   /* If non-NULL, then we are parsing a construct where new type
1331      definitions are not permitted.  The string stored here will be
1332      issued as an error message if a type is defined.  */
1333   const char *type_definition_forbidden_message;
1334
1335   /* A list of lists. The outer list is a stack, used for member
1336      functions of local classes. At each level there are two sub-list,
1337      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1338      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1339      TREE_VALUE's. The functions are chained in reverse declaration
1340      order.
1341
1342      The TREE_PURPOSE sublist contains those functions with default
1343      arguments that need post processing, and the TREE_VALUE sublist
1344      contains those functions with definitions that need post
1345      processing.
1346
1347      These lists can only be processed once the outermost class being
1348      defined is complete.  */
1349   tree unparsed_functions_queues;
1350
1351   /* The number of classes whose definitions are currently in
1352      progress.  */
1353   unsigned num_classes_being_defined;
1354
1355   /* The number of template parameter lists that apply directly to the
1356      current declaration.  */
1357   unsigned num_template_parameter_lists;
1358 } cp_parser;
1359
1360 /* Prototypes.  */
1361
1362 /* Constructors and destructors.  */
1363
1364 static cp_parser *cp_parser_new
1365   (void);
1366
1367 /* Routines to parse various constructs.
1368
1369    Those that return `tree' will return the error_mark_node (rather
1370    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1371    Sometimes, they will return an ordinary node if error-recovery was
1372    attempted, even though a parse error occurred.  So, to check
1373    whether or not a parse error occurred, you should always use
1374    cp_parser_error_occurred.  If the construct is optional (indicated
1375    either by an `_opt' in the name of the function that does the
1376    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1377    the construct is not present.  */
1378
1379 /* Lexical conventions [gram.lex]  */
1380
1381 static tree cp_parser_identifier
1382   (cp_parser *);
1383 static tree cp_parser_string_literal
1384   (cp_parser *, bool, bool);
1385
1386 /* Basic concepts [gram.basic]  */
1387
1388 static bool cp_parser_translation_unit
1389   (cp_parser *);
1390
1391 /* Expressions [gram.expr]  */
1392
1393 static tree cp_parser_primary_expression
1394   (cp_parser *, bool, bool, bool, cp_id_kind *);
1395 static tree cp_parser_id_expression
1396   (cp_parser *, bool, bool, bool *, bool, bool, bool);
1397 static tree cp_parser_unqualified_id
1398   (cp_parser *, bool, bool, bool, bool, bool);
1399 static tree cp_parser_nested_name_specifier_opt
1400   (cp_parser *, bool, bool, bool, bool);
1401 static tree cp_parser_nested_name_specifier
1402   (cp_parser *, bool, bool, bool, bool);
1403 static tree cp_parser_class_or_namespace_name
1404   (cp_parser *, bool, bool, bool, bool, bool);
1405 static tree cp_parser_postfix_expression
1406   (cp_parser *, bool, bool);
1407 static tree cp_parser_postfix_open_square_expression
1408   (cp_parser *, tree, bool);
1409 static tree cp_parser_postfix_dot_deref_expression
1410   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1411 static tree cp_parser_parenthesized_expression_list
1412   (cp_parser *, bool, bool, bool *);
1413 static void cp_parser_pseudo_destructor_name
1414   (cp_parser *, tree *, tree *);
1415 static tree cp_parser_unary_expression
1416   (cp_parser *, bool, bool);
1417 static enum tree_code cp_parser_unary_operator
1418   (cp_token *);
1419 static tree cp_parser_new_expression
1420   (cp_parser *);
1421 static tree cp_parser_new_placement
1422   (cp_parser *);
1423 static tree cp_parser_new_type_id
1424   (cp_parser *, tree *);
1425 static cp_declarator *cp_parser_new_declarator_opt
1426   (cp_parser *);
1427 static cp_declarator *cp_parser_direct_new_declarator
1428   (cp_parser *);
1429 static tree cp_parser_new_initializer
1430   (cp_parser *);
1431 static tree cp_parser_delete_expression
1432   (cp_parser *);
1433 static tree cp_parser_cast_expression
1434   (cp_parser *, bool, bool);
1435 static tree cp_parser_binary_expression
1436   (cp_parser *, bool);
1437 static tree cp_parser_question_colon_clause
1438   (cp_parser *, tree);
1439 static tree cp_parser_assignment_expression
1440   (cp_parser *, bool);
1441 static enum tree_code cp_parser_assignment_operator_opt
1442   (cp_parser *);
1443 static tree cp_parser_expression
1444   (cp_parser *, bool);
1445 static tree cp_parser_constant_expression
1446   (cp_parser *, bool, bool *);
1447 static tree cp_parser_builtin_offsetof
1448   (cp_parser *);
1449
1450 /* Statements [gram.stmt.stmt]  */
1451
1452 static void cp_parser_statement
1453   (cp_parser *, tree, bool);
1454 static tree cp_parser_labeled_statement
1455   (cp_parser *, tree, bool);
1456 static tree cp_parser_expression_statement
1457   (cp_parser *, tree);
1458 static tree cp_parser_compound_statement
1459   (cp_parser *, tree, bool);
1460 static void cp_parser_statement_seq_opt
1461   (cp_parser *, tree);
1462 static tree cp_parser_selection_statement
1463   (cp_parser *);
1464 static tree cp_parser_condition
1465   (cp_parser *);
1466 static tree cp_parser_iteration_statement
1467   (cp_parser *);
1468 static void cp_parser_for_init_statement
1469   (cp_parser *);
1470 static tree cp_parser_jump_statement
1471   (cp_parser *);
1472 static void cp_parser_declaration_statement
1473   (cp_parser *);
1474
1475 static tree cp_parser_implicitly_scoped_statement
1476   (cp_parser *);
1477 static void cp_parser_already_scoped_statement
1478   (cp_parser *);
1479
1480 /* Declarations [gram.dcl.dcl] */
1481
1482 static void cp_parser_declaration_seq_opt
1483   (cp_parser *);
1484 static void cp_parser_declaration
1485   (cp_parser *);
1486 static void cp_parser_block_declaration
1487   (cp_parser *, bool);
1488 static void cp_parser_simple_declaration
1489   (cp_parser *, bool);
1490 static void cp_parser_decl_specifier_seq
1491   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1492 static tree cp_parser_storage_class_specifier_opt
1493   (cp_parser *);
1494 static tree cp_parser_function_specifier_opt
1495   (cp_parser *, cp_decl_specifier_seq *);
1496 static tree cp_parser_type_specifier
1497   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1498    int *, bool *);
1499 static tree cp_parser_simple_type_specifier
1500   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1501 static tree cp_parser_type_name
1502   (cp_parser *);
1503 static tree cp_parser_elaborated_type_specifier
1504   (cp_parser *, bool, bool);
1505 static tree cp_parser_enum_specifier
1506   (cp_parser *);
1507 static void cp_parser_enumerator_list
1508   (cp_parser *, tree);
1509 static void cp_parser_enumerator_definition
1510   (cp_parser *, tree);
1511 static tree cp_parser_namespace_name
1512   (cp_parser *);
1513 static void cp_parser_namespace_definition
1514   (cp_parser *);
1515 static void cp_parser_namespace_body
1516   (cp_parser *);
1517 static tree cp_parser_qualified_namespace_specifier
1518   (cp_parser *);
1519 static void cp_parser_namespace_alias_definition
1520   (cp_parser *);
1521 static void cp_parser_using_declaration
1522   (cp_parser *);
1523 static void cp_parser_using_directive
1524   (cp_parser *);
1525 static void cp_parser_asm_definition
1526   (cp_parser *);
1527 static void cp_parser_linkage_specification
1528   (cp_parser *);
1529
1530 /* Declarators [gram.dcl.decl] */
1531
1532 static tree cp_parser_init_declarator
1533   (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
1534 static cp_declarator *cp_parser_declarator
1535   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1536 static cp_declarator *cp_parser_direct_declarator
1537   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1538 static enum tree_code cp_parser_ptr_operator
1539   (cp_parser *, tree *, cp_cv_quals *);
1540 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1541   (cp_parser *);
1542 static tree cp_parser_declarator_id
1543   (cp_parser *, bool);
1544 static tree cp_parser_type_id
1545   (cp_parser *);
1546 static void cp_parser_type_specifier_seq
1547   (cp_parser *, bool, cp_decl_specifier_seq *);
1548 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1549   (cp_parser *);
1550 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1551   (cp_parser *, bool *);
1552 static cp_parameter_declarator *cp_parser_parameter_declaration
1553   (cp_parser *, bool, bool *);
1554 static void cp_parser_function_body
1555   (cp_parser *);
1556 static tree cp_parser_initializer
1557   (cp_parser *, bool *, bool *);
1558 static tree cp_parser_initializer_clause
1559   (cp_parser *, bool *);
1560 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1561   (cp_parser *, bool *);
1562
1563 static bool cp_parser_ctor_initializer_opt_and_function_body
1564   (cp_parser *);
1565
1566 /* Classes [gram.class] */
1567
1568 static tree cp_parser_class_name
1569   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1570 static tree cp_parser_class_specifier
1571   (cp_parser *);
1572 static tree cp_parser_class_head
1573   (cp_parser *, bool *, tree *);
1574 static enum tag_types cp_parser_class_key
1575   (cp_parser *);
1576 static void cp_parser_member_specification_opt
1577   (cp_parser *);
1578 static void cp_parser_member_declaration
1579   (cp_parser *);
1580 static tree cp_parser_pure_specifier
1581   (cp_parser *);
1582 static tree cp_parser_constant_initializer
1583   (cp_parser *);
1584
1585 /* Derived classes [gram.class.derived] */
1586
1587 static tree cp_parser_base_clause
1588   (cp_parser *);
1589 static tree cp_parser_base_specifier
1590   (cp_parser *);
1591
1592 /* Special member functions [gram.special] */
1593
1594 static tree cp_parser_conversion_function_id
1595   (cp_parser *);
1596 static tree cp_parser_conversion_type_id
1597   (cp_parser *);
1598 static cp_declarator *cp_parser_conversion_declarator_opt
1599   (cp_parser *);
1600 static bool cp_parser_ctor_initializer_opt
1601   (cp_parser *);
1602 static void cp_parser_mem_initializer_list
1603   (cp_parser *);
1604 static tree cp_parser_mem_initializer
1605   (cp_parser *);
1606 static tree cp_parser_mem_initializer_id
1607   (cp_parser *);
1608
1609 /* Overloading [gram.over] */
1610
1611 static tree cp_parser_operator_function_id
1612   (cp_parser *);
1613 static tree cp_parser_operator
1614   (cp_parser *);
1615
1616 /* Templates [gram.temp] */
1617
1618 static void cp_parser_template_declaration
1619   (cp_parser *, bool);
1620 static tree cp_parser_template_parameter_list
1621   (cp_parser *);
1622 static tree cp_parser_template_parameter
1623   (cp_parser *, bool *);
1624 static tree cp_parser_type_parameter
1625   (cp_parser *);
1626 static tree cp_parser_template_id
1627   (cp_parser *, bool, bool, bool);
1628 static tree cp_parser_template_name
1629   (cp_parser *, bool, bool, bool, bool *);
1630 static tree cp_parser_template_argument_list
1631   (cp_parser *);
1632 static tree cp_parser_template_argument
1633   (cp_parser *);
1634 static void cp_parser_explicit_instantiation
1635   (cp_parser *);
1636 static void cp_parser_explicit_specialization
1637   (cp_parser *);
1638
1639 /* Exception handling [gram.exception] */
1640
1641 static tree cp_parser_try_block
1642   (cp_parser *);
1643 static bool cp_parser_function_try_block
1644   (cp_parser *);
1645 static void cp_parser_handler_seq
1646   (cp_parser *);
1647 static void cp_parser_handler
1648   (cp_parser *);
1649 static tree cp_parser_exception_declaration
1650   (cp_parser *);
1651 static tree cp_parser_throw_expression
1652   (cp_parser *);
1653 static tree cp_parser_exception_specification_opt
1654   (cp_parser *);
1655 static tree cp_parser_type_id_list
1656   (cp_parser *);
1657
1658 /* GNU Extensions */
1659
1660 static tree cp_parser_asm_specification_opt
1661   (cp_parser *);
1662 static tree cp_parser_asm_operand_list
1663   (cp_parser *);
1664 static tree cp_parser_asm_clobber_list
1665   (cp_parser *);
1666 static tree cp_parser_attributes_opt
1667   (cp_parser *);
1668 static tree cp_parser_attribute_list
1669   (cp_parser *);
1670 static bool cp_parser_extension_opt
1671   (cp_parser *, int *);
1672 static void cp_parser_label_declaration
1673   (cp_parser *);
1674
1675 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1676 static bool cp_parser_pragma
1677   (cp_parser *, enum pragma_context);
1678
1679 /* Objective-C++ Productions */
1680
1681 static tree cp_parser_objc_message_receiver
1682   (cp_parser *);
1683 static tree cp_parser_objc_message_args
1684   (cp_parser *);
1685 static tree cp_parser_objc_message_expression
1686   (cp_parser *);
1687 static tree cp_parser_objc_encode_expression
1688   (cp_parser *);
1689 static tree cp_parser_objc_defs_expression
1690   (cp_parser *);
1691 static tree cp_parser_objc_protocol_expression
1692   (cp_parser *);
1693 static tree cp_parser_objc_selector_expression
1694   (cp_parser *);
1695 static tree cp_parser_objc_expression
1696   (cp_parser *);
1697 static bool cp_parser_objc_selector_p
1698   (enum cpp_ttype);
1699 static tree cp_parser_objc_selector
1700   (cp_parser *);
1701 static tree cp_parser_objc_protocol_refs_opt
1702   (cp_parser *);
1703 static void cp_parser_objc_declaration
1704   (cp_parser *);
1705 static tree cp_parser_objc_statement
1706   (cp_parser *);
1707
1708 /* Utility Routines */
1709
1710 static tree cp_parser_lookup_name
1711   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1712 static tree cp_parser_lookup_name_simple
1713   (cp_parser *, tree);
1714 static tree cp_parser_maybe_treat_template_as_class
1715   (tree, bool);
1716 static bool cp_parser_check_declarator_template_parameters
1717   (cp_parser *, cp_declarator *);
1718 static bool cp_parser_check_template_parameters
1719   (cp_parser *, unsigned);
1720 static tree cp_parser_simple_cast_expression
1721   (cp_parser *);
1722 static tree cp_parser_global_scope_opt
1723   (cp_parser *, bool, bool);
1724 static bool cp_parser_constructor_declarator_p
1725   (cp_parser *, bool);
1726 static tree cp_parser_function_definition_from_specifiers_and_declarator
1727   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1728 static tree cp_parser_function_definition_after_declarator
1729   (cp_parser *, bool);
1730 static void cp_parser_template_declaration_after_export
1731   (cp_parser *, bool);
1732 static void cp_parser_perform_template_parameter_access_checks
1733   (tree);
1734 static tree cp_parser_single_declaration
1735   (cp_parser *, tree, bool, bool *);
1736 static tree cp_parser_functional_cast
1737   (cp_parser *, tree);
1738 static tree cp_parser_save_member_function_body
1739   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1740 static tree cp_parser_enclosed_template_argument_list
1741   (cp_parser *);
1742 static void cp_parser_save_default_args
1743   (cp_parser *, tree);
1744 static void cp_parser_late_parsing_for_member
1745   (cp_parser *, tree);
1746 static void cp_parser_late_parsing_default_args
1747   (cp_parser *, tree);
1748 static tree cp_parser_sizeof_operand
1749   (cp_parser *, enum rid);
1750 static bool cp_parser_declares_only_class_p
1751   (cp_parser *);
1752 static void cp_parser_set_storage_class
1753   (cp_parser *, cp_decl_specifier_seq *, enum rid);
1754 static void cp_parser_set_decl_spec_type
1755   (cp_decl_specifier_seq *, tree, bool);
1756 static bool cp_parser_friend_p
1757   (const cp_decl_specifier_seq *);
1758 static cp_token *cp_parser_require
1759   (cp_parser *, enum cpp_ttype, const char *);
1760 static cp_token *cp_parser_require_keyword
1761   (cp_parser *, enum rid, const char *);
1762 static bool cp_parser_token_starts_function_definition_p
1763   (cp_token *);
1764 static bool cp_parser_next_token_starts_class_definition_p
1765   (cp_parser *);
1766 static bool cp_parser_next_token_ends_template_argument_p
1767   (cp_parser *);
1768 static bool cp_parser_nth_token_starts_template_argument_list_p
1769   (cp_parser *, size_t);
1770 static enum tag_types cp_parser_token_is_class_key
1771   (cp_token *);
1772 static void cp_parser_check_class_key
1773   (enum tag_types, tree type);
1774 static void cp_parser_check_access_in_redeclaration
1775   (tree type);
1776 static bool cp_parser_optional_template_keyword
1777   (cp_parser *);
1778 static void cp_parser_pre_parsed_nested_name_specifier
1779   (cp_parser *);
1780 static void cp_parser_cache_group
1781   (cp_parser *, enum cpp_ttype, unsigned);
1782 static void cp_parser_parse_tentatively
1783   (cp_parser *);
1784 static void cp_parser_commit_to_tentative_parse
1785   (cp_parser *);
1786 static void cp_parser_abort_tentative_parse
1787   (cp_parser *);
1788 static bool cp_parser_parse_definitely
1789   (cp_parser *);
1790 static inline bool cp_parser_parsing_tentatively
1791   (cp_parser *);
1792 static bool cp_parser_uncommitted_to_tentative_parse_p
1793   (cp_parser *);
1794 static void cp_parser_error
1795   (cp_parser *, const char *);
1796 static void cp_parser_name_lookup_error
1797   (cp_parser *, tree, tree, const char *);
1798 static bool cp_parser_simulate_error
1799   (cp_parser *);
1800 static void cp_parser_check_type_definition
1801   (cp_parser *);
1802 static void cp_parser_check_for_definition_in_return_type
1803   (cp_declarator *, tree);
1804 static void cp_parser_check_for_invalid_template_id
1805   (cp_parser *, tree);
1806 static bool cp_parser_non_integral_constant_expression
1807   (cp_parser *, const char *);
1808 static void cp_parser_diagnose_invalid_type_name
1809   (cp_parser *, tree, tree);
1810 static bool cp_parser_parse_and_diagnose_invalid_type_name
1811   (cp_parser *);
1812 static int cp_parser_skip_to_closing_parenthesis
1813   (cp_parser *, bool, bool, bool);
1814 static void cp_parser_skip_to_end_of_statement
1815   (cp_parser *);
1816 static void cp_parser_consume_semicolon_at_end_of_statement
1817   (cp_parser *);
1818 static void cp_parser_skip_to_end_of_block_or_statement
1819   (cp_parser *);
1820 static void cp_parser_skip_to_closing_brace
1821   (cp_parser *);
1822 static void cp_parser_skip_until_found
1823   (cp_parser *, enum cpp_ttype, const char *);
1824 static void cp_parser_skip_to_pragma_eol
1825   (cp_parser*, cp_token *);
1826 static bool cp_parser_error_occurred
1827   (cp_parser *);
1828 static bool cp_parser_allow_gnu_extensions_p
1829   (cp_parser *);
1830 static bool cp_parser_is_string_literal
1831   (cp_token *);
1832 static bool cp_parser_is_keyword
1833   (cp_token *, enum rid);
1834 static tree cp_parser_make_typename_type
1835   (cp_parser *, tree, tree);
1836
1837 /* Returns nonzero if we are parsing tentatively.  */
1838
1839 static inline bool
1840 cp_parser_parsing_tentatively (cp_parser* parser)
1841 {
1842   return parser->context->next != NULL;
1843 }
1844
1845 /* Returns nonzero if TOKEN is a string literal.  */
1846
1847 static bool
1848 cp_parser_is_string_literal (cp_token* token)
1849 {
1850   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1851 }
1852
1853 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1854
1855 static bool
1856 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1857 {
1858   return token->keyword == keyword;
1859 }
1860
1861 /* If not parsing tentatively, issue a diagnostic of the form
1862       FILE:LINE: MESSAGE before TOKEN
1863    where TOKEN is the next token in the input stream.  MESSAGE
1864    (specified by the caller) is usually of the form "expected
1865    OTHER-TOKEN".  */
1866
1867 static void
1868 cp_parser_error (cp_parser* parser, const char* message)
1869 {
1870   if (!cp_parser_simulate_error (parser))
1871     {
1872       cp_token *token = cp_lexer_peek_token (parser->lexer);
1873       /* This diagnostic makes more sense if it is tagged to the line
1874          of the token we just peeked at.  */
1875       cp_lexer_set_source_position_from_token (token);
1876
1877       if (token->type == CPP_PRAGMA)
1878         {
1879           error ("%<#pragma%> is not allowed here");
1880           cp_parser_skip_to_pragma_eol (parser, token);
1881           return;
1882         }
1883
1884       c_parse_error (message,
1885                      /* Because c_parser_error does not understand
1886                         CPP_KEYWORD, keywords are treated like
1887                         identifiers.  */
1888                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1889                      token->value);
1890     }
1891 }
1892
1893 /* Issue an error about name-lookup failing.  NAME is the
1894    IDENTIFIER_NODE DECL is the result of
1895    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1896    the thing that we hoped to find.  */
1897
1898 static void
1899 cp_parser_name_lookup_error (cp_parser* parser,
1900                              tree name,
1901                              tree decl,
1902                              const char* desired)
1903 {
1904   /* If name lookup completely failed, tell the user that NAME was not
1905      declared.  */
1906   if (decl == error_mark_node)
1907     {
1908       if (parser->scope && parser->scope != global_namespace)
1909         error ("%<%D::%D%> has not been declared",
1910                parser->scope, name);
1911       else if (parser->scope == global_namespace)
1912         error ("%<::%D%> has not been declared", name);
1913       else if (parser->object_scope
1914                && !CLASS_TYPE_P (parser->object_scope))
1915         error ("request for member %qD in non-class type %qT",
1916                name, parser->object_scope);
1917       else if (parser->object_scope)
1918         error ("%<%T::%D%> has not been declared",
1919                parser->object_scope, name);
1920       else
1921         error ("%qD has not been declared", name);
1922     }
1923   else if (parser->scope && parser->scope != global_namespace)
1924     error ("%<%D::%D%> %s", parser->scope, name, desired);
1925   else if (parser->scope == global_namespace)
1926     error ("%<::%D%> %s", name, desired);
1927   else
1928     error ("%qD %s", name, desired);
1929 }
1930
1931 /* If we are parsing tentatively, remember that an error has occurred
1932    during this tentative parse.  Returns true if the error was
1933    simulated; false if a message should be issued by the caller.  */
1934
1935 static bool
1936 cp_parser_simulate_error (cp_parser* parser)
1937 {
1938   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1939     {
1940       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1941       return true;
1942     }
1943   return false;
1944 }
1945
1946 /* Check for repeated decl-specifiers.  */
1947
1948 static void
1949 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
1950 {
1951   cp_decl_spec ds;
1952
1953   for (ds = ds_first; ds != ds_last; ++ds)
1954     {
1955       unsigned count = decl_specs->specs[(int)ds];
1956       if (count < 2)
1957         continue;
1958       /* The "long" specifier is a special case because of "long long".  */
1959       if (ds == ds_long)
1960         {
1961           if (count > 2)
1962             error ("%<long long long%> is too long for GCC");
1963           else if (pedantic && !in_system_header && warn_long_long)
1964             pedwarn ("ISO C++ does not support %<long long%>");
1965         }
1966       else if (count > 1)
1967         {
1968           static const char *const decl_spec_names[] = {
1969             "signed",
1970             "unsigned",
1971             "short",
1972             "long",
1973             "const",
1974             "volatile",
1975             "restrict",
1976             "inline",
1977             "virtual",
1978             "explicit",
1979             "friend",
1980             "typedef",
1981             "__complex",
1982             "__thread"
1983           };
1984           error ("duplicate %qs", decl_spec_names[(int)ds]);
1985         }
1986     }
1987 }
1988
1989 /* This function is called when a type is defined.  If type
1990    definitions are forbidden at this point, an error message is
1991    issued.  */
1992
1993 static void
1994 cp_parser_check_type_definition (cp_parser* parser)
1995 {
1996   /* If types are forbidden here, issue a message.  */
1997   if (parser->type_definition_forbidden_message)
1998     /* Use `%s' to print the string in case there are any escape
1999        characters in the message.  */
2000     error ("%s", parser->type_definition_forbidden_message);
2001 }
2002
2003 /* This function is called when the DECLARATOR is processed.  The TYPE
2004    was a type defined in the decl-specifiers.  If it is invalid to
2005    define a type in the decl-specifiers for DECLARATOR, an error is
2006    issued.  */
2007
2008 static void
2009 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2010                                                tree type)
2011 {
2012   /* [dcl.fct] forbids type definitions in return types.
2013      Unfortunately, it's not easy to know whether or not we are
2014      processing a return type until after the fact.  */
2015   while (declarator
2016          && (declarator->kind == cdk_pointer
2017              || declarator->kind == cdk_reference
2018              || declarator->kind == cdk_ptrmem))
2019     declarator = declarator->declarator;
2020   if (declarator
2021       && declarator->kind == cdk_function)
2022     {
2023       error ("new types may not be defined in a return type");
2024       inform ("(perhaps a semicolon is missing after the definition of %qT)",
2025               type);
2026     }
2027 }
2028
2029 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2030    "<" in any valid C++ program.  If the next token is indeed "<",
2031    issue a message warning the user about what appears to be an
2032    invalid attempt to form a template-id.  */
2033
2034 static void
2035 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2036                                          tree type)
2037 {
2038   cp_token_position start = 0;
2039
2040   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2041     {
2042       if (TYPE_P (type))
2043         error ("%qT is not a template", type);
2044       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2045         error ("%qE is not a template", type);
2046       else
2047         error ("invalid template-id");
2048       /* Remember the location of the invalid "<".  */
2049       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2050         start = cp_lexer_token_position (parser->lexer, true);
2051       /* Consume the "<".  */
2052       cp_lexer_consume_token (parser->lexer);
2053       /* Parse the template arguments.  */
2054       cp_parser_enclosed_template_argument_list (parser);
2055       /* Permanently remove the invalid template arguments so that
2056          this error message is not issued again.  */
2057       if (start)
2058         cp_lexer_purge_tokens_after (parser->lexer, start);
2059     }
2060 }
2061
2062 /* If parsing an integral constant-expression, issue an error message
2063    about the fact that THING appeared and return true.  Otherwise,
2064    return false.  In either case, set
2065    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2066
2067 static bool
2068 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2069                                             const char *thing)
2070 {
2071   parser->non_integral_constant_expression_p = true;
2072   if (parser->integral_constant_expression_p)
2073     {
2074       if (!parser->allow_non_integral_constant_expression_p)
2075         {
2076           error ("%s cannot appear in a constant-expression", thing);
2077           return true;
2078         }
2079     }
2080   return false;
2081 }
2082
2083 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2084    qualifying scope (or NULL, if none) for ID.  This function commits
2085    to the current active tentative parse, if any.  (Otherwise, the
2086    problematic construct might be encountered again later, resulting
2087    in duplicate error messages.)  */
2088
2089 static void
2090 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2091 {
2092   tree decl, old_scope;
2093   /* Try to lookup the identifier.  */
2094   old_scope = parser->scope;
2095   parser->scope = scope;
2096   decl = cp_parser_lookup_name_simple (parser, id);
2097   parser->scope = old_scope;
2098   /* If the lookup found a template-name, it means that the user forgot
2099   to specify an argument list. Emit a useful error message.  */
2100   if (TREE_CODE (decl) == TEMPLATE_DECL)
2101     error ("invalid use of template-name %qE without an argument list", decl);
2102   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2103     error ("invalid use of destructor %qD as a type", id);
2104   else if (!parser->scope)
2105     {
2106       /* Issue an error message.  */
2107       error ("%qE does not name a type", id);
2108       /* If we're in a template class, it's possible that the user was
2109          referring to a type from a base class.  For example:
2110
2111            template <typename T> struct A { typedef T X; };
2112            template <typename T> struct B : public A<T> { X x; };
2113
2114          The user should have said "typename A<T>::X".  */
2115       if (processing_template_decl && current_class_type
2116           && TYPE_BINFO (current_class_type))
2117         {
2118           tree b;
2119
2120           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2121                b;
2122                b = TREE_CHAIN (b))
2123             {
2124               tree base_type = BINFO_TYPE (b);
2125               if (CLASS_TYPE_P (base_type)
2126                   && dependent_type_p (base_type))
2127                 {
2128                   tree field;
2129                   /* Go from a particular instantiation of the
2130                      template (which will have an empty TYPE_FIELDs),
2131                      to the main version.  */
2132                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2133                   for (field = TYPE_FIELDS (base_type);
2134                        field;
2135                        field = TREE_CHAIN (field))
2136                     if (TREE_CODE (field) == TYPE_DECL
2137                         && DECL_NAME (field) == id)
2138                       {
2139                         inform ("(perhaps %<typename %T::%E%> was intended)",
2140                                 BINFO_TYPE (b), id);
2141                         break;
2142                       }
2143                   if (field)
2144                     break;
2145                 }
2146             }
2147         }
2148     }
2149   /* Here we diagnose qualified-ids where the scope is actually correct,
2150      but the identifier does not resolve to a valid type name.  */
2151   else if (parser->scope != error_mark_node)
2152     {
2153       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2154         error ("%qE in namespace %qE does not name a type",
2155                id, parser->scope);
2156       else if (TYPE_P (parser->scope))
2157         error ("%qE in class %qT does not name a type", id, parser->scope);
2158       else
2159         gcc_unreachable ();
2160     }
2161   cp_parser_commit_to_tentative_parse (parser);
2162 }
2163
2164 /* Check for a common situation where a type-name should be present,
2165    but is not, and issue a sensible error message.  Returns true if an
2166    invalid type-name was detected.
2167
2168    The situation handled by this function are variable declarations of the
2169    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2170    Usually, `ID' should name a type, but if we got here it means that it
2171    does not. We try to emit the best possible error message depending on
2172    how exactly the id-expression looks like.  */
2173
2174 static bool
2175 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2176 {
2177   tree id;
2178
2179   cp_parser_parse_tentatively (parser);
2180   id = cp_parser_id_expression (parser,
2181                                 /*template_keyword_p=*/false,
2182                                 /*check_dependency_p=*/true,
2183                                 /*template_p=*/NULL,
2184                                 /*declarator_p=*/true,
2185                                 /*optional_p=*/false,
2186                                 /*member_p=*/false);
2187   /* After the id-expression, there should be a plain identifier,
2188      otherwise this is not a simple variable declaration. Also, if
2189      the scope is dependent, we cannot do much.  */
2190   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2191       || (parser->scope && TYPE_P (parser->scope)
2192           && dependent_type_p (parser->scope)))
2193     {
2194       cp_parser_abort_tentative_parse (parser);
2195       return false;
2196     }
2197   if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2198     return false;
2199
2200   /* Emit a diagnostic for the invalid type.  */
2201   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2202   /* Skip to the end of the declaration; there's no point in
2203      trying to process it.  */
2204   cp_parser_skip_to_end_of_block_or_statement (parser);
2205   return true;
2206 }
2207
2208 /* Consume tokens up to, and including, the next non-nested closing `)'.
2209    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2210    are doing error recovery. Returns -1 if OR_COMMA is true and we
2211    found an unnested comma.  */
2212
2213 static int
2214 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2215                                        bool recovering,
2216                                        bool or_comma,
2217                                        bool consume_paren)
2218 {
2219   unsigned paren_depth = 0;
2220   unsigned brace_depth = 0;
2221
2222   if (recovering && !or_comma
2223       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2224     return 0;
2225
2226   while (true)
2227     {
2228       cp_token * token = cp_lexer_peek_token (parser->lexer);
2229
2230       switch (token->type)
2231         {
2232         case CPP_EOF:
2233         case CPP_PRAGMA_EOL:
2234           /* If we've run out of tokens, then there is no closing `)'.  */
2235           return 0;
2236
2237         case CPP_SEMICOLON:
2238           /* This matches the processing in skip_to_end_of_statement.  */
2239           if (!brace_depth)
2240             return 0;
2241           break;
2242
2243         case CPP_OPEN_BRACE:
2244           ++brace_depth;
2245           break;
2246         case CPP_CLOSE_BRACE:
2247           if (!brace_depth--)
2248             return 0;
2249           break;
2250
2251         case CPP_COMMA:
2252           if (recovering && or_comma && !brace_depth && !paren_depth)
2253             return -1;
2254           break;
2255
2256         case CPP_OPEN_PAREN:
2257           if (!brace_depth)
2258             ++paren_depth;
2259           break;
2260
2261         case CPP_CLOSE_PAREN:
2262           if (!brace_depth && !paren_depth--)
2263             {
2264               if (consume_paren)
2265                 cp_lexer_consume_token (parser->lexer);
2266               return 1;
2267             }
2268           break;
2269
2270         default:
2271           break;
2272         }
2273
2274       /* Consume the token.  */
2275       cp_lexer_consume_token (parser->lexer);
2276     }
2277 }
2278
2279 /* Consume tokens until we reach the end of the current statement.
2280    Normally, that will be just before consuming a `;'.  However, if a
2281    non-nested `}' comes first, then we stop before consuming that.  */
2282
2283 static void
2284 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2285 {
2286   unsigned nesting_depth = 0;
2287
2288   while (true)
2289     {
2290       cp_token *token = cp_lexer_peek_token (parser->lexer);
2291
2292       switch (token->type)
2293         {
2294         case CPP_EOF:
2295         case CPP_PRAGMA_EOL:
2296           /* If we've run out of tokens, stop.  */
2297           return;
2298
2299         case CPP_SEMICOLON:
2300           /* If the next token is a `;', we have reached the end of the
2301              statement.  */
2302           if (!nesting_depth)
2303             return;
2304           break;
2305
2306         case CPP_CLOSE_BRACE:
2307           /* If this is a non-nested '}', stop before consuming it.
2308              That way, when confronted with something like:
2309
2310                { 3 + }
2311
2312              we stop before consuming the closing '}', even though we
2313              have not yet reached a `;'.  */
2314           if (nesting_depth == 0)
2315             return;
2316
2317           /* If it is the closing '}' for a block that we have
2318              scanned, stop -- but only after consuming the token.
2319              That way given:
2320
2321                 void f g () { ... }
2322                 typedef int I;
2323
2324              we will stop after the body of the erroneously declared
2325              function, but before consuming the following `typedef'
2326              declaration.  */
2327           if (--nesting_depth == 0)
2328             {
2329               cp_lexer_consume_token (parser->lexer);
2330               return;
2331             }
2332
2333         case CPP_OPEN_BRACE:
2334           ++nesting_depth;
2335           break;
2336
2337         default:
2338           break;
2339         }
2340
2341       /* Consume the token.  */
2342       cp_lexer_consume_token (parser->lexer);
2343     }
2344 }
2345
2346 /* This function is called at the end of a statement or declaration.
2347    If the next token is a semicolon, it is consumed; otherwise, error
2348    recovery is attempted.  */
2349
2350 static void
2351 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2352 {
2353   /* Look for the trailing `;'.  */
2354   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2355     {
2356       /* If there is additional (erroneous) input, skip to the end of
2357          the statement.  */
2358       cp_parser_skip_to_end_of_statement (parser);
2359       /* If the next token is now a `;', consume it.  */
2360       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2361         cp_lexer_consume_token (parser->lexer);
2362     }
2363 }
2364
2365 /* Skip tokens until we have consumed an entire block, or until we
2366    have consumed a non-nested `;'.  */
2367
2368 static void
2369 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2370 {
2371   int nesting_depth = 0;
2372
2373   while (nesting_depth >= 0)
2374     {
2375       cp_token *token = cp_lexer_peek_token (parser->lexer);
2376
2377       switch (token->type)
2378         {
2379         case CPP_EOF:
2380         case CPP_PRAGMA_EOL:
2381           /* If we've run out of tokens, stop.  */
2382           return;
2383
2384         case CPP_SEMICOLON:
2385           /* Stop if this is an unnested ';'. */
2386           if (!nesting_depth)
2387             nesting_depth = -1;
2388           break;
2389
2390         case CPP_CLOSE_BRACE:
2391           /* Stop if this is an unnested '}', or closes the outermost
2392              nesting level.  */
2393           nesting_depth--;
2394           if (!nesting_depth)
2395             nesting_depth = -1;
2396           break;
2397
2398         case CPP_OPEN_BRACE:
2399           /* Nest. */
2400           nesting_depth++;
2401           break;
2402
2403         default:
2404           break;
2405         }
2406
2407       /* Consume the token.  */
2408       cp_lexer_consume_token (parser->lexer);
2409     }
2410 }
2411
2412 /* Skip tokens until a non-nested closing curly brace is the next
2413    token.  */
2414
2415 static void
2416 cp_parser_skip_to_closing_brace (cp_parser *parser)
2417 {
2418   unsigned nesting_depth = 0;
2419
2420   while (true)
2421     {
2422       cp_token *token = cp_lexer_peek_token (parser->lexer);
2423
2424       switch (token->type)
2425         {
2426         case CPP_EOF:
2427         case CPP_PRAGMA_EOL:
2428           /* If we've run out of tokens, stop.  */
2429           return;
2430
2431         case CPP_CLOSE_BRACE:
2432           /* If the next token is a non-nested `}', then we have reached
2433              the end of the current block.  */
2434           if (nesting_depth-- == 0)
2435             return;
2436           break;
2437
2438         case CPP_OPEN_BRACE:
2439           /* If it the next token is a `{', then we are entering a new
2440              block.  Consume the entire block.  */
2441           ++nesting_depth;
2442           break;
2443
2444         default:
2445           break;
2446         }
2447
2448       /* Consume the token.  */
2449       cp_lexer_consume_token (parser->lexer);
2450     }
2451 }
2452
2453 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2454    parameter is the PRAGMA token, allowing us to purge the entire pragma
2455    sequence.  */
2456
2457 static void
2458 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2459 {
2460   cp_token *token;
2461
2462   parser->lexer->in_pragma = false;
2463
2464   do
2465     token = cp_lexer_consume_token (parser->lexer);
2466   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2467
2468   /* Ensure that the pragma is not parsed again.  */
2469   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2470 }
2471
2472 /* Require pragma end of line, resyncing with it as necessary.  The
2473    arguments are as for cp_parser_skip_to_pragma_eol.  */
2474
2475 static void
2476 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2477 {
2478   parser->lexer->in_pragma = false;
2479   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2480     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2481 }
2482
2483 /* This is a simple wrapper around make_typename_type. When the id is
2484    an unresolved identifier node, we can provide a superior diagnostic
2485    using cp_parser_diagnose_invalid_type_name.  */
2486
2487 static tree
2488 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2489 {
2490   tree result;
2491   if (TREE_CODE (id) == IDENTIFIER_NODE)
2492     {
2493       result = make_typename_type (scope, id, typename_type,
2494                                    /*complain=*/tf_none);
2495       if (result == error_mark_node)
2496         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2497       return result;
2498     }
2499   return make_typename_type (scope, id, typename_type, tf_error);
2500 }
2501
2502
2503 /* Create a new C++ parser.  */
2504
2505 static cp_parser *
2506 cp_parser_new (void)
2507 {
2508   cp_parser *parser;
2509   cp_lexer *lexer;
2510   unsigned i;
2511
2512   /* cp_lexer_new_main is called before calling ggc_alloc because
2513      cp_lexer_new_main might load a PCH file.  */
2514   lexer = cp_lexer_new_main ();
2515
2516   /* Initialize the binops_by_token so that we can get the tree
2517      directly from the token.  */
2518   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2519     binops_by_token[binops[i].token_type] = binops[i];
2520
2521   parser = GGC_CNEW (cp_parser);
2522   parser->lexer = lexer;
2523   parser->context = cp_parser_context_new (NULL);
2524
2525   /* For now, we always accept GNU extensions.  */
2526   parser->allow_gnu_extensions_p = 1;
2527
2528   /* The `>' token is a greater-than operator, not the end of a
2529      template-id.  */
2530   parser->greater_than_is_operator_p = true;
2531
2532   parser->default_arg_ok_p = true;
2533
2534   /* We are not parsing a constant-expression.  */
2535   parser->integral_constant_expression_p = false;
2536   parser->allow_non_integral_constant_expression_p = false;
2537   parser->non_integral_constant_expression_p = false;
2538
2539   /* Local variable names are not forbidden.  */
2540   parser->local_variables_forbidden_p = false;
2541
2542   /* We are not processing an `extern "C"' declaration.  */
2543   parser->in_unbraced_linkage_specification_p = false;
2544
2545   /* We are not processing a declarator.  */
2546   parser->in_declarator_p = false;
2547
2548   /* We are not processing a template-argument-list.  */
2549   parser->in_template_argument_list_p = false;
2550
2551   /* We are not in an iteration statement.  */
2552   parser->in_statement = 0;
2553
2554   /* We are not in a switch statement.  */
2555   parser->in_switch_statement_p = false;
2556
2557   /* We are not parsing a type-id inside an expression.  */
2558   parser->in_type_id_in_expr_p = false;
2559
2560   /* Declarations aren't implicitly extern "C".  */
2561   parser->implicit_extern_c = false;
2562
2563   /* String literals should be translated to the execution character set.  */
2564   parser->translate_strings_p = true;
2565
2566   /* The unparsed function queue is empty.  */
2567   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2568
2569   /* There are no classes being defined.  */
2570   parser->num_classes_being_defined = 0;
2571
2572   /* No template parameters apply.  */
2573   parser->num_template_parameter_lists = 0;
2574
2575   return parser;
2576 }
2577
2578 /* Create a cp_lexer structure which will emit the tokens in CACHE
2579    and push it onto the parser's lexer stack.  This is used for delayed
2580    parsing of in-class method bodies and default arguments, and should
2581    not be confused with tentative parsing.  */
2582 static void
2583 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2584 {
2585   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2586   lexer->next = parser->lexer;
2587   parser->lexer = lexer;
2588
2589   /* Move the current source position to that of the first token in the
2590      new lexer.  */
2591   cp_lexer_set_source_position_from_token (lexer->next_token);
2592 }
2593
2594 /* Pop the top lexer off the parser stack.  This is never used for the
2595    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2596 static void
2597 cp_parser_pop_lexer (cp_parser *parser)
2598 {
2599   cp_lexer *lexer = parser->lexer;
2600   parser->lexer = lexer->next;
2601   cp_lexer_destroy (lexer);
2602
2603   /* Put the current source position back where it was before this
2604      lexer was pushed.  */
2605   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2606 }
2607
2608 /* Lexical conventions [gram.lex]  */
2609
2610 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2611    identifier.  */
2612
2613 static tree
2614 cp_parser_identifier (cp_parser* parser)
2615 {
2616   cp_token *token;
2617
2618   /* Look for the identifier.  */
2619   token = cp_parser_require (parser, CPP_NAME, "identifier");
2620   /* Return the value.  */
2621   return token ? token->value : error_mark_node;
2622 }
2623
2624 /* Parse a sequence of adjacent string constants.  Returns a
2625    TREE_STRING representing the combined, nul-terminated string
2626    constant.  If TRANSLATE is true, translate the string to the
2627    execution character set.  If WIDE_OK is true, a wide string is
2628    invalid here.
2629
2630    C++98 [lex.string] says that if a narrow string literal token is
2631    adjacent to a wide string literal token, the behavior is undefined.
2632    However, C99 6.4.5p4 says that this results in a wide string literal.
2633    We follow C99 here, for consistency with the C front end.
2634
2635    This code is largely lifted from lex_string() in c-lex.c.
2636
2637    FUTURE: ObjC++ will need to handle @-strings here.  */
2638 static tree
2639 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2640 {
2641   tree value;
2642   bool wide = false;
2643   size_t count;
2644   struct obstack str_ob;
2645   cpp_string str, istr, *strs;
2646   cp_token *tok;
2647
2648   tok = cp_lexer_peek_token (parser->lexer);
2649   if (!cp_parser_is_string_literal (tok))
2650     {
2651       cp_parser_error (parser, "expected string-literal");
2652       return error_mark_node;
2653     }
2654
2655   /* Try to avoid the overhead of creating and destroying an obstack
2656      for the common case of just one string.  */
2657   if (!cp_parser_is_string_literal
2658       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2659     {
2660       cp_lexer_consume_token (parser->lexer);
2661
2662       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2663       str.len = TREE_STRING_LENGTH (tok->value);
2664       count = 1;
2665       if (tok->type == CPP_WSTRING)
2666         wide = true;
2667
2668       strs = &str;
2669     }
2670   else
2671     {
2672       gcc_obstack_init (&str_ob);
2673       count = 0;
2674
2675       do
2676         {
2677           cp_lexer_consume_token (parser->lexer);
2678           count++;
2679           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2680           str.len = TREE_STRING_LENGTH (tok->value);
2681           if (tok->type == CPP_WSTRING)
2682             wide = true;
2683
2684           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2685
2686           tok = cp_lexer_peek_token (parser->lexer);
2687         }
2688       while (cp_parser_is_string_literal (tok));
2689
2690       strs = (cpp_string *) obstack_finish (&str_ob);
2691     }
2692
2693   if (wide && !wide_ok)
2694     {
2695       cp_parser_error (parser, "a wide string is invalid in this context");
2696       wide = false;
2697     }
2698
2699   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2700       (parse_in, strs, count, &istr, wide))
2701     {
2702       value = build_string (istr.len, (char *)istr.text);
2703       free ((void *)istr.text);
2704
2705       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2706       value = fix_string_type (value);
2707     }
2708   else
2709     /* cpp_interpret_string has issued an error.  */
2710     value = error_mark_node;
2711
2712   if (count > 1)
2713     obstack_free (&str_ob, 0);
2714
2715   return value;
2716 }
2717
2718
2719 /* Basic concepts [gram.basic]  */
2720
2721 /* Parse a translation-unit.
2722
2723    translation-unit:
2724      declaration-seq [opt]
2725
2726    Returns TRUE if all went well.  */
2727
2728 static bool
2729 cp_parser_translation_unit (cp_parser* parser)
2730 {
2731   /* The address of the first non-permanent object on the declarator
2732      obstack.  */
2733   static void *declarator_obstack_base;
2734
2735   bool success;
2736
2737   /* Create the declarator obstack, if necessary.  */
2738   if (!cp_error_declarator)
2739     {
2740       gcc_obstack_init (&declarator_obstack);
2741       /* Create the error declarator.  */
2742       cp_error_declarator = make_declarator (cdk_error);
2743       /* Create the empty parameter list.  */
2744       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2745       /* Remember where the base of the declarator obstack lies.  */
2746       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2747     }
2748
2749   cp_parser_declaration_seq_opt (parser);
2750
2751   /* If there are no tokens left then all went well.  */
2752   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2753     {
2754       /* Get rid of the token array; we don't need it any more.  */
2755       cp_lexer_destroy (parser->lexer);
2756       parser->lexer = NULL;
2757
2758       /* This file might have been a context that's implicitly extern
2759          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2760       if (parser->implicit_extern_c)
2761         {
2762           pop_lang_context ();
2763           parser->implicit_extern_c = false;
2764         }
2765
2766       /* Finish up.  */
2767       finish_translation_unit ();
2768
2769       success = true;
2770     }
2771   else
2772     {
2773       cp_parser_error (parser, "expected declaration");
2774       success = false;
2775     }
2776
2777   /* Make sure the declarator obstack was fully cleaned up.  */
2778   gcc_assert (obstack_next_free (&declarator_obstack)
2779               == declarator_obstack_base);
2780
2781   /* All went well.  */
2782   return success;
2783 }
2784
2785 /* Expressions [gram.expr] */
2786
2787 /* Parse a primary-expression.
2788
2789    primary-expression:
2790      literal
2791      this
2792      ( expression )
2793      id-expression
2794
2795    GNU Extensions:
2796
2797    primary-expression:
2798      ( compound-statement )
2799      __builtin_va_arg ( assignment-expression , type-id )
2800      __builtin_offsetof ( type-id , offsetof-expression )
2801
2802    Objective-C++ Extension:
2803
2804    primary-expression:
2805      objc-expression
2806
2807    literal:
2808      __null
2809
2810    ADDRESS_P is true iff this expression was immediately preceded by
2811    "&" and therefore might denote a pointer-to-member.  CAST_P is true
2812    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
2813    true iff this expression is a template argument.
2814
2815    Returns a representation of the expression.  Upon return, *IDK
2816    indicates what kind of id-expression (if any) was present.  */
2817
2818 static tree
2819 cp_parser_primary_expression (cp_parser *parser,
2820                               bool address_p,
2821                               bool cast_p,
2822                               bool template_arg_p,
2823                               cp_id_kind *idk)
2824 {
2825   cp_token *token;
2826
2827   /* Assume the primary expression is not an id-expression.  */
2828   *idk = CP_ID_KIND_NONE;
2829
2830   /* Peek at the next token.  */
2831   token = cp_lexer_peek_token (parser->lexer);
2832   switch (token->type)
2833     {
2834       /* literal:
2835            integer-literal
2836            character-literal
2837            floating-literal
2838            string-literal
2839            boolean-literal  */
2840     case CPP_CHAR:
2841     case CPP_WCHAR:
2842     case CPP_NUMBER:
2843       token = cp_lexer_consume_token (parser->lexer);
2844       /* Floating-point literals are only allowed in an integral
2845          constant expression if they are cast to an integral or
2846          enumeration type.  */
2847       if (TREE_CODE (token->value) == REAL_CST
2848           && parser->integral_constant_expression_p
2849           && pedantic)
2850         {
2851           /* CAST_P will be set even in invalid code like "int(2.7 +
2852              ...)".   Therefore, we have to check that the next token
2853              is sure to end the cast.  */
2854           if (cast_p)
2855             {
2856               cp_token *next_token;
2857
2858               next_token = cp_lexer_peek_token (parser->lexer);
2859               if (/* The comma at the end of an
2860                      enumerator-definition.  */
2861                   next_token->type != CPP_COMMA
2862                   /* The curly brace at the end of an enum-specifier.  */
2863                   && next_token->type != CPP_CLOSE_BRACE
2864                   /* The end of a statement.  */
2865                   && next_token->type != CPP_SEMICOLON
2866                   /* The end of the cast-expression.  */
2867                   && next_token->type != CPP_CLOSE_PAREN
2868                   /* The end of an array bound.  */
2869                   && next_token->type != CPP_CLOSE_SQUARE
2870                   /* The closing ">" in a template-argument-list.  */
2871                   && (next_token->type != CPP_GREATER
2872                       || parser->greater_than_is_operator_p))
2873                 cast_p = false;
2874             }
2875
2876           /* If we are within a cast, then the constraint that the
2877              cast is to an integral or enumeration type will be
2878              checked at that point.  If we are not within a cast, then
2879              this code is invalid.  */
2880           if (!cast_p)
2881             cp_parser_non_integral_constant_expression
2882               (parser, "floating-point literal");
2883         }
2884       return token->value;
2885
2886     case CPP_STRING:
2887     case CPP_WSTRING:
2888       /* ??? Should wide strings be allowed when parser->translate_strings_p
2889          is false (i.e. in attributes)?  If not, we can kill the third
2890          argument to cp_parser_string_literal.  */
2891       return cp_parser_string_literal (parser,
2892                                        parser->translate_strings_p,
2893                                        true);
2894
2895     case CPP_OPEN_PAREN:
2896       {
2897         tree expr;
2898         bool saved_greater_than_is_operator_p;
2899
2900         /* Consume the `('.  */
2901         cp_lexer_consume_token (parser->lexer);
2902         /* Within a parenthesized expression, a `>' token is always
2903            the greater-than operator.  */
2904         saved_greater_than_is_operator_p
2905           = parser->greater_than_is_operator_p;
2906         parser->greater_than_is_operator_p = true;
2907         /* If we see `( { ' then we are looking at the beginning of
2908            a GNU statement-expression.  */
2909         if (cp_parser_allow_gnu_extensions_p (parser)
2910             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2911           {
2912             /* Statement-expressions are not allowed by the standard.  */
2913             if (pedantic)
2914               pedwarn ("ISO C++ forbids braced-groups within expressions");
2915
2916             /* And they're not allowed outside of a function-body; you
2917                cannot, for example, write:
2918
2919                  int i = ({ int j = 3; j + 1; });
2920
2921                at class or namespace scope.  */
2922             if (!at_function_scope_p ())
2923               error ("statement-expressions are allowed only inside functions");
2924             /* Start the statement-expression.  */
2925             expr = begin_stmt_expr ();
2926             /* Parse the compound-statement.  */
2927             cp_parser_compound_statement (parser, expr, false);
2928             /* Finish up.  */
2929             expr = finish_stmt_expr (expr, false);
2930           }
2931         else
2932           {
2933             /* Parse the parenthesized expression.  */
2934             expr = cp_parser_expression (parser, cast_p);
2935             /* Let the front end know that this expression was
2936                enclosed in parentheses. This matters in case, for
2937                example, the expression is of the form `A::B', since
2938                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2939                not.  */
2940             finish_parenthesized_expr (expr);
2941           }
2942         /* The `>' token might be the end of a template-id or
2943            template-parameter-list now.  */
2944         parser->greater_than_is_operator_p
2945           = saved_greater_than_is_operator_p;
2946         /* Consume the `)'.  */
2947         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2948           cp_parser_skip_to_end_of_statement (parser);
2949
2950         return expr;
2951       }
2952
2953     case CPP_KEYWORD:
2954       switch (token->keyword)
2955         {
2956           /* These two are the boolean literals.  */
2957         case RID_TRUE:
2958           cp_lexer_consume_token (parser->lexer);
2959           return boolean_true_node;
2960         case RID_FALSE:
2961           cp_lexer_consume_token (parser->lexer);
2962           return boolean_false_node;
2963
2964           /* The `__null' literal.  */
2965         case RID_NULL:
2966           cp_lexer_consume_token (parser->lexer);
2967           return null_node;
2968
2969           /* Recognize the `this' keyword.  */
2970         case RID_THIS:
2971           cp_lexer_consume_token (parser->lexer);
2972           if (parser->local_variables_forbidden_p)
2973             {
2974               error ("%<this%> may not be used in this context");
2975               return error_mark_node;
2976             }
2977           /* Pointers cannot appear in constant-expressions.  */
2978           if (cp_parser_non_integral_constant_expression (parser,
2979                                                           "`this'"))
2980             return error_mark_node;
2981           return finish_this_expr ();
2982
2983           /* The `operator' keyword can be the beginning of an
2984              id-expression.  */
2985         case RID_OPERATOR:
2986           goto id_expression;
2987
2988         case RID_FUNCTION_NAME:
2989         case RID_PRETTY_FUNCTION_NAME:
2990         case RID_C99_FUNCTION_NAME:
2991           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2992              __func__ are the names of variables -- but they are
2993              treated specially.  Therefore, they are handled here,
2994              rather than relying on the generic id-expression logic
2995              below.  Grammatically, these names are id-expressions.
2996
2997              Consume the token.  */
2998           token = cp_lexer_consume_token (parser->lexer);
2999           /* Look up the name.  */
3000           return finish_fname (token->value);
3001
3002         case RID_VA_ARG:
3003           {
3004             tree expression;
3005             tree type;
3006
3007             /* The `__builtin_va_arg' construct is used to handle
3008                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3009             cp_lexer_consume_token (parser->lexer);
3010             /* Look for the opening `('.  */
3011             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3012             /* Now, parse the assignment-expression.  */
3013             expression = cp_parser_assignment_expression (parser,
3014                                                           /*cast_p=*/false);
3015             /* Look for the `,'.  */
3016             cp_parser_require (parser, CPP_COMMA, "`,'");
3017             /* Parse the type-id.  */
3018             type = cp_parser_type_id (parser);
3019             /* Look for the closing `)'.  */
3020             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3021             /* Using `va_arg' in a constant-expression is not
3022                allowed.  */
3023             if (cp_parser_non_integral_constant_expression (parser,
3024                                                             "`va_arg'"))
3025               return error_mark_node;
3026             return build_x_va_arg (expression, type);
3027           }
3028
3029         case RID_OFFSETOF:
3030           return cp_parser_builtin_offsetof (parser);
3031
3032           /* Objective-C++ expressions.  */
3033         case RID_AT_ENCODE:
3034         case RID_AT_PROTOCOL:
3035         case RID_AT_SELECTOR:
3036           return cp_parser_objc_expression (parser);
3037
3038         default:
3039           cp_parser_error (parser, "expected primary-expression");
3040           return error_mark_node;
3041         }
3042
3043       /* An id-expression can start with either an identifier, a
3044          `::' as the beginning of a qualified-id, or the "operator"
3045          keyword.  */
3046     case CPP_NAME:
3047     case CPP_SCOPE:
3048     case CPP_TEMPLATE_ID:
3049     case CPP_NESTED_NAME_SPECIFIER:
3050       {
3051         tree id_expression;
3052         tree decl;
3053         const char *error_msg;
3054         bool template_p;
3055         bool done;
3056
3057       id_expression:
3058         /* Parse the id-expression.  */
3059         id_expression
3060           = cp_parser_id_expression (parser,
3061                                      /*template_keyword_p=*/false,
3062                                      /*check_dependency_p=*/true,
3063                                      &template_p,
3064                                      /*declarator_p=*/false,
3065                                      /*optional_p=*/false,
3066                                      /*member_p=*/false);
3067         if (id_expression == error_mark_node)
3068           return error_mark_node;
3069         token = cp_lexer_peek_token (parser->lexer);
3070         done = (token->type != CPP_OPEN_SQUARE
3071                 && token->type != CPP_OPEN_PAREN
3072                 && token->type != CPP_DOT
3073                 && token->type != CPP_DEREF
3074                 && token->type != CPP_PLUS_PLUS
3075                 && token->type != CPP_MINUS_MINUS);
3076         /* If we have a template-id, then no further lookup is
3077            required.  If the template-id was for a template-class, we
3078            will sometimes have a TYPE_DECL at this point.  */
3079         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3080                  || TREE_CODE (id_expression) == TYPE_DECL)
3081           decl = id_expression;
3082         /* Look up the name.  */
3083         else
3084           {
3085             tree ambiguous_decls;
3086
3087             decl = cp_parser_lookup_name (parser, id_expression,
3088                                           none_type,
3089                                           template_p,
3090                                           /*is_namespace=*/false,
3091                                           /*check_dependency=*/true,
3092                                           &ambiguous_decls);
3093             /* If the lookup was ambiguous, an error will already have
3094                been issued.  */
3095             if (ambiguous_decls)
3096               return error_mark_node;
3097
3098             /* In Objective-C++, an instance variable (ivar) may be preferred
3099                to whatever cp_parser_lookup_name() found.  */
3100             decl = objc_lookup_ivar (decl, id_expression);
3101
3102             /* If name lookup gives us a SCOPE_REF, then the
3103                qualifying scope was dependent.  */
3104             if (TREE_CODE (decl) == SCOPE_REF)
3105               return decl;
3106             /* Check to see if DECL is a local variable in a context
3107                where that is forbidden.  */
3108             if (parser->local_variables_forbidden_p
3109                 && local_variable_p (decl))
3110               {
3111                 /* It might be that we only found DECL because we are
3112                    trying to be generous with pre-ISO scoping rules.
3113                    For example, consider:
3114
3115                      int i;
3116                      void g() {
3117                        for (int i = 0; i < 10; ++i) {}
3118                        extern void f(int j = i);
3119                      }
3120
3121                    Here, name look up will originally find the out
3122                    of scope `i'.  We need to issue a warning message,
3123                    but then use the global `i'.  */
3124                 decl = check_for_out_of_scope_variable (decl);
3125                 if (local_variable_p (decl))
3126                   {
3127                     error ("local variable %qD may not appear in this context",
3128                            decl);
3129                     return error_mark_node;
3130                   }
3131               }
3132           }
3133
3134         decl = (finish_id_expression
3135                 (id_expression, decl, parser->scope,
3136                  idk,
3137                  parser->integral_constant_expression_p,
3138                  parser->allow_non_integral_constant_expression_p,
3139                  &parser->non_integral_constant_expression_p,
3140                  template_p, done, address_p,
3141                  template_arg_p,
3142                  &error_msg));
3143         if (error_msg)
3144           cp_parser_error (parser, error_msg);
3145         return decl;
3146       }
3147
3148       /* Anything else is an error.  */
3149     default:
3150       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3151       if (c_dialect_objc ()
3152           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3153         return cp_parser_objc_expression (parser);
3154
3155       cp_parser_error (parser, "expected primary-expression");
3156       return error_mark_node;
3157     }
3158 }
3159
3160 /* Parse an id-expression.
3161
3162    id-expression:
3163      unqualified-id
3164      qualified-id
3165
3166    qualified-id:
3167      :: [opt] nested-name-specifier template [opt] unqualified-id
3168      :: identifier
3169      :: operator-function-id
3170      :: template-id
3171
3172    Return a representation of the unqualified portion of the
3173    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3174    a `::' or nested-name-specifier.
3175
3176    Often, if the id-expression was a qualified-id, the caller will
3177    want to make a SCOPE_REF to represent the qualified-id.  This
3178    function does not do this in order to avoid wastefully creating
3179    SCOPE_REFs when they are not required.
3180
3181    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3182    `template' keyword.
3183
3184    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3185    uninstantiated templates.
3186
3187    If *TEMPLATE_P is non-NULL, it is set to true iff the
3188    `template' keyword is used to explicitly indicate that the entity
3189    named is a template.
3190
3191    If DECLARATOR_P is true, the id-expression is appearing as part of
3192    a declarator, rather than as part of an expression.  */
3193
3194 static tree
3195 cp_parser_id_expression (cp_parser *parser,
3196                          bool template_keyword_p,
3197                          bool check_dependency_p,
3198                          bool *template_p,
3199                          bool declarator_p,
3200                          bool optional_p,
3201                          bool member_p)
3202 {
3203   bool global_scope_p;
3204   bool nested_name_specifier_p;
3205
3206   /* Assume the `template' keyword was not used.  */
3207   if (template_p)
3208     *template_p = template_keyword_p;
3209
3210   /* Look for the optional `::' operator.  */
3211   global_scope_p
3212     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false,
3213                                    /*object_scope_valid_p=*/member_p)
3214        != NULL_TREE);
3215   
3216   /* Look for the optional nested-name-specifier.  */
3217   nested_name_specifier_p
3218     = (cp_parser_nested_name_specifier_opt (parser,
3219                                             /*typename_keyword_p=*/false,
3220                                             check_dependency_p,
3221                                             /*type_p=*/false,
3222                                             declarator_p)
3223        != NULL_TREE);
3224   /* If there is a nested-name-specifier, then we are looking at
3225      the first qualified-id production.  */
3226   if (nested_name_specifier_p)
3227     {
3228       tree saved_scope;
3229       tree saved_object_scope;
3230       tree saved_qualifying_scope;
3231       tree unqualified_id;
3232       bool is_template;
3233
3234       /* See if the next token is the `template' keyword.  */
3235       if (!template_p)
3236         template_p = &is_template;
3237       *template_p = cp_parser_optional_template_keyword (parser);
3238       /* Name lookup we do during the processing of the
3239          unqualified-id might obliterate SCOPE.  */
3240       saved_scope = parser->scope;
3241       saved_object_scope = parser->object_scope;
3242       saved_qualifying_scope = parser->qualifying_scope;
3243       /* Process the final unqualified-id.  */
3244       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3245                                                  check_dependency_p,
3246                                                  declarator_p,
3247                                                  /*optional_p=*/false,
3248                                                  /*member_p=*/false);
3249       /* Restore the SAVED_SCOPE for our caller.  */
3250       parser->scope = saved_scope;
3251       parser->object_scope = saved_object_scope;
3252       parser->qualifying_scope = saved_qualifying_scope;
3253
3254       return unqualified_id;
3255     }
3256   /* Otherwise, if we are in global scope, then we are looking at one
3257      of the other qualified-id productions.  */
3258   else if (global_scope_p)
3259     {
3260       cp_token *token;
3261       tree id;
3262
3263       /* Peek at the next token.  */
3264       token = cp_lexer_peek_token (parser->lexer);
3265
3266       /* If it's an identifier, and the next token is not a "<", then
3267          we can avoid the template-id case.  This is an optimization
3268          for this common case.  */
3269       if (token->type == CPP_NAME
3270           && !cp_parser_nth_token_starts_template_argument_list_p
3271                (parser, 2))
3272         return cp_parser_identifier (parser);
3273
3274       cp_parser_parse_tentatively (parser);
3275       /* Try a template-id.  */
3276       id = cp_parser_template_id (parser,
3277                                   /*template_keyword_p=*/false,
3278                                   /*check_dependency_p=*/true,
3279                                   declarator_p);
3280       /* If that worked, we're done.  */
3281       if (cp_parser_parse_definitely (parser))
3282         return id;
3283
3284       /* Peek at the next token.  (Changes in the token buffer may
3285          have invalidated the pointer obtained above.)  */
3286       token = cp_lexer_peek_token (parser->lexer);
3287
3288       switch (token->type)
3289         {
3290         case CPP_NAME:
3291           return cp_parser_identifier (parser);
3292
3293         case CPP_KEYWORD:
3294           if (token->keyword == RID_OPERATOR)
3295             return cp_parser_operator_function_id (parser);
3296           /* Fall through.  */
3297
3298         default:
3299           cp_parser_error (parser, "expected id-expression");
3300           return error_mark_node;
3301         }
3302     }
3303   else
3304     return cp_parser_unqualified_id (parser, template_keyword_p,
3305                                      /*check_dependency_p=*/true,
3306                                      declarator_p, optional_p, member_p);
3307 }
3308
3309 /* Parse an unqualified-id.
3310
3311    unqualified-id:
3312      identifier
3313      operator-function-id
3314      conversion-function-id
3315      ~ class-name
3316      template-id
3317
3318    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3319    keyword, in a construct like `A::template ...'.
3320
3321    Returns a representation of unqualified-id.  For the `identifier'
3322    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3323    production a BIT_NOT_EXPR is returned; the operand of the
3324    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3325    other productions, see the documentation accompanying the
3326    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3327    names are looked up in uninstantiated templates.  If DECLARATOR_P
3328    is true, the unqualified-id is appearing as part of a declarator,
3329    rather than as part of an expression.  */
3330
3331 static tree
3332 cp_parser_unqualified_id (cp_parser* parser,
3333                           bool template_keyword_p,
3334                           bool check_dependency_p,
3335                           bool declarator_p,
3336                           bool optional_p,
3337                           bool member_p)
3338 {
3339   cp_token *token;
3340
3341   /* Peek at the next token.  */
3342   token = cp_lexer_peek_token (parser->lexer);
3343
3344   switch (token->type)
3345     {
3346     case CPP_NAME:
3347       {
3348         tree id;
3349
3350         /* We don't know yet whether or not this will be a
3351            template-id.  */
3352         cp_parser_parse_tentatively (parser);
3353         /* Try a template-id.  */
3354         id = cp_parser_template_id (parser, template_keyword_p,
3355                                     check_dependency_p,
3356                                     declarator_p);
3357         /* If it worked, we're done.  */
3358         if (cp_parser_parse_definitely (parser))
3359           return id;
3360         /* Otherwise, it's an ordinary identifier.  */
3361         return cp_parser_identifier (parser);
3362       }
3363
3364     case CPP_TEMPLATE_ID:
3365       return cp_parser_template_id (parser, template_keyword_p,
3366                                     check_dependency_p,
3367                                     declarator_p);
3368
3369     case CPP_COMPL:
3370       {
3371         tree type_decl;
3372         tree qualifying_scope;
3373         tree object_scope;
3374         tree scope;
3375         bool done;
3376
3377         /* Consume the `~' token.  */
3378         cp_lexer_consume_token (parser->lexer);
3379         /* Parse the class-name.  The standard, as written, seems to
3380            say that:
3381
3382              template <typename T> struct S { ~S (); };
3383              template <typename T> S<T>::~S() {}
3384
3385            is invalid, since `~' must be followed by a class-name, but
3386            `S<T>' is dependent, and so not known to be a class.
3387            That's not right; we need to look in uninstantiated
3388            templates.  A further complication arises from:
3389
3390              template <typename T> void f(T t) {
3391                t.T::~T();
3392              }
3393
3394            Here, it is not possible to look up `T' in the scope of `T'
3395            itself.  We must look in both the current scope, and the
3396            scope of the containing complete expression.
3397
3398            Yet another issue is:
3399
3400              struct S {
3401                int S;
3402                ~S();
3403              };
3404
3405              S::~S() {}
3406
3407            The standard does not seem to say that the `S' in `~S'
3408            should refer to the type `S' and not the data member
3409            `S::S'.  */
3410
3411         /* DR 244 says that we look up the name after the "~" in the
3412            same scope as we looked up the qualifying name.  That idea
3413            isn't fully worked out; it's more complicated than that.  */
3414         scope = parser->scope;
3415         object_scope = parser->object_scope;
3416         qualifying_scope = parser->qualifying_scope;
3417
3418         /* Check for invalid scopes.  */
3419         if (scope == error_mark_node)
3420           {
3421             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3422               cp_lexer_consume_token (parser->lexer);
3423             return error_mark_node;
3424           }
3425         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3426           {
3427             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3428               error ("scope %qT before %<~%> is not a class-name", scope);
3429             cp_parser_simulate_error (parser);
3430             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3431               cp_lexer_consume_token (parser->lexer);
3432             return error_mark_node;
3433           }
3434         gcc_assert (!scope || TYPE_P (scope));
3435
3436         /* If the name is of the form "X::~X" it's OK.  */
3437         token = cp_lexer_peek_token (parser->lexer);
3438         if (scope
3439             && token->type == CPP_NAME
3440             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3441                 == CPP_OPEN_PAREN)
3442             && constructor_name_p (token->value, scope))
3443           {
3444             cp_lexer_consume_token (parser->lexer);
3445             return build_nt (BIT_NOT_EXPR, scope);
3446           }
3447
3448         /* If there was an explicit qualification (S::~T), first look
3449            in the scope given by the qualification (i.e., S).  */
3450         done = false;
3451         type_decl = NULL_TREE;
3452         if (scope)
3453           {
3454             cp_parser_parse_tentatively (parser);
3455             type_decl = cp_parser_class_name (parser,
3456                                               /*typename_keyword_p=*/false,
3457                                               /*template_keyword_p=*/false,
3458                                               none_type,
3459                                               /*check_dependency=*/false,
3460                                               /*class_head_p=*/false,
3461                                               declarator_p);
3462             if (cp_parser_parse_definitely (parser))
3463               done = true;
3464           }
3465
3466         /* In "N::S::~S", look in "N" as well.  */
3467         if (!done && scope && qualifying_scope)
3468           {
3469             cp_parser_parse_tentatively (parser);
3470             parser->scope = qualifying_scope;
3471             parser->object_scope = NULL_TREE;
3472             parser->qualifying_scope = NULL_TREE;
3473             type_decl
3474               = cp_parser_class_name (parser,
3475                                       /*typename_keyword_p=*/false,
3476                                       /*template_keyword_p=*/false,
3477                                       none_type,
3478                                       /*check_dependency=*/false,
3479                                       /*class_head_p=*/false,
3480                                       declarator_p);
3481             if (cp_parser_parse_definitely (parser))
3482               done = true;
3483           }
3484         /* In "p->~T", look in the scope given by "*p" as well.  */
3485         else if (!done && member_p)
3486           {
3487             if (!object_scope)
3488               {
3489                 /* It's a dependent expression, so just parse the
3490                    dtor name.  */
3491                 tree id;
3492
3493                 if (template_keyword_p)
3494                   /* It's a template-id.  */
3495                   id = cp_parser_template_id (parser, true,
3496                                               check_dependency_p,
3497                                               declarator_p);
3498                 else
3499                   {
3500                     /* Otherwise, it's an ordinary identifier.  */
3501                     id = cp_parser_identifier (parser);
3502                     /* If ID is a template type parm, then use that
3503                        directly.  */
3504                     if (TREE_TYPE (id)
3505                         && TREE_CODE (TREE_TYPE (id)) == TEMPLATE_TYPE_PARM)
3506                       id = TREE_TYPE (id);
3507                   }
3508
3509                 if (id != error_mark_node)
3510                   id = build_nt (BIT_NOT_EXPR, id);
3511                 return id;
3512               }
3513
3514             cp_parser_parse_tentatively (parser);
3515             parser->scope = object_scope;
3516             parser->object_scope = NULL_TREE;
3517             parser->qualifying_scope = NULL_TREE;
3518             type_decl
3519               = cp_parser_class_name (parser,
3520                                         /*typename_keyword_p=*/false,
3521                                       /*template_keyword_p=*/false,
3522                                       none_type,
3523                                       /*check_dependency=*/false,
3524                                       /*class_head_p=*/false,
3525                                       declarator_p);
3526             /* The name is not qualified, so reset the parser scopes
3527                so our callers do not get confused.  */
3528             parser->object_scope = object_scope;
3529             parser->scope = NULL_TREE;
3530             if (cp_parser_parse_definitely (parser))
3531               done = true;
3532           }
3533         
3534         /* Look in the surrounding context.  */
3535         if (!done)
3536           {
3537             parser->scope = NULL_TREE;
3538             parser->object_scope = NULL_TREE;
3539             parser->qualifying_scope = NULL_TREE;
3540             type_decl
3541               = cp_parser_class_name (parser,
3542                                       /*typename_keyword_p=*/false,
3543                                       /*template_keyword_p=*/false,
3544                                       none_type,
3545                                       /*check_dependency=*/false,
3546                                       /*class_head_p=*/false,
3547                                       declarator_p);
3548           }
3549         /* If an error occurred, assume that the name of the
3550            destructor is the same as the name of the qualifying
3551            class.  That allows us to keep parsing after running
3552            into ill-formed destructor names.  */
3553         if (type_decl == error_mark_node && scope)
3554           return build_nt (BIT_NOT_EXPR, scope);
3555         else if (type_decl == error_mark_node)
3556           return error_mark_node;
3557
3558         /* Check that destructor name and scope match.  */
3559         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3560           {
3561             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3562               error ("declaration of %<~%T%> as member of %qT",
3563                      type_decl, scope);
3564             cp_parser_simulate_error (parser);
3565             return error_mark_node;
3566           }
3567
3568         /* [class.dtor]
3569
3570            A typedef-name that names a class shall not be used as the
3571            identifier in the declarator for a destructor declaration.  */
3572         if (declarator_p
3573             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3574             && !DECL_SELF_REFERENCE_P (type_decl)
3575             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3576           error ("typedef-name %qD used as destructor declarator",
3577                  type_decl);
3578
3579         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3580       }
3581
3582     case CPP_KEYWORD:
3583       if (token->keyword == RID_OPERATOR)
3584         {
3585           tree id;
3586
3587           /* This could be a template-id, so we try that first.  */
3588           cp_parser_parse_tentatively (parser);
3589           /* Try a template-id.  */
3590           id = cp_parser_template_id (parser, template_keyword_p,
3591                                       /*check_dependency_p=*/true,
3592                                       declarator_p);
3593           /* If that worked, we're done.  */
3594           if (cp_parser_parse_definitely (parser))
3595             return id;
3596           /* We still don't know whether we're looking at an
3597              operator-function-id or a conversion-function-id.  */
3598           cp_parser_parse_tentatively (parser);
3599           /* Try an operator-function-id.  */
3600           id = cp_parser_operator_function_id (parser);
3601           /* If that didn't work, try a conversion-function-id.  */
3602           if (!cp_parser_parse_definitely (parser))
3603             id = cp_parser_conversion_function_id (parser);
3604
3605           return id;
3606         }
3607       /* Fall through.  */
3608
3609     default:
3610       if (optional_p)
3611         return NULL_TREE;
3612       cp_parser_error (parser, "expected unqualified-id");
3613       return error_mark_node;
3614     }
3615 }
3616
3617 /* Parse an (optional) nested-name-specifier.
3618
3619    nested-name-specifier:
3620      class-or-namespace-name :: nested-name-specifier [opt]
3621      class-or-namespace-name :: template nested-name-specifier [opt]
3622
3623    PARSER->SCOPE should be set appropriately before this function is
3624    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3625    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3626    in name lookups.
3627
3628    Sets PARSER->SCOPE to the class (TYPE) or namespace
3629    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3630    it unchanged if there is no nested-name-specifier.  Returns the new
3631    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3632
3633    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3634    part of a declaration and/or decl-specifier.  */
3635
3636 static tree
3637 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3638                                      bool typename_keyword_p,
3639                                      bool check_dependency_p,
3640                                      bool type_p,
3641                                      bool is_declaration)
3642 {
3643   bool success = false;
3644   cp_token_position start = 0;
3645   cp_token *token;
3646
3647   /* Remember where the nested-name-specifier starts.  */
3648   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3649     {
3650       start = cp_lexer_token_position (parser->lexer, false);
3651       push_deferring_access_checks (dk_deferred);
3652     }
3653
3654   while (true)
3655     {
3656       tree new_scope;
3657       tree old_scope;
3658       tree saved_qualifying_scope;
3659       bool template_keyword_p;
3660
3661       /* Spot cases that cannot be the beginning of a
3662          nested-name-specifier.  */
3663       token = cp_lexer_peek_token (parser->lexer);
3664
3665       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3666          the already parsed nested-name-specifier.  */
3667       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3668         {
3669           /* Grab the nested-name-specifier and continue the loop.  */
3670           cp_parser_pre_parsed_nested_name_specifier (parser);
3671           success = true;
3672           continue;
3673         }
3674
3675       /* Spot cases that cannot be the beginning of a
3676          nested-name-specifier.  On the second and subsequent times
3677          through the loop, we look for the `template' keyword.  */
3678       if (success && token->keyword == RID_TEMPLATE)
3679         ;
3680       /* A template-id can start a nested-name-specifier.  */
3681       else if (token->type == CPP_TEMPLATE_ID)
3682         ;
3683       else
3684         {
3685           /* If the next token is not an identifier, then it is
3686              definitely not a class-or-namespace-name.  */
3687           if (token->type != CPP_NAME)
3688             break;
3689           /* If the following token is neither a `<' (to begin a
3690              template-id), nor a `::', then we are not looking at a
3691              nested-name-specifier.  */
3692           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3693           if (token->type != CPP_SCOPE
3694               && !cp_parser_nth_token_starts_template_argument_list_p
3695                   (parser, 2))
3696             break;
3697         }
3698
3699       /* The nested-name-specifier is optional, so we parse
3700          tentatively.  */
3701       cp_parser_parse_tentatively (parser);
3702
3703       /* Look for the optional `template' keyword, if this isn't the
3704          first time through the loop.  */
3705       if (success)
3706         template_keyword_p = cp_parser_optional_template_keyword (parser);
3707       else
3708         template_keyword_p = false;
3709
3710       /* Save the old scope since the name lookup we are about to do
3711          might destroy it.  */
3712       old_scope = parser->scope;
3713       saved_qualifying_scope = parser->qualifying_scope;
3714       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3715          look up names in "X<T>::I" in order to determine that "Y" is
3716          a template.  So, if we have a typename at this point, we make
3717          an effort to look through it.  */
3718       if (is_declaration
3719           && !typename_keyword_p
3720           && parser->scope
3721           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3722         parser->scope = resolve_typename_type (parser->scope,
3723                                                /*only_current_p=*/false);
3724       /* Parse the qualifying entity.  */
3725       new_scope
3726         = cp_parser_class_or_namespace_name (parser,
3727                                              typename_keyword_p,
3728                                              template_keyword_p,
3729                                              check_dependency_p,
3730                                              type_p,
3731                                              is_declaration);
3732       /* Look for the `::' token.  */
3733       cp_parser_require (parser, CPP_SCOPE, "`::'");
3734
3735       /* If we found what we wanted, we keep going; otherwise, we're
3736          done.  */
3737       if (!cp_parser_parse_definitely (parser))
3738         {
3739           bool error_p = false;
3740
3741           /* Restore the OLD_SCOPE since it was valid before the
3742              failed attempt at finding the last
3743              class-or-namespace-name.  */
3744           parser->scope = old_scope;
3745           parser->qualifying_scope = saved_qualifying_scope;
3746           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3747             break;
3748           /* If the next token is an identifier, and the one after
3749              that is a `::', then any valid interpretation would have
3750              found a class-or-namespace-name.  */
3751           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3752                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3753                      == CPP_SCOPE)
3754                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3755                      != CPP_COMPL))
3756             {
3757               token = cp_lexer_consume_token (parser->lexer);
3758               if (!error_p)
3759                 {
3760                   if (!token->ambiguous_p)
3761                     {
3762                       tree decl;
3763                       tree ambiguous_decls;
3764
3765                       decl = cp_parser_lookup_name (parser, token->value,
3766                                                     none_type,
3767                                                     /*is_template=*/false,
3768                                                     /*is_namespace=*/false,
3769                                                     /*check_dependency=*/true,
3770                                                     &ambiguous_decls);
3771                       if (TREE_CODE (decl) == TEMPLATE_DECL)
3772                         error ("%qD used without template parameters", decl);
3773                       else if (ambiguous_decls)
3774                         {
3775                           error ("reference to %qD is ambiguous",
3776                                  token->value);
3777                           print_candidates (ambiguous_decls);
3778                           decl = error_mark_node;
3779                         }
3780                       else
3781                         cp_parser_name_lookup_error
3782                           (parser, token->value, decl,
3783                            "is not a class or namespace");
3784                     }
3785                   parser->scope = error_mark_node;
3786                   error_p = true;
3787                   /* Treat this as a successful nested-name-specifier
3788                      due to:
3789
3790                      [basic.lookup.qual]
3791
3792                      If the name found is not a class-name (clause
3793                      _class_) or namespace-name (_namespace.def_), the
3794                      program is ill-formed.  */
3795                   success = true;
3796                 }
3797               cp_lexer_consume_token (parser->lexer);
3798             }
3799           break;
3800         }
3801       /* We've found one valid nested-name-specifier.  */
3802       success = true;
3803       /* Name lookup always gives us a DECL.  */
3804       if (TREE_CODE (new_scope) == TYPE_DECL)
3805         new_scope = TREE_TYPE (new_scope);
3806       /* Uses of "template" must be followed by actual templates.  */
3807       if (template_keyword_p
3808           && !(CLASS_TYPE_P (new_scope)
3809                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3810                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3811                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
3812           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3813                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3814                    == TEMPLATE_ID_EXPR)))
3815         pedwarn (TYPE_P (new_scope)
3816                  ? "%qT is not a template"
3817                  : "%qD is not a template",
3818                  new_scope);
3819       /* If it is a class scope, try to complete it; we are about to
3820          be looking up names inside the class.  */
3821       if (TYPE_P (new_scope)
3822           /* Since checking types for dependency can be expensive,
3823              avoid doing it if the type is already complete.  */
3824           && !COMPLETE_TYPE_P (new_scope)
3825           /* Do not try to complete dependent types.  */
3826           && !dependent_type_p (new_scope))
3827         new_scope = complete_type (new_scope);
3828       /* Make sure we look in the right scope the next time through
3829          the loop.  */
3830       parser->scope = new_scope;
3831     }
3832
3833   /* If parsing tentatively, replace the sequence of tokens that makes
3834      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3835      token.  That way, should we re-parse the token stream, we will
3836      not have to repeat the effort required to do the parse, nor will
3837      we issue duplicate error messages.  */
3838   if (success && start)
3839     {
3840       cp_token *token;
3841       tree access_checks;
3842
3843       token = cp_lexer_token_at (parser->lexer, start);
3844       /* Reset the contents of the START token.  */
3845       token->type = CPP_NESTED_NAME_SPECIFIER;
3846       /* Retrieve any deferred checks.  Do not pop this access checks yet
3847          so the memory will not be reclaimed during token replacing below.  */
3848       access_checks = get_deferred_access_checks ();
3849       token->value = build_tree_list (copy_list (access_checks),
3850                                       parser->scope);
3851       TREE_TYPE (token->value) = parser->qualifying_scope;
3852       token->keyword = RID_MAX;
3853
3854       /* Purge all subsequent tokens.  */
3855       cp_lexer_purge_tokens_after (parser->lexer, start);
3856     }
3857
3858   if (start)
3859     pop_to_parent_deferring_access_checks ();
3860
3861   return success ? parser->scope : NULL_TREE;
3862 }
3863
3864 /* Parse a nested-name-specifier.  See
3865    cp_parser_nested_name_specifier_opt for details.  This function
3866    behaves identically, except that it will an issue an error if no
3867    nested-name-specifier is present.  */
3868
3869 static tree
3870 cp_parser_nested_name_specifier (cp_parser *parser,
3871                                  bool typename_keyword_p,
3872                                  bool check_dependency_p,
3873                                  bool type_p,
3874                                  bool is_declaration)
3875 {
3876   tree scope;
3877
3878   /* Look for the nested-name-specifier.  */
3879   scope = cp_parser_nested_name_specifier_opt (parser,
3880                                                typename_keyword_p,
3881                                                check_dependency_p,
3882                                                type_p,
3883                                                is_declaration);
3884   /* If it was not present, issue an error message.  */
3885   if (!scope)
3886     {
3887       cp_parser_error (parser, "expected nested-name-specifier");
3888       parser->scope = NULL_TREE;
3889     }
3890
3891   return scope;
3892 }
3893
3894 /* Parse a class-or-namespace-name.
3895
3896    class-or-namespace-name:
3897      class-name
3898      namespace-name
3899
3900    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3901    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3902    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3903    TYPE_P is TRUE iff the next name should be taken as a class-name,
3904    even the same name is declared to be another entity in the same
3905    scope.
3906
3907    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3908    specified by the class-or-namespace-name.  If neither is found the
3909    ERROR_MARK_NODE is returned.  */
3910
3911 static tree
3912 cp_parser_class_or_namespace_name (cp_parser *parser,
3913                                    bool typename_keyword_p,
3914                                    bool template_keyword_p,
3915                                    bool check_dependency_p,
3916                                    bool type_p,
3917                                    bool is_declaration)
3918 {
3919   tree saved_scope;
3920   tree saved_qualifying_scope;
3921   tree saved_object_scope;
3922   tree scope;
3923   bool only_class_p;
3924
3925   /* Before we try to parse the class-name, we must save away the
3926      current PARSER->SCOPE since cp_parser_class_name will destroy
3927      it.  */
3928   saved_scope = parser->scope;
3929   saved_qualifying_scope = parser->qualifying_scope;
3930   saved_object_scope = parser->object_scope;
3931   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3932      there is no need to look for a namespace-name.  */
3933   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3934   if (!only_class_p)
3935     cp_parser_parse_tentatively (parser);
3936   scope = cp_parser_class_name (parser,
3937                                 typename_keyword_p,
3938                                 template_keyword_p,
3939                                 type_p ? class_type : none_type,
3940                                 check_dependency_p,
3941                                 /*class_head_p=*/false,
3942                                 is_declaration);
3943   /* If that didn't work, try for a namespace-name.  */
3944   if (!only_class_p && !cp_parser_parse_definitely (parser))
3945     {
3946       /* Restore the saved scope.  */
3947       parser->scope = saved_scope;
3948       parser->qualifying_scope = saved_qualifying_scope;
3949       parser->object_scope = saved_object_scope;
3950       /* If we are not looking at an identifier followed by the scope
3951          resolution operator, then this is not part of a
3952          nested-name-specifier.  (Note that this function is only used
3953          to parse the components of a nested-name-specifier.)  */
3954       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3955           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3956         return error_mark_node;
3957       scope = cp_parser_namespace_name (parser);
3958     }
3959
3960   return scope;
3961 }
3962
3963 /* Parse a postfix-expression.
3964
3965    postfix-expression:
3966      primary-expression
3967      postfix-expression [ expression ]
3968      postfix-expression ( expression-list [opt] )
3969      simple-type-specifier ( expression-list [opt] )
3970      typename :: [opt] nested-name-specifier identifier
3971        ( expression-list [opt] )
3972      typename :: [opt] nested-name-specifier template [opt] template-id
3973        ( expression-list [opt] )
3974      postfix-expression . template [opt] id-expression
3975      postfix-expression -> template [opt] id-expression
3976      postfix-expression . pseudo-destructor-name
3977      postfix-expression -> pseudo-destructor-name
3978      postfix-expression ++
3979      postfix-expression --
3980      dynamic_cast < type-id > ( expression )
3981      static_cast < type-id > ( expression )
3982      reinterpret_cast < type-id > ( expression )
3983      const_cast < type-id > ( expression )
3984      typeid ( expression )
3985      typeid ( type-id )
3986
3987    GNU Extension:
3988
3989    postfix-expression:
3990      ( type-id ) { initializer-list , [opt] }
3991
3992    This extension is a GNU version of the C99 compound-literal
3993    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3994    but they are essentially the same concept.)
3995
3996    If ADDRESS_P is true, the postfix expression is the operand of the
3997    `&' operator.  CAST_P is true if this expression is the target of a
3998    cast.
3999
4000    Returns a representation of the expression.  */
4001
4002 static tree
4003 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4004 {
4005   cp_token *token;
4006   enum rid keyword;
4007   cp_id_kind idk = CP_ID_KIND_NONE;
4008   tree postfix_expression = NULL_TREE;
4009
4010   /* Peek at the next token.  */
4011   token = cp_lexer_peek_token (parser->lexer);
4012   /* Some of the productions are determined by keywords.  */
4013   keyword = token->keyword;
4014   switch (keyword)
4015     {
4016     case RID_DYNCAST:
4017     case RID_STATCAST:
4018     case RID_REINTCAST:
4019     case RID_CONSTCAST:
4020       {
4021         tree type;
4022         tree expression;
4023         const char *saved_message;
4024
4025         /* All of these can be handled in the same way from the point
4026            of view of parsing.  Begin by consuming the token
4027            identifying the cast.  */
4028         cp_lexer_consume_token (parser->lexer);
4029
4030         /* New types cannot be defined in the cast.  */
4031         saved_message = parser->type_definition_forbidden_message;
4032         parser->type_definition_forbidden_message
4033           = "types may not be defined in casts";
4034
4035         /* Look for the opening `<'.  */
4036         cp_parser_require (parser, CPP_LESS, "`<'");
4037         /* Parse the type to which we are casting.  */
4038         type = cp_parser_type_id (parser);
4039         /* Look for the closing `>'.  */
4040         cp_parser_require (parser, CPP_GREATER, "`>'");
4041         /* Restore the old message.  */
4042         parser->type_definition_forbidden_message = saved_message;
4043
4044         /* And the expression which is being cast.  */
4045         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4046         expression = cp_parser_expression (parser, /*cast_p=*/true);
4047         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4048
4049         /* Only type conversions to integral or enumeration types
4050            can be used in constant-expressions.  */
4051         if (!cast_valid_in_integral_constant_expression_p (type)
4052             && (cp_parser_non_integral_constant_expression
4053                 (parser,
4054                  "a cast to a type other than an integral or "
4055                  "enumeration type")))
4056           return error_mark_node;
4057
4058         switch (keyword)
4059           {
4060           case RID_DYNCAST:
4061             postfix_expression
4062               = build_dynamic_cast (type, expression);
4063             break;
4064           case RID_STATCAST:
4065             postfix_expression
4066               = build_static_cast (type, expression);
4067             break;
4068           case RID_REINTCAST:
4069             postfix_expression
4070               = build_reinterpret_cast (type, expression);
4071             break;
4072           case RID_CONSTCAST:
4073             postfix_expression
4074               = build_const_cast (type, expression);
4075             break;
4076           default:
4077             gcc_unreachable ();
4078           }
4079       }
4080       break;
4081
4082     case RID_TYPEID:
4083       {
4084         tree type;
4085         const char *saved_message;
4086         bool saved_in_type_id_in_expr_p;
4087
4088         /* Consume the `typeid' token.  */
4089         cp_lexer_consume_token (parser->lexer);
4090         /* Look for the `(' token.  */
4091         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4092         /* Types cannot be defined in a `typeid' expression.  */
4093         saved_message = parser->type_definition_forbidden_message;
4094         parser->type_definition_forbidden_message
4095           = "types may not be defined in a `typeid\' expression";
4096         /* We can't be sure yet whether we're looking at a type-id or an
4097            expression.  */
4098         cp_parser_parse_tentatively (parser);
4099         /* Try a type-id first.  */
4100         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4101         parser->in_type_id_in_expr_p = true;
4102         type = cp_parser_type_id (parser);
4103         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4104         /* Look for the `)' token.  Otherwise, we can't be sure that
4105            we're not looking at an expression: consider `typeid (int
4106            (3))', for example.  */
4107         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4108         /* If all went well, simply lookup the type-id.  */
4109         if (cp_parser_parse_definitely (parser))
4110           postfix_expression = get_typeid (type);
4111         /* Otherwise, fall back to the expression variant.  */
4112         else
4113           {
4114             tree expression;
4115
4116             /* Look for an expression.  */
4117             expression = cp_parser_expression (parser, /*cast_p=*/false);
4118             /* Compute its typeid.  */
4119             postfix_expression = build_typeid (expression);
4120             /* Look for the `)' token.  */
4121             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4122           }
4123         /* Restore the saved message.  */
4124         parser->type_definition_forbidden_message = saved_message;
4125         /* `typeid' may not appear in an integral constant expression.  */
4126         if (cp_parser_non_integral_constant_expression(parser,
4127                                                        "`typeid' operator"))
4128           return error_mark_node;
4129       }
4130       break;
4131
4132     case RID_TYPENAME:
4133       {
4134         tree type;
4135         /* The syntax permitted here is the same permitted for an
4136            elaborated-type-specifier.  */
4137         type = cp_parser_elaborated_type_specifier (parser,
4138                                                     /*is_friend=*/false,
4139                                                     /*is_declaration=*/false);
4140         postfix_expression = cp_parser_functional_cast (parser, type);
4141       }
4142       break;
4143
4144     default:
4145       {
4146         tree type;
4147
4148         /* If the next thing is a simple-type-specifier, we may be
4149            looking at a functional cast.  We could also be looking at
4150            an id-expression.  So, we try the functional cast, and if
4151            that doesn't work we fall back to the primary-expression.  */
4152         cp_parser_parse_tentatively (parser);
4153         /* Look for the simple-type-specifier.  */
4154         type = cp_parser_simple_type_specifier (parser,
4155                                                 /*decl_specs=*/NULL,
4156                                                 CP_PARSER_FLAGS_NONE);
4157         /* Parse the cast itself.  */
4158         if (!cp_parser_error_occurred (parser))
4159           postfix_expression
4160             = cp_parser_functional_cast (parser, type);
4161         /* If that worked, we're done.  */
4162         if (cp_parser_parse_definitely (parser))
4163           break;
4164
4165         /* If the functional-cast didn't work out, try a
4166            compound-literal.  */
4167         if (cp_parser_allow_gnu_extensions_p (parser)
4168             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4169           {
4170             VEC(constructor_elt,gc) *initializer_list = NULL;
4171             bool saved_in_type_id_in_expr_p;
4172
4173             cp_parser_parse_tentatively (parser);
4174             /* Consume the `('.  */
4175             cp_lexer_consume_token (parser->lexer);
4176             /* Parse the type.  */
4177             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4178             parser->in_type_id_in_expr_p = true;
4179             type = cp_parser_type_id (parser);
4180             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4181             /* Look for the `)'.  */
4182             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4183             /* Look for the `{'.  */
4184             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4185             /* If things aren't going well, there's no need to
4186                keep going.  */
4187             if (!cp_parser_error_occurred (parser))
4188               {
4189                 bool non_constant_p;
4190                 /* Parse the initializer-list.  */
4191                 initializer_list
4192                   = cp_parser_initializer_list (parser, &non_constant_p);
4193                 /* Allow a trailing `,'.  */
4194                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4195                   cp_lexer_consume_token (parser->lexer);
4196                 /* Look for the final `}'.  */
4197                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4198               }
4199             /* If that worked, we're definitely looking at a
4200                compound-literal expression.  */
4201             if (cp_parser_parse_definitely (parser))
4202               {
4203                 /* Warn the user that a compound literal is not
4204                    allowed in standard C++.  */
4205                 if (pedantic)
4206                   pedwarn ("ISO C++ forbids compound-literals");
4207                 /* Form the representation of the compound-literal.  */
4208                 postfix_expression
4209                   = finish_compound_literal (type, initializer_list);
4210                 break;
4211               }
4212           }
4213
4214         /* It must be a primary-expression.  */
4215         postfix_expression
4216           = cp_parser_primary_expression (parser, address_p, cast_p,
4217                                           /*template_arg_p=*/false,
4218                                           &idk);
4219       }
4220       break;
4221     }
4222
4223   /* Keep looping until the postfix-expression is complete.  */
4224   while (true)
4225     {
4226       if (idk == CP_ID_KIND_UNQUALIFIED
4227           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4228           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4229         /* It is not a Koenig lookup function call.  */
4230         postfix_expression
4231           = unqualified_name_lookup_error (postfix_expression);
4232
4233       /* Peek at the next token.  */
4234       token = cp_lexer_peek_token (parser->lexer);
4235
4236       switch (token->type)
4237         {
4238         case CPP_OPEN_SQUARE:
4239           postfix_expression
4240             = cp_parser_postfix_open_square_expression (parser,
4241                                                         postfix_expression,
4242                                                         false);
4243           idk = CP_ID_KIND_NONE;
4244           break;
4245
4246         case CPP_OPEN_PAREN:
4247           /* postfix-expression ( expression-list [opt] ) */
4248           {
4249             bool koenig_p;
4250             bool is_builtin_constant_p;
4251             bool saved_integral_constant_expression_p = false;
4252             bool saved_non_integral_constant_expression_p = false;
4253             tree args;
4254
4255             is_builtin_constant_p
4256               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4257             if (is_builtin_constant_p)
4258               {
4259                 /* The whole point of __builtin_constant_p is to allow
4260                    non-constant expressions to appear as arguments.  */
4261                 saved_integral_constant_expression_p
4262                   = parser->integral_constant_expression_p;
4263                 saved_non_integral_constant_expression_p
4264                   = parser->non_integral_constant_expression_p;
4265                 parser->integral_constant_expression_p = false;
4266               }
4267             args = (cp_parser_parenthesized_expression_list
4268                     (parser, /*is_attribute_list=*/false,
4269                      /*cast_p=*/false,
4270                      /*non_constant_p=*/NULL));
4271             if (is_builtin_constant_p)
4272               {
4273                 parser->integral_constant_expression_p
4274                   = saved_integral_constant_expression_p;
4275                 parser->non_integral_constant_expression_p
4276                   = saved_non_integral_constant_expression_p;
4277               }
4278
4279             if (args == error_mark_node)
4280               {
4281                 postfix_expression = error_mark_node;
4282                 break;
4283               }
4284
4285             /* Function calls are not permitted in
4286                constant-expressions.  */
4287             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4288                 && cp_parser_non_integral_constant_expression (parser,
4289                                                                "a function call"))
4290               {
4291                 postfix_expression = error_mark_node;
4292                 break;
4293               }
4294
4295             koenig_p = false;
4296             if (idk == CP_ID_KIND_UNQUALIFIED)
4297               {
4298                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4299                   {
4300                     if (args)
4301                       {
4302                         koenig_p = true;
4303                         postfix_expression
4304                           = perform_koenig_lookup (postfix_expression, args);
4305                       }
4306                     else
4307                       postfix_expression
4308                         = unqualified_fn_lookup_error (postfix_expression);
4309                   }
4310                 /* We do not perform argument-dependent lookup if
4311                    normal lookup finds a non-function, in accordance
4312                    with the expected resolution of DR 218.  */
4313                 else if (args && is_overloaded_fn (postfix_expression))
4314                   {
4315                     tree fn = get_first_fn (postfix_expression);
4316
4317                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4318                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4319
4320                     /* Only do argument dependent lookup if regular
4321                        lookup does not find a set of member functions.
4322                        [basic.lookup.koenig]/2a  */
4323                     if (!DECL_FUNCTION_MEMBER_P (fn))
4324                       {
4325                         koenig_p = true;
4326                         postfix_expression
4327                           = perform_koenig_lookup (postfix_expression, args);
4328                       }
4329                   }
4330               }
4331
4332             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4333               {
4334                 tree instance = TREE_OPERAND (postfix_expression, 0);
4335                 tree fn = TREE_OPERAND (postfix_expression, 1);
4336
4337                 if (processing_template_decl
4338                     && (type_dependent_expression_p (instance)
4339                         || (!BASELINK_P (fn)
4340                             && TREE_CODE (fn) != FIELD_DECL)
4341                         || type_dependent_expression_p (fn)
4342                         || any_type_dependent_arguments_p (args)))
4343                   {
4344                     postfix_expression
4345                       = build_min_nt (CALL_EXPR, postfix_expression,
4346                                       args, NULL_TREE);
4347                     break;
4348                   }
4349
4350                 if (BASELINK_P (fn))
4351                   postfix_expression
4352                     = (build_new_method_call
4353                        (instance, fn, args, NULL_TREE,
4354                         (idk == CP_ID_KIND_QUALIFIED
4355                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4356                         /*fn_p=*/NULL));
4357                 else
4358                   postfix_expression
4359                     = finish_call_expr (postfix_expression, args,
4360                                         /*disallow_virtual=*/false,
4361                                         /*koenig_p=*/false);
4362               }
4363             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4364                      || TREE_CODE (postfix_expression) == MEMBER_REF
4365                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4366               postfix_expression = (build_offset_ref_call_from_tree
4367                                     (postfix_expression, args));
4368             else if (idk == CP_ID_KIND_QUALIFIED)
4369               /* A call to a static class member, or a namespace-scope
4370                  function.  */
4371               postfix_expression
4372                 = finish_call_expr (postfix_expression, args,
4373                                     /*disallow_virtual=*/true,
4374                                     koenig_p);
4375             else
4376               /* All other function calls.  */
4377               postfix_expression
4378                 = finish_call_expr (postfix_expression, args,
4379                                     /*disallow_virtual=*/false,
4380                                     koenig_p);
4381
4382             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4383             idk = CP_ID_KIND_NONE;
4384           }
4385           break;
4386
4387         case CPP_DOT:
4388         case CPP_DEREF:
4389           /* postfix-expression . template [opt] id-expression
4390              postfix-expression . pseudo-destructor-name
4391              postfix-expression -> template [opt] id-expression
4392              postfix-expression -> pseudo-destructor-name */
4393
4394           /* Consume the `.' or `->' operator.  */
4395           cp_lexer_consume_token (parser->lexer);
4396
4397           postfix_expression
4398             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4399                                                       postfix_expression,
4400                                                       false, &idk);
4401           break;
4402
4403         case CPP_PLUS_PLUS:
4404           /* postfix-expression ++  */
4405           /* Consume the `++' token.  */
4406           cp_lexer_consume_token (parser->lexer);
4407           /* Generate a representation for the complete expression.  */
4408           postfix_expression
4409             = finish_increment_expr (postfix_expression,
4410                                      POSTINCREMENT_EXPR);
4411           /* Increments may not appear in constant-expressions.  */
4412           if (cp_parser_non_integral_constant_expression (parser,
4413                                                           "an increment"))
4414             postfix_expression = error_mark_node;
4415           idk = CP_ID_KIND_NONE;
4416           break;
4417
4418         case CPP_MINUS_MINUS:
4419           /* postfix-expression -- */
4420           /* Consume the `--' token.  */
4421           cp_lexer_consume_token (parser->lexer);
4422           /* Generate a representation for the complete expression.  */
4423           postfix_expression
4424             = finish_increment_expr (postfix_expression,
4425                                      POSTDECREMENT_EXPR);
4426           /* Decrements may not appear in constant-expressions.  */
4427           if (cp_parser_non_integral_constant_expression (parser,
4428                                                           "a decrement"))
4429             postfix_expression = error_mark_node;
4430           idk = CP_ID_KIND_NONE;
4431           break;
4432
4433         default:
4434           return postfix_expression;
4435         }
4436     }
4437
4438   /* We should never get here.  */
4439   gcc_unreachable ();
4440   return error_mark_node;
4441 }
4442
4443 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4444    by cp_parser_builtin_offsetof.  We're looking for
4445
4446      postfix-expression [ expression ]
4447
4448    FOR_OFFSETOF is set if we're being called in that context, which
4449    changes how we deal with integer constant expressions.  */
4450
4451 static tree
4452 cp_parser_postfix_open_square_expression (cp_parser *parser,
4453                                           tree postfix_expression,
4454                                           bool for_offsetof)
4455 {
4456   tree index;
4457
4458   /* Consume the `[' token.  */
4459   cp_lexer_consume_token (parser->lexer);
4460
4461   /* Parse the index expression.  */
4462   /* ??? For offsetof, there is a question of what to allow here.  If
4463      offsetof is not being used in an integral constant expression context,
4464      then we *could* get the right answer by computing the value at runtime.
4465      If we are in an integral constant expression context, then we might
4466      could accept any constant expression; hard to say without analysis.
4467      Rather than open the barn door too wide right away, allow only integer
4468      constant expressions here.  */
4469   if (for_offsetof)
4470     index = cp_parser_constant_expression (parser, false, NULL);
4471   else
4472     index = cp_parser_expression (parser, /*cast_p=*/false);
4473
4474   /* Look for the closing `]'.  */
4475   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4476
4477   /* Build the ARRAY_REF.  */
4478   postfix_expression = grok_array_decl (postfix_expression, index);
4479
4480   /* When not doing offsetof, array references are not permitted in
4481      constant-expressions.  */
4482   if (!for_offsetof
4483       && (cp_parser_non_integral_constant_expression
4484           (parser, "an array reference")))
4485     postfix_expression = error_mark_node;
4486
4487   return postfix_expression;
4488 }
4489
4490 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4491    by cp_parser_builtin_offsetof.  We're looking for
4492
4493      postfix-expression . template [opt] id-expression
4494      postfix-expression . pseudo-destructor-name
4495      postfix-expression -> template [opt] id-expression
4496      postfix-expression -> pseudo-destructor-name
4497
4498    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4499    limits what of the above we'll actually accept, but nevermind.
4500    TOKEN_TYPE is the "." or "->" token, which will already have been
4501    removed from the stream.  */
4502
4503 static tree
4504 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4505                                         enum cpp_ttype token_type,
4506                                         tree postfix_expression,
4507                                         bool for_offsetof, cp_id_kind *idk)
4508 {
4509   tree name;
4510   bool dependent_p;
4511   bool pseudo_destructor_p;
4512   tree scope = NULL_TREE;
4513
4514   /* If this is a `->' operator, dereference the pointer.  */
4515   if (token_type == CPP_DEREF)
4516     postfix_expression = build_x_arrow (postfix_expression);
4517   /* Check to see whether or not the expression is type-dependent.  */
4518   dependent_p = type_dependent_expression_p (postfix_expression);
4519   /* The identifier following the `->' or `.' is not qualified.  */
4520   parser->scope = NULL_TREE;
4521   parser->qualifying_scope = NULL_TREE;
4522   parser->object_scope = NULL_TREE;
4523   *idk = CP_ID_KIND_NONE;
4524
4525   /* Enter the scope corresponding to the type of the object
4526      given by the POSTFIX_EXPRESSION.  */
4527   scope = TREE_TYPE (postfix_expression);
4528   if (!dependent_p && scope)
4529     {
4530       /* According to the standard, no expression should ever have
4531          reference type.  Unfortunately, we do not currently match
4532          the standard in this respect in that our internal representation
4533          of an expression may have reference type even when the standard
4534          says it does not.  Therefore, we have to manually obtain the
4535          underlying type here.  */
4536       scope = non_reference (scope);
4537       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4538       if (scope == unknown_type_node)
4539         {
4540           error ("%qE does not have class type", postfix_expression);
4541           scope = NULL_TREE;
4542         }
4543       else if (!dependent_p)
4544         scope = complete_type_or_else (scope, NULL_TREE);
4545       /* If something went wrong, we want to be able to discern that case,
4546          as opposed to the case where there was no SCOPE due to the type
4547          of expression being dependent.  */
4548       if (!scope)
4549         scope = error_mark_node;
4550       /* If the SCOPE was erroneous, make the various semantic analysis
4551          functions exit quickly -- and without issuing additional error
4552          messages.  */
4553       if (scope == error_mark_node)
4554         postfix_expression = error_mark_node;
4555     }
4556   /* Let the name lookup machinery know that we are processing a class
4557      member access expression.  */
4558   parser->context->object_type = scope;
4559   parser->object_scope = scope;
4560
4561   /* Assume this expression is not a pseudo-destructor access.  */
4562   pseudo_destructor_p = false;
4563
4564   /* If the SCOPE is a scalar type, then, if this is a valid program,
4565      we must be looking at a pseudo-destructor-name.  */
4566   if (scope && SCALAR_TYPE_P (scope))
4567     {
4568       tree s;
4569       tree type;
4570
4571       cp_parser_parse_tentatively (parser);
4572       /* Parse the pseudo-destructor-name.  */
4573       s = NULL_TREE;
4574       cp_parser_pseudo_destructor_name (parser, &s, &type);
4575       if (cp_parser_parse_definitely (parser))
4576         {
4577           pseudo_destructor_p = true;
4578           postfix_expression
4579             = finish_pseudo_destructor_expr (postfix_expression,
4580                                              s, TREE_TYPE (type));
4581         }
4582     }
4583
4584   if (!pseudo_destructor_p)
4585     {
4586       /* If the SCOPE is not a scalar type, we are looking at an
4587          ordinary class member access expression, rather than a
4588          pseudo-destructor-name.  */
4589       bool template_p;
4590       /* Parse the id-expression.  */
4591       name = (cp_parser_id_expression
4592               (parser,
4593                cp_parser_optional_template_keyword (parser),
4594                /*check_dependency_p=*/true,
4595                &template_p,
4596                /*declarator_p=*/false,
4597                /*optional_p=*/false,
4598                /*member_p=*/true));
4599       /* In general, build a SCOPE_REF if the member name is qualified.
4600          However, if the name was not dependent and has already been
4601          resolved; there is no need to build the SCOPE_REF.  For example;
4602
4603              struct X { void f(); };
4604              template <typename T> void f(T* t) { t->X::f(); }
4605
4606          Even though "t" is dependent, "X::f" is not and has been resolved
4607          to a BASELINK; there is no need to include scope information.  */
4608
4609       /* But we do need to remember that there was an explicit scope for
4610          virtual function calls.  */
4611       if (parser->scope)
4612         *idk = CP_ID_KIND_QUALIFIED;
4613
4614       /* If the name is a template-id that names a type, we will get a
4615          TYPE_DECL here.  That is invalid code.  */
4616       if (TREE_CODE (name) == TYPE_DECL)
4617         {
4618           error ("invalid use of %qD", name);
4619           postfix_expression = error_mark_node;
4620         }
4621       else
4622         {
4623           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4624             {
4625               name = build_qualified_name (/*type=*/NULL_TREE,
4626                                            parser->scope,
4627                                            name,
4628                                            template_p);
4629               parser->scope = NULL_TREE;
4630               parser->qualifying_scope = NULL_TREE;
4631               parser->object_scope = NULL_TREE;
4632             }
4633           if (scope && name && BASELINK_P (name))
4634             adjust_result_of_qualified_name_lookup
4635               (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4636           postfix_expression
4637             = finish_class_member_access_expr (postfix_expression, name,
4638                                                template_p);
4639         }
4640     }
4641
4642   /* We no longer need to look up names in the scope of the object on
4643      the left-hand side of the `.' or `->' operator.  */
4644   parser->context->object_type = NULL_TREE;
4645
4646   /* Outside of offsetof, these operators may not appear in
4647      constant-expressions.  */
4648   if (!for_offsetof
4649       && (cp_parser_non_integral_constant_expression
4650           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4651     postfix_expression = error_mark_node;
4652
4653   return postfix_expression;
4654 }
4655
4656 /* Parse a parenthesized expression-list.
4657
4658    expression-list:
4659      assignment-expression
4660      expression-list, assignment-expression
4661
4662    attribute-list:
4663      expression-list
4664      identifier
4665      identifier, expression-list
4666
4667    CAST_P is true if this expression is the target of a cast.
4668
4669    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4670    representation of an assignment-expression.  Note that a TREE_LIST
4671    is returned even if there is only a single expression in the list.
4672    error_mark_node is returned if the ( and or ) are
4673    missing. NULL_TREE is returned on no expressions. The parentheses
4674    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4675    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4676    indicates whether or not all of the expressions in the list were
4677    constant.  */
4678
4679 static tree
4680 cp_parser_parenthesized_expression_list (cp_parser* parser,
4681                                          bool is_attribute_list,
4682                                          bool cast_p,
4683                                          bool *non_constant_p)
4684 {
4685   tree expression_list = NULL_TREE;
4686   bool fold_expr_p = is_attribute_list;
4687   tree identifier = NULL_TREE;
4688
4689   /* Assume all the expressions will be constant.  */
4690   if (non_constant_p)
4691     *non_constant_p = false;
4692
4693   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4694     return error_mark_node;
4695
4696   /* Consume expressions until there are no more.  */
4697   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4698     while (true)
4699       {
4700         tree expr;
4701
4702         /* At the beginning of attribute lists, check to see if the
4703            next token is an identifier.  */
4704         if (is_attribute_list
4705             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4706           {
4707             cp_token *token;
4708
4709             /* Consume the identifier.  */
4710             token = cp_lexer_consume_token (parser->lexer);
4711             /* Save the identifier.  */
4712             identifier = token->value;
4713           }
4714         else
4715           {
4716             /* Parse the next assignment-expression.  */
4717             if (non_constant_p)
4718               {
4719                 bool expr_non_constant_p;
4720                 expr = (cp_parser_constant_expression
4721                         (parser, /*allow_non_constant_p=*/true,
4722                          &expr_non_constant_p));
4723                 if (expr_non_constant_p)
4724                   *non_constant_p = true;
4725               }
4726             else
4727               expr = cp_parser_assignment_expression (parser, cast_p);
4728
4729             if (fold_expr_p)
4730               expr = fold_non_dependent_expr (expr);
4731
4732              /* Add it to the list.  We add error_mark_node
4733                 expressions to the list, so that we can still tell if
4734                 the correct form for a parenthesized expression-list
4735                 is found. That gives better errors.  */
4736             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4737
4738             if (expr == error_mark_node)
4739               goto skip_comma;
4740           }
4741
4742         /* After the first item, attribute lists look the same as
4743            expression lists.  */
4744         is_attribute_list = false;
4745
4746       get_comma:;
4747         /* If the next token isn't a `,', then we are done.  */
4748         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4749           break;
4750
4751         /* Otherwise, consume the `,' and keep going.  */
4752         cp_lexer_consume_token (parser->lexer);
4753       }
4754
4755   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4756     {
4757       int ending;
4758
4759     skip_comma:;
4760       /* We try and resync to an unnested comma, as that will give the
4761          user better diagnostics.  */
4762       ending = cp_parser_skip_to_closing_parenthesis (parser,
4763                                                       /*recovering=*/true,
4764                                                       /*or_comma=*/true,
4765                                                       /*consume_paren=*/true);
4766       if (ending < 0)
4767         goto get_comma;
4768       if (!ending)
4769         return error_mark_node;
4770     }
4771
4772   /* We built up the list in reverse order so we must reverse it now.  */
4773   expression_list = nreverse (expression_list);
4774   if (identifier)
4775     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4776
4777   return expression_list;
4778 }
4779
4780 /* Parse a pseudo-destructor-name.
4781
4782    pseudo-destructor-name:
4783      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4784      :: [opt] nested-name-specifier template template-id :: ~ type-name
4785      :: [opt] nested-name-specifier [opt] ~ type-name
4786
4787    If either of the first two productions is used, sets *SCOPE to the
4788    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4789    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4790    or ERROR_MARK_NODE if the parse fails.  */
4791
4792 static void
4793 cp_parser_pseudo_destructor_name (cp_parser* parser,
4794                                   tree* scope,
4795                                   tree* type)
4796 {
4797   bool nested_name_specifier_p;
4798
4799   /* Assume that things will not work out.  */
4800   *type = error_mark_node;
4801
4802   /* Look for the optional `::' operator.  */
4803   cp_parser_global_scope_opt (parser,
4804                               /*current_scope_valid_p=*/true,
4805                               /*object_scop_valid_p=*/true);
4806   /* Look for the optional nested-name-specifier.  */
4807   nested_name_specifier_p
4808     = (cp_parser_nested_name_specifier_opt (parser,
4809                                             /*typename_keyword_p=*/false,
4810                                             /*check_dependency_p=*/true,
4811                                             /*type_p=*/false,
4812                                             /*is_declaration=*/true)
4813        != NULL_TREE);
4814   /* Now, if we saw a nested-name-specifier, we might be doing the
4815      second production.  */
4816   if (nested_name_specifier_p
4817       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4818     {
4819       /* Consume the `template' keyword.  */
4820       cp_lexer_consume_token (parser->lexer);
4821       /* Parse the template-id.  */
4822       cp_parser_template_id (parser,
4823                              /*template_keyword_p=*/true,
4824                              /*check_dependency_p=*/false,
4825                              /*is_declaration=*/true);
4826       /* Look for the `::' token.  */
4827       cp_parser_require (parser, CPP_SCOPE, "`::'");
4828     }
4829   /* If the next token is not a `~', then there might be some
4830      additional qualification.  */
4831   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4832     {
4833       /* Look for the type-name.  */
4834       *scope = TREE_TYPE (cp_parser_type_name (parser));
4835
4836       if (*scope == error_mark_node)
4837         return;
4838
4839       /* If we don't have ::~, then something has gone wrong.  Since
4840          the only caller of this function is looking for something
4841          after `.' or `->' after a scalar type, most likely the
4842          program is trying to get a member of a non-aggregate
4843          type.  */
4844       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4845           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4846         {
4847           cp_parser_error (parser, "request for member of non-aggregate type");
4848           return;
4849         }
4850
4851       /* Look for the `::' token.  */
4852       cp_parser_require (parser, CPP_SCOPE, "`::'");
4853     }
4854   else
4855     *scope = NULL_TREE;
4856
4857   /* Look for the `~'.  */
4858   cp_parser_require (parser, CPP_COMPL, "`~'");
4859   /* Look for the type-name again.  We are not responsible for
4860      checking that it matches the first type-name.  */
4861   *type = cp_parser_type_name (parser);
4862 }
4863
4864 /* Parse a unary-expression.
4865
4866    unary-expression:
4867      postfix-expression
4868      ++ cast-expression
4869      -- cast-expression
4870      unary-operator cast-expression
4871      sizeof unary-expression
4872      sizeof ( type-id )
4873      new-expression
4874      delete-expression
4875
4876    GNU Extensions:
4877
4878    unary-expression:
4879      __extension__ cast-expression
4880      __alignof__ unary-expression
4881      __alignof__ ( type-id )
4882      __real__ cast-expression
4883      __imag__ cast-expression
4884      && identifier
4885
4886    ADDRESS_P is true iff the unary-expression is appearing as the
4887    operand of the `&' operator.   CAST_P is true if this expression is
4888    the target of a cast.
4889
4890    Returns a representation of the expression.  */
4891
4892 static tree
4893 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4894 {
4895   cp_token *token;
4896   enum tree_code unary_operator;
4897
4898   /* Peek at the next token.  */
4899   token = cp_lexer_peek_token (parser->lexer);
4900   /* Some keywords give away the kind of expression.  */
4901   if (token->type == CPP_KEYWORD)
4902     {
4903       enum rid keyword = token->keyword;
4904
4905       switch (keyword)
4906         {
4907         case RID_ALIGNOF:
4908         case RID_SIZEOF:
4909           {
4910             tree operand;
4911             enum tree_code op;
4912
4913             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4914             /* Consume the token.  */
4915             cp_lexer_consume_token (parser->lexer);
4916             /* Parse the operand.  */
4917             operand = cp_parser_sizeof_operand (parser, keyword);
4918
4919             if (TYPE_P (operand))
4920               return cxx_sizeof_or_alignof_type (operand, op, true);
4921             else
4922               return cxx_sizeof_or_alignof_expr (operand, op);
4923           }
4924
4925         case RID_NEW:
4926           return cp_parser_new_expression (parser);
4927
4928         case RID_DELETE:
4929           return cp_parser_delete_expression (parser);
4930
4931         case RID_EXTENSION:
4932           {
4933             /* The saved value of the PEDANTIC flag.  */
4934             int saved_pedantic;
4935             tree expr;
4936
4937             /* Save away the PEDANTIC flag.  */
4938             cp_parser_extension_opt (parser, &saved_pedantic);
4939             /* Parse the cast-expression.  */
4940             expr = cp_parser_simple_cast_expression (parser);
4941             /* Restore the PEDANTIC flag.  */
4942             pedantic = saved_pedantic;
4943
4944             return expr;
4945           }
4946
4947         case RID_REALPART:
4948         case RID_IMAGPART:
4949           {
4950             tree expression;
4951
4952             /* Consume the `__real__' or `__imag__' token.  */
4953             cp_lexer_consume_token (parser->lexer);
4954             /* Parse the cast-expression.  */
4955             expression = cp_parser_simple_cast_expression (parser);
4956             /* Create the complete representation.  */
4957             return build_x_unary_op ((keyword == RID_REALPART
4958                                       ? REALPART_EXPR : IMAGPART_EXPR),
4959                                      expression);
4960           }
4961           break;
4962
4963         default:
4964           break;
4965         }
4966     }
4967
4968   /* Look for the `:: new' and `:: delete', which also signal the
4969      beginning of a new-expression, or delete-expression,
4970      respectively.  If the next token is `::', then it might be one of
4971      these.  */
4972   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4973     {
4974       enum rid keyword;
4975
4976       /* See if the token after the `::' is one of the keywords in
4977          which we're interested.  */
4978       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4979       /* If it's `new', we have a new-expression.  */
4980       if (keyword == RID_NEW)
4981         return cp_parser_new_expression (parser);
4982       /* Similarly, for `delete'.  */
4983       else if (keyword == RID_DELETE)
4984         return cp_parser_delete_expression (parser);
4985     }
4986
4987   /* Look for a unary operator.  */
4988   unary_operator = cp_parser_unary_operator (token);
4989   /* The `++' and `--' operators can be handled similarly, even though
4990      they are not technically unary-operators in the grammar.  */
4991   if (unary_operator == ERROR_MARK)
4992     {
4993       if (token->type == CPP_PLUS_PLUS)
4994         unary_operator = PREINCREMENT_EXPR;
4995       else if (token->type == CPP_MINUS_MINUS)
4996         unary_operator = PREDECREMENT_EXPR;
4997       /* Handle the GNU address-of-label extension.  */
4998       else if (cp_parser_allow_gnu_extensions_p (parser)
4999                && token->type == CPP_AND_AND)
5000         {
5001           tree identifier;
5002
5003           /* Consume the '&&' token.  */
5004           cp_lexer_consume_token (parser->lexer);
5005           /* Look for the identifier.  */
5006           identifier = cp_parser_identifier (parser);
5007           /* Create an expression representing the address.  */
5008           return finish_label_address_expr (identifier);
5009         }
5010     }
5011   if (unary_operator != ERROR_MARK)
5012     {
5013       tree cast_expression;
5014       tree expression = error_mark_node;
5015       const char *non_constant_p = NULL;
5016
5017       /* Consume the operator token.  */
5018       token = cp_lexer_consume_token (parser->lexer);
5019       /* Parse the cast-expression.  */
5020       cast_expression
5021         = cp_parser_cast_expression (parser,
5022                                      unary_operator == ADDR_EXPR,
5023                                      /*cast_p=*/false);
5024       /* Now, build an appropriate representation.  */
5025       switch (unary_operator)
5026         {
5027         case INDIRECT_REF:
5028           non_constant_p = "`*'";
5029           expression = build_x_indirect_ref (cast_expression, "unary *");
5030           break;
5031
5032         case ADDR_EXPR:
5033           non_constant_p = "`&'";
5034           /* Fall through.  */
5035         case BIT_NOT_EXPR:
5036           expression = build_x_unary_op (unary_operator, cast_expression);
5037           break;
5038
5039         case PREINCREMENT_EXPR:
5040         case PREDECREMENT_EXPR:
5041           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5042                             ? "`++'" : "`--'");
5043           /* Fall through.  */
5044         case UNARY_PLUS_EXPR:
5045         case NEGATE_EXPR:
5046         case TRUTH_NOT_EXPR:
5047           expression = finish_unary_op_expr (unary_operator, cast_expression);
5048           break;
5049
5050         default:
5051           gcc_unreachable ();
5052         }
5053
5054       if (non_constant_p
5055           && cp_parser_non_integral_constant_expression (parser,
5056                                                          non_constant_p))
5057         expression = error_mark_node;
5058
5059       return expression;
5060     }
5061
5062   return cp_parser_postfix_expression (parser, address_p, cast_p);
5063 }
5064
5065 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5066    unary-operator, the corresponding tree code is returned.  */
5067
5068 static enum tree_code
5069 cp_parser_unary_operator (cp_token* token)
5070 {
5071   switch (token->type)
5072     {
5073     case CPP_MULT:
5074       return INDIRECT_REF;
5075
5076     case CPP_AND:
5077       return ADDR_EXPR;
5078
5079     case CPP_PLUS:
5080       return UNARY_PLUS_EXPR;
5081
5082     case CPP_MINUS:
5083       return NEGATE_EXPR;
5084
5085     case CPP_NOT:
5086       return TRUTH_NOT_EXPR;
5087
5088     case CPP_COMPL:
5089       return BIT_NOT_EXPR;
5090
5091     default:
5092       return ERROR_MARK;
5093     }
5094 }
5095
5096 /* Parse a new-expression.
5097
5098    new-expression:
5099      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5100      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5101
5102    Returns a representation of the expression.  */
5103
5104 static tree
5105 cp_parser_new_expression (cp_parser* parser)
5106 {
5107   bool global_scope_p;
5108   tree placement;
5109   tree type;
5110   tree initializer;
5111   tree nelts;
5112
5113   /* Look for the optional `::' operator.  */
5114   global_scope_p
5115     = (cp_parser_global_scope_opt (parser,
5116                                    /*current_scope_valid_p=*/false,
5117                                    /*object_scope_valid_p=*/false)
5118        != NULL_TREE);
5119   /* Look for the `new' operator.  */
5120   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5121   /* There's no easy way to tell a new-placement from the
5122      `( type-id )' construct.  */
5123   cp_parser_parse_tentatively (parser);
5124   /* Look for a new-placement.  */
5125   placement = cp_parser_new_placement (parser);
5126   /* If that didn't work out, there's no new-placement.  */
5127   if (!cp_parser_parse_definitely (parser))
5128     placement = NULL_TREE;
5129
5130   /* If the next token is a `(', then we have a parenthesized
5131      type-id.  */
5132   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5133     {
5134       /* Consume the `('.  */
5135       cp_lexer_consume_token (parser->lexer);
5136       /* Parse the type-id.  */
5137       type = cp_parser_type_id (parser);
5138       /* Look for the closing `)'.  */
5139       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5140       /* There should not be a direct-new-declarator in this production,
5141          but GCC used to allowed this, so we check and emit a sensible error
5142          message for this case.  */
5143       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5144         {
5145           error ("array bound forbidden after parenthesized type-id");
5146           inform ("try removing the parentheses around the type-id");
5147           cp_parser_direct_new_declarator (parser);
5148         }
5149       nelts = NULL_TREE;
5150     }
5151   /* Otherwise, there must be a new-type-id.  */
5152   else
5153     type = cp_parser_new_type_id (parser, &nelts);
5154
5155   /* If the next token is a `(', then we have a new-initializer.  */
5156   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5157     initializer = cp_parser_new_initializer (parser);
5158   else
5159     initializer = NULL_TREE;
5160
5161   /* A new-expression may not appear in an integral constant
5162      expression.  */
5163   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5164     return error_mark_node;
5165
5166   /* Create a representation of the new-expression.  */
5167   return build_new (placement, type, nelts, initializer, global_scope_p);
5168 }
5169
5170 /* Parse a new-placement.
5171
5172    new-placement:
5173      ( expression-list )
5174
5175    Returns the same representation as for an expression-list.  */
5176
5177 static tree
5178 cp_parser_new_placement (cp_parser* parser)
5179 {
5180   tree expression_list;
5181
5182   /* Parse the expression-list.  */
5183   expression_list = (cp_parser_parenthesized_expression_list
5184                      (parser, false, /*cast_p=*/false,
5185                       /*non_constant_p=*/NULL));
5186
5187   return expression_list;
5188 }
5189
5190 /* Parse a new-type-id.
5191
5192    new-type-id:
5193      type-specifier-seq new-declarator [opt]
5194
5195    Returns the TYPE allocated.  If the new-type-id indicates an array
5196    type, *NELTS is set to the number of elements in the last array
5197    bound; the TYPE will not include the last array bound.  */
5198
5199 static tree
5200 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5201 {
5202   cp_decl_specifier_seq type_specifier_seq;
5203   cp_declarator *new_declarator;
5204   cp_declarator *declarator;
5205   cp_declarator *outer_declarator;
5206   const char *saved_message;
5207   tree type;
5208
5209   /* The type-specifier sequence must not contain type definitions.
5210      (It cannot contain declarations of new types either, but if they
5211      are not definitions we will catch that because they are not
5212      complete.)  */
5213   saved_message = parser->type_definition_forbidden_message;
5214   parser->type_definition_forbidden_message
5215     = "types may not be defined in a new-type-id";
5216   /* Parse the type-specifier-seq.  */
5217   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5218                                 &type_specifier_seq);
5219   /* Restore the old message.  */
5220   parser->type_definition_forbidden_message = saved_message;
5221   /* Parse the new-declarator.  */
5222   new_declarator = cp_parser_new_declarator_opt (parser);
5223
5224   /* Determine the number of elements in the last array dimension, if
5225      any.  */
5226   *nelts = NULL_TREE;
5227   /* Skip down to the last array dimension.  */
5228   declarator = new_declarator;
5229   outer_declarator = NULL;
5230   while (declarator && (declarator->kind == cdk_pointer
5231                         || declarator->kind == cdk_ptrmem))
5232     {
5233       outer_declarator = declarator;
5234       declarator = declarator->declarator;
5235     }
5236   while (declarator
5237          && declarator->kind == cdk_array
5238          && declarator->declarator
5239          && declarator->declarator->kind == cdk_array)
5240     {
5241       outer_declarator = declarator;
5242       declarator = declarator->declarator;
5243     }
5244
5245   if (declarator && declarator->kind == cdk_array)
5246     {
5247       *nelts = declarator->u.array.bounds;
5248       if (*nelts == error_mark_node)
5249         *nelts = integer_one_node;
5250
5251       if (outer_declarator)
5252         outer_declarator->declarator = declarator->declarator;
5253       else
5254         new_declarator = NULL;
5255     }
5256
5257   type = groktypename (&type_specifier_seq, new_declarator);
5258   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5259     {
5260       *nelts = array_type_nelts_top (type);
5261       type = TREE_TYPE (type);
5262     }
5263   return type;
5264 }
5265
5266 /* Parse an (optional) new-declarator.
5267
5268    new-declarator:
5269      ptr-operator new-declarator [opt]
5270      direct-new-declarator
5271
5272    Returns the declarator.  */
5273
5274 static cp_declarator *
5275 cp_parser_new_declarator_opt (cp_parser* parser)
5276 {
5277   enum tree_code code;
5278   tree type;
5279   cp_cv_quals cv_quals;
5280
5281   /* We don't know if there's a ptr-operator next, or not.  */
5282   cp_parser_parse_tentatively (parser);
5283   /* Look for a ptr-operator.  */
5284   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5285   /* If that worked, look for more new-declarators.  */
5286   if (cp_parser_parse_definitely (parser))
5287     {
5288       cp_declarator *declarator;
5289
5290       /* Parse another optional declarator.  */
5291       declarator = cp_parser_new_declarator_opt (parser);
5292
5293       /* Create the representation of the declarator.  */
5294       if (type)
5295         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5296       else if (code == INDIRECT_REF)
5297         declarator = make_pointer_declarator (cv_quals, declarator);
5298       else
5299         declarator = make_reference_declarator (cv_quals, declarator);
5300
5301       return declarator;
5302     }
5303
5304   /* If the next token is a `[', there is a direct-new-declarator.  */
5305   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5306     return cp_parser_direct_new_declarator (parser);
5307
5308   return NULL;
5309 }
5310
5311 /* Parse a direct-new-declarator.
5312
5313    direct-new-declarator:
5314      [ expression ]
5315      direct-new-declarator [constant-expression]
5316
5317    */
5318
5319 static cp_declarator *
5320 cp_parser_direct_new_declarator (cp_parser* parser)
5321 {
5322   cp_declarator *declarator = NULL;
5323
5324   while (true)
5325     {
5326       tree expression;
5327
5328       /* Look for the opening `['.  */
5329       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5330       /* The first expression is not required to be constant.  */
5331       if (!declarator)
5332         {
5333           expression = cp_parser_expression (parser, /*cast_p=*/false);
5334           /* The standard requires that the expression have integral
5335              type.  DR 74 adds enumeration types.  We believe that the
5336              real intent is that these expressions be handled like the
5337              expression in a `switch' condition, which also allows
5338              classes with a single conversion to integral or
5339              enumeration type.  */
5340           if (!processing_template_decl)
5341             {
5342               expression
5343                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5344                                               expression,
5345                                               /*complain=*/true);
5346               if (!expression)
5347                 {
5348                   error ("expression in new-declarator must have integral "
5349                          "or enumeration type");
5350                   expression = error_mark_node;
5351                 }
5352             }
5353         }
5354       /* But all the other expressions must be.  */
5355       else
5356         expression
5357           = cp_parser_constant_expression (parser,
5358                                            /*allow_non_constant=*/false,
5359                                            NULL);
5360       /* Look for the closing `]'.  */
5361       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5362
5363       /* Add this bound to the declarator.  */
5364       declarator = make_array_declarator (declarator, expression);
5365
5366       /* If the next token is not a `[', then there are no more
5367          bounds.  */
5368       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5369         break;
5370     }
5371
5372   return declarator;
5373 }
5374
5375 /* Parse a new-initializer.
5376
5377    new-initializer:
5378      ( expression-list [opt] )
5379
5380    Returns a representation of the expression-list.  If there is no
5381    expression-list, VOID_ZERO_NODE is returned.  */
5382
5383 static tree
5384 cp_parser_new_initializer (cp_parser* parser)
5385 {
5386   tree expression_list;
5387
5388   expression_list = (cp_parser_parenthesized_expression_list
5389                      (parser, false, /*cast_p=*/false,
5390                       /*non_constant_p=*/NULL));
5391   if (!expression_list)
5392     expression_list = void_zero_node;
5393
5394   return expression_list;
5395 }
5396
5397 /* Parse a delete-expression.
5398
5399    delete-expression:
5400      :: [opt] delete cast-expression
5401      :: [opt] delete [ ] cast-expression
5402
5403    Returns a representation of the expression.  */
5404
5405 static tree
5406 cp_parser_delete_expression (cp_parser* parser)
5407 {
5408   bool global_scope_p;
5409   bool array_p;
5410   tree expression;
5411
5412   /* Look for the optional `::' operator.  */
5413   global_scope_p
5414     = (cp_parser_global_scope_opt (parser,
5415                                    /*current_scope_valid_p=*/false,
5416                                    /*object_scope_valid_p=*/false)
5417        != NULL_TREE);
5418   /* Look for the `delete' keyword.  */
5419   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5420   /* See if the array syntax is in use.  */
5421   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5422     {
5423       /* Consume the `[' token.  */
5424       cp_lexer_consume_token (parser->lexer);
5425       /* Look for the `]' token.  */
5426       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5427       /* Remember that this is the `[]' construct.  */
5428       array_p = true;
5429     }
5430   else
5431     array_p = false;
5432
5433   /* Parse the cast-expression.  */
5434   expression = cp_parser_simple_cast_expression (parser);
5435
5436   /* A delete-expression may not appear in an integral constant
5437      expression.  */
5438   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5439     return error_mark_node;
5440
5441   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5442 }
5443
5444 /* Parse a cast-expression.
5445
5446    cast-expression:
5447      unary-expression
5448      ( type-id ) cast-expression
5449
5450    ADDRESS_P is true iff the unary-expression is appearing as the
5451    operand of the `&' operator.   CAST_P is true if this expression is
5452    the target of a cast.
5453
5454    Returns a representation of the expression.  */
5455
5456 static tree
5457 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5458 {
5459   /* If it's a `(', then we might be looking at a cast.  */
5460   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5461     {
5462       tree type = NULL_TREE;
5463       tree expr = NULL_TREE;
5464       bool compound_literal_p;
5465       const char *saved_message;
5466
5467       /* There's no way to know yet whether or not this is a cast.
5468          For example, `(int (3))' is a unary-expression, while `(int)
5469          3' is a cast.  So, we resort to parsing tentatively.  */
5470       cp_parser_parse_tentatively (parser);
5471       /* Types may not be defined in a cast.  */
5472       saved_message = parser->type_definition_forbidden_message;
5473       parser->type_definition_forbidden_message
5474         = "types may not be defined in casts";
5475       /* Consume the `('.  */
5476       cp_lexer_consume_token (parser->lexer);
5477       /* A very tricky bit is that `(struct S) { 3 }' is a
5478          compound-literal (which we permit in C++ as an extension).
5479          But, that construct is not a cast-expression -- it is a
5480          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5481          is legal; if the compound-literal were a cast-expression,
5482          you'd need an extra set of parentheses.)  But, if we parse
5483          the type-id, and it happens to be a class-specifier, then we
5484          will commit to the parse at that point, because we cannot
5485          undo the action that is done when creating a new class.  So,
5486          then we cannot back up and do a postfix-expression.
5487
5488          Therefore, we scan ahead to the closing `)', and check to see
5489          if the token after the `)' is a `{'.  If so, we are not
5490          looking at a cast-expression.
5491
5492          Save tokens so that we can put them back.  */
5493       cp_lexer_save_tokens (parser->lexer);
5494       /* Skip tokens until the next token is a closing parenthesis.
5495          If we find the closing `)', and the next token is a `{', then
5496          we are looking at a compound-literal.  */
5497       compound_literal_p
5498         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5499                                                   /*consume_paren=*/true)
5500            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5501       /* Roll back the tokens we skipped.  */
5502       cp_lexer_rollback_tokens (parser->lexer);
5503       /* If we were looking at a compound-literal, simulate an error
5504          so that the call to cp_parser_parse_definitely below will
5505          fail.  */
5506       if (compound_literal_p)
5507         cp_parser_simulate_error (parser);
5508       else
5509         {
5510           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5511           parser->in_type_id_in_expr_p = true;
5512           /* Look for the type-id.  */
5513           type = cp_parser_type_id (parser);
5514           /* Look for the closing `)'.  */
5515           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5516           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5517         }
5518
5519       /* Restore the saved message.  */
5520       parser->type_definition_forbidden_message = saved_message;
5521
5522       /* If ok so far, parse the dependent expression. We cannot be
5523          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5524          ctor of T, but looks like a cast to function returning T
5525          without a dependent expression.  */
5526       if (!cp_parser_error_occurred (parser))
5527         expr = cp_parser_cast_expression (parser,
5528                                           /*address_p=*/false,
5529                                           /*cast_p=*/true);
5530
5531       if (cp_parser_parse_definitely (parser))
5532         {
5533           /* Warn about old-style casts, if so requested.  */
5534           if (warn_old_style_cast
5535               && !in_system_header
5536               && !VOID_TYPE_P (type)
5537               && current_lang_name != lang_name_c)
5538             warning (OPT_Wold_style_cast, "use of old-style cast");
5539
5540           /* Only type conversions to integral or enumeration types
5541              can be used in constant-expressions.  */
5542           if (!cast_valid_in_integral_constant_expression_p (type)
5543               && (cp_parser_non_integral_constant_expression
5544                   (parser,
5545                    "a cast to a type other than an integral or "
5546                    "enumeration type")))
5547             return error_mark_node;
5548
5549           /* Perform the cast.  */
5550           expr = build_c_cast (type, expr);
5551           return expr;
5552         }
5553     }
5554
5555   /* If we get here, then it's not a cast, so it must be a
5556      unary-expression.  */
5557   return cp_parser_unary_expression (parser, address_p, cast_p);
5558 }
5559
5560 /* Parse a binary expression of the general form:
5561
5562    pm-expression:
5563      cast-expression
5564      pm-expression .* cast-expression
5565      pm-expression ->* cast-expression
5566
5567    multiplicative-expression:
5568      pm-expression
5569      multiplicative-expression * pm-expression
5570      multiplicative-expression / pm-expression
5571      multiplicative-expression % pm-expression
5572
5573    additive-expression:
5574      multiplicative-expression
5575      additive-expression + multiplicative-expression
5576      additive-expression - multiplicative-expression
5577
5578    shift-expression:
5579      additive-expression
5580      shift-expression << additive-expression
5581      shift-expression >> additive-expression
5582
5583    relational-expression:
5584      shift-expression
5585      relational-expression < shift-expression
5586      relational-expression > shift-expression
5587      relational-expression <= shift-expression
5588      relational-expression >= shift-expression
5589
5590   GNU Extension:
5591
5592    relational-expression:
5593      relational-expression <? shift-expression
5594      relational-expression >? shift-expression
5595
5596    equality-expression:
5597      relational-expression
5598      equality-expression == relational-expression
5599      equality-expression != relational-expression
5600
5601    and-expression:
5602      equality-expression
5603      and-expression & equality-expression
5604
5605    exclusive-or-expression:
5606      and-expression
5607      exclusive-or-expression ^ and-expression
5608
5609    inclusive-or-expression:
5610      exclusive-or-expression
5611      inclusive-or-expression | exclusive-or-expression
5612
5613    logical-and-expression:
5614      inclusive-or-expression
5615      logical-and-expression && inclusive-or-expression
5616
5617    logical-or-expression:
5618      logical-and-expression
5619      logical-or-expression || logical-and-expression
5620
5621    All these are implemented with a single function like:
5622
5623    binary-expression:
5624      simple-cast-expression
5625      binary-expression <token> binary-expression
5626
5627    CAST_P is true if this expression is the target of a cast.
5628
5629    The binops_by_token map is used to get the tree codes for each <token> type.
5630    binary-expressions are associated according to a precedence table.  */
5631
5632 #define TOKEN_PRECEDENCE(token) \
5633   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5634    ? PREC_NOT_OPERATOR \
5635    : binops_by_token[token->type].prec)
5636
5637 static tree
5638 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5639 {
5640   cp_parser_expression_stack stack;
5641   cp_parser_expression_stack_entry *sp = &stack[0];
5642   tree lhs, rhs;
5643   cp_token *token;
5644   enum tree_code tree_type;
5645   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5646   bool overloaded_p;
5647
5648   /* Parse the first expression.  */
5649   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5650
5651   for (;;)
5652     {
5653       /* Get an operator token.  */
5654       token = cp_lexer_peek_token (parser->lexer);
5655
5656       new_prec = TOKEN_PRECEDENCE (token);
5657
5658       /* Popping an entry off the stack means we completed a subexpression:
5659          - either we found a token which is not an operator (`>' where it is not
5660            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5661            will happen repeatedly;
5662          - or, we found an operator which has lower priority.  This is the case
5663            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5664            parsing `3 * 4'.  */
5665       if (new_prec <= prec)
5666         {
5667           if (sp == stack)
5668             break;
5669           else
5670             goto pop;
5671         }
5672
5673      get_rhs:
5674       tree_type = binops_by_token[token->type].tree_type;
5675
5676       /* We used the operator token.  */
5677       cp_lexer_consume_token (parser->lexer);
5678
5679       /* Extract another operand.  It may be the RHS of this expression
5680          or the LHS of a new, higher priority expression.  */
5681       rhs = cp_parser_simple_cast_expression (parser);
5682
5683       /* Get another operator token.  Look up its precedence to avoid
5684          building a useless (immediately popped) stack entry for common
5685          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5686       token = cp_lexer_peek_token (parser->lexer);
5687       lookahead_prec = TOKEN_PRECEDENCE (token);
5688       if (lookahead_prec > new_prec)
5689         {
5690           /* ... and prepare to parse the RHS of the new, higher priority
5691              expression.  Since precedence levels on the stack are
5692              monotonically increasing, we do not have to care about
5693              stack overflows.  */
5694           sp->prec = prec;
5695           sp->tree_type = tree_type;
5696           sp->lhs = lhs;
5697           sp++;
5698           lhs = rhs;
5699           prec = new_prec;
5700           new_prec = lookahead_prec;
5701           goto get_rhs;
5702
5703          pop:
5704           /* If the stack is not empty, we have parsed into LHS the right side
5705              (`4' in the example above) of an expression we had suspended.
5706              We can use the information on the stack to recover the LHS (`3')
5707              from the stack together with the tree code (`MULT_EXPR'), and
5708              the precedence of the higher level subexpression
5709              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5710              which will be used to actually build the additive expression.  */
5711           --sp;
5712           prec = sp->prec;
5713           tree_type = sp->tree_type;
5714           rhs = lhs;
5715           lhs = sp->lhs;
5716         }
5717
5718       overloaded_p = false;
5719       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5720
5721       /* If the binary operator required the use of an overloaded operator,
5722          then this expression cannot be an integral constant-expression.
5723          An overloaded operator can be used even if both operands are
5724          otherwise permissible in an integral constant-expression if at
5725          least one of the operands is of enumeration type.  */
5726
5727       if (overloaded_p
5728           && (cp_parser_non_integral_constant_expression
5729               (parser, "calls to overloaded operators")))
5730         return error_mark_node;
5731     }
5732
5733   return lhs;
5734 }
5735
5736
5737 /* Parse the `? expression : assignment-expression' part of a
5738    conditional-expression.  The LOGICAL_OR_EXPR is the
5739    logical-or-expression that started the conditional-expression.
5740    Returns a representation of the entire conditional-expression.
5741
5742    This routine is used by cp_parser_assignment_expression.
5743
5744      ? expression : assignment-expression
5745
5746    GNU Extensions:
5747
5748      ? : assignment-expression */
5749
5750 static tree
5751 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5752 {
5753   tree expr;
5754   tree assignment_expr;
5755
5756   /* Consume the `?' token.  */
5757   cp_lexer_consume_token (parser->lexer);
5758   if (cp_parser_allow_gnu_extensions_p (parser)
5759       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5760     /* Implicit true clause.  */
5761     expr = NULL_TREE;
5762   else
5763     /* Parse the expression.  */
5764     expr = cp_parser_expression (parser, /*cast_p=*/false);
5765
5766   /* The next token should be a `:'.  */
5767   cp_parser_require (parser, CPP_COLON, "`:'");
5768   /* Parse the assignment-expression.  */
5769   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5770
5771   /* Build the conditional-expression.  */
5772   return build_x_conditional_expr (logical_or_expr,
5773                                    expr,
5774                                    assignment_expr);
5775 }
5776
5777 /* Parse an assignment-expression.
5778
5779    assignment-expression:
5780      conditional-expression
5781      logical-or-expression assignment-operator assignment_expression
5782      throw-expression
5783
5784    CAST_P is true if this expression is the target of a cast.
5785
5786    Returns a representation for the expression.  */
5787
5788 static tree
5789 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5790 {
5791   tree expr;
5792
5793   /* If the next token is the `throw' keyword, then we're looking at
5794      a throw-expression.  */
5795   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5796     expr = cp_parser_throw_expression (parser);
5797   /* Otherwise, it must be that we are looking at a
5798      logical-or-expression.  */
5799   else
5800     {
5801       /* Parse the binary expressions (logical-or-expression).  */
5802       expr = cp_parser_binary_expression (parser, cast_p);
5803       /* If the next token is a `?' then we're actually looking at a
5804          conditional-expression.  */
5805       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5806         return cp_parser_question_colon_clause (parser, expr);
5807       else
5808         {
5809           enum tree_code assignment_operator;
5810
5811           /* If it's an assignment-operator, we're using the second
5812              production.  */
5813           assignment_operator
5814             = cp_parser_assignment_operator_opt (parser);
5815           if (assignment_operator != ERROR_MARK)
5816             {
5817               tree rhs;
5818
5819               /* Parse the right-hand side of the assignment.  */
5820               rhs = cp_parser_assignment_expression (parser, cast_p);
5821               /* An assignment may not appear in a
5822                  constant-expression.  */
5823               if (cp_parser_non_integral_constant_expression (parser,
5824                                                               "an assignment"))
5825                 return error_mark_node;
5826               /* Build the assignment expression.  */
5827               expr = build_x_modify_expr (expr,
5828                                           assignment_operator,
5829                                           rhs);
5830             }
5831         }
5832     }
5833
5834   return expr;
5835 }
5836
5837 /* Parse an (optional) assignment-operator.
5838
5839    assignment-operator: one of
5840      = *= /= %= += -= >>= <<= &= ^= |=
5841
5842    GNU Extension:
5843
5844    assignment-operator: one of
5845      <?= >?=
5846
5847    If the next token is an assignment operator, the corresponding tree
5848    code is returned, and the token is consumed.  For example, for
5849    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5850    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5851    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5852    operator, ERROR_MARK is returned.  */
5853
5854 static enum tree_code
5855 cp_parser_assignment_operator_opt (cp_parser* parser)
5856 {
5857   enum tree_code op;
5858   cp_token *token;
5859
5860   /* Peek at the next toen.  */
5861   token = cp_lexer_peek_token (parser->lexer);
5862
5863   switch (token->type)
5864     {
5865     case CPP_EQ:
5866       op = NOP_EXPR;
5867       break;
5868
5869     case CPP_MULT_EQ:
5870       op = MULT_EXPR;
5871       break;
5872
5873     case CPP_DIV_EQ:
5874       op = TRUNC_DIV_EXPR;
5875       break;
5876
5877     case CPP_MOD_EQ:
5878       op = TRUNC_MOD_EXPR;
5879       break;
5880
5881     case CPP_PLUS_EQ:
5882       op = PLUS_EXPR;
5883       break;
5884
5885     case CPP_MINUS_EQ:
5886       op = MINUS_EXPR;
5887       break;
5888
5889     case CPP_RSHIFT_EQ:
5890       op = RSHIFT_EXPR;
5891       break;
5892
5893     case CPP_LSHIFT_EQ:
5894       op = LSHIFT_EXPR;
5895       break;
5896
5897     case CPP_AND_EQ:
5898       op = BIT_AND_EXPR;
5899       break;
5900
5901     case CPP_XOR_EQ:
5902       op = BIT_XOR_EXPR;
5903       break;
5904
5905     case CPP_OR_EQ:
5906       op = BIT_IOR_EXPR;
5907       break;
5908
5909     default:
5910       /* Nothing else is an assignment operator.  */
5911       op = ERROR_MARK;
5912     }
5913
5914   /* If it was an assignment operator, consume it.  */
5915   if (op != ERROR_MARK)
5916     cp_lexer_consume_token (parser->lexer);
5917
5918   return op;
5919 }
5920
5921 /* Parse an expression.
5922
5923    expression:
5924      assignment-expression
5925      expression , assignment-expression
5926
5927    CAST_P is true if this expression is the target of a cast.
5928
5929    Returns a representation of the expression.  */
5930
5931 static tree
5932 cp_parser_expression (cp_parser* parser, bool cast_p)
5933 {
5934   tree expression = NULL_TREE;
5935
5936   while (true)
5937     {
5938       tree assignment_expression;
5939
5940       /* Parse the next assignment-expression.  */
5941       assignment_expression
5942         = cp_parser_assignment_expression (parser, cast_p);
5943       /* If this is the first assignment-expression, we can just
5944          save it away.  */
5945       if (!expression)
5946         expression = assignment_expression;
5947       else
5948         expression = build_x_compound_expr (expression,
5949                                             assignment_expression);
5950       /* If the next token is not a comma, then we are done with the
5951          expression.  */
5952       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5953         break;
5954       /* Consume the `,'.  */
5955       cp_lexer_consume_token (parser->lexer);
5956       /* A comma operator cannot appear in a constant-expression.  */
5957       if (cp_parser_non_integral_constant_expression (parser,
5958                                                       "a comma operator"))
5959         expression = error_mark_node;
5960     }
5961
5962   return expression;
5963 }
5964
5965 /* Parse a constant-expression.
5966
5967    constant-expression:
5968      conditional-expression
5969
5970   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5971   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5972   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5973   is false, NON_CONSTANT_P should be NULL.  */
5974
5975 static tree
5976 cp_parser_constant_expression (cp_parser* parser,
5977                                bool allow_non_constant_p,
5978                                bool *non_constant_p)
5979 {
5980   bool saved_integral_constant_expression_p;
5981   bool saved_allow_non_integral_constant_expression_p;
5982   bool saved_non_integral_constant_expression_p;
5983   tree expression;
5984
5985   /* It might seem that we could simply parse the
5986      conditional-expression, and then check to see if it were
5987      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5988      one that the compiler can figure out is constant, possibly after
5989      doing some simplifications or optimizations.  The standard has a
5990      precise definition of constant-expression, and we must honor
5991      that, even though it is somewhat more restrictive.
5992
5993      For example:
5994
5995        int i[(2, 3)];
5996
5997      is not a legal declaration, because `(2, 3)' is not a
5998      constant-expression.  The `,' operator is forbidden in a
5999      constant-expression.  However, GCC's constant-folding machinery
6000      will fold this operation to an INTEGER_CST for `3'.  */
6001
6002   /* Save the old settings.  */
6003   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6004   saved_allow_non_integral_constant_expression_p
6005     = parser->allow_non_integral_constant_expression_p;
6006   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6007   /* We are now parsing a constant-expression.  */
6008   parser->integral_constant_expression_p = true;
6009   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6010   parser->non_integral_constant_expression_p = false;
6011   /* Although the grammar says "conditional-expression", we parse an
6012      "assignment-expression", which also permits "throw-expression"
6013      and the use of assignment operators.  In the case that
6014      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6015      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6016      actually essential that we look for an assignment-expression.
6017      For example, cp_parser_initializer_clauses uses this function to
6018      determine whether a particular assignment-expression is in fact
6019      constant.  */
6020   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6021   /* Restore the old settings.  */
6022   parser->integral_constant_expression_p
6023     = saved_integral_constant_expression_p;
6024   parser->allow_non_integral_constant_expression_p
6025     = saved_allow_non_integral_constant_expression_p;
6026   if (allow_non_constant_p)
6027     *non_constant_p = parser->non_integral_constant_expression_p;
6028   else if (parser->non_integral_constant_expression_p)
6029     expression = error_mark_node;
6030   parser->non_integral_constant_expression_p
6031     = saved_non_integral_constant_expression_p;
6032
6033   return expression;
6034 }
6035
6036 /* Parse __builtin_offsetof.
6037
6038    offsetof-expression:
6039      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6040
6041    offsetof-member-designator:
6042      id-expression
6043      | offsetof-member-designator "." id-expression
6044      | offsetof-member-designator "[" expression "]"  */
6045
6046 static tree
6047 cp_parser_builtin_offsetof (cp_parser *parser)
6048 {
6049   int save_ice_p, save_non_ice_p;
6050   tree type, expr;
6051   cp_id_kind dummy;
6052
6053   /* We're about to accept non-integral-constant things, but will
6054      definitely yield an integral constant expression.  Save and
6055      restore these values around our local parsing.  */
6056   save_ice_p = parser->integral_constant_expression_p;
6057   save_non_ice_p = parser->non_integral_constant_expression_p;
6058
6059   /* Consume the "__builtin_offsetof" token.  */
6060   cp_lexer_consume_token (parser->lexer);
6061   /* Consume the opening `('.  */
6062   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6063   /* Parse the type-id.  */
6064   type = cp_parser_type_id (parser);
6065   /* Look for the `,'.  */
6066   cp_parser_require (parser, CPP_COMMA, "`,'");
6067
6068   /* Build the (type *)null that begins the traditional offsetof macro.  */
6069   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6070
6071   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6072   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6073                                                  true, &dummy);
6074   while (true)
6075     {
6076       cp_token *token = cp_lexer_peek_token (parser->lexer);
6077       switch (token->type)
6078         {
6079         case CPP_OPEN_SQUARE:
6080           /* offsetof-member-designator "[" expression "]" */
6081           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6082           break;
6083
6084         case CPP_DOT:
6085           /* offsetof-member-designator "." identifier */
6086           cp_lexer_consume_token (parser->lexer);
6087           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6088                                                          true, &dummy);
6089           break;
6090
6091         case CPP_CLOSE_PAREN:
6092           /* Consume the ")" token.  */
6093           cp_lexer_consume_token (parser->lexer);
6094           goto success;
6095
6096         default:
6097           /* Error.  We know the following require will fail, but
6098              that gives the proper error message.  */
6099           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6100           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6101           expr = error_mark_node;
6102           goto failure;
6103         }
6104     }
6105
6106  success:
6107   /* If we're processing a template, we can't finish the semantics yet.
6108      Otherwise we can fold the entire expression now.  */
6109   if (processing_template_decl)
6110     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6111   else
6112     expr = finish_offsetof (expr);
6113
6114  failure:
6115   parser->integral_constant_expression_p = save_ice_p;
6116   parser->non_integral_constant_expression_p = save_non_ice_p;
6117
6118   return expr;
6119 }
6120
6121 /* Statements [gram.stmt.stmt]  */
6122
6123 /* Parse a statement.
6124
6125    statement:
6126      labeled-statement
6127      expression-statement
6128      compound-statement
6129      selection-statement
6130      iteration-statement
6131      jump-statement
6132      declaration-statement
6133      try-block
6134
6135   IN_COMPOUND is true when the statement is nested inside a
6136   cp_parser_compound_statement; this matters for certain pragmas.  */
6137
6138 static void
6139 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6140                      bool in_compound)
6141 {
6142   tree statement;
6143   cp_token *token;
6144   location_t statement_location;
6145
6146  restart:
6147   /* There is no statement yet.  */
6148   statement = NULL_TREE;
6149   /* Peek at the next token.  */
6150   token = cp_lexer_peek_token (parser->lexer);
6151   /* Remember the location of the first token in the statement.  */
6152   statement_location = token->location;
6153   /* If this is a keyword, then that will often determine what kind of
6154      statement we have.  */
6155   if (token->type == CPP_KEYWORD)
6156     {
6157       enum rid keyword = token->keyword;
6158
6159       switch (keyword)
6160         {
6161         case RID_CASE:
6162         case RID_DEFAULT:
6163           statement = cp_parser_labeled_statement (parser, in_statement_expr,
6164                                                    in_compound);
6165           break;
6166
6167         case RID_IF:
6168         case RID_SWITCH:
6169           statement = cp_parser_selection_statement (parser);
6170           break;
6171
6172         case RID_WHILE:
6173         case RID_DO:
6174         case RID_FOR:
6175           statement = cp_parser_iteration_statement (parser);
6176           break;
6177
6178         case RID_BREAK:
6179         case RID_CONTINUE:
6180         case RID_RETURN:
6181         case RID_GOTO:
6182           statement = cp_parser_jump_statement (parser);
6183           break;
6184
6185           /* Objective-C++ exception-handling constructs.  */
6186         case RID_AT_TRY:
6187         case RID_AT_CATCH:
6188         case RID_AT_FINALLY:
6189         case RID_AT_SYNCHRONIZED:
6190         case RID_AT_THROW:
6191           statement = cp_parser_objc_statement (parser);
6192           break;
6193
6194         case RID_TRY:
6195           statement = cp_parser_try_block (parser);
6196           break;
6197
6198         default:
6199           /* It might be a keyword like `int' that can start a
6200              declaration-statement.  */
6201           break;
6202         }
6203     }
6204   else if (token->type == CPP_NAME)
6205     {
6206       /* If the next token is a `:', then we are looking at a
6207          labeled-statement.  */
6208       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6209       if (token->type == CPP_COLON)
6210         statement = cp_parser_labeled_statement (parser, in_statement_expr,
6211                                                  in_compound);
6212     }
6213   /* Anything that starts with a `{' must be a compound-statement.  */
6214   else if (token->type == CPP_OPEN_BRACE)
6215     statement = cp_parser_compound_statement (parser, NULL, false);
6216   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6217      a statement all its own.  */
6218   else if (token->type == CPP_PRAGMA)
6219     {
6220       /* Only certain OpenMP pragmas are attached to statements, and thus
6221          are considered statements themselves.  All others are not.  In
6222          the context of a compound, accept the pragma as a "statement" and
6223          return so that we can check for a close brace.  Otherwise we
6224          require a real statement and must go back and read one.  */
6225       if (in_compound)
6226         cp_parser_pragma (parser, pragma_compound);
6227       else if (!cp_parser_pragma (parser, pragma_stmt))
6228         goto restart;
6229       return;
6230     }
6231   else if (token->type == CPP_EOF)
6232     {
6233       cp_parser_error (parser, "expected statement");
6234       return;
6235     }
6236
6237   /* Everything else must be a declaration-statement or an
6238      expression-statement.  Try for the declaration-statement
6239      first, unless we are looking at a `;', in which case we know that
6240      we have an expression-statement.  */
6241   if (!statement)
6242     {
6243       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6244         {
6245           cp_parser_parse_tentatively (parser);
6246           /* Try to parse the declaration-statement.  */
6247           cp_parser_declaration_statement (parser);
6248           /* If that worked, we're done.  */
6249           if (cp_parser_parse_definitely (parser))
6250             return;
6251         }
6252       /* Look for an expression-statement instead.  */
6253       statement = cp_parser_expression_statement (parser, in_statement_expr);
6254     }
6255
6256   /* Set the line number for the statement.  */
6257   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6258     SET_EXPR_LOCATION (statement, statement_location);
6259 }
6260
6261 /* Parse a labeled-statement.
6262
6263    labeled-statement:
6264      identifier : statement
6265      case constant-expression : statement
6266      default : statement
6267
6268    GNU Extension:
6269
6270    labeled-statement:
6271      case constant-expression ... constant-expression : statement
6272
6273    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6274    For an ordinary label, returns a LABEL_EXPR.
6275
6276    IN_COMPOUND is as for cp_parser_statement: true when we're nested
6277    inside a compound.  */
6278
6279 static tree
6280 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6281                              bool in_compound)
6282 {
6283   cp_token *token;
6284   tree statement = error_mark_node;
6285
6286   /* The next token should be an identifier.  */
6287   token = cp_lexer_peek_token (parser->lexer);
6288   if (token->type != CPP_NAME
6289       && token->type != CPP_KEYWORD)
6290     {
6291       cp_parser_error (parser, "expected labeled-statement");
6292       return error_mark_node;
6293     }
6294
6295   switch (token->keyword)
6296     {
6297     case RID_CASE:
6298       {
6299         tree expr, expr_hi;
6300         cp_token *ellipsis;
6301
6302         /* Consume the `case' token.  */
6303         cp_lexer_consume_token (parser->lexer);
6304         /* Parse the constant-expression.  */
6305         expr = cp_parser_constant_expression (parser,
6306                                               /*allow_non_constant_p=*/false,
6307                                               NULL);
6308
6309         ellipsis = cp_lexer_peek_token (parser->lexer);
6310         if (ellipsis->type == CPP_ELLIPSIS)
6311           {
6312             /* Consume the `...' token.  */
6313             cp_lexer_consume_token (parser->lexer);
6314             expr_hi =
6315               cp_parser_constant_expression (parser,
6316                                              /*allow_non_constant_p=*/false,
6317                                              NULL);
6318             /* We don't need to emit warnings here, as the common code
6319                will do this for us.  */
6320           }
6321         else
6322           expr_hi = NULL_TREE;
6323
6324         if (parser->in_switch_statement_p)
6325           statement = finish_case_label (expr, expr_hi);
6326         else
6327           error ("case label %qE not within a switch statement", expr);
6328       }
6329       break;
6330
6331     case RID_DEFAULT:
6332       /* Consume the `default' token.  */
6333       cp_lexer_consume_token (parser->lexer);
6334
6335       if (parser->in_switch_statement_p)
6336         statement = finish_case_label (NULL_TREE, NULL_TREE);
6337       else
6338         error ("case label not within a switch statement");
6339       break;
6340
6341     default:
6342       /* Anything else must be an ordinary label.  */
6343       statement = finish_label_stmt (cp_parser_identifier (parser));
6344       break;
6345     }
6346
6347   /* Require the `:' token.  */
6348   cp_parser_require (parser, CPP_COLON, "`:'");
6349   /* Parse the labeled statement.  */
6350   cp_parser_statement (parser, in_statement_expr, in_compound);
6351
6352   /* Return the label, in the case of a `case' or `default' label.  */
6353   return statement;
6354 }
6355
6356 /* Parse an expression-statement.
6357
6358    expression-statement:
6359      expression [opt] ;
6360
6361    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6362    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6363    indicates whether this expression-statement is part of an
6364    expression statement.  */
6365
6366 static tree
6367 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6368 {
6369   tree statement = NULL_TREE;
6370
6371   /* If the next token is a ';', then there is no expression
6372      statement.  */
6373   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6374     statement = cp_parser_expression (parser, /*cast_p=*/false);
6375
6376   /* Consume the final `;'.  */
6377   cp_parser_consume_semicolon_at_end_of_statement (parser);
6378
6379   if (in_statement_expr
6380       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6381     /* This is the final expression statement of a statement
6382        expression.  */
6383     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6384   else if (statement)
6385     statement = finish_expr_stmt (statement);
6386   else
6387     finish_stmt ();
6388
6389   return statement;
6390 }
6391
6392 /* Parse a compound-statement.
6393
6394    compound-statement:
6395      { statement-seq [opt] }
6396
6397    Returns a tree representing the statement.  */
6398
6399 static tree
6400 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6401                               bool in_try)
6402 {
6403   tree compound_stmt;
6404
6405   /* Consume the `{'.  */
6406   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6407     return error_mark_node;
6408   /* Begin the compound-statement.  */
6409   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6410   /* Parse an (optional) statement-seq.  */
6411   cp_parser_statement_seq_opt (parser, in_statement_expr);
6412   /* Finish the compound-statement.  */
6413   finish_compound_stmt (compound_stmt);
6414   /* Consume the `}'.  */
6415   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6416
6417   return compound_stmt;
6418 }
6419
6420 /* Parse an (optional) statement-seq.
6421
6422    statement-seq:
6423      statement
6424      statement-seq [opt] statement  */
6425
6426 static void
6427 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6428 {
6429   /* Scan statements until there aren't any more.  */
6430   while (true)
6431     {
6432       cp_token *token = cp_lexer_peek_token (parser->lexer);
6433
6434       /* If we're looking at a `}', then we've run out of statements.  */
6435       if (token->type == CPP_CLOSE_BRACE
6436           || token->type == CPP_EOF
6437           || token->type == CPP_PRAGMA_EOL)
6438         break;
6439
6440       /* Parse the statement.  */
6441       cp_parser_statement (parser, in_statement_expr, true);
6442     }
6443 }
6444
6445 /* Parse a selection-statement.
6446
6447    selection-statement:
6448      if ( condition ) statement
6449      if ( condition ) statement else statement
6450      switch ( condition ) statement
6451
6452    Returns the new IF_STMT or SWITCH_STMT.  */
6453
6454 static tree
6455 cp_parser_selection_statement (cp_parser* parser)
6456 {
6457   cp_token *token;
6458   enum rid keyword;
6459
6460   /* Peek at the next token.  */
6461   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6462
6463   /* See what kind of keyword it is.  */
6464   keyword = token->keyword;
6465   switch (keyword)
6466     {
6467     case RID_IF:
6468     case RID_SWITCH:
6469       {
6470         tree statement;
6471         tree condition;
6472
6473         /* Look for the `('.  */
6474         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6475           {
6476             cp_parser_skip_to_end_of_statement (parser);
6477             return error_mark_node;
6478           }
6479
6480         /* Begin the selection-statement.  */
6481         if (keyword == RID_IF)
6482           statement = begin_if_stmt ();
6483         else
6484           statement = begin_switch_stmt ();
6485
6486         /* Parse the condition.  */
6487         condition = cp_parser_condition (parser);
6488         /* Look for the `)'.  */
6489         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6490           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6491                                                  /*consume_paren=*/true);
6492
6493         if (keyword == RID_IF)
6494           {
6495             /* Add the condition.  */
6496             finish_if_stmt_cond (condition, statement);
6497
6498             /* Parse the then-clause.  */
6499             cp_parser_implicitly_scoped_statement (parser);
6500             finish_then_clause (statement);
6501
6502             /* If the next token is `else', parse the else-clause.  */
6503             if (cp_lexer_next_token_is_keyword (parser->lexer,
6504                                                 RID_ELSE))
6505               {
6506                 /* Consume the `else' keyword.  */
6507                 cp_lexer_consume_token (parser->lexer);
6508                 begin_else_clause (statement);
6509                 /* Parse the else-clause.  */
6510                 cp_parser_implicitly_scoped_statement (parser);
6511                 finish_else_clause (statement);
6512               }
6513
6514             /* Now we're all done with the if-statement.  */
6515             finish_if_stmt (statement);
6516           }
6517         else
6518           {
6519             bool in_switch_statement_p;
6520             unsigned char in_statement;
6521
6522             /* Add the condition.  */
6523             finish_switch_cond (condition, statement);
6524
6525             /* Parse the body of the switch-statement.  */
6526             in_switch_statement_p = parser->in_switch_statement_p;
6527             in_statement = parser->in_statement;
6528             parser->in_switch_statement_p = true;
6529             parser->in_statement |= IN_SWITCH_STMT;
6530             cp_parser_implicitly_scoped_statement (parser);
6531             parser->in_switch_statement_p = in_switch_statement_p;
6532             parser->in_statement = in_statement;
6533
6534             /* Now we're all done with the switch-statement.  */
6535             finish_switch_stmt (statement);
6536           }
6537
6538         return statement;
6539       }
6540       break;
6541
6542     default:
6543       cp_parser_error (parser, "expected selection-statement");
6544       return error_mark_node;
6545     }
6546 }
6547
6548 /* Parse a condition.
6549
6550    condition:
6551      expression
6552      type-specifier-seq declarator = assignment-expression
6553
6554    GNU Extension:
6555
6556    condition:
6557      type-specifier-seq declarator asm-specification [opt]
6558        attributes [opt] = assignment-expression
6559
6560    Returns the expression that should be tested.  */
6561
6562 static tree
6563 cp_parser_condition (cp_parser* parser)
6564 {
6565   cp_decl_specifier_seq type_specifiers;
6566   const char *saved_message;
6567
6568   /* Try the declaration first.  */
6569   cp_parser_parse_tentatively (parser);
6570   /* New types are not allowed in the type-specifier-seq for a
6571      condition.  */
6572   saved_message = parser->type_definition_forbidden_message;
6573   parser->type_definition_forbidden_message
6574     = "types may not be defined in conditions";
6575   /* Parse the type-specifier-seq.  */
6576   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6577                                 &type_specifiers);
6578   /* Restore the saved message.  */
6579   parser->type_definition_forbidden_message = saved_message;
6580   /* If all is well, we might be looking at a declaration.  */
6581   if (!cp_parser_error_occurred (parser))
6582     {
6583       tree decl;
6584       tree asm_specification;
6585       tree attributes;
6586       cp_declarator *declarator;
6587       tree initializer = NULL_TREE;
6588
6589       /* Parse the declarator.  */
6590       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6591                                          /*ctor_dtor_or_conv_p=*/NULL,
6592                                          /*parenthesized_p=*/NULL,
6593                                          /*member_p=*/false);
6594       /* Parse the attributes.  */
6595       attributes = cp_parser_attributes_opt (parser);
6596       /* Parse the asm-specification.  */
6597       asm_specification = cp_parser_asm_specification_opt (parser);
6598       /* If the next token is not an `=', then we might still be
6599          looking at an expression.  For example:
6600
6601            if (A(a).x)
6602
6603          looks like a decl-specifier-seq and a declarator -- but then
6604          there is no `=', so this is an expression.  */
6605       cp_parser_require (parser, CPP_EQ, "`='");
6606       /* If we did see an `=', then we are looking at a declaration
6607          for sure.  */
6608       if (cp_parser_parse_definitely (parser))
6609         {
6610           tree pushed_scope;
6611           bool non_constant_p;
6612
6613           /* Create the declaration.  */
6614           decl = start_decl (declarator, &type_specifiers,
6615                              /*initialized_p=*/true,
6616                              attributes, /*prefix_attributes=*/NULL_TREE,
6617                              &pushed_scope);
6618           /* Parse the assignment-expression.  */
6619           initializer
6620             = cp_parser_constant_expression (parser,
6621                                              /*allow_non_constant_p=*/true,
6622                                              &non_constant_p);
6623           if (!non_constant_p)
6624             initializer = fold_non_dependent_expr (initializer);
6625
6626           /* Process the initializer.  */
6627           cp_finish_decl (decl,
6628                           initializer, !non_constant_p,
6629                           asm_specification,
6630                           LOOKUP_ONLYCONVERTING);
6631
6632           if (pushed_scope)
6633             pop_scope (pushed_scope);
6634
6635           return convert_from_reference (decl);
6636         }
6637     }
6638   /* If we didn't even get past the declarator successfully, we are
6639      definitely not looking at a declaration.  */
6640   else
6641     cp_parser_abort_tentative_parse (parser);
6642
6643   /* Otherwise, we are looking at an expression.  */
6644   return cp_parser_expression (parser, /*cast_p=*/false);
6645 }
6646
6647 /* Parse an iteration-statement.
6648
6649    iteration-statement:
6650      while ( condition ) statement
6651      do statement while ( expression ) ;
6652      for ( for-init-statement condition [opt] ; expression [opt] )
6653        statement
6654
6655    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6656
6657 static tree
6658 cp_parser_iteration_statement (cp_parser* parser)
6659 {
6660   cp_token *token;
6661   enum rid keyword;
6662   tree statement;
6663   unsigned char in_statement;
6664
6665   /* Peek at the next token.  */
6666   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6667   if (!token)
6668     return error_mark_node;
6669
6670   /* Remember whether or not we are already within an iteration
6671      statement.  */
6672   in_statement = parser->in_statement;
6673
6674   /* See what kind of keyword it is.  */
6675   keyword = token->keyword;
6676   switch (keyword)
6677     {
6678     case RID_WHILE:
6679       {
6680         tree condition;
6681
6682         /* Begin the while-statement.  */
6683         statement = begin_while_stmt ();
6684         /* Look for the `('.  */
6685         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6686         /* Parse the condition.  */
6687         condition = cp_parser_condition (parser);
6688         finish_while_stmt_cond (condition, statement);
6689         /* Look for the `)'.  */
6690         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6691         /* Parse the dependent statement.  */
6692         parser->in_statement = IN_ITERATION_STMT;
6693         cp_parser_already_scoped_statement (parser);
6694         parser->in_statement = in_statement;
6695         /* We're done with the while-statement.  */
6696         finish_while_stmt (statement);
6697       }
6698       break;
6699
6700     case RID_DO:
6701       {
6702         tree expression;
6703
6704         /* Begin the do-statement.  */
6705         statement = begin_do_stmt ();
6706         /* Parse the body of the do-statement.  */
6707         parser->in_statement = IN_ITERATION_STMT;
6708         cp_parser_implicitly_scoped_statement (parser);
6709         parser->in_statement = in_statement;
6710         finish_do_body (statement);
6711         /* Look for the `while' keyword.  */
6712         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6713         /* Look for the `('.  */
6714         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6715         /* Parse the expression.  */
6716         expression = cp_parser_expression (parser, /*cast_p=*/false);
6717         /* We're done with the do-statement.  */
6718         finish_do_stmt (expression, statement);
6719         /* Look for the `)'.  */
6720         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6721         /* Look for the `;'.  */
6722         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6723       }
6724       break;
6725
6726     case RID_FOR:
6727       {
6728         tree condition = NULL_TREE;
6729         tree expression = NULL_TREE;
6730
6731         /* Begin the for-statement.  */
6732         statement = begin_for_stmt ();
6733         /* Look for the `('.  */
6734         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6735         /* Parse the initialization.  */
6736         cp_parser_for_init_statement (parser);
6737         finish_for_init_stmt (statement);
6738
6739         /* If there's a condition, process it.  */
6740         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6741           condition = cp_parser_condition (parser);
6742         finish_for_cond (condition, statement);
6743         /* Look for the `;'.  */
6744         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6745
6746         /* If there's an expression, process it.  */
6747         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6748           expression = cp_parser_expression (parser, /*cast_p=*/false);
6749         finish_for_expr (expression, statement);
6750         /* Look for the `)'.  */
6751         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6752
6753         /* Parse the body of the for-statement.  */
6754         parser->in_statement = IN_ITERATION_STMT;
6755         cp_parser_already_scoped_statement (parser);
6756         parser->in_statement = in_statement;
6757
6758         /* We're done with the for-statement.  */
6759         finish_for_stmt (statement);
6760       }
6761       break;
6762
6763     default:
6764       cp_parser_error (parser, "expected iteration-statement");
6765       statement = error_mark_node;
6766       break;
6767     }
6768
6769   return statement;
6770 }
6771
6772 /* Parse a for-init-statement.
6773
6774    for-init-statement:
6775      expression-statement
6776      simple-declaration  */
6777
6778 static void
6779 cp_parser_for_init_statement (cp_parser* parser)
6780 {
6781   /* If the next token is a `;', then we have an empty
6782      expression-statement.  Grammatically, this is also a
6783      simple-declaration, but an invalid one, because it does not
6784      declare anything.  Therefore, if we did not handle this case
6785      specially, we would issue an error message about an invalid
6786      declaration.  */
6787   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6788     {
6789       /* We're going to speculatively look for a declaration, falling back
6790          to an expression, if necessary.  */
6791       cp_parser_parse_tentatively (parser);
6792       /* Parse the declaration.  */
6793       cp_parser_simple_declaration (parser,
6794                                     /*function_definition_allowed_p=*/false);
6795       /* If the tentative parse failed, then we shall need to look for an
6796          expression-statement.  */
6797       if (cp_parser_parse_definitely (parser))
6798         return;
6799     }
6800
6801   cp_parser_expression_statement (parser, false);
6802 }
6803
6804 /* Parse a jump-statement.
6805
6806    jump-statement:
6807      break ;
6808      continue ;
6809      return expression [opt] ;
6810      goto identifier ;
6811
6812    GNU extension:
6813
6814    jump-statement:
6815      goto * expression ;
6816
6817    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6818
6819 static tree
6820 cp_parser_jump_statement (cp_parser* parser)
6821 {
6822   tree statement = error_mark_node;
6823   cp_token *token;
6824   enum rid keyword;
6825
6826   /* Peek at the next token.  */
6827   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6828   if (!token)
6829     return error_mark_node;
6830
6831   /* See what kind of keyword it is.  */
6832   keyword = token->keyword;
6833   switch (keyword)
6834     {
6835     case RID_BREAK:
6836       switch (parser->in_statement)
6837         {
6838         case 0:
6839           error ("break statement not within loop or switch");
6840           break;
6841         default:
6842           gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6843                       || parser->in_statement == IN_ITERATION_STMT);
6844           statement = finish_break_stmt ();
6845           break;
6846         case IN_OMP_BLOCK:
6847           error ("invalid exit from OpenMP structured block");
6848           break;
6849         case IN_OMP_FOR:
6850           error ("break statement used with OpenMP for loop");
6851           break;
6852         }
6853       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6854       break;
6855
6856     case RID_CONTINUE:
6857       switch (parser->in_statement & ~IN_SWITCH_STMT)
6858         {
6859         case 0:
6860           error ("continue statement not within a loop");
6861           break;
6862         case IN_ITERATION_STMT:
6863         case IN_OMP_FOR:
6864           statement = finish_continue_stmt ();
6865           break;
6866         case IN_OMP_BLOCK:
6867           error ("invalid exit from OpenMP structured block");
6868           break;
6869         default:
6870           gcc_unreachable ();
6871         }
6872       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6873       break;
6874
6875     case RID_RETURN:
6876       {
6877         tree expr;
6878
6879         /* If the next token is a `;', then there is no
6880            expression.  */
6881         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6882           expr = cp_parser_expression (parser, /*cast_p=*/false);
6883         else
6884           expr = NULL_TREE;
6885         /* Build the return-statement.  */
6886         statement = finish_return_stmt (expr);
6887         /* Look for the final `;'.  */
6888         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6889       }
6890       break;
6891
6892     case RID_GOTO:
6893       /* Create the goto-statement.  */
6894       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6895         {
6896           /* Issue a warning about this use of a GNU extension.  */
6897           if (pedantic)
6898             pedwarn ("ISO C++ forbids computed gotos");
6899           /* Consume the '*' token.  */
6900           cp_lexer_consume_token (parser->lexer);
6901           /* Parse the dependent expression.  */
6902           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6903         }
6904       else
6905         finish_goto_stmt (cp_parser_identifier (parser));
6906       /* Look for the final `;'.  */
6907       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6908       break;
6909
6910     default:
6911       cp_parser_error (parser, "expected jump-statement");
6912       break;
6913     }
6914
6915   return statement;
6916 }
6917
6918 /* Parse a declaration-statement.
6919
6920    declaration-statement:
6921      block-declaration  */
6922
6923 static void
6924 cp_parser_declaration_statement (cp_parser* parser)
6925 {
6926   void *p;
6927
6928   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6929   p = obstack_alloc (&declarator_obstack, 0);
6930
6931  /* Parse the block-declaration.  */
6932   cp_parser_block_declaration (parser, /*statement_p=*/true);
6933
6934   /* Free any declarators allocated.  */
6935   obstack_free (&declarator_obstack, p);
6936
6937   /* Finish off the statement.  */
6938   finish_stmt ();
6939 }
6940
6941 /* Some dependent statements (like `if (cond) statement'), are
6942    implicitly in their own scope.  In other words, if the statement is
6943    a single statement (as opposed to a compound-statement), it is
6944    none-the-less treated as if it were enclosed in braces.  Any
6945    declarations appearing in the dependent statement are out of scope
6946    after control passes that point.  This function parses a statement,
6947    but ensures that is in its own scope, even if it is not a
6948    compound-statement.
6949
6950    Returns the new statement.  */
6951
6952 static tree
6953 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6954 {
6955   tree statement;
6956
6957   /* Mark if () ; with a special NOP_EXPR.  */
6958   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6959     {
6960       cp_lexer_consume_token (parser->lexer);
6961       statement = add_stmt (build_empty_stmt ());
6962     }
6963   /* if a compound is opened, we simply parse the statement directly.  */
6964   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6965     statement = cp_parser_compound_statement (parser, NULL, false);
6966   /* If the token is not a `{', then we must take special action.  */
6967   else
6968     {
6969       /* Create a compound-statement.  */
6970       statement = begin_compound_stmt (0);
6971       /* Parse the dependent-statement.  */
6972       cp_parser_statement (parser, NULL_TREE, false);
6973       /* Finish the dummy compound-statement.  */
6974       finish_compound_stmt (statement);
6975     }
6976
6977   /* Return the statement.  */
6978   return statement;
6979 }
6980
6981 /* For some dependent statements (like `while (cond) statement'), we
6982    have already created a scope.  Therefore, even if the dependent
6983    statement is a compound-statement, we do not want to create another
6984    scope.  */
6985
6986 static void
6987 cp_parser_already_scoped_statement (cp_parser* parser)
6988 {
6989   /* If the token is a `{', then we must take special action.  */
6990   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6991     cp_parser_statement (parser, NULL_TREE, false);
6992   else
6993     {
6994       /* Avoid calling cp_parser_compound_statement, so that we
6995          don't create a new scope.  Do everything else by hand.  */
6996       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6997       cp_parser_statement_seq_opt (parser, NULL_TREE);
6998       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6999     }
7000 }
7001
7002 /* Declarations [gram.dcl.dcl] */
7003
7004 /* Parse an optional declaration-sequence.
7005
7006    declaration-seq:
7007      declaration
7008      declaration-seq declaration  */
7009
7010 static void
7011 cp_parser_declaration_seq_opt (cp_parser* parser)
7012 {
7013   while (true)
7014     {
7015       cp_token *token;
7016
7017       token = cp_lexer_peek_token (parser->lexer);
7018
7019       if (token->type == CPP_CLOSE_BRACE
7020           || token->type == CPP_EOF
7021           || token->type == CPP_PRAGMA_EOL)
7022         break;
7023
7024       if (token->type == CPP_SEMICOLON)
7025         {
7026           /* A declaration consisting of a single semicolon is
7027              invalid.  Allow it unless we're being pedantic.  */
7028           cp_lexer_consume_token (parser->lexer);
7029           if (pedantic && !in_system_header)
7030             pedwarn ("extra %<;%>");
7031           continue;
7032         }
7033
7034       /* If we're entering or exiting a region that's implicitly
7035          extern "C", modify the lang context appropriately.  */
7036       if (!parser->implicit_extern_c && token->implicit_extern_c)
7037         {
7038           push_lang_context (lang_name_c);
7039           parser->implicit_extern_c = true;
7040         }
7041       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7042         {
7043           pop_lang_context ();
7044           parser->implicit_extern_c = false;
7045         }
7046
7047       if (token->type == CPP_PRAGMA)
7048         {
7049           /* A top-level declaration can consist solely of a #pragma.
7050              A nested declaration cannot, so this is done here and not
7051              in cp_parser_declaration.  (A #pragma at block scope is
7052              handled in cp_parser_statement.)  */
7053           cp_parser_pragma (parser, pragma_external);
7054           continue;
7055         }
7056
7057       /* Parse the declaration itself.  */
7058       cp_parser_declaration (parser);
7059     }
7060 }
7061
7062 /* Parse a declaration.
7063
7064    declaration:
7065      block-declaration
7066      function-definition
7067      template-declaration
7068      explicit-instantiation
7069      explicit-specialization
7070      linkage-specification
7071      namespace-definition
7072
7073    GNU extension:
7074
7075    declaration:
7076       __extension__ declaration */
7077
7078 static void
7079 cp_parser_declaration (cp_parser* parser)
7080 {
7081   cp_token token1;
7082   cp_token token2;
7083   int saved_pedantic;
7084   void *p;
7085
7086   /* Check for the `__extension__' keyword.  */
7087   if (cp_parser_extension_opt (parser, &saved_pedantic))
7088     {
7089       /* Parse the qualified declaration.  */
7090       cp_parser_declaration (parser);
7091       /* Restore the PEDANTIC flag.  */
7092       pedantic = saved_pedantic;
7093
7094       return;
7095     }
7096
7097   /* Try to figure out what kind of declaration is present.  */
7098   token1 = *cp_lexer_peek_token (parser->lexer);
7099
7100   if (token1.type != CPP_EOF)
7101     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7102   else
7103     {
7104       token2.type = CPP_EOF;
7105       token2.keyword = RID_MAX;
7106     }
7107
7108   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7109   p = obstack_alloc (&declarator_obstack, 0);
7110
7111   /* If the next token is `extern' and the following token is a string
7112      literal, then we have a linkage specification.  */
7113   if (token1.keyword == RID_EXTERN
7114       && cp_parser_is_string_literal (&token2))
7115     cp_parser_linkage_specification (parser);
7116   /* If the next token is `template', then we have either a template
7117      declaration, an explicit instantiation, or an explicit
7118      specialization.  */
7119   else if (token1.keyword == RID_TEMPLATE)
7120     {
7121       /* `template <>' indicates a template specialization.  */
7122       if (token2.type == CPP_LESS
7123           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7124         cp_parser_explicit_specialization (parser);
7125       /* `template <' indicates a template declaration.  */
7126       else if (token2.type == CPP_LESS)
7127         cp_parser_template_declaration (parser, /*member_p=*/false);
7128       /* Anything else must be an explicit instantiation.  */
7129       else
7130         cp_parser_explicit_instantiation (parser);
7131     }
7132   /* If the next token is `export', then we have a template
7133      declaration.  */
7134   else if (token1.keyword == RID_EXPORT)
7135     cp_parser_template_declaration (parser, /*member_p=*/false);
7136   /* If the next token is `extern', 'static' or 'inline' and the one
7137      after that is `template', we have a GNU extended explicit
7138      instantiation directive.  */
7139   else if (cp_parser_allow_gnu_extensions_p (parser)
7140            && (token1.keyword == RID_EXTERN
7141                || token1.keyword == RID_STATIC
7142                || token1.keyword == RID_INLINE)
7143            && token2.keyword == RID_TEMPLATE)
7144     cp_parser_explicit_instantiation (parser);
7145   /* If the next token is `namespace', check for a named or unnamed
7146      namespace definition.  */
7147   else if (token1.keyword == RID_NAMESPACE
7148            && (/* A named namespace definition.  */
7149                (token2.type == CPP_NAME
7150                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7151                     != CPP_EQ))
7152                /* An unnamed namespace definition.  */
7153                || token2.type == CPP_OPEN_BRACE
7154                || token2.keyword == RID_ATTRIBUTE))
7155     cp_parser_namespace_definition (parser);
7156   /* Objective-C++ declaration/definition.  */
7157   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7158     cp_parser_objc_declaration (parser);
7159   /* We must have either a block declaration or a function
7160      definition.  */
7161   else
7162     /* Try to parse a block-declaration, or a function-definition.  */
7163     cp_parser_block_declaration (parser, /*statement_p=*/false);
7164
7165   /* Free any declarators allocated.  */
7166   obstack_free (&declarator_obstack, p);
7167 }
7168
7169 /* Parse a block-declaration.
7170
7171    block-declaration:
7172      simple-declaration
7173      asm-definition
7174      namespace-alias-definition
7175      using-declaration
7176      using-directive
7177
7178    GNU Extension:
7179
7180    block-declaration:
7181      __extension__ block-declaration
7182      label-declaration
7183
7184    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7185    part of a declaration-statement.  */
7186
7187 static void
7188 cp_parser_block_declaration (cp_parser *parser,
7189                              bool      statement_p)
7190 {
7191   cp_token *token1;
7192   int saved_pedantic;
7193
7194   /* Check for the `__extension__' keyword.  */
7195   if (cp_parser_extension_opt (parser, &saved_pedantic))
7196     {
7197       /* Parse the qualified declaration.  */
7198       cp_parser_block_declaration (parser, statement_p);
7199       /* Restore the PEDANTIC flag.  */
7200       pedantic = saved_pedantic;
7201
7202       return;
7203     }
7204
7205   /* Peek at the next token to figure out which kind of declaration is
7206      present.  */
7207   token1 = cp_lexer_peek_token (parser->lexer);
7208
7209   /* If the next keyword is `asm', we have an asm-definition.  */
7210   if (token1->keyword == RID_ASM)
7211     {
7212       if (statement_p)
7213         cp_parser_commit_to_tentative_parse (parser);
7214       cp_parser_asm_definition (parser);
7215     }
7216   /* If the next keyword is `namespace', we have a
7217      namespace-alias-definition.  */
7218   else if (token1->keyword == RID_NAMESPACE)
7219     cp_parser_namespace_alias_definition (parser);
7220   /* If the next keyword is `using', we have either a
7221      using-declaration or a using-directive.  */
7222   else if (token1->keyword == RID_USING)
7223     {
7224       cp_token *token2;
7225
7226       if (statement_p)
7227         cp_parser_commit_to_tentative_parse (parser);
7228       /* If the token after `using' is `namespace', then we have a
7229          using-directive.  */
7230       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7231       if (token2->keyword == RID_NAMESPACE)
7232         cp_parser_using_directive (parser);
7233       /* Otherwise, it's a using-declaration.  */
7234       else
7235         cp_parser_using_declaration (parser);
7236     }
7237   /* If the next keyword is `__label__' we have a label declaration.  */
7238   else if (token1->keyword == RID_LABEL)
7239     {
7240       if (statement_p)
7241         cp_parser_commit_to_tentative_parse (parser);
7242       cp_parser_label_declaration (parser);
7243     }
7244   /* Anything else must be a simple-declaration.  */
7245   else
7246     cp_parser_simple_declaration (parser, !statement_p);
7247 }
7248
7249 /* Parse a simple-declaration.
7250
7251    simple-declaration:
7252      decl-specifier-seq [opt] init-declarator-list [opt] ;
7253
7254    init-declarator-list:
7255      init-declarator
7256      init-declarator-list , init-declarator
7257
7258    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7259    function-definition as a simple-declaration.  */
7260
7261 static void
7262 cp_parser_simple_declaration (cp_parser* parser,
7263                               bool function_definition_allowed_p)
7264 {
7265   cp_decl_specifier_seq decl_specifiers;
7266   int declares_class_or_enum;
7267   bool saw_declarator;
7268
7269   /* Defer access checks until we know what is being declared; the
7270      checks for names appearing in the decl-specifier-seq should be
7271      done as if we were in the scope of the thing being declared.  */
7272   push_deferring_access_checks (dk_deferred);
7273
7274   /* Parse the decl-specifier-seq.  We have to keep track of whether
7275      or not the decl-specifier-seq declares a named class or
7276      enumeration type, since that is the only case in which the
7277      init-declarator-list is allowed to be empty.
7278
7279      [dcl.dcl]
7280
7281      In a simple-declaration, the optional init-declarator-list can be
7282      omitted only when declaring a class or enumeration, that is when
7283      the decl-specifier-seq contains either a class-specifier, an
7284      elaborated-type-specifier, or an enum-specifier.  */
7285   cp_parser_decl_specifier_seq (parser,
7286                                 CP_PARSER_FLAGS_OPTIONAL,
7287                                 &decl_specifiers,
7288                                 &declares_class_or_enum);
7289   /* We no longer need to defer access checks.  */
7290   stop_deferring_access_checks ();
7291
7292   /* In a block scope, a valid declaration must always have a
7293      decl-specifier-seq.  By not trying to parse declarators, we can
7294      resolve the declaration/expression ambiguity more quickly.  */
7295   if (!function_definition_allowed_p
7296       && !decl_specifiers.any_specifiers_p)
7297     {
7298       cp_parser_error (parser, "expected declaration");
7299       goto done;
7300     }
7301
7302   /* If the next two tokens are both identifiers, the code is
7303      erroneous. The usual cause of this situation is code like:
7304
7305        T t;
7306
7307      where "T" should name a type -- but does not.  */
7308   if (!decl_specifiers.type
7309       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7310     {
7311       /* If parsing tentatively, we should commit; we really are
7312          looking at a declaration.  */
7313       cp_parser_commit_to_tentative_parse (parser);
7314       /* Give up.  */
7315       goto done;
7316     }
7317
7318   /* If we have seen at least one decl-specifier, and the next token
7319      is not a parenthesis, then we must be looking at a declaration.
7320      (After "int (" we might be looking at a functional cast.)  */
7321   if (decl_specifiers.any_specifiers_p
7322       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7323     cp_parser_commit_to_tentative_parse (parser);
7324
7325   /* Keep going until we hit the `;' at the end of the simple
7326      declaration.  */
7327   saw_declarator = false;
7328   while (cp_lexer_next_token_is_not (parser->lexer,
7329                                      CPP_SEMICOLON))
7330     {
7331       cp_token *token;
7332       bool function_definition_p;
7333       tree decl;
7334
7335       if (saw_declarator)
7336         {
7337           /* If we are processing next declarator, coma is expected */
7338           token = cp_lexer_peek_token (parser->lexer);
7339           gcc_assert (token->type == CPP_COMMA);
7340           cp_lexer_consume_token (parser->lexer);
7341         }
7342       else
7343         saw_declarator = true;
7344
7345       /* Parse the init-declarator.  */
7346       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7347                                         /*checks=*/NULL_TREE,
7348                                         function_definition_allowed_p,
7349                                         /*member_p=*/false,
7350                                         declares_class_or_enum,
7351                                         &function_definition_p);
7352       /* If an error occurred while parsing tentatively, exit quickly.
7353          (That usually happens when in the body of a function; each
7354          statement is treated as a declaration-statement until proven
7355          otherwise.)  */
7356       if (cp_parser_error_occurred (parser))
7357         goto done;
7358       /* Handle function definitions specially.  */
7359       if (function_definition_p)
7360         {
7361           /* If the next token is a `,', then we are probably
7362              processing something like:
7363
7364                void f() {}, *p;
7365
7366              which is erroneous.  */
7367           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7368             error ("mixing declarations and function-definitions is forbidden");
7369           /* Otherwise, we're done with the list of declarators.  */
7370           else
7371             {
7372               pop_deferring_access_checks ();
7373               return;
7374             }
7375         }
7376       /* The next token should be either a `,' or a `;'.  */
7377       token = cp_lexer_peek_token (parser->lexer);
7378       /* If it's a `,', there are more declarators to come.  */
7379       if (token->type == CPP_COMMA)
7380         /* will be consumed next time around */;
7381       /* If it's a `;', we are done.  */
7382       else if (token->type == CPP_SEMICOLON)
7383         break;
7384       /* Anything else is an error.  */
7385       else
7386         {
7387           /* If we have already issued an error message we don't need
7388              to issue another one.  */
7389           if (decl != error_mark_node
7390               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7391             cp_parser_error (parser, "expected %<,%> or %<;%>");
7392           /* Skip tokens until we reach the end of the statement.  */
7393           cp_parser_skip_to_end_of_statement (parser);
7394           /* If the next token is now a `;', consume it.  */
7395           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7396             cp_lexer_consume_token (parser->lexer);
7397           goto done;
7398         }
7399       /* After the first time around, a function-definition is not
7400          allowed -- even if it was OK at first.  For example:
7401
7402            int i, f() {}
7403
7404          is not valid.  */
7405       function_definition_allowed_p = false;
7406     }
7407
7408   /* Issue an error message if no declarators are present, and the
7409      decl-specifier-seq does not itself declare a class or
7410      enumeration.  */
7411   if (!saw_declarator)
7412     {
7413       if (cp_parser_declares_only_class_p (parser))
7414         shadow_tag (&decl_specifiers);
7415       /* Perform any deferred access checks.  */
7416       perform_deferred_access_checks ();
7417     }
7418
7419   /* Consume the `;'.  */
7420   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7421
7422  done:
7423   pop_deferring_access_checks ();
7424 }
7425
7426 /* Parse a decl-specifier-seq.
7427
7428    decl-specifier-seq:
7429      decl-specifier-seq [opt] decl-specifier
7430
7431    decl-specifier:
7432      storage-class-specifier
7433      type-specifier
7434      function-specifier
7435      friend
7436      typedef
7437
7438    GNU Extension:
7439
7440    decl-specifier:
7441      attributes
7442
7443    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7444
7445    The parser flags FLAGS is used to control type-specifier parsing.
7446
7447    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7448    flags:
7449
7450      1: one of the decl-specifiers is an elaborated-type-specifier
7451         (i.e., a type declaration)
7452      2: one of the decl-specifiers is an enum-specifier or a
7453         class-specifier (i.e., a type definition)
7454
7455    */
7456
7457 static void
7458 cp_parser_decl_specifier_seq (cp_parser* parser,
7459                               cp_parser_flags flags,
7460                               cp_decl_specifier_seq *decl_specs,
7461                               int* declares_class_or_enum)
7462 {
7463   bool constructor_possible_p = !parser->in_declarator_p;
7464
7465   /* Clear DECL_SPECS.  */
7466   clear_decl_specs (decl_specs);
7467
7468   /* Assume no class or enumeration type is declared.  */
7469   *declares_class_or_enum = 0;
7470
7471   /* Keep reading specifiers until there are no more to read.  */
7472   while (true)
7473     {
7474       bool constructor_p;
7475       bool found_decl_spec;
7476       cp_token *token;
7477
7478       /* Peek at the next token.  */
7479       token = cp_lexer_peek_token (parser->lexer);
7480       /* Handle attributes.  */
7481       if (token->keyword == RID_ATTRIBUTE)
7482         {
7483           /* Parse the attributes.  */
7484           decl_specs->attributes
7485             = chainon (decl_specs->attributes,
7486                        cp_parser_attributes_opt (parser));
7487           continue;
7488         }
7489       /* Assume we will find a decl-specifier keyword.  */
7490       found_decl_spec = true;
7491       /* If the next token is an appropriate keyword, we can simply
7492          add it to the list.  */
7493       switch (token->keyword)
7494         {
7495           /* decl-specifier:
7496                friend  */
7497         case RID_FRIEND:
7498           if (!at_class_scope_p ())
7499             {
7500               error ("%<friend%> used outside of class");
7501               cp_lexer_purge_token (parser->lexer);
7502             }
7503           else
7504             {
7505               ++decl_specs->specs[(int) ds_friend];
7506               /* Consume the token.  */
7507               cp_lexer_consume_token (parser->lexer);
7508             }
7509           break;
7510
7511           /* function-specifier:
7512                inline
7513                virtual
7514                explicit  */
7515         case RID_INLINE:
7516         case RID_VIRTUAL:
7517         case RID_EXPLICIT:
7518           cp_parser_function_specifier_opt (parser, decl_specs);
7519           break;
7520
7521           /* decl-specifier:
7522                typedef  */
7523         case RID_TYPEDEF:
7524           ++decl_specs->specs[(int) ds_typedef];
7525           /* Consume the token.  */
7526           cp_lexer_consume_token (parser->lexer);
7527           /* A constructor declarator cannot appear in a typedef.  */
7528           constructor_possible_p = false;
7529           /* The "typedef" keyword can only occur in a declaration; we
7530              may as well commit at this point.  */
7531           cp_parser_commit_to_tentative_parse (parser);
7532           break;
7533
7534           /* storage-class-specifier:
7535                auto
7536                register
7537                static
7538                extern
7539                mutable
7540
7541              GNU Extension:
7542                thread  */
7543         case RID_AUTO:
7544         case RID_REGISTER:
7545         case RID_STATIC:
7546         case RID_EXTERN:
7547         case RID_MUTABLE:
7548           /* Consume the token.  */
7549           cp_lexer_consume_token (parser->lexer);
7550           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7551           break;
7552         case RID_THREAD:
7553           /* Consume the token.  */
7554           cp_lexer_consume_token (parser->lexer);
7555           ++decl_specs->specs[(int) ds_thread];
7556           break;
7557
7558         default:
7559           /* We did not yet find a decl-specifier yet.  */
7560           found_decl_spec = false;
7561           break;
7562         }
7563
7564       /* Constructors are a special case.  The `S' in `S()' is not a
7565          decl-specifier; it is the beginning of the declarator.  */
7566       constructor_p
7567         = (!found_decl_spec
7568            && constructor_possible_p
7569            && (cp_parser_constructor_declarator_p
7570                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7571
7572       /* If we don't have a DECL_SPEC yet, then we must be looking at
7573          a type-specifier.  */
7574       if (!found_decl_spec && !constructor_p)
7575         {
7576           int decl_spec_declares_class_or_enum;
7577           bool is_cv_qualifier;
7578           tree type_spec;
7579
7580           type_spec
7581             = cp_parser_type_specifier (parser, flags,
7582                                         decl_specs,
7583                                         /*is_declaration=*/true,
7584                                         &decl_spec_declares_class_or_enum,
7585                                         &is_cv_qualifier);
7586
7587           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7588
7589           /* If this type-specifier referenced a user-defined type
7590              (a typedef, class-name, etc.), then we can't allow any
7591              more such type-specifiers henceforth.
7592
7593              [dcl.spec]
7594
7595              The longest sequence of decl-specifiers that could
7596              possibly be a type name is taken as the
7597              decl-specifier-seq of a declaration.  The sequence shall
7598              be self-consistent as described below.
7599
7600              [dcl.type]
7601
7602              As a general rule, at most one type-specifier is allowed
7603              in the complete decl-specifier-seq of a declaration.  The
7604              only exceptions are the following:
7605
7606              -- const or volatile can be combined with any other
7607                 type-specifier.
7608
7609              -- signed or unsigned can be combined with char, long,
7610                 short, or int.
7611
7612              -- ..
7613
7614              Example:
7615
7616                typedef char* Pc;
7617                void g (const int Pc);
7618
7619              Here, Pc is *not* part of the decl-specifier seq; it's
7620              the declarator.  Therefore, once we see a type-specifier
7621              (other than a cv-qualifier), we forbid any additional
7622              user-defined types.  We *do* still allow things like `int
7623              int' to be considered a decl-specifier-seq, and issue the
7624              error message later.  */
7625           if (type_spec && !is_cv_qualifier)
7626             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7627           /* A constructor declarator cannot follow a type-specifier.  */
7628           if (type_spec)
7629             {
7630               constructor_possible_p = false;
7631               found_decl_spec = true;
7632             }
7633         }
7634
7635       /* If we still do not have a DECL_SPEC, then there are no more
7636          decl-specifiers.  */
7637       if (!found_decl_spec)
7638         break;
7639
7640       decl_specs->any_specifiers_p = true;
7641       /* After we see one decl-specifier, further decl-specifiers are
7642          always optional.  */
7643       flags |= CP_PARSER_FLAGS_OPTIONAL;
7644     }
7645
7646   cp_parser_check_decl_spec (decl_specs);
7647
7648   /* Don't allow a friend specifier with a class definition.  */
7649   if (decl_specs->specs[(int) ds_friend] != 0
7650       && (*declares_class_or_enum & 2))
7651     error ("class definition may not be declared a friend");
7652 }
7653
7654 /* Parse an (optional) storage-class-specifier.
7655
7656    storage-class-specifier:
7657      auto
7658      register
7659      static
7660      extern
7661      mutable
7662
7663    GNU Extension:
7664
7665    storage-class-specifier:
7666      thread
7667
7668    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7669
7670 static tree
7671 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7672 {
7673   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7674     {
7675     case RID_AUTO:
7676     case RID_REGISTER:
7677     case RID_STATIC:
7678     case RID_EXTERN:
7679     case RID_MUTABLE:
7680     case RID_THREAD:
7681       /* Consume the token.  */
7682       return cp_lexer_consume_token (parser->lexer)->value;
7683
7684     default:
7685       return NULL_TREE;
7686     }
7687 }
7688
7689 /* Parse an (optional) function-specifier.
7690
7691    function-specifier:
7692      inline
7693      virtual
7694      explicit
7695
7696    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7697    Updates DECL_SPECS, if it is non-NULL.  */
7698
7699 static tree
7700 cp_parser_function_specifier_opt (cp_parser* parser,
7701                                   cp_decl_specifier_seq *decl_specs)
7702 {
7703   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7704     {
7705     case RID_INLINE:
7706       if (decl_specs)
7707         ++decl_specs->specs[(int) ds_inline];
7708       break;
7709
7710     case RID_VIRTUAL:
7711       /* 14.5.2.3 [temp.mem]
7712
7713          A member function template shall not be virtual.  */
7714       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7715         error ("templates may not be %<virtual%>");
7716       else if (decl_specs)
7717         ++decl_specs->specs[(int) ds_virtual];
7718       break;
7719
7720     case RID_EXPLICIT:
7721       if (decl_specs)
7722         ++decl_specs->specs[(int) ds_explicit];
7723       break;
7724
7725     default:
7726       return NULL_TREE;
7727     }
7728
7729   /* Consume the token.  */
7730   return cp_lexer_consume_token (parser->lexer)->value;
7731 }
7732
7733 /* Parse a linkage-specification.
7734
7735    linkage-specification:
7736      extern string-literal { declaration-seq [opt] }
7737      extern string-literal declaration  */
7738
7739 static void
7740 cp_parser_linkage_specification (cp_parser* parser)
7741 {
7742   tree linkage;
7743
7744   /* Look for the `extern' keyword.  */
7745   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7746
7747   /* Look for the string-literal.  */
7748   linkage = cp_parser_string_literal (parser, false, false);
7749
7750   /* Transform the literal into an identifier.  If the literal is a
7751      wide-character string, or contains embedded NULs, then we can't
7752      handle it as the user wants.  */
7753   if (strlen (TREE_STRING_POINTER (linkage))
7754       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7755     {
7756       cp_parser_error (parser, "invalid linkage-specification");
7757       /* Assume C++ linkage.  */
7758       linkage = lang_name_cplusplus;
7759     }
7760   else
7761     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7762
7763   /* We're now using the new linkage.  */
7764   push_lang_context (linkage);
7765
7766   /* If the next token is a `{', then we're using the first
7767      production.  */
7768   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7769     {
7770       /* Consume the `{' token.  */
7771       cp_lexer_consume_token (parser->lexer);
7772       /* Parse the declarations.  */
7773       cp_parser_declaration_seq_opt (parser);
7774       /* Look for the closing `}'.  */
7775       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7776     }
7777   /* Otherwise, there's just one declaration.  */
7778   else
7779     {
7780       bool saved_in_unbraced_linkage_specification_p;
7781
7782       saved_in_unbraced_linkage_specification_p
7783         = parser->in_unbraced_linkage_specification_p;
7784       parser->in_unbraced_linkage_specification_p = true;
7785       cp_parser_declaration (parser);
7786       parser->in_unbraced_linkage_specification_p
7787         = saved_in_unbraced_linkage_specification_p;
7788     }
7789
7790   /* We're done with the linkage-specification.  */
7791   pop_lang_context ();
7792 }
7793
7794 /* Special member functions [gram.special] */
7795
7796 /* Parse a conversion-function-id.
7797
7798    conversion-function-id:
7799      operator conversion-type-id
7800
7801    Returns an IDENTIFIER_NODE representing the operator.  */
7802
7803 static tree
7804 cp_parser_conversion_function_id (cp_parser* parser)
7805 {
7806   tree type;
7807   tree saved_scope;
7808   tree saved_qualifying_scope;
7809   tree saved_object_scope;
7810   tree pushed_scope = NULL_TREE;
7811
7812   /* Look for the `operator' token.  */
7813   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7814     return error_mark_node;
7815   /* When we parse the conversion-type-id, the current scope will be
7816      reset.  However, we need that information in able to look up the
7817      conversion function later, so we save it here.  */
7818   saved_scope = parser->scope;
7819   saved_qualifying_scope = parser->qualifying_scope;
7820   saved_object_scope = parser->object_scope;
7821   /* We must enter the scope of the class so that the names of
7822      entities declared within the class are available in the
7823      conversion-type-id.  For example, consider:
7824
7825        struct S {
7826          typedef int I;
7827          operator I();
7828        };
7829
7830        S::operator I() { ... }
7831
7832      In order to see that `I' is a type-name in the definition, we
7833      must be in the scope of `S'.  */
7834   if (saved_scope)
7835     pushed_scope = push_scope (saved_scope);
7836   /* Parse the conversion-type-id.  */
7837   type = cp_parser_conversion_type_id (parser);
7838   /* Leave the scope of the class, if any.  */
7839   if (pushed_scope)
7840     pop_scope (pushed_scope);
7841   /* Restore the saved scope.  */
7842   parser->scope = saved_scope;
7843   parser->qualifying_scope = saved_qualifying_scope;
7844   parser->object_scope = saved_object_scope;
7845   /* If the TYPE is invalid, indicate failure.  */
7846   if (type == error_mark_node)
7847     return error_mark_node;
7848   return mangle_conv_op_name_for_type (type);
7849 }
7850
7851 /* Parse a conversion-type-id:
7852
7853    conversion-type-id:
7854      type-specifier-seq conversion-declarator [opt]
7855
7856    Returns the TYPE specified.  */
7857
7858 static tree
7859 cp_parser_conversion_type_id (cp_parser* parser)
7860 {
7861   tree attributes;
7862   cp_decl_specifier_seq type_specifiers;
7863   cp_declarator *declarator;
7864   tree type_specified;
7865
7866   /* Parse the attributes.  */
7867   attributes = cp_parser_attributes_opt (parser);
7868   /* Parse the type-specifiers.  */
7869   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7870                                 &type_specifiers);
7871   /* If that didn't work, stop.  */
7872   if (type_specifiers.type == error_mark_node)
7873     return error_mark_node;
7874   /* Parse the conversion-declarator.  */
7875   declarator = cp_parser_conversion_declarator_opt (parser);
7876
7877   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7878                                     /*initialized=*/0, &attributes);
7879   if (attributes)
7880     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7881   return type_specified;
7882 }
7883
7884 /* Parse an (optional) conversion-declarator.
7885
7886    conversion-declarator:
7887      ptr-operator conversion-declarator [opt]
7888
7889    */
7890
7891 static cp_declarator *
7892 cp_parser_conversion_declarator_opt (cp_parser* parser)
7893 {
7894   enum tree_code code;
7895   tree class_type;
7896   cp_cv_quals cv_quals;
7897
7898   /* We don't know if there's a ptr-operator next, or not.  */
7899   cp_parser_parse_tentatively (parser);
7900   /* Try the ptr-operator.  */
7901   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7902   /* If it worked, look for more conversion-declarators.  */
7903   if (cp_parser_parse_definitely (parser))
7904     {
7905       cp_declarator *declarator;
7906
7907       /* Parse another optional declarator.  */
7908       declarator = cp_parser_conversion_declarator_opt (parser);
7909
7910       /* Create the representation of the declarator.  */
7911       if (class_type)
7912         declarator = make_ptrmem_declarator (cv_quals, class_type,
7913                                              declarator);
7914       else if (code == INDIRECT_REF)
7915         declarator = make_pointer_declarator (cv_quals, declarator);
7916       else
7917         declarator = make_reference_declarator (cv_quals, declarator);
7918
7919       return declarator;
7920    }
7921
7922   return NULL;
7923 }
7924
7925 /* Parse an (optional) ctor-initializer.
7926
7927    ctor-initializer:
7928      : mem-initializer-list
7929
7930    Returns TRUE iff the ctor-initializer was actually present.  */
7931
7932 static bool
7933 cp_parser_ctor_initializer_opt (cp_parser* parser)
7934 {
7935   /* If the next token is not a `:', then there is no
7936      ctor-initializer.  */
7937   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7938     {
7939       /* Do default initialization of any bases and members.  */
7940       if (DECL_CONSTRUCTOR_P (current_function_decl))
7941         finish_mem_initializers (NULL_TREE);
7942
7943       return false;
7944     }
7945
7946   /* Consume the `:' token.  */
7947   cp_lexer_consume_token (parser->lexer);
7948   /* And the mem-initializer-list.  */
7949   cp_parser_mem_initializer_list (parser);
7950
7951   return true;
7952 }
7953
7954 /* Parse a mem-initializer-list.
7955
7956    mem-initializer-list:
7957      mem-initializer
7958      mem-initializer , mem-initializer-list  */
7959
7960 static void
7961 cp_parser_mem_initializer_list (cp_parser* parser)
7962 {
7963   tree mem_initializer_list = NULL_TREE;
7964
7965   /* Let the semantic analysis code know that we are starting the
7966      mem-initializer-list.  */
7967   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7968     error ("only constructors take base initializers");
7969
7970   /* Loop through the list.  */
7971   while (true)
7972     {
7973       tree mem_initializer;
7974
7975       /* Parse the mem-initializer.  */
7976       mem_initializer = cp_parser_mem_initializer (parser);
7977       /* Add it to the list, unless it was erroneous.  */
7978       if (mem_initializer != error_mark_node)
7979         {
7980           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7981           mem_initializer_list = mem_initializer;
7982         }
7983       /* If the next token is not a `,', we're done.  */
7984       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7985         break;
7986       /* Consume the `,' token.  */
7987       cp_lexer_consume_token (parser->lexer);
7988     }
7989
7990   /* Perform semantic analysis.  */
7991   if (DECL_CONSTRUCTOR_P (current_function_decl))
7992     finish_mem_initializers (mem_initializer_list);
7993 }
7994
7995 /* Parse a mem-initializer.
7996
7997    mem-initializer:
7998      mem-initializer-id ( expression-list [opt] )
7999
8000    GNU extension:
8001
8002    mem-initializer:
8003      ( expression-list [opt] )
8004
8005    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
8006    class) or FIELD_DECL (for a non-static data member) to initialize;
8007    the TREE_VALUE is the expression-list.  An empty initialization
8008    list is represented by void_list_node.  */
8009
8010 static tree
8011 cp_parser_mem_initializer (cp_parser* parser)
8012 {
8013   tree mem_initializer_id;
8014   tree expression_list;
8015   tree member;
8016
8017   /* Find out what is being initialized.  */
8018   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8019     {
8020       pedwarn ("anachronistic old-style base class initializer");
8021       mem_initializer_id = NULL_TREE;
8022     }
8023   else
8024     mem_initializer_id = cp_parser_mem_initializer_id (parser);
8025   member = expand_member_init (mem_initializer_id);
8026   if (member && !DECL_P (member))
8027     in_base_initializer = 1;
8028
8029   expression_list
8030     = cp_parser_parenthesized_expression_list (parser, false,
8031                                                /*cast_p=*/false,
8032                                                /*non_constant_p=*/NULL);
8033   if (expression_list == error_mark_node)
8034     return error_mark_node;
8035   if (!expression_list)
8036     expression_list = void_type_node;
8037
8038   in_base_initializer = 0;
8039
8040   return member ? build_tree_list (member, expression_list) : error_mark_node;
8041 }
8042
8043 /* Parse a mem-initializer-id.
8044
8045    mem-initializer-id:
8046      :: [opt] nested-name-specifier [opt] class-name
8047      identifier
8048
8049    Returns a TYPE indicating the class to be initializer for the first
8050    production.  Returns an IDENTIFIER_NODE indicating the data member
8051    to be initialized for the second production.  */
8052
8053 static tree
8054 cp_parser_mem_initializer_id (cp_parser* parser)
8055 {
8056   bool global_scope_p;
8057   bool nested_name_specifier_p;
8058   bool template_p = false;
8059   tree id;
8060
8061   /* `typename' is not allowed in this context ([temp.res]).  */
8062   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8063     {
8064       error ("keyword %<typename%> not allowed in this context (a qualified "
8065              "member initializer is implicitly a type)");
8066       cp_lexer_consume_token (parser->lexer);
8067     }
8068   /* Look for the optional `::' operator.  */
8069   global_scope_p
8070     = (cp_parser_global_scope_opt (parser,
8071                                    /*current_scope_valid_p=*/false,
8072                                    /*object_scope_valid_p=*/false)
8073        != NULL_TREE);
8074   /* Look for the optional nested-name-specifier.  The simplest way to
8075      implement:
8076
8077        [temp.res]
8078
8079        The keyword `typename' is not permitted in a base-specifier or
8080        mem-initializer; in these contexts a qualified name that
8081        depends on a template-parameter is implicitly assumed to be a
8082        type name.
8083
8084      is to assume that we have seen the `typename' keyword at this
8085      point.  */
8086   nested_name_specifier_p
8087     = (cp_parser_nested_name_specifier_opt (parser,
8088                                             /*typename_keyword_p=*/true,
8089                                             /*check_dependency_p=*/true,
8090                                             /*type_p=*/true,
8091                                             /*is_declaration=*/true)
8092        != NULL_TREE);
8093   if (nested_name_specifier_p)
8094     template_p = cp_parser_optional_template_keyword (parser);
8095   /* If there is a `::' operator or a nested-name-specifier, then we
8096      are definitely looking for a class-name.  */
8097   if (global_scope_p || nested_name_specifier_p)
8098     return cp_parser_class_name (parser,
8099                                  /*typename_keyword_p=*/true,
8100                                  /*template_keyword_p=*/template_p,
8101                                  none_type,
8102                                  /*check_dependency_p=*/true,
8103                                  /*class_head_p=*/false,
8104                                  /*is_declaration=*/true);
8105   /* Otherwise, we could also be looking for an ordinary identifier.  */
8106   cp_parser_parse_tentatively (parser);
8107   /* Try a class-name.  */
8108   id = cp_parser_class_name (parser,
8109                              /*typename_keyword_p=*/true,
8110                              /*template_keyword_p=*/false,
8111                              none_type,
8112                              /*check_dependency_p=*/true,
8113                              /*class_head_p=*/false,
8114                              /*is_declaration=*/true);
8115   /* If we found one, we're done.  */
8116   if (cp_parser_parse_definitely (parser))
8117     return id;
8118   /* Otherwise, look for an ordinary identifier.  */
8119   return cp_parser_identifier (parser);
8120 }
8121
8122 /* Overloading [gram.over] */
8123
8124 /* Parse an operator-function-id.
8125
8126    operator-function-id:
8127      operator operator
8128
8129    Returns an IDENTIFIER_NODE for the operator which is a
8130    human-readable spelling of the identifier, e.g., `operator +'.  */
8131
8132 static tree
8133 cp_parser_operator_function_id (cp_parser* parser)
8134 {
8135   /* Look for the `operator' keyword.  */
8136   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8137     return error_mark_node;
8138   /* And then the name of the operator itself.  */
8139   return cp_parser_operator (parser);
8140 }
8141
8142 /* Parse an operator.
8143
8144    operator:
8145      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8146      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8147      || ++ -- , ->* -> () []
8148
8149    GNU Extensions:
8150
8151    operator:
8152      <? >? <?= >?=
8153
8154    Returns an IDENTIFIER_NODE for the operator which is a
8155    human-readable spelling of the identifier, e.g., `operator +'.  */
8156
8157 static tree
8158 cp_parser_operator (cp_parser* parser)
8159 {
8160   tree id = NULL_TREE;
8161   cp_token *token;
8162
8163   /* Peek at the next token.  */
8164   token = cp_lexer_peek_token (parser->lexer);
8165   /* Figure out which operator we have.  */
8166   switch (token->type)
8167     {
8168     case CPP_KEYWORD:
8169       {
8170         enum tree_code op;
8171
8172         /* The keyword should be either `new' or `delete'.  */
8173         if (token->keyword == RID_NEW)
8174           op = NEW_EXPR;
8175         else if (token->keyword == RID_DELETE)
8176           op = DELETE_EXPR;
8177         else
8178           break;
8179
8180         /* Consume the `new' or `delete' token.  */
8181         cp_lexer_consume_token (parser->lexer);
8182
8183         /* Peek at the next token.  */
8184         token = cp_lexer_peek_token (parser->lexer);
8185         /* If it's a `[' token then this is the array variant of the
8186            operator.  */
8187         if (token->type == CPP_OPEN_SQUARE)
8188           {
8189             /* Consume the `[' token.  */
8190             cp_lexer_consume_token (parser->lexer);
8191             /* Look for the `]' token.  */
8192             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8193             id = ansi_opname (op == NEW_EXPR
8194                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8195           }
8196         /* Otherwise, we have the non-array variant.  */
8197         else
8198           id = ansi_opname (op);
8199
8200         return id;
8201       }
8202
8203     case CPP_PLUS:
8204       id = ansi_opname (PLUS_EXPR);
8205       break;
8206
8207     case CPP_MINUS:
8208       id = ansi_opname (MINUS_EXPR);
8209       break;
8210
8211     case CPP_MULT:
8212       id = ansi_opname (MULT_EXPR);
8213       break;
8214
8215     case CPP_DIV:
8216       id = ansi_opname (TRUNC_DIV_EXPR);
8217       break;
8218
8219     case CPP_MOD:
8220       id = ansi_opname (TRUNC_MOD_EXPR);
8221       break;
8222
8223     case CPP_XOR:
8224       id = ansi_opname (BIT_XOR_EXPR);
8225       break;
8226
8227     case CPP_AND:
8228       id = ansi_opname (BIT_AND_EXPR);
8229       break;
8230
8231     case CPP_OR:
8232       id = ansi_opname (BIT_IOR_EXPR);
8233       break;
8234
8235     case CPP_COMPL:
8236       id = ansi_opname (BIT_NOT_EXPR);
8237       break;
8238
8239     case CPP_NOT:
8240       id = ansi_opname (TRUTH_NOT_EXPR);
8241       break;
8242
8243     case CPP_EQ:
8244       id = ansi_assopname (NOP_EXPR);
8245       break;
8246
8247     case CPP_LESS:
8248       id = ansi_opname (LT_EXPR);
8249       break;
8250
8251     case CPP_GREATER:
8252       id = ansi_opname (GT_EXPR);
8253       break;
8254
8255     case CPP_PLUS_EQ:
8256       id = ansi_assopname (PLUS_EXPR);
8257       break;
8258
8259     case CPP_MINUS_EQ:
8260       id = ansi_assopname (MINUS_EXPR);
8261       break;
8262
8263     case CPP_MULT_EQ:
8264       id = ansi_assopname (MULT_EXPR);
8265       break;
8266
8267     case CPP_DIV_EQ:
8268       id = ansi_assopname (TRUNC_DIV_EXPR);
8269       break;
8270
8271     case CPP_MOD_EQ:
8272       id = ansi_assopname (TRUNC_MOD_EXPR);
8273       break;
8274
8275     case CPP_XOR_EQ:
8276       id = ansi_assopname (BIT_XOR_EXPR);
8277       break;
8278
8279     case CPP_AND_EQ:
8280       id = ansi_assopname (BIT_AND_EXPR);
8281       break;
8282
8283     case CPP_OR_EQ:
8284       id = ansi_assopname (BIT_IOR_EXPR);
8285       break;
8286
8287     case CPP_LSHIFT:
8288       id = ansi_opname (LSHIFT_EXPR);
8289       break;
8290
8291     case CPP_RSHIFT:
8292       id = ansi_opname (RSHIFT_EXPR);
8293       break;
8294
8295     case CPP_LSHIFT_EQ:
8296       id = ansi_assopname (LSHIFT_EXPR);
8297       break;
8298
8299     case CPP_RSHIFT_EQ:
8300       id = ansi_assopname (RSHIFT_EXPR);
8301       break;
8302
8303     case CPP_EQ_EQ:
8304       id = ansi_opname (EQ_EXPR);
8305       break;
8306
8307     case CPP_NOT_EQ:
8308       id = ansi_opname (NE_EXPR);
8309       break;
8310
8311     case CPP_LESS_EQ:
8312       id = ansi_opname (LE_EXPR);
8313       break;
8314
8315     case CPP_GREATER_EQ:
8316       id = ansi_opname (GE_EXPR);
8317       break;
8318
8319     case CPP_AND_AND:
8320       id = ansi_opname (TRUTH_ANDIF_EXPR);
8321       break;
8322
8323     case CPP_OR_OR:
8324       id = ansi_opname (TRUTH_ORIF_EXPR);
8325       break;
8326
8327     case CPP_PLUS_PLUS:
8328       id = ansi_opname (POSTINCREMENT_EXPR);
8329       break;
8330
8331     case CPP_MINUS_MINUS:
8332       id = ansi_opname (PREDECREMENT_EXPR);
8333       break;
8334
8335     case CPP_COMMA:
8336       id = ansi_opname (COMPOUND_EXPR);
8337       break;
8338
8339     case CPP_DEREF_STAR:
8340       id = ansi_opname (MEMBER_REF);
8341       break;
8342
8343     case CPP_DEREF:
8344       id = ansi_opname (COMPONENT_REF);
8345       break;
8346
8347     case CPP_OPEN_PAREN:
8348       /* Consume the `('.  */
8349       cp_lexer_consume_token (parser->lexer);
8350       /* Look for the matching `)'.  */
8351       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8352       return ansi_opname (CALL_EXPR);
8353
8354     case CPP_OPEN_SQUARE:
8355       /* Consume the `['.  */
8356       cp_lexer_consume_token (parser->lexer);
8357       /* Look for the matching `]'.  */
8358       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8359       return ansi_opname (ARRAY_REF);
8360
8361     default:
8362       /* Anything else is an error.  */
8363       break;
8364     }
8365
8366   /* If we have selected an identifier, we need to consume the
8367      operator token.  */
8368   if (id)
8369     cp_lexer_consume_token (parser->lexer);
8370   /* Otherwise, no valid operator name was present.  */
8371   else
8372     {
8373       cp_parser_error (parser, "expected operator");
8374       id = error_mark_node;
8375     }
8376
8377   return id;
8378 }
8379
8380 /* Parse a template-declaration.
8381
8382    template-declaration:
8383      export [opt] template < template-parameter-list > declaration
8384
8385    If MEMBER_P is TRUE, this template-declaration occurs within a
8386    class-specifier.
8387
8388    The grammar rule given by the standard isn't correct.  What
8389    is really meant is:
8390
8391    template-declaration:
8392      export [opt] template-parameter-list-seq
8393        decl-specifier-seq [opt] init-declarator [opt] ;
8394      export [opt] template-parameter-list-seq
8395        function-definition
8396
8397    template-parameter-list-seq:
8398      template-parameter-list-seq [opt]
8399      template < template-parameter-list >  */
8400
8401 static void
8402 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8403 {
8404   /* Check for `export'.  */
8405   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8406     {
8407       /* Consume the `export' token.  */
8408       cp_lexer_consume_token (parser->lexer);
8409       /* Warn that we do not support `export'.  */
8410       warning (0, "keyword %<export%> not implemented, and will be ignored");
8411     }
8412
8413   cp_parser_template_declaration_after_export (parser, member_p);
8414 }
8415
8416 /* Parse a template-parameter-list.
8417
8418    template-parameter-list:
8419      template-parameter
8420      template-parameter-list , template-parameter
8421
8422    Returns a TREE_LIST.  Each node represents a template parameter.
8423    The nodes are connected via their TREE_CHAINs.  */
8424
8425 static tree
8426 cp_parser_template_parameter_list (cp_parser* parser)
8427 {
8428   tree parameter_list = NULL_TREE;
8429
8430   begin_template_parm_list ();
8431   while (true)
8432     {
8433       tree parameter;
8434       cp_token *token;
8435       bool is_non_type;
8436
8437       /* Parse the template-parameter.  */
8438       parameter = cp_parser_template_parameter (parser, &is_non_type);
8439       /* Add it to the list.  */
8440       if (parameter != error_mark_node)
8441         parameter_list = process_template_parm (parameter_list,
8442                                                 parameter,
8443                                                 is_non_type);
8444       else
8445        {
8446          tree err_parm = build_tree_list (parameter, parameter);
8447          TREE_VALUE (err_parm) = error_mark_node;
8448          parameter_list = chainon (parameter_list, err_parm);
8449        }
8450
8451       /* Peek at the next token.  */
8452       token = cp_lexer_peek_token (parser->lexer);
8453       /* If it's not a `,', we're done.  */
8454       if (token->type != CPP_COMMA)
8455         break;
8456       /* Otherwise, consume the `,' token.  */
8457       cp_lexer_consume_token (parser->lexer);
8458     }
8459
8460   return end_template_parm_list (parameter_list);
8461 }
8462
8463 /* Parse a template-parameter.
8464
8465    template-parameter:
8466      type-parameter
8467      parameter-declaration
8468
8469    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8470    the parameter.  The TREE_PURPOSE is the default value, if any.
8471    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8472    iff this parameter is a non-type parameter.  */
8473
8474 static tree
8475 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8476 {
8477   cp_token *token;
8478   cp_parameter_declarator *parameter_declarator;
8479   tree parm;
8480
8481   /* Assume it is a type parameter or a template parameter.  */
8482   *is_non_type = false;
8483   /* Peek at the next token.  */
8484   token = cp_lexer_peek_token (parser->lexer);
8485   /* If it is `class' or `template', we have a type-parameter.  */
8486   if (token->keyword == RID_TEMPLATE)
8487     return cp_parser_type_parameter (parser);
8488   /* If it is `class' or `typename' we do not know yet whether it is a
8489      type parameter or a non-type parameter.  Consider:
8490
8491        template <typename T, typename T::X X> ...
8492
8493      or:
8494
8495        template <class C, class D*> ...
8496
8497      Here, the first parameter is a type parameter, and the second is
8498      a non-type parameter.  We can tell by looking at the token after
8499      the identifier -- if it is a `,', `=', or `>' then we have a type
8500      parameter.  */
8501   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8502     {
8503       /* Peek at the token after `class' or `typename'.  */
8504       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8505       /* If it's an identifier, skip it.  */
8506       if (token->type == CPP_NAME)
8507         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8508       /* Now, see if the token looks like the end of a template
8509          parameter.  */
8510       if (token->type == CPP_COMMA
8511           || token->type == CPP_EQ
8512           || token->type == CPP_GREATER)
8513         return cp_parser_type_parameter (parser);
8514     }
8515
8516   /* Otherwise, it is a non-type parameter.
8517
8518      [temp.param]
8519
8520      When parsing a default template-argument for a non-type
8521      template-parameter, the first non-nested `>' is taken as the end
8522      of the template parameter-list rather than a greater-than
8523      operator.  */
8524   *is_non_type = true;
8525   parameter_declarator
8526      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8527                                         /*parenthesized_p=*/NULL);
8528   parm = grokdeclarator (parameter_declarator->declarator,
8529                          &parameter_declarator->decl_specifiers,
8530                          PARM, /*initialized=*/0,
8531                          /*attrlist=*/NULL);
8532   if (parm == error_mark_node)
8533     return error_mark_node;
8534   return build_tree_list (parameter_declarator->default_argument, parm);
8535 }
8536
8537 /* Parse a type-parameter.
8538
8539    type-parameter:
8540      class identifier [opt]
8541      class identifier [opt] = type-id
8542      typename identifier [opt]
8543      typename identifier [opt] = type-id
8544      template < template-parameter-list > class identifier [opt]
8545      template < template-parameter-list > class identifier [opt]
8546        = id-expression
8547
8548    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8549    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8550    the declaration of the parameter.  */
8551
8552 static tree
8553 cp_parser_type_parameter (cp_parser* parser)
8554 {
8555   cp_token *token;
8556   tree parameter;
8557
8558   /* Look for a keyword to tell us what kind of parameter this is.  */
8559   token = cp_parser_require (parser, CPP_KEYWORD,
8560                              "`class', `typename', or `template'");
8561   if (!token)
8562     return error_mark_node;
8563
8564   switch (token->keyword)
8565     {
8566     case RID_CLASS:
8567     case RID_TYPENAME:
8568       {
8569         tree identifier;
8570         tree default_argument;
8571
8572         /* If the next token is an identifier, then it names the
8573            parameter.  */
8574         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8575           identifier = cp_parser_identifier (parser);
8576         else
8577           identifier = NULL_TREE;
8578
8579         /* Create the parameter.  */
8580         parameter = finish_template_type_parm (class_type_node, identifier);
8581
8582         /* If the next token is an `=', we have a default argument.  */
8583         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8584           {
8585             /* Consume the `=' token.  */
8586             cp_lexer_consume_token (parser->lexer);
8587             /* Parse the default-argument.  */
8588             push_deferring_access_checks (dk_no_deferred);
8589             default_argument = cp_parser_type_id (parser);
8590             pop_deferring_access_checks ();
8591           }
8592         else
8593           default_argument = NULL_TREE;
8594
8595         /* Create the combined representation of the parameter and the
8596            default argument.  */
8597         parameter = build_tree_list (default_argument, parameter);
8598       }
8599       break;
8600
8601     case RID_TEMPLATE:
8602       {
8603         tree parameter_list;
8604         tree identifier;
8605         tree default_argument;
8606
8607         /* Look for the `<'.  */
8608         cp_parser_require (parser, CPP_LESS, "`<'");
8609         /* Parse the template-parameter-list.  */
8610         parameter_list = cp_parser_template_parameter_list (parser);
8611         /* Look for the `>'.  */
8612         cp_parser_require (parser, CPP_GREATER, "`>'");
8613         /* Look for the `class' keyword.  */
8614         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8615         /* If the next token is an `=', then there is a
8616            default-argument.  If the next token is a `>', we are at
8617            the end of the parameter-list.  If the next token is a `,',
8618            then we are at the end of this parameter.  */
8619         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8620             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8621             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8622           {
8623             identifier = cp_parser_identifier (parser);
8624             /* Treat invalid names as if the parameter were nameless.  */
8625             if (identifier == error_mark_node)
8626               identifier = NULL_TREE;
8627           }
8628         else
8629           identifier = NULL_TREE;
8630
8631         /* Create the template parameter.  */
8632         parameter = finish_template_template_parm (class_type_node,
8633                                                    identifier);
8634
8635         /* If the next token is an `=', then there is a
8636            default-argument.  */
8637         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8638           {
8639             bool is_template;
8640
8641             /* Consume the `='.  */
8642             cp_lexer_consume_token (parser->lexer);
8643             /* Parse the id-expression.  */
8644             push_deferring_access_checks (dk_no_deferred);
8645             default_argument
8646               = cp_parser_id_expression (parser,
8647                                          /*template_keyword_p=*/false,
8648                                          /*check_dependency_p=*/true,
8649                                          /*template_p=*/&is_template,
8650                                          /*declarator_p=*/false,
8651                                          /*optional_p=*/false,
8652                                          /*member_p=*/false);
8653             if (TREE_CODE (default_argument) == TYPE_DECL)
8654               /* If the id-expression was a template-id that refers to
8655                  a template-class, we already have the declaration here,
8656                  so no further lookup is needed.  */
8657                  ;
8658             else
8659               /* Look up the name.  */
8660               default_argument
8661                 = cp_parser_lookup_name (parser, default_argument,
8662                                          none_type,
8663                                          /*is_template=*/is_template,
8664                                          /*is_namespace=*/false,
8665                                          /*check_dependency=*/true,
8666                                          /*ambiguous_decls=*/NULL);
8667             /* See if the default argument is valid.  */
8668             default_argument
8669               = check_template_template_default_arg (default_argument);
8670             pop_deferring_access_checks ();
8671           }
8672         else
8673           default_argument = NULL_TREE;
8674
8675         /* Create the combined representation of the parameter and the
8676            default argument.  */
8677         parameter = build_tree_list (default_argument, parameter);
8678       }
8679       break;
8680
8681     default:
8682       gcc_unreachable ();
8683       break;
8684     }
8685
8686   return parameter;
8687 }
8688
8689 /* Parse a template-id.
8690
8691    template-id:
8692      template-name < template-argument-list [opt] >
8693
8694    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8695    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8696    returned.  Otherwise, if the template-name names a function, or set
8697    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8698    names a class, returns a TYPE_DECL for the specialization.
8699
8700    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8701    uninstantiated templates.  */
8702
8703 static tree
8704 cp_parser_template_id (cp_parser *parser,
8705                        bool template_keyword_p,
8706                        bool check_dependency_p,
8707                        bool is_declaration)
8708 {
8709   tree template;
8710   tree arguments;
8711   tree template_id;
8712   cp_token_position start_of_id = 0;
8713   tree access_check = NULL_TREE;
8714   cp_token *next_token, *next_token_2;
8715   bool is_identifier;
8716
8717   /* If the next token corresponds to a template-id, there is no need
8718      to reparse it.  */
8719   next_token = cp_lexer_peek_token (parser->lexer);
8720   if (next_token->type == CPP_TEMPLATE_ID)
8721     {
8722       tree value;
8723       tree check;
8724
8725       /* Get the stored value.  */
8726       value = cp_lexer_consume_token (parser->lexer)->value;
8727       /* Perform any access checks that were deferred.  */
8728       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8729         perform_or_defer_access_check (TREE_PURPOSE (check),
8730                                        TREE_VALUE (check));
8731       /* Return the stored value.  */
8732       return TREE_VALUE (value);
8733     }
8734
8735   /* Avoid performing name lookup if there is no possibility of
8736      finding a template-id.  */
8737   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8738       || (next_token->type == CPP_NAME
8739           && !cp_parser_nth_token_starts_template_argument_list_p
8740                (parser, 2)))
8741     {
8742       cp_parser_error (parser, "expected template-id");
8743       return error_mark_node;
8744     }
8745
8746   /* Remember where the template-id starts.  */
8747   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8748     start_of_id = cp_lexer_token_position (parser->lexer, false);
8749
8750   push_deferring_access_checks (dk_deferred);
8751
8752   /* Parse the template-name.  */
8753   is_identifier = false;
8754   template = cp_parser_template_name (parser, template_keyword_p,
8755                                       check_dependency_p,
8756                                       is_declaration,
8757                                       &is_identifier);
8758   if (template == error_mark_node || is_identifier)
8759     {
8760       pop_deferring_access_checks ();
8761       return template;
8762     }
8763
8764   /* If we find the sequence `[:' after a template-name, it's probably
8765      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8766      parse correctly the argument list.  */
8767   next_token = cp_lexer_peek_token (parser->lexer);
8768   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8769   if (next_token->type == CPP_OPEN_SQUARE
8770       && next_token->flags & DIGRAPH
8771       && next_token_2->type == CPP_COLON
8772       && !(next_token_2->flags & PREV_WHITE))
8773     {
8774       cp_parser_parse_tentatively (parser);
8775       /* Change `:' into `::'.  */
8776       next_token_2->type = CPP_SCOPE;
8777       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8778          CPP_LESS.  */
8779       cp_lexer_consume_token (parser->lexer);
8780       /* Parse the arguments.  */
8781       arguments = cp_parser_enclosed_template_argument_list (parser);
8782       if (!cp_parser_parse_definitely (parser))
8783         {
8784           /* If we couldn't parse an argument list, then we revert our changes
8785              and return simply an error. Maybe this is not a template-id
8786              after all.  */
8787           next_token_2->type = CPP_COLON;
8788           cp_parser_error (parser, "expected %<<%>");
8789           pop_deferring_access_checks ();
8790           return error_mark_node;
8791         }
8792       /* Otherwise, emit an error about the invalid digraph, but continue
8793          parsing because we got our argument list.  */
8794       pedwarn ("%<<::%> cannot begin a template-argument list");
8795       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8796               "between %<<%> and %<::%>");
8797       if (!flag_permissive)
8798         {
8799           static bool hint;
8800           if (!hint)
8801             {
8802               inform ("(if you use -fpermissive G++ will accept your code)");
8803               hint = true;
8804             }
8805         }
8806     }
8807   else
8808     {
8809       /* Look for the `<' that starts the template-argument-list.  */
8810       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8811         {
8812           pop_deferring_access_checks ();
8813           return error_mark_node;
8814         }
8815       /* Parse the arguments.  */
8816       arguments = cp_parser_enclosed_template_argument_list (parser);
8817     }
8818
8819   /* Build a representation of the specialization.  */
8820   if (TREE_CODE (template) == IDENTIFIER_NODE)
8821     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8822   else if (DECL_CLASS_TEMPLATE_P (template)
8823            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8824     {
8825       bool entering_scope;
8826       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8827          template (rather than some instantiation thereof) only if
8828          is not nested within some other construct.  For example, in
8829          "template <typename T> void f(T) { A<T>::", A<T> is just an
8830          instantiation of A.  */
8831       entering_scope = (template_parm_scope_p ()
8832                         && cp_lexer_next_token_is (parser->lexer,
8833                                                    CPP_SCOPE));
8834       template_id
8835         = finish_template_type (template, arguments, entering_scope);
8836     }
8837   else
8838     {
8839       /* If it's not a class-template or a template-template, it should be
8840          a function-template.  */
8841       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8842                    || TREE_CODE (template) == OVERLOAD
8843                    || BASELINK_P (template)));
8844
8845       template_id = lookup_template_function (template, arguments);
8846     }
8847
8848   /* Retrieve any deferred checks.  Do not pop this access checks yet
8849      so the memory will not be reclaimed during token replacing below.  */
8850   access_check = get_deferred_access_checks ();
8851
8852   /* If parsing tentatively, replace the sequence of tokens that makes
8853      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8854      should we re-parse the token stream, we will not have to repeat
8855      the effort required to do the parse, nor will we issue duplicate
8856      error messages about problems during instantiation of the
8857      template.  */
8858   if (start_of_id)
8859     {
8860       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8861
8862       /* Reset the contents of the START_OF_ID token.  */
8863       token->type = CPP_TEMPLATE_ID;
8864       token->value = build_tree_list (access_check, template_id);
8865       token->keyword = RID_MAX;
8866
8867       /* Purge all subsequent tokens.  */
8868       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8869
8870       /* ??? Can we actually assume that, if template_id ==
8871          error_mark_node, we will have issued a diagnostic to the
8872          user, as opposed to simply marking the tentative parse as
8873          failed?  */
8874       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8875         error ("parse error in template argument list");
8876     }
8877
8878   pop_deferring_access_checks ();
8879   return template_id;
8880 }
8881
8882 /* Parse a template-name.
8883
8884    template-name:
8885      identifier
8886
8887    The standard should actually say:
8888
8889    template-name:
8890      identifier
8891      operator-function-id
8892
8893    A defect report has been filed about this issue.
8894
8895    A conversion-function-id cannot be a template name because they cannot
8896    be part of a template-id. In fact, looking at this code:
8897
8898    a.operator K<int>()
8899
8900    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8901    It is impossible to call a templated conversion-function-id with an
8902    explicit argument list, since the only allowed template parameter is
8903    the type to which it is converting.
8904
8905    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8906    `template' keyword, in a construction like:
8907
8908      T::template f<3>()
8909
8910    In that case `f' is taken to be a template-name, even though there
8911    is no way of knowing for sure.
8912
8913    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8914    name refers to a set of overloaded functions, at least one of which
8915    is a template, or an IDENTIFIER_NODE with the name of the template,
8916    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8917    names are looked up inside uninstantiated templates.  */
8918
8919 static tree
8920 cp_parser_template_name (cp_parser* parser,
8921                          bool template_keyword_p,
8922                          bool check_dependency_p,
8923                          bool is_declaration,
8924                          bool *is_identifier)
8925 {
8926   tree identifier;
8927   tree decl;
8928   tree fns;
8929
8930   /* If the next token is `operator', then we have either an
8931      operator-function-id or a conversion-function-id.  */
8932   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8933     {
8934       /* We don't know whether we're looking at an
8935          operator-function-id or a conversion-function-id.  */
8936       cp_parser_parse_tentatively (parser);
8937       /* Try an operator-function-id.  */
8938       identifier = cp_parser_operator_function_id (parser);
8939       /* If that didn't work, try a conversion-function-id.  */
8940       if (!cp_parser_parse_definitely (parser))
8941         {
8942           cp_parser_error (parser, "expected template-name");
8943           return error_mark_node;
8944         }
8945     }
8946   /* Look for the identifier.  */
8947   else
8948     identifier = cp_parser_identifier (parser);
8949
8950   /* If we didn't find an identifier, we don't have a template-id.  */
8951   if (identifier == error_mark_node)
8952     return error_mark_node;
8953
8954   /* If the name immediately followed the `template' keyword, then it
8955      is a template-name.  However, if the next token is not `<', then
8956      we do not treat it as a template-name, since it is not being used
8957      as part of a template-id.  This enables us to handle constructs
8958      like:
8959
8960        template <typename T> struct S { S(); };
8961        template <typename T> S<T>::S();
8962
8963      correctly.  We would treat `S' as a template -- if it were `S<T>'
8964      -- but we do not if there is no `<'.  */
8965
8966   if (processing_template_decl
8967       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8968     {
8969       /* In a declaration, in a dependent context, we pretend that the
8970          "template" keyword was present in order to improve error
8971          recovery.  For example, given:
8972
8973            template <typename T> void f(T::X<int>);
8974
8975          we want to treat "X<int>" as a template-id.  */
8976       if (is_declaration
8977           && !template_keyword_p
8978           && parser->scope && TYPE_P (parser->scope)
8979           && check_dependency_p
8980           && dependent_type_p (parser->scope)
8981           /* Do not do this for dtors (or ctors), since they never
8982              need the template keyword before their name.  */
8983           && !constructor_name_p (identifier, parser->scope))
8984         {
8985           cp_token_position start = 0;
8986
8987           /* Explain what went wrong.  */
8988           error ("non-template %qD used as template", identifier);
8989           inform ("use %<%T::template %D%> to indicate that it is a template",
8990                   parser->scope, identifier);
8991           /* If parsing tentatively, find the location of the "<" token.  */
8992           if (cp_parser_simulate_error (parser))
8993             start = cp_lexer_token_position (parser->lexer, true);
8994           /* Parse the template arguments so that we can issue error
8995              messages about them.  */
8996           cp_lexer_consume_token (parser->lexer);
8997           cp_parser_enclosed_template_argument_list (parser);
8998           /* Skip tokens until we find a good place from which to
8999              continue parsing.  */
9000           cp_parser_skip_to_closing_parenthesis (parser,
9001                                                  /*recovering=*/true,
9002                                                  /*or_comma=*/true,
9003                                                  /*consume_paren=*/false);
9004           /* If parsing tentatively, permanently remove the
9005              template argument list.  That will prevent duplicate
9006              error messages from being issued about the missing
9007              "template" keyword.  */
9008           if (start)
9009             cp_lexer_purge_tokens_after (parser->lexer, start);
9010           if (is_identifier)
9011             *is_identifier = true;
9012           return identifier;
9013         }
9014
9015       /* If the "template" keyword is present, then there is generally
9016          no point in doing name-lookup, so we just return IDENTIFIER.
9017          But, if the qualifying scope is non-dependent then we can
9018          (and must) do name-lookup normally.  */
9019       if (template_keyword_p
9020           && (!parser->scope
9021               || (TYPE_P (parser->scope)
9022                   && dependent_type_p (parser->scope))))
9023         return identifier;
9024     }
9025
9026   /* Look up the name.  */
9027   decl = cp_parser_lookup_name (parser, identifier,
9028                                 none_type,
9029                                 /*is_template=*/false,
9030                                 /*is_namespace=*/false,
9031                                 check_dependency_p,
9032                                 /*ambiguous_decls=*/NULL);
9033   decl = maybe_get_template_decl_from_type_decl (decl);
9034
9035   /* If DECL is a template, then the name was a template-name.  */
9036   if (TREE_CODE (decl) == TEMPLATE_DECL)
9037     ;
9038   else
9039     {
9040       tree fn = NULL_TREE;
9041
9042       /* The standard does not explicitly indicate whether a name that
9043          names a set of overloaded declarations, some of which are
9044          templates, is a template-name.  However, such a name should
9045          be a template-name; otherwise, there is no way to form a
9046          template-id for the overloaded templates.  */
9047       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9048       if (TREE_CODE (fns) == OVERLOAD)
9049         for (fn = fns; fn; fn = OVL_NEXT (fn))
9050           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9051             break;
9052
9053       if (!fn)
9054         {
9055           /* The name does not name a template.  */
9056           cp_parser_error (parser, "expected template-name");
9057           return error_mark_node;
9058         }
9059     }
9060
9061   /* If DECL is dependent, and refers to a function, then just return
9062      its name; we will look it up again during template instantiation.  */
9063   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9064     {
9065       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9066       if (TYPE_P (scope) && dependent_type_p (scope))
9067         return identifier;
9068     }
9069
9070   return decl;
9071 }
9072
9073 /* Parse a template-argument-list.
9074
9075    template-argument-list:
9076      template-argument
9077      template-argument-list , template-argument
9078
9079    Returns a TREE_VEC containing the arguments.  */
9080
9081 static tree
9082 cp_parser_template_argument_list (cp_parser* parser)
9083 {
9084   tree fixed_args[10];
9085   unsigned n_args = 0;
9086   unsigned alloced = 10;
9087   tree *arg_ary = fixed_args;
9088   tree vec;
9089   bool saved_in_template_argument_list_p;
9090   bool saved_ice_p;
9091   bool saved_non_ice_p;
9092
9093   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9094   parser->in_template_argument_list_p = true;
9095   /* Even if the template-id appears in an integral
9096      constant-expression, the contents of the argument list do
9097      not.  */
9098   saved_ice_p = parser->integral_constant_expression_p;
9099   parser->integral_constant_expression_p = false;
9100   saved_non_ice_p = parser->non_integral_constant_expression_p;
9101   parser->non_integral_constant_expression_p = false;
9102   /* Parse the arguments.  */
9103   do
9104     {
9105       tree argument;
9106
9107       if (n_args)
9108         /* Consume the comma.  */
9109         cp_lexer_consume_token (parser->lexer);
9110
9111       /* Parse the template-argument.  */
9112       argument = cp_parser_template_argument (parser);
9113       if (n_args == alloced)
9114         {
9115           alloced *= 2;
9116
9117           if (arg_ary == fixed_args)
9118             {
9119               arg_ary = XNEWVEC (tree, alloced);
9120               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9121             }
9122           else
9123             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9124         }
9125       arg_ary[n_args++] = argument;
9126     }
9127   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9128
9129   vec = make_tree_vec (n_args);
9130
9131   while (n_args--)
9132     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9133
9134   if (arg_ary != fixed_args)
9135     free (arg_ary);
9136   parser->non_integral_constant_expression_p = saved_non_ice_p;
9137   parser->integral_constant_expression_p = saved_ice_p;
9138   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9139   return vec;
9140 }
9141
9142 /* Parse a template-argument.
9143
9144    template-argument:
9145      assignment-expression
9146      type-id
9147      id-expression
9148
9149    The representation is that of an assignment-expression, type-id, or
9150    id-expression -- except that the qualified id-expression is
9151    evaluated, so that the value returned is either a DECL or an
9152    OVERLOAD.
9153
9154    Although the standard says "assignment-expression", it forbids
9155    throw-expressions or assignments in the template argument.
9156    Therefore, we use "conditional-expression" instead.  */
9157
9158 static tree
9159 cp_parser_template_argument (cp_parser* parser)
9160 {
9161   tree argument;
9162   bool template_p;
9163   bool address_p;
9164   bool maybe_type_id = false;
9165   cp_token *token;
9166   cp_id_kind idk;
9167
9168   /* There's really no way to know what we're looking at, so we just
9169      try each alternative in order.
9170
9171        [temp.arg]
9172
9173        In a template-argument, an ambiguity between a type-id and an
9174        expression is resolved to a type-id, regardless of the form of
9175        the corresponding template-parameter.
9176
9177      Therefore, we try a type-id first.  */
9178   cp_parser_parse_tentatively (parser);
9179   argument = cp_parser_type_id (parser);
9180   /* If there was no error parsing the type-id but the next token is a '>>',
9181      we probably found a typo for '> >'. But there are type-id which are
9182      also valid expressions. For instance:
9183
9184      struct X { int operator >> (int); };
9185      template <int V> struct Foo {};
9186      Foo<X () >> 5> r;
9187
9188      Here 'X()' is a valid type-id of a function type, but the user just
9189      wanted to write the expression "X() >> 5". Thus, we remember that we
9190      found a valid type-id, but we still try to parse the argument as an
9191      expression to see what happens.  */
9192   if (!cp_parser_error_occurred (parser)
9193       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9194     {
9195       maybe_type_id = true;
9196       cp_parser_abort_tentative_parse (parser);
9197     }
9198   else
9199     {
9200       /* If the next token isn't a `,' or a `>', then this argument wasn't
9201       really finished. This means that the argument is not a valid
9202       type-id.  */
9203       if (!cp_parser_next_token_ends_template_argument_p (parser))
9204         cp_parser_error (parser, "expected template-argument");
9205       /* If that worked, we're done.  */
9206       if (cp_parser_parse_definitely (parser))
9207         return argument;
9208     }
9209   /* We're still not sure what the argument will be.  */
9210   cp_parser_parse_tentatively (parser);
9211   /* Try a template.  */
9212   argument = cp_parser_id_expression (parser,
9213                                       /*template_keyword_p=*/false,
9214                                       /*check_dependency_p=*/true,
9215                                       &template_p,
9216                                       /*declarator_p=*/false,
9217                                       /*optional_p=*/false,
9218                                       /*member_p=*/false);
9219   /* If the next token isn't a `,' or a `>', then this argument wasn't
9220      really finished.  */
9221   if (!cp_parser_next_token_ends_template_argument_p (parser))
9222     cp_parser_error (parser, "expected template-argument");
9223   if (!cp_parser_error_occurred (parser))
9224     {
9225       /* Figure out what is being referred to.  If the id-expression
9226          was for a class template specialization, then we will have a
9227          TYPE_DECL at this point.  There is no need to do name lookup
9228          at this point in that case.  */
9229       if (TREE_CODE (argument) != TYPE_DECL)
9230         argument = cp_parser_lookup_name (parser, argument,
9231                                           none_type,
9232                                           /*is_template=*/template_p,
9233                                           /*is_namespace=*/false,
9234                                           /*check_dependency=*/true,
9235                                           /*ambiguous_decls=*/NULL);
9236       if (TREE_CODE (argument) != TEMPLATE_DECL
9237           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9238         cp_parser_error (parser, "expected template-name");
9239     }
9240   if (cp_parser_parse_definitely (parser))
9241     return argument;
9242   /* It must be a non-type argument.  There permitted cases are given
9243      in [temp.arg.nontype]:
9244
9245      -- an integral constant-expression of integral or enumeration
9246         type; or
9247
9248      -- the name of a non-type template-parameter; or
9249
9250      -- the name of an object or function with external linkage...
9251
9252      -- the address of an object or function with external linkage...
9253
9254      -- a pointer to member...  */
9255   /* Look for a non-type template parameter.  */
9256   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9257     {
9258       cp_parser_parse_tentatively (parser);
9259       argument = cp_parser_primary_expression (parser,
9260                                                /*adress_p=*/false,
9261                                                /*cast_p=*/false,
9262                                                /*template_arg_p=*/true,
9263                                                &idk);
9264       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9265           || !cp_parser_next_token_ends_template_argument_p (parser))
9266         cp_parser_simulate_error (parser);
9267       if (cp_parser_parse_definitely (parser))
9268         return argument;
9269     }
9270
9271   /* If the next token is "&", the argument must be the address of an
9272      object or function with external linkage.  */
9273   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9274   if (address_p)
9275     cp_lexer_consume_token (parser->lexer);
9276   /* See if we might have an id-expression.  */
9277   token = cp_lexer_peek_token (parser->lexer);
9278   if (token->type == CPP_NAME
9279       || token->keyword == RID_OPERATOR
9280       || token->type == CPP_SCOPE
9281       || token->type == CPP_TEMPLATE_ID
9282       || token->type == CPP_NESTED_NAME_SPECIFIER)
9283     {
9284       cp_parser_parse_tentatively (parser);
9285       argument = cp_parser_primary_expression (parser,
9286                                                address_p,
9287                                                /*cast_p=*/false,
9288                                                /*template_arg_p=*/true,
9289                                                &idk);
9290       if (cp_parser_error_occurred (parser)
9291           || !cp_parser_next_token_ends_template_argument_p (parser))
9292         cp_parser_abort_tentative_parse (parser);
9293       else
9294         {
9295           if (TREE_CODE (argument) == INDIRECT_REF)
9296             {
9297               gcc_assert (REFERENCE_REF_P (argument));
9298               argument = TREE_OPERAND (argument, 0);
9299             }
9300
9301           if (TREE_CODE (argument) == VAR_DECL)
9302             {
9303               /* A variable without external linkage might still be a
9304                  valid constant-expression, so no error is issued here
9305                  if the external-linkage check fails.  */
9306               if (!DECL_EXTERNAL_LINKAGE_P (argument))
9307                 cp_parser_simulate_error (parser);
9308             }
9309           else if (is_overloaded_fn (argument))
9310             /* All overloaded functions are allowed; if the external
9311                linkage test does not pass, an error will be issued
9312                later.  */
9313             ;
9314           else if (address_p
9315                    && (TREE_CODE (argument) == OFFSET_REF
9316                        || TREE_CODE (argument) == SCOPE_REF))
9317             /* A pointer-to-member.  */
9318             ;
9319           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9320             ;
9321           else
9322             cp_parser_simulate_error (parser);
9323
9324           if (cp_parser_parse_definitely (parser))
9325             {
9326               if (address_p)
9327                 argument = build_x_unary_op (ADDR_EXPR, argument);
9328               return argument;
9329             }
9330         }
9331     }
9332   /* If the argument started with "&", there are no other valid
9333      alternatives at this point.  */
9334   if (address_p)
9335     {
9336       cp_parser_error (parser, "invalid non-type template argument");
9337       return error_mark_node;
9338     }
9339
9340   /* If the argument wasn't successfully parsed as a type-id followed
9341      by '>>', the argument can only be a constant expression now.
9342      Otherwise, we try parsing the constant-expression tentatively,
9343      because the argument could really be a type-id.  */
9344   if (maybe_type_id)
9345     cp_parser_parse_tentatively (parser);
9346   argument = cp_parser_constant_expression (parser,
9347                                             /*allow_non_constant_p=*/false,
9348                                             /*non_constant_p=*/NULL);
9349   argument = fold_non_dependent_expr (argument);
9350   if (!maybe_type_id)
9351     return argument;
9352   if (!cp_parser_next_token_ends_template_argument_p (parser))
9353     cp_parser_error (parser, "expected template-argument");
9354   if (cp_parser_parse_definitely (parser))
9355     return argument;
9356   /* We did our best to parse the argument as a non type-id, but that
9357      was the only alternative that matched (albeit with a '>' after
9358      it). We can assume it's just a typo from the user, and a
9359      diagnostic will then be issued.  */
9360   return cp_parser_type_id (parser);
9361 }
9362
9363 /* Parse an explicit-instantiation.
9364
9365    explicit-instantiation:
9366      template declaration
9367
9368    Although the standard says `declaration', what it really means is:
9369
9370    explicit-instantiation:
9371      template decl-specifier-seq [opt] declarator [opt] ;
9372
9373    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9374    supposed to be allowed.  A defect report has been filed about this
9375    issue.
9376
9377    GNU Extension:
9378
9379    explicit-instantiation:
9380      storage-class-specifier template
9381        decl-specifier-seq [opt] declarator [opt] ;
9382      function-specifier template
9383        decl-specifier-seq [opt] declarator [opt] ;  */
9384
9385 static void
9386 cp_parser_explicit_instantiation (cp_parser* parser)
9387 {
9388   int declares_class_or_enum;
9389   cp_decl_specifier_seq decl_specifiers;
9390   tree extension_specifier = NULL_TREE;
9391
9392   /* Look for an (optional) storage-class-specifier or
9393      function-specifier.  */
9394   if (cp_parser_allow_gnu_extensions_p (parser))
9395     {
9396       extension_specifier
9397         = cp_parser_storage_class_specifier_opt (parser);
9398       if (!extension_specifier)
9399         extension_specifier
9400           = cp_parser_function_specifier_opt (parser,
9401                                               /*decl_specs=*/NULL);
9402     }
9403
9404   /* Look for the `template' keyword.  */
9405   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9406   /* Let the front end know that we are processing an explicit
9407      instantiation.  */
9408   begin_explicit_instantiation ();
9409   /* [temp.explicit] says that we are supposed to ignore access
9410      control while processing explicit instantiation directives.  */
9411   push_deferring_access_checks (dk_no_check);
9412   /* Parse a decl-specifier-seq.  */
9413   cp_parser_decl_specifier_seq (parser,
9414                                 CP_PARSER_FLAGS_OPTIONAL,
9415                                 &decl_specifiers,
9416                                 &declares_class_or_enum);
9417   /* If there was exactly one decl-specifier, and it declared a class,
9418      and there's no declarator, then we have an explicit type
9419      instantiation.  */
9420   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9421     {
9422       tree type;
9423
9424       type = check_tag_decl (&decl_specifiers);
9425       /* Turn access control back on for names used during
9426          template instantiation.  */
9427       pop_deferring_access_checks ();
9428       if (type)
9429         do_type_instantiation (type, extension_specifier,
9430                                /*complain=*/tf_error);
9431     }
9432   else
9433     {
9434       cp_declarator *declarator;
9435       tree decl;
9436
9437       /* Parse the declarator.  */
9438       declarator
9439         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9440                                 /*ctor_dtor_or_conv_p=*/NULL,
9441                                 /*parenthesized_p=*/NULL,
9442                                 /*member_p=*/false);
9443       if (declares_class_or_enum & 2)
9444         cp_parser_check_for_definition_in_return_type (declarator,
9445                                                        decl_specifiers.type);
9446       if (declarator != cp_error_declarator)
9447         {
9448           decl = grokdeclarator (declarator, &decl_specifiers,
9449                                  NORMAL, 0, &decl_specifiers.attributes);
9450           /* Turn access control back on for names used during
9451              template instantiation.  */
9452           pop_deferring_access_checks ();
9453           /* Do the explicit instantiation.  */
9454           do_decl_instantiation (decl, extension_specifier);
9455         }
9456       else
9457         {
9458           pop_deferring_access_checks ();
9459           /* Skip the body of the explicit instantiation.  */
9460           cp_parser_skip_to_end_of_statement (parser);
9461         }
9462     }
9463   /* We're done with the instantiation.  */
9464   end_explicit_instantiation ();
9465
9466   cp_parser_consume_semicolon_at_end_of_statement (parser);
9467 }
9468
9469 /* Parse an explicit-specialization.
9470
9471    explicit-specialization:
9472      template < > declaration
9473
9474    Although the standard says `declaration', what it really means is:
9475
9476    explicit-specialization:
9477      template <> decl-specifier [opt] init-declarator [opt] ;
9478      template <> function-definition
9479      template <> explicit-specialization
9480      template <> template-declaration  */
9481
9482 static void
9483 cp_parser_explicit_specialization (cp_parser* parser)
9484 {
9485   bool need_lang_pop;
9486   /* Look for the `template' keyword.  */
9487   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9488   /* Look for the `<'.  */
9489   cp_parser_require (parser, CPP_LESS, "`<'");
9490   /* Look for the `>'.  */
9491   cp_parser_require (parser, CPP_GREATER, "`>'");
9492   /* We have processed another parameter list.  */
9493   ++parser->num_template_parameter_lists;
9494   /* [temp]
9495
9496      A template ... explicit specialization ... shall not have C
9497      linkage.  */
9498   if (current_lang_name == lang_name_c)
9499     {
9500       error ("template specialization with C linkage");
9501       /* Give it C++ linkage to avoid confusing other parts of the
9502          front end.  */
9503       push_lang_context (lang_name_cplusplus);
9504       need_lang_pop = true;
9505     }
9506   else
9507     need_lang_pop = false;
9508   /* Let the front end know that we are beginning a specialization.  */
9509   begin_specialization ();
9510   /* If the next keyword is `template', we need to figure out whether
9511      or not we're looking a template-declaration.  */
9512   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9513     {
9514       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9515           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9516         cp_parser_template_declaration_after_export (parser,
9517                                                      /*member_p=*/false);
9518       else
9519         cp_parser_explicit_specialization (parser);
9520     }
9521   else
9522     /* Parse the dependent declaration.  */
9523     cp_parser_single_declaration (parser,
9524                                   /*checks=*/NULL_TREE,
9525                                   /*member_p=*/false,
9526                                   /*friend_p=*/NULL);
9527   /* We're done with the specialization.  */
9528   end_specialization ();
9529   /* For the erroneous case of a template with C linkage, we pushed an
9530      implicit C++ linkage scope; exit that scope now.  */
9531   if (need_lang_pop)
9532     pop_lang_context ();
9533   /* We're done with this parameter list.  */
9534   --parser->num_template_parameter_lists;
9535 }
9536
9537 /* Parse a type-specifier.
9538
9539    type-specifier:
9540      simple-type-specifier
9541      class-specifier
9542      enum-specifier
9543      elaborated-type-specifier
9544      cv-qualifier
9545
9546    GNU Extension:
9547
9548    type-specifier:
9549      __complex__
9550
9551    Returns a representation of the type-specifier.  For a
9552    class-specifier, enum-specifier, or elaborated-type-specifier, a
9553    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9554
9555    The parser flags FLAGS is used to control type-specifier parsing.
9556
9557    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9558    in a decl-specifier-seq.
9559
9560    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9561    class-specifier, enum-specifier, or elaborated-type-specifier, then
9562    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9563    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9564    zero.
9565
9566    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9567    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9568    is set to FALSE.  */
9569
9570 static tree
9571 cp_parser_type_specifier (cp_parser* parser,
9572                           cp_parser_flags flags,
9573                           cp_decl_specifier_seq *decl_specs,
9574                           bool is_declaration,
9575                           int* declares_class_or_enum,
9576                           bool* is_cv_qualifier)
9577 {
9578   tree type_spec = NULL_TREE;
9579   cp_token *token;
9580   enum rid keyword;
9581   cp_decl_spec ds = ds_last;
9582
9583   /* Assume this type-specifier does not declare a new type.  */
9584   if (declares_class_or_enum)
9585     *declares_class_or_enum = 0;
9586   /* And that it does not specify a cv-qualifier.  */
9587   if (is_cv_qualifier)
9588     *is_cv_qualifier = false;
9589   /* Peek at the next token.  */
9590   token = cp_lexer_peek_token (parser->lexer);
9591
9592   /* If we're looking at a keyword, we can use that to guide the
9593      production we choose.  */
9594   keyword = token->keyword;
9595   switch (keyword)
9596     {
9597     case RID_ENUM:
9598       /* Look for the enum-specifier.  */
9599       type_spec = cp_parser_enum_specifier (parser);
9600       /* If that worked, we're done.  */
9601       if (type_spec)
9602         {
9603           if (declares_class_or_enum)
9604             *declares_class_or_enum = 2;
9605           if (decl_specs)
9606             cp_parser_set_decl_spec_type (decl_specs,
9607                                           type_spec,
9608                                           /*user_defined_p=*/true);
9609           return type_spec;
9610         }
9611       else
9612         goto elaborated_type_specifier;
9613
9614       /* Any of these indicate either a class-specifier, or an
9615          elaborated-type-specifier.  */
9616     case RID_CLASS:
9617     case RID_STRUCT:
9618     case RID_UNION:
9619       /* Parse tentatively so that we can back up if we don't find a
9620          class-specifier.  */
9621       cp_parser_parse_tentatively (parser);
9622       /* Look for the class-specifier.  */
9623       type_spec = cp_parser_class_specifier (parser);
9624       /* If that worked, we're done.  */
9625       if (cp_parser_parse_definitely (parser))
9626         {
9627           if (declares_class_or_enum)
9628             *declares_class_or_enum = 2;
9629           if (decl_specs)
9630             cp_parser_set_decl_spec_type (decl_specs,
9631                                           type_spec,
9632                                           /*user_defined_p=*/true);
9633           return type_spec;
9634         }
9635
9636       /* Fall through.  */
9637     elaborated_type_specifier:
9638       /* We're declaring (not defining) a class or enum.  */
9639       if (declares_class_or_enum)
9640         *declares_class_or_enum = 1;
9641
9642       /* Fall through.  */
9643     case RID_TYPENAME:
9644       /* Look for an elaborated-type-specifier.  */
9645       type_spec
9646         = (cp_parser_elaborated_type_specifier
9647            (parser,
9648             decl_specs && decl_specs->specs[(int) ds_friend],
9649             is_declaration));
9650       if (decl_specs)
9651         cp_parser_set_decl_spec_type (decl_specs,
9652                                       type_spec,
9653                                       /*user_defined_p=*/true);
9654       return type_spec;
9655
9656     case RID_CONST:
9657       ds = ds_const;
9658       if (is_cv_qualifier)
9659         *is_cv_qualifier = true;
9660       break;
9661
9662     case RID_VOLATILE:
9663       ds = ds_volatile;
9664       if (is_cv_qualifier)
9665         *is_cv_qualifier = true;
9666       break;
9667
9668     case RID_RESTRICT:
9669       ds = ds_restrict;
9670       if (is_cv_qualifier)
9671         *is_cv_qualifier = true;
9672       break;
9673
9674     case RID_COMPLEX:
9675       /* The `__complex__' keyword is a GNU extension.  */
9676       ds = ds_complex;
9677       break;
9678
9679     default:
9680       break;
9681     }
9682
9683   /* Handle simple keywords.  */
9684   if (ds != ds_last)
9685     {
9686       if (decl_specs)
9687         {
9688           ++decl_specs->specs[(int)ds];
9689           decl_specs->any_specifiers_p = true;
9690         }
9691       return cp_lexer_consume_token (parser->lexer)->value;
9692     }
9693
9694   /* If we do not already have a type-specifier, assume we are looking
9695      at a simple-type-specifier.  */
9696   type_spec = cp_parser_simple_type_specifier (parser,
9697                                                decl_specs,
9698                                                flags);
9699
9700   /* If we didn't find a type-specifier, and a type-specifier was not
9701      optional in this context, issue an error message.  */
9702   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9703     {
9704       cp_parser_error (parser, "expected type specifier");
9705       return error_mark_node;
9706     }
9707
9708   return type_spec;
9709 }
9710
9711 /* Parse a simple-type-specifier.
9712
9713    simple-type-specifier:
9714      :: [opt] nested-name-specifier [opt] type-name
9715      :: [opt] nested-name-specifier template template-id
9716      char
9717      wchar_t
9718      bool
9719      short
9720      int
9721      long
9722      signed
9723      unsigned
9724      float
9725      double
9726      void
9727
9728    GNU Extension:
9729
9730    simple-type-specifier:
9731      __typeof__ unary-expression
9732      __typeof__ ( type-id )
9733
9734    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9735    appropriately updated.  */
9736
9737 static tree
9738 cp_parser_simple_type_specifier (cp_parser* parser,
9739                                  cp_decl_specifier_seq *decl_specs,
9740                                  cp_parser_flags flags)
9741 {
9742   tree type = NULL_TREE;
9743   cp_token *token;
9744
9745   /* Peek at the next token.  */
9746   token = cp_lexer_peek_token (parser->lexer);
9747
9748   /* If we're looking at a keyword, things are easy.  */
9749   switch (token->keyword)
9750     {
9751     case RID_CHAR:
9752       if (decl_specs)
9753         decl_specs->explicit_char_p = true;
9754       type = char_type_node;
9755       break;
9756     case RID_WCHAR:
9757       type = wchar_type_node;
9758       break;
9759     case RID_BOOL:
9760       type = boolean_type_node;
9761       break;
9762     case RID_SHORT:
9763       if (decl_specs)
9764         ++decl_specs->specs[(int) ds_short];
9765       type = short_integer_type_node;
9766       break;
9767     case RID_INT:
9768       if (decl_specs)
9769         decl_specs->explicit_int_p = true;
9770       type = integer_type_node;
9771       break;
9772     case RID_LONG:
9773       if (decl_specs)
9774         ++decl_specs->specs[(int) ds_long];
9775       type = long_integer_type_node;
9776       break;
9777     case RID_SIGNED:
9778       if (decl_specs)
9779         ++decl_specs->specs[(int) ds_signed];
9780       type = integer_type_node;
9781       break;
9782     case RID_UNSIGNED:
9783       if (decl_specs)
9784         ++decl_specs->specs[(int) ds_unsigned];
9785       type = unsigned_type_node;
9786       break;
9787     case RID_FLOAT:
9788       type = float_type_node;
9789       break;
9790     case RID_DOUBLE:
9791       type = double_type_node;
9792       break;
9793     case RID_VOID:
9794       type = void_type_node;
9795       break;
9796
9797     case RID_TYPEOF:
9798       /* Consume the `typeof' token.  */
9799       cp_lexer_consume_token (parser->lexer);
9800       /* Parse the operand to `typeof'.  */
9801       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9802       /* If it is not already a TYPE, take its type.  */
9803       if (!TYPE_P (type))
9804         type = finish_typeof (type);
9805
9806       if (decl_specs)
9807         cp_parser_set_decl_spec_type (decl_specs, type,
9808                                       /*user_defined_p=*/true);
9809
9810       return type;
9811
9812     default:
9813       break;
9814     }
9815
9816   /* If the type-specifier was for a built-in type, we're done.  */
9817   if (type)
9818     {
9819       tree id;
9820
9821       /* Record the type.  */
9822       if (decl_specs
9823           && (token->keyword != RID_SIGNED
9824               && token->keyword != RID_UNSIGNED
9825               && token->keyword != RID_SHORT
9826               && token->keyword != RID_LONG))
9827         cp_parser_set_decl_spec_type (decl_specs,
9828                                       type,
9829                                       /*user_defined=*/false);
9830       if (decl_specs)
9831         decl_specs->any_specifiers_p = true;
9832
9833       /* Consume the token.  */
9834       id = cp_lexer_consume_token (parser->lexer)->value;
9835
9836       /* There is no valid C++ program where a non-template type is
9837          followed by a "<".  That usually indicates that the user thought
9838          that the type was a template.  */
9839       cp_parser_check_for_invalid_template_id (parser, type);
9840
9841       return TYPE_NAME (type);
9842     }
9843
9844   /* The type-specifier must be a user-defined type.  */
9845   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9846     {
9847       bool qualified_p;
9848       bool global_p;
9849
9850       /* Don't gobble tokens or issue error messages if this is an
9851          optional type-specifier.  */
9852       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9853         cp_parser_parse_tentatively (parser);
9854
9855       /* Look for the optional `::' operator.  */
9856       global_p
9857         = (cp_parser_global_scope_opt (parser,
9858                                        /*current_scope_valid_p=*/false,
9859                                        /*object_scope_valid_p=*/false)
9860            != NULL_TREE);
9861       /* Look for the nested-name specifier.  */
9862       qualified_p
9863         = (cp_parser_nested_name_specifier_opt (parser,
9864                                                 /*typename_keyword_p=*/false,
9865                                                 /*check_dependency_p=*/true,
9866                                                 /*type_p=*/false,
9867                                                 /*is_declaration=*/false)
9868            != NULL_TREE);
9869       /* If we have seen a nested-name-specifier, and the next token
9870          is `template', then we are using the template-id production.  */
9871       if (parser->scope
9872           && cp_parser_optional_template_keyword (parser))
9873         {
9874           /* Look for the template-id.  */
9875           type = cp_parser_template_id (parser,
9876                                         /*template_keyword_p=*/true,
9877                                         /*check_dependency_p=*/true,
9878                                         /*is_declaration=*/false);
9879           /* If the template-id did not name a type, we are out of
9880              luck.  */
9881           if (TREE_CODE (type) != TYPE_DECL)
9882             {
9883               cp_parser_error (parser, "expected template-id for type");
9884               type = NULL_TREE;
9885             }
9886         }
9887       /* Otherwise, look for a type-name.  */
9888       else
9889         type = cp_parser_type_name (parser);
9890       /* Keep track of all name-lookups performed in class scopes.  */
9891       if (type
9892           && !global_p
9893           && !qualified_p
9894           && TREE_CODE (type) == TYPE_DECL
9895           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9896         maybe_note_name_used_in_class (DECL_NAME (type), type);
9897       /* If it didn't work out, we don't have a TYPE.  */
9898       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9899           && !cp_parser_parse_definitely (parser))
9900         type = NULL_TREE;
9901       if (type && decl_specs)
9902         cp_parser_set_decl_spec_type (decl_specs, type,
9903                                       /*user_defined=*/true);
9904     }
9905
9906   /* If we didn't get a type-name, issue an error message.  */
9907   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9908     {
9909       cp_parser_error (parser, "expected type-name");
9910       return error_mark_node;
9911     }
9912
9913   /* There is no valid C++ program where a non-template type is
9914      followed by a "<".  That usually indicates that the user thought
9915      that the type was a template.  */
9916   if (type && type != error_mark_node)
9917     {
9918       /* As a last-ditch effort, see if TYPE is an Objective-C type.
9919          If it is, then the '<'...'>' enclose protocol names rather than
9920          template arguments, and so everything is fine.  */
9921       if (c_dialect_objc ()
9922           && (objc_is_id (type) || objc_is_class_name (type)))
9923         {
9924           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9925           tree qual_type = objc_get_protocol_qualified_type (type, protos);
9926
9927           /* Clobber the "unqualified" type previously entered into
9928              DECL_SPECS with the new, improved protocol-qualified version.  */
9929           if (decl_specs)
9930             decl_specs->type = qual_type;
9931
9932           return qual_type;
9933         }
9934
9935       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9936     }
9937
9938   return type;
9939 }
9940
9941 /* Parse a type-name.
9942
9943    type-name:
9944      class-name
9945      enum-name
9946      typedef-name
9947
9948    enum-name:
9949      identifier
9950
9951    typedef-name:
9952      identifier
9953
9954    Returns a TYPE_DECL for the type.  */
9955
9956 static tree
9957 cp_parser_type_name (cp_parser* parser)
9958 {
9959   tree type_decl;
9960   tree identifier;
9961
9962   /* We can't know yet whether it is a class-name or not.  */
9963   cp_parser_parse_tentatively (parser);
9964   /* Try a class-name.  */
9965   type_decl = cp_parser_class_name (parser,
9966                                     /*typename_keyword_p=*/false,
9967                                     /*template_keyword_p=*/false,
9968                                     none_type,
9969                                     /*check_dependency_p=*/true,
9970                                     /*class_head_p=*/false,
9971                                     /*is_declaration=*/false);
9972   /* If it's not a class-name, keep looking.  */
9973   if (!cp_parser_parse_definitely (parser))
9974     {
9975       /* It must be a typedef-name or an enum-name.  */
9976       identifier = cp_parser_identifier (parser);
9977       if (identifier == error_mark_node)
9978         return error_mark_node;
9979
9980       /* Look up the type-name.  */
9981       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9982
9983       if (TREE_CODE (type_decl) != TYPE_DECL
9984           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9985         {
9986           /* See if this is an Objective-C type.  */
9987           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9988           tree type = objc_get_protocol_qualified_type (identifier, protos);
9989           if (type)
9990             type_decl = TYPE_NAME (type);
9991         }
9992
9993       /* Issue an error if we did not find a type-name.  */
9994       if (TREE_CODE (type_decl) != TYPE_DECL)
9995         {
9996           if (!cp_parser_simulate_error (parser))
9997             cp_parser_name_lookup_error (parser, identifier, type_decl,
9998                                          "is not a type");
9999           type_decl = error_mark_node;
10000         }
10001       /* Remember that the name was used in the definition of the
10002          current class so that we can check later to see if the
10003          meaning would have been different after the class was
10004          entirely defined.  */
10005       else if (type_decl != error_mark_node
10006                && !parser->scope)
10007         maybe_note_name_used_in_class (identifier, type_decl);
10008     }
10009
10010   return type_decl;
10011 }
10012
10013
10014 /* Parse an elaborated-type-specifier.  Note that the grammar given
10015    here incorporates the resolution to DR68.
10016
10017    elaborated-type-specifier:
10018      class-key :: [opt] nested-name-specifier [opt] identifier
10019      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10020      enum :: [opt] nested-name-specifier [opt] identifier
10021      typename :: [opt] nested-name-specifier identifier
10022      typename :: [opt] nested-name-specifier template [opt]
10023        template-id
10024
10025    GNU extension:
10026
10027    elaborated-type-specifier:
10028      class-key attributes :: [opt] nested-name-specifier [opt] identifier
10029      class-key attributes :: [opt] nested-name-specifier [opt]
10030                template [opt] template-id
10031      enum attributes :: [opt] nested-name-specifier [opt] identifier
10032
10033    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10034    declared `friend'.  If IS_DECLARATION is TRUE, then this
10035    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10036    something is being declared.
10037
10038    Returns the TYPE specified.  */
10039
10040 static tree
10041 cp_parser_elaborated_type_specifier (cp_parser* parser,
10042                                      bool is_friend,
10043                                      bool is_declaration)
10044 {
10045   enum tag_types tag_type;
10046   tree identifier;
10047   tree type = NULL_TREE;
10048   tree attributes = NULL_TREE;
10049
10050   /* See if we're looking at the `enum' keyword.  */
10051   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10052     {
10053       /* Consume the `enum' token.  */
10054       cp_lexer_consume_token (parser->lexer);
10055       /* Remember that it's an enumeration type.  */
10056       tag_type = enum_type;
10057       /* Parse the attributes.  */
10058       attributes = cp_parser_attributes_opt (parser);
10059     }
10060   /* Or, it might be `typename'.  */
10061   else if (cp_lexer_next_token_is_keyword (parser->lexer,
10062                                            RID_TYPENAME))
10063     {
10064       /* Consume the `typename' token.  */
10065       cp_lexer_consume_token (parser->lexer);
10066       /* Remember that it's a `typename' type.  */
10067       tag_type = typename_type;
10068       /* The `typename' keyword is only allowed in templates.  */
10069       if (!processing_template_decl)
10070         pedwarn ("using %<typename%> outside of template");
10071     }
10072   /* Otherwise it must be a class-key.  */
10073   else
10074     {
10075       tag_type = cp_parser_class_key (parser);
10076       if (tag_type == none_type)
10077         return error_mark_node;
10078       /* Parse the attributes.  */
10079       attributes = cp_parser_attributes_opt (parser);
10080     }
10081
10082   /* Look for the `::' operator.  */
10083   cp_parser_global_scope_opt (parser,
10084                               /*current_scope_valid_p=*/false,
10085                               /*object_scope_valid_p=*/false);
10086   /* Look for the nested-name-specifier.  */
10087   if (tag_type == typename_type)
10088     {
10089       if (!cp_parser_nested_name_specifier (parser,
10090                                            /*typename_keyword_p=*/true,
10091                                            /*check_dependency_p=*/true,
10092                                            /*type_p=*/true,
10093                                             is_declaration))
10094         return error_mark_node;
10095     }
10096   else
10097     /* Even though `typename' is not present, the proposed resolution
10098        to Core Issue 180 says that in `class A<T>::B', `B' should be
10099        considered a type-name, even if `A<T>' is dependent.  */
10100     cp_parser_nested_name_specifier_opt (parser,
10101                                          /*typename_keyword_p=*/true,
10102                                          /*check_dependency_p=*/true,
10103                                          /*type_p=*/true,
10104                                          is_declaration);
10105   /* For everything but enumeration types, consider a template-id.  */
10106   /* For an enumeration type, consider only a plain identifier.  */
10107   if (tag_type != enum_type)
10108     {
10109       bool template_p = false;
10110       tree decl;
10111
10112       /* Allow the `template' keyword.  */
10113       template_p = cp_parser_optional_template_keyword (parser);
10114       /* If we didn't see `template', we don't know if there's a
10115          template-id or not.  */
10116       if (!template_p)
10117         cp_parser_parse_tentatively (parser);
10118       /* Parse the template-id.  */
10119       decl = cp_parser_template_id (parser, template_p,
10120                                     /*check_dependency_p=*/true,
10121                                     is_declaration);
10122       /* If we didn't find a template-id, look for an ordinary
10123          identifier.  */
10124       if (!template_p && !cp_parser_parse_definitely (parser))
10125         ;
10126       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10127          in effect, then we must assume that, upon instantiation, the
10128          template will correspond to a class.  */
10129       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10130                && tag_type == typename_type)
10131         type = make_typename_type (parser->scope, decl,
10132                                    typename_type,
10133                                    /*complain=*/tf_error);
10134       else
10135         type = TREE_TYPE (decl);
10136     }
10137
10138   if (!type)
10139     {
10140       identifier = cp_parser_identifier (parser);
10141
10142       if (identifier == error_mark_node)
10143         {
10144           parser->scope = NULL_TREE;
10145           return error_mark_node;
10146         }
10147
10148       /* For a `typename', we needn't call xref_tag.  */
10149       if (tag_type == typename_type
10150           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10151         return cp_parser_make_typename_type (parser, parser->scope,
10152                                              identifier);
10153       /* Look up a qualified name in the usual way.  */
10154       if (parser->scope)
10155         {
10156           tree decl;
10157
10158           decl = cp_parser_lookup_name (parser, identifier,
10159                                         tag_type,
10160                                         /*is_template=*/false,
10161                                         /*is_namespace=*/false,
10162                                         /*check_dependency=*/true,
10163                                         /*ambiguous_decls=*/NULL);
10164
10165           /* If we are parsing friend declaration, DECL may be a
10166              TEMPLATE_DECL tree node here.  However, we need to check
10167              whether this TEMPLATE_DECL results in valid code.  Consider
10168              the following example:
10169
10170                namespace N {
10171                  template <class T> class C {};
10172                }
10173                class X {
10174                  template <class T> friend class N::C; // #1, valid code
10175                };
10176                template <class T> class Y {
10177                  friend class N::C;                    // #2, invalid code
10178                };
10179
10180              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10181              name lookup of `N::C'.  We see that friend declaration must
10182              be template for the code to be valid.  Note that
10183              processing_template_decl does not work here since it is
10184              always 1 for the above two cases.  */
10185
10186           decl = (cp_parser_maybe_treat_template_as_class
10187                   (decl, /*tag_name_p=*/is_friend
10188                          && parser->num_template_parameter_lists));
10189
10190           if (TREE_CODE (decl) != TYPE_DECL)
10191             {
10192               cp_parser_diagnose_invalid_type_name (parser,
10193                                                     parser->scope,
10194                                                     identifier);
10195               return error_mark_node;
10196             }
10197
10198           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10199             check_elaborated_type_specifier
10200               (tag_type, decl,
10201                (parser->num_template_parameter_lists
10202                 || DECL_SELF_REFERENCE_P (decl)));
10203
10204           type = TREE_TYPE (decl);
10205         }
10206       else
10207         {
10208           /* An elaborated-type-specifier sometimes introduces a new type and
10209              sometimes names an existing type.  Normally, the rule is that it
10210              introduces a new type only if there is not an existing type of
10211              the same name already in scope.  For example, given:
10212
10213                struct S {};
10214                void f() { struct S s; }
10215
10216              the `struct S' in the body of `f' is the same `struct S' as in
10217              the global scope; the existing definition is used.  However, if
10218              there were no global declaration, this would introduce a new
10219              local class named `S'.
10220
10221              An exception to this rule applies to the following code:
10222
10223                namespace N { struct S; }
10224
10225              Here, the elaborated-type-specifier names a new type
10226              unconditionally; even if there is already an `S' in the
10227              containing scope this declaration names a new type.
10228              This exception only applies if the elaborated-type-specifier
10229              forms the complete declaration:
10230
10231                [class.name]
10232
10233                A declaration consisting solely of `class-key identifier ;' is
10234                either a redeclaration of the name in the current scope or a
10235                forward declaration of the identifier as a class name.  It
10236                introduces the name into the current scope.
10237
10238              We are in this situation precisely when the next token is a `;'.
10239
10240              An exception to the exception is that a `friend' declaration does
10241              *not* name a new type; i.e., given:
10242
10243                struct S { friend struct T; };
10244
10245              `T' is not a new type in the scope of `S'.
10246
10247              Also, `new struct S' or `sizeof (struct S)' never results in the
10248              definition of a new type; a new type can only be declared in a
10249              declaration context.  */
10250
10251           tag_scope ts;
10252           bool template_p;
10253
10254           if (is_friend)
10255             /* Friends have special name lookup rules.  */
10256             ts = ts_within_enclosing_non_class;
10257           else if (is_declaration
10258                    && cp_lexer_next_token_is (parser->lexer,
10259                                               CPP_SEMICOLON))
10260             /* This is a `class-key identifier ;' */
10261             ts = ts_current;
10262           else
10263             ts = ts_global;
10264
10265           template_p =
10266             (parser->num_template_parameter_lists
10267              && (cp_parser_next_token_starts_class_definition_p (parser)
10268                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10269           /* An unqualified name was used to reference this type, so
10270              there were no qualifying templates.  */
10271           if (!cp_parser_check_template_parameters (parser,
10272                                                     /*num_templates=*/0))
10273             return error_mark_node;
10274           type = xref_tag (tag_type, identifier, ts, template_p);
10275         }
10276     }
10277
10278   if (type == error_mark_node)
10279     return error_mark_node;
10280
10281   /* Allow attributes on forward declarations of classes.  */
10282   if (attributes)
10283     {
10284       if (TREE_CODE (type) == TYPENAME_TYPE)
10285         warning (OPT_Wattributes,
10286                  "attributes ignored on uninstantiated type");
10287       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10288                && ! processing_explicit_instantiation)
10289         warning (OPT_Wattributes,
10290                  "attributes ignored on template instantiation");
10291       else if (is_declaration && cp_parser_declares_only_class_p (parser))
10292         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10293       else
10294         warning (OPT_Wattributes,
10295                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10296     }
10297
10298   if (tag_type != enum_type)
10299     cp_parser_check_class_key (tag_type, type);
10300
10301   /* A "<" cannot follow an elaborated type specifier.  If that
10302      happens, the user was probably trying to form a template-id.  */
10303   cp_parser_check_for_invalid_template_id (parser, type);
10304
10305   return type;
10306 }
10307
10308 /* Parse an enum-specifier.
10309
10310    enum-specifier:
10311      enum identifier [opt] { enumerator-list [opt] }
10312
10313    GNU Extensions:
10314      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10315        attributes[opt]
10316
10317    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10318    if the token stream isn't an enum-specifier after all.  */
10319
10320 static tree
10321 cp_parser_enum_specifier (cp_parser* parser)
10322 {
10323   tree identifier;
10324   tree type;
10325   tree attributes;
10326
10327   /* Parse tentatively so that we can back up if we don't find a
10328      enum-specifier.  */
10329   cp_parser_parse_tentatively (parser);
10330
10331   /* Caller guarantees that the current token is 'enum', an identifier
10332      possibly follows, and the token after that is an opening brace.
10333      If we don't have an identifier, fabricate an anonymous name for
10334      the enumeration being defined.  */
10335   cp_lexer_consume_token (parser->lexer);
10336
10337   attributes = cp_parser_attributes_opt (parser);
10338
10339   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10340     identifier = cp_parser_identifier (parser);
10341   else
10342     identifier = make_anon_name ();
10343
10344   /* Look for the `{' but don't consume it yet.  */
10345   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10346     cp_parser_simulate_error (parser);
10347
10348   if (!cp_parser_parse_definitely (parser))
10349     return NULL_TREE;
10350
10351   /* Issue an error message if type-definitions are forbidden here.  */
10352   cp_parser_check_type_definition (parser);
10353
10354   /* Create the new type.  We do this before consuming the opening brace
10355      so the enum will be recorded as being on the line of its tag (or the
10356      'enum' keyword, if there is no tag).  */
10357   type = start_enum (identifier);
10358
10359   /* Consume the opening brace.  */
10360   cp_lexer_consume_token (parser->lexer);
10361
10362   if (type == error_mark_node)
10363     {
10364       cp_parser_skip_to_end_of_block_or_statement (parser);
10365       return error_mark_node;
10366     }
10367
10368   /* If the next token is not '}', then there are some enumerators.  */
10369   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10370     cp_parser_enumerator_list (parser, type);
10371
10372   /* Consume the final '}'.  */
10373   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10374
10375   /* Look for trailing attributes to apply to this enumeration, and
10376      apply them if appropriate.  */
10377   if (cp_parser_allow_gnu_extensions_p (parser))
10378     {
10379       tree trailing_attr = cp_parser_attributes_opt (parser);
10380       cplus_decl_attributes (&type,
10381                              trailing_attr,
10382                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10383     }
10384
10385   /* Finish up the enumeration.  */
10386   finish_enum (type);
10387
10388   return type;
10389 }
10390
10391 /* Parse an enumerator-list.  The enumerators all have the indicated
10392    TYPE.
10393
10394    enumerator-list:
10395      enumerator-definition
10396      enumerator-list , enumerator-definition  */
10397
10398 static void
10399 cp_parser_enumerator_list (cp_parser* parser, tree type)
10400 {
10401   while (true)
10402     {
10403       /* Parse an enumerator-definition.  */
10404       cp_parser_enumerator_definition (parser, type);
10405
10406       /* If the next token is not a ',', we've reached the end of
10407          the list.  */
10408       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10409         break;
10410       /* Otherwise, consume the `,' and keep going.  */
10411       cp_lexer_consume_token (parser->lexer);
10412       /* If the next token is a `}', there is a trailing comma.  */
10413       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10414         {
10415           if (pedantic && !in_system_header)
10416             pedwarn ("comma at end of enumerator list");
10417           break;
10418         }
10419     }
10420 }
10421
10422 /* Parse an enumerator-definition.  The enumerator has the indicated
10423    TYPE.
10424
10425    enumerator-definition:
10426      enumerator
10427      enumerator = constant-expression
10428
10429    enumerator:
10430      identifier  */
10431
10432 static void
10433 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10434 {
10435   tree identifier;
10436   tree value;
10437
10438   /* Look for the identifier.  */
10439   identifier = cp_parser_identifier (parser);
10440   if (identifier == error_mark_node)
10441     return;
10442
10443   /* If the next token is an '=', then there is an explicit value.  */
10444   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10445     {
10446       /* Consume the `=' token.  */
10447       cp_lexer_consume_token (parser->lexer);
10448       /* Parse the value.  */
10449       value = cp_parser_constant_expression (parser,
10450                                              /*allow_non_constant_p=*/false,
10451                                              NULL);
10452     }
10453   else
10454     value = NULL_TREE;
10455
10456   /* Create the enumerator.  */
10457   build_enumerator (identifier, value, type);
10458 }
10459
10460 /* Parse a namespace-name.
10461
10462    namespace-name:
10463      original-namespace-name
10464      namespace-alias
10465
10466    Returns the NAMESPACE_DECL for the namespace.  */
10467
10468 static tree
10469 cp_parser_namespace_name (cp_parser* parser)
10470 {
10471   tree identifier;
10472   tree namespace_decl;
10473
10474   /* Get the name of the namespace.  */
10475   identifier = cp_parser_identifier (parser);
10476   if (identifier == error_mark_node)
10477     return error_mark_node;
10478
10479   /* Look up the identifier in the currently active scope.  Look only
10480      for namespaces, due to:
10481
10482        [basic.lookup.udir]
10483
10484        When looking up a namespace-name in a using-directive or alias
10485        definition, only namespace names are considered.
10486
10487      And:
10488
10489        [basic.lookup.qual]
10490
10491        During the lookup of a name preceding the :: scope resolution
10492        operator, object, function, and enumerator names are ignored.
10493
10494      (Note that cp_parser_class_or_namespace_name only calls this
10495      function if the token after the name is the scope resolution
10496      operator.)  */
10497   namespace_decl = cp_parser_lookup_name (parser, identifier,
10498                                           none_type,
10499                                           /*is_template=*/false,
10500                                           /*is_namespace=*/true,
10501                                           /*check_dependency=*/true,
10502                                           /*ambiguous_decls=*/NULL);
10503   /* If it's not a namespace, issue an error.  */
10504   if (namespace_decl == error_mark_node
10505       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10506     {
10507       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10508         error ("%qD is not a namespace-name", identifier);
10509       cp_parser_error (parser, "expected namespace-name");
10510       namespace_decl = error_mark_node;
10511     }
10512
10513   return namespace_decl;
10514 }
10515
10516 /* Parse a namespace-definition.
10517
10518    namespace-definition:
10519      named-namespace-definition
10520      unnamed-namespace-definition
10521
10522    named-namespace-definition:
10523      original-namespace-definition
10524      extension-namespace-definition
10525
10526    original-namespace-definition:
10527      namespace identifier { namespace-body }
10528
10529    extension-namespace-definition:
10530      namespace original-namespace-name { namespace-body }
10531
10532    unnamed-namespace-definition:
10533      namespace { namespace-body } */
10534
10535 static void
10536 cp_parser_namespace_definition (cp_parser* parser)
10537 {
10538   tree identifier, attribs;
10539
10540   /* Look for the `namespace' keyword.  */
10541   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10542
10543   /* Get the name of the namespace.  We do not attempt to distinguish
10544      between an original-namespace-definition and an
10545      extension-namespace-definition at this point.  The semantic
10546      analysis routines are responsible for that.  */
10547   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10548     identifier = cp_parser_identifier (parser);
10549   else
10550     identifier = NULL_TREE;
10551
10552   /* Parse any specified attributes.  */
10553   attribs = cp_parser_attributes_opt (parser);
10554
10555   /* Look for the `{' to start the namespace.  */
10556   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10557   /* Start the namespace.  */
10558   push_namespace_with_attribs (identifier, attribs);
10559   /* Parse the body of the namespace.  */
10560   cp_parser_namespace_body (parser);
10561   /* Finish the namespace.  */
10562   pop_namespace ();
10563   /* Look for the final `}'.  */
10564   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10565 }
10566
10567 /* Parse a namespace-body.
10568
10569    namespace-body:
10570      declaration-seq [opt]  */
10571
10572 static void
10573 cp_parser_namespace_body (cp_parser* parser)
10574 {
10575   cp_parser_declaration_seq_opt (parser);
10576 }
10577
10578 /* Parse a namespace-alias-definition.
10579
10580    namespace-alias-definition:
10581      namespace identifier = qualified-namespace-specifier ;  */
10582
10583 static void
10584 cp_parser_namespace_alias_definition (cp_parser* parser)
10585 {
10586   tree identifier;
10587   tree namespace_specifier;
10588
10589   /* Look for the `namespace' keyword.  */
10590   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10591   /* Look for the identifier.  */
10592   identifier = cp_parser_identifier (parser);
10593   if (identifier == error_mark_node)
10594     return;
10595   /* Look for the `=' token.  */
10596   cp_parser_require (parser, CPP_EQ, "`='");
10597   /* Look for the qualified-namespace-specifier.  */
10598   namespace_specifier
10599     = cp_parser_qualified_namespace_specifier (parser);
10600   /* Look for the `;' token.  */
10601   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10602
10603   /* Register the alias in the symbol table.  */
10604   do_namespace_alias (identifier, namespace_specifier);
10605 }
10606
10607 /* Parse a qualified-namespace-specifier.
10608
10609    qualified-namespace-specifier:
10610      :: [opt] nested-name-specifier [opt] namespace-name
10611
10612    Returns a NAMESPACE_DECL corresponding to the specified
10613    namespace.  */
10614
10615 static tree
10616 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10617 {
10618   /* Look for the optional `::'.  */
10619   cp_parser_global_scope_opt (parser,
10620                               /*current_scope_valid_p=*/false,
10621                               /*object_scope_valid_p=*/false);
10622
10623   /* Look for the optional nested-name-specifier.  */
10624   cp_parser_nested_name_specifier_opt (parser,
10625                                        /*typename_keyword_p=*/false,
10626                                        /*check_dependency_p=*/true,
10627                                        /*type_p=*/false,
10628                                        /*is_declaration=*/true);
10629
10630   return cp_parser_namespace_name (parser);
10631 }
10632
10633 /* Parse a using-declaration.
10634
10635    using-declaration:
10636      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10637      using :: unqualified-id ;  */
10638
10639 static void
10640 cp_parser_using_declaration (cp_parser* parser)
10641 {
10642   cp_token *token;
10643   bool typename_p = false;
10644   bool global_scope_p;
10645   tree decl;
10646   tree identifier;
10647   tree qscope;
10648
10649   /* Look for the `using' keyword.  */
10650   cp_parser_require_keyword (parser, RID_USING, "`using'");
10651
10652   /* Peek at the next token.  */
10653   token = cp_lexer_peek_token (parser->lexer);
10654   /* See if it's `typename'.  */
10655   if (token->keyword == RID_TYPENAME)
10656     {
10657       /* Remember that we've seen it.  */
10658       typename_p = true;
10659       /* Consume the `typename' token.  */
10660       cp_lexer_consume_token (parser->lexer);
10661     }
10662
10663   /* Look for the optional global scope qualification.  */
10664   global_scope_p
10665     = (cp_parser_global_scope_opt (parser,
10666                                    /*current_scope_valid_p=*/false,
10667                                    /*object_scope_valid_p=*/false)
10668        != NULL_TREE);
10669
10670   /* If we saw `typename', or didn't see `::', then there must be a
10671      nested-name-specifier present.  */
10672   if (typename_p || !global_scope_p)
10673     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10674                                               /*check_dependency_p=*/true,
10675                                               /*type_p=*/false,
10676                                               /*is_declaration=*/true);
10677   /* Otherwise, we could be in either of the two productions.  In that
10678      case, treat the nested-name-specifier as optional.  */
10679   else
10680     qscope = cp_parser_nested_name_specifier_opt (parser,
10681                                                   /*typename_keyword_p=*/false,
10682                                                   /*check_dependency_p=*/true,
10683                                                   /*type_p=*/false,
10684                                                   /*is_declaration=*/true);
10685   if (!qscope)
10686     qscope = global_namespace;
10687
10688   /* Parse the unqualified-id.  */
10689   identifier = cp_parser_unqualified_id (parser,
10690                                          /*template_keyword_p=*/false,
10691                                          /*check_dependency_p=*/true,
10692                                          /*declarator_p=*/true,
10693                                          /*optional_p=*/false,
10694                                          /*member_p=*/false);
10695
10696   /* The function we call to handle a using-declaration is different
10697      depending on what scope we are in.  */
10698   if (qscope == error_mark_node || identifier == error_mark_node)
10699     ;
10700   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10701            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10702     /* [namespace.udecl]
10703
10704        A using declaration shall not name a template-id.  */
10705     error ("a template-id may not appear in a using-declaration");
10706   else
10707     {
10708       if (at_class_scope_p ())
10709         {
10710           /* Create the USING_DECL.  */
10711           decl = do_class_using_decl (parser->scope, identifier);
10712           /* Add it to the list of members in this class.  */
10713           finish_member_declaration (decl);
10714         }
10715       else
10716         {
10717           decl = cp_parser_lookup_name_simple (parser, identifier);
10718           if (decl == error_mark_node)
10719             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10720           else if (!at_namespace_scope_p ())
10721             do_local_using_decl (decl, qscope, identifier);
10722           else
10723             do_toplevel_using_decl (decl, qscope, identifier);
10724         }
10725     }
10726
10727   /* Look for the final `;'.  */
10728   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10729 }
10730
10731 /* Parse a using-directive.
10732
10733    using-directive:
10734      using namespace :: [opt] nested-name-specifier [opt]
10735        namespace-name ;  */
10736
10737 static void
10738 cp_parser_using_directive (cp_parser* parser)
10739 {
10740   tree namespace_decl;
10741   tree attribs;
10742
10743   /* Look for the `using' keyword.  */
10744   cp_parser_require_keyword (parser, RID_USING, "`using'");
10745   /* And the `namespace' keyword.  */
10746   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10747   /* Look for the optional `::' operator.  */
10748   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false,
10749                               /*object_scope_valid_p=*/false);
10750   /* And the optional nested-name-specifier.  */
10751   cp_parser_nested_name_specifier_opt (parser,
10752                                        /*typename_keyword_p=*/false,
10753                                        /*check_dependency_p=*/true,
10754                                        /*type_p=*/false,
10755                                        /*is_declaration=*/true);
10756   /* Get the namespace being used.  */
10757   namespace_decl = cp_parser_namespace_name (parser);
10758   /* And any specified attributes.  */
10759   attribs = cp_parser_attributes_opt (parser);
10760   /* Update the symbol table.  */
10761   parse_using_directive (namespace_decl, attribs);
10762   /* Look for the final `;'.  */
10763   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10764 }
10765
10766 /* Parse an asm-definition.
10767
10768    asm-definition:
10769      asm ( string-literal ) ;
10770
10771    GNU Extension:
10772
10773    asm-definition:
10774      asm volatile [opt] ( string-literal ) ;
10775      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10776      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10777                           : asm-operand-list [opt] ) ;
10778      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10779                           : asm-operand-list [opt]
10780                           : asm-operand-list [opt] ) ;  */
10781
10782 static void
10783 cp_parser_asm_definition (cp_parser* parser)
10784 {
10785   tree string;
10786   tree outputs = NULL_TREE;
10787   tree inputs = NULL_TREE;
10788   tree clobbers = NULL_TREE;
10789   tree asm_stmt;
10790   bool volatile_p = false;
10791   bool extended_p = false;
10792
10793   /* Look for the `asm' keyword.  */
10794   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10795   /* See if the next token is `volatile'.  */
10796   if (cp_parser_allow_gnu_extensions_p (parser)
10797       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10798     {
10799       /* Remember that we saw the `volatile' keyword.  */
10800       volatile_p = true;
10801       /* Consume the token.  */
10802       cp_lexer_consume_token (parser->lexer);
10803     }
10804   /* Look for the opening `('.  */
10805   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10806     return;
10807   /* Look for the string.  */
10808   string = cp_parser_string_literal (parser, false, false);
10809   if (string == error_mark_node)
10810     {
10811       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10812                                              /*consume_paren=*/true);
10813       return;
10814     }
10815
10816   /* If we're allowing GNU extensions, check for the extended assembly
10817      syntax.  Unfortunately, the `:' tokens need not be separated by
10818      a space in C, and so, for compatibility, we tolerate that here
10819      too.  Doing that means that we have to treat the `::' operator as
10820      two `:' tokens.  */
10821   if (cp_parser_allow_gnu_extensions_p (parser)
10822       && at_function_scope_p ()
10823       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10824           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10825     {
10826       bool inputs_p = false;
10827       bool clobbers_p = false;
10828
10829       /* The extended syntax was used.  */
10830       extended_p = true;
10831
10832       /* Look for outputs.  */
10833       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10834         {
10835           /* Consume the `:'.  */
10836           cp_lexer_consume_token (parser->lexer);
10837           /* Parse the output-operands.  */
10838           if (cp_lexer_next_token_is_not (parser->lexer,
10839                                           CPP_COLON)
10840               && cp_lexer_next_token_is_not (parser->lexer,
10841                                              CPP_SCOPE)
10842               && cp_lexer_next_token_is_not (parser->lexer,
10843                                              CPP_CLOSE_PAREN))
10844             outputs = cp_parser_asm_operand_list (parser);
10845         }
10846       /* If the next token is `::', there are no outputs, and the
10847          next token is the beginning of the inputs.  */
10848       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10849         /* The inputs are coming next.  */
10850         inputs_p = true;
10851
10852       /* Look for inputs.  */
10853       if (inputs_p
10854           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10855         {
10856           /* Consume the `:' or `::'.  */
10857           cp_lexer_consume_token (parser->lexer);
10858           /* Parse the output-operands.  */
10859           if (cp_lexer_next_token_is_not (parser->lexer,
10860                                           CPP_COLON)
10861               && cp_lexer_next_token_is_not (parser->lexer,
10862                                              CPP_CLOSE_PAREN))
10863             inputs = cp_parser_asm_operand_list (parser);
10864         }
10865       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10866         /* The clobbers are coming next.  */
10867         clobbers_p = true;
10868
10869       /* Look for clobbers.  */
10870       if (clobbers_p
10871           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10872         {
10873           /* Consume the `:' or `::'.  */
10874           cp_lexer_consume_token (parser->lexer);
10875           /* Parse the clobbers.  */
10876           if (cp_lexer_next_token_is_not (parser->lexer,
10877                                           CPP_CLOSE_PAREN))
10878             clobbers = cp_parser_asm_clobber_list (parser);
10879         }
10880     }
10881   /* Look for the closing `)'.  */
10882   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10883     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10884                                            /*consume_paren=*/true);
10885   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10886
10887   /* Create the ASM_EXPR.  */
10888   if (at_function_scope_p ())
10889     {
10890       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10891                                   inputs, clobbers);
10892       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10893       if (!extended_p)
10894         {
10895           tree temp = asm_stmt;
10896           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10897             temp = TREE_OPERAND (temp, 0);
10898
10899           ASM_INPUT_P (temp) = 1;
10900         }
10901     }
10902   else
10903     cgraph_add_asm_node (string);
10904 }
10905
10906 /* Declarators [gram.dcl.decl] */
10907
10908 /* Parse an init-declarator.
10909
10910    init-declarator:
10911      declarator initializer [opt]
10912
10913    GNU Extension:
10914
10915    init-declarator:
10916      declarator asm-specification [opt] attributes [opt] initializer [opt]
10917
10918    function-definition:
10919      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10920        function-body
10921      decl-specifier-seq [opt] declarator function-try-block
10922
10923    GNU Extension:
10924
10925    function-definition:
10926      __extension__ function-definition
10927
10928    The DECL_SPECIFIERS apply to this declarator.  Returns a
10929    representation of the entity declared.  If MEMBER_P is TRUE, then
10930    this declarator appears in a class scope.  The new DECL created by
10931    this declarator is returned.
10932
10933    The CHECKS are access checks that should be performed once we know
10934    what entity is being declared (and, therefore, what classes have
10935    befriended it).
10936
10937    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10938    for a function-definition here as well.  If the declarator is a
10939    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10940    be TRUE upon return.  By that point, the function-definition will
10941    have been completely parsed.
10942
10943    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10944    is FALSE.  */
10945
10946 static tree
10947 cp_parser_init_declarator (cp_parser* parser,
10948                            cp_decl_specifier_seq *decl_specifiers,
10949                            tree checks,
10950                            bool function_definition_allowed_p,
10951                            bool member_p,
10952                            int declares_class_or_enum,
10953                            bool* function_definition_p)
10954 {
10955   cp_token *token;
10956   cp_declarator *declarator;
10957   tree prefix_attributes;
10958   tree attributes;
10959   tree asm_specification;
10960   tree initializer;
10961   tree decl = NULL_TREE;
10962   tree scope;
10963   bool is_initialized;
10964   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
10965      initialized with "= ..", CPP_OPEN_PAREN if initialized with
10966      "(...)".  */
10967   enum cpp_ttype initialization_kind;
10968   bool is_parenthesized_init = false;
10969   bool is_non_constant_init;
10970   int ctor_dtor_or_conv_p;
10971   bool friend_p;
10972   tree pushed_scope = NULL;
10973
10974   /* Gather the attributes that were provided with the
10975      decl-specifiers.  */
10976   prefix_attributes = decl_specifiers->attributes;
10977
10978   /* Assume that this is not the declarator for a function
10979      definition.  */
10980   if (function_definition_p)
10981     *function_definition_p = false;
10982
10983   /* Defer access checks while parsing the declarator; we cannot know
10984      what names are accessible until we know what is being
10985      declared.  */
10986   resume_deferring_access_checks ();
10987
10988   /* Parse the declarator.  */
10989   declarator
10990     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10991                             &ctor_dtor_or_conv_p,
10992                             /*parenthesized_p=*/NULL,
10993                             /*member_p=*/false);
10994   /* Gather up the deferred checks.  */
10995   stop_deferring_access_checks ();
10996
10997   /* If the DECLARATOR was erroneous, there's no need to go
10998      further.  */
10999   if (declarator == cp_error_declarator)
11000     return error_mark_node;
11001
11002   if (declares_class_or_enum & 2)
11003     cp_parser_check_for_definition_in_return_type (declarator,
11004                                                    decl_specifiers->type);
11005
11006   /* Figure out what scope the entity declared by the DECLARATOR is
11007      located in.  `grokdeclarator' sometimes changes the scope, so
11008      we compute it now.  */
11009   scope = get_scope_of_declarator (declarator);
11010
11011   /* If we're allowing GNU extensions, look for an asm-specification
11012      and attributes.  */
11013   if (cp_parser_allow_gnu_extensions_p (parser))
11014     {
11015       /* Look for an asm-specification.  */
11016       asm_specification = cp_parser_asm_specification_opt (parser);
11017       /* And attributes.  */
11018       attributes = cp_parser_attributes_opt (parser);
11019     }
11020   else
11021     {
11022       asm_specification = NULL_TREE;
11023       attributes = NULL_TREE;
11024     }
11025
11026   /* Peek at the next token.  */
11027   token = cp_lexer_peek_token (parser->lexer);
11028   /* Check to see if the token indicates the start of a
11029      function-definition.  */
11030   if (cp_parser_token_starts_function_definition_p (token))
11031     {
11032       if (!function_definition_allowed_p)
11033         {
11034           /* If a function-definition should not appear here, issue an
11035              error message.  */
11036           cp_parser_error (parser,
11037                            "a function-definition is not allowed here");
11038           return error_mark_node;
11039         }
11040       else
11041         {
11042           /* Neither attributes nor an asm-specification are allowed
11043              on a function-definition.  */
11044           if (asm_specification)
11045             error ("an asm-specification is not allowed on a function-definition");
11046           if (attributes)
11047             error ("attributes are not allowed on a function-definition");
11048           /* This is a function-definition.  */
11049           *function_definition_p = true;
11050
11051           /* Parse the function definition.  */
11052           if (member_p)
11053             decl = cp_parser_save_member_function_body (parser,
11054                                                         decl_specifiers,
11055                                                         declarator,
11056                                                         prefix_attributes);
11057           else
11058             decl
11059               = (cp_parser_function_definition_from_specifiers_and_declarator
11060                  (parser, decl_specifiers, prefix_attributes, declarator));
11061
11062           return decl;
11063         }
11064     }
11065
11066   /* [dcl.dcl]
11067
11068      Only in function declarations for constructors, destructors, and
11069      type conversions can the decl-specifier-seq be omitted.
11070
11071      We explicitly postpone this check past the point where we handle
11072      function-definitions because we tolerate function-definitions
11073      that are missing their return types in some modes.  */
11074   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11075     {
11076       cp_parser_error (parser,
11077                        "expected constructor, destructor, or type conversion");
11078       return error_mark_node;
11079     }
11080
11081   /* An `=' or an `(' indicates an initializer.  */
11082   if (token->type == CPP_EQ
11083       || token->type == CPP_OPEN_PAREN)
11084     {
11085       is_initialized = true;
11086       initialization_kind = token->type;
11087     }
11088   else
11089     {
11090       /* If the init-declarator isn't initialized and isn't followed by a
11091          `,' or `;', it's not a valid init-declarator.  */
11092       if (token->type != CPP_COMMA
11093           && token->type != CPP_SEMICOLON)
11094         {
11095           cp_parser_error (parser, "expected initializer");
11096           return error_mark_node;
11097         }
11098       is_initialized = false;
11099       initialization_kind = CPP_EOF;
11100     }
11101
11102   /* Because start_decl has side-effects, we should only call it if we
11103      know we're going ahead.  By this point, we know that we cannot
11104      possibly be looking at any other construct.  */
11105   cp_parser_commit_to_tentative_parse (parser);
11106
11107   /* If the decl specifiers were bad, issue an error now that we're
11108      sure this was intended to be a declarator.  Then continue
11109      declaring the variable(s), as int, to try to cut down on further
11110      errors.  */
11111   if (decl_specifiers->any_specifiers_p
11112       && decl_specifiers->type == error_mark_node)
11113     {
11114       cp_parser_error (parser, "invalid type in declaration");
11115       decl_specifiers->type = integer_type_node;
11116     }
11117
11118   /* Check to see whether or not this declaration is a friend.  */
11119   friend_p = cp_parser_friend_p (decl_specifiers);
11120
11121   /* Check that the number of template-parameter-lists is OK.  */
11122   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11123     return error_mark_node;
11124
11125   /* Enter the newly declared entry in the symbol table.  If we're
11126      processing a declaration in a class-specifier, we wait until
11127      after processing the initializer.  */
11128   if (!member_p)
11129     {
11130       if (parser->in_unbraced_linkage_specification_p)
11131         decl_specifiers->storage_class = sc_extern;
11132       decl = start_decl (declarator, decl_specifiers,
11133                          is_initialized, attributes, prefix_attributes,
11134                          &pushed_scope);
11135     }
11136   else if (scope)
11137     /* Enter the SCOPE.  That way unqualified names appearing in the
11138        initializer will be looked up in SCOPE.  */
11139     pushed_scope = push_scope (scope);
11140
11141   /* Perform deferred access control checks, now that we know in which
11142      SCOPE the declared entity resides.  */
11143   if (!member_p && decl)
11144     {
11145       tree saved_current_function_decl = NULL_TREE;
11146
11147       /* If the entity being declared is a function, pretend that we
11148          are in its scope.  If it is a `friend', it may have access to
11149          things that would not otherwise be accessible.  */
11150       if (TREE_CODE (decl) == FUNCTION_DECL)
11151         {
11152           saved_current_function_decl = current_function_decl;
11153           current_function_decl = decl;
11154         }
11155
11156       /* Perform access checks for template parameters.  */
11157       cp_parser_perform_template_parameter_access_checks (checks);
11158
11159       /* Perform the access control checks for the declarator and the
11160          the decl-specifiers.  */
11161       perform_deferred_access_checks ();
11162
11163       /* Restore the saved value.  */
11164       if (TREE_CODE (decl) == FUNCTION_DECL)
11165         current_function_decl = saved_current_function_decl;
11166     }
11167
11168   /* Parse the initializer.  */
11169   initializer = NULL_TREE;
11170   is_parenthesized_init = false;
11171   is_non_constant_init = true;
11172   if (is_initialized)
11173     {
11174       if (declarator->kind == cdk_function
11175           && declarator->declarator->kind == cdk_id
11176           && initialization_kind == CPP_EQ)
11177         initializer = cp_parser_pure_specifier (parser);
11178       else
11179         initializer = cp_parser_initializer (parser,
11180                                              &is_parenthesized_init,
11181                                              &is_non_constant_init);
11182     }
11183
11184   /* The old parser allows attributes to appear after a parenthesized
11185      initializer.  Mark Mitchell proposed removing this functionality
11186      on the GCC mailing lists on 2002-08-13.  This parser accepts the
11187      attributes -- but ignores them.  */
11188   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11189     if (cp_parser_attributes_opt (parser))
11190       warning (OPT_Wattributes,
11191                "attributes after parenthesized initializer ignored");
11192
11193   /* For an in-class declaration, use `grokfield' to create the
11194      declaration.  */
11195   if (member_p)
11196     {
11197       if (pushed_scope)
11198         {
11199           pop_scope (pushed_scope);
11200           pushed_scope = false;
11201         }
11202       decl = grokfield (declarator, decl_specifiers,
11203                         initializer, !is_non_constant_init,
11204                         /*asmspec=*/NULL_TREE,
11205                         prefix_attributes);
11206       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11207         cp_parser_save_default_args (parser, decl);
11208     }
11209
11210   /* Finish processing the declaration.  But, skip friend
11211      declarations.  */
11212   if (!friend_p && decl && decl != error_mark_node)
11213     {
11214       cp_finish_decl (decl,
11215                       initializer, !is_non_constant_init,
11216                       asm_specification,
11217                       /* If the initializer is in parentheses, then this is
11218                          a direct-initialization, which means that an
11219                          `explicit' constructor is OK.  Otherwise, an
11220                          `explicit' constructor cannot be used.  */
11221                       ((is_parenthesized_init || !is_initialized)
11222                      ? 0 : LOOKUP_ONLYCONVERTING));
11223     }
11224   if (!friend_p && pushed_scope)
11225     pop_scope (pushed_scope);
11226
11227   return decl;
11228 }
11229
11230 /* Parse a declarator.
11231
11232    declarator:
11233      direct-declarator
11234      ptr-operator declarator
11235
11236    abstract-declarator:
11237      ptr-operator abstract-declarator [opt]
11238      direct-abstract-declarator
11239
11240    GNU Extensions:
11241
11242    declarator:
11243      attributes [opt] direct-declarator
11244      attributes [opt] ptr-operator declarator
11245
11246    abstract-declarator:
11247      attributes [opt] ptr-operator abstract-declarator [opt]
11248      attributes [opt] direct-abstract-declarator
11249
11250    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11251    detect constructor, destructor or conversion operators. It is set
11252    to -1 if the declarator is a name, and +1 if it is a
11253    function. Otherwise it is set to zero. Usually you just want to
11254    test for >0, but internally the negative value is used.
11255
11256    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11257    a decl-specifier-seq unless it declares a constructor, destructor,
11258    or conversion.  It might seem that we could check this condition in
11259    semantic analysis, rather than parsing, but that makes it difficult
11260    to handle something like `f()'.  We want to notice that there are
11261    no decl-specifiers, and therefore realize that this is an
11262    expression, not a declaration.)
11263
11264    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11265    the declarator is a direct-declarator of the form "(...)".
11266
11267    MEMBER_P is true iff this declarator is a member-declarator.  */
11268
11269 static cp_declarator *
11270 cp_parser_declarator (cp_parser* parser,
11271                       cp_parser_declarator_kind dcl_kind,
11272                       int* ctor_dtor_or_conv_p,
11273                       bool* parenthesized_p,
11274                       bool member_p)
11275 {
11276   cp_token *token;
11277   cp_declarator *declarator;
11278   enum tree_code code;
11279   cp_cv_quals cv_quals;
11280   tree class_type;
11281   tree attributes = NULL_TREE;
11282
11283   /* Assume this is not a constructor, destructor, or type-conversion
11284      operator.  */
11285   if (ctor_dtor_or_conv_p)
11286     *ctor_dtor_or_conv_p = 0;
11287
11288   if (cp_parser_allow_gnu_extensions_p (parser))
11289     attributes = cp_parser_attributes_opt (parser);
11290
11291   /* Peek at the next token.  */
11292   token = cp_lexer_peek_token (parser->lexer);
11293
11294   /* Check for the ptr-operator production.  */
11295   cp_parser_parse_tentatively (parser);
11296   /* Parse the ptr-operator.  */
11297   code = cp_parser_ptr_operator (parser,
11298                                  &class_type,
11299                                  &cv_quals);
11300   /* If that worked, then we have a ptr-operator.  */
11301   if (cp_parser_parse_definitely (parser))
11302     {
11303       /* If a ptr-operator was found, then this declarator was not
11304          parenthesized.  */
11305       if (parenthesized_p)
11306         *parenthesized_p = true;
11307       /* The dependent declarator is optional if we are parsing an
11308          abstract-declarator.  */
11309       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11310         cp_parser_parse_tentatively (parser);
11311
11312       /* Parse the dependent declarator.  */
11313       declarator = cp_parser_declarator (parser, dcl_kind,
11314                                          /*ctor_dtor_or_conv_p=*/NULL,
11315                                          /*parenthesized_p=*/NULL,
11316                                          /*member_p=*/false);
11317
11318       /* If we are parsing an abstract-declarator, we must handle the
11319          case where the dependent declarator is absent.  */
11320       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11321           && !cp_parser_parse_definitely (parser))
11322         declarator = NULL;
11323
11324       /* Build the representation of the ptr-operator.  */
11325       if (class_type)
11326         declarator = make_ptrmem_declarator (cv_quals,
11327                                              class_type,
11328                                              declarator);
11329       else if (code == INDIRECT_REF)
11330         declarator = make_pointer_declarator (cv_quals, declarator);
11331       else
11332         declarator = make_reference_declarator (cv_quals, declarator);
11333     }
11334   /* Everything else is a direct-declarator.  */
11335   else
11336     {
11337       if (parenthesized_p)
11338         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11339                                                    CPP_OPEN_PAREN);
11340       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11341                                                 ctor_dtor_or_conv_p,
11342                                                 member_p);
11343     }
11344
11345   if (attributes && declarator && declarator != cp_error_declarator)
11346     declarator->attributes = attributes;
11347
11348   return declarator;
11349 }
11350
11351 /* Parse a direct-declarator or direct-abstract-declarator.
11352
11353    direct-declarator:
11354      declarator-id
11355      direct-declarator ( parameter-declaration-clause )
11356        cv-qualifier-seq [opt]
11357        exception-specification [opt]
11358      direct-declarator [ constant-expression [opt] ]
11359      ( declarator )
11360
11361    direct-abstract-declarator:
11362      direct-abstract-declarator [opt]
11363        ( parameter-declaration-clause )
11364        cv-qualifier-seq [opt]
11365        exception-specification [opt]
11366      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11367      ( abstract-declarator )
11368
11369    Returns a representation of the declarator.  DCL_KIND is
11370    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11371    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11372    we are parsing a direct-declarator.  It is
11373    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11374    of ambiguity we prefer an abstract declarator, as per
11375    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11376    cp_parser_declarator.  */
11377
11378 static cp_declarator *
11379 cp_parser_direct_declarator (cp_parser* parser,
11380                              cp_parser_declarator_kind dcl_kind,
11381                              int* ctor_dtor_or_conv_p,
11382                              bool member_p)
11383 {
11384   cp_token *token;
11385   cp_declarator *declarator = NULL;
11386   tree scope = NULL_TREE;
11387   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11388   bool saved_in_declarator_p = parser->in_declarator_p;
11389   bool first = true;
11390   tree pushed_scope = NULL_TREE;
11391
11392   while (true)
11393     {
11394       /* Peek at the next token.  */
11395       token = cp_lexer_peek_token (parser->lexer);
11396       if (token->type == CPP_OPEN_PAREN)
11397         {
11398           /* This is either a parameter-declaration-clause, or a
11399              parenthesized declarator. When we know we are parsing a
11400              named declarator, it must be a parenthesized declarator
11401              if FIRST is true. For instance, `(int)' is a
11402              parameter-declaration-clause, with an omitted
11403              direct-abstract-declarator. But `((*))', is a
11404              parenthesized abstract declarator. Finally, when T is a
11405              template parameter `(T)' is a
11406              parameter-declaration-clause, and not a parenthesized
11407              named declarator.
11408
11409              We first try and parse a parameter-declaration-clause,
11410              and then try a nested declarator (if FIRST is true).
11411
11412              It is not an error for it not to be a
11413              parameter-declaration-clause, even when FIRST is
11414              false. Consider,
11415
11416                int i (int);
11417                int i (3);
11418
11419              The first is the declaration of a function while the
11420              second is a the definition of a variable, including its
11421              initializer.
11422
11423              Having seen only the parenthesis, we cannot know which of
11424              these two alternatives should be selected.  Even more
11425              complex are examples like:
11426
11427                int i (int (a));
11428                int i (int (3));
11429
11430              The former is a function-declaration; the latter is a
11431              variable initialization.
11432
11433              Thus again, we try a parameter-declaration-clause, and if
11434              that fails, we back out and return.  */
11435
11436           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11437             {
11438               cp_parameter_declarator *params;
11439               unsigned saved_num_template_parameter_lists;
11440
11441               /* In a member-declarator, the only valid interpretation
11442                  of a parenthesis is the start of a
11443                  parameter-declaration-clause.  (It is invalid to
11444                  initialize a static data member with a parenthesized
11445                  initializer; only the "=" form of initialization is
11446                  permitted.)  */
11447               if (!member_p)
11448                 cp_parser_parse_tentatively (parser);
11449
11450               /* Consume the `('.  */
11451               cp_lexer_consume_token (parser->lexer);
11452               if (first)
11453                 {
11454                   /* If this is going to be an abstract declarator, we're
11455                      in a declarator and we can't have default args.  */
11456                   parser->default_arg_ok_p = false;
11457                   parser->in_declarator_p = true;
11458                 }
11459
11460               /* Inside the function parameter list, surrounding
11461                  template-parameter-lists do not apply.  */
11462               saved_num_template_parameter_lists
11463                 = parser->num_template_parameter_lists;
11464               parser->num_template_parameter_lists = 0;
11465
11466               /* Parse the parameter-declaration-clause.  */
11467               params = cp_parser_parameter_declaration_clause (parser);
11468
11469               parser->num_template_parameter_lists
11470                 = saved_num_template_parameter_lists;
11471
11472               /* If all went well, parse the cv-qualifier-seq and the
11473                  exception-specification.  */
11474               if (member_p || cp_parser_parse_definitely (parser))
11475                 {
11476                   cp_cv_quals cv_quals;
11477                   tree exception_specification;
11478
11479                   if (ctor_dtor_or_conv_p)
11480                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11481                   first = false;
11482                   /* Consume the `)'.  */
11483                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11484
11485                   /* Parse the cv-qualifier-seq.  */
11486                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11487                   /* And the exception-specification.  */
11488                   exception_specification
11489                     = cp_parser_exception_specification_opt (parser);
11490
11491                   /* Create the function-declarator.  */
11492                   declarator = make_call_declarator (declarator,
11493                                                      params,
11494                                                      cv_quals,
11495                                                      exception_specification);
11496                   /* Any subsequent parameter lists are to do with
11497                      return type, so are not those of the declared
11498                      function.  */
11499                   parser->default_arg_ok_p = false;
11500
11501                   /* Repeat the main loop.  */
11502                   continue;
11503                 }
11504             }
11505
11506           /* If this is the first, we can try a parenthesized
11507              declarator.  */
11508           if (first)
11509             {
11510               bool saved_in_type_id_in_expr_p;
11511
11512               parser->default_arg_ok_p = saved_default_arg_ok_p;
11513               parser->in_declarator_p = saved_in_declarator_p;
11514
11515               /* Consume the `('.  */
11516               cp_lexer_consume_token (parser->lexer);
11517               /* Parse the nested declarator.  */
11518               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11519               parser->in_type_id_in_expr_p = true;
11520               declarator
11521                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11522                                         /*parenthesized_p=*/NULL,
11523                                         member_p);
11524               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11525               first = false;
11526               /* Expect a `)'.  */
11527               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11528                 declarator = cp_error_declarator;
11529               if (declarator == cp_error_declarator)
11530                 break;
11531
11532               goto handle_declarator;
11533             }
11534           /* Otherwise, we must be done.  */
11535           else
11536             break;
11537         }
11538       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11539                && token->type == CPP_OPEN_SQUARE)
11540         {
11541           /* Parse an array-declarator.  */
11542           tree bounds;
11543
11544           if (ctor_dtor_or_conv_p)
11545             *ctor_dtor_or_conv_p = 0;
11546
11547           first = false;
11548           parser->default_arg_ok_p = false;
11549           parser->in_declarator_p = true;
11550           /* Consume the `['.  */
11551           cp_lexer_consume_token (parser->lexer);
11552           /* Peek at the next token.  */
11553           token = cp_lexer_peek_token (parser->lexer);
11554           /* If the next token is `]', then there is no
11555              constant-expression.  */
11556           if (token->type != CPP_CLOSE_SQUARE)
11557             {
11558               bool non_constant_p;
11559
11560               bounds
11561                 = cp_parser_constant_expression (parser,
11562                                                  /*allow_non_constant=*/true,
11563                                                  &non_constant_p);
11564               if (!non_constant_p)
11565                 bounds = fold_non_dependent_expr (bounds);
11566               /* Normally, the array bound must be an integral constant
11567                  expression.  However, as an extension, we allow VLAs
11568                  in function scopes.  */
11569               else if (!at_function_scope_p ())
11570                 {
11571                   error ("array bound is not an integer constant");
11572                   bounds = error_mark_node;
11573                 }
11574             }
11575           else
11576             bounds = NULL_TREE;
11577           /* Look for the closing `]'.  */
11578           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11579             {
11580               declarator = cp_error_declarator;
11581               break;
11582             }
11583
11584           declarator = make_array_declarator (declarator, bounds);
11585         }
11586       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11587         {
11588           tree qualifying_scope;
11589           tree unqualified_name;
11590           special_function_kind sfk;
11591           bool abstract_ok;
11592
11593           /* Parse a declarator-id */
11594           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11595           if (abstract_ok)
11596             cp_parser_parse_tentatively (parser);
11597           unqualified_name
11598             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11599           qualifying_scope = parser->scope;
11600           if (abstract_ok)
11601             {
11602               if (!cp_parser_parse_definitely (parser))
11603                 unqualified_name = error_mark_node;
11604               else if (unqualified_name
11605                        && (qualifying_scope
11606                            || (TREE_CODE (unqualified_name)
11607                                != IDENTIFIER_NODE)))
11608                 {
11609                   cp_parser_error (parser, "expected unqualified-id");
11610                   unqualified_name = error_mark_node;
11611                 }
11612             }
11613
11614           if (!unqualified_name)
11615             return NULL;
11616           if (unqualified_name == error_mark_node)
11617             {
11618               declarator = cp_error_declarator;
11619               break;
11620             }
11621
11622           if (qualifying_scope && at_namespace_scope_p ()
11623               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11624             {
11625               /* In the declaration of a member of a template class
11626                  outside of the class itself, the SCOPE will sometimes
11627                  be a TYPENAME_TYPE.  For example, given:
11628
11629                  template <typename T>
11630                  int S<T>::R::i = 3;
11631
11632                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11633                  this context, we must resolve S<T>::R to an ordinary
11634                  type, rather than a typename type.
11635
11636                  The reason we normally avoid resolving TYPENAME_TYPEs
11637                  is that a specialization of `S' might render
11638                  `S<T>::R' not a type.  However, if `S' is
11639                  specialized, then this `i' will not be used, so there
11640                  is no harm in resolving the types here.  */
11641               tree type;
11642
11643               /* Resolve the TYPENAME_TYPE.  */
11644               type = resolve_typename_type (qualifying_scope,
11645                                             /*only_current_p=*/false);
11646               /* If that failed, the declarator is invalid.  */
11647               if (type == error_mark_node)
11648                 error ("%<%T::%D%> is not a type",
11649                        TYPE_CONTEXT (qualifying_scope),
11650                        TYPE_IDENTIFIER (qualifying_scope));
11651               qualifying_scope = type;
11652             }
11653
11654           sfk = sfk_none;
11655           if (unqualified_name)
11656             {
11657               tree class_type;
11658
11659               if (qualifying_scope
11660                   && CLASS_TYPE_P (qualifying_scope))
11661                 class_type = qualifying_scope;
11662               else
11663                 class_type = current_class_type;
11664
11665               if (TREE_CODE (unqualified_name) == TYPE_DECL)
11666                 {
11667                   tree name_type = TREE_TYPE (unqualified_name);
11668                   if (class_type && same_type_p (name_type, class_type))
11669                     {
11670                       if (qualifying_scope
11671                           && CLASSTYPE_USE_TEMPLATE (name_type))
11672                         {
11673                           error ("invalid use of constructor as a template");
11674                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11675                                   "name the constructor in a qualified name",
11676                                   class_type,
11677                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11678                                   class_type, name_type);
11679                           declarator = cp_error_declarator;
11680                           break;
11681                         }
11682                       else
11683                         unqualified_name = constructor_name (class_type);
11684                     }
11685                   else
11686                     {
11687                       /* We do not attempt to print the declarator
11688                          here because we do not have enough
11689                          information about its original syntactic
11690                          form.  */
11691                       cp_parser_error (parser, "invalid declarator");
11692                       declarator = cp_error_declarator;
11693                       break;
11694                     }
11695                 }
11696
11697               if (class_type)
11698                 {
11699                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11700                     sfk = sfk_destructor;
11701                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11702                     sfk = sfk_conversion;
11703                   else if (/* There's no way to declare a constructor
11704                               for an anonymous type, even if the type
11705                               got a name for linkage purposes.  */
11706                            !TYPE_WAS_ANONYMOUS (class_type)
11707                            && constructor_name_p (unqualified_name,
11708                                                   class_type))
11709                     {
11710                       unqualified_name = constructor_name (class_type);
11711                       sfk = sfk_constructor;
11712                     }
11713
11714                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
11715                     *ctor_dtor_or_conv_p = -1;
11716                 }
11717             }
11718           declarator = make_id_declarator (qualifying_scope,
11719                                            unqualified_name,
11720                                            sfk);
11721           declarator->id_loc = token->location;
11722
11723         handle_declarator:;
11724           scope = get_scope_of_declarator (declarator);
11725           if (scope)
11726             /* Any names that appear after the declarator-id for a
11727                member are looked up in the containing scope.  */
11728             pushed_scope = push_scope (scope);
11729           parser->in_declarator_p = true;
11730           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11731               || (declarator && declarator->kind == cdk_id))
11732             /* Default args are only allowed on function
11733                declarations.  */
11734             parser->default_arg_ok_p = saved_default_arg_ok_p;
11735           else
11736             parser->default_arg_ok_p = false;
11737
11738           first = false;
11739         }
11740       /* We're done.  */
11741       else
11742         break;
11743     }
11744
11745   /* For an abstract declarator, we might wind up with nothing at this
11746      point.  That's an error; the declarator is not optional.  */
11747   if (!declarator)
11748     cp_parser_error (parser, "expected declarator");
11749
11750   /* If we entered a scope, we must exit it now.  */
11751   if (pushed_scope)
11752     pop_scope (pushed_scope);
11753
11754   parser->default_arg_ok_p = saved_default_arg_ok_p;
11755   parser->in_declarator_p = saved_in_declarator_p;
11756
11757   return declarator;
11758 }
11759
11760 /* Parse a ptr-operator.
11761
11762    ptr-operator:
11763      * cv-qualifier-seq [opt]
11764      &
11765      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11766
11767    GNU Extension:
11768
11769    ptr-operator:
11770      & cv-qualifier-seq [opt]
11771
11772    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11773    Returns ADDR_EXPR if a reference was used.  In the case of a
11774    pointer-to-member, *TYPE is filled in with the TYPE containing the
11775    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11776    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11777    ERROR_MARK if an error occurred.  */
11778
11779 static enum tree_code
11780 cp_parser_ptr_operator (cp_parser* parser,
11781                         tree* type,
11782                         cp_cv_quals *cv_quals)
11783 {
11784   enum tree_code code = ERROR_MARK;
11785   cp_token *token;
11786
11787   /* Assume that it's not a pointer-to-member.  */
11788   *type = NULL_TREE;
11789   /* And that there are no cv-qualifiers.  */
11790   *cv_quals = TYPE_UNQUALIFIED;
11791
11792   /* Peek at the next token.  */
11793   token = cp_lexer_peek_token (parser->lexer);
11794   /* If it's a `*' or `&' we have a pointer or reference.  */
11795   if (token->type == CPP_MULT || token->type == CPP_AND)
11796     {
11797       /* Remember which ptr-operator we were processing.  */
11798       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11799
11800       /* Consume the `*' or `&'.  */
11801       cp_lexer_consume_token (parser->lexer);
11802
11803       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11804          `&', if we are allowing GNU extensions.  (The only qualifier
11805          that can legally appear after `&' is `restrict', but that is
11806          enforced during semantic analysis.  */
11807       if (code == INDIRECT_REF
11808           || cp_parser_allow_gnu_extensions_p (parser))
11809         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11810     }
11811   else
11812     {
11813       /* Try the pointer-to-member case.  */
11814       cp_parser_parse_tentatively (parser);
11815       /* Look for the optional `::' operator.  */
11816       cp_parser_global_scope_opt (parser,
11817                                   /*current_scope_valid_p=*/false,
11818                                   /*object_scope_valid_p=*/false);
11819       /* Look for the nested-name specifier.  */
11820       cp_parser_nested_name_specifier (parser,
11821                                        /*typename_keyword_p=*/false,
11822                                        /*check_dependency_p=*/true,
11823                                        /*type_p=*/false,
11824                                        /*is_declaration=*/false);
11825       /* If we found it, and the next token is a `*', then we are
11826          indeed looking at a pointer-to-member operator.  */
11827       if (!cp_parser_error_occurred (parser)
11828           && cp_parser_require (parser, CPP_MULT, "`*'"))
11829         {
11830           /* Indicate that the `*' operator was used.  */
11831           code = INDIRECT_REF;
11832
11833           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11834             error ("%qD is a namespace", parser->scope);
11835           else
11836             {
11837               /* The type of which the member is a member is given by the
11838                  current SCOPE.  */
11839               *type = parser->scope;
11840               /* The next name will not be qualified.  */
11841               parser->scope = NULL_TREE;
11842               parser->qualifying_scope = NULL_TREE;
11843               parser->object_scope = NULL_TREE;
11844               /* Look for the optional cv-qualifier-seq.  */
11845               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11846             }
11847         }
11848       /* If that didn't work we don't have a ptr-operator.  */
11849       if (!cp_parser_parse_definitely (parser))
11850         cp_parser_error (parser, "expected ptr-operator");
11851     }
11852
11853   return code;
11854 }
11855
11856 /* Parse an (optional) cv-qualifier-seq.
11857
11858    cv-qualifier-seq:
11859      cv-qualifier cv-qualifier-seq [opt]
11860
11861    cv-qualifier:
11862      const
11863      volatile
11864
11865    GNU Extension:
11866
11867    cv-qualifier:
11868      __restrict__
11869
11870    Returns a bitmask representing the cv-qualifiers.  */
11871
11872 static cp_cv_quals
11873 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11874 {
11875   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11876
11877   while (true)
11878     {
11879       cp_token *token;
11880       cp_cv_quals cv_qualifier;
11881
11882       /* Peek at the next token.  */
11883       token = cp_lexer_peek_token (parser->lexer);
11884       /* See if it's a cv-qualifier.  */
11885       switch (token->keyword)
11886         {
11887         case RID_CONST:
11888           cv_qualifier = TYPE_QUAL_CONST;
11889           break;
11890
11891         case RID_VOLATILE:
11892           cv_qualifier = TYPE_QUAL_VOLATILE;
11893           break;
11894
11895         case RID_RESTRICT:
11896           cv_qualifier = TYPE_QUAL_RESTRICT;
11897           break;
11898
11899         default:
11900           cv_qualifier = TYPE_UNQUALIFIED;
11901           break;
11902         }
11903
11904       if (!cv_qualifier)
11905         break;
11906
11907       if (cv_quals & cv_qualifier)
11908         {
11909           error ("duplicate cv-qualifier");
11910           cp_lexer_purge_token (parser->lexer);
11911         }
11912       else
11913         {
11914           cp_lexer_consume_token (parser->lexer);
11915           cv_quals |= cv_qualifier;
11916         }
11917     }
11918
11919   return cv_quals;
11920 }
11921
11922 /* Parse a declarator-id.
11923
11924    declarator-id:
11925      id-expression
11926      :: [opt] nested-name-specifier [opt] type-name
11927
11928    In the `id-expression' case, the value returned is as for
11929    cp_parser_id_expression if the id-expression was an unqualified-id.
11930    If the id-expression was a qualified-id, then a SCOPE_REF is
11931    returned.  The first operand is the scope (either a NAMESPACE_DECL
11932    or TREE_TYPE), but the second is still just a representation of an
11933    unqualified-id.  */
11934
11935 static tree
11936 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
11937 {
11938   tree id;
11939   /* The expression must be an id-expression.  Assume that qualified
11940      names are the names of types so that:
11941
11942        template <class T>
11943        int S<T>::R::i = 3;
11944
11945      will work; we must treat `S<T>::R' as the name of a type.
11946      Similarly, assume that qualified names are templates, where
11947      required, so that:
11948
11949        template <class T>
11950        int S<T>::R<T>::i = 3;
11951
11952      will work, too.  */
11953   id = cp_parser_id_expression (parser,
11954                                 /*template_keyword_p=*/false,
11955                                 /*check_dependency_p=*/false,
11956                                 /*template_p=*/NULL,
11957                                 /*declarator_p=*/true,
11958                                 optional_p,
11959                                 /*member_p=*/false);
11960   if (id && BASELINK_P (id))
11961     id = BASELINK_FUNCTIONS (id);
11962   return id;
11963 }
11964
11965 /* Parse a type-id.
11966
11967    type-id:
11968      type-specifier-seq abstract-declarator [opt]
11969
11970    Returns the TYPE specified.  */
11971
11972 static tree
11973 cp_parser_type_id (cp_parser* parser)
11974 {
11975   cp_decl_specifier_seq type_specifier_seq;
11976   cp_declarator *abstract_declarator;
11977
11978   /* Parse the type-specifier-seq.  */
11979   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11980                                 &type_specifier_seq);
11981   if (type_specifier_seq.type == error_mark_node)
11982     return error_mark_node;
11983
11984   /* There might or might not be an abstract declarator.  */
11985   cp_parser_parse_tentatively (parser);
11986   /* Look for the declarator.  */
11987   abstract_declarator
11988     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11989                             /*parenthesized_p=*/NULL,
11990                             /*member_p=*/false);
11991   /* Check to see if there really was a declarator.  */
11992   if (!cp_parser_parse_definitely (parser))
11993     abstract_declarator = NULL;
11994
11995   return groktypename (&type_specifier_seq, abstract_declarator);
11996 }
11997
11998 /* Parse a type-specifier-seq.
11999
12000    type-specifier-seq:
12001      type-specifier type-specifier-seq [opt]
12002
12003    GNU extension:
12004
12005    type-specifier-seq:
12006      attributes type-specifier-seq [opt]
12007
12008    If IS_CONDITION is true, we are at the start of a "condition",
12009    e.g., we've just seen "if (".
12010
12011    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
12012
12013 static void
12014 cp_parser_type_specifier_seq (cp_parser* parser,
12015                               bool is_condition,
12016                               cp_decl_specifier_seq *type_specifier_seq)
12017 {
12018   bool seen_type_specifier = false;
12019   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12020
12021   /* Clear the TYPE_SPECIFIER_SEQ.  */
12022   clear_decl_specs (type_specifier_seq);
12023
12024   /* Parse the type-specifiers and attributes.  */
12025   while (true)
12026     {
12027       tree type_specifier;
12028       bool is_cv_qualifier;
12029
12030       /* Check for attributes first.  */
12031       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12032         {
12033           type_specifier_seq->attributes =
12034             chainon (type_specifier_seq->attributes,
12035                      cp_parser_attributes_opt (parser));
12036           continue;
12037         }
12038
12039       /* Look for the type-specifier.  */
12040       type_specifier = cp_parser_type_specifier (parser,
12041                                                  flags,
12042                                                  type_specifier_seq,
12043                                                  /*is_declaration=*/false,
12044                                                  NULL,
12045                                                  &is_cv_qualifier);
12046       if (!type_specifier)
12047         {
12048           /* If the first type-specifier could not be found, this is not a
12049              type-specifier-seq at all.  */
12050           if (!seen_type_specifier)
12051             {
12052               cp_parser_error (parser, "expected type-specifier");
12053               type_specifier_seq->type = error_mark_node;
12054               return;
12055             }
12056           /* If subsequent type-specifiers could not be found, the
12057              type-specifier-seq is complete.  */
12058           break;
12059         }
12060
12061       seen_type_specifier = true;
12062       /* The standard says that a condition can be:
12063
12064             type-specifier-seq declarator = assignment-expression
12065
12066          However, given:
12067
12068            struct S {};
12069            if (int S = ...)
12070
12071          we should treat the "S" as a declarator, not as a
12072          type-specifier.  The standard doesn't say that explicitly for
12073          type-specifier-seq, but it does say that for
12074          decl-specifier-seq in an ordinary declaration.  Perhaps it
12075          would be clearer just to allow a decl-specifier-seq here, and
12076          then add a semantic restriction that if any decl-specifiers
12077          that are not type-specifiers appear, the program is invalid.  */
12078       if (is_condition && !is_cv_qualifier)
12079         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12080     }
12081
12082   cp_parser_check_decl_spec (type_specifier_seq);
12083 }
12084
12085 /* Parse a parameter-declaration-clause.
12086
12087    parameter-declaration-clause:
12088      parameter-declaration-list [opt] ... [opt]
12089      parameter-declaration-list , ...
12090
12091    Returns a representation for the parameter declarations.  A return
12092    value of NULL indicates a parameter-declaration-clause consisting
12093    only of an ellipsis.  */
12094
12095 static cp_parameter_declarator *
12096 cp_parser_parameter_declaration_clause (cp_parser* parser)
12097 {
12098   cp_parameter_declarator *parameters;
12099   cp_token *token;
12100   bool ellipsis_p;
12101   bool is_error;
12102
12103   /* Peek at the next token.  */
12104   token = cp_lexer_peek_token (parser->lexer);
12105   /* Check for trivial parameter-declaration-clauses.  */
12106   if (token->type == CPP_ELLIPSIS)
12107     {
12108       /* Consume the `...' token.  */
12109       cp_lexer_consume_token (parser->lexer);
12110       return NULL;
12111     }
12112   else if (token->type == CPP_CLOSE_PAREN)
12113     /* There are no parameters.  */
12114     {
12115 #ifndef NO_IMPLICIT_EXTERN_C
12116       if (in_system_header && current_class_type == NULL
12117           && current_lang_name == lang_name_c)
12118         return NULL;
12119       else
12120 #endif
12121         return no_parameters;
12122     }
12123   /* Check for `(void)', too, which is a special case.  */
12124   else if (token->keyword == RID_VOID
12125            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12126                == CPP_CLOSE_PAREN))
12127     {
12128       /* Consume the `void' token.  */
12129       cp_lexer_consume_token (parser->lexer);
12130       /* There are no parameters.  */
12131       return no_parameters;
12132     }
12133
12134   /* Parse the parameter-declaration-list.  */
12135   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12136   /* If a parse error occurred while parsing the
12137      parameter-declaration-list, then the entire
12138      parameter-declaration-clause is erroneous.  */
12139   if (is_error)
12140     return NULL;
12141
12142   /* Peek at the next token.  */
12143   token = cp_lexer_peek_token (parser->lexer);
12144   /* If it's a `,', the clause should terminate with an ellipsis.  */
12145   if (token->type == CPP_COMMA)
12146     {
12147       /* Consume the `,'.  */
12148       cp_lexer_consume_token (parser->lexer);
12149       /* Expect an ellipsis.  */
12150       ellipsis_p
12151         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12152     }
12153   /* It might also be `...' if the optional trailing `,' was
12154      omitted.  */
12155   else if (token->type == CPP_ELLIPSIS)
12156     {
12157       /* Consume the `...' token.  */
12158       cp_lexer_consume_token (parser->lexer);
12159       /* And remember that we saw it.  */
12160       ellipsis_p = true;
12161     }
12162   else
12163     ellipsis_p = false;
12164
12165   /* Finish the parameter list.  */
12166   if (parameters && ellipsis_p)
12167     parameters->ellipsis_p = true;
12168
12169   return parameters;
12170 }
12171
12172 /* Parse a parameter-declaration-list.
12173
12174    parameter-declaration-list:
12175      parameter-declaration
12176      parameter-declaration-list , parameter-declaration
12177
12178    Returns a representation of the parameter-declaration-list, as for
12179    cp_parser_parameter_declaration_clause.  However, the
12180    `void_list_node' is never appended to the list.  Upon return,
12181    *IS_ERROR will be true iff an error occurred.  */
12182
12183 static cp_parameter_declarator *
12184 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12185 {
12186   cp_parameter_declarator *parameters = NULL;
12187   cp_parameter_declarator **tail = &parameters;
12188   bool saved_in_unbraced_linkage_specification_p;
12189
12190   /* Assume all will go well.  */
12191   *is_error = false;
12192   /* The special considerations that apply to a function within an
12193      unbraced linkage specifications do not apply to the parameters
12194      to the function.  */
12195   saved_in_unbraced_linkage_specification_p 
12196     = parser->in_unbraced_linkage_specification_p;
12197   parser->in_unbraced_linkage_specification_p = false;
12198
12199   /* Look for more parameters.  */
12200   while (true)
12201     {
12202       cp_parameter_declarator *parameter;
12203       bool parenthesized_p;
12204       /* Parse the parameter.  */
12205       parameter
12206         = cp_parser_parameter_declaration (parser,
12207                                            /*template_parm_p=*/false,
12208                                            &parenthesized_p);
12209
12210       /* If a parse error occurred parsing the parameter declaration,
12211          then the entire parameter-declaration-list is erroneous.  */
12212       if (!parameter)
12213         {
12214           *is_error = true;
12215           parameters = NULL;
12216           break;
12217         }
12218       /* Add the new parameter to the list.  */
12219       *tail = parameter;
12220       tail = &parameter->next;
12221
12222       /* Peek at the next token.  */
12223       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12224           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12225           /* These are for Objective-C++ */
12226           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12227           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12228         /* The parameter-declaration-list is complete.  */
12229         break;
12230       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12231         {
12232           cp_token *token;
12233
12234           /* Peek at the next token.  */
12235           token = cp_lexer_peek_nth_token (parser->lexer, 2);
12236           /* If it's an ellipsis, then the list is complete.  */
12237           if (token->type == CPP_ELLIPSIS)
12238             break;
12239           /* Otherwise, there must be more parameters.  Consume the
12240              `,'.  */
12241           cp_lexer_consume_token (parser->lexer);
12242           /* When parsing something like:
12243
12244                 int i(float f, double d)
12245
12246              we can tell after seeing the declaration for "f" that we
12247              are not looking at an initialization of a variable "i",
12248              but rather at the declaration of a function "i".
12249
12250              Due to the fact that the parsing of template arguments
12251              (as specified to a template-id) requires backtracking we
12252              cannot use this technique when inside a template argument
12253              list.  */
12254           if (!parser->in_template_argument_list_p
12255               && !parser->in_type_id_in_expr_p
12256               && cp_parser_uncommitted_to_tentative_parse_p (parser)
12257               /* However, a parameter-declaration of the form
12258                  "foat(f)" (which is a valid declaration of a
12259                  parameter "f") can also be interpreted as an
12260                  expression (the conversion of "f" to "float").  */
12261               && !parenthesized_p)
12262             cp_parser_commit_to_tentative_parse (parser);
12263         }
12264       else
12265         {
12266           cp_parser_error (parser, "expected %<,%> or %<...%>");
12267           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12268             cp_parser_skip_to_closing_parenthesis (parser,
12269                                                    /*recovering=*/true,
12270                                                    /*or_comma=*/false,
12271                                                    /*consume_paren=*/false);
12272           break;
12273         }
12274     }
12275
12276   parser->in_unbraced_linkage_specification_p
12277     = saved_in_unbraced_linkage_specification_p;
12278
12279   return parameters;
12280 }
12281
12282 /* Parse a parameter declaration.
12283
12284    parameter-declaration:
12285      decl-specifier-seq declarator
12286      decl-specifier-seq declarator = assignment-expression
12287      decl-specifier-seq abstract-declarator [opt]
12288      decl-specifier-seq abstract-declarator [opt] = assignment-expression
12289
12290    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12291    declares a template parameter.  (In that case, a non-nested `>'
12292    token encountered during the parsing of the assignment-expression
12293    is not interpreted as a greater-than operator.)
12294
12295    Returns a representation of the parameter, or NULL if an error
12296    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12297    true iff the declarator is of the form "(p)".  */
12298
12299 static cp_parameter_declarator *
12300 cp_parser_parameter_declaration (cp_parser *parser,
12301                                  bool template_parm_p,
12302                                  bool *parenthesized_p)
12303 {
12304   int declares_class_or_enum;
12305   bool greater_than_is_operator_p;
12306   cp_decl_specifier_seq decl_specifiers;
12307   cp_declarator *declarator;
12308   tree default_argument;
12309   cp_token *token;
12310   const char *saved_message;
12311
12312   /* In a template parameter, `>' is not an operator.
12313
12314      [temp.param]
12315
12316      When parsing a default template-argument for a non-type
12317      template-parameter, the first non-nested `>' is taken as the end
12318      of the template parameter-list rather than a greater-than
12319      operator.  */
12320   greater_than_is_operator_p = !template_parm_p;
12321
12322   /* Type definitions may not appear in parameter types.  */
12323   saved_message = parser->type_definition_forbidden_message;
12324   parser->type_definition_forbidden_message
12325     = "types may not be defined in parameter types";
12326
12327   /* Parse the declaration-specifiers.  */
12328   cp_parser_decl_specifier_seq (parser,
12329                                 CP_PARSER_FLAGS_NONE,
12330                                 &decl_specifiers,
12331                                 &declares_class_or_enum);
12332   /* If an error occurred, there's no reason to attempt to parse the
12333      rest of the declaration.  */
12334   if (cp_parser_error_occurred (parser))
12335     {
12336       parser->type_definition_forbidden_message = saved_message;
12337       return NULL;
12338     }
12339
12340   /* Peek at the next token.  */
12341   token = cp_lexer_peek_token (parser->lexer);
12342   /* If the next token is a `)', `,', `=', `>', or `...', then there
12343      is no declarator.  */
12344   if (token->type == CPP_CLOSE_PAREN
12345       || token->type == CPP_COMMA
12346       || token->type == CPP_EQ
12347       || token->type == CPP_ELLIPSIS
12348       || token->type == CPP_GREATER)
12349     {
12350       declarator = NULL;
12351       if (parenthesized_p)
12352         *parenthesized_p = false;
12353     }
12354   /* Otherwise, there should be a declarator.  */
12355   else
12356     {
12357       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12358       parser->default_arg_ok_p = false;
12359
12360       /* After seeing a decl-specifier-seq, if the next token is not a
12361          "(", there is no possibility that the code is a valid
12362          expression.  Therefore, if parsing tentatively, we commit at
12363          this point.  */
12364       if (!parser->in_template_argument_list_p
12365           /* In an expression context, having seen:
12366
12367                (int((char ...
12368
12369              we cannot be sure whether we are looking at a
12370              function-type (taking a "char" as a parameter) or a cast
12371              of some object of type "char" to "int".  */
12372           && !parser->in_type_id_in_expr_p
12373           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12374           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12375         cp_parser_commit_to_tentative_parse (parser);
12376       /* Parse the declarator.  */
12377       declarator = cp_parser_declarator (parser,
12378                                          CP_PARSER_DECLARATOR_EITHER,
12379                                          /*ctor_dtor_or_conv_p=*/NULL,
12380                                          parenthesized_p,
12381                                          /*member_p=*/false);
12382       parser->default_arg_ok_p = saved_default_arg_ok_p;
12383       /* After the declarator, allow more attributes.  */
12384       decl_specifiers.attributes
12385         = chainon (decl_specifiers.attributes,
12386                    cp_parser_attributes_opt (parser));
12387     }
12388
12389   /* The restriction on defining new types applies only to the type
12390      of the parameter, not to the default argument.  */
12391   parser->type_definition_forbidden_message = saved_message;
12392
12393   /* If the next token is `=', then process a default argument.  */
12394   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12395     {
12396       bool saved_greater_than_is_operator_p;
12397       /* Consume the `='.  */
12398       cp_lexer_consume_token (parser->lexer);
12399
12400       /* If we are defining a class, then the tokens that make up the
12401          default argument must be saved and processed later.  */
12402       if (!template_parm_p && at_class_scope_p ()
12403           && TYPE_BEING_DEFINED (current_class_type))
12404         {
12405           unsigned depth = 0;
12406           cp_token *first_token;
12407           cp_token *token;
12408
12409           /* Add tokens until we have processed the entire default
12410              argument.  We add the range [first_token, token).  */
12411           first_token = cp_lexer_peek_token (parser->lexer);
12412           while (true)
12413             {
12414               bool done = false;
12415
12416               /* Peek at the next token.  */
12417               token = cp_lexer_peek_token (parser->lexer);
12418               /* What we do depends on what token we have.  */
12419               switch (token->type)
12420                 {
12421                   /* In valid code, a default argument must be
12422                      immediately followed by a `,' `)', or `...'.  */
12423                 case CPP_COMMA:
12424                 case CPP_CLOSE_PAREN:
12425                 case CPP_ELLIPSIS:
12426                   /* If we run into a non-nested `;', `}', or `]',
12427                      then the code is invalid -- but the default
12428                      argument is certainly over.  */
12429                 case CPP_SEMICOLON:
12430                 case CPP_CLOSE_BRACE:
12431                 case CPP_CLOSE_SQUARE:
12432                   if (depth == 0)
12433                     done = true;
12434                   /* Update DEPTH, if necessary.  */
12435                   else if (token->type == CPP_CLOSE_PAREN
12436                            || token->type == CPP_CLOSE_BRACE
12437                            || token->type == CPP_CLOSE_SQUARE)
12438                     --depth;
12439                   break;
12440
12441                 case CPP_OPEN_PAREN:
12442                 case CPP_OPEN_SQUARE:
12443                 case CPP_OPEN_BRACE:
12444                   ++depth;
12445                   break;
12446
12447                 case CPP_GREATER:
12448                   /* If we see a non-nested `>', and `>' is not an
12449                      operator, then it marks the end of the default
12450                      argument.  */
12451                   if (!depth && !greater_than_is_operator_p)
12452                     done = true;
12453                   break;
12454
12455                   /* If we run out of tokens, issue an error message.  */
12456                 case CPP_EOF:
12457                 case CPP_PRAGMA_EOL:
12458                   error ("file ends in default argument");
12459                   done = true;
12460                   break;
12461
12462                 case CPP_NAME:
12463                 case CPP_SCOPE:
12464                   /* In these cases, we should look for template-ids.
12465                      For example, if the default argument is
12466                      `X<int, double>()', we need to do name lookup to
12467                      figure out whether or not `X' is a template; if
12468                      so, the `,' does not end the default argument.
12469
12470                      That is not yet done.  */
12471                   break;
12472
12473                 default:
12474                   break;
12475                 }
12476
12477               /* If we've reached the end, stop.  */
12478               if (done)
12479                 break;
12480
12481               /* Add the token to the token block.  */
12482               token = cp_lexer_consume_token (parser->lexer);
12483             }
12484
12485           /* Create a DEFAULT_ARG to represented the unparsed default
12486              argument.  */
12487           default_argument = make_node (DEFAULT_ARG);
12488           DEFARG_TOKENS (default_argument)
12489             = cp_token_cache_new (first_token, token);
12490           DEFARG_INSTANTIATIONS (default_argument) = NULL;
12491         }
12492       /* Outside of a class definition, we can just parse the
12493          assignment-expression.  */
12494       else
12495         {
12496           bool saved_local_variables_forbidden_p;
12497
12498           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12499              set correctly.  */
12500           saved_greater_than_is_operator_p
12501             = parser->greater_than_is_operator_p;
12502           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12503           /* Local variable names (and the `this' keyword) may not
12504              appear in a default argument.  */
12505           saved_local_variables_forbidden_p
12506             = parser->local_variables_forbidden_p;
12507           parser->local_variables_forbidden_p = true;
12508           /* The default argument expression may cause implicitly
12509              defined member functions to be synthesized, which will
12510              result in garbage collection.  We must treat this
12511              situation as if we were within the body of function so as
12512              to avoid collecting live data on the stack.  */
12513           ++function_depth;
12514           /* Parse the assignment-expression.  */
12515           if (template_parm_p)
12516             push_deferring_access_checks (dk_no_deferred);
12517           default_argument
12518             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12519           if (template_parm_p)
12520             pop_deferring_access_checks ();
12521           /* Restore saved state.  */
12522           --function_depth;
12523           parser->greater_than_is_operator_p
12524             = saved_greater_than_is_operator_p;
12525           parser->local_variables_forbidden_p
12526             = saved_local_variables_forbidden_p;
12527         }
12528       if (!parser->default_arg_ok_p)
12529         {
12530           if (!flag_pedantic_errors)
12531             warning (0, "deprecated use of default argument for parameter of non-function");
12532           else
12533             {
12534               error ("default arguments are only permitted for function parameters");
12535               default_argument = NULL_TREE;
12536             }
12537         }
12538     }
12539   else
12540     default_argument = NULL_TREE;
12541
12542   return make_parameter_declarator (&decl_specifiers,
12543                                     declarator,
12544                                     default_argument);
12545 }
12546
12547 /* Parse a function-body.
12548
12549    function-body:
12550      compound_statement  */
12551
12552 static void
12553 cp_parser_function_body (cp_parser *parser)
12554 {
12555   cp_parser_compound_statement (parser, NULL, false);
12556 }
12557
12558 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12559    true if a ctor-initializer was present.  */
12560
12561 static bool
12562 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12563 {
12564   tree body;
12565   bool ctor_initializer_p;
12566
12567   /* Begin the function body.  */
12568   body = begin_function_body ();
12569   /* Parse the optional ctor-initializer.  */
12570   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12571   /* Parse the function-body.  */
12572   cp_parser_function_body (parser);
12573   /* Finish the function body.  */
12574   finish_function_body (body);
12575
12576   return ctor_initializer_p;
12577 }
12578
12579 /* Parse an initializer.
12580
12581    initializer:
12582      = initializer-clause
12583      ( expression-list )
12584
12585    Returns an expression representing the initializer.  If no
12586    initializer is present, NULL_TREE is returned.
12587
12588    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12589    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12590    set to FALSE if there is no initializer present.  If there is an
12591    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12592    is set to true; otherwise it is set to false.  */
12593
12594 static tree
12595 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12596                        bool* non_constant_p)
12597 {
12598   cp_token *token;
12599   tree init;
12600
12601   /* Peek at the next token.  */
12602   token = cp_lexer_peek_token (parser->lexer);
12603
12604   /* Let our caller know whether or not this initializer was
12605      parenthesized.  */
12606   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12607   /* Assume that the initializer is constant.  */
12608   *non_constant_p = false;
12609
12610   if (token->type == CPP_EQ)
12611     {
12612       /* Consume the `='.  */
12613       cp_lexer_consume_token (parser->lexer);
12614       /* Parse the initializer-clause.  */
12615       init = cp_parser_initializer_clause (parser, non_constant_p);
12616     }
12617   else if (token->type == CPP_OPEN_PAREN)
12618     init = cp_parser_parenthesized_expression_list (parser, false,
12619                                                     /*cast_p=*/false,
12620                                                     non_constant_p);
12621   else
12622     {
12623       /* Anything else is an error.  */
12624       cp_parser_error (parser, "expected initializer");
12625       init = error_mark_node;
12626     }
12627
12628   return init;
12629 }
12630
12631 /* Parse an initializer-clause.
12632
12633    initializer-clause:
12634      assignment-expression
12635      { initializer-list , [opt] }
12636      { }
12637
12638    Returns an expression representing the initializer.
12639
12640    If the `assignment-expression' production is used the value
12641    returned is simply a representation for the expression.
12642
12643    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12644    the elements of the initializer-list (or NULL, if the last
12645    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12646    NULL_TREE.  There is no way to detect whether or not the optional
12647    trailing `,' was provided.  NON_CONSTANT_P is as for
12648    cp_parser_initializer.  */
12649
12650 static tree
12651 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12652 {
12653   tree initializer;
12654
12655   /* Assume the expression is constant.  */
12656   *non_constant_p = false;
12657
12658   /* If it is not a `{', then we are looking at an
12659      assignment-expression.  */
12660   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12661     {
12662       initializer
12663         = cp_parser_constant_expression (parser,
12664                                         /*allow_non_constant_p=*/true,
12665                                         non_constant_p);
12666       if (!*non_constant_p)
12667         initializer = fold_non_dependent_expr (initializer);
12668     }
12669   else
12670     {
12671       /* Consume the `{' token.  */
12672       cp_lexer_consume_token (parser->lexer);
12673       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12674       initializer = make_node (CONSTRUCTOR);
12675       /* If it's not a `}', then there is a non-trivial initializer.  */
12676       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12677         {
12678           /* Parse the initializer list.  */
12679           CONSTRUCTOR_ELTS (initializer)
12680             = cp_parser_initializer_list (parser, non_constant_p);
12681           /* A trailing `,' token is allowed.  */
12682           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12683             cp_lexer_consume_token (parser->lexer);
12684         }
12685       /* Now, there should be a trailing `}'.  */
12686       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12687     }
12688
12689   return initializer;
12690 }
12691
12692 /* Parse an initializer-list.
12693
12694    initializer-list:
12695      initializer-clause
12696      initializer-list , initializer-clause
12697
12698    GNU Extension:
12699
12700    initializer-list:
12701      identifier : initializer-clause
12702      initializer-list, identifier : initializer-clause
12703
12704    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12705    for the initializer.  If the INDEX of the elt is non-NULL, it is the
12706    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12707    as for cp_parser_initializer.  */
12708
12709 static VEC(constructor_elt,gc) *
12710 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12711 {
12712   VEC(constructor_elt,gc) *v = NULL;
12713
12714   /* Assume all of the expressions are constant.  */
12715   *non_constant_p = false;
12716
12717   /* Parse the rest of the list.  */
12718   while (true)
12719     {
12720       cp_token *token;
12721       tree identifier;
12722       tree initializer;
12723       bool clause_non_constant_p;
12724
12725       /* If the next token is an identifier and the following one is a
12726          colon, we are looking at the GNU designated-initializer
12727          syntax.  */
12728       if (cp_parser_allow_gnu_extensions_p (parser)
12729           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12730           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12731         {
12732           /* Consume the identifier.  */
12733           identifier = cp_lexer_consume_token (parser->lexer)->value;
12734           /* Consume the `:'.  */
12735           cp_lexer_consume_token (parser->lexer);
12736         }
12737       else
12738         identifier = NULL_TREE;
12739
12740       /* Parse the initializer.  */
12741       initializer = cp_parser_initializer_clause (parser,
12742                                                   &clause_non_constant_p);
12743       /* If any clause is non-constant, so is the entire initializer.  */
12744       if (clause_non_constant_p)
12745         *non_constant_p = true;
12746
12747       /* Add it to the vector.  */
12748       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12749
12750       /* If the next token is not a comma, we have reached the end of
12751          the list.  */
12752       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12753         break;
12754
12755       /* Peek at the next token.  */
12756       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12757       /* If the next token is a `}', then we're still done.  An
12758          initializer-clause can have a trailing `,' after the
12759          initializer-list and before the closing `}'.  */
12760       if (token->type == CPP_CLOSE_BRACE)
12761         break;
12762
12763       /* Consume the `,' token.  */
12764       cp_lexer_consume_token (parser->lexer);
12765     }
12766
12767   return v;
12768 }
12769
12770 /* Classes [gram.class] */
12771
12772 /* Parse a class-name.
12773
12774    class-name:
12775      identifier
12776      template-id
12777
12778    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12779    to indicate that names looked up in dependent types should be
12780    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12781    keyword has been used to indicate that the name that appears next
12782    is a template.  TAG_TYPE indicates the explicit tag given before
12783    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12784    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12785    is the class being defined in a class-head.
12786
12787    Returns the TYPE_DECL representing the class.  */
12788
12789 static tree
12790 cp_parser_class_name (cp_parser *parser,
12791                       bool typename_keyword_p,
12792                       bool template_keyword_p,
12793                       enum tag_types tag_type,
12794                       bool check_dependency_p,
12795                       bool class_head_p,
12796                       bool is_declaration)
12797 {
12798   tree decl;
12799   tree scope;
12800   bool typename_p;
12801   cp_token *token;
12802
12803   /* All class-names start with an identifier.  */
12804   token = cp_lexer_peek_token (parser->lexer);
12805   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12806     {
12807       cp_parser_error (parser, "expected class-name");
12808       return error_mark_node;
12809     }
12810
12811   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12812      to a template-id, so we save it here.  */
12813   scope = parser->scope;
12814   if (scope == error_mark_node)
12815     return error_mark_node;
12816
12817   /* Any name names a type if we're following the `typename' keyword
12818      in a qualified name where the enclosing scope is type-dependent.  */
12819   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12820                 && dependent_type_p (scope));
12821   /* Handle the common case (an identifier, but not a template-id)
12822      efficiently.  */
12823   if (token->type == CPP_NAME
12824       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12825     {
12826       cp_token *identifier_token;
12827       tree identifier;
12828       bool ambiguous_p;
12829
12830       /* Look for the identifier.  */
12831       identifier_token = cp_lexer_peek_token (parser->lexer);
12832       ambiguous_p = identifier_token->ambiguous_p;
12833       identifier = cp_parser_identifier (parser);
12834       /* If the next token isn't an identifier, we are certainly not
12835          looking at a class-name.  */
12836       if (identifier == error_mark_node)
12837         decl = error_mark_node;
12838       /* If we know this is a type-name, there's no need to look it
12839          up.  */
12840       else if (typename_p)
12841         decl = identifier;
12842       else
12843         {
12844           tree ambiguous_decls;
12845           /* If we already know that this lookup is ambiguous, then
12846              we've already issued an error message; there's no reason
12847              to check again.  */
12848           if (ambiguous_p)
12849             {
12850               cp_parser_simulate_error (parser);
12851               return error_mark_node;
12852             }
12853           /* If the next token is a `::', then the name must be a type
12854              name.
12855
12856              [basic.lookup.qual]
12857
12858              During the lookup for a name preceding the :: scope
12859              resolution operator, object, function, and enumerator
12860              names are ignored.  */
12861           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12862             tag_type = typename_type;
12863           /* Look up the name.  */
12864           decl = cp_parser_lookup_name (parser, identifier,
12865                                         tag_type,
12866                                         /*is_template=*/false,
12867                                         /*is_namespace=*/false,
12868                                         check_dependency_p,
12869                                         &ambiguous_decls);
12870           if (ambiguous_decls)
12871             {
12872               error ("reference to %qD is ambiguous", identifier);
12873               print_candidates (ambiguous_decls);
12874               if (cp_parser_parsing_tentatively (parser))
12875                 {
12876                   identifier_token->ambiguous_p = true;
12877                   cp_parser_simulate_error (parser);
12878                 }
12879               return error_mark_node;
12880             }
12881         }
12882     }
12883   else
12884     {
12885       /* Try a template-id.  */
12886       decl = cp_parser_template_id (parser, template_keyword_p,
12887                                     check_dependency_p,
12888                                     is_declaration);
12889       if (decl == error_mark_node)
12890         return error_mark_node;
12891     }
12892
12893   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12894
12895   /* If this is a typename, create a TYPENAME_TYPE.  */
12896   if (typename_p && decl != error_mark_node)
12897     {
12898       decl = make_typename_type (scope, decl, typename_type,
12899                                  /*complain=*/tf_error);
12900       if (decl != error_mark_node)
12901         decl = TYPE_NAME (decl);
12902     }
12903
12904   /* Check to see that it is really the name of a class.  */
12905   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12906       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12907       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12908     /* Situations like this:
12909
12910          template <typename T> struct A {
12911            typename T::template X<int>::I i;
12912          };
12913
12914        are problematic.  Is `T::template X<int>' a class-name?  The
12915        standard does not seem to be definitive, but there is no other
12916        valid interpretation of the following `::'.  Therefore, those
12917        names are considered class-names.  */
12918     {
12919       decl = make_typename_type (scope, decl, tag_type, tf_error);
12920       if (decl != error_mark_node)
12921         decl = TYPE_NAME (decl);
12922     }
12923   else if (TREE_CODE (decl) != TYPE_DECL
12924            || TREE_TYPE (decl) == error_mark_node
12925            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12926     decl = error_mark_node;
12927
12928   if (decl == error_mark_node)
12929     cp_parser_error (parser, "expected class-name");
12930
12931   return decl;
12932 }
12933
12934 /* Parse a class-specifier.
12935
12936    class-specifier:
12937      class-head { member-specification [opt] }
12938
12939    Returns the TREE_TYPE representing the class.  */
12940
12941 static tree
12942 cp_parser_class_specifier (cp_parser* parser)
12943 {
12944   cp_token *token;
12945   tree type;
12946   tree attributes = NULL_TREE;
12947   int has_trailing_semicolon;
12948   bool nested_name_specifier_p;
12949   unsigned saved_num_template_parameter_lists;
12950   tree old_scope = NULL_TREE;
12951   tree scope = NULL_TREE;
12952
12953   push_deferring_access_checks (dk_no_deferred);
12954
12955   /* Parse the class-head.  */
12956   type = cp_parser_class_head (parser,
12957                                &nested_name_specifier_p,
12958                                &attributes);
12959   /* If the class-head was a semantic disaster, skip the entire body
12960      of the class.  */
12961   if (!type)
12962     {
12963       cp_parser_skip_to_end_of_block_or_statement (parser);
12964       pop_deferring_access_checks ();
12965       return error_mark_node;
12966     }
12967
12968   /* Look for the `{'.  */
12969   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12970     {
12971       pop_deferring_access_checks ();
12972       return error_mark_node;
12973     }
12974
12975   /* Issue an error message if type-definitions are forbidden here.  */
12976   cp_parser_check_type_definition (parser);
12977   /* Remember that we are defining one more class.  */
12978   ++parser->num_classes_being_defined;
12979   /* Inside the class, surrounding template-parameter-lists do not
12980      apply.  */
12981   saved_num_template_parameter_lists
12982     = parser->num_template_parameter_lists;
12983   parser->num_template_parameter_lists = 0;
12984
12985   /* Start the class.  */
12986   if (nested_name_specifier_p)
12987     {
12988       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12989       old_scope = push_inner_scope (scope);
12990     }
12991   type = begin_class_definition (type, attributes);
12992
12993   if (type == error_mark_node)
12994     /* If the type is erroneous, skip the entire body of the class.  */
12995     cp_parser_skip_to_closing_brace (parser);
12996   else
12997     /* Parse the member-specification.  */
12998     cp_parser_member_specification_opt (parser);
12999
13000   /* Look for the trailing `}'.  */
13001   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13002   /* We get better error messages by noticing a common problem: a
13003      missing trailing `;'.  */
13004   token = cp_lexer_peek_token (parser->lexer);
13005   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13006   /* Look for trailing attributes to apply to this class.  */
13007   if (cp_parser_allow_gnu_extensions_p (parser))
13008     attributes = cp_parser_attributes_opt (parser);
13009   if (type != error_mark_node)
13010     type = finish_struct (type, attributes);
13011   if (nested_name_specifier_p)
13012     pop_inner_scope (old_scope, scope);
13013   /* If this class is not itself within the scope of another class,
13014      then we need to parse the bodies of all of the queued function
13015      definitions.  Note that the queued functions defined in a class
13016      are not always processed immediately following the
13017      class-specifier for that class.  Consider:
13018
13019        struct A {
13020          struct B { void f() { sizeof (A); } };
13021        };
13022
13023      If `f' were processed before the processing of `A' were
13024      completed, there would be no way to compute the size of `A'.
13025      Note that the nesting we are interested in here is lexical --
13026      not the semantic nesting given by TYPE_CONTEXT.  In particular,
13027      for:
13028
13029        struct A { struct B; };
13030        struct A::B { void f() { } };
13031
13032      there is no need to delay the parsing of `A::B::f'.  */
13033   if (--parser->num_classes_being_defined == 0)
13034     {
13035       tree queue_entry;
13036       tree fn;
13037       tree class_type = NULL_TREE;
13038       tree pushed_scope = NULL_TREE;
13039
13040       /* In a first pass, parse default arguments to the functions.
13041          Then, in a second pass, parse the bodies of the functions.
13042          This two-phased approach handles cases like:
13043
13044             struct S {
13045               void f() { g(); }
13046               void g(int i = 3);
13047             };
13048
13049          */
13050       for (TREE_PURPOSE (parser->unparsed_functions_queues)
13051              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13052            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13053            TREE_PURPOSE (parser->unparsed_functions_queues)
13054              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13055         {
13056           fn = TREE_VALUE (queue_entry);
13057           /* If there are default arguments that have not yet been processed,
13058              take care of them now.  */
13059           if (class_type != TREE_PURPOSE (queue_entry))
13060             {
13061               if (pushed_scope)
13062                 pop_scope (pushed_scope);
13063               class_type = TREE_PURPOSE (queue_entry);
13064               pushed_scope = push_scope (class_type);
13065             }
13066           /* Make sure that any template parameters are in scope.  */
13067           maybe_begin_member_template_processing (fn);
13068           /* Parse the default argument expressions.  */
13069           cp_parser_late_parsing_default_args (parser, fn);
13070           /* Remove any template parameters from the symbol table.  */
13071           maybe_end_member_template_processing ();
13072         }
13073       if (pushed_scope)
13074         pop_scope (pushed_scope);
13075       /* Now parse the body of the functions.  */
13076       for (TREE_VALUE (parser->unparsed_functions_queues)
13077              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13078            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13079            TREE_VALUE (parser->unparsed_functions_queues)
13080              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13081         {
13082           /* Figure out which function we need to process.  */
13083           fn = TREE_VALUE (queue_entry);
13084           /* Parse the function.  */
13085           cp_parser_late_parsing_for_member (parser, fn);
13086         }
13087     }
13088
13089   /* Put back any saved access checks.  */
13090   pop_deferring_access_checks ();
13091
13092   /* Restore the count of active template-parameter-lists.  */
13093   parser->num_template_parameter_lists
13094     = saved_num_template_parameter_lists;
13095
13096   return type;
13097 }
13098
13099 /* Parse a class-head.
13100
13101    class-head:
13102      class-key identifier [opt] base-clause [opt]
13103      class-key nested-name-specifier identifier base-clause [opt]
13104      class-key nested-name-specifier [opt] template-id
13105        base-clause [opt]
13106
13107    GNU Extensions:
13108      class-key attributes identifier [opt] base-clause [opt]
13109      class-key attributes nested-name-specifier identifier base-clause [opt]
13110      class-key attributes nested-name-specifier [opt] template-id
13111        base-clause [opt]
13112
13113    Returns the TYPE of the indicated class.  Sets
13114    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13115    involving a nested-name-specifier was used, and FALSE otherwise.
13116
13117    Returns error_mark_node if this is not a class-head.
13118
13119    Returns NULL_TREE if the class-head is syntactically valid, but
13120    semantically invalid in a way that means we should skip the entire
13121    body of the class.  */
13122
13123 static tree
13124 cp_parser_class_head (cp_parser* parser,
13125                       bool* nested_name_specifier_p,
13126                       tree *attributes_p)
13127 {
13128   tree nested_name_specifier;
13129   enum tag_types class_key;
13130   tree id = NULL_TREE;
13131   tree type = NULL_TREE;
13132   tree attributes;
13133   bool template_id_p = false;
13134   bool qualified_p = false;
13135   bool invalid_nested_name_p = false;
13136   bool invalid_explicit_specialization_p = false;
13137   tree pushed_scope = NULL_TREE;
13138   unsigned num_templates;
13139   tree bases;
13140
13141   /* Assume no nested-name-specifier will be present.  */
13142   *nested_name_specifier_p = false;
13143   /* Assume no template parameter lists will be used in defining the
13144      type.  */
13145   num_templates = 0;
13146
13147   /* Look for the class-key.  */
13148   class_key = cp_parser_class_key (parser);
13149   if (class_key == none_type)
13150     return error_mark_node;
13151
13152   /* Parse the attributes.  */
13153   attributes = cp_parser_attributes_opt (parser);
13154
13155   /* If the next token is `::', that is invalid -- but sometimes
13156      people do try to write:
13157
13158        struct ::S {};
13159
13160      Handle this gracefully by accepting the extra qualifier, and then
13161      issuing an error about it later if this really is a
13162      class-head.  If it turns out just to be an elaborated type
13163      specifier, remain silent.  */
13164   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false,
13165                                   /*object_scope_valid_p=*/false))
13166     qualified_p = true;
13167
13168   push_deferring_access_checks (dk_no_check);
13169
13170   /* Determine the name of the class.  Begin by looking for an
13171      optional nested-name-specifier.  */
13172   nested_name_specifier
13173     = cp_parser_nested_name_specifier_opt (parser,
13174                                            /*typename_keyword_p=*/false,
13175                                            /*check_dependency_p=*/false,
13176                                            /*type_p=*/false,
13177                                            /*is_declaration=*/false);
13178   /* If there was a nested-name-specifier, then there *must* be an
13179      identifier.  */
13180   if (nested_name_specifier)
13181     {
13182       /* Although the grammar says `identifier', it really means
13183          `class-name' or `template-name'.  You are only allowed to
13184          define a class that has already been declared with this
13185          syntax.
13186
13187          The proposed resolution for Core Issue 180 says that wherever
13188          you see `class T::X' you should treat `X' as a type-name.
13189
13190          It is OK to define an inaccessible class; for example:
13191
13192            class A { class B; };
13193            class A::B {};
13194
13195          We do not know if we will see a class-name, or a
13196          template-name.  We look for a class-name first, in case the
13197          class-name is a template-id; if we looked for the
13198          template-name first we would stop after the template-name.  */
13199       cp_parser_parse_tentatively (parser);
13200       type = cp_parser_class_name (parser,
13201                                    /*typename_keyword_p=*/false,
13202                                    /*template_keyword_p=*/false,
13203                                    class_type,
13204                                    /*check_dependency_p=*/false,
13205                                    /*class_head_p=*/true,
13206                                    /*is_declaration=*/false);
13207       /* If that didn't work, ignore the nested-name-specifier.  */
13208       if (!cp_parser_parse_definitely (parser))
13209         {
13210           invalid_nested_name_p = true;
13211           id = cp_parser_identifier (parser);
13212           if (id == error_mark_node)
13213             id = NULL_TREE;
13214         }
13215       /* If we could not find a corresponding TYPE, treat this
13216          declaration like an unqualified declaration.  */
13217       if (type == error_mark_node)
13218         nested_name_specifier = NULL_TREE;
13219       /* Otherwise, count the number of templates used in TYPE and its
13220          containing scopes.  */
13221       else
13222         {
13223           tree scope;
13224
13225           for (scope = TREE_TYPE (type);
13226                scope && TREE_CODE (scope) != NAMESPACE_DECL;
13227                scope = (TYPE_P (scope)
13228                         ? TYPE_CONTEXT (scope)
13229                         : DECL_CONTEXT (scope)))
13230             if (TYPE_P (scope)
13231                 && CLASS_TYPE_P (scope)
13232                 && CLASSTYPE_TEMPLATE_INFO (scope)
13233                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13234                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13235               ++num_templates;
13236         }
13237     }
13238   /* Otherwise, the identifier is optional.  */
13239   else
13240     {
13241       /* We don't know whether what comes next is a template-id,
13242          an identifier, or nothing at all.  */
13243       cp_parser_parse_tentatively (parser);
13244       /* Check for a template-id.  */
13245       id = cp_parser_template_id (parser,
13246                                   /*template_keyword_p=*/false,
13247                                   /*check_dependency_p=*/true,
13248                                   /*is_declaration=*/true);
13249       /* If that didn't work, it could still be an identifier.  */
13250       if (!cp_parser_parse_definitely (parser))
13251         {
13252           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13253             id = cp_parser_identifier (parser);
13254           else
13255             id = NULL_TREE;
13256         }
13257       else
13258         {
13259           template_id_p = true;
13260           ++num_templates;
13261         }
13262     }
13263
13264   pop_deferring_access_checks ();
13265
13266   if (id)
13267     cp_parser_check_for_invalid_template_id (parser, id);
13268
13269   /* If it's not a `:' or a `{' then we can't really be looking at a
13270      class-head, since a class-head only appears as part of a
13271      class-specifier.  We have to detect this situation before calling
13272      xref_tag, since that has irreversible side-effects.  */
13273   if (!cp_parser_next_token_starts_class_definition_p (parser))
13274     {
13275       cp_parser_error (parser, "expected %<{%> or %<:%>");
13276       return error_mark_node;
13277     }
13278
13279   /* At this point, we're going ahead with the class-specifier, even
13280      if some other problem occurs.  */
13281   cp_parser_commit_to_tentative_parse (parser);
13282   /* Issue the error about the overly-qualified name now.  */
13283   if (qualified_p)
13284     cp_parser_error (parser,
13285                      "global qualification of class name is invalid");
13286   else if (invalid_nested_name_p)
13287     cp_parser_error (parser,
13288                      "qualified name does not name a class");
13289   else if (nested_name_specifier)
13290     {
13291       tree scope;
13292
13293       /* Reject typedef-names in class heads.  */
13294       if (!DECL_IMPLICIT_TYPEDEF_P (type))
13295         {
13296           error ("invalid class name in declaration of %qD", type);
13297           type = NULL_TREE;
13298           goto done;
13299         }
13300
13301       /* Figure out in what scope the declaration is being placed.  */
13302       scope = current_scope ();
13303       /* If that scope does not contain the scope in which the
13304          class was originally declared, the program is invalid.  */
13305       if (scope && !is_ancestor (scope, nested_name_specifier))
13306         {
13307           error ("declaration of %qD in %qD which does not enclose %qD",
13308                  type, scope, nested_name_specifier);
13309           type = NULL_TREE;
13310           goto done;
13311         }
13312       /* [dcl.meaning]
13313
13314          A declarator-id shall not be qualified exception of the
13315          definition of a ... nested class outside of its class
13316          ... [or] a the definition or explicit instantiation of a
13317          class member of a namespace outside of its namespace.  */
13318       if (scope == nested_name_specifier)
13319         {
13320           pedwarn ("extra qualification ignored");
13321           nested_name_specifier = NULL_TREE;
13322           num_templates = 0;
13323         }
13324     }
13325   /* An explicit-specialization must be preceded by "template <>".  If
13326      it is not, try to recover gracefully.  */
13327   if (at_namespace_scope_p ()
13328       && parser->num_template_parameter_lists == 0
13329       && template_id_p)
13330     {
13331       error ("an explicit specialization must be preceded by %<template <>%>");
13332       invalid_explicit_specialization_p = true;
13333       /* Take the same action that would have been taken by
13334          cp_parser_explicit_specialization.  */
13335       ++parser->num_template_parameter_lists;
13336       begin_specialization ();
13337     }
13338   /* There must be no "return" statements between this point and the
13339      end of this function; set "type "to the correct return value and
13340      use "goto done;" to return.  */
13341   /* Make sure that the right number of template parameters were
13342      present.  */
13343   if (!cp_parser_check_template_parameters (parser, num_templates))
13344     {
13345       /* If something went wrong, there is no point in even trying to
13346          process the class-definition.  */
13347       type = NULL_TREE;
13348       goto done;
13349     }
13350
13351   /* Look up the type.  */
13352   if (template_id_p)
13353     {
13354       type = TREE_TYPE (id);
13355       type = maybe_process_partial_specialization (type);
13356       if (nested_name_specifier)
13357         pushed_scope = push_scope (nested_name_specifier);
13358     }
13359   else if (nested_name_specifier)
13360     {
13361       tree class_type;
13362
13363       /* Given:
13364
13365             template <typename T> struct S { struct T };
13366             template <typename T> struct S<T>::T { };
13367
13368          we will get a TYPENAME_TYPE when processing the definition of
13369          `S::T'.  We need to resolve it to the actual type before we
13370          try to define it.  */
13371       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13372         {
13373           class_type = resolve_typename_type (TREE_TYPE (type),
13374                                               /*only_current_p=*/false);
13375           if (class_type != error_mark_node)
13376             type = TYPE_NAME (class_type);
13377           else
13378             {
13379               cp_parser_error (parser, "could not resolve typename type");
13380               type = error_mark_node;
13381             }
13382         }
13383
13384       maybe_process_partial_specialization (TREE_TYPE (type));
13385       class_type = current_class_type;
13386       /* Enter the scope indicated by the nested-name-specifier.  */
13387       pushed_scope = push_scope (nested_name_specifier);
13388       /* Get the canonical version of this type.  */
13389       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13390       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13391           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13392         {
13393           type = push_template_decl (type);
13394           if (type == error_mark_node)
13395             {
13396               type = NULL_TREE;
13397               goto done;
13398             }
13399         }
13400
13401       type = TREE_TYPE (type);
13402       *nested_name_specifier_p = true;
13403     }
13404   else      /* The name is not a nested name.  */
13405     {
13406       /* If the class was unnamed, create a dummy name.  */
13407       if (!id)
13408         id = make_anon_name ();
13409       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13410                        parser->num_template_parameter_lists);
13411     }
13412
13413   /* Indicate whether this class was declared as a `class' or as a
13414      `struct'.  */
13415   if (TREE_CODE (type) == RECORD_TYPE)
13416     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13417   cp_parser_check_class_key (class_key, type);
13418
13419   /* If this type was already complete, and we see another definition,
13420      that's an error.  */
13421   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13422     {
13423       error ("redefinition of %q#T", type);
13424       error ("previous definition of %q+#T", type);
13425       type = NULL_TREE;
13426       goto done;
13427     }
13428
13429   /* We will have entered the scope containing the class; the names of
13430      base classes should be looked up in that context.  For example:
13431
13432        struct A { struct B {}; struct C; };
13433        struct A::C : B {};
13434
13435      is valid.  */
13436   bases = NULL_TREE;
13437
13438   /* Get the list of base-classes, if there is one.  */
13439   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13440     bases = cp_parser_base_clause (parser);
13441
13442   /* Process the base classes.  */
13443   xref_basetypes (type, bases);
13444
13445  done:
13446   /* Leave the scope given by the nested-name-specifier.  We will
13447      enter the class scope itself while processing the members.  */
13448   if (pushed_scope)
13449     pop_scope (pushed_scope);
13450
13451   if (invalid_explicit_specialization_p)
13452     {
13453       end_specialization ();
13454       --parser->num_template_parameter_lists;
13455     }
13456   *attributes_p = attributes;
13457   return type;
13458 }
13459
13460 /* Parse a class-key.
13461
13462    class-key:
13463      class
13464      struct
13465      union
13466
13467    Returns the kind of class-key specified, or none_type to indicate
13468    error.  */
13469
13470 static enum tag_types
13471 cp_parser_class_key (cp_parser* parser)
13472 {
13473   cp_token *token;
13474   enum tag_types tag_type;
13475
13476   /* Look for the class-key.  */
13477   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13478   if (!token)
13479     return none_type;
13480
13481   /* Check to see if the TOKEN is a class-key.  */
13482   tag_type = cp_parser_token_is_class_key (token);
13483   if (!tag_type)
13484     cp_parser_error (parser, "expected class-key");
13485   return tag_type;
13486 }
13487
13488 /* Parse an (optional) member-specification.
13489
13490    member-specification:
13491      member-declaration member-specification [opt]
13492      access-specifier : member-specification [opt]  */
13493
13494 static void
13495 cp_parser_member_specification_opt (cp_parser* parser)
13496 {
13497   while (true)
13498     {
13499       cp_token *token;
13500       enum rid keyword;
13501
13502       /* Peek at the next token.  */
13503       token = cp_lexer_peek_token (parser->lexer);
13504       /* If it's a `}', or EOF then we've seen all the members.  */
13505       if (token->type == CPP_CLOSE_BRACE
13506           || token->type == CPP_EOF
13507           || token->type == CPP_PRAGMA_EOL)
13508         break;
13509
13510       /* See if this token is a keyword.  */
13511       keyword = token->keyword;
13512       switch (keyword)
13513         {
13514         case RID_PUBLIC:
13515         case RID_PROTECTED:
13516         case RID_PRIVATE:
13517           /* Consume the access-specifier.  */
13518           cp_lexer_consume_token (parser->lexer);
13519           /* Remember which access-specifier is active.  */
13520           current_access_specifier = token->value;
13521           /* Look for the `:'.  */
13522           cp_parser_require (parser, CPP_COLON, "`:'");
13523           break;
13524
13525         default:
13526           /* Accept #pragmas at class scope.  */
13527           if (token->type == CPP_PRAGMA)
13528             {
13529               cp_parser_pragma (parser, pragma_external);
13530               break;
13531             }
13532
13533           /* Otherwise, the next construction must be a
13534              member-declaration.  */
13535           cp_parser_member_declaration (parser);
13536         }
13537     }
13538 }
13539
13540 /* Parse a member-declaration.
13541
13542    member-declaration:
13543      decl-specifier-seq [opt] member-declarator-list [opt] ;
13544      function-definition ; [opt]
13545      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13546      using-declaration
13547      template-declaration
13548
13549    member-declarator-list:
13550      member-declarator
13551      member-declarator-list , member-declarator
13552
13553    member-declarator:
13554      declarator pure-specifier [opt]
13555      declarator constant-initializer [opt]
13556      identifier [opt] : constant-expression
13557
13558    GNU Extensions:
13559
13560    member-declaration:
13561      __extension__ member-declaration
13562
13563    member-declarator:
13564      declarator attributes [opt] pure-specifier [opt]
13565      declarator attributes [opt] constant-initializer [opt]
13566      identifier [opt] attributes [opt] : constant-expression  */
13567
13568 static void
13569 cp_parser_member_declaration (cp_parser* parser)
13570 {
13571   cp_decl_specifier_seq decl_specifiers;
13572   tree prefix_attributes;
13573   tree decl;
13574   int declares_class_or_enum;
13575   bool friend_p;
13576   cp_token *token;
13577   int saved_pedantic;
13578
13579   /* Check for the `__extension__' keyword.  */
13580   if (cp_parser_extension_opt (parser, &saved_pedantic))
13581     {
13582       /* Recurse.  */
13583       cp_parser_member_declaration (parser);
13584       /* Restore the old value of the PEDANTIC flag.  */
13585       pedantic = saved_pedantic;
13586
13587       return;
13588     }
13589
13590   /* Check for a template-declaration.  */
13591   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13592     {
13593       /* An explicit specialization here is an error condition, and we
13594          expect the specialization handler to detect and report this.  */
13595       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13596           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13597         cp_parser_explicit_specialization (parser);
13598       else
13599         cp_parser_template_declaration (parser, /*member_p=*/true);
13600
13601       return;
13602     }
13603
13604   /* Check for a using-declaration.  */
13605   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13606     {
13607       /* Parse the using-declaration.  */
13608       cp_parser_using_declaration (parser);
13609
13610       return;
13611     }
13612
13613   /* Check for @defs.  */
13614   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13615     {
13616       tree ivar, member;
13617       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13618       ivar = ivar_chains;
13619       while (ivar)
13620         {
13621           member = ivar;
13622           ivar = TREE_CHAIN (member);
13623           TREE_CHAIN (member) = NULL_TREE;
13624           finish_member_declaration (member);
13625         }
13626       return;
13627     }
13628
13629   /* Parse the decl-specifier-seq.  */
13630   cp_parser_decl_specifier_seq (parser,
13631                                 CP_PARSER_FLAGS_OPTIONAL,
13632                                 &decl_specifiers,
13633                                 &declares_class_or_enum);
13634   prefix_attributes = decl_specifiers.attributes;
13635   decl_specifiers.attributes = NULL_TREE;
13636   /* Check for an invalid type-name.  */
13637   if (!decl_specifiers.type
13638       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13639     return;
13640   /* If there is no declarator, then the decl-specifier-seq should
13641      specify a type.  */
13642   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13643     {
13644       /* If there was no decl-specifier-seq, and the next token is a
13645          `;', then we have something like:
13646
13647            struct S { ; };
13648
13649          [class.mem]
13650
13651          Each member-declaration shall declare at least one member
13652          name of the class.  */
13653       if (!decl_specifiers.any_specifiers_p)
13654         {
13655           cp_token *token = cp_lexer_peek_token (parser->lexer);
13656           if (pedantic && !token->in_system_header)
13657             pedwarn ("%Hextra %<;%>", &token->location);
13658         }
13659       else
13660         {
13661           tree type;
13662
13663           /* See if this declaration is a friend.  */
13664           friend_p = cp_parser_friend_p (&decl_specifiers);
13665           /* If there were decl-specifiers, check to see if there was
13666              a class-declaration.  */
13667           type = check_tag_decl (&decl_specifiers);
13668           /* Nested classes have already been added to the class, but
13669              a `friend' needs to be explicitly registered.  */
13670           if (friend_p)
13671             {
13672               /* If the `friend' keyword was present, the friend must
13673                  be introduced with a class-key.  */
13674                if (!declares_class_or_enum)
13675                  error ("a class-key must be used when declaring a friend");
13676                /* In this case:
13677
13678                     template <typename T> struct A {
13679                       friend struct A<T>::B;
13680                     };
13681
13682                   A<T>::B will be represented by a TYPENAME_TYPE, and
13683                   therefore not recognized by check_tag_decl.  */
13684                if (!type
13685                    && decl_specifiers.type
13686                    && TYPE_P (decl_specifiers.type))
13687                  type = decl_specifiers.type;
13688                if (!type || !TYPE_P (type))
13689                  error ("friend declaration does not name a class or "
13690                         "function");
13691                else
13692                  make_friend_class (current_class_type, type,
13693                                     /*complain=*/true);
13694             }
13695           /* If there is no TYPE, an error message will already have
13696              been issued.  */
13697           else if (!type || type == error_mark_node)
13698             ;
13699           /* An anonymous aggregate has to be handled specially; such
13700              a declaration really declares a data member (with a
13701              particular type), as opposed to a nested class.  */
13702           else if (ANON_AGGR_TYPE_P (type))
13703             {
13704               /* Remove constructors and such from TYPE, now that we
13705                  know it is an anonymous aggregate.  */
13706               fixup_anonymous_aggr (type);
13707               /* And make the corresponding data member.  */
13708               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13709               /* Add it to the class.  */
13710               finish_member_declaration (decl);
13711             }
13712           else
13713             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13714         }
13715     }
13716   else
13717     {
13718       /* See if these declarations will be friends.  */
13719       friend_p = cp_parser_friend_p (&decl_specifiers);
13720
13721       /* Keep going until we hit the `;' at the end of the
13722          declaration.  */
13723       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13724         {
13725           tree attributes = NULL_TREE;
13726           tree first_attribute;
13727
13728           /* Peek at the next token.  */
13729           token = cp_lexer_peek_token (parser->lexer);
13730
13731           /* Check for a bitfield declaration.  */
13732           if (token->type == CPP_COLON
13733               || (token->type == CPP_NAME
13734                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13735                   == CPP_COLON))
13736             {
13737               tree identifier;
13738               tree width;
13739
13740               /* Get the name of the bitfield.  Note that we cannot just
13741                  check TOKEN here because it may have been invalidated by
13742                  the call to cp_lexer_peek_nth_token above.  */
13743               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13744                 identifier = cp_parser_identifier (parser);
13745               else
13746                 identifier = NULL_TREE;
13747
13748               /* Consume the `:' token.  */
13749               cp_lexer_consume_token (parser->lexer);
13750               /* Get the width of the bitfield.  */
13751               width
13752                 = cp_parser_constant_expression (parser,
13753                                                  /*allow_non_constant=*/false,
13754                                                  NULL);
13755
13756               /* Look for attributes that apply to the bitfield.  */
13757               attributes = cp_parser_attributes_opt (parser);
13758               /* Remember which attributes are prefix attributes and
13759                  which are not.  */
13760               first_attribute = attributes;
13761               /* Combine the attributes.  */
13762               attributes = chainon (prefix_attributes, attributes);
13763
13764               /* Create the bitfield declaration.  */
13765               decl = grokbitfield (identifier
13766                                    ? make_id_declarator (NULL_TREE,
13767                                                          identifier,
13768                                                          sfk_none)
13769                                    : NULL,
13770                                    &decl_specifiers,
13771                                    width);
13772               /* Apply the attributes.  */
13773               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13774             }
13775           else
13776             {
13777               cp_declarator *declarator;
13778               tree initializer;
13779               tree asm_specification;
13780               int ctor_dtor_or_conv_p;
13781
13782               /* Parse the declarator.  */
13783               declarator
13784                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13785                                         &ctor_dtor_or_conv_p,
13786                                         /*parenthesized_p=*/NULL,
13787                                         /*member_p=*/true);
13788
13789               /* If something went wrong parsing the declarator, make sure
13790                  that we at least consume some tokens.  */
13791               if (declarator == cp_error_declarator)
13792                 {
13793                   /* Skip to the end of the statement.  */
13794                   cp_parser_skip_to_end_of_statement (parser);
13795                   /* If the next token is not a semicolon, that is
13796                      probably because we just skipped over the body of
13797                      a function.  So, we consume a semicolon if
13798                      present, but do not issue an error message if it
13799                      is not present.  */
13800                   if (cp_lexer_next_token_is (parser->lexer,
13801                                               CPP_SEMICOLON))
13802                     cp_lexer_consume_token (parser->lexer);
13803                   return;
13804                 }
13805
13806               if (declares_class_or_enum & 2)
13807                 cp_parser_check_for_definition_in_return_type
13808                   (declarator, decl_specifiers.type);
13809
13810               /* Look for an asm-specification.  */
13811               asm_specification = cp_parser_asm_specification_opt (parser);
13812               /* Look for attributes that apply to the declaration.  */
13813               attributes = cp_parser_attributes_opt (parser);
13814               /* Remember which attributes are prefix attributes and
13815                  which are not.  */
13816               first_attribute = attributes;
13817               /* Combine the attributes.  */
13818               attributes = chainon (prefix_attributes, attributes);
13819
13820               /* If it's an `=', then we have a constant-initializer or a
13821                  pure-specifier.  It is not correct to parse the
13822                  initializer before registering the member declaration
13823                  since the member declaration should be in scope while
13824                  its initializer is processed.  However, the rest of the
13825                  front end does not yet provide an interface that allows
13826                  us to handle this correctly.  */
13827               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13828                 {
13829                   /* In [class.mem]:
13830
13831                      A pure-specifier shall be used only in the declaration of
13832                      a virtual function.
13833
13834                      A member-declarator can contain a constant-initializer
13835                      only if it declares a static member of integral or
13836                      enumeration type.
13837
13838                      Therefore, if the DECLARATOR is for a function, we look
13839                      for a pure-specifier; otherwise, we look for a
13840                      constant-initializer.  When we call `grokfield', it will
13841                      perform more stringent semantics checks.  */
13842                   if (declarator->kind == cdk_function
13843                       && declarator->declarator->kind == cdk_id)
13844                     initializer = cp_parser_pure_specifier (parser);
13845                   else
13846                     /* Parse the initializer.  */
13847                     initializer = cp_parser_constant_initializer (parser);
13848                 }
13849               /* Otherwise, there is no initializer.  */
13850               else
13851                 initializer = NULL_TREE;
13852
13853               /* See if we are probably looking at a function
13854                  definition.  We are certainly not looking at a
13855                  member-declarator.  Calling `grokfield' has
13856                  side-effects, so we must not do it unless we are sure
13857                  that we are looking at a member-declarator.  */
13858               if (cp_parser_token_starts_function_definition_p
13859                   (cp_lexer_peek_token (parser->lexer)))
13860                 {
13861                   /* The grammar does not allow a pure-specifier to be
13862                      used when a member function is defined.  (It is
13863                      possible that this fact is an oversight in the
13864                      standard, since a pure function may be defined
13865                      outside of the class-specifier.  */
13866                   if (initializer)
13867                     error ("pure-specifier on function-definition");
13868                   decl = cp_parser_save_member_function_body (parser,
13869                                                               &decl_specifiers,
13870                                                               declarator,
13871                                                               attributes);
13872                   /* If the member was not a friend, declare it here.  */
13873                   if (!friend_p)
13874                     finish_member_declaration (decl);
13875                   /* Peek at the next token.  */
13876                   token = cp_lexer_peek_token (parser->lexer);
13877                   /* If the next token is a semicolon, consume it.  */
13878                   if (token->type == CPP_SEMICOLON)
13879                     cp_lexer_consume_token (parser->lexer);
13880                   return;
13881                 }
13882               else
13883                 /* Create the declaration.  */
13884                 decl = grokfield (declarator, &decl_specifiers,
13885                                   initializer, /*init_const_expr_p=*/true,
13886                                   asm_specification,
13887                                   attributes);
13888             }
13889
13890           /* Reset PREFIX_ATTRIBUTES.  */
13891           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13892             attributes = TREE_CHAIN (attributes);
13893           if (attributes)
13894             TREE_CHAIN (attributes) = NULL_TREE;
13895
13896           /* If there is any qualification still in effect, clear it
13897              now; we will be starting fresh with the next declarator.  */
13898           parser->scope = NULL_TREE;
13899           parser->qualifying_scope = NULL_TREE;
13900           parser->object_scope = NULL_TREE;
13901           /* If it's a `,', then there are more declarators.  */
13902           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13903             cp_lexer_consume_token (parser->lexer);
13904           /* If the next token isn't a `;', then we have a parse error.  */
13905           else if (cp_lexer_next_token_is_not (parser->lexer,
13906                                                CPP_SEMICOLON))
13907             {
13908               cp_parser_error (parser, "expected %<;%>");
13909               /* Skip tokens until we find a `;'.  */
13910               cp_parser_skip_to_end_of_statement (parser);
13911
13912               break;
13913             }
13914
13915           if (decl)
13916             {
13917               /* Add DECL to the list of members.  */
13918               if (!friend_p)
13919                 finish_member_declaration (decl);
13920
13921               if (TREE_CODE (decl) == FUNCTION_DECL)
13922                 cp_parser_save_default_args (parser, decl);
13923             }
13924         }
13925     }
13926
13927   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13928 }
13929
13930 /* Parse a pure-specifier.
13931
13932    pure-specifier:
13933      = 0
13934
13935    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13936    Otherwise, ERROR_MARK_NODE is returned.  */
13937
13938 static tree
13939 cp_parser_pure_specifier (cp_parser* parser)
13940 {
13941   cp_token *token;
13942
13943   /* Look for the `=' token.  */
13944   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13945     return error_mark_node;
13946   /* Look for the `0' token.  */
13947   token = cp_lexer_consume_token (parser->lexer);
13948   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
13949   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
13950     {
13951       cp_parser_error (parser,
13952                        "invalid pure specifier (only `= 0' is allowed)");
13953       cp_parser_skip_to_end_of_statement (parser);
13954       return error_mark_node;
13955     }
13956   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
13957     {
13958       error ("templates may not be %<virtual%>");
13959       return error_mark_node;
13960     }
13961
13962   return integer_zero_node;
13963 }
13964
13965 /* Parse a constant-initializer.
13966
13967    constant-initializer:
13968      = constant-expression
13969
13970    Returns a representation of the constant-expression.  */
13971
13972 static tree
13973 cp_parser_constant_initializer (cp_parser* parser)
13974 {
13975   /* Look for the `=' token.  */
13976   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13977     return error_mark_node;
13978
13979   /* It is invalid to write:
13980
13981        struct S { static const int i = { 7 }; };
13982
13983      */
13984   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13985     {
13986       cp_parser_error (parser,
13987                        "a brace-enclosed initializer is not allowed here");
13988       /* Consume the opening brace.  */
13989       cp_lexer_consume_token (parser->lexer);
13990       /* Skip the initializer.  */
13991       cp_parser_skip_to_closing_brace (parser);
13992       /* Look for the trailing `}'.  */
13993       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13994
13995       return error_mark_node;
13996     }
13997
13998   return cp_parser_constant_expression (parser,
13999                                         /*allow_non_constant=*/false,
14000                                         NULL);
14001 }
14002
14003 /* Derived classes [gram.class.derived] */
14004
14005 /* Parse a base-clause.
14006
14007    base-clause:
14008      : base-specifier-list
14009
14010    base-specifier-list:
14011      base-specifier
14012      base-specifier-list , base-specifier
14013
14014    Returns a TREE_LIST representing the base-classes, in the order in
14015    which they were declared.  The representation of each node is as
14016    described by cp_parser_base_specifier.
14017
14018    In the case that no bases are specified, this function will return
14019    NULL_TREE, not ERROR_MARK_NODE.  */
14020
14021 static tree
14022 cp_parser_base_clause (cp_parser* parser)
14023 {
14024   tree bases = NULL_TREE;
14025
14026   /* Look for the `:' that begins the list.  */
14027   cp_parser_require (parser, CPP_COLON, "`:'");
14028
14029   /* Scan the base-specifier-list.  */
14030   while (true)
14031     {
14032       cp_token *token;
14033       tree base;
14034
14035       /* Look for the base-specifier.  */
14036       base = cp_parser_base_specifier (parser);
14037       /* Add BASE to the front of the list.  */
14038       if (base != error_mark_node)
14039         {
14040           TREE_CHAIN (base) = bases;
14041           bases = base;
14042         }
14043       /* Peek at the next token.  */
14044       token = cp_lexer_peek_token (parser->lexer);
14045       /* If it's not a comma, then the list is complete.  */
14046       if (token->type != CPP_COMMA)
14047         break;
14048       /* Consume the `,'.  */
14049       cp_lexer_consume_token (parser->lexer);
14050     }
14051
14052   /* PARSER->SCOPE may still be non-NULL at this point, if the last
14053      base class had a qualified name.  However, the next name that
14054      appears is certainly not qualified.  */
14055   parser->scope = NULL_TREE;
14056   parser->qualifying_scope = NULL_TREE;
14057   parser->object_scope = NULL_TREE;
14058
14059   return nreverse (bases);
14060 }
14061
14062 /* Parse a base-specifier.
14063
14064    base-specifier:
14065      :: [opt] nested-name-specifier [opt] class-name
14066      virtual access-specifier [opt] :: [opt] nested-name-specifier
14067        [opt] class-name
14068      access-specifier virtual [opt] :: [opt] nested-name-specifier
14069        [opt] class-name
14070
14071    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
14072    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14073    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
14074    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
14075
14076 static tree
14077 cp_parser_base_specifier (cp_parser* parser)
14078 {
14079   cp_token *token;
14080   bool done = false;
14081   bool virtual_p = false;
14082   bool duplicate_virtual_error_issued_p = false;
14083   bool duplicate_access_error_issued_p = false;
14084   bool class_scope_p, template_p;
14085   tree access = access_default_node;
14086   tree type;
14087
14088   /* Process the optional `virtual' and `access-specifier'.  */
14089   while (!done)
14090     {
14091       /* Peek at the next token.  */
14092       token = cp_lexer_peek_token (parser->lexer);
14093       /* Process `virtual'.  */
14094       switch (token->keyword)
14095         {
14096         case RID_VIRTUAL:
14097           /* If `virtual' appears more than once, issue an error.  */
14098           if (virtual_p && !duplicate_virtual_error_issued_p)
14099             {
14100               cp_parser_error (parser,
14101                                "%<virtual%> specified more than once in base-specified");
14102               duplicate_virtual_error_issued_p = true;
14103             }
14104
14105           virtual_p = true;
14106
14107           /* Consume the `virtual' token.  */
14108           cp_lexer_consume_token (parser->lexer);
14109
14110           break;
14111
14112         case RID_PUBLIC:
14113         case RID_PROTECTED:
14114         case RID_PRIVATE:
14115           /* If more than one access specifier appears, issue an
14116              error.  */
14117           if (access != access_default_node
14118               && !duplicate_access_error_issued_p)
14119             {
14120               cp_parser_error (parser,
14121                                "more than one access specifier in base-specified");
14122               duplicate_access_error_issued_p = true;
14123             }
14124
14125           access = ridpointers[(int) token->keyword];
14126
14127           /* Consume the access-specifier.  */
14128           cp_lexer_consume_token (parser->lexer);
14129
14130           break;
14131
14132         default:
14133           done = true;
14134           break;
14135         }
14136     }
14137   /* It is not uncommon to see programs mechanically, erroneously, use
14138      the 'typename' keyword to denote (dependent) qualified types
14139      as base classes.  */
14140   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14141     {
14142       if (!processing_template_decl)
14143         error ("keyword %<typename%> not allowed outside of templates");
14144       else
14145         error ("keyword %<typename%> not allowed in this context "
14146                "(the base class is implicitly a type)");
14147       cp_lexer_consume_token (parser->lexer);
14148     }
14149
14150   /* Look for the optional `::' operator.  */
14151   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false,
14152                               /*object_scope_valid_p=*/false);
14153   /* Look for the nested-name-specifier.  The simplest way to
14154      implement:
14155
14156        [temp.res]
14157
14158        The keyword `typename' is not permitted in a base-specifier or
14159        mem-initializer; in these contexts a qualified name that
14160        depends on a template-parameter is implicitly assumed to be a
14161        type name.
14162
14163      is to pretend that we have seen the `typename' keyword at this
14164      point.  */
14165   cp_parser_nested_name_specifier_opt (parser,
14166                                        /*typename_keyword_p=*/true,
14167                                        /*check_dependency_p=*/true,
14168                                        typename_type,
14169                                        /*is_declaration=*/true);
14170   /* If the base class is given by a qualified name, assume that names
14171      we see are type names or templates, as appropriate.  */
14172   class_scope_p = (parser->scope && TYPE_P (parser->scope));
14173   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14174
14175   /* Finally, look for the class-name.  */
14176   type = cp_parser_class_name (parser,
14177                                class_scope_p,
14178                                template_p,
14179                                typename_type,
14180                                /*check_dependency_p=*/true,
14181                                /*class_head_p=*/false,
14182                                /*is_declaration=*/true);
14183
14184   if (type == error_mark_node)
14185     return error_mark_node;
14186
14187   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14188 }
14189
14190 /* Exception handling [gram.exception] */
14191
14192 /* Parse an (optional) exception-specification.
14193
14194    exception-specification:
14195      throw ( type-id-list [opt] )
14196
14197    Returns a TREE_LIST representing the exception-specification.  The
14198    TREE_VALUE of each node is a type.  */
14199
14200 static tree
14201 cp_parser_exception_specification_opt (cp_parser* parser)
14202 {
14203   cp_token *token;
14204   tree type_id_list;
14205
14206   /* Peek at the next token.  */
14207   token = cp_lexer_peek_token (parser->lexer);
14208   /* If it's not `throw', then there's no exception-specification.  */
14209   if (!cp_parser_is_keyword (token, RID_THROW))
14210     return NULL_TREE;
14211
14212   /* Consume the `throw'.  */
14213   cp_lexer_consume_token (parser->lexer);
14214
14215   /* Look for the `('.  */
14216   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14217
14218   /* Peek at the next token.  */
14219   token = cp_lexer_peek_token (parser->lexer);
14220   /* If it's not a `)', then there is a type-id-list.  */
14221   if (token->type != CPP_CLOSE_PAREN)
14222     {
14223       const char *saved_message;
14224
14225       /* Types may not be defined in an exception-specification.  */
14226       saved_message = parser->type_definition_forbidden_message;
14227       parser->type_definition_forbidden_message
14228         = "types may not be defined in an exception-specification";
14229       /* Parse the type-id-list.  */
14230       type_id_list = cp_parser_type_id_list (parser);
14231       /* Restore the saved message.  */
14232       parser->type_definition_forbidden_message = saved_message;
14233     }
14234   else
14235     type_id_list = empty_except_spec;
14236
14237   /* Look for the `)'.  */
14238   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14239
14240   return type_id_list;
14241 }
14242
14243 /* Parse an (optional) type-id-list.
14244
14245    type-id-list:
14246      type-id
14247      type-id-list , type-id
14248
14249    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14250    in the order that the types were presented.  */
14251
14252 static tree
14253 cp_parser_type_id_list (cp_parser* parser)
14254 {
14255   tree types = NULL_TREE;
14256
14257   while (true)
14258     {
14259       cp_token *token;
14260       tree type;
14261
14262       /* Get the next type-id.  */
14263       type = cp_parser_type_id (parser);
14264       /* Add it to the list.  */
14265       types = add_exception_specifier (types, type, /*complain=*/1);
14266       /* Peek at the next token.  */
14267       token = cp_lexer_peek_token (parser->lexer);
14268       /* If it is not a `,', we are done.  */
14269       if (token->type != CPP_COMMA)
14270         break;
14271       /* Consume the `,'.  */
14272       cp_lexer_consume_token (parser->lexer);
14273     }
14274
14275   return nreverse (types);
14276 }
14277
14278 /* Parse a try-block.
14279
14280    try-block:
14281      try compound-statement handler-seq  */
14282
14283 static tree
14284 cp_parser_try_block (cp_parser* parser)
14285 {
14286   tree try_block;
14287
14288   cp_parser_require_keyword (parser, RID_TRY, "`try'");
14289   try_block = begin_try_block ();
14290   cp_parser_compound_statement (parser, NULL, true);
14291   finish_try_block (try_block);
14292   cp_parser_handler_seq (parser);
14293   finish_handler_sequence (try_block);
14294
14295   return try_block;
14296 }
14297
14298 /* Parse a function-try-block.
14299
14300    function-try-block:
14301      try ctor-initializer [opt] function-body handler-seq  */
14302
14303 static bool
14304 cp_parser_function_try_block (cp_parser* parser)
14305 {
14306   tree compound_stmt;
14307   tree try_block;
14308   bool ctor_initializer_p;
14309
14310   /* Look for the `try' keyword.  */
14311   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14312     return false;
14313   /* Let the rest of the front-end know where we are.  */
14314   try_block = begin_function_try_block (&compound_stmt);
14315   /* Parse the function-body.  */
14316   ctor_initializer_p
14317     = cp_parser_ctor_initializer_opt_and_function_body (parser);
14318   /* We're done with the `try' part.  */
14319   finish_function_try_block (try_block);
14320   /* Parse the handlers.  */
14321   cp_parser_handler_seq (parser);
14322   /* We're done with the handlers.  */
14323   finish_function_handler_sequence (try_block, compound_stmt);
14324
14325   return ctor_initializer_p;
14326 }
14327
14328 /* Parse a handler-seq.
14329
14330    handler-seq:
14331      handler handler-seq [opt]  */
14332
14333 static void
14334 cp_parser_handler_seq (cp_parser* parser)
14335 {
14336   while (true)
14337     {
14338       cp_token *token;
14339
14340       /* Parse the handler.  */
14341       cp_parser_handler (parser);
14342       /* Peek at the next token.  */
14343       token = cp_lexer_peek_token (parser->lexer);
14344       /* If it's not `catch' then there are no more handlers.  */
14345       if (!cp_parser_is_keyword (token, RID_CATCH))
14346         break;
14347     }
14348 }
14349
14350 /* Parse a handler.
14351
14352    handler:
14353      catch ( exception-declaration ) compound-statement  */
14354
14355 static void
14356 cp_parser_handler (cp_parser* parser)
14357 {
14358   tree handler;
14359   tree declaration;
14360
14361   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14362   handler = begin_handler ();
14363   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14364   declaration = cp_parser_exception_declaration (parser);
14365   finish_handler_parms (declaration, handler);
14366   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14367   cp_parser_compound_statement (parser, NULL, false);
14368   finish_handler (handler);
14369 }
14370
14371 /* Parse an exception-declaration.
14372
14373    exception-declaration:
14374      type-specifier-seq declarator
14375      type-specifier-seq abstract-declarator
14376      type-specifier-seq
14377      ...
14378
14379    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14380    ellipsis variant is used.  */
14381
14382 static tree
14383 cp_parser_exception_declaration (cp_parser* parser)
14384 {
14385   cp_decl_specifier_seq type_specifiers;
14386   cp_declarator *declarator;
14387   const char *saved_message;
14388
14389   /* If it's an ellipsis, it's easy to handle.  */
14390   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14391     {
14392       /* Consume the `...' token.  */
14393       cp_lexer_consume_token (parser->lexer);
14394       return NULL_TREE;
14395     }
14396
14397   /* Types may not be defined in exception-declarations.  */
14398   saved_message = parser->type_definition_forbidden_message;
14399   parser->type_definition_forbidden_message
14400     = "types may not be defined in exception-declarations";
14401
14402   /* Parse the type-specifier-seq.  */
14403   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14404                                 &type_specifiers);
14405   /* If it's a `)', then there is no declarator.  */
14406   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14407     declarator = NULL;
14408   else
14409     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14410                                        /*ctor_dtor_or_conv_p=*/NULL,
14411                                        /*parenthesized_p=*/NULL,
14412                                        /*member_p=*/false);
14413
14414   /* Restore the saved message.  */
14415   parser->type_definition_forbidden_message = saved_message;
14416
14417   if (!type_specifiers.any_specifiers_p)
14418     return error_mark_node;
14419
14420   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14421 }
14422
14423 /* Parse a throw-expression.
14424
14425    throw-expression:
14426      throw assignment-expression [opt]
14427
14428    Returns a THROW_EXPR representing the throw-expression.  */
14429
14430 static tree
14431 cp_parser_throw_expression (cp_parser* parser)
14432 {
14433   tree expression;
14434   cp_token* token;
14435
14436   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14437   token = cp_lexer_peek_token (parser->lexer);
14438   /* Figure out whether or not there is an assignment-expression
14439      following the "throw" keyword.  */
14440   if (token->type == CPP_COMMA
14441       || token->type == CPP_SEMICOLON
14442       || token->type == CPP_CLOSE_PAREN
14443       || token->type == CPP_CLOSE_SQUARE
14444       || token->type == CPP_CLOSE_BRACE
14445       || token->type == CPP_COLON)
14446     expression = NULL_TREE;
14447   else
14448     expression = cp_parser_assignment_expression (parser,
14449                                                   /*cast_p=*/false);
14450
14451   return build_throw (expression);
14452 }
14453
14454 /* GNU Extensions */
14455
14456 /* Parse an (optional) asm-specification.
14457
14458    asm-specification:
14459      asm ( string-literal )
14460
14461    If the asm-specification is present, returns a STRING_CST
14462    corresponding to the string-literal.  Otherwise, returns
14463    NULL_TREE.  */
14464
14465 static tree
14466 cp_parser_asm_specification_opt (cp_parser* parser)
14467 {
14468   cp_token *token;
14469   tree asm_specification;
14470
14471   /* Peek at the next token.  */
14472   token = cp_lexer_peek_token (parser->lexer);
14473   /* If the next token isn't the `asm' keyword, then there's no
14474      asm-specification.  */
14475   if (!cp_parser_is_keyword (token, RID_ASM))
14476     return NULL_TREE;
14477
14478   /* Consume the `asm' token.  */
14479   cp_lexer_consume_token (parser->lexer);
14480   /* Look for the `('.  */
14481   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14482
14483   /* Look for the string-literal.  */
14484   asm_specification = cp_parser_string_literal (parser, false, false);
14485
14486   /* Look for the `)'.  */
14487   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14488
14489   return asm_specification;
14490 }
14491
14492 /* Parse an asm-operand-list.
14493
14494    asm-operand-list:
14495      asm-operand
14496      asm-operand-list , asm-operand
14497
14498    asm-operand:
14499      string-literal ( expression )
14500      [ string-literal ] string-literal ( expression )
14501
14502    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14503    each node is the expression.  The TREE_PURPOSE is itself a
14504    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14505    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14506    is a STRING_CST for the string literal before the parenthesis.  */
14507
14508 static tree
14509 cp_parser_asm_operand_list (cp_parser* parser)
14510 {
14511   tree asm_operands = NULL_TREE;
14512
14513   while (true)
14514     {
14515       tree string_literal;
14516       tree expression;
14517       tree name;
14518
14519       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14520         {
14521           /* Consume the `[' token.  */
14522           cp_lexer_consume_token (parser->lexer);
14523           /* Read the operand name.  */
14524           name = cp_parser_identifier (parser);
14525           if (name != error_mark_node)
14526             name = build_string (IDENTIFIER_LENGTH (name),
14527                                  IDENTIFIER_POINTER (name));
14528           /* Look for the closing `]'.  */
14529           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14530         }
14531       else
14532         name = NULL_TREE;
14533       /* Look for the string-literal.  */
14534       string_literal = cp_parser_string_literal (parser, false, false);
14535
14536       /* Look for the `('.  */
14537       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14538       /* Parse the expression.  */
14539       expression = cp_parser_expression (parser, /*cast_p=*/false);
14540       /* Look for the `)'.  */
14541       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14542
14543       /* Add this operand to the list.  */
14544       asm_operands = tree_cons (build_tree_list (name, string_literal),
14545                                 expression,
14546                                 asm_operands);
14547       /* If the next token is not a `,', there are no more
14548          operands.  */
14549       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14550         break;
14551       /* Consume the `,'.  */
14552       cp_lexer_consume_token (parser->lexer);
14553     }
14554
14555   return nreverse (asm_operands);
14556 }
14557
14558 /* Parse an asm-clobber-list.
14559
14560    asm-clobber-list:
14561      string-literal
14562      asm-clobber-list , string-literal
14563
14564    Returns a TREE_LIST, indicating the clobbers in the order that they
14565    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14566
14567 static tree
14568 cp_parser_asm_clobber_list (cp_parser* parser)
14569 {
14570   tree clobbers = NULL_TREE;
14571
14572   while (true)
14573     {
14574       tree string_literal;
14575
14576       /* Look for the string literal.  */
14577       string_literal = cp_parser_string_literal (parser, false, false);
14578       /* Add it to the list.  */
14579       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14580       /* If the next token is not a `,', then the list is
14581          complete.  */
14582       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14583         break;
14584       /* Consume the `,' token.  */
14585       cp_lexer_consume_token (parser->lexer);
14586     }
14587
14588   return clobbers;
14589 }
14590
14591 /* Parse an (optional) series of attributes.
14592
14593    attributes:
14594      attributes attribute
14595
14596    attribute:
14597      __attribute__ (( attribute-list [opt] ))
14598
14599    The return value is as for cp_parser_attribute_list.  */
14600
14601 static tree
14602 cp_parser_attributes_opt (cp_parser* parser)
14603 {
14604   tree attributes = NULL_TREE;
14605
14606   while (true)
14607     {
14608       cp_token *token;
14609       tree attribute_list;
14610
14611       /* Peek at the next token.  */
14612       token = cp_lexer_peek_token (parser->lexer);
14613       /* If it's not `__attribute__', then we're done.  */
14614       if (token->keyword != RID_ATTRIBUTE)
14615         break;
14616
14617       /* Consume the `__attribute__' keyword.  */
14618       cp_lexer_consume_token (parser->lexer);
14619       /* Look for the two `(' tokens.  */
14620       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14621       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14622
14623       /* Peek at the next token.  */
14624       token = cp_lexer_peek_token (parser->lexer);
14625       if (token->type != CPP_CLOSE_PAREN)
14626         /* Parse the attribute-list.  */
14627         attribute_list = cp_parser_attribute_list (parser);
14628       else
14629         /* If the next token is a `)', then there is no attribute
14630            list.  */
14631         attribute_list = NULL;
14632
14633       /* Look for the two `)' tokens.  */
14634       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14635       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14636
14637       /* Add these new attributes to the list.  */
14638       attributes = chainon (attributes, attribute_list);
14639     }
14640
14641   return attributes;
14642 }
14643
14644 /* Parse an attribute-list.
14645
14646    attribute-list:
14647      attribute
14648      attribute-list , attribute
14649
14650    attribute:
14651      identifier
14652      identifier ( identifier )
14653      identifier ( identifier , expression-list )
14654      identifier ( expression-list )
14655
14656    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14657    to an attribute.  The TREE_PURPOSE of each node is the identifier
14658    indicating which attribute is in use.  The TREE_VALUE represents
14659    the arguments, if any.  */
14660
14661 static tree
14662 cp_parser_attribute_list (cp_parser* parser)
14663 {
14664   tree attribute_list = NULL_TREE;
14665   bool save_translate_strings_p = parser->translate_strings_p;
14666
14667   parser->translate_strings_p = false;
14668   while (true)
14669     {
14670       cp_token *token;
14671       tree identifier;
14672       tree attribute;
14673
14674       /* Look for the identifier.  We also allow keywords here; for
14675          example `__attribute__ ((const))' is legal.  */
14676       token = cp_lexer_peek_token (parser->lexer);
14677       if (token->type == CPP_NAME
14678           || token->type == CPP_KEYWORD)
14679         {
14680           tree arguments = NULL_TREE;
14681
14682           /* Consume the token.  */
14683           token = cp_lexer_consume_token (parser->lexer);
14684
14685           /* Save away the identifier that indicates which attribute
14686              this is.  */
14687           identifier = token->value;
14688           attribute = build_tree_list (identifier, NULL_TREE);
14689
14690           /* Peek at the next token.  */
14691           token = cp_lexer_peek_token (parser->lexer);
14692           /* If it's an `(', then parse the attribute arguments.  */
14693           if (token->type == CPP_OPEN_PAREN)
14694             {
14695               arguments = cp_parser_parenthesized_expression_list
14696                           (parser, true, /*cast_p=*/false,
14697                            /*non_constant_p=*/NULL);
14698               /* Save the arguments away.  */
14699               TREE_VALUE (attribute) = arguments;
14700             }
14701
14702           if (arguments != error_mark_node)
14703             {
14704               /* Add this attribute to the list.  */
14705               TREE_CHAIN (attribute) = attribute_list;
14706               attribute_list = attribute;
14707             }
14708
14709           token = cp_lexer_peek_token (parser->lexer);
14710         }
14711       /* Now, look for more attributes.  If the next token isn't a
14712          `,', we're done.  */
14713       if (token->type != CPP_COMMA)
14714         break;
14715
14716       /* Consume the comma and keep going.  */
14717       cp_lexer_consume_token (parser->lexer);
14718     }
14719   parser->translate_strings_p = save_translate_strings_p;
14720
14721   /* We built up the list in reverse order.  */
14722   return nreverse (attribute_list);
14723 }
14724
14725 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14726    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14727    current value of the PEDANTIC flag, regardless of whether or not
14728    the `__extension__' keyword is present.  The caller is responsible
14729    for restoring the value of the PEDANTIC flag.  */
14730
14731 static bool
14732 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14733 {
14734   /* Save the old value of the PEDANTIC flag.  */
14735   *saved_pedantic = pedantic;
14736
14737   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14738     {
14739       /* Consume the `__extension__' token.  */
14740       cp_lexer_consume_token (parser->lexer);
14741       /* We're not being pedantic while the `__extension__' keyword is
14742          in effect.  */
14743       pedantic = 0;
14744
14745       return true;
14746     }
14747
14748   return false;
14749 }
14750
14751 /* Parse a label declaration.
14752
14753    label-declaration:
14754      __label__ label-declarator-seq ;
14755
14756    label-declarator-seq:
14757      identifier , label-declarator-seq
14758      identifier  */
14759
14760 static void
14761 cp_parser_label_declaration (cp_parser* parser)
14762 {
14763   /* Look for the `__label__' keyword.  */
14764   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14765
14766   while (true)
14767     {
14768       tree identifier;
14769
14770       /* Look for an identifier.  */
14771       identifier = cp_parser_identifier (parser);
14772       /* If we failed, stop.  */
14773       if (identifier == error_mark_node)
14774         break;
14775       /* Declare it as a label.  */
14776       finish_label_decl (identifier);
14777       /* If the next token is a `;', stop.  */
14778       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14779         break;
14780       /* Look for the `,' separating the label declarations.  */
14781       cp_parser_require (parser, CPP_COMMA, "`,'");
14782     }
14783
14784   /* Look for the final `;'.  */
14785   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14786 }
14787
14788 /* Support Functions */
14789
14790 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14791    NAME should have one of the representations used for an
14792    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14793    is returned.  If PARSER->SCOPE is a dependent type, then a
14794    SCOPE_REF is returned.
14795
14796    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14797    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14798    was formed.  Abstractly, such entities should not be passed to this
14799    function, because they do not need to be looked up, but it is
14800    simpler to check for this special case here, rather than at the
14801    call-sites.
14802
14803    In cases not explicitly covered above, this function returns a
14804    DECL, OVERLOAD, or baselink representing the result of the lookup.
14805    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14806    is returned.
14807
14808    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14809    (e.g., "struct") that was used.  In that case bindings that do not
14810    refer to types are ignored.
14811
14812    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14813    ignored.
14814
14815    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14816    are ignored.
14817
14818    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14819    types.
14820
14821    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14822    TREE_LIST of candidates if name-lookup results in an ambiguity, and
14823    NULL_TREE otherwise.  */
14824
14825 static tree
14826 cp_parser_lookup_name (cp_parser *parser, tree name,
14827                        enum tag_types tag_type,
14828                        bool is_template,
14829                        bool is_namespace,
14830                        bool check_dependency,
14831                        tree *ambiguous_decls)
14832 {
14833   int flags = 0;
14834   tree decl;
14835   tree object_type = parser->context->object_type;
14836
14837   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14838     flags |= LOOKUP_COMPLAIN;
14839
14840   /* Assume that the lookup will be unambiguous.  */
14841   if (ambiguous_decls)
14842     *ambiguous_decls = NULL_TREE;
14843
14844   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14845      no longer valid.  Note that if we are parsing tentatively, and
14846      the parse fails, OBJECT_TYPE will be automatically restored.  */
14847   parser->context->object_type = NULL_TREE;
14848
14849   if (name == error_mark_node)
14850     return error_mark_node;
14851
14852   /* A template-id has already been resolved; there is no lookup to
14853      do.  */
14854   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14855     return name;
14856   if (BASELINK_P (name))
14857     {
14858       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14859                   == TEMPLATE_ID_EXPR);
14860       return name;
14861     }
14862
14863   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14864      it should already have been checked to make sure that the name
14865      used matches the type being destroyed.  */
14866   if (TREE_CODE (name) == BIT_NOT_EXPR)
14867     {
14868       tree type;
14869
14870       /* Figure out to which type this destructor applies.  */
14871       if (parser->scope)
14872         type = parser->scope;
14873       else if (object_type)
14874         type = object_type;
14875       else
14876         type = current_class_type;
14877       /* If that's not a class type, there is no destructor.  */
14878       if (!type || !CLASS_TYPE_P (type))
14879         return error_mark_node;
14880       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14881         lazily_declare_fn (sfk_destructor, type);
14882       if (!CLASSTYPE_DESTRUCTORS (type))
14883           return error_mark_node;
14884       /* If it was a class type, return the destructor.  */
14885       return CLASSTYPE_DESTRUCTORS (type);
14886     }
14887
14888   /* By this point, the NAME should be an ordinary identifier.  If
14889      the id-expression was a qualified name, the qualifying scope is
14890      stored in PARSER->SCOPE at this point.  */
14891   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14892
14893   /* Perform the lookup.  */
14894   if (parser->scope)
14895     {
14896       bool dependent_p;
14897
14898       if (parser->scope == error_mark_node)
14899         return error_mark_node;
14900
14901       /* If the SCOPE is dependent, the lookup must be deferred until
14902          the template is instantiated -- unless we are explicitly
14903          looking up names in uninstantiated templates.  Even then, we
14904          cannot look up the name if the scope is not a class type; it
14905          might, for example, be a template type parameter.  */
14906       dependent_p = (TYPE_P (parser->scope)
14907                      && !(parser->in_declarator_p
14908                           && currently_open_class (parser->scope))
14909                      && dependent_type_p (parser->scope));
14910       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14911            && dependent_p)
14912         {
14913           if (tag_type)
14914             {
14915               tree type;
14916
14917               /* The resolution to Core Issue 180 says that `struct
14918                  A::B' should be considered a type-name, even if `A'
14919                  is dependent.  */
14920               type = make_typename_type (parser->scope, name, tag_type,
14921                                          /*complain=*/tf_error);
14922               decl = TYPE_NAME (type);
14923             }
14924           else if (is_template
14925                    && (cp_parser_next_token_ends_template_argument_p (parser)
14926                        || cp_lexer_next_token_is (parser->lexer,
14927                                                   CPP_CLOSE_PAREN)))
14928             decl = make_unbound_class_template (parser->scope,
14929                                                 name, NULL_TREE,
14930                                                 /*complain=*/tf_error);
14931           else
14932             decl = build_qualified_name (/*type=*/NULL_TREE,
14933                                          parser->scope, name,
14934                                          is_template);
14935         }
14936       else
14937         {
14938           tree pushed_scope = NULL_TREE;
14939
14940           /* If PARSER->SCOPE is a dependent type, then it must be a
14941              class type, and we must not be checking dependencies;
14942              otherwise, we would have processed this lookup above.  So
14943              that PARSER->SCOPE is not considered a dependent base by
14944              lookup_member, we must enter the scope here.  */
14945           if (dependent_p)
14946             pushed_scope = push_scope (parser->scope);
14947           /* If the PARSER->SCOPE is a template specialization, it
14948              may be instantiated during name lookup.  In that case,
14949              errors may be issued.  Even if we rollback the current
14950              tentative parse, those errors are valid.  */
14951           decl = lookup_qualified_name (parser->scope, name,
14952                                         tag_type != none_type,
14953                                         /*complain=*/true);
14954           if (pushed_scope)
14955             pop_scope (pushed_scope);
14956         }
14957       parser->qualifying_scope = parser->scope;
14958       parser->object_scope = NULL_TREE;
14959     }
14960   else if (object_type)
14961     {
14962       tree object_decl = NULL_TREE;
14963       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14964          OBJECT_TYPE is not a class.  */
14965       if (CLASS_TYPE_P (object_type))
14966         /* If the OBJECT_TYPE is a template specialization, it may
14967            be instantiated during name lookup.  In that case, errors
14968            may be issued.  Even if we rollback the current tentative
14969            parse, those errors are valid.  */
14970         object_decl = lookup_member (object_type,
14971                                      name,
14972                                      /*protect=*/0,
14973                                      tag_type != none_type);
14974       /* Look it up in the enclosing context, too.  */
14975       decl = lookup_name_real (name, tag_type != none_type,
14976                                /*nonclass=*/0,
14977                                /*block_p=*/true, is_namespace, flags);
14978       parser->object_scope = object_type;
14979       parser->qualifying_scope = NULL_TREE;
14980       if (object_decl)
14981         decl = object_decl;
14982     }
14983   else
14984     {
14985       decl = lookup_name_real (name, tag_type != none_type,
14986                                /*nonclass=*/0,
14987                                /*block_p=*/true, is_namespace, flags);
14988       parser->qualifying_scope = NULL_TREE;
14989       parser->object_scope = NULL_TREE;
14990     }
14991
14992   /* If the lookup failed, let our caller know.  */
14993   if (!decl || decl == error_mark_node)
14994     return error_mark_node;
14995
14996   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14997   if (TREE_CODE (decl) == TREE_LIST)
14998     {
14999       if (ambiguous_decls)
15000         *ambiguous_decls = decl;
15001       /* The error message we have to print is too complicated for
15002          cp_parser_error, so we incorporate its actions directly.  */
15003       if (!cp_parser_simulate_error (parser))
15004         {
15005           error ("reference to %qD is ambiguous", name);
15006           print_candidates (decl);
15007         }
15008       return error_mark_node;
15009     }
15010
15011   gcc_assert (DECL_P (decl)
15012               || TREE_CODE (decl) == OVERLOAD
15013               || TREE_CODE (decl) == SCOPE_REF
15014               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15015               || BASELINK_P (decl));
15016
15017   /* If we have resolved the name of a member declaration, check to
15018      see if the declaration is accessible.  When the name resolves to
15019      set of overloaded functions, accessibility is checked when
15020      overload resolution is done.
15021
15022      During an explicit instantiation, access is not checked at all,
15023      as per [temp.explicit].  */
15024   if (DECL_P (decl))
15025     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15026
15027   return decl;
15028 }
15029
15030 /* Like cp_parser_lookup_name, but for use in the typical case where
15031    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15032    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
15033
15034 static tree
15035 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15036 {
15037   return cp_parser_lookup_name (parser, name,
15038                                 none_type,
15039                                 /*is_template=*/false,
15040                                 /*is_namespace=*/false,
15041                                 /*check_dependency=*/true,
15042                                 /*ambiguous_decls=*/NULL);
15043 }
15044
15045 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15046    the current context, return the TYPE_DECL.  If TAG_NAME_P is
15047    true, the DECL indicates the class being defined in a class-head,
15048    or declared in an elaborated-type-specifier.
15049
15050    Otherwise, return DECL.  */
15051
15052 static tree
15053 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15054 {
15055   /* If the TEMPLATE_DECL is being declared as part of a class-head,
15056      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15057
15058        struct A {
15059          template <typename T> struct B;
15060        };
15061
15062        template <typename T> struct A::B {};
15063
15064      Similarly, in an elaborated-type-specifier:
15065
15066        namespace N { struct X{}; }
15067
15068        struct A {
15069          template <typename T> friend struct N::X;
15070        };
15071
15072      However, if the DECL refers to a class type, and we are in
15073      the scope of the class, then the name lookup automatically
15074      finds the TYPE_DECL created by build_self_reference rather
15075      than a TEMPLATE_DECL.  For example, in:
15076
15077        template <class T> struct S {
15078          S s;
15079        };
15080
15081      there is no need to handle such case.  */
15082
15083   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15084     return DECL_TEMPLATE_RESULT (decl);
15085
15086   return decl;
15087 }
15088
15089 /* If too many, or too few, template-parameter lists apply to the
15090    declarator, issue an error message.  Returns TRUE if all went well,
15091    and FALSE otherwise.  */
15092
15093 static bool
15094 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15095                                                 cp_declarator *declarator)
15096 {
15097   unsigned num_templates;
15098
15099   /* We haven't seen any classes that involve template parameters yet.  */
15100   num_templates = 0;
15101
15102   switch (declarator->kind)
15103     {
15104     case cdk_id:
15105       if (declarator->u.id.qualifying_scope)
15106         {
15107           tree scope;
15108           tree member;
15109
15110           scope = declarator->u.id.qualifying_scope;
15111           member = declarator->u.id.unqualified_name;
15112
15113           while (scope && CLASS_TYPE_P (scope))
15114             {
15115               /* You're supposed to have one `template <...>'
15116                  for every template class, but you don't need one
15117                  for a full specialization.  For example:
15118
15119                  template <class T> struct S{};
15120                  template <> struct S<int> { void f(); };
15121                  void S<int>::f () {}
15122
15123                  is correct; there shouldn't be a `template <>' for
15124                  the definition of `S<int>::f'.  */
15125               if (CLASSTYPE_TEMPLATE_INFO (scope)
15126                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
15127                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
15128                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15129                 ++num_templates;
15130
15131               scope = TYPE_CONTEXT (scope);
15132             }
15133         }
15134       else if (TREE_CODE (declarator->u.id.unqualified_name)
15135                == TEMPLATE_ID_EXPR)
15136         /* If the DECLARATOR has the form `X<y>' then it uses one
15137            additional level of template parameters.  */
15138         ++num_templates;
15139
15140       return cp_parser_check_template_parameters (parser,
15141                                                   num_templates);
15142
15143     case cdk_function:
15144     case cdk_array:
15145     case cdk_pointer:
15146     case cdk_reference:
15147     case cdk_ptrmem:
15148       return (cp_parser_check_declarator_template_parameters
15149               (parser, declarator->declarator));
15150
15151     case cdk_error:
15152       return true;
15153
15154     default:
15155       gcc_unreachable ();
15156     }
15157   return false;
15158 }
15159
15160 /* NUM_TEMPLATES were used in the current declaration.  If that is
15161    invalid, return FALSE and issue an error messages.  Otherwise,
15162    return TRUE.  */
15163
15164 static bool
15165 cp_parser_check_template_parameters (cp_parser* parser,
15166                                      unsigned num_templates)
15167 {
15168   /* If there are more template classes than parameter lists, we have
15169      something like:
15170
15171        template <class T> void S<T>::R<T>::f ();  */
15172   if (parser->num_template_parameter_lists < num_templates)
15173     {
15174       error ("too few template-parameter-lists");
15175       return false;
15176     }
15177   /* If there are the same number of template classes and parameter
15178      lists, that's OK.  */
15179   if (parser->num_template_parameter_lists == num_templates)
15180     return true;
15181   /* If there are more, but only one more, then we are referring to a
15182      member template.  That's OK too.  */
15183   if (parser->num_template_parameter_lists == num_templates + 1)
15184       return true;
15185   /* Otherwise, there are too many template parameter lists.  We have
15186      something like:
15187
15188      template <class T> template <class U> void S::f();  */
15189   error ("too many template-parameter-lists");
15190   return false;
15191 }
15192
15193 /* Parse an optional `::' token indicating that the following name is
15194    from the global namespace.  If so, PARSER->SCOPE is set to the
15195    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15196    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15197    Returns the new value of PARSER->SCOPE, if the `::' token is
15198    present, and NULL_TREE otherwise.  */
15199
15200 static tree
15201 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p,
15202                             bool object_scope_valid_p)
15203 {
15204   cp_token *token;
15205
15206   /* Peek at the next token.  */
15207   token = cp_lexer_peek_token (parser->lexer);
15208   /* If we're looking at a `::' token then we're starting from the
15209      global namespace, not our current location.  */
15210   if (token->type == CPP_SCOPE)
15211     {
15212       /* Consume the `::' token.  */
15213       cp_lexer_consume_token (parser->lexer);
15214       /* Set the SCOPE so that we know where to start the lookup.  */
15215       parser->scope = global_namespace;
15216       parser->qualifying_scope = global_namespace;
15217       parser->object_scope = NULL_TREE;
15218
15219       return parser->scope;
15220     }
15221
15222   if (!current_scope_valid_p)
15223     {
15224       parser->scope = NULL_TREE;
15225       parser->qualifying_scope = NULL_TREE;
15226     }
15227   
15228   if (!object_scope_valid_p)
15229     parser->object_scope = NULL_TREE;
15230
15231   return NULL_TREE;
15232 }
15233
15234 /* Returns TRUE if the upcoming token sequence is the start of a
15235    constructor declarator.  If FRIEND_P is true, the declarator is
15236    preceded by the `friend' specifier.  */
15237
15238 static bool
15239 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15240 {
15241   bool constructor_p;
15242   tree type_decl = NULL_TREE;
15243   bool nested_name_p;
15244   cp_token *next_token;
15245
15246   /* The common case is that this is not a constructor declarator, so
15247      try to avoid doing lots of work if at all possible.  It's not
15248      valid declare a constructor at function scope.  */
15249   if (at_function_scope_p ())
15250     return false;
15251   /* And only certain tokens can begin a constructor declarator.  */
15252   next_token = cp_lexer_peek_token (parser->lexer);
15253   if (next_token->type != CPP_NAME
15254       && next_token->type != CPP_SCOPE
15255       && next_token->type != CPP_NESTED_NAME_SPECIFIER
15256       && next_token->type != CPP_TEMPLATE_ID)
15257     return false;
15258
15259   /* Parse tentatively; we are going to roll back all of the tokens
15260      consumed here.  */
15261   cp_parser_parse_tentatively (parser);
15262   /* Assume that we are looking at a constructor declarator.  */
15263   constructor_p = true;
15264
15265   /* Look for the optional `::' operator.  */
15266   cp_parser_global_scope_opt (parser,
15267                               /*current_scope_valid_p=*/false,
15268                               /*object_scope_valid_p=*/false);
15269   /* Look for the nested-name-specifier.  */
15270   nested_name_p
15271     = (cp_parser_nested_name_specifier_opt (parser,
15272                                             /*typename_keyword_p=*/false,
15273                                             /*check_dependency_p=*/false,
15274                                             /*type_p=*/false,
15275                                             /*is_declaration=*/false)
15276        != NULL_TREE);
15277   /* Outside of a class-specifier, there must be a
15278      nested-name-specifier.  */
15279   if (!nested_name_p &&
15280       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15281        || friend_p))
15282     constructor_p = false;
15283   /* If we still think that this might be a constructor-declarator,
15284      look for a class-name.  */
15285   if (constructor_p)
15286     {
15287       /* If we have:
15288
15289            template <typename T> struct S { S(); };
15290            template <typename T> S<T>::S ();
15291
15292          we must recognize that the nested `S' names a class.
15293          Similarly, for:
15294
15295            template <typename T> S<T>::S<T> ();
15296
15297          we must recognize that the nested `S' names a template.  */
15298       type_decl = cp_parser_class_name (parser,
15299                                         /*typename_keyword_p=*/false,
15300                                         /*template_keyword_p=*/false,
15301                                         none_type,
15302                                         /*check_dependency_p=*/false,
15303                                         /*class_head_p=*/false,
15304                                         /*is_declaration=*/false);
15305       /* If there was no class-name, then this is not a constructor.  */
15306       constructor_p = !cp_parser_error_occurred (parser);
15307     }
15308
15309   /* If we're still considering a constructor, we have to see a `(',
15310      to begin the parameter-declaration-clause, followed by either a
15311      `)', an `...', or a decl-specifier.  We need to check for a
15312      type-specifier to avoid being fooled into thinking that:
15313
15314        S::S (f) (int);
15315
15316      is a constructor.  (It is actually a function named `f' that
15317      takes one parameter (of type `int') and returns a value of type
15318      `S::S'.  */
15319   if (constructor_p
15320       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15321     {
15322       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15323           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15324           /* A parameter declaration begins with a decl-specifier,
15325              which is either the "attribute" keyword, a storage class
15326              specifier, or (usually) a type-specifier.  */
15327           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15328           && !cp_parser_storage_class_specifier_opt (parser))
15329         {
15330           tree type;
15331           tree pushed_scope = NULL_TREE;
15332           unsigned saved_num_template_parameter_lists;
15333
15334           /* Names appearing in the type-specifier should be looked up
15335              in the scope of the class.  */
15336           if (current_class_type)
15337             type = NULL_TREE;
15338           else
15339             {
15340               type = TREE_TYPE (type_decl);
15341               if (TREE_CODE (type) == TYPENAME_TYPE)
15342                 {
15343                   type = resolve_typename_type (type,
15344                                                 /*only_current_p=*/false);
15345                   if (type == error_mark_node)
15346                     {
15347                       cp_parser_abort_tentative_parse (parser);
15348                       return false;
15349                     }
15350                 }
15351               pushed_scope = push_scope (type);
15352             }
15353
15354           /* Inside the constructor parameter list, surrounding
15355              template-parameter-lists do not apply.  */
15356           saved_num_template_parameter_lists
15357             = parser->num_template_parameter_lists;
15358           parser->num_template_parameter_lists = 0;
15359
15360           /* Look for the type-specifier.  */
15361           cp_parser_type_specifier (parser,
15362                                     CP_PARSER_FLAGS_NONE,
15363                                     /*decl_specs=*/NULL,
15364                                     /*is_declarator=*/true,
15365                                     /*declares_class_or_enum=*/NULL,
15366                                     /*is_cv_qualifier=*/NULL);
15367
15368           parser->num_template_parameter_lists
15369             = saved_num_template_parameter_lists;
15370
15371           /* Leave the scope of the class.  */
15372           if (pushed_scope)
15373             pop_scope (pushed_scope);
15374
15375           constructor_p = !cp_parser_error_occurred (parser);
15376         }
15377     }
15378   else
15379     constructor_p = false;
15380   /* We did not really want to consume any tokens.  */
15381   cp_parser_abort_tentative_parse (parser);
15382
15383   return constructor_p;
15384 }
15385
15386 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15387    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15388    they must be performed once we are in the scope of the function.
15389
15390    Returns the function defined.  */
15391
15392 static tree
15393 cp_parser_function_definition_from_specifiers_and_declarator
15394   (cp_parser* parser,
15395    cp_decl_specifier_seq *decl_specifiers,
15396    tree attributes,
15397    const cp_declarator *declarator)
15398 {
15399   tree fn;
15400   bool success_p;
15401
15402   /* Begin the function-definition.  */
15403   success_p = start_function (decl_specifiers, declarator, attributes);
15404
15405   /* The things we're about to see are not directly qualified by any
15406      template headers we've seen thus far.  */
15407   reset_specialization ();
15408
15409   /* If there were names looked up in the decl-specifier-seq that we
15410      did not check, check them now.  We must wait until we are in the
15411      scope of the function to perform the checks, since the function
15412      might be a friend.  */
15413   perform_deferred_access_checks ();
15414
15415   if (!success_p)
15416     {
15417       /* Skip the entire function.  */
15418       cp_parser_skip_to_end_of_block_or_statement (parser);
15419       fn = error_mark_node;
15420     }
15421   else
15422     fn = cp_parser_function_definition_after_declarator (parser,
15423                                                          /*inline_p=*/false);
15424
15425   return fn;
15426 }
15427
15428 /* Parse the part of a function-definition that follows the
15429    declarator.  INLINE_P is TRUE iff this function is an inline
15430    function defined with a class-specifier.
15431
15432    Returns the function defined.  */
15433
15434 static tree
15435 cp_parser_function_definition_after_declarator (cp_parser* parser,
15436                                                 bool inline_p)
15437 {
15438   tree fn;
15439   bool ctor_initializer_p = false;
15440   bool saved_in_unbraced_linkage_specification_p;
15441   unsigned saved_num_template_parameter_lists;
15442
15443   /* If the next token is `return', then the code may be trying to
15444      make use of the "named return value" extension that G++ used to
15445      support.  */
15446   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15447     {
15448       /* Consume the `return' keyword.  */
15449       cp_lexer_consume_token (parser->lexer);
15450       /* Look for the identifier that indicates what value is to be
15451          returned.  */
15452       cp_parser_identifier (parser);
15453       /* Issue an error message.  */
15454       error ("named return values are no longer supported");
15455       /* Skip tokens until we reach the start of the function body.  */
15456       while (true)
15457         {
15458           cp_token *token = cp_lexer_peek_token (parser->lexer);
15459           if (token->type == CPP_OPEN_BRACE
15460               || token->type == CPP_EOF
15461               || token->type == CPP_PRAGMA_EOL)
15462             break;
15463           cp_lexer_consume_token (parser->lexer);
15464         }
15465     }
15466   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15467      anything declared inside `f'.  */
15468   saved_in_unbraced_linkage_specification_p
15469     = parser->in_unbraced_linkage_specification_p;
15470   parser->in_unbraced_linkage_specification_p = false;
15471   /* Inside the function, surrounding template-parameter-lists do not
15472      apply.  */
15473   saved_num_template_parameter_lists
15474     = parser->num_template_parameter_lists;
15475   parser->num_template_parameter_lists = 0;
15476   /* If the next token is `try', then we are looking at a
15477      function-try-block.  */
15478   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15479     ctor_initializer_p = cp_parser_function_try_block (parser);
15480   /* A function-try-block includes the function-body, so we only do
15481      this next part if we're not processing a function-try-block.  */
15482   else
15483     ctor_initializer_p
15484       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15485
15486   /* Finish the function.  */
15487   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15488                         (inline_p ? 2 : 0));
15489   /* Generate code for it, if necessary.  */
15490   expand_or_defer_fn (fn);
15491   /* Restore the saved values.  */
15492   parser->in_unbraced_linkage_specification_p
15493     = saved_in_unbraced_linkage_specification_p;
15494   parser->num_template_parameter_lists
15495     = saved_num_template_parameter_lists;
15496
15497   return fn;
15498 }
15499
15500 /* Parse a template-declaration, assuming that the `export' (and
15501    `extern') keywords, if present, has already been scanned.  MEMBER_P
15502    is as for cp_parser_template_declaration.  */
15503
15504 static void
15505 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15506 {
15507   tree decl = NULL_TREE;
15508   tree checks;
15509   tree parameter_list;
15510   bool friend_p = false;
15511   bool need_lang_pop;
15512
15513   /* Look for the `template' keyword.  */
15514   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15515     return;
15516
15517   /* And the `<'.  */
15518   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15519     return;
15520   /* [temp]
15521
15522      A template ... shall not have C linkage.  */
15523   if (current_lang_name == lang_name_c)
15524     {
15525       error ("template with C linkage");
15526       /* Give it C++ linkage to avoid confusing other parts of the
15527          front end.  */
15528       push_lang_context (lang_name_cplusplus);
15529       need_lang_pop = true;
15530     }
15531   else
15532     need_lang_pop = false;
15533
15534   /* We cannot perform access checks on the template parameter
15535      declarations until we know what is being declared, just as we
15536      cannot check the decl-specifier list.  */
15537   push_deferring_access_checks (dk_deferred);
15538
15539   /* If the next token is `>', then we have an invalid
15540      specialization.  Rather than complain about an invalid template
15541      parameter, issue an error message here.  */
15542   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15543     {
15544       cp_parser_error (parser, "invalid explicit specialization");
15545       begin_specialization ();
15546       parameter_list = NULL_TREE;
15547     }
15548   else
15549     /* Parse the template parameters.  */
15550     parameter_list = cp_parser_template_parameter_list (parser);
15551
15552   /* Get the deferred access checks from the parameter list.  These
15553      will be checked once we know what is being declared, as for a
15554      member template the checks must be performed in the scope of the
15555      class containing the member.  */
15556   checks = get_deferred_access_checks ();
15557
15558   /* Look for the `>'.  */
15559   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15560   /* We just processed one more parameter list.  */
15561   ++parser->num_template_parameter_lists;
15562   /* If the next token is `template', there are more template
15563      parameters.  */
15564   if (cp_lexer_next_token_is_keyword (parser->lexer,
15565                                       RID_TEMPLATE))
15566     cp_parser_template_declaration_after_export (parser, member_p);
15567   else
15568     {
15569       /* There are no access checks when parsing a template, as we do not
15570          know if a specialization will be a friend.  */
15571       push_deferring_access_checks (dk_no_check);
15572       decl = cp_parser_single_declaration (parser,
15573                                            checks,
15574                                            member_p,
15575                                            &friend_p);
15576       pop_deferring_access_checks ();
15577
15578       /* If this is a member template declaration, let the front
15579          end know.  */
15580       if (member_p && !friend_p && decl)
15581         {
15582           if (TREE_CODE (decl) == TYPE_DECL)
15583             cp_parser_check_access_in_redeclaration (decl);
15584
15585           decl = finish_member_template_decl (decl);
15586         }
15587       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15588         make_friend_class (current_class_type, TREE_TYPE (decl),
15589                            /*complain=*/true);
15590     }
15591   /* We are done with the current parameter list.  */
15592   --parser->num_template_parameter_lists;
15593
15594   pop_deferring_access_checks ();
15595
15596   /* Finish up.  */
15597   finish_template_decl (parameter_list);
15598
15599   /* Register member declarations.  */
15600   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15601     finish_member_declaration (decl);
15602   /* For the erroneous case of a template with C linkage, we pushed an
15603      implicit C++ linkage scope; exit that scope now.  */
15604   if (need_lang_pop)
15605     pop_lang_context ();
15606   /* If DECL is a function template, we must return to parse it later.
15607      (Even though there is no definition, there might be default
15608      arguments that need handling.)  */
15609   if (member_p && decl
15610       && (TREE_CODE (decl) == FUNCTION_DECL
15611           || DECL_FUNCTION_TEMPLATE_P (decl)))
15612     TREE_VALUE (parser->unparsed_functions_queues)
15613       = tree_cons (NULL_TREE, decl,
15614                    TREE_VALUE (parser->unparsed_functions_queues));
15615 }
15616
15617 /* Perform the deferred access checks from a template-parameter-list.
15618    CHECKS is a TREE_LIST of access checks, as returned by
15619    get_deferred_access_checks.  */
15620
15621 static void
15622 cp_parser_perform_template_parameter_access_checks (tree checks)
15623 {
15624   ++processing_template_parmlist;
15625   perform_access_checks (checks);
15626   --processing_template_parmlist;
15627 }
15628
15629 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15630    `function-definition' sequence.  MEMBER_P is true, this declaration
15631    appears in a class scope.
15632
15633    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15634    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15635
15636 static tree
15637 cp_parser_single_declaration (cp_parser* parser,
15638                               tree checks,
15639                               bool member_p,
15640                               bool* friend_p)
15641 {
15642   int declares_class_or_enum;
15643   tree decl = NULL_TREE;
15644   cp_decl_specifier_seq decl_specifiers;
15645   bool function_definition_p = false;
15646
15647   /* This function is only used when processing a template
15648      declaration.  */
15649   gcc_assert (innermost_scope_kind () == sk_template_parms
15650               || innermost_scope_kind () == sk_template_spec);
15651
15652   /* Defer access checks until we know what is being declared.  */
15653   push_deferring_access_checks (dk_deferred);
15654
15655   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15656      alternative.  */
15657   cp_parser_decl_specifier_seq (parser,
15658                                 CP_PARSER_FLAGS_OPTIONAL,
15659                                 &decl_specifiers,
15660                                 &declares_class_or_enum);
15661   if (friend_p)
15662     *friend_p = cp_parser_friend_p (&decl_specifiers);
15663
15664   /* There are no template typedefs.  */
15665   if (decl_specifiers.specs[(int) ds_typedef])
15666     {
15667       error ("template declaration of %qs", "typedef");
15668       decl = error_mark_node;
15669     }
15670
15671   /* Gather up the access checks that occurred the
15672      decl-specifier-seq.  */
15673   stop_deferring_access_checks ();
15674
15675   /* Check for the declaration of a template class.  */
15676   if (declares_class_or_enum)
15677     {
15678       if (cp_parser_declares_only_class_p (parser))
15679         {
15680           decl = shadow_tag (&decl_specifiers);
15681
15682           /* In this case:
15683
15684                struct C {
15685                  friend template <typename T> struct A<T>::B;
15686                };
15687
15688              A<T>::B will be represented by a TYPENAME_TYPE, and
15689              therefore not recognized by shadow_tag.  */
15690           if (friend_p && *friend_p
15691               && !decl
15692               && decl_specifiers.type
15693               && TYPE_P (decl_specifiers.type))
15694             decl = decl_specifiers.type;
15695
15696           if (decl && decl != error_mark_node)
15697             decl = TYPE_NAME (decl);
15698           else
15699             decl = error_mark_node;
15700
15701           /* Perform access checks for template parameters.  */
15702           cp_parser_perform_template_parameter_access_checks (checks);
15703         }
15704     }
15705   /* If it's not a template class, try for a template function.  If
15706      the next token is a `;', then this declaration does not declare
15707      anything.  But, if there were errors in the decl-specifiers, then
15708      the error might well have come from an attempted class-specifier.
15709      In that case, there's no need to warn about a missing declarator.  */
15710   if (!decl
15711       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15712           || decl_specifiers.type != error_mark_node))
15713     decl = cp_parser_init_declarator (parser,
15714                                       &decl_specifiers,
15715                                       checks,
15716                                       /*function_definition_allowed_p=*/true,
15717                                       member_p,
15718                                       declares_class_or_enum,
15719                                       &function_definition_p);
15720
15721   pop_deferring_access_checks ();
15722
15723   /* Clear any current qualification; whatever comes next is the start
15724      of something new.  */
15725   parser->scope = NULL_TREE;
15726   parser->qualifying_scope = NULL_TREE;
15727   parser->object_scope = NULL_TREE;
15728   /* Look for a trailing `;' after the declaration.  */
15729   if (!function_definition_p
15730       && (decl == error_mark_node
15731           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15732     cp_parser_skip_to_end_of_block_or_statement (parser);
15733
15734   return decl;
15735 }
15736
15737 /* Parse a cast-expression that is not the operand of a unary "&".  */
15738
15739 static tree
15740 cp_parser_simple_cast_expression (cp_parser *parser)
15741 {
15742   return cp_parser_cast_expression (parser, /*address_p=*/false,
15743                                     /*cast_p=*/false);
15744 }
15745
15746 /* Parse a functional cast to TYPE.  Returns an expression
15747    representing the cast.  */
15748
15749 static tree
15750 cp_parser_functional_cast (cp_parser* parser, tree type)
15751 {
15752   tree expression_list;
15753   tree cast;
15754
15755   expression_list
15756     = cp_parser_parenthesized_expression_list (parser, false,
15757                                                /*cast_p=*/true,
15758                                                /*non_constant_p=*/NULL);
15759
15760   cast = build_functional_cast (type, expression_list);
15761   /* [expr.const]/1: In an integral constant expression "only type
15762      conversions to integral or enumeration type can be used".  */
15763   if (TREE_CODE (type) == TYPE_DECL)
15764     type = TREE_TYPE (type);
15765   if (cast != error_mark_node
15766       && !cast_valid_in_integral_constant_expression_p (type)
15767       && (cp_parser_non_integral_constant_expression
15768           (parser, "a call to a constructor")))
15769     return error_mark_node;
15770   return cast;
15771 }
15772
15773 /* Save the tokens that make up the body of a member function defined
15774    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15775    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15776    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15777    for the member function.  */
15778
15779 static tree
15780 cp_parser_save_member_function_body (cp_parser* parser,
15781                                      cp_decl_specifier_seq *decl_specifiers,
15782                                      cp_declarator *declarator,
15783                                      tree attributes)
15784 {
15785   cp_token *first;
15786   cp_token *last;
15787   tree fn;
15788
15789   /* Create the function-declaration.  */
15790   fn = start_method (decl_specifiers, declarator, attributes);
15791   /* If something went badly wrong, bail out now.  */
15792   if (fn == error_mark_node)
15793     {
15794       /* If there's a function-body, skip it.  */
15795       if (cp_parser_token_starts_function_definition_p
15796           (cp_lexer_peek_token (parser->lexer)))
15797         cp_parser_skip_to_end_of_block_or_statement (parser);
15798       return error_mark_node;
15799     }
15800
15801   /* Remember it, if there default args to post process.  */
15802   cp_parser_save_default_args (parser, fn);
15803
15804   /* Save away the tokens that make up the body of the
15805      function.  */
15806   first = parser->lexer->next_token;
15807   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15808   /* Handle function try blocks.  */
15809   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15810     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15811   last = parser->lexer->next_token;
15812
15813   /* Save away the inline definition; we will process it when the
15814      class is complete.  */
15815   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15816   DECL_PENDING_INLINE_P (fn) = 1;
15817
15818   /* We need to know that this was defined in the class, so that
15819      friend templates are handled correctly.  */
15820   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15821
15822   /* We're done with the inline definition.  */
15823   finish_method (fn);
15824
15825   /* Add FN to the queue of functions to be parsed later.  */
15826   TREE_VALUE (parser->unparsed_functions_queues)
15827     = tree_cons (NULL_TREE, fn,
15828                  TREE_VALUE (parser->unparsed_functions_queues));
15829
15830   return fn;
15831 }
15832
15833 /* Parse a template-argument-list, as well as the trailing ">" (but
15834    not the opening ">").  See cp_parser_template_argument_list for the
15835    return value.  */
15836
15837 static tree
15838 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15839 {
15840   tree arguments;
15841   tree saved_scope;
15842   tree saved_qualifying_scope;
15843   tree saved_object_scope;
15844   bool saved_greater_than_is_operator_p;
15845   bool saved_skip_evaluation;
15846
15847   /* [temp.names]
15848
15849      When parsing a template-id, the first non-nested `>' is taken as
15850      the end of the template-argument-list rather than a greater-than
15851      operator.  */
15852   saved_greater_than_is_operator_p
15853     = parser->greater_than_is_operator_p;
15854   parser->greater_than_is_operator_p = false;
15855   /* Parsing the argument list may modify SCOPE, so we save it
15856      here.  */
15857   saved_scope = parser->scope;
15858   saved_qualifying_scope = parser->qualifying_scope;
15859   saved_object_scope = parser->object_scope;
15860   /* We need to evaluate the template arguments, even though this
15861      template-id may be nested within a "sizeof".  */
15862   saved_skip_evaluation = skip_evaluation;
15863   skip_evaluation = false;
15864   /* Parse the template-argument-list itself.  */
15865   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15866     arguments = NULL_TREE;
15867   else
15868     arguments = cp_parser_template_argument_list (parser);
15869   /* Look for the `>' that ends the template-argument-list. If we find
15870      a '>>' instead, it's probably just a typo.  */
15871   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15872     {
15873       if (!saved_greater_than_is_operator_p)
15874         {
15875           /* If we're in a nested template argument list, the '>>' has
15876             to be a typo for '> >'. We emit the error message, but we
15877             continue parsing and we push a '>' as next token, so that
15878             the argument list will be parsed correctly.  Note that the
15879             global source location is still on the token before the
15880             '>>', so we need to say explicitly where we want it.  */
15881           cp_token *token = cp_lexer_peek_token (parser->lexer);
15882           error ("%H%<>>%> should be %<> >%> "
15883                  "within a nested template argument list",
15884                  &token->location);
15885
15886           /* ??? Proper recovery should terminate two levels of
15887              template argument list here.  */
15888           token->type = CPP_GREATER;
15889         }
15890       else
15891         {
15892           /* If this is not a nested template argument list, the '>>'
15893             is a typo for '>'. Emit an error message and continue.
15894             Same deal about the token location, but here we can get it
15895             right by consuming the '>>' before issuing the diagnostic.  */
15896           cp_lexer_consume_token (parser->lexer);
15897           error ("spurious %<>>%>, use %<>%> to terminate "
15898                  "a template argument list");
15899         }
15900     }
15901   else
15902     cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15903   /* The `>' token might be a greater-than operator again now.  */
15904   parser->greater_than_is_operator_p
15905     = saved_greater_than_is_operator_p;
15906   /* Restore the SAVED_SCOPE.  */
15907   parser->scope = saved_scope;
15908   parser->qualifying_scope = saved_qualifying_scope;
15909   parser->object_scope = saved_object_scope;
15910   skip_evaluation = saved_skip_evaluation;
15911
15912   return arguments;
15913 }
15914
15915 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15916    arguments, or the body of the function have not yet been parsed,
15917    parse them now.  */
15918
15919 static void
15920 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15921 {
15922   /* If this member is a template, get the underlying
15923      FUNCTION_DECL.  */
15924   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15925     member_function = DECL_TEMPLATE_RESULT (member_function);
15926
15927   /* There should not be any class definitions in progress at this
15928      point; the bodies of members are only parsed outside of all class
15929      definitions.  */
15930   gcc_assert (parser->num_classes_being_defined == 0);
15931   /* While we're parsing the member functions we might encounter more
15932      classes.  We want to handle them right away, but we don't want
15933      them getting mixed up with functions that are currently in the
15934      queue.  */
15935   parser->unparsed_functions_queues
15936     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15937
15938   /* Make sure that any template parameters are in scope.  */
15939   maybe_begin_member_template_processing (member_function);
15940
15941   /* If the body of the function has not yet been parsed, parse it
15942      now.  */
15943   if (DECL_PENDING_INLINE_P (member_function))
15944     {
15945       tree function_scope;
15946       cp_token_cache *tokens;
15947
15948       /* The function is no longer pending; we are processing it.  */
15949       tokens = DECL_PENDING_INLINE_INFO (member_function);
15950       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15951       DECL_PENDING_INLINE_P (member_function) = 0;
15952
15953       /* If this is a local class, enter the scope of the containing
15954          function.  */
15955       function_scope = current_function_decl;
15956       if (function_scope)
15957         push_function_context_to (function_scope);
15958
15959
15960       /* Push the body of the function onto the lexer stack.  */
15961       cp_parser_push_lexer_for_tokens (parser, tokens);
15962
15963       /* Let the front end know that we going to be defining this
15964          function.  */
15965       start_preparsed_function (member_function, NULL_TREE,
15966                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15967
15968       /* Don't do access checking if it is a templated function.  */
15969       if (processing_template_decl)
15970         push_deferring_access_checks (dk_no_check);
15971
15972       /* Now, parse the body of the function.  */
15973       cp_parser_function_definition_after_declarator (parser,
15974                                                       /*inline_p=*/true);
15975
15976       if (processing_template_decl)
15977         pop_deferring_access_checks ();
15978
15979       /* Leave the scope of the containing function.  */
15980       if (function_scope)
15981         pop_function_context_from (function_scope);
15982       cp_parser_pop_lexer (parser);
15983     }
15984
15985   /* Remove any template parameters from the symbol table.  */
15986   maybe_end_member_template_processing ();
15987
15988   /* Restore the queue.  */
15989   parser->unparsed_functions_queues
15990     = TREE_CHAIN (parser->unparsed_functions_queues);
15991 }
15992
15993 /* If DECL contains any default args, remember it on the unparsed
15994    functions queue.  */
15995
15996 static void
15997 cp_parser_save_default_args (cp_parser* parser, tree decl)
15998 {
15999   tree probe;
16000
16001   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16002        probe;
16003        probe = TREE_CHAIN (probe))
16004     if (TREE_PURPOSE (probe))
16005       {
16006         TREE_PURPOSE (parser->unparsed_functions_queues)
16007           = tree_cons (current_class_type, decl,
16008                        TREE_PURPOSE (parser->unparsed_functions_queues));
16009         break;
16010       }
16011 }
16012
16013 /* FN is a FUNCTION_DECL which may contains a parameter with an
16014    unparsed DEFAULT_ARG.  Parse the default args now.  This function
16015    assumes that the current scope is the scope in which the default
16016    argument should be processed.  */
16017
16018 static void
16019 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16020 {
16021   bool saved_local_variables_forbidden_p;
16022   tree parm;
16023
16024   /* While we're parsing the default args, we might (due to the
16025      statement expression extension) encounter more classes.  We want
16026      to handle them right away, but we don't want them getting mixed
16027      up with default args that are currently in the queue.  */
16028   parser->unparsed_functions_queues
16029     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16030
16031   /* Local variable names (and the `this' keyword) may not appear
16032      in a default argument.  */
16033   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16034   parser->local_variables_forbidden_p = true;
16035
16036   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16037        parm;
16038        parm = TREE_CHAIN (parm))
16039     {
16040       cp_token_cache *tokens;
16041       tree default_arg = TREE_PURPOSE (parm);
16042       tree parsed_arg;
16043       VEC(tree,gc) *insts;
16044       tree copy;
16045       unsigned ix;
16046
16047       if (!default_arg)
16048         continue;
16049
16050       if (TREE_CODE (default_arg) != DEFAULT_ARG)
16051         /* This can happen for a friend declaration for a function
16052            already declared with default arguments.  */
16053         continue;
16054
16055        /* Push the saved tokens for the default argument onto the parser's
16056           lexer stack.  */
16057       tokens = DEFARG_TOKENS (default_arg);
16058       cp_parser_push_lexer_for_tokens (parser, tokens);
16059
16060       /* Parse the assignment-expression.  */
16061       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16062
16063       if (!processing_template_decl)
16064         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16065
16066       TREE_PURPOSE (parm) = parsed_arg;
16067
16068       /* Update any instantiations we've already created.  */
16069       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16070            VEC_iterate (tree, insts, ix, copy); ix++)
16071         TREE_PURPOSE (copy) = parsed_arg;
16072
16073       /* If the token stream has not been completely used up, then
16074          there was extra junk after the end of the default
16075          argument.  */
16076       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16077         cp_parser_error (parser, "expected %<,%>");
16078
16079       /* Revert to the main lexer.  */
16080       cp_parser_pop_lexer (parser);
16081     }
16082
16083   /* Make sure no default arg is missing.  */
16084   check_default_args (fn);
16085
16086   /* Restore the state of local_variables_forbidden_p.  */
16087   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16088
16089   /* Restore the queue.  */
16090   parser->unparsed_functions_queues
16091     = TREE_CHAIN (parser->unparsed_functions_queues);
16092 }
16093
16094 /* Parse the operand of `sizeof' (or a similar operator).  Returns
16095    either a TYPE or an expression, depending on the form of the
16096    input.  The KEYWORD indicates which kind of expression we have
16097    encountered.  */
16098
16099 static tree
16100 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16101 {
16102   static const char *format;
16103   tree expr = NULL_TREE;
16104   const char *saved_message;
16105   bool saved_integral_constant_expression_p;
16106   bool saved_non_integral_constant_expression_p;
16107
16108   /* Initialize FORMAT the first time we get here.  */
16109   if (!format)
16110     format = "types may not be defined in '%s' expressions";
16111
16112   /* Types cannot be defined in a `sizeof' expression.  Save away the
16113      old message.  */
16114   saved_message = parser->type_definition_forbidden_message;
16115   /* And create the new one.  */
16116   parser->type_definition_forbidden_message
16117     = XNEWVEC (const char, strlen (format)
16118                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16119                + 1 /* `\0' */);
16120   sprintf ((char *) parser->type_definition_forbidden_message,
16121            format, IDENTIFIER_POINTER (ridpointers[keyword]));
16122
16123   /* The restrictions on constant-expressions do not apply inside
16124      sizeof expressions.  */
16125   saved_integral_constant_expression_p
16126     = parser->integral_constant_expression_p;
16127   saved_non_integral_constant_expression_p
16128     = parser->non_integral_constant_expression_p;
16129   parser->integral_constant_expression_p = false;
16130
16131   /* Do not actually evaluate the expression.  */
16132   ++skip_evaluation;
16133   /* If it's a `(', then we might be looking at the type-id
16134      construction.  */
16135   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16136     {
16137       tree type;
16138       bool saved_in_type_id_in_expr_p;
16139
16140       /* We can't be sure yet whether we're looking at a type-id or an
16141          expression.  */
16142       cp_parser_parse_tentatively (parser);
16143       /* Consume the `('.  */
16144       cp_lexer_consume_token (parser->lexer);
16145       /* Parse the type-id.  */
16146       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16147       parser->in_type_id_in_expr_p = true;
16148       type = cp_parser_type_id (parser);
16149       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16150       /* Now, look for the trailing `)'.  */
16151       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16152       /* If all went well, then we're done.  */
16153       if (cp_parser_parse_definitely (parser))
16154         {
16155           cp_decl_specifier_seq decl_specs;
16156
16157           /* Build a trivial decl-specifier-seq.  */
16158           clear_decl_specs (&decl_specs);
16159           decl_specs.type = type;
16160
16161           /* Call grokdeclarator to figure out what type this is.  */
16162           expr = grokdeclarator (NULL,
16163                                  &decl_specs,
16164                                  TYPENAME,
16165                                  /*initialized=*/0,
16166                                  /*attrlist=*/NULL);
16167         }
16168     }
16169
16170   /* If the type-id production did not work out, then we must be
16171      looking at the unary-expression production.  */
16172   if (!expr)
16173     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16174                                        /*cast_p=*/false);
16175   /* Go back to evaluating expressions.  */
16176   --skip_evaluation;
16177
16178   /* Free the message we created.  */
16179   free ((char *) parser->type_definition_forbidden_message);
16180   /* And restore the old one.  */
16181   parser->type_definition_forbidden_message = saved_message;
16182   parser->integral_constant_expression_p
16183     = saved_integral_constant_expression_p;
16184   parser->non_integral_constant_expression_p
16185     = saved_non_integral_constant_expression_p;
16186
16187   return expr;
16188 }
16189
16190 /* If the current declaration has no declarator, return true.  */
16191
16192 static bool
16193 cp_parser_declares_only_class_p (cp_parser *parser)
16194 {
16195   /* If the next token is a `;' or a `,' then there is no
16196      declarator.  */
16197   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16198           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16199 }
16200
16201 /* Update the DECL_SPECS to reflect the storage class indicated by
16202    KEYWORD.  */
16203
16204 static void
16205 cp_parser_set_storage_class (cp_parser *parser,
16206                              cp_decl_specifier_seq *decl_specs,
16207                              enum rid keyword)
16208 {
16209   cp_storage_class storage_class;
16210
16211   if (parser->in_unbraced_linkage_specification_p)
16212     {
16213       error ("invalid use of %qD in linkage specification",
16214              ridpointers[keyword]);
16215       return;
16216     }
16217   else if (decl_specs->storage_class != sc_none)
16218     {
16219       decl_specs->multiple_storage_classes_p = true;
16220       return;
16221     }
16222
16223   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16224       && decl_specs->specs[(int) ds_thread])
16225     {
16226       error ("%<__thread%> before %qD", ridpointers[keyword]);
16227       decl_specs->specs[(int) ds_thread] = 0;
16228     }
16229
16230   switch (keyword)
16231     {
16232     case RID_AUTO:
16233       storage_class = sc_auto;
16234       break;
16235     case RID_REGISTER:
16236       storage_class = sc_register;
16237       break;
16238     case RID_STATIC:
16239       storage_class = sc_static;
16240       break;
16241     case RID_EXTERN:
16242       storage_class = sc_extern;
16243       break;
16244     case RID_MUTABLE:
16245       storage_class = sc_mutable;
16246       break;
16247     default:
16248       gcc_unreachable ();
16249     }
16250   decl_specs->storage_class = storage_class;
16251 }
16252
16253 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
16254    is true, the type is a user-defined type; otherwise it is a
16255    built-in type specified by a keyword.  */
16256
16257 static void
16258 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16259                               tree type_spec,
16260                               bool user_defined_p)
16261 {
16262   decl_specs->any_specifiers_p = true;
16263
16264   /* If the user tries to redeclare bool or wchar_t (with, for
16265      example, in "typedef int wchar_t;") we remember that this is what
16266      happened.  In system headers, we ignore these declarations so
16267      that G++ can work with system headers that are not C++-safe.  */
16268   if (decl_specs->specs[(int) ds_typedef]
16269       && !user_defined_p
16270       && (type_spec == boolean_type_node
16271           || type_spec == wchar_type_node)
16272       && (decl_specs->type
16273           || decl_specs->specs[(int) ds_long]
16274           || decl_specs->specs[(int) ds_short]
16275           || decl_specs->specs[(int) ds_unsigned]
16276           || decl_specs->specs[(int) ds_signed]))
16277     {
16278       decl_specs->redefined_builtin_type = type_spec;
16279       if (!decl_specs->type)
16280         {
16281           decl_specs->type = type_spec;
16282           decl_specs->user_defined_type_p = false;
16283         }
16284     }
16285   else if (decl_specs->type)
16286     decl_specs->multiple_types_p = true;
16287   else
16288     {
16289       decl_specs->type = type_spec;
16290       decl_specs->user_defined_type_p = user_defined_p;
16291       decl_specs->redefined_builtin_type = NULL_TREE;
16292     }
16293 }
16294
16295 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16296    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
16297
16298 static bool
16299 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16300 {
16301   return decl_specifiers->specs[(int) ds_friend] != 0;
16302 }
16303
16304 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
16305    issue an error message indicating that TOKEN_DESC was expected.
16306
16307    Returns the token consumed, if the token had the appropriate type.
16308    Otherwise, returns NULL.  */
16309
16310 static cp_token *
16311 cp_parser_require (cp_parser* parser,
16312                    enum cpp_ttype type,
16313                    const char* token_desc)
16314 {
16315   if (cp_lexer_next_token_is (parser->lexer, type))
16316     return cp_lexer_consume_token (parser->lexer);
16317   else
16318     {
16319       /* Output the MESSAGE -- unless we're parsing tentatively.  */
16320       if (!cp_parser_simulate_error (parser))
16321         {
16322           char *message = concat ("expected ", token_desc, NULL);
16323           cp_parser_error (parser, message);
16324           free (message);
16325         }
16326       return NULL;
16327     }
16328 }
16329
16330 /* Like cp_parser_require, except that tokens will be skipped until
16331    the desired token is found.  An error message is still produced if
16332    the next token is not as expected.  */
16333
16334 static void
16335 cp_parser_skip_until_found (cp_parser* parser,
16336                             enum cpp_ttype type,
16337                             const char* token_desc)
16338 {
16339   cp_token *token;
16340   unsigned nesting_depth = 0;
16341
16342   if (cp_parser_require (parser, type, token_desc))
16343     return;
16344
16345   /* Skip tokens until the desired token is found.  */
16346   while (true)
16347     {
16348       /* Peek at the next token.  */
16349       token = cp_lexer_peek_token (parser->lexer);
16350
16351       /* If we've reached the token we want, consume it and stop.  */
16352       if (token->type == type && !nesting_depth)
16353         {
16354           cp_lexer_consume_token (parser->lexer);
16355           return;
16356         }
16357
16358       switch (token->type)
16359         {
16360         case CPP_EOF:
16361         case CPP_PRAGMA_EOL:
16362           /* If we've run out of tokens, stop.  */
16363           return;
16364
16365         case CPP_OPEN_BRACE:
16366         case CPP_OPEN_PAREN:
16367         case CPP_OPEN_SQUARE:
16368           ++nesting_depth;
16369           break;
16370
16371         case CPP_CLOSE_BRACE:
16372         case CPP_CLOSE_PAREN:
16373         case CPP_CLOSE_SQUARE:
16374           if (nesting_depth-- == 0)
16375             return;
16376           break;
16377
16378         default:
16379           break;
16380         }
16381
16382       /* Consume this token.  */
16383       cp_lexer_consume_token (parser->lexer);
16384     }
16385 }
16386
16387 /* If the next token is the indicated keyword, consume it.  Otherwise,
16388    issue an error message indicating that TOKEN_DESC was expected.
16389
16390    Returns the token consumed, if the token had the appropriate type.
16391    Otherwise, returns NULL.  */
16392
16393 static cp_token *
16394 cp_parser_require_keyword (cp_parser* parser,
16395                            enum rid keyword,
16396                            const char* token_desc)
16397 {
16398   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16399
16400   if (token && token->keyword != keyword)
16401     {
16402       dyn_string_t error_msg;
16403
16404       /* Format the error message.  */
16405       error_msg = dyn_string_new (0);
16406       dyn_string_append_cstr (error_msg, "expected ");
16407       dyn_string_append_cstr (error_msg, token_desc);
16408       cp_parser_error (parser, error_msg->s);
16409       dyn_string_delete (error_msg);
16410       return NULL;
16411     }
16412
16413   return token;
16414 }
16415
16416 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16417    function-definition.  */
16418
16419 static bool
16420 cp_parser_token_starts_function_definition_p (cp_token* token)
16421 {
16422   return (/* An ordinary function-body begins with an `{'.  */
16423           token->type == CPP_OPEN_BRACE
16424           /* A ctor-initializer begins with a `:'.  */
16425           || token->type == CPP_COLON
16426           /* A function-try-block begins with `try'.  */
16427           || token->keyword == RID_TRY
16428           /* The named return value extension begins with `return'.  */
16429           || token->keyword == RID_RETURN);
16430 }
16431
16432 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16433    definition.  */
16434
16435 static bool
16436 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16437 {
16438   cp_token *token;
16439
16440   token = cp_lexer_peek_token (parser->lexer);
16441   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16442 }
16443
16444 /* Returns TRUE iff the next token is the "," or ">" ending a
16445    template-argument.  */
16446
16447 static bool
16448 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16449 {
16450   cp_token *token;
16451
16452   token = cp_lexer_peek_token (parser->lexer);
16453   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16454 }
16455
16456 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16457    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
16458
16459 static bool
16460 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16461                                                      size_t n)
16462 {
16463   cp_token *token;
16464
16465   token = cp_lexer_peek_nth_token (parser->lexer, n);
16466   if (token->type == CPP_LESS)
16467     return true;
16468   /* Check for the sequence `<::' in the original code. It would be lexed as
16469      `[:', where `[' is a digraph, and there is no whitespace before
16470      `:'.  */
16471   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16472     {
16473       cp_token *token2;
16474       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16475       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16476         return true;
16477     }
16478   return false;
16479 }
16480
16481 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16482    or none_type otherwise.  */
16483
16484 static enum tag_types
16485 cp_parser_token_is_class_key (cp_token* token)
16486 {
16487   switch (token->keyword)
16488     {
16489     case RID_CLASS:
16490       return class_type;
16491     case RID_STRUCT:
16492       return record_type;
16493     case RID_UNION:
16494       return union_type;
16495
16496     default:
16497       return none_type;
16498     }
16499 }
16500
16501 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16502
16503 static void
16504 cp_parser_check_class_key (enum tag_types class_key, tree type)
16505 {
16506   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16507     pedwarn ("%qs tag used in naming %q#T",
16508             class_key == union_type ? "union"
16509              : class_key == record_type ? "struct" : "class",
16510              type);
16511 }
16512
16513 /* Issue an error message if DECL is redeclared with different
16514    access than its original declaration [class.access.spec/3].
16515    This applies to nested classes and nested class templates.
16516    [class.mem/1].  */
16517
16518 static void
16519 cp_parser_check_access_in_redeclaration (tree decl)
16520 {
16521   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16522     return;
16523
16524   if ((TREE_PRIVATE (decl)
16525        != (current_access_specifier == access_private_node))
16526       || (TREE_PROTECTED (decl)
16527           != (current_access_specifier == access_protected_node)))
16528     error ("%qD redeclared with different access", decl);
16529 }
16530
16531 /* Look for the `template' keyword, as a syntactic disambiguator.
16532    Return TRUE iff it is present, in which case it will be
16533    consumed.  */
16534
16535 static bool
16536 cp_parser_optional_template_keyword (cp_parser *parser)
16537 {
16538   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16539     {
16540       /* The `template' keyword can only be used within templates;
16541          outside templates the parser can always figure out what is a
16542          template and what is not.  */
16543       if (!processing_template_decl)
16544         {
16545           error ("%<template%> (as a disambiguator) is only allowed "
16546                  "within templates");
16547           /* If this part of the token stream is rescanned, the same
16548              error message would be generated.  So, we purge the token
16549              from the stream.  */
16550           cp_lexer_purge_token (parser->lexer);
16551           return false;
16552         }
16553       else
16554         {
16555           /* Consume the `template' keyword.  */
16556           cp_lexer_consume_token (parser->lexer);
16557           return true;
16558         }
16559     }
16560
16561   return false;
16562 }
16563
16564 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16565    set PARSER->SCOPE, and perform other related actions.  */
16566
16567 static void
16568 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16569 {
16570   tree value;
16571   tree check;
16572
16573   /* Get the stored value.  */
16574   value = cp_lexer_consume_token (parser->lexer)->value;
16575   /* Perform any access checks that were deferred.  */
16576   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16577     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16578   /* Set the scope from the stored value.  */
16579   parser->scope = TREE_VALUE (value);
16580   parser->qualifying_scope = TREE_TYPE (value);
16581   parser->object_scope = NULL_TREE;
16582 }
16583
16584 /* Consume tokens up through a non-nested END token.  */
16585
16586 static void
16587 cp_parser_cache_group (cp_parser *parser,
16588                        enum cpp_ttype end,
16589                        unsigned depth)
16590 {
16591   while (true)
16592     {
16593       cp_token *token;
16594
16595       /* Abort a parenthesized expression if we encounter a brace.  */
16596       if ((end == CPP_CLOSE_PAREN || depth == 0)
16597           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16598         return;
16599       /* If we've reached the end of the file, stop.  */
16600       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16601           || (end != CPP_PRAGMA_EOL
16602               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16603         return;
16604       /* Consume the next token.  */
16605       token = cp_lexer_consume_token (parser->lexer);
16606       /* See if it starts a new group.  */
16607       if (token->type == CPP_OPEN_BRACE)
16608         {
16609           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16610           if (depth == 0)
16611             return;
16612         }
16613       else if (token->type == CPP_OPEN_PAREN)
16614         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16615       else if (token->type == CPP_PRAGMA)
16616         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16617       else if (token->type == end)
16618         return;
16619     }
16620 }
16621
16622 /* Begin parsing tentatively.  We always save tokens while parsing
16623    tentatively so that if the tentative parsing fails we can restore the
16624    tokens.  */
16625
16626 static void
16627 cp_parser_parse_tentatively (cp_parser* parser)
16628 {
16629   /* Enter a new parsing context.  */
16630   parser->context = cp_parser_context_new (parser->context);
16631   /* Begin saving tokens.  */
16632   cp_lexer_save_tokens (parser->lexer);
16633   /* In order to avoid repetitive access control error messages,
16634      access checks are queued up until we are no longer parsing
16635      tentatively.  */
16636   push_deferring_access_checks (dk_deferred);
16637 }
16638
16639 /* Commit to the currently active tentative parse.  */
16640
16641 static void
16642 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16643 {
16644   cp_parser_context *context;
16645   cp_lexer *lexer;
16646
16647   /* Mark all of the levels as committed.  */
16648   lexer = parser->lexer;
16649   for (context = parser->context; context->next; context = context->next)
16650     {
16651       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16652         break;
16653       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16654       while (!cp_lexer_saving_tokens (lexer))
16655         lexer = lexer->next;
16656       cp_lexer_commit_tokens (lexer);
16657     }
16658 }
16659
16660 /* Abort the currently active tentative parse.  All consumed tokens
16661    will be rolled back, and no diagnostics will be issued.  */
16662
16663 static void
16664 cp_parser_abort_tentative_parse (cp_parser* parser)
16665 {
16666   cp_parser_simulate_error (parser);
16667   /* Now, pretend that we want to see if the construct was
16668      successfully parsed.  */
16669   cp_parser_parse_definitely (parser);
16670 }
16671
16672 /* Stop parsing tentatively.  If a parse error has occurred, restore the
16673    token stream.  Otherwise, commit to the tokens we have consumed.
16674    Returns true if no error occurred; false otherwise.  */
16675
16676 static bool
16677 cp_parser_parse_definitely (cp_parser* parser)
16678 {
16679   bool error_occurred;
16680   cp_parser_context *context;
16681
16682   /* Remember whether or not an error occurred, since we are about to
16683      destroy that information.  */
16684   error_occurred = cp_parser_error_occurred (parser);
16685   /* Remove the topmost context from the stack.  */
16686   context = parser->context;
16687   parser->context = context->next;
16688   /* If no parse errors occurred, commit to the tentative parse.  */
16689   if (!error_occurred)
16690     {
16691       /* Commit to the tokens read tentatively, unless that was
16692          already done.  */
16693       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16694         cp_lexer_commit_tokens (parser->lexer);
16695
16696       pop_to_parent_deferring_access_checks ();
16697     }
16698   /* Otherwise, if errors occurred, roll back our state so that things
16699      are just as they were before we began the tentative parse.  */
16700   else
16701     {
16702       cp_lexer_rollback_tokens (parser->lexer);
16703       pop_deferring_access_checks ();
16704     }
16705   /* Add the context to the front of the free list.  */
16706   context->next = cp_parser_context_free_list;
16707   cp_parser_context_free_list = context;
16708
16709   return !error_occurred;
16710 }
16711
16712 /* Returns true if we are parsing tentatively and are not committed to
16713    this tentative parse.  */
16714
16715 static bool
16716 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16717 {
16718   return (cp_parser_parsing_tentatively (parser)
16719           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16720 }
16721
16722 /* Returns nonzero iff an error has occurred during the most recent
16723    tentative parse.  */
16724
16725 static bool
16726 cp_parser_error_occurred (cp_parser* parser)
16727 {
16728   return (cp_parser_parsing_tentatively (parser)
16729           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16730 }
16731
16732 /* Returns nonzero if GNU extensions are allowed.  */
16733
16734 static bool
16735 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16736 {
16737   return parser->allow_gnu_extensions_p;
16738 }
16739 \f
16740 /* Objective-C++ Productions */
16741
16742
16743 /* Parse an Objective-C expression, which feeds into a primary-expression
16744    above.
16745
16746    objc-expression:
16747      objc-message-expression
16748      objc-string-literal
16749      objc-encode-expression
16750      objc-protocol-expression
16751      objc-selector-expression
16752
16753   Returns a tree representation of the expression.  */
16754
16755 static tree
16756 cp_parser_objc_expression (cp_parser* parser)
16757 {
16758   /* Try to figure out what kind of declaration is present.  */
16759   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16760
16761   switch (kwd->type)
16762     {
16763     case CPP_OPEN_SQUARE:
16764       return cp_parser_objc_message_expression (parser);
16765
16766     case CPP_OBJC_STRING:
16767       kwd = cp_lexer_consume_token (parser->lexer);
16768       return objc_build_string_object (kwd->value);
16769
16770     case CPP_KEYWORD:
16771       switch (kwd->keyword)
16772         {
16773         case RID_AT_ENCODE:
16774           return cp_parser_objc_encode_expression (parser);
16775
16776         case RID_AT_PROTOCOL:
16777           return cp_parser_objc_protocol_expression (parser);
16778
16779         case RID_AT_SELECTOR:
16780           return cp_parser_objc_selector_expression (parser);
16781
16782         default:
16783           break;
16784         }
16785     default:
16786       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16787       cp_parser_skip_to_end_of_block_or_statement (parser);
16788     }
16789
16790   return error_mark_node;
16791 }
16792
16793 /* Parse an Objective-C message expression.
16794
16795    objc-message-expression:
16796      [ objc-message-receiver objc-message-args ]
16797
16798    Returns a representation of an Objective-C message.  */
16799
16800 static tree
16801 cp_parser_objc_message_expression (cp_parser* parser)
16802 {
16803   tree receiver, messageargs;
16804
16805   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
16806   receiver = cp_parser_objc_message_receiver (parser);
16807   messageargs = cp_parser_objc_message_args (parser);
16808   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16809
16810   return objc_build_message_expr (build_tree_list (receiver, messageargs));
16811 }
16812
16813 /* Parse an objc-message-receiver.
16814
16815    objc-message-receiver:
16816      expression
16817      simple-type-specifier
16818
16819   Returns a representation of the type or expression.  */
16820
16821 static tree
16822 cp_parser_objc_message_receiver (cp_parser* parser)
16823 {
16824   tree rcv;
16825
16826   /* An Objective-C message receiver may be either (1) a type
16827      or (2) an expression.  */
16828   cp_parser_parse_tentatively (parser);
16829   rcv = cp_parser_expression (parser, false);
16830
16831   if (cp_parser_parse_definitely (parser))
16832     return rcv;
16833
16834   rcv = cp_parser_simple_type_specifier (parser,
16835                                          /*decl_specs=*/NULL,
16836                                          CP_PARSER_FLAGS_NONE);
16837
16838   return objc_get_class_reference (rcv);
16839 }
16840
16841 /* Parse the arguments and selectors comprising an Objective-C message.
16842
16843    objc-message-args:
16844      objc-selector
16845      objc-selector-args
16846      objc-selector-args , objc-comma-args
16847
16848    objc-selector-args:
16849      objc-selector [opt] : assignment-expression
16850      objc-selector-args objc-selector [opt] : assignment-expression
16851
16852    objc-comma-args:
16853      assignment-expression
16854      objc-comma-args , assignment-expression
16855
16856    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16857    selector arguments and TREE_VALUE containing a list of comma
16858    arguments.  */
16859
16860 static tree
16861 cp_parser_objc_message_args (cp_parser* parser)
16862 {
16863   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16864   bool maybe_unary_selector_p = true;
16865   cp_token *token = cp_lexer_peek_token (parser->lexer);
16866
16867   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16868     {
16869       tree selector = NULL_TREE, arg;
16870
16871       if (token->type != CPP_COLON)
16872         selector = cp_parser_objc_selector (parser);
16873
16874       /* Detect if we have a unary selector.  */
16875       if (maybe_unary_selector_p
16876           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16877         return build_tree_list (selector, NULL_TREE);
16878
16879       maybe_unary_selector_p = false;
16880       cp_parser_require (parser, CPP_COLON, "`:'");
16881       arg = cp_parser_assignment_expression (parser, false);
16882
16883       sel_args
16884         = chainon (sel_args,
16885                    build_tree_list (selector, arg));
16886
16887       token = cp_lexer_peek_token (parser->lexer);
16888     }
16889
16890   /* Handle non-selector arguments, if any. */
16891   while (token->type == CPP_COMMA)
16892     {
16893       tree arg;
16894
16895       cp_lexer_consume_token (parser->lexer);
16896       arg = cp_parser_assignment_expression (parser, false);
16897
16898       addl_args
16899         = chainon (addl_args,
16900                    build_tree_list (NULL_TREE, arg));
16901
16902       token = cp_lexer_peek_token (parser->lexer);
16903     }
16904
16905   return build_tree_list (sel_args, addl_args);
16906 }
16907
16908 /* Parse an Objective-C encode expression.
16909
16910    objc-encode-expression:
16911      @encode objc-typename
16912
16913    Returns an encoded representation of the type argument.  */
16914
16915 static tree
16916 cp_parser_objc_encode_expression (cp_parser* parser)
16917 {
16918   tree type;
16919
16920   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
16921   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16922   type = complete_type (cp_parser_type_id (parser));
16923   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16924
16925   if (!type)
16926     {
16927       error ("%<@encode%> must specify a type as an argument");
16928       return error_mark_node;
16929     }
16930
16931   return objc_build_encode_expr (type);
16932 }
16933
16934 /* Parse an Objective-C @defs expression.  */
16935
16936 static tree
16937 cp_parser_objc_defs_expression (cp_parser *parser)
16938 {
16939   tree name;
16940
16941   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
16942   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16943   name = cp_parser_identifier (parser);
16944   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16945
16946   return objc_get_class_ivars (name);
16947 }
16948
16949 /* Parse an Objective-C protocol expression.
16950
16951   objc-protocol-expression:
16952     @protocol ( identifier )
16953
16954   Returns a representation of the protocol expression.  */
16955
16956 static tree
16957 cp_parser_objc_protocol_expression (cp_parser* parser)
16958 {
16959   tree proto;
16960
16961   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
16962   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16963   proto = cp_parser_identifier (parser);
16964   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16965
16966   return objc_build_protocol_expr (proto);
16967 }
16968
16969 /* Parse an Objective-C selector expression.
16970
16971    objc-selector-expression:
16972      @selector ( objc-method-signature )
16973
16974    objc-method-signature:
16975      objc-selector
16976      objc-selector-seq
16977
16978    objc-selector-seq:
16979      objc-selector :
16980      objc-selector-seq objc-selector :
16981
16982   Returns a representation of the method selector.  */
16983
16984 static tree
16985 cp_parser_objc_selector_expression (cp_parser* parser)
16986 {
16987   tree sel_seq = NULL_TREE;
16988   bool maybe_unary_selector_p = true;
16989   cp_token *token;
16990
16991   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
16992   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16993   token = cp_lexer_peek_token (parser->lexer);
16994
16995   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16996          || token->type == CPP_SCOPE)
16997     {
16998       tree selector = NULL_TREE;
16999
17000       if (token->type != CPP_COLON
17001           || token->type == CPP_SCOPE)
17002         selector = cp_parser_objc_selector (parser);
17003
17004       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17005           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17006         {
17007           /* Detect if we have a unary selector.  */
17008           if (maybe_unary_selector_p)
17009             {
17010               sel_seq = selector;
17011               goto finish_selector;
17012             }
17013           else
17014             {
17015               cp_parser_error (parser, "expected %<:%>");
17016             }
17017         }
17018       maybe_unary_selector_p = false;
17019       token = cp_lexer_consume_token (parser->lexer);
17020
17021       if (token->type == CPP_SCOPE)
17022         {
17023           sel_seq
17024             = chainon (sel_seq,
17025                        build_tree_list (selector, NULL_TREE));
17026           sel_seq
17027             = chainon (sel_seq,
17028                        build_tree_list (NULL_TREE, NULL_TREE));
17029         }
17030       else
17031         sel_seq
17032           = chainon (sel_seq,
17033                      build_tree_list (selector, NULL_TREE));
17034
17035       token = cp_lexer_peek_token (parser->lexer);
17036     }
17037
17038  finish_selector:
17039   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17040
17041   return objc_build_selector_expr (sel_seq);
17042 }
17043
17044 /* Parse a list of identifiers.
17045
17046    objc-identifier-list:
17047      identifier
17048      objc-identifier-list , identifier
17049
17050    Returns a TREE_LIST of identifier nodes.  */
17051
17052 static tree
17053 cp_parser_objc_identifier_list (cp_parser* parser)
17054 {
17055   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17056   cp_token *sep = cp_lexer_peek_token (parser->lexer);
17057
17058   while (sep->type == CPP_COMMA)
17059     {
17060       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17061       list = chainon (list,
17062                       build_tree_list (NULL_TREE,
17063                                        cp_parser_identifier (parser)));
17064       sep = cp_lexer_peek_token (parser->lexer);
17065     }
17066
17067   return list;
17068 }
17069
17070 /* Parse an Objective-C alias declaration.
17071
17072    objc-alias-declaration:
17073      @compatibility_alias identifier identifier ;
17074
17075    This function registers the alias mapping with the Objective-C front-end.
17076    It returns nothing.  */
17077
17078 static void
17079 cp_parser_objc_alias_declaration (cp_parser* parser)
17080 {
17081   tree alias, orig;
17082
17083   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
17084   alias = cp_parser_identifier (parser);
17085   orig = cp_parser_identifier (parser);
17086   objc_declare_alias (alias, orig);
17087   cp_parser_consume_semicolon_at_end_of_statement (parser);
17088 }
17089
17090 /* Parse an Objective-C class forward-declaration.
17091
17092    objc-class-declaration:
17093      @class objc-identifier-list ;
17094
17095    The function registers the forward declarations with the Objective-C
17096    front-end.  It returns nothing.  */
17097
17098 static void
17099 cp_parser_objc_class_declaration (cp_parser* parser)
17100 {
17101   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
17102   objc_declare_class (cp_parser_objc_identifier_list (parser));
17103   cp_parser_consume_semicolon_at_end_of_statement (parser);
17104 }
17105
17106 /* Parse a list of Objective-C protocol references.
17107
17108    objc-protocol-refs-opt:
17109      objc-protocol-refs [opt]
17110
17111    objc-protocol-refs:
17112      < objc-identifier-list >
17113
17114    Returns a TREE_LIST of identifiers, if any.  */
17115
17116 static tree
17117 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17118 {
17119   tree protorefs = NULL_TREE;
17120
17121   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17122     {
17123       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
17124       protorefs = cp_parser_objc_identifier_list (parser);
17125       cp_parser_require (parser, CPP_GREATER, "`>'");
17126     }
17127
17128   return protorefs;
17129 }
17130
17131 /* Parse a Objective-C visibility specification.  */
17132
17133 static void
17134 cp_parser_objc_visibility_spec (cp_parser* parser)
17135 {
17136   cp_token *vis = cp_lexer_peek_token (parser->lexer);
17137
17138   switch (vis->keyword)
17139     {
17140     case RID_AT_PRIVATE:
17141       objc_set_visibility (2);
17142       break;
17143     case RID_AT_PROTECTED:
17144       objc_set_visibility (0);
17145       break;
17146     case RID_AT_PUBLIC:
17147       objc_set_visibility (1);
17148       break;
17149     default:
17150       return;
17151     }
17152
17153   /* Eat '@private'/'@protected'/'@public'.  */
17154   cp_lexer_consume_token (parser->lexer);
17155 }
17156
17157 /* Parse an Objective-C method type.  */
17158
17159 static void
17160 cp_parser_objc_method_type (cp_parser* parser)
17161 {
17162   objc_set_method_type
17163    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17164     ? PLUS_EXPR
17165     : MINUS_EXPR);
17166 }
17167
17168 /* Parse an Objective-C protocol qualifier.  */
17169
17170 static tree
17171 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17172 {
17173   tree quals = NULL_TREE, node;
17174   cp_token *token = cp_lexer_peek_token (parser->lexer);
17175
17176   node = token->value;
17177
17178   while (node && TREE_CODE (node) == IDENTIFIER_NODE
17179          && (node == ridpointers [(int) RID_IN]
17180              || node == ridpointers [(int) RID_OUT]
17181              || node == ridpointers [(int) RID_INOUT]
17182              || node == ridpointers [(int) RID_BYCOPY]
17183              || node == ridpointers [(int) RID_BYREF]
17184              || node == ridpointers [(int) RID_ONEWAY]))
17185     {
17186       quals = tree_cons (NULL_TREE, node, quals);
17187       cp_lexer_consume_token (parser->lexer);
17188       token = cp_lexer_peek_token (parser->lexer);
17189       node = token->value;
17190     }
17191
17192   return quals;
17193 }
17194
17195 /* Parse an Objective-C typename.  */
17196
17197 static tree
17198 cp_parser_objc_typename (cp_parser* parser)
17199 {
17200   tree typename = NULL_TREE;
17201
17202   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17203     {
17204       tree proto_quals, cp_type = NULL_TREE;
17205
17206       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17207       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17208
17209       /* An ObjC type name may consist of just protocol qualifiers, in which
17210          case the type shall default to 'id'.  */
17211       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17212         cp_type = cp_parser_type_id (parser);
17213
17214       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17215       typename = build_tree_list (proto_quals, cp_type);
17216     }
17217
17218   return typename;
17219 }
17220
17221 /* Check to see if TYPE refers to an Objective-C selector name.  */
17222
17223 static bool
17224 cp_parser_objc_selector_p (enum cpp_ttype type)
17225 {
17226   return (type == CPP_NAME || type == CPP_KEYWORD
17227           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17228           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17229           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17230           || type == CPP_XOR || type == CPP_XOR_EQ);
17231 }
17232
17233 /* Parse an Objective-C selector.  */
17234
17235 static tree
17236 cp_parser_objc_selector (cp_parser* parser)
17237 {
17238   cp_token *token = cp_lexer_consume_token (parser->lexer);
17239
17240   if (!cp_parser_objc_selector_p (token->type))
17241     {
17242       error ("invalid Objective-C++ selector name");
17243       return error_mark_node;
17244     }
17245
17246   /* C++ operator names are allowed to appear in ObjC selectors.  */
17247   switch (token->type)
17248     {
17249     case CPP_AND_AND: return get_identifier ("and");
17250     case CPP_AND_EQ: return get_identifier ("and_eq");
17251     case CPP_AND: return get_identifier ("bitand");
17252     case CPP_OR: return get_identifier ("bitor");
17253     case CPP_COMPL: return get_identifier ("compl");
17254     case CPP_NOT: return get_identifier ("not");
17255     case CPP_NOT_EQ: return get_identifier ("not_eq");
17256     case CPP_OR_OR: return get_identifier ("or");
17257     case CPP_OR_EQ: return get_identifier ("or_eq");
17258     case CPP_XOR: return get_identifier ("xor");
17259     case CPP_XOR_EQ: return get_identifier ("xor_eq");
17260     default: return token->value;
17261     }
17262 }
17263
17264 /* Parse an Objective-C params list.  */
17265
17266 static tree
17267 cp_parser_objc_method_keyword_params (cp_parser* parser)
17268 {
17269   tree params = NULL_TREE;
17270   bool maybe_unary_selector_p = true;
17271   cp_token *token = cp_lexer_peek_token (parser->lexer);
17272
17273   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17274     {
17275       tree selector = NULL_TREE, typename, identifier;
17276
17277       if (token->type != CPP_COLON)
17278         selector = cp_parser_objc_selector (parser);
17279
17280       /* Detect if we have a unary selector.  */
17281       if (maybe_unary_selector_p
17282           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17283         return selector;
17284
17285       maybe_unary_selector_p = false;
17286       cp_parser_require (parser, CPP_COLON, "`:'");
17287       typename = cp_parser_objc_typename (parser);
17288       identifier = cp_parser_identifier (parser);
17289
17290       params
17291         = chainon (params,
17292                    objc_build_keyword_decl (selector,
17293                                             typename,
17294                                             identifier));
17295
17296       token = cp_lexer_peek_token (parser->lexer);
17297     }
17298
17299   return params;
17300 }
17301
17302 /* Parse the non-keyword Objective-C params.  */
17303
17304 static tree
17305 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17306 {
17307   tree params = make_node (TREE_LIST);
17308   cp_token *token = cp_lexer_peek_token (parser->lexer);
17309   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
17310
17311   while (token->type == CPP_COMMA)
17312     {
17313       cp_parameter_declarator *parmdecl;
17314       tree parm;
17315
17316       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17317       token = cp_lexer_peek_token (parser->lexer);
17318
17319       if (token->type == CPP_ELLIPSIS)
17320         {
17321           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
17322           *ellipsisp = true;
17323           break;
17324         }
17325
17326       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17327       parm = grokdeclarator (parmdecl->declarator,
17328                              &parmdecl->decl_specifiers,
17329                              PARM, /*initialized=*/0,
17330                              /*attrlist=*/NULL);
17331
17332       chainon (params, build_tree_list (NULL_TREE, parm));
17333       token = cp_lexer_peek_token (parser->lexer);
17334     }
17335
17336   return params;
17337 }
17338
17339 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
17340
17341 static void
17342 cp_parser_objc_interstitial_code (cp_parser* parser)
17343 {
17344   cp_token *token = cp_lexer_peek_token (parser->lexer);
17345
17346   /* If the next token is `extern' and the following token is a string
17347      literal, then we have a linkage specification.  */
17348   if (token->keyword == RID_EXTERN
17349       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17350     cp_parser_linkage_specification (parser);
17351   /* Handle #pragma, if any.  */
17352   else if (token->type == CPP_PRAGMA)
17353     cp_parser_pragma (parser, pragma_external);
17354   /* Allow stray semicolons.  */
17355   else if (token->type == CPP_SEMICOLON)
17356     cp_lexer_consume_token (parser->lexer);
17357   /* Finally, try to parse a block-declaration, or a function-definition.  */
17358   else
17359     cp_parser_block_declaration (parser, /*statement_p=*/false);
17360 }
17361
17362 /* Parse a method signature.  */
17363
17364 static tree
17365 cp_parser_objc_method_signature (cp_parser* parser)
17366 {
17367   tree rettype, kwdparms, optparms;
17368   bool ellipsis = false;
17369
17370   cp_parser_objc_method_type (parser);
17371   rettype = cp_parser_objc_typename (parser);
17372   kwdparms = cp_parser_objc_method_keyword_params (parser);
17373   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17374
17375   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17376 }
17377
17378 /* Pars an Objective-C method prototype list.  */
17379
17380 static void
17381 cp_parser_objc_method_prototype_list (cp_parser* parser)
17382 {
17383   cp_token *token = cp_lexer_peek_token (parser->lexer);
17384
17385   while (token->keyword != RID_AT_END)
17386     {
17387       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17388         {
17389           objc_add_method_declaration
17390            (cp_parser_objc_method_signature (parser));
17391           cp_parser_consume_semicolon_at_end_of_statement (parser);
17392         }
17393       else
17394         /* Allow for interspersed non-ObjC++ code.  */
17395         cp_parser_objc_interstitial_code (parser);
17396
17397       token = cp_lexer_peek_token (parser->lexer);
17398     }
17399
17400   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17401   objc_finish_interface ();
17402 }
17403
17404 /* Parse an Objective-C method definition list.  */
17405
17406 static void
17407 cp_parser_objc_method_definition_list (cp_parser* parser)
17408 {
17409   cp_token *token = cp_lexer_peek_token (parser->lexer);
17410
17411   while (token->keyword != RID_AT_END)
17412     {
17413       tree meth;
17414
17415       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17416         {
17417           push_deferring_access_checks (dk_deferred);
17418           objc_start_method_definition
17419            (cp_parser_objc_method_signature (parser));
17420
17421           /* For historical reasons, we accept an optional semicolon.  */
17422           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17423             cp_lexer_consume_token (parser->lexer);
17424
17425           perform_deferred_access_checks ();
17426           stop_deferring_access_checks ();
17427           meth = cp_parser_function_definition_after_declarator (parser,
17428                                                                  false);
17429           pop_deferring_access_checks ();
17430           objc_finish_method_definition (meth);
17431         }
17432       else
17433         /* Allow for interspersed non-ObjC++ code.  */
17434         cp_parser_objc_interstitial_code (parser);
17435
17436       token = cp_lexer_peek_token (parser->lexer);
17437     }
17438
17439   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17440   objc_finish_implementation ();
17441 }
17442
17443 /* Parse Objective-C ivars.  */
17444
17445 static void
17446 cp_parser_objc_class_ivars (cp_parser* parser)
17447 {
17448   cp_token *token = cp_lexer_peek_token (parser->lexer);
17449
17450   if (token->type != CPP_OPEN_BRACE)
17451     return;     /* No ivars specified.  */
17452
17453   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
17454   token = cp_lexer_peek_token (parser->lexer);
17455
17456   while (token->type != CPP_CLOSE_BRACE)
17457     {
17458       cp_decl_specifier_seq declspecs;
17459       int decl_class_or_enum_p;
17460       tree prefix_attributes;
17461
17462       cp_parser_objc_visibility_spec (parser);
17463
17464       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17465         break;
17466
17467       cp_parser_decl_specifier_seq (parser,
17468                                     CP_PARSER_FLAGS_OPTIONAL,
17469                                     &declspecs,
17470                                     &decl_class_or_enum_p);
17471       prefix_attributes = declspecs.attributes;
17472       declspecs.attributes = NULL_TREE;
17473
17474       /* Keep going until we hit the `;' at the end of the
17475          declaration.  */
17476       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17477         {
17478           tree width = NULL_TREE, attributes, first_attribute, decl;
17479           cp_declarator *declarator = NULL;
17480           int ctor_dtor_or_conv_p;
17481
17482           /* Check for a (possibly unnamed) bitfield declaration.  */
17483           token = cp_lexer_peek_token (parser->lexer);
17484           if (token->type == CPP_COLON)
17485             goto eat_colon;
17486
17487           if (token->type == CPP_NAME
17488               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17489                   == CPP_COLON))
17490             {
17491               /* Get the name of the bitfield.  */
17492               declarator = make_id_declarator (NULL_TREE,
17493                                                cp_parser_identifier (parser),
17494                                                sfk_none);
17495
17496              eat_colon:
17497               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17498               /* Get the width of the bitfield.  */
17499               width
17500                 = cp_parser_constant_expression (parser,
17501                                                  /*allow_non_constant=*/false,
17502                                                  NULL);
17503             }
17504           else
17505             {
17506               /* Parse the declarator.  */
17507               declarator
17508                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17509                                         &ctor_dtor_or_conv_p,
17510                                         /*parenthesized_p=*/NULL,
17511                                         /*member_p=*/false);
17512             }
17513
17514           /* Look for attributes that apply to the ivar.  */
17515           attributes = cp_parser_attributes_opt (parser);
17516           /* Remember which attributes are prefix attributes and
17517              which are not.  */
17518           first_attribute = attributes;
17519           /* Combine the attributes.  */
17520           attributes = chainon (prefix_attributes, attributes);
17521
17522           if (width)
17523             {
17524               /* Create the bitfield declaration.  */
17525               decl = grokbitfield (declarator, &declspecs, width);
17526               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17527             }
17528           else
17529             decl = grokfield (declarator, &declspecs,
17530                               NULL_TREE, /*init_const_expr_p=*/false,
17531                               NULL_TREE, attributes);
17532
17533           /* Add the instance variable.  */
17534           objc_add_instance_variable (decl);
17535
17536           /* Reset PREFIX_ATTRIBUTES.  */
17537           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17538             attributes = TREE_CHAIN (attributes);
17539           if (attributes)
17540             TREE_CHAIN (attributes) = NULL_TREE;
17541
17542           token = cp_lexer_peek_token (parser->lexer);
17543
17544           if (token->type == CPP_COMMA)
17545             {
17546               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17547               continue;
17548             }
17549           break;
17550         }
17551
17552       cp_parser_consume_semicolon_at_end_of_statement (parser);
17553       token = cp_lexer_peek_token (parser->lexer);
17554     }
17555
17556   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17557   /* For historical reasons, we accept an optional semicolon.  */
17558   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17559     cp_lexer_consume_token (parser->lexer);
17560 }
17561
17562 /* Parse an Objective-C protocol declaration.  */
17563
17564 static void
17565 cp_parser_objc_protocol_declaration (cp_parser* parser)
17566 {
17567   tree proto, protorefs;
17568   cp_token *tok;
17569
17570   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17571   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17572     {
17573       error ("identifier expected after %<@protocol%>");
17574       goto finish;
17575     }
17576
17577   /* See if we have a forward declaration or a definition.  */
17578   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17579
17580   /* Try a forward declaration first.  */
17581   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17582     {
17583       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17584      finish:
17585       cp_parser_consume_semicolon_at_end_of_statement (parser);
17586     }
17587
17588   /* Ok, we got a full-fledged definition (or at least should).  */
17589   else
17590     {
17591       proto = cp_parser_identifier (parser);
17592       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17593       objc_start_protocol (proto, protorefs);
17594       cp_parser_objc_method_prototype_list (parser);
17595     }
17596 }
17597
17598 /* Parse an Objective-C superclass or category.  */
17599
17600 static void
17601 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17602                                                           tree *categ)
17603 {
17604   cp_token *next = cp_lexer_peek_token (parser->lexer);
17605
17606   *super = *categ = NULL_TREE;
17607   if (next->type == CPP_COLON)
17608     {
17609       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17610       *super = cp_parser_identifier (parser);
17611     }
17612   else if (next->type == CPP_OPEN_PAREN)
17613     {
17614       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17615       *categ = cp_parser_identifier (parser);
17616       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17617     }
17618 }
17619
17620 /* Parse an Objective-C class interface.  */
17621
17622 static void
17623 cp_parser_objc_class_interface (cp_parser* parser)
17624 {
17625   tree name, super, categ, protos;
17626
17627   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17628   name = cp_parser_identifier (parser);
17629   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17630   protos = cp_parser_objc_protocol_refs_opt (parser);
17631
17632   /* We have either a class or a category on our hands.  */
17633   if (categ)
17634     objc_start_category_interface (name, categ, protos);
17635   else
17636     {
17637       objc_start_class_interface (name, super, protos);
17638       /* Handle instance variable declarations, if any.  */
17639       cp_parser_objc_class_ivars (parser);
17640       objc_continue_interface ();
17641     }
17642
17643   cp_parser_objc_method_prototype_list (parser);
17644 }
17645
17646 /* Parse an Objective-C class implementation.  */
17647
17648 static void
17649 cp_parser_objc_class_implementation (cp_parser* parser)
17650 {
17651   tree name, super, categ;
17652
17653   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17654   name = cp_parser_identifier (parser);
17655   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17656
17657   /* We have either a class or a category on our hands.  */
17658   if (categ)
17659     objc_start_category_implementation (name, categ);
17660   else
17661     {
17662       objc_start_class_implementation (name, super);
17663       /* Handle instance variable declarations, if any.  */
17664       cp_parser_objc_class_ivars (parser);
17665       objc_continue_implementation ();
17666     }
17667
17668   cp_parser_objc_method_definition_list (parser);
17669 }
17670
17671 /* Consume the @end token and finish off the implementation.  */
17672
17673 static void
17674 cp_parser_objc_end_implementation (cp_parser* parser)
17675 {
17676   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17677   objc_finish_implementation ();
17678 }
17679
17680 /* Parse an Objective-C declaration.  */
17681
17682 static void
17683 cp_parser_objc_declaration (cp_parser* parser)
17684 {
17685   /* Try to figure out what kind of declaration is present.  */
17686   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17687
17688   switch (kwd->keyword)
17689     {
17690     case RID_AT_ALIAS:
17691       cp_parser_objc_alias_declaration (parser);
17692       break;
17693     case RID_AT_CLASS:
17694       cp_parser_objc_class_declaration (parser);
17695       break;
17696     case RID_AT_PROTOCOL:
17697       cp_parser_objc_protocol_declaration (parser);
17698       break;
17699     case RID_AT_INTERFACE:
17700       cp_parser_objc_class_interface (parser);
17701       break;
17702     case RID_AT_IMPLEMENTATION:
17703       cp_parser_objc_class_implementation (parser);
17704       break;
17705     case RID_AT_END:
17706       cp_parser_objc_end_implementation (parser);
17707       break;
17708     default:
17709       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17710       cp_parser_skip_to_end_of_block_or_statement (parser);
17711     }
17712 }
17713
17714 /* Parse an Objective-C try-catch-finally statement.
17715
17716    objc-try-catch-finally-stmt:
17717      @try compound-statement objc-catch-clause-seq [opt]
17718        objc-finally-clause [opt]
17719
17720    objc-catch-clause-seq:
17721      objc-catch-clause objc-catch-clause-seq [opt]
17722
17723    objc-catch-clause:
17724      @catch ( exception-declaration ) compound-statement
17725
17726    objc-finally-clause
17727      @finally compound-statement
17728
17729    Returns NULL_TREE.  */
17730
17731 static tree
17732 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17733   location_t location;
17734   tree stmt;
17735
17736   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17737   location = cp_lexer_peek_token (parser->lexer)->location;
17738   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17739      node, lest it get absorbed into the surrounding block.  */
17740   stmt = push_stmt_list ();
17741   cp_parser_compound_statement (parser, NULL, false);
17742   objc_begin_try_stmt (location, pop_stmt_list (stmt));
17743
17744   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17745     {
17746       cp_parameter_declarator *parmdecl;
17747       tree parm;
17748
17749       cp_lexer_consume_token (parser->lexer);
17750       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17751       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17752       parm = grokdeclarator (parmdecl->declarator,
17753                              &parmdecl->decl_specifiers,
17754                              PARM, /*initialized=*/0,
17755                              /*attrlist=*/NULL);
17756       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17757       objc_begin_catch_clause (parm);
17758       cp_parser_compound_statement (parser, NULL, false);
17759       objc_finish_catch_clause ();
17760     }
17761
17762   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17763     {
17764       cp_lexer_consume_token (parser->lexer);
17765       location = cp_lexer_peek_token (parser->lexer)->location;
17766       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17767          node, lest it get absorbed into the surrounding block.  */
17768       stmt = push_stmt_list ();
17769       cp_parser_compound_statement (parser, NULL, false);
17770       objc_build_finally_clause (location, pop_stmt_list (stmt));
17771     }
17772
17773   return objc_finish_try_stmt ();
17774 }
17775
17776 /* Parse an Objective-C synchronized statement.
17777
17778    objc-synchronized-stmt:
17779      @synchronized ( expression ) compound-statement
17780
17781    Returns NULL_TREE.  */
17782
17783 static tree
17784 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17785   location_t location;
17786   tree lock, stmt;
17787
17788   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17789
17790   location = cp_lexer_peek_token (parser->lexer)->location;
17791   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17792   lock = cp_parser_expression (parser, false);
17793   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17794
17795   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17796      node, lest it get absorbed into the surrounding block.  */
17797   stmt = push_stmt_list ();
17798   cp_parser_compound_statement (parser, NULL, false);
17799
17800   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17801 }
17802
17803 /* Parse an Objective-C throw statement.
17804
17805    objc-throw-stmt:
17806      @throw assignment-expression [opt] ;
17807
17808    Returns a constructed '@throw' statement.  */
17809
17810 static tree
17811 cp_parser_objc_throw_statement (cp_parser *parser) {
17812   tree expr = NULL_TREE;
17813
17814   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17815
17816   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17817     expr = cp_parser_assignment_expression (parser, false);
17818
17819   cp_parser_consume_semicolon_at_end_of_statement (parser);
17820
17821   return objc_build_throw_stmt (expr);
17822 }
17823
17824 /* Parse an Objective-C statement.  */
17825
17826 static tree
17827 cp_parser_objc_statement (cp_parser * parser) {
17828   /* Try to figure out what kind of declaration is present.  */
17829   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17830
17831   switch (kwd->keyword)
17832     {
17833     case RID_AT_TRY:
17834       return cp_parser_objc_try_catch_finally_statement (parser);
17835     case RID_AT_SYNCHRONIZED:
17836       return cp_parser_objc_synchronized_statement (parser);
17837     case RID_AT_THROW:
17838       return cp_parser_objc_throw_statement (parser);
17839     default:
17840       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17841       cp_parser_skip_to_end_of_block_or_statement (parser);
17842     }
17843
17844   return error_mark_node;
17845 }
17846 \f
17847 /* OpenMP 2.5 parsing routines.  */
17848
17849 /* All OpenMP clauses.  OpenMP 2.5.  */
17850 typedef enum pragma_omp_clause {
17851   PRAGMA_OMP_CLAUSE_NONE = 0,
17852
17853   PRAGMA_OMP_CLAUSE_COPYIN,
17854   PRAGMA_OMP_CLAUSE_COPYPRIVATE,
17855   PRAGMA_OMP_CLAUSE_DEFAULT,
17856   PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
17857   PRAGMA_OMP_CLAUSE_IF,
17858   PRAGMA_OMP_CLAUSE_LASTPRIVATE,
17859   PRAGMA_OMP_CLAUSE_NOWAIT,
17860   PRAGMA_OMP_CLAUSE_NUM_THREADS,
17861   PRAGMA_OMP_CLAUSE_ORDERED,
17862   PRAGMA_OMP_CLAUSE_PRIVATE,
17863   PRAGMA_OMP_CLAUSE_REDUCTION,
17864   PRAGMA_OMP_CLAUSE_SCHEDULE,
17865   PRAGMA_OMP_CLAUSE_SHARED
17866 } pragma_omp_clause;
17867
17868 /* Returns name of the next clause.
17869    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17870    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
17871    returned and the token is consumed.  */
17872
17873 static pragma_omp_clause
17874 cp_parser_omp_clause_name (cp_parser *parser)
17875 {
17876   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
17877
17878   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
17879     result = PRAGMA_OMP_CLAUSE_IF;
17880   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
17881     result = PRAGMA_OMP_CLAUSE_DEFAULT;
17882   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
17883     result = PRAGMA_OMP_CLAUSE_PRIVATE;
17884   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17885     {
17886       tree id = cp_lexer_peek_token (parser->lexer)->value;
17887       const char *p = IDENTIFIER_POINTER (id);
17888
17889       switch (p[0])
17890         {
17891         case 'c':
17892           if (!strcmp ("copyin", p))
17893             result = PRAGMA_OMP_CLAUSE_COPYIN;
17894           else if (!strcmp ("copyprivate", p))
17895             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
17896           break;
17897         case 'f':
17898           if (!strcmp ("firstprivate", p))
17899             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
17900           break;
17901         case 'l':
17902           if (!strcmp ("lastprivate", p))
17903             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
17904           break;
17905         case 'n':
17906           if (!strcmp ("nowait", p))
17907             result = PRAGMA_OMP_CLAUSE_NOWAIT;
17908           else if (!strcmp ("num_threads", p))
17909             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
17910           break;
17911         case 'o':
17912           if (!strcmp ("ordered", p))
17913             result = PRAGMA_OMP_CLAUSE_ORDERED;
17914           break;
17915         case 'r':
17916           if (!strcmp ("reduction", p))
17917             result = PRAGMA_OMP_CLAUSE_REDUCTION;
17918           break;
17919         case 's':
17920           if (!strcmp ("schedule", p))
17921             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
17922           else if (!strcmp ("shared", p))
17923             result = PRAGMA_OMP_CLAUSE_SHARED;
17924           break;
17925         }
17926     }
17927
17928   if (result != PRAGMA_OMP_CLAUSE_NONE)
17929     cp_lexer_consume_token (parser->lexer);
17930
17931   return result;
17932 }
17933
17934 /* Validate that a clause of the given type does not already exist.  */
17935
17936 static void
17937 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
17938 {
17939   tree c;
17940
17941   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
17942     if (OMP_CLAUSE_CODE (c) == code)
17943       {
17944         error ("too many %qs clauses", name);
17945         break;
17946       }
17947 }
17948
17949 /* OpenMP 2.5:
17950    variable-list:
17951      identifier
17952      variable-list , identifier
17953
17954    In addition, we match a closing parenthesis.  An opening parenthesis
17955    will have been consumed by the caller.
17956
17957    If KIND is nonzero, create the appropriate node and install the decl
17958    in OMP_CLAUSE_DECL and add the node to the head of the list.
17959
17960    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17961    return the list created.  */
17962
17963 static tree
17964 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
17965                                 tree list)
17966 {
17967   while (1)
17968     {
17969       tree name, decl;
17970
17971       name = cp_parser_id_expression (parser, /*template_p=*/false,
17972                                       /*check_dependency_p=*/true,
17973                                       /*template_p=*/NULL,
17974                                       /*declarator_p=*/false,
17975                                       /*optional_p=*/false,
17976                                       /*member_p=*/false);
17977       if (name == error_mark_node)
17978         goto skip_comma;
17979
17980       decl = cp_parser_lookup_name_simple (parser, name);
17981       if (decl == error_mark_node)
17982         cp_parser_name_lookup_error (parser, name, decl, NULL);
17983       else if (kind != 0)
17984         {
17985           tree u = build_omp_clause (kind);
17986           OMP_CLAUSE_DECL (u) = decl;
17987           OMP_CLAUSE_CHAIN (u) = list;
17988           list = u;
17989         }
17990       else
17991         list = tree_cons (decl, NULL_TREE, list);
17992
17993     get_comma:
17994       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17995         break;
17996       cp_lexer_consume_token (parser->lexer);
17997     }
17998
17999   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18000     {
18001       int ending;
18002
18003       /* Try to resync to an unnested comma.  Copied from
18004          cp_parser_parenthesized_expression_list.  */
18005     skip_comma:
18006       ending = cp_parser_skip_to_closing_parenthesis (parser,
18007                                                       /*recovering=*/true,
18008                                                       /*or_comma=*/true,
18009                                                       /*consume_paren=*/true);
18010       if (ending < 0)
18011         goto get_comma;
18012     }
18013
18014   return list;
18015 }
18016
18017 /* Similarly, but expect leading and trailing parenthesis.  This is a very
18018    common case for omp clauses.  */
18019
18020 static tree
18021 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18022 {
18023   if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18024     return cp_parser_omp_var_list_no_open (parser, kind, list);
18025   return list;
18026 }
18027
18028 /* OpenMP 2.5:
18029    default ( shared | none ) */
18030
18031 static tree
18032 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18033 {
18034   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18035   tree c;
18036
18037   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18038     return list;
18039   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18040     {
18041       tree id = cp_lexer_peek_token (parser->lexer)->value;
18042       const char *p = IDENTIFIER_POINTER (id);
18043
18044       switch (p[0])
18045         {
18046         case 'n':
18047           if (strcmp ("none", p) != 0)
18048             goto invalid_kind;
18049           kind = OMP_CLAUSE_DEFAULT_NONE;
18050           break;
18051
18052         case 's':
18053           if (strcmp ("shared", p) != 0)
18054             goto invalid_kind;
18055           kind = OMP_CLAUSE_DEFAULT_SHARED;
18056           break;
18057
18058         default:
18059           goto invalid_kind;
18060         }
18061
18062       cp_lexer_consume_token (parser->lexer);
18063     }
18064   else
18065     {
18066     invalid_kind:
18067       cp_parser_error (parser, "expected %<none%> or %<shared%>");
18068     }
18069
18070   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18071     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18072                                            /*or_comma=*/false,
18073                                            /*consume_paren=*/true);
18074
18075   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18076     return list;
18077
18078   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18079   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18080   OMP_CLAUSE_CHAIN (c) = list;
18081   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18082
18083   return c;
18084 }
18085
18086 /* OpenMP 2.5:
18087    if ( expression ) */
18088
18089 static tree
18090 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18091 {
18092   tree t, c;
18093
18094   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18095     return list;
18096
18097   t = cp_parser_condition (parser);
18098
18099   if (t == error_mark_node
18100       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18101     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18102                                            /*or_comma=*/false,
18103                                            /*consume_paren=*/true);
18104
18105   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18106
18107   c = build_omp_clause (OMP_CLAUSE_IF);
18108   OMP_CLAUSE_IF_EXPR (c) = t;
18109   OMP_CLAUSE_CHAIN (c) = list;
18110
18111   return c;
18112 }
18113
18114 /* OpenMP 2.5:
18115    nowait */
18116
18117 static tree
18118 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18119 {
18120   tree c;
18121
18122   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18123
18124   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18125   OMP_CLAUSE_CHAIN (c) = list;
18126   return c;
18127 }
18128
18129 /* OpenMP 2.5:
18130    num_threads ( expression ) */
18131
18132 static tree
18133 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18134 {
18135   tree t, c;
18136
18137   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18138     return list;
18139
18140   t = cp_parser_expression (parser, false);
18141
18142   if (t == error_mark_node
18143       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18144     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18145                                            /*or_comma=*/false,
18146                                            /*consume_paren=*/true);
18147
18148   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18149
18150   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18151   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18152   OMP_CLAUSE_CHAIN (c) = list;
18153
18154   return c;
18155 }
18156
18157 /* OpenMP 2.5:
18158    ordered */
18159
18160 static tree
18161 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18162 {
18163   tree c;
18164
18165   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18166
18167   c = build_omp_clause (OMP_CLAUSE_ORDERED);
18168   OMP_CLAUSE_CHAIN (c) = list;
18169   return c;
18170 }
18171
18172 /* OpenMP 2.5:
18173    reduction ( reduction-operator : variable-list )
18174
18175    reduction-operator:
18176      One of: + * - & ^ | && || */
18177
18178 static tree
18179 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18180 {
18181   enum tree_code code;
18182   tree nlist, c;
18183
18184   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18185     return list;
18186
18187   switch (cp_lexer_peek_token (parser->lexer)->type)
18188     {
18189     case CPP_PLUS:
18190       code = PLUS_EXPR;
18191       break;
18192     case CPP_MULT:
18193       code = MULT_EXPR;
18194       break;
18195     case CPP_MINUS:
18196       code = MINUS_EXPR;
18197       break;
18198     case CPP_AND:
18199       code = BIT_AND_EXPR;
18200       break;
18201     case CPP_XOR:
18202       code = BIT_XOR_EXPR;
18203       break;
18204     case CPP_OR:
18205       code = BIT_IOR_EXPR;
18206       break;
18207     case CPP_AND_AND:
18208       code = TRUTH_ANDIF_EXPR;
18209       break;
18210     case CPP_OR_OR:
18211       code = TRUTH_ORIF_EXPR;
18212       break;
18213     default:
18214       cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18215     resync_fail:
18216       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18217                                              /*or_comma=*/false,
18218                                              /*consume_paren=*/true);
18219       return list;
18220     }
18221   cp_lexer_consume_token (parser->lexer);
18222
18223   if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18224     goto resync_fail;
18225
18226   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18227   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18228     OMP_CLAUSE_REDUCTION_CODE (c) = code;
18229
18230   return nlist;
18231 }
18232
18233 /* OpenMP 2.5:
18234    schedule ( schedule-kind )
18235    schedule ( schedule-kind , expression )
18236
18237    schedule-kind:
18238      static | dynamic | guided | runtime  */
18239
18240 static tree
18241 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18242 {
18243   tree c, t;
18244
18245   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18246     return list;
18247
18248   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18249
18250   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18251     {
18252       tree id = cp_lexer_peek_token (parser->lexer)->value;
18253       const char *p = IDENTIFIER_POINTER (id);
18254
18255       switch (p[0])
18256         {
18257         case 'd':
18258           if (strcmp ("dynamic", p) != 0)
18259             goto invalid_kind;
18260           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18261           break;
18262
18263         case 'g':
18264           if (strcmp ("guided", p) != 0)
18265             goto invalid_kind;
18266           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18267           break;
18268
18269         case 'r':
18270           if (strcmp ("runtime", p) != 0)
18271             goto invalid_kind;
18272           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18273           break;
18274
18275         default:
18276           goto invalid_kind;
18277         }
18278     }
18279   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18280     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18281   else
18282     goto invalid_kind;
18283   cp_lexer_consume_token (parser->lexer);
18284
18285   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18286     {
18287       cp_lexer_consume_token (parser->lexer);
18288
18289       t = cp_parser_assignment_expression (parser, false);
18290
18291       if (t == error_mark_node)
18292         goto resync_fail;
18293       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18294         error ("schedule %<runtime%> does not take "
18295                "a %<chunk_size%> parameter");
18296       else
18297         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18298
18299       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18300         goto resync_fail;
18301     }
18302   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18303     goto resync_fail;
18304
18305   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18306   OMP_CLAUSE_CHAIN (c) = list;
18307   return c;
18308
18309  invalid_kind:
18310   cp_parser_error (parser, "invalid schedule kind");
18311  resync_fail:
18312   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18313                                          /*or_comma=*/false,
18314                                          /*consume_paren=*/true);
18315   return list;
18316 }
18317
18318 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
18319    is a bitmask in MASK.  Return the list of clauses found; the result
18320    of clause default goes in *pdefault.  */
18321
18322 static tree
18323 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18324                            const char *where, cp_token *pragma_tok)
18325 {
18326   tree clauses = NULL;
18327
18328   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18329     {
18330       pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18331       const char *c_name;
18332       tree prev = clauses;
18333
18334       switch (c_kind)
18335         {
18336         case PRAGMA_OMP_CLAUSE_COPYIN:
18337           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18338           c_name = "copyin";
18339           break;
18340         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18341           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18342                                             clauses);
18343           c_name = "copyprivate";
18344           break;
18345         case PRAGMA_OMP_CLAUSE_DEFAULT:
18346           clauses = cp_parser_omp_clause_default (parser, clauses);
18347           c_name = "default";
18348           break;
18349         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18350           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18351                                             clauses);
18352           c_name = "firstprivate";
18353           break;
18354         case PRAGMA_OMP_CLAUSE_IF:
18355           clauses = cp_parser_omp_clause_if (parser, clauses);
18356           c_name = "if";
18357           break;
18358         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18359           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18360                                             clauses);
18361           c_name = "lastprivate";
18362           break;
18363         case PRAGMA_OMP_CLAUSE_NOWAIT:
18364           clauses = cp_parser_omp_clause_nowait (parser, clauses);
18365           c_name = "nowait";
18366           break;
18367         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18368           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18369           c_name = "num_threads";
18370           break;
18371         case PRAGMA_OMP_CLAUSE_ORDERED:
18372           clauses = cp_parser_omp_clause_ordered (parser, clauses);
18373           c_name = "ordered";
18374           break;
18375         case PRAGMA_OMP_CLAUSE_PRIVATE:
18376           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18377                                             clauses);
18378           c_name = "private";
18379           break;
18380         case PRAGMA_OMP_CLAUSE_REDUCTION:
18381           clauses = cp_parser_omp_clause_reduction (parser, clauses);
18382           c_name = "reduction";
18383           break;
18384         case PRAGMA_OMP_CLAUSE_SCHEDULE:
18385           clauses = cp_parser_omp_clause_schedule (parser, clauses);
18386           c_name = "schedule";
18387           break;
18388         case PRAGMA_OMP_CLAUSE_SHARED:
18389           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18390                                             clauses);
18391           c_name = "shared";
18392           break;
18393         default:
18394           cp_parser_error (parser, "expected %<#pragma omp%> clause");
18395           goto saw_error;
18396         }
18397
18398       if (((mask >> c_kind) & 1) == 0)
18399         {
18400           /* Remove the invalid clause(s) from the list to avoid
18401              confusing the rest of the compiler.  */
18402           clauses = prev;
18403           error ("%qs is not valid for %qs", c_name, where);
18404         }
18405     }
18406  saw_error:
18407   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18408   return finish_omp_clauses (clauses);
18409 }
18410
18411 /* OpenMP 2.5:
18412    structured-block:
18413      statement
18414
18415    In practice, we're also interested in adding the statement to an
18416    outer node.  So it is convenient if we work around the fact that
18417    cp_parser_statement calls add_stmt.  */
18418
18419 static unsigned
18420 cp_parser_begin_omp_structured_block (cp_parser *parser)
18421 {
18422   unsigned save = parser->in_statement;
18423
18424   /* Only move the values to IN_OMP_BLOCK if they weren't false.
18425      This preserves the "not within loop or switch" style error messages
18426      for nonsense cases like
18427         void foo() {
18428         #pragma omp single
18429           break;
18430         }
18431   */
18432   if (parser->in_statement)
18433     parser->in_statement = IN_OMP_BLOCK;
18434
18435   return save;
18436 }
18437
18438 static void
18439 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18440 {
18441   parser->in_statement = save;
18442 }
18443
18444 static tree
18445 cp_parser_omp_structured_block (cp_parser *parser)
18446 {
18447   tree stmt = begin_omp_structured_block ();
18448   unsigned int save = cp_parser_begin_omp_structured_block (parser);
18449
18450   cp_parser_statement (parser, NULL_TREE, false);
18451
18452   cp_parser_end_omp_structured_block (parser, save);
18453   return finish_omp_structured_block (stmt);
18454 }
18455
18456 /* OpenMP 2.5:
18457    # pragma omp atomic new-line
18458      expression-stmt
18459
18460    expression-stmt:
18461      x binop= expr | x++ | ++x | x-- | --x
18462    binop:
18463      +, *, -, /, &, ^, |, <<, >>
18464
18465   where x is an lvalue expression with scalar type.  */
18466
18467 static void
18468 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18469 {
18470   tree lhs, rhs;
18471   enum tree_code code;
18472
18473   cp_parser_require_pragma_eol (parser, pragma_tok);
18474
18475   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18476                                     /*cast_p=*/false);
18477   switch (TREE_CODE (lhs))
18478     {
18479     case ERROR_MARK:
18480       goto saw_error;
18481
18482     case PREINCREMENT_EXPR:
18483     case POSTINCREMENT_EXPR:
18484       lhs = TREE_OPERAND (lhs, 0);
18485       code = PLUS_EXPR;
18486       rhs = integer_one_node;
18487       break;
18488
18489     case PREDECREMENT_EXPR:
18490     case POSTDECREMENT_EXPR:
18491       lhs = TREE_OPERAND (lhs, 0);
18492       code = MINUS_EXPR;
18493       rhs = integer_one_node;
18494       break;
18495
18496     default:
18497       switch (cp_lexer_peek_token (parser->lexer)->type)
18498         {
18499         case CPP_MULT_EQ:
18500           code = MULT_EXPR;
18501           break;
18502         case CPP_DIV_EQ:
18503           code = TRUNC_DIV_EXPR;
18504           break;
18505         case CPP_PLUS_EQ:
18506           code = PLUS_EXPR;
18507           break;
18508         case CPP_MINUS_EQ:
18509           code = MINUS_EXPR;
18510           break;
18511         case CPP_LSHIFT_EQ:
18512           code = LSHIFT_EXPR;
18513           break;
18514         case CPP_RSHIFT_EQ:
18515           code = RSHIFT_EXPR;
18516           break;
18517         case CPP_AND_EQ:
18518           code = BIT_AND_EXPR;
18519           break;
18520         case CPP_OR_EQ:
18521           code = BIT_IOR_EXPR;
18522           break;
18523         case CPP_XOR_EQ:
18524           code = BIT_XOR_EXPR;
18525           break;
18526         default:
18527           cp_parser_error (parser,
18528                            "invalid operator for %<#pragma omp atomic%>");
18529           goto saw_error;
18530         }
18531       cp_lexer_consume_token (parser->lexer);
18532
18533       rhs = cp_parser_expression (parser, false);
18534       if (rhs == error_mark_node)
18535         goto saw_error;
18536       break;
18537     }
18538   finish_omp_atomic (code, lhs, rhs);
18539   cp_parser_consume_semicolon_at_end_of_statement (parser);
18540   return;
18541
18542  saw_error:
18543   cp_parser_skip_to_end_of_block_or_statement (parser);
18544 }
18545
18546
18547 /* OpenMP 2.5:
18548    # pragma omp barrier new-line  */
18549
18550 static void
18551 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18552 {
18553   cp_parser_require_pragma_eol (parser, pragma_tok);
18554   finish_omp_barrier ();
18555 }
18556
18557 /* OpenMP 2.5:
18558    # pragma omp critical [(name)] new-line
18559      structured-block  */
18560
18561 static tree
18562 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18563 {
18564   tree stmt, name = NULL;
18565
18566   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18567     {
18568       cp_lexer_consume_token (parser->lexer);
18569
18570       name = cp_parser_identifier (parser);
18571
18572       if (name == error_mark_node
18573           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18574         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18575                                                /*or_comma=*/false,
18576                                                /*consume_paren=*/true);
18577       if (name == error_mark_node)
18578         name = NULL;
18579     }
18580   cp_parser_require_pragma_eol (parser, pragma_tok);
18581
18582   stmt = cp_parser_omp_structured_block (parser);
18583   return c_finish_omp_critical (stmt, name);
18584 }
18585
18586 /* OpenMP 2.5:
18587    # pragma omp flush flush-vars[opt] new-line
18588
18589    flush-vars:
18590      ( variable-list ) */
18591
18592 static void
18593 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18594 {
18595   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18596     (void) cp_parser_omp_var_list (parser, 0, NULL);
18597   cp_parser_require_pragma_eol (parser, pragma_tok);
18598
18599   finish_omp_flush ();
18600 }
18601
18602 /* Parse the restricted form of the for statment allowed by OpenMP.  */
18603
18604 static tree
18605 cp_parser_omp_for_loop (cp_parser *parser)
18606 {
18607   tree init, cond, incr, body, decl, pre_body;
18608   location_t loc;
18609
18610   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18611     {
18612       cp_parser_error (parser, "for statement expected");
18613       return NULL;
18614     }
18615   loc = cp_lexer_consume_token (parser->lexer)->location;
18616   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18617     return NULL;
18618
18619   init = decl = NULL;
18620   pre_body = push_stmt_list ();
18621   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18622     {
18623       cp_decl_specifier_seq type_specifiers;
18624
18625       /* First, try to parse as an initialized declaration.  See
18626          cp_parser_condition, from whence the bulk of this is copied.  */
18627
18628       cp_parser_parse_tentatively (parser);
18629       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18630                                     &type_specifiers);
18631       if (!cp_parser_error_occurred (parser))
18632         {
18633           tree asm_specification, attributes;
18634           cp_declarator *declarator;
18635
18636           declarator = cp_parser_declarator (parser,
18637                                              CP_PARSER_DECLARATOR_NAMED,
18638                                              /*ctor_dtor_or_conv_p=*/NULL,
18639                                              /*parenthesized_p=*/NULL,
18640                                              /*member_p=*/false);
18641           attributes = cp_parser_attributes_opt (parser);
18642           asm_specification = cp_parser_asm_specification_opt (parser);
18643
18644           cp_parser_require (parser, CPP_EQ, "`='");
18645           if (cp_parser_parse_definitely (parser))
18646             {
18647               tree pushed_scope;
18648
18649               decl = start_decl (declarator, &type_specifiers,
18650                                  /*initialized_p=*/false, attributes,
18651                                  /*prefix_attributes=*/NULL_TREE,
18652                                  &pushed_scope);
18653
18654               init = cp_parser_assignment_expression (parser, false);
18655
18656               cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18657                               asm_specification, LOOKUP_ONLYCONVERTING);
18658
18659               if (pushed_scope)
18660                 pop_scope (pushed_scope);
18661             }
18662         }
18663       else
18664         cp_parser_abort_tentative_parse (parser);
18665
18666       /* If parsing as an initialized declaration failed, try again as
18667          a simple expression.  */
18668       if (decl == NULL)
18669         init = cp_parser_expression (parser, false);
18670     }
18671   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18672   pre_body = pop_stmt_list (pre_body);
18673
18674   cond = NULL;
18675   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18676     cond = cp_parser_condition (parser);
18677   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18678
18679   incr = NULL;
18680   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18681     incr = cp_parser_expression (parser, false);
18682
18683   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18684     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18685                                            /*or_comma=*/false,
18686                                            /*consume_paren=*/true);
18687
18688   /* Note that we saved the original contents of this flag when we entered
18689      the structured block, and so we don't need to re-save it here.  */
18690   parser->in_statement = IN_OMP_FOR;
18691
18692   /* Note that the grammar doesn't call for a structured block here,
18693      though the loop as a whole is a structured block.  */
18694   body = push_stmt_list ();
18695   cp_parser_statement (parser, NULL_TREE, false);
18696   body = pop_stmt_list (body);
18697
18698   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18699 }
18700
18701 /* OpenMP 2.5:
18702    #pragma omp for for-clause[optseq] new-line
18703      for-loop  */
18704
18705 #define OMP_FOR_CLAUSE_MASK                             \
18706         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18707         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18708         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
18709         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
18710         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
18711         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
18712         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18713
18714 static tree
18715 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18716 {
18717   tree clauses, sb, ret;
18718   unsigned int save;
18719
18720   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18721                                        "#pragma omp for", pragma_tok);
18722
18723   sb = begin_omp_structured_block ();
18724   save = cp_parser_begin_omp_structured_block (parser);
18725
18726   ret = cp_parser_omp_for_loop (parser);
18727   if (ret)
18728     OMP_FOR_CLAUSES (ret) = clauses;
18729
18730   cp_parser_end_omp_structured_block (parser, save);
18731   add_stmt (finish_omp_structured_block (sb));
18732
18733   return ret;
18734 }
18735
18736 /* OpenMP 2.5:
18737    # pragma omp master new-line
18738      structured-block  */
18739
18740 static tree
18741 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18742 {
18743   cp_parser_require_pragma_eol (parser, pragma_tok);
18744   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18745 }
18746
18747 /* OpenMP 2.5:
18748    # pragma omp ordered new-line
18749      structured-block  */
18750
18751 static tree
18752 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18753 {
18754   cp_parser_require_pragma_eol (parser, pragma_tok);
18755   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18756 }
18757
18758 /* OpenMP 2.5:
18759
18760    section-scope:
18761      { section-sequence }
18762
18763    section-sequence:
18764      section-directive[opt] structured-block
18765      section-sequence section-directive structured-block  */
18766
18767 static tree
18768 cp_parser_omp_sections_scope (cp_parser *parser)
18769 {
18770   tree stmt, substmt;
18771   bool error_suppress = false;
18772   cp_token *tok;
18773
18774   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18775     return NULL_TREE;
18776
18777   stmt = push_stmt_list ();
18778
18779   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18780     {
18781       unsigned save;
18782
18783       substmt = begin_omp_structured_block ();
18784       save = cp_parser_begin_omp_structured_block (parser);
18785
18786       while (1)
18787         {
18788           cp_parser_statement (parser, NULL_TREE, false);
18789
18790           tok = cp_lexer_peek_token (parser->lexer);
18791           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18792             break;
18793           if (tok->type == CPP_CLOSE_BRACE)
18794             break;
18795           if (tok->type == CPP_EOF)
18796             break;
18797         }
18798
18799       cp_parser_end_omp_structured_block (parser, save);
18800       substmt = finish_omp_structured_block (substmt);
18801       substmt = build1 (OMP_SECTION, void_type_node, substmt);
18802       add_stmt (substmt);
18803     }
18804
18805   while (1)
18806     {
18807       tok = cp_lexer_peek_token (parser->lexer);
18808       if (tok->type == CPP_CLOSE_BRACE)
18809         break;
18810       if (tok->type == CPP_EOF)
18811         break;
18812
18813       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18814         {
18815           cp_lexer_consume_token (parser->lexer);
18816           cp_parser_require_pragma_eol (parser, tok);
18817           error_suppress = false;
18818         }
18819       else if (!error_suppress)
18820         {
18821           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
18822           error_suppress = true;
18823         }
18824
18825       substmt = cp_parser_omp_structured_block (parser);
18826       substmt = build1 (OMP_SECTION, void_type_node, substmt);
18827       add_stmt (substmt);
18828     }
18829   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
18830
18831   substmt = pop_stmt_list (stmt);
18832
18833   stmt = make_node (OMP_SECTIONS);
18834   TREE_TYPE (stmt) = void_type_node;
18835   OMP_SECTIONS_BODY (stmt) = substmt;
18836
18837   add_stmt (stmt);
18838   return stmt;
18839 }
18840
18841 /* OpenMP 2.5:
18842    # pragma omp sections sections-clause[optseq] newline
18843      sections-scope  */
18844
18845 #define OMP_SECTIONS_CLAUSE_MASK                        \
18846         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18847         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18848         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
18849         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
18850         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18851
18852 static tree
18853 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
18854 {
18855   tree clauses, ret;
18856
18857   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
18858                                        "#pragma omp sections", pragma_tok);
18859
18860   ret = cp_parser_omp_sections_scope (parser);
18861   if (ret)
18862     OMP_SECTIONS_CLAUSES (ret) = clauses;
18863
18864   return ret;
18865 }
18866
18867 /* OpenMP 2.5:
18868    # pragma parallel parallel-clause new-line
18869    # pragma parallel for parallel-for-clause new-line
18870    # pragma parallel sections parallel-sections-clause new-line  */
18871
18872 #define OMP_PARALLEL_CLAUSE_MASK                        \
18873         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
18874         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18875         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18876         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
18877         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
18878         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
18879         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
18880         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18881
18882 static tree
18883 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
18884 {
18885   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
18886   const char *p_name = "#pragma omp parallel";
18887   tree stmt, clauses, par_clause, ws_clause, block;
18888   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
18889   unsigned int save;
18890
18891   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18892     {
18893       cp_lexer_consume_token (parser->lexer);
18894       p_kind = PRAGMA_OMP_PARALLEL_FOR;
18895       p_name = "#pragma omp parallel for";
18896       mask |= OMP_FOR_CLAUSE_MASK;
18897       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18898     }
18899   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18900     {
18901       tree id = cp_lexer_peek_token (parser->lexer)->value;
18902       const char *p = IDENTIFIER_POINTER (id);
18903       if (strcmp (p, "sections") == 0)
18904         {
18905           cp_lexer_consume_token (parser->lexer);
18906           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
18907           p_name = "#pragma omp parallel sections";
18908           mask |= OMP_SECTIONS_CLAUSE_MASK;
18909           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18910         }
18911     }
18912
18913   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
18914   block = begin_omp_parallel ();
18915   save = cp_parser_begin_omp_structured_block (parser);
18916
18917   switch (p_kind)
18918     {
18919     case PRAGMA_OMP_PARALLEL:
18920       cp_parser_already_scoped_statement (parser);
18921       par_clause = clauses;
18922       break;
18923
18924     case PRAGMA_OMP_PARALLEL_FOR:
18925       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18926       stmt = cp_parser_omp_for_loop (parser);
18927       if (stmt)
18928         OMP_FOR_CLAUSES (stmt) = ws_clause;
18929       break;
18930
18931     case PRAGMA_OMP_PARALLEL_SECTIONS:
18932       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18933       stmt = cp_parser_omp_sections_scope (parser);
18934       if (stmt)
18935         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
18936       break;
18937
18938     default:
18939       gcc_unreachable ();
18940     }
18941
18942   cp_parser_end_omp_structured_block (parser, save);
18943   stmt = finish_omp_parallel (par_clause, block);
18944   if (p_kind != PRAGMA_OMP_PARALLEL)
18945     OMP_PARALLEL_COMBINED (stmt) = 1;
18946   return stmt;
18947 }
18948
18949 /* OpenMP 2.5:
18950    # pragma omp single single-clause[optseq] new-line
18951      structured-block  */
18952
18953 #define OMP_SINGLE_CLAUSE_MASK                          \
18954         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18955         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18956         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
18957         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18958
18959 static tree
18960 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
18961 {
18962   tree stmt = make_node (OMP_SINGLE);
18963   TREE_TYPE (stmt) = void_type_node;
18964
18965   OMP_SINGLE_CLAUSES (stmt)
18966     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18967                                  "#pragma omp single", pragma_tok);
18968   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
18969
18970   return add_stmt (stmt);
18971 }
18972
18973 /* OpenMP 2.5:
18974    # pragma omp threadprivate (variable-list) */
18975
18976 static void
18977 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
18978 {
18979   tree vars;
18980
18981   vars = cp_parser_omp_var_list (parser, 0, NULL);
18982   cp_parser_require_pragma_eol (parser, pragma_tok);
18983
18984   if (!targetm.have_tls)
18985     sorry ("threadprivate variables not supported in this target");
18986
18987   finish_omp_threadprivate (vars);
18988 }
18989
18990 /* Main entry point to OpenMP statement pragmas.  */
18991
18992 static void
18993 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
18994 {
18995   tree stmt;
18996
18997   switch (pragma_tok->pragma_kind)
18998     {
18999     case PRAGMA_OMP_ATOMIC:
19000       cp_parser_omp_atomic (parser, pragma_tok);
19001       return;
19002     case PRAGMA_OMP_CRITICAL:
19003       stmt = cp_parser_omp_critical (parser, pragma_tok);
19004       break;
19005     case PRAGMA_OMP_FOR:
19006       stmt = cp_parser_omp_for (parser, pragma_tok);
19007       break;
19008     case PRAGMA_OMP_MASTER:
19009       stmt = cp_parser_omp_master (parser, pragma_tok);
19010       break;
19011     case PRAGMA_OMP_ORDERED:
19012       stmt = cp_parser_omp_ordered (parser, pragma_tok);
19013       break;
19014     case PRAGMA_OMP_PARALLEL:
19015       stmt = cp_parser_omp_parallel (parser, pragma_tok);
19016       break;
19017     case PRAGMA_OMP_SECTIONS:
19018       stmt = cp_parser_omp_sections (parser, pragma_tok);
19019       break;
19020     case PRAGMA_OMP_SINGLE:
19021       stmt = cp_parser_omp_single (parser, pragma_tok);
19022       break;
19023     default:
19024       gcc_unreachable ();
19025     }
19026
19027   if (stmt)
19028     SET_EXPR_LOCATION (stmt, pragma_tok->location);
19029 }
19030 \f
19031 /* The parser.  */
19032
19033 static GTY (()) cp_parser *the_parser;
19034
19035 \f
19036 /* Special handling for the first token or line in the file.  The first
19037    thing in the file might be #pragma GCC pch_preprocess, which loads a
19038    PCH file, which is a GC collection point.  So we need to handle this
19039    first pragma without benefit of an existing lexer structure.
19040
19041    Always returns one token to the caller in *FIRST_TOKEN.  This is
19042    either the true first token of the file, or the first token after
19043    the initial pragma.  */
19044
19045 static void
19046 cp_parser_initial_pragma (cp_token *first_token)
19047 {
19048   tree name = NULL;
19049
19050   cp_lexer_get_preprocessor_token (NULL, first_token);
19051   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19052     return;
19053
19054   cp_lexer_get_preprocessor_token (NULL, first_token);
19055   if (first_token->type == CPP_STRING)
19056     {
19057       name = first_token->value;
19058
19059       cp_lexer_get_preprocessor_token (NULL, first_token);
19060       if (first_token->type != CPP_PRAGMA_EOL)
19061         error ("junk at end of %<#pragma GCC pch_preprocess%>");
19062     }
19063   else
19064     error ("expected string literal");
19065
19066   /* Skip to the end of the pragma.  */
19067   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19068     cp_lexer_get_preprocessor_token (NULL, first_token);
19069
19070   /* Now actually load the PCH file.  */
19071   if (name)
19072     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19073
19074   /* Read one more token to return to our caller.  We have to do this
19075      after reading the PCH file in, since its pointers have to be
19076      live.  */
19077   cp_lexer_get_preprocessor_token (NULL, first_token);
19078 }
19079
19080 /* Normal parsing of a pragma token.  Here we can (and must) use the
19081    regular lexer.  */
19082
19083 static bool
19084 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19085 {
19086   cp_token *pragma_tok;
19087   unsigned int id;
19088
19089   pragma_tok = cp_lexer_consume_token (parser->lexer);
19090   gcc_assert (pragma_tok->type == CPP_PRAGMA);
19091   parser->lexer->in_pragma = true;
19092
19093   id = pragma_tok->pragma_kind;
19094   switch (id)
19095     {
19096     case PRAGMA_GCC_PCH_PREPROCESS:
19097       error ("%<#pragma GCC pch_preprocess%> must be first");
19098       break;
19099
19100     case PRAGMA_OMP_BARRIER:
19101       switch (context)
19102         {
19103         case pragma_compound:
19104           cp_parser_omp_barrier (parser, pragma_tok);
19105           return false;
19106         case pragma_stmt:
19107           error ("%<#pragma omp barrier%> may only be "
19108                  "used in compound statements");
19109           break;
19110         default:
19111           goto bad_stmt;
19112         }
19113       break;
19114
19115     case PRAGMA_OMP_FLUSH:
19116       switch (context)
19117         {
19118         case pragma_compound:
19119           cp_parser_omp_flush (parser, pragma_tok);
19120           return false;
19121         case pragma_stmt:
19122           error ("%<#pragma omp flush%> may only be "
19123                  "used in compound statements");
19124           break;
19125         default:
19126           goto bad_stmt;
19127         }
19128       break;
19129
19130     case PRAGMA_OMP_THREADPRIVATE:
19131       cp_parser_omp_threadprivate (parser, pragma_tok);
19132       return false;
19133
19134     case PRAGMA_OMP_ATOMIC:
19135     case PRAGMA_OMP_CRITICAL:
19136     case PRAGMA_OMP_FOR:
19137     case PRAGMA_OMP_MASTER:
19138     case PRAGMA_OMP_ORDERED:
19139     case PRAGMA_OMP_PARALLEL:
19140     case PRAGMA_OMP_SECTIONS:
19141     case PRAGMA_OMP_SINGLE:
19142       if (context == pragma_external)
19143         goto bad_stmt;
19144       cp_parser_omp_construct (parser, pragma_tok);
19145       return true;
19146
19147     case PRAGMA_OMP_SECTION:
19148       error ("%<#pragma omp section%> may only be used in "
19149              "%<#pragma omp sections%> construct");
19150       break;
19151
19152     default:
19153       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19154       c_invoke_pragma_handler (id);
19155       break;
19156
19157     bad_stmt:
19158       cp_parser_error (parser, "expected declaration specifiers");
19159       break;
19160     }
19161
19162   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19163   return false;
19164 }
19165
19166 /* The interface the pragma parsers have to the lexer.  */
19167
19168 enum cpp_ttype
19169 pragma_lex (tree *value)
19170 {
19171   cp_token *tok;
19172   enum cpp_ttype ret;
19173
19174   tok = cp_lexer_peek_token (the_parser->lexer);
19175
19176   ret = tok->type;
19177   *value = tok->value;
19178
19179   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19180     ret = CPP_EOF;
19181   else if (ret == CPP_STRING)
19182     *value = cp_parser_string_literal (the_parser, false, false);
19183   else
19184     {
19185       cp_lexer_consume_token (the_parser->lexer);
19186       if (ret == CPP_KEYWORD)
19187         ret = CPP_NAME;
19188     }
19189
19190   return ret;
19191 }
19192
19193 \f
19194 /* External interface.  */
19195
19196 /* Parse one entire translation unit.  */
19197
19198 void
19199 c_parse_file (void)
19200 {
19201   bool error_occurred;
19202   static bool already_called = false;
19203
19204   if (already_called)
19205     {
19206       sorry ("inter-module optimizations not implemented for C++");
19207       return;
19208     }
19209   already_called = true;
19210
19211   the_parser = cp_parser_new ();
19212   push_deferring_access_checks (flag_access_control
19213                                 ? dk_no_deferred : dk_no_check);
19214   error_occurred = cp_parser_translation_unit (the_parser);
19215   the_parser = NULL;
19216 }
19217
19218 /* This variable must be provided by every front end.  */
19219
19220 int yydebug;
19221
19222 #include "gt-cp-parser.h"