8a1137290e7dbd7f2db3a85dcd289c65a8046eea
[gcc.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, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, 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
40 \f
41 /* The lexer. */
42
43 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
44 and c-lex.c) and the C++ parser. */
45
46 /* A C++ token. */
47
48 typedef struct cp_token GTY (())
49 {
50 /* The kind of token. */
51 ENUM_BITFIELD (cpp_ttype) type : 8;
52 /* If this token is a keyword, this value indicates which keyword.
53 Otherwise, this value is RID_MAX. */
54 ENUM_BITFIELD (rid) keyword : 8;
55 /* Token flags. */
56 unsigned char flags;
57 /* True if this token is from a system header. */
58 BOOL_BITFIELD in_system_header : 1;
59 /* True if this token is from a context where it is implicitly extern "C" */
60 BOOL_BITFIELD implicit_extern_c : 1;
61 /* The value associated with this token, if any. */
62 tree value;
63 /* The location at which this token was found. */
64 location_t location;
65 } cp_token;
66
67 /* We use a stack of token pointer for saving token sets. */
68 typedef struct cp_token *cp_token_position;
69 DEF_VEC_MALLOC_P (cp_token_position);
70
71 static const cp_token eof_token =
72 {
73 CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
74 #if USE_MAPPED_LOCATION
75 0
76 #else
77 {0, 0}
78 #endif
79 };
80
81 /* The cp_lexer structure represents the C++ lexer. It is responsible
82 for managing the token stream from the preprocessor and supplying
83 it to the parser. Tokens are never added to the cp_lexer after
84 it is created. */
85
86 typedef struct cp_lexer GTY (())
87 {
88 /* The memory allocated for the buffer. NULL if this lexer does not
89 own the token buffer. */
90 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
91 /* If the lexer owns the buffer, this is the number of tokens in the
92 buffer. */
93 size_t buffer_length;
94
95 /* A pointer just past the last available token. The tokens
96 in this lexer are [buffer, last_token). */
97 cp_token_position GTY ((skip)) last_token;
98
99 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
100 no more available tokens. */
101 cp_token_position GTY ((skip)) next_token;
102
103 /* A stack indicating positions at which cp_lexer_save_tokens was
104 called. The top entry is the most recent position at which we
105 began saving tokens. If the stack is non-empty, we are saving
106 tokens. */
107 VEC (cp_token_position) *GTY ((skip)) saved_tokens;
108
109 /* True if we should output debugging information. */
110 bool debugging_p;
111
112 /* The next lexer in a linked list of lexers. */
113 struct cp_lexer *next;
114 } cp_lexer;
115
116 /* cp_token_cache is a range of tokens. There is no need to represent
117 allocate heap memory for it, since tokens are never removed from the
118 lexer's array. There is also no need for the GC to walk through
119 a cp_token_cache, since everything in here is referenced through
120 a lexer. */
121
122 typedef struct cp_token_cache GTY(())
123 {
124 /* The beginning of the token range. */
125 cp_token * GTY((skip)) first;
126
127 /* Points immediately after the last token in the range. */
128 cp_token * GTY ((skip)) last;
129 } cp_token_cache;
130
131 /* Prototypes. */
132
133 static cp_lexer *cp_lexer_new_main
134 (void);
135 static cp_lexer *cp_lexer_new_from_tokens
136 (cp_token_cache *tokens);
137 static void cp_lexer_destroy
138 (cp_lexer *);
139 static int cp_lexer_saving_tokens
140 (const cp_lexer *);
141 static cp_token_position cp_lexer_token_position
142 (cp_lexer *, bool);
143 static cp_token *cp_lexer_token_at
144 (cp_lexer *, cp_token_position);
145 static void cp_lexer_get_preprocessor_token
146 (cp_lexer *, cp_token *);
147 static inline cp_token *cp_lexer_peek_token
148 (cp_lexer *);
149 static cp_token *cp_lexer_peek_nth_token
150 (cp_lexer *, size_t);
151 static inline bool cp_lexer_next_token_is
152 (cp_lexer *, enum cpp_ttype);
153 static bool cp_lexer_next_token_is_not
154 (cp_lexer *, enum cpp_ttype);
155 static bool cp_lexer_next_token_is_keyword
156 (cp_lexer *, enum rid);
157 static cp_token *cp_lexer_consume_token
158 (cp_lexer *);
159 static void cp_lexer_purge_token
160 (cp_lexer *);
161 static void cp_lexer_purge_tokens_after
162 (cp_lexer *, cp_token_position);
163 static void cp_lexer_handle_pragma
164 (cp_lexer *);
165 static void cp_lexer_save_tokens
166 (cp_lexer *);
167 static void cp_lexer_commit_tokens
168 (cp_lexer *);
169 static void cp_lexer_rollback_tokens
170 (cp_lexer *);
171 #ifdef ENABLE_CHECKING
172 static void cp_lexer_print_token
173 (FILE *, cp_token *);
174 static inline bool cp_lexer_debugging_p
175 (cp_lexer *);
176 static void cp_lexer_start_debugging
177 (cp_lexer *) ATTRIBUTE_UNUSED;
178 static void cp_lexer_stop_debugging
179 (cp_lexer *) ATTRIBUTE_UNUSED;
180 #else
181 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
182 about passing NULL to functions that require non-NULL arguments
183 (fputs, fprintf). It will never be used, so all we need is a value
184 of the right type that's guaranteed not to be NULL. */
185 #define cp_lexer_debug_stream stdout
186 #define cp_lexer_print_token(str, tok) (void) 0
187 #define cp_lexer_debugging_p(lexer) 0
188 #endif /* ENABLE_CHECKING */
189
190 static cp_token_cache *cp_token_cache_new
191 (cp_token *, cp_token *);
192
193 /* Manifest constants. */
194 #define CP_LEXER_BUFFER_SIZE 10000
195 #define CP_SAVED_TOKEN_STACK 5
196
197 /* A token type for keywords, as opposed to ordinary identifiers. */
198 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
199
200 /* A token type for template-ids. If a template-id is processed while
201 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
202 the value of the CPP_TEMPLATE_ID is whatever was returned by
203 cp_parser_template_id. */
204 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
205
206 /* A token type for nested-name-specifiers. If a
207 nested-name-specifier is processed while parsing tentatively, it is
208 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
209 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
210 cp_parser_nested_name_specifier_opt. */
211 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
212
213 /* A token type for tokens that are not tokens at all; these are used
214 to represent slots in the array where there used to be a token
215 that has now been deleted. */
216 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
217
218 /* The number of token types, including C++-specific ones. */
219 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
220
221 /* Variables. */
222
223 #ifdef ENABLE_CHECKING
224 /* The stream to which debugging output should be written. */
225 static FILE *cp_lexer_debug_stream;
226 #endif /* ENABLE_CHECKING */
227
228 /* Create a new main C++ lexer, the lexer that gets tokens from the
229 preprocessor. */
230
231 static cp_lexer *
232 cp_lexer_new_main (void)
233 {
234 cp_token first_token;
235 cp_lexer *lexer;
236 cp_token *pos;
237 size_t alloc;
238 size_t space;
239 cp_token *buffer;
240
241 /* It's possible that lexing the first token will load a PCH file,
242 which is a GC collection point. So we have to grab the first
243 token before allocating any memory. Pragmas must not be deferred
244 as -fpch-preprocess can generate a pragma to load the PCH file in
245 the preprocessed output used by -save-temps. */
246 cp_lexer_get_preprocessor_token (NULL, &first_token);
247
248 /* Tell cpplib we want CPP_PRAGMA tokens. */
249 cpp_get_options (parse_in)->defer_pragmas = true;
250
251 /* Tell c_lex not to merge string constants. */
252 c_lex_return_raw_strings = true;
253
254 c_common_no_more_pch ();
255
256 /* Allocate the memory. */
257 lexer = GGC_CNEW (cp_lexer);
258
259 #ifdef ENABLE_CHECKING
260 /* Initially we are not debugging. */
261 lexer->debugging_p = false;
262 #endif /* ENABLE_CHECKING */
263 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
264
265 /* Create the buffer. */
266 alloc = CP_LEXER_BUFFER_SIZE;
267 buffer = ggc_alloc (alloc * sizeof (cp_token));
268
269 /* Put the first token in the buffer. */
270 space = alloc;
271 pos = buffer;
272 *pos = first_token;
273
274 /* Get the remaining tokens from the preprocessor. */
275 while (pos->type != CPP_EOF)
276 {
277 pos++;
278 if (!--space)
279 {
280 space = alloc;
281 alloc *= 2;
282 buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
283 pos = buffer + space;
284 }
285 cp_lexer_get_preprocessor_token (lexer, pos);
286 }
287 lexer->buffer = buffer;
288 lexer->buffer_length = alloc - space;
289 lexer->last_token = pos;
290 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
291
292 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
293 direct calls to c_lex. Those callers all expect c_lex to do
294 string constant concatenation. */
295 c_lex_return_raw_strings = false;
296
297 gcc_assert (lexer->next_token->type != CPP_PURGED);
298 return lexer;
299 }
300
301 /* Create a new lexer whose token stream is primed with the tokens in
302 CACHE. When these tokens are exhausted, no new tokens will be read. */
303
304 static cp_lexer *
305 cp_lexer_new_from_tokens (cp_token_cache *cache)
306 {
307 cp_token *first = cache->first;
308 cp_token *last = cache->last;
309 cp_lexer *lexer = GGC_CNEW (cp_lexer);
310
311 /* We do not own the buffer. */
312 lexer->buffer = NULL;
313 lexer->buffer_length = 0;
314 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
315 lexer->last_token = last;
316
317 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
318
319 #ifdef ENABLE_CHECKING
320 /* Initially we are not debugging. */
321 lexer->debugging_p = false;
322 #endif
323
324 gcc_assert (lexer->next_token->type != CPP_PURGED);
325 return lexer;
326 }
327
328 /* Frees all resources associated with LEXER. */
329
330 static void
331 cp_lexer_destroy (cp_lexer *lexer)
332 {
333 if (lexer->buffer)
334 ggc_free (lexer->buffer);
335 VEC_free (cp_token_position, lexer->saved_tokens);
336 ggc_free (lexer);
337 }
338
339 /* Returns nonzero if debugging information should be output. */
340
341 #ifdef ENABLE_CHECKING
342
343 static inline bool
344 cp_lexer_debugging_p (cp_lexer *lexer)
345 {
346 return lexer->debugging_p;
347 }
348
349 #endif /* ENABLE_CHECKING */
350
351 static inline cp_token_position
352 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
353 {
354 gcc_assert (!previous_p || lexer->next_token != &eof_token);
355
356 return lexer->next_token - previous_p;
357 }
358
359 static inline cp_token *
360 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
361 {
362 return pos;
363 }
364
365 /* nonzero if we are presently saving tokens. */
366
367 static inline int
368 cp_lexer_saving_tokens (const cp_lexer* lexer)
369 {
370 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
371 }
372
373 /* Store the next token from the preprocessor in *TOKEN. Return true
374 if we reach EOF. */
375
376 static void
377 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
378 cp_token *token)
379 {
380 static int is_extern_c = 0;
381
382 /* Get a new token from the preprocessor. */
383 token->type
384 = c_lex_with_flags (&token->value, &token->location, &token->flags);
385 token->in_system_header = in_system_header;
386
387 /* On some systems, some header files are surrounded by an
388 implicit extern "C" block. Set a flag in the token if it
389 comes from such a header. */
390 is_extern_c += pending_lang_change;
391 pending_lang_change = 0;
392 token->implicit_extern_c = is_extern_c > 0;
393
394 /* Check to see if this token is a keyword. */
395 if (token->type == CPP_NAME
396 && C_IS_RESERVED_WORD (token->value))
397 {
398 /* Mark this token as a keyword. */
399 token->type = CPP_KEYWORD;
400 /* Record which keyword. */
401 token->keyword = C_RID_CODE (token->value);
402 /* Update the value. Some keywords are mapped to particular
403 entities, rather than simply having the value of the
404 corresponding IDENTIFIER_NODE. For example, `__const' is
405 mapped to `const'. */
406 token->value = ridpointers[token->keyword];
407 }
408 else
409 token->keyword = RID_MAX;
410 }
411
412 /* Update the globals input_location and in_system_header from TOKEN. */
413 static inline void
414 cp_lexer_set_source_position_from_token (cp_token *token)
415 {
416 if (token->type != CPP_EOF)
417 {
418 input_location = token->location;
419 in_system_header = token->in_system_header;
420 }
421 }
422
423 /* Return a pointer to the next token in the token stream, but do not
424 consume it. */
425
426 static inline cp_token *
427 cp_lexer_peek_token (cp_lexer *lexer)
428 {
429 if (cp_lexer_debugging_p (lexer))
430 {
431 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
432 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
433 putc ('\n', cp_lexer_debug_stream);
434 }
435 return lexer->next_token;
436 }
437
438 /* Return true if the next token has the indicated TYPE. */
439
440 static inline bool
441 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
442 {
443 return cp_lexer_peek_token (lexer)->type == type;
444 }
445
446 /* Return true if the next token does not have the indicated TYPE. */
447
448 static inline bool
449 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
450 {
451 return !cp_lexer_next_token_is (lexer, type);
452 }
453
454 /* Return true if the next token is the indicated KEYWORD. */
455
456 static inline bool
457 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
458 {
459 cp_token *token;
460
461 /* Peek at the next token. */
462 token = cp_lexer_peek_token (lexer);
463 /* Check to see if it is the indicated keyword. */
464 return token->keyword == keyword;
465 }
466
467 /* Return a pointer to the Nth token in the token stream. If N is 1,
468 then this is precisely equivalent to cp_lexer_peek_token (except
469 that it is not inline). One would like to disallow that case, but
470 there is one case (cp_parser_nth_token_starts_template_id) where
471 the caller passes a variable for N and it might be 1. */
472
473 static cp_token *
474 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
475 {
476 cp_token *token;
477
478 /* N is 1-based, not zero-based. */
479 gcc_assert (n > 0 && lexer->next_token != &eof_token);
480
481 if (cp_lexer_debugging_p (lexer))
482 fprintf (cp_lexer_debug_stream,
483 "cp_lexer: peeking ahead %ld at token: ", (long)n);
484
485 --n;
486 token = lexer->next_token;
487 while (n != 0)
488 {
489 ++token;
490 if (token == lexer->last_token)
491 {
492 token = (cp_token *)&eof_token;
493 break;
494 }
495
496 if (token->type != CPP_PURGED)
497 --n;
498 }
499
500 if (cp_lexer_debugging_p (lexer))
501 {
502 cp_lexer_print_token (cp_lexer_debug_stream, token);
503 putc ('\n', cp_lexer_debug_stream);
504 }
505
506 return token;
507 }
508
509 /* Return the next token, and advance the lexer's next_token pointer
510 to point to the next non-purged token. */
511
512 static cp_token *
513 cp_lexer_consume_token (cp_lexer* lexer)
514 {
515 cp_token *token = lexer->next_token;
516
517 gcc_assert (token != &eof_token);
518
519 do
520 {
521 lexer->next_token++;
522 if (lexer->next_token == lexer->last_token)
523 {
524 lexer->next_token = (cp_token *)&eof_token;
525 break;
526 }
527
528 }
529 while (lexer->next_token->type == CPP_PURGED);
530
531 cp_lexer_set_source_position_from_token (token);
532
533 /* Provide debugging output. */
534 if (cp_lexer_debugging_p (lexer))
535 {
536 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
537 cp_lexer_print_token (cp_lexer_debug_stream, token);
538 putc ('\n', cp_lexer_debug_stream);
539 }
540
541 return token;
542 }
543
544 /* Permanently remove the next token from the token stream, and
545 advance the next_token pointer to refer to the next non-purged
546 token. */
547
548 static void
549 cp_lexer_purge_token (cp_lexer *lexer)
550 {
551 cp_token *tok = lexer->next_token;
552
553 gcc_assert (tok != &eof_token);
554 tok->type = CPP_PURGED;
555 tok->location = UNKNOWN_LOCATION;
556 tok->value = NULL_TREE;
557 tok->keyword = RID_MAX;
558
559 do
560 {
561 tok++;
562 if (tok == lexer->last_token)
563 {
564 tok = (cp_token *)&eof_token;
565 break;
566 }
567 }
568 while (tok->type == CPP_PURGED);
569 lexer->next_token = tok;
570 }
571
572 /* Permanently remove all tokens after TOK, up to, but not
573 including, the token that will be returned next by
574 cp_lexer_peek_token. */
575
576 static void
577 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
578 {
579 cp_token *peek = lexer->next_token;
580
581 if (peek == &eof_token)
582 peek = lexer->last_token;
583
584 gcc_assert (tok < peek);
585
586 for ( tok += 1; tok != peek; tok += 1)
587 {
588 tok->type = CPP_PURGED;
589 tok->location = UNKNOWN_LOCATION;
590 tok->value = NULL_TREE;
591 tok->keyword = RID_MAX;
592 }
593 }
594
595 /* Consume and handle a pragma token. */
596 static void
597 cp_lexer_handle_pragma (cp_lexer *lexer)
598 {
599 cpp_string s;
600 cp_token *token = cp_lexer_consume_token (lexer);
601 gcc_assert (token->type == CPP_PRAGMA);
602 gcc_assert (token->value);
603
604 s.len = TREE_STRING_LENGTH (token->value);
605 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
606
607 cpp_handle_deferred_pragma (parse_in, &s);
608
609 /* Clearing token->value here means that we will get an ICE if we
610 try to process this #pragma again (which should be impossible). */
611 token->value = NULL;
612 }
613
614 /* Begin saving tokens. All tokens consumed after this point will be
615 preserved. */
616
617 static void
618 cp_lexer_save_tokens (cp_lexer* lexer)
619 {
620 /* Provide debugging output. */
621 if (cp_lexer_debugging_p (lexer))
622 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
623
624 VEC_safe_push (cp_token_position, lexer->saved_tokens, lexer->next_token);
625 }
626
627 /* Commit to the portion of the token stream most recently saved. */
628
629 static void
630 cp_lexer_commit_tokens (cp_lexer* lexer)
631 {
632 /* Provide debugging output. */
633 if (cp_lexer_debugging_p (lexer))
634 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
635
636 VEC_pop (cp_token_position, lexer->saved_tokens);
637 }
638
639 /* Return all tokens saved since the last call to cp_lexer_save_tokens
640 to the token stream. Stop saving tokens. */
641
642 static void
643 cp_lexer_rollback_tokens (cp_lexer* lexer)
644 {
645 /* Provide debugging output. */
646 if (cp_lexer_debugging_p (lexer))
647 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
648
649 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
650 }
651
652 /* Print a representation of the TOKEN on the STREAM. */
653
654 #ifdef ENABLE_CHECKING
655
656 static void
657 cp_lexer_print_token (FILE * stream, cp_token *token)
658 {
659 /* We don't use cpp_type2name here because the parser defines
660 a few tokens of its own. */
661 static const char *const token_names[] = {
662 /* cpplib-defined token types */
663 #define OP(e, s) #e,
664 #define TK(e, s) #e,
665 TTYPE_TABLE
666 #undef OP
667 #undef TK
668 /* C++ parser token types - see "Manifest constants", above. */
669 "KEYWORD",
670 "TEMPLATE_ID",
671 "NESTED_NAME_SPECIFIER",
672 "PURGED"
673 };
674
675 /* If we have a name for the token, print it out. Otherwise, we
676 simply give the numeric code. */
677 gcc_assert (token->type < ARRAY_SIZE(token_names));
678 fputs (token_names[token->type], stream);
679
680 /* For some tokens, print the associated data. */
681 switch (token->type)
682 {
683 case CPP_KEYWORD:
684 /* Some keywords have a value that is not an IDENTIFIER_NODE.
685 For example, `struct' is mapped to an INTEGER_CST. */
686 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
687 break;
688 /* else fall through */
689 case CPP_NAME:
690 fputs (IDENTIFIER_POINTER (token->value), stream);
691 break;
692
693 case CPP_STRING:
694 case CPP_WSTRING:
695 case CPP_PRAGMA:
696 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
697 break;
698
699 default:
700 break;
701 }
702 }
703
704 /* Start emitting debugging information. */
705
706 static void
707 cp_lexer_start_debugging (cp_lexer* lexer)
708 {
709 lexer->debugging_p = true;
710 }
711
712 /* Stop emitting debugging information. */
713
714 static void
715 cp_lexer_stop_debugging (cp_lexer* lexer)
716 {
717 lexer->debugging_p = false;
718 }
719
720 #endif /* ENABLE_CHECKING */
721
722 /* Create a new cp_token_cache, representing a range of tokens. */
723
724 static cp_token_cache *
725 cp_token_cache_new (cp_token *first, cp_token *last)
726 {
727 cp_token_cache *cache = GGC_NEW (cp_token_cache);
728 cache->first = first;
729 cache->last = last;
730 return cache;
731 }
732
733 \f
734 /* Decl-specifiers. */
735
736 static void clear_decl_specs
737 (cp_decl_specifier_seq *);
738
739 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
740
741 static void
742 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
743 {
744 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
745 }
746
747 /* Declarators. */
748
749 /* Nothing other than the parser should be creating declarators;
750 declarators are a semi-syntactic representation of C++ entities.
751 Other parts of the front end that need to create entities (like
752 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
753
754 static cp_declarator *make_call_declarator
755 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
756 static cp_declarator *make_array_declarator
757 (cp_declarator *, tree);
758 static cp_declarator *make_pointer_declarator
759 (cp_cv_quals, cp_declarator *);
760 static cp_declarator *make_reference_declarator
761 (cp_cv_quals, cp_declarator *);
762 static cp_parameter_declarator *make_parameter_declarator
763 (cp_decl_specifier_seq *, cp_declarator *, tree);
764 static cp_declarator *make_ptrmem_declarator
765 (cp_cv_quals, tree, cp_declarator *);
766
767 cp_declarator *cp_error_declarator;
768
769 /* The obstack on which declarators and related data structures are
770 allocated. */
771 static struct obstack declarator_obstack;
772
773 /* Alloc BYTES from the declarator memory pool. */
774
775 static inline void *
776 alloc_declarator (size_t bytes)
777 {
778 return obstack_alloc (&declarator_obstack, bytes);
779 }
780
781 /* Allocate a declarator of the indicated KIND. Clear fields that are
782 common to all declarators. */
783
784 static cp_declarator *
785 make_declarator (cp_declarator_kind kind)
786 {
787 cp_declarator *declarator;
788
789 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
790 declarator->kind = kind;
791 declarator->attributes = NULL_TREE;
792 declarator->declarator = NULL;
793
794 return declarator;
795 }
796
797 /* Make a declarator for a generalized identifier. If non-NULL, the
798 identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
799 just UNQUALIFIED_NAME. */
800
801 static cp_declarator *
802 make_id_declarator (tree qualifying_scope, tree unqualified_name)
803 {
804 cp_declarator *declarator;
805
806 /* It is valid to write:
807
808 class C { void f(); };
809 typedef C D;
810 void D::f();
811
812 The standard is not clear about whether `typedef const C D' is
813 legal; as of 2002-09-15 the committee is considering that
814 question. EDG 3.0 allows that syntax. Therefore, we do as
815 well. */
816 if (qualifying_scope && TYPE_P (qualifying_scope))
817 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
818
819 declarator = make_declarator (cdk_id);
820 declarator->u.id.qualifying_scope = qualifying_scope;
821 declarator->u.id.unqualified_name = unqualified_name;
822 declarator->u.id.sfk = sfk_none;
823
824 return declarator;
825 }
826
827 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
828 of modifiers such as const or volatile to apply to the pointer
829 type, represented as identifiers. */
830
831 cp_declarator *
832 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
833 {
834 cp_declarator *declarator;
835
836 declarator = make_declarator (cdk_pointer);
837 declarator->declarator = target;
838 declarator->u.pointer.qualifiers = cv_qualifiers;
839 declarator->u.pointer.class_type = NULL_TREE;
840
841 return declarator;
842 }
843
844 /* Like make_pointer_declarator -- but for references. */
845
846 cp_declarator *
847 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
848 {
849 cp_declarator *declarator;
850
851 declarator = make_declarator (cdk_reference);
852 declarator->declarator = target;
853 declarator->u.pointer.qualifiers = cv_qualifiers;
854 declarator->u.pointer.class_type = NULL_TREE;
855
856 return declarator;
857 }
858
859 /* Like make_pointer_declarator -- but for a pointer to a non-static
860 member of CLASS_TYPE. */
861
862 cp_declarator *
863 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
864 cp_declarator *pointee)
865 {
866 cp_declarator *declarator;
867
868 declarator = make_declarator (cdk_ptrmem);
869 declarator->declarator = pointee;
870 declarator->u.pointer.qualifiers = cv_qualifiers;
871 declarator->u.pointer.class_type = class_type;
872
873 return declarator;
874 }
875
876 /* Make a declarator for the function given by TARGET, with the
877 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
878 "const"-qualified member function. The EXCEPTION_SPECIFICATION
879 indicates what exceptions can be thrown. */
880
881 cp_declarator *
882 make_call_declarator (cp_declarator *target,
883 cp_parameter_declarator *parms,
884 cp_cv_quals cv_qualifiers,
885 tree exception_specification)
886 {
887 cp_declarator *declarator;
888
889 declarator = make_declarator (cdk_function);
890 declarator->declarator = target;
891 declarator->u.function.parameters = parms;
892 declarator->u.function.qualifiers = cv_qualifiers;
893 declarator->u.function.exception_specification = exception_specification;
894
895 return declarator;
896 }
897
898 /* Make a declarator for an array of BOUNDS elements, each of which is
899 defined by ELEMENT. */
900
901 cp_declarator *
902 make_array_declarator (cp_declarator *element, tree bounds)
903 {
904 cp_declarator *declarator;
905
906 declarator = make_declarator (cdk_array);
907 declarator->declarator = element;
908 declarator->u.array.bounds = bounds;
909
910 return declarator;
911 }
912
913 cp_parameter_declarator *no_parameters;
914
915 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
916 DECLARATOR and DEFAULT_ARGUMENT. */
917
918 cp_parameter_declarator *
919 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
920 cp_declarator *declarator,
921 tree default_argument)
922 {
923 cp_parameter_declarator *parameter;
924
925 parameter = ((cp_parameter_declarator *)
926 alloc_declarator (sizeof (cp_parameter_declarator)));
927 parameter->next = NULL;
928 if (decl_specifiers)
929 parameter->decl_specifiers = *decl_specifiers;
930 else
931 clear_decl_specs (&parameter->decl_specifiers);
932 parameter->declarator = declarator;
933 parameter->default_argument = default_argument;
934 parameter->ellipsis_p = false;
935
936 return parameter;
937 }
938
939 /* The parser. */
940
941 /* Overview
942 --------
943
944 A cp_parser parses the token stream as specified by the C++
945 grammar. Its job is purely parsing, not semantic analysis. For
946 example, the parser breaks the token stream into declarators,
947 expressions, statements, and other similar syntactic constructs.
948 It does not check that the types of the expressions on either side
949 of an assignment-statement are compatible, or that a function is
950 not declared with a parameter of type `void'.
951
952 The parser invokes routines elsewhere in the compiler to perform
953 semantic analysis and to build up the abstract syntax tree for the
954 code processed.
955
956 The parser (and the template instantiation code, which is, in a
957 way, a close relative of parsing) are the only parts of the
958 compiler that should be calling push_scope and pop_scope, or
959 related functions. The parser (and template instantiation code)
960 keeps track of what scope is presently active; everything else
961 should simply honor that. (The code that generates static
962 initializers may also need to set the scope, in order to check
963 access control correctly when emitting the initializers.)
964
965 Methodology
966 -----------
967
968 The parser is of the standard recursive-descent variety. Upcoming
969 tokens in the token stream are examined in order to determine which
970 production to use when parsing a non-terminal. Some C++ constructs
971 require arbitrary look ahead to disambiguate. For example, it is
972 impossible, in the general case, to tell whether a statement is an
973 expression or declaration without scanning the entire statement.
974 Therefore, the parser is capable of "parsing tentatively." When the
975 parser is not sure what construct comes next, it enters this mode.
976 Then, while we attempt to parse the construct, the parser queues up
977 error messages, rather than issuing them immediately, and saves the
978 tokens it consumes. If the construct is parsed successfully, the
979 parser "commits", i.e., it issues any queued error messages and
980 the tokens that were being preserved are permanently discarded.
981 If, however, the construct is not parsed successfully, the parser
982 rolls back its state completely so that it can resume parsing using
983 a different alternative.
984
985 Future Improvements
986 -------------------
987
988 The performance of the parser could probably be improved substantially.
989 We could often eliminate the need to parse tentatively by looking ahead
990 a little bit. In some places, this approach might not entirely eliminate
991 the need to parse tentatively, but it might still speed up the average
992 case. */
993
994 /* Flags that are passed to some parsing functions. These values can
995 be bitwise-ored together. */
996
997 typedef enum cp_parser_flags
998 {
999 /* No flags. */
1000 CP_PARSER_FLAGS_NONE = 0x0,
1001 /* The construct is optional. If it is not present, then no error
1002 should be issued. */
1003 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1004 /* When parsing a type-specifier, do not allow user-defined types. */
1005 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1006 } cp_parser_flags;
1007
1008 /* The different kinds of declarators we want to parse. */
1009
1010 typedef enum cp_parser_declarator_kind
1011 {
1012 /* We want an abstract declarator. */
1013 CP_PARSER_DECLARATOR_ABSTRACT,
1014 /* We want a named declarator. */
1015 CP_PARSER_DECLARATOR_NAMED,
1016 /* We don't mind, but the name must be an unqualified-id. */
1017 CP_PARSER_DECLARATOR_EITHER
1018 } cp_parser_declarator_kind;
1019
1020 /* The precedence values used to parse binary expressions. The minimum value
1021 of PREC must be 1, because zero is reserved to quickly discriminate
1022 binary operators from other tokens. */
1023
1024 enum cp_parser_prec
1025 {
1026 PREC_NOT_OPERATOR,
1027 PREC_LOGICAL_OR_EXPRESSION,
1028 PREC_LOGICAL_AND_EXPRESSION,
1029 PREC_INCLUSIVE_OR_EXPRESSION,
1030 PREC_EXCLUSIVE_OR_EXPRESSION,
1031 PREC_AND_EXPRESSION,
1032 PREC_EQUALITY_EXPRESSION,
1033 PREC_RELATIONAL_EXPRESSION,
1034 PREC_SHIFT_EXPRESSION,
1035 PREC_ADDITIVE_EXPRESSION,
1036 PREC_MULTIPLICATIVE_EXPRESSION,
1037 PREC_PM_EXPRESSION,
1038 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1039 };
1040
1041 /* A mapping from a token type to a corresponding tree node type, with a
1042 precedence value. */
1043
1044 typedef struct cp_parser_binary_operations_map_node
1045 {
1046 /* The token type. */
1047 enum cpp_ttype token_type;
1048 /* The corresponding tree code. */
1049 enum tree_code tree_type;
1050 /* The precedence of this operator. */
1051 enum cp_parser_prec prec;
1052 } cp_parser_binary_operations_map_node;
1053
1054 /* The status of a tentative parse. */
1055
1056 typedef enum cp_parser_status_kind
1057 {
1058 /* No errors have occurred. */
1059 CP_PARSER_STATUS_KIND_NO_ERROR,
1060 /* An error has occurred. */
1061 CP_PARSER_STATUS_KIND_ERROR,
1062 /* We are committed to this tentative parse, whether or not an error
1063 has occurred. */
1064 CP_PARSER_STATUS_KIND_COMMITTED
1065 } cp_parser_status_kind;
1066
1067 typedef struct cp_parser_expression_stack_entry
1068 {
1069 tree lhs;
1070 enum tree_code tree_type;
1071 int prec;
1072 } cp_parser_expression_stack_entry;
1073
1074 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1075 entries because precedence levels on the stack are monotonically
1076 increasing. */
1077 typedef struct cp_parser_expression_stack_entry
1078 cp_parser_expression_stack[NUM_PREC_VALUES];
1079
1080 /* Context that is saved and restored when parsing tentatively. */
1081 typedef struct cp_parser_context GTY (())
1082 {
1083 /* If this is a tentative parsing context, the status of the
1084 tentative parse. */
1085 enum cp_parser_status_kind status;
1086 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1087 that are looked up in this context must be looked up both in the
1088 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1089 the context of the containing expression. */
1090 tree object_type;
1091
1092 /* The next parsing context in the stack. */
1093 struct cp_parser_context *next;
1094 } cp_parser_context;
1095
1096 /* Prototypes. */
1097
1098 /* Constructors and destructors. */
1099
1100 static cp_parser_context *cp_parser_context_new
1101 (cp_parser_context *);
1102
1103 /* Class variables. */
1104
1105 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1106
1107 /* The operator-precedence table used by cp_parser_binary_expression.
1108 Transformed into an associative array (binops_by_token) by
1109 cp_parser_new. */
1110
1111 static const cp_parser_binary_operations_map_node binops[] = {
1112 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1113 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1114
1115 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1116 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1117 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1118
1119 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1120 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1121
1122 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1123 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1124
1125 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1126 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1127 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1128 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1129 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1130 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1131
1132 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1133 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1134
1135 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1136
1137 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1138
1139 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1140
1141 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1142
1143 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1144 };
1145
1146 /* The same as binops, but initialized by cp_parser_new so that
1147 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1148 for speed. */
1149 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1150
1151 /* Constructors and destructors. */
1152
1153 /* Construct a new context. The context below this one on the stack
1154 is given by NEXT. */
1155
1156 static cp_parser_context *
1157 cp_parser_context_new (cp_parser_context* next)
1158 {
1159 cp_parser_context *context;
1160
1161 /* Allocate the storage. */
1162 if (cp_parser_context_free_list != NULL)
1163 {
1164 /* Pull the first entry from the free list. */
1165 context = cp_parser_context_free_list;
1166 cp_parser_context_free_list = context->next;
1167 memset (context, 0, sizeof (*context));
1168 }
1169 else
1170 context = GGC_CNEW (cp_parser_context);
1171
1172 /* No errors have occurred yet in this context. */
1173 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1174 /* If this is not the bottomost context, copy information that we
1175 need from the previous context. */
1176 if (next)
1177 {
1178 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1179 expression, then we are parsing one in this context, too. */
1180 context->object_type = next->object_type;
1181 /* Thread the stack. */
1182 context->next = next;
1183 }
1184
1185 return context;
1186 }
1187
1188 /* The cp_parser structure represents the C++ parser. */
1189
1190 typedef struct cp_parser GTY(())
1191 {
1192 /* The lexer from which we are obtaining tokens. */
1193 cp_lexer *lexer;
1194
1195 /* The scope in which names should be looked up. If NULL_TREE, then
1196 we look up names in the scope that is currently open in the
1197 source program. If non-NULL, this is either a TYPE or
1198 NAMESPACE_DECL for the scope in which we should look.
1199
1200 This value is not cleared automatically after a name is looked
1201 up, so we must be careful to clear it before starting a new look
1202 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1203 will look up `Z' in the scope of `X', rather than the current
1204 scope.) Unfortunately, it is difficult to tell when name lookup
1205 is complete, because we sometimes peek at a token, look it up,
1206 and then decide not to consume it. */
1207 tree scope;
1208
1209 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1210 last lookup took place. OBJECT_SCOPE is used if an expression
1211 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1212 respectively. QUALIFYING_SCOPE is used for an expression of the
1213 form "X::Y"; it refers to X. */
1214 tree object_scope;
1215 tree qualifying_scope;
1216
1217 /* A stack of parsing contexts. All but the bottom entry on the
1218 stack will be tentative contexts.
1219
1220 We parse tentatively in order to determine which construct is in
1221 use in some situations. For example, in order to determine
1222 whether a statement is an expression-statement or a
1223 declaration-statement we parse it tentatively as a
1224 declaration-statement. If that fails, we then reparse the same
1225 token stream as an expression-statement. */
1226 cp_parser_context *context;
1227
1228 /* True if we are parsing GNU C++. If this flag is not set, then
1229 GNU extensions are not recognized. */
1230 bool allow_gnu_extensions_p;
1231
1232 /* TRUE if the `>' token should be interpreted as the greater-than
1233 operator. FALSE if it is the end of a template-id or
1234 template-parameter-list. */
1235 bool greater_than_is_operator_p;
1236
1237 /* TRUE if default arguments are allowed within a parameter list
1238 that starts at this point. FALSE if only a gnu extension makes
1239 them permissible. */
1240 bool default_arg_ok_p;
1241
1242 /* TRUE if we are parsing an integral constant-expression. See
1243 [expr.const] for a precise definition. */
1244 bool integral_constant_expression_p;
1245
1246 /* TRUE if we are parsing an integral constant-expression -- but a
1247 non-constant expression should be permitted as well. This flag
1248 is used when parsing an array bound so that GNU variable-length
1249 arrays are tolerated. */
1250 bool allow_non_integral_constant_expression_p;
1251
1252 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1253 been seen that makes the expression non-constant. */
1254 bool non_integral_constant_expression_p;
1255
1256 /* TRUE if local variable names and `this' are forbidden in the
1257 current context. */
1258 bool local_variables_forbidden_p;
1259
1260 /* TRUE if the declaration we are parsing is part of a
1261 linkage-specification of the form `extern string-literal
1262 declaration'. */
1263 bool in_unbraced_linkage_specification_p;
1264
1265 /* TRUE if we are presently parsing a declarator, after the
1266 direct-declarator. */
1267 bool in_declarator_p;
1268
1269 /* TRUE if we are presently parsing a template-argument-list. */
1270 bool in_template_argument_list_p;
1271
1272 /* TRUE if we are presently parsing the body of an
1273 iteration-statement. */
1274 bool in_iteration_statement_p;
1275
1276 /* TRUE if we are presently parsing the body of a switch
1277 statement. */
1278 bool in_switch_statement_p;
1279
1280 /* TRUE if we are parsing a type-id in an expression context. In
1281 such a situation, both "type (expr)" and "type (type)" are valid
1282 alternatives. */
1283 bool in_type_id_in_expr_p;
1284
1285 /* TRUE if we are currently in a header file where declarations are
1286 implicitly extern "C". */
1287 bool implicit_extern_c;
1288
1289 /* TRUE if strings in expressions should be translated to the execution
1290 character set. */
1291 bool translate_strings_p;
1292
1293 /* If non-NULL, then we are parsing a construct where new type
1294 definitions are not permitted. The string stored here will be
1295 issued as an error message if a type is defined. */
1296 const char *type_definition_forbidden_message;
1297
1298 /* A list of lists. The outer list is a stack, used for member
1299 functions of local classes. At each level there are two sub-list,
1300 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1301 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1302 TREE_VALUE's. The functions are chained in reverse declaration
1303 order.
1304
1305 The TREE_PURPOSE sublist contains those functions with default
1306 arguments that need post processing, and the TREE_VALUE sublist
1307 contains those functions with definitions that need post
1308 processing.
1309
1310 These lists can only be processed once the outermost class being
1311 defined is complete. */
1312 tree unparsed_functions_queues;
1313
1314 /* The number of classes whose definitions are currently in
1315 progress. */
1316 unsigned num_classes_being_defined;
1317
1318 /* The number of template parameter lists that apply directly to the
1319 current declaration. */
1320 unsigned num_template_parameter_lists;
1321 } cp_parser;
1322
1323 /* The type of a function that parses some kind of expression. */
1324 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1325
1326 /* Prototypes. */
1327
1328 /* Constructors and destructors. */
1329
1330 static cp_parser *cp_parser_new
1331 (void);
1332
1333 /* Routines to parse various constructs.
1334
1335 Those that return `tree' will return the error_mark_node (rather
1336 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1337 Sometimes, they will return an ordinary node if error-recovery was
1338 attempted, even though a parse error occurred. So, to check
1339 whether or not a parse error occurred, you should always use
1340 cp_parser_error_occurred. If the construct is optional (indicated
1341 either by an `_opt' in the name of the function that does the
1342 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1343 the construct is not present. */
1344
1345 /* Lexical conventions [gram.lex] */
1346
1347 static tree cp_parser_identifier
1348 (cp_parser *);
1349 static tree cp_parser_string_literal
1350 (cp_parser *, bool, bool);
1351
1352 /* Basic concepts [gram.basic] */
1353
1354 static bool cp_parser_translation_unit
1355 (cp_parser *);
1356
1357 /* Expressions [gram.expr] */
1358
1359 static tree cp_parser_primary_expression
1360 (cp_parser *, bool, cp_id_kind *, tree *);
1361 static tree cp_parser_id_expression
1362 (cp_parser *, bool, bool, bool *, bool);
1363 static tree cp_parser_unqualified_id
1364 (cp_parser *, bool, bool, bool);
1365 static tree cp_parser_nested_name_specifier_opt
1366 (cp_parser *, bool, bool, bool, bool);
1367 static tree cp_parser_nested_name_specifier
1368 (cp_parser *, bool, bool, bool, bool);
1369 static tree cp_parser_class_or_namespace_name
1370 (cp_parser *, bool, bool, bool, bool, bool);
1371 static tree cp_parser_postfix_expression
1372 (cp_parser *, bool, bool);
1373 static tree cp_parser_postfix_open_square_expression
1374 (cp_parser *, tree, bool);
1375 static tree cp_parser_postfix_dot_deref_expression
1376 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1377 static tree cp_parser_parenthesized_expression_list
1378 (cp_parser *, bool, bool, bool *);
1379 static void cp_parser_pseudo_destructor_name
1380 (cp_parser *, tree *, tree *);
1381 static tree cp_parser_unary_expression
1382 (cp_parser *, bool, bool);
1383 static enum tree_code cp_parser_unary_operator
1384 (cp_token *);
1385 static tree cp_parser_new_expression
1386 (cp_parser *);
1387 static tree cp_parser_new_placement
1388 (cp_parser *);
1389 static tree cp_parser_new_type_id
1390 (cp_parser *, tree *);
1391 static cp_declarator *cp_parser_new_declarator_opt
1392 (cp_parser *);
1393 static cp_declarator *cp_parser_direct_new_declarator
1394 (cp_parser *);
1395 static tree cp_parser_new_initializer
1396 (cp_parser *);
1397 static tree cp_parser_delete_expression
1398 (cp_parser *);
1399 static tree cp_parser_cast_expression
1400 (cp_parser *, bool, bool);
1401 static tree cp_parser_binary_expression
1402 (cp_parser *, bool);
1403 static tree cp_parser_question_colon_clause
1404 (cp_parser *, tree);
1405 static tree cp_parser_assignment_expression
1406 (cp_parser *, bool);
1407 static enum tree_code cp_parser_assignment_operator_opt
1408 (cp_parser *);
1409 static tree cp_parser_expression
1410 (cp_parser *, bool);
1411 static tree cp_parser_constant_expression
1412 (cp_parser *, bool, bool *);
1413 static tree cp_parser_builtin_offsetof
1414 (cp_parser *);
1415
1416 /* Statements [gram.stmt.stmt] */
1417
1418 static void cp_parser_statement
1419 (cp_parser *, tree);
1420 static tree cp_parser_labeled_statement
1421 (cp_parser *, tree);
1422 static tree cp_parser_expression_statement
1423 (cp_parser *, tree);
1424 static tree cp_parser_compound_statement
1425 (cp_parser *, tree, bool);
1426 static void cp_parser_statement_seq_opt
1427 (cp_parser *, tree);
1428 static tree cp_parser_selection_statement
1429 (cp_parser *);
1430 static tree cp_parser_condition
1431 (cp_parser *);
1432 static tree cp_parser_iteration_statement
1433 (cp_parser *);
1434 static void cp_parser_for_init_statement
1435 (cp_parser *);
1436 static tree cp_parser_jump_statement
1437 (cp_parser *);
1438 static void cp_parser_declaration_statement
1439 (cp_parser *);
1440
1441 static tree cp_parser_implicitly_scoped_statement
1442 (cp_parser *);
1443 static void cp_parser_already_scoped_statement
1444 (cp_parser *);
1445
1446 /* Declarations [gram.dcl.dcl] */
1447
1448 static void cp_parser_declaration_seq_opt
1449 (cp_parser *);
1450 static void cp_parser_declaration
1451 (cp_parser *);
1452 static void cp_parser_block_declaration
1453 (cp_parser *, bool);
1454 static void cp_parser_simple_declaration
1455 (cp_parser *, bool);
1456 static void cp_parser_decl_specifier_seq
1457 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1458 static tree cp_parser_storage_class_specifier_opt
1459 (cp_parser *);
1460 static tree cp_parser_function_specifier_opt
1461 (cp_parser *, cp_decl_specifier_seq *);
1462 static tree cp_parser_type_specifier
1463 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1464 int *, bool *);
1465 static tree cp_parser_simple_type_specifier
1466 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1467 static tree cp_parser_type_name
1468 (cp_parser *);
1469 static tree cp_parser_elaborated_type_specifier
1470 (cp_parser *, bool, bool);
1471 static tree cp_parser_enum_specifier
1472 (cp_parser *);
1473 static void cp_parser_enumerator_list
1474 (cp_parser *, tree);
1475 static void cp_parser_enumerator_definition
1476 (cp_parser *, tree);
1477 static tree cp_parser_namespace_name
1478 (cp_parser *);
1479 static void cp_parser_namespace_definition
1480 (cp_parser *);
1481 static void cp_parser_namespace_body
1482 (cp_parser *);
1483 static tree cp_parser_qualified_namespace_specifier
1484 (cp_parser *);
1485 static void cp_parser_namespace_alias_definition
1486 (cp_parser *);
1487 static void cp_parser_using_declaration
1488 (cp_parser *);
1489 static void cp_parser_using_directive
1490 (cp_parser *);
1491 static void cp_parser_asm_definition
1492 (cp_parser *);
1493 static void cp_parser_linkage_specification
1494 (cp_parser *);
1495
1496 /* Declarators [gram.dcl.decl] */
1497
1498 static tree cp_parser_init_declarator
1499 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1500 static cp_declarator *cp_parser_declarator
1501 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1502 static cp_declarator *cp_parser_direct_declarator
1503 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1504 static enum tree_code cp_parser_ptr_operator
1505 (cp_parser *, tree *, cp_cv_quals *);
1506 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1507 (cp_parser *);
1508 static tree cp_parser_declarator_id
1509 (cp_parser *);
1510 static tree cp_parser_type_id
1511 (cp_parser *);
1512 static void cp_parser_type_specifier_seq
1513 (cp_parser *, bool, cp_decl_specifier_seq *);
1514 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1515 (cp_parser *);
1516 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1517 (cp_parser *, bool *);
1518 static cp_parameter_declarator *cp_parser_parameter_declaration
1519 (cp_parser *, bool, bool *);
1520 static void cp_parser_function_body
1521 (cp_parser *);
1522 static tree cp_parser_initializer
1523 (cp_parser *, bool *, bool *);
1524 static tree cp_parser_initializer_clause
1525 (cp_parser *, bool *);
1526 static tree cp_parser_initializer_list
1527 (cp_parser *, bool *);
1528
1529 static bool cp_parser_ctor_initializer_opt_and_function_body
1530 (cp_parser *);
1531
1532 /* Classes [gram.class] */
1533
1534 static tree cp_parser_class_name
1535 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1536 static tree cp_parser_class_specifier
1537 (cp_parser *);
1538 static tree cp_parser_class_head
1539 (cp_parser *, bool *, tree *);
1540 static enum tag_types cp_parser_class_key
1541 (cp_parser *);
1542 static void cp_parser_member_specification_opt
1543 (cp_parser *);
1544 static void cp_parser_member_declaration
1545 (cp_parser *);
1546 static tree cp_parser_pure_specifier
1547 (cp_parser *);
1548 static tree cp_parser_constant_initializer
1549 (cp_parser *);
1550
1551 /* Derived classes [gram.class.derived] */
1552
1553 static tree cp_parser_base_clause
1554 (cp_parser *);
1555 static tree cp_parser_base_specifier
1556 (cp_parser *);
1557
1558 /* Special member functions [gram.special] */
1559
1560 static tree cp_parser_conversion_function_id
1561 (cp_parser *);
1562 static tree cp_parser_conversion_type_id
1563 (cp_parser *);
1564 static cp_declarator *cp_parser_conversion_declarator_opt
1565 (cp_parser *);
1566 static bool cp_parser_ctor_initializer_opt
1567 (cp_parser *);
1568 static void cp_parser_mem_initializer_list
1569 (cp_parser *);
1570 static tree cp_parser_mem_initializer
1571 (cp_parser *);
1572 static tree cp_parser_mem_initializer_id
1573 (cp_parser *);
1574
1575 /* Overloading [gram.over] */
1576
1577 static tree cp_parser_operator_function_id
1578 (cp_parser *);
1579 static tree cp_parser_operator
1580 (cp_parser *);
1581
1582 /* Templates [gram.temp] */
1583
1584 static void cp_parser_template_declaration
1585 (cp_parser *, bool);
1586 static tree cp_parser_template_parameter_list
1587 (cp_parser *);
1588 static tree cp_parser_template_parameter
1589 (cp_parser *, bool *);
1590 static tree cp_parser_type_parameter
1591 (cp_parser *);
1592 static tree cp_parser_template_id
1593 (cp_parser *, bool, bool, bool);
1594 static tree cp_parser_template_name
1595 (cp_parser *, bool, bool, bool, bool *);
1596 static tree cp_parser_template_argument_list
1597 (cp_parser *);
1598 static tree cp_parser_template_argument
1599 (cp_parser *);
1600 static void cp_parser_explicit_instantiation
1601 (cp_parser *);
1602 static void cp_parser_explicit_specialization
1603 (cp_parser *);
1604
1605 /* Exception handling [gram.exception] */
1606
1607 static tree cp_parser_try_block
1608 (cp_parser *);
1609 static bool cp_parser_function_try_block
1610 (cp_parser *);
1611 static void cp_parser_handler_seq
1612 (cp_parser *);
1613 static void cp_parser_handler
1614 (cp_parser *);
1615 static tree cp_parser_exception_declaration
1616 (cp_parser *);
1617 static tree cp_parser_throw_expression
1618 (cp_parser *);
1619 static tree cp_parser_exception_specification_opt
1620 (cp_parser *);
1621 static tree cp_parser_type_id_list
1622 (cp_parser *);
1623
1624 /* GNU Extensions */
1625
1626 static tree cp_parser_asm_specification_opt
1627 (cp_parser *);
1628 static tree cp_parser_asm_operand_list
1629 (cp_parser *);
1630 static tree cp_parser_asm_clobber_list
1631 (cp_parser *);
1632 static tree cp_parser_attributes_opt
1633 (cp_parser *);
1634 static tree cp_parser_attribute_list
1635 (cp_parser *);
1636 static bool cp_parser_extension_opt
1637 (cp_parser *, int *);
1638 static void cp_parser_label_declaration
1639 (cp_parser *);
1640
1641 /* Utility Routines */
1642
1643 static tree cp_parser_lookup_name
1644 (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
1645 static tree cp_parser_lookup_name_simple
1646 (cp_parser *, tree);
1647 static tree cp_parser_maybe_treat_template_as_class
1648 (tree, bool);
1649 static bool cp_parser_check_declarator_template_parameters
1650 (cp_parser *, cp_declarator *);
1651 static bool cp_parser_check_template_parameters
1652 (cp_parser *, unsigned);
1653 static tree cp_parser_simple_cast_expression
1654 (cp_parser *);
1655 static tree cp_parser_global_scope_opt
1656 (cp_parser *, bool);
1657 static bool cp_parser_constructor_declarator_p
1658 (cp_parser *, bool);
1659 static tree cp_parser_function_definition_from_specifiers_and_declarator
1660 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1661 static tree cp_parser_function_definition_after_declarator
1662 (cp_parser *, bool);
1663 static void cp_parser_template_declaration_after_export
1664 (cp_parser *, bool);
1665 static tree cp_parser_single_declaration
1666 (cp_parser *, bool, bool *);
1667 static tree cp_parser_functional_cast
1668 (cp_parser *, tree);
1669 static tree cp_parser_save_member_function_body
1670 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1671 static tree cp_parser_enclosed_template_argument_list
1672 (cp_parser *);
1673 static void cp_parser_save_default_args
1674 (cp_parser *, tree);
1675 static void cp_parser_late_parsing_for_member
1676 (cp_parser *, tree);
1677 static void cp_parser_late_parsing_default_args
1678 (cp_parser *, tree);
1679 static tree cp_parser_sizeof_operand
1680 (cp_parser *, enum rid);
1681 static bool cp_parser_declares_only_class_p
1682 (cp_parser *);
1683 static void cp_parser_set_storage_class
1684 (cp_decl_specifier_seq *, cp_storage_class);
1685 static void cp_parser_set_decl_spec_type
1686 (cp_decl_specifier_seq *, tree, bool);
1687 static bool cp_parser_friend_p
1688 (const cp_decl_specifier_seq *);
1689 static cp_token *cp_parser_require
1690 (cp_parser *, enum cpp_ttype, const char *);
1691 static cp_token *cp_parser_require_keyword
1692 (cp_parser *, enum rid, const char *);
1693 static bool cp_parser_token_starts_function_definition_p
1694 (cp_token *);
1695 static bool cp_parser_next_token_starts_class_definition_p
1696 (cp_parser *);
1697 static bool cp_parser_next_token_ends_template_argument_p
1698 (cp_parser *);
1699 static bool cp_parser_nth_token_starts_template_argument_list_p
1700 (cp_parser *, size_t);
1701 static enum tag_types cp_parser_token_is_class_key
1702 (cp_token *);
1703 static void cp_parser_check_class_key
1704 (enum tag_types, tree type);
1705 static void cp_parser_check_access_in_redeclaration
1706 (tree type);
1707 static bool cp_parser_optional_template_keyword
1708 (cp_parser *);
1709 static void cp_parser_pre_parsed_nested_name_specifier
1710 (cp_parser *);
1711 static void cp_parser_cache_group
1712 (cp_parser *, enum cpp_ttype, unsigned);
1713 static void cp_parser_parse_tentatively
1714 (cp_parser *);
1715 static void cp_parser_commit_to_tentative_parse
1716 (cp_parser *);
1717 static void cp_parser_abort_tentative_parse
1718 (cp_parser *);
1719 static bool cp_parser_parse_definitely
1720 (cp_parser *);
1721 static inline bool cp_parser_parsing_tentatively
1722 (cp_parser *);
1723 static bool cp_parser_uncommitted_to_tentative_parse_p
1724 (cp_parser *);
1725 static void cp_parser_error
1726 (cp_parser *, const char *);
1727 static void cp_parser_name_lookup_error
1728 (cp_parser *, tree, tree, const char *);
1729 static bool cp_parser_simulate_error
1730 (cp_parser *);
1731 static void cp_parser_check_type_definition
1732 (cp_parser *);
1733 static void cp_parser_check_for_definition_in_return_type
1734 (cp_declarator *, tree);
1735 static void cp_parser_check_for_invalid_template_id
1736 (cp_parser *, tree);
1737 static bool cp_parser_non_integral_constant_expression
1738 (cp_parser *, const char *);
1739 static void cp_parser_diagnose_invalid_type_name
1740 (cp_parser *, tree, tree);
1741 static bool cp_parser_parse_and_diagnose_invalid_type_name
1742 (cp_parser *);
1743 static int cp_parser_skip_to_closing_parenthesis
1744 (cp_parser *, bool, bool, bool);
1745 static void cp_parser_skip_to_end_of_statement
1746 (cp_parser *);
1747 static void cp_parser_consume_semicolon_at_end_of_statement
1748 (cp_parser *);
1749 static void cp_parser_skip_to_end_of_block_or_statement
1750 (cp_parser *);
1751 static void cp_parser_skip_to_closing_brace
1752 (cp_parser *);
1753 static void cp_parser_skip_until_found
1754 (cp_parser *, enum cpp_ttype, const char *);
1755 static bool cp_parser_error_occurred
1756 (cp_parser *);
1757 static bool cp_parser_allow_gnu_extensions_p
1758 (cp_parser *);
1759 static bool cp_parser_is_string_literal
1760 (cp_token *);
1761 static bool cp_parser_is_keyword
1762 (cp_token *, enum rid);
1763 static tree cp_parser_make_typename_type
1764 (cp_parser *, tree, tree);
1765
1766 /* Returns nonzero if we are parsing tentatively. */
1767
1768 static inline bool
1769 cp_parser_parsing_tentatively (cp_parser* parser)
1770 {
1771 return parser->context->next != NULL;
1772 }
1773
1774 /* Returns nonzero if TOKEN is a string literal. */
1775
1776 static bool
1777 cp_parser_is_string_literal (cp_token* token)
1778 {
1779 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1780 }
1781
1782 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1783
1784 static bool
1785 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1786 {
1787 return token->keyword == keyword;
1788 }
1789
1790 /* A minimum or maximum operator has been seen. As these are
1791 deprecated, issue a warning. */
1792
1793 static inline void
1794 cp_parser_warn_min_max (void)
1795 {
1796 if (warn_deprecated && !in_system_header)
1797 warning ("minimum/maximum operators are deprecated");
1798 }
1799
1800 /* If not parsing tentatively, issue a diagnostic of the form
1801 FILE:LINE: MESSAGE before TOKEN
1802 where TOKEN is the next token in the input stream. MESSAGE
1803 (specified by the caller) is usually of the form "expected
1804 OTHER-TOKEN". */
1805
1806 static void
1807 cp_parser_error (cp_parser* parser, const char* message)
1808 {
1809 if (!cp_parser_simulate_error (parser))
1810 {
1811 cp_token *token = cp_lexer_peek_token (parser->lexer);
1812 /* This diagnostic makes more sense if it is tagged to the line
1813 of the token we just peeked at. */
1814 cp_lexer_set_source_position_from_token (token);
1815 if (token->type == CPP_PRAGMA)
1816 {
1817 error ("%<#pragma%> is not allowed here");
1818 cp_lexer_purge_token (parser->lexer);
1819 return;
1820 }
1821 c_parse_error (message,
1822 /* Because c_parser_error does not understand
1823 CPP_KEYWORD, keywords are treated like
1824 identifiers. */
1825 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1826 token->value);
1827 }
1828 }
1829
1830 /* Issue an error about name-lookup failing. NAME is the
1831 IDENTIFIER_NODE DECL is the result of
1832 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1833 the thing that we hoped to find. */
1834
1835 static void
1836 cp_parser_name_lookup_error (cp_parser* parser,
1837 tree name,
1838 tree decl,
1839 const char* desired)
1840 {
1841 /* If name lookup completely failed, tell the user that NAME was not
1842 declared. */
1843 if (decl == error_mark_node)
1844 {
1845 if (parser->scope && parser->scope != global_namespace)
1846 error ("%<%D::%D%> has not been declared",
1847 parser->scope, name);
1848 else if (parser->scope == global_namespace)
1849 error ("%<::%D%> has not been declared", name);
1850 else if (parser->object_scope
1851 && !CLASS_TYPE_P (parser->object_scope))
1852 error ("request for member %qD in non-class type %qT",
1853 name, parser->object_scope);
1854 else if (parser->object_scope)
1855 error ("%<%T::%D%> has not been declared",
1856 parser->object_scope, name);
1857 else
1858 error ("%qD has not been declared", name);
1859 }
1860 else if (parser->scope && parser->scope != global_namespace)
1861 error ("%<%D::%D%> %s", parser->scope, name, desired);
1862 else if (parser->scope == global_namespace)
1863 error ("%<::%D%> %s", name, desired);
1864 else
1865 error ("%qD %s", name, desired);
1866 }
1867
1868 /* If we are parsing tentatively, remember that an error has occurred
1869 during this tentative parse. Returns true if the error was
1870 simulated; false if a message should be issued by the caller. */
1871
1872 static bool
1873 cp_parser_simulate_error (cp_parser* parser)
1874 {
1875 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1876 {
1877 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1878 return true;
1879 }
1880 return false;
1881 }
1882
1883 /* This function is called when a type is defined. If type
1884 definitions are forbidden at this point, an error message is
1885 issued. */
1886
1887 static void
1888 cp_parser_check_type_definition (cp_parser* parser)
1889 {
1890 /* If types are forbidden here, issue a message. */
1891 if (parser->type_definition_forbidden_message)
1892 /* Use `%s' to print the string in case there are any escape
1893 characters in the message. */
1894 error ("%s", parser->type_definition_forbidden_message);
1895 }
1896
1897 /* This function is called when the DECLARATOR is processed. The TYPE
1898 was a type defined in the decl-specifiers. If it is invalid to
1899 define a type in the decl-specifiers for DECLARATOR, an error is
1900 issued. */
1901
1902 static void
1903 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1904 tree type)
1905 {
1906 /* [dcl.fct] forbids type definitions in return types.
1907 Unfortunately, it's not easy to know whether or not we are
1908 processing a return type until after the fact. */
1909 while (declarator
1910 && (declarator->kind == cdk_pointer
1911 || declarator->kind == cdk_reference
1912 || declarator->kind == cdk_ptrmem))
1913 declarator = declarator->declarator;
1914 if (declarator
1915 && declarator->kind == cdk_function)
1916 {
1917 error ("new types may not be defined in a return type");
1918 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1919 type);
1920 }
1921 }
1922
1923 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1924 "<" in any valid C++ program. If the next token is indeed "<",
1925 issue a message warning the user about what appears to be an
1926 invalid attempt to form a template-id. */
1927
1928 static void
1929 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1930 tree type)
1931 {
1932 cp_token_position start = 0;
1933
1934 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1935 {
1936 if (TYPE_P (type))
1937 error ("%qT is not a template", type);
1938 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1939 error ("%qE is not a template", type);
1940 else
1941 error ("invalid template-id");
1942 /* Remember the location of the invalid "<". */
1943 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1944 start = cp_lexer_token_position (parser->lexer, true);
1945 /* Consume the "<". */
1946 cp_lexer_consume_token (parser->lexer);
1947 /* Parse the template arguments. */
1948 cp_parser_enclosed_template_argument_list (parser);
1949 /* Permanently remove the invalid template arguments so that
1950 this error message is not issued again. */
1951 if (start)
1952 cp_lexer_purge_tokens_after (parser->lexer, start);
1953 }
1954 }
1955
1956 /* If parsing an integral constant-expression, issue an error message
1957 about the fact that THING appeared and return true. Otherwise,
1958 return false. In either case, set
1959 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
1960
1961 static bool
1962 cp_parser_non_integral_constant_expression (cp_parser *parser,
1963 const char *thing)
1964 {
1965 parser->non_integral_constant_expression_p = true;
1966 if (parser->integral_constant_expression_p)
1967 {
1968 if (!parser->allow_non_integral_constant_expression_p)
1969 {
1970 error ("%s cannot appear in a constant-expression", thing);
1971 return true;
1972 }
1973 }
1974 return false;
1975 }
1976
1977 /* Emit a diagnostic for an invalid type name. SCOPE is the
1978 qualifying scope (or NULL, if none) for ID. This function commits
1979 to the current active tentative parse, if any. (Otherwise, the
1980 problematic construct might be encountered again later, resulting
1981 in duplicate error messages.) */
1982
1983 static void
1984 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1985 {
1986 tree decl, old_scope;
1987 /* Try to lookup the identifier. */
1988 old_scope = parser->scope;
1989 parser->scope = scope;
1990 decl = cp_parser_lookup_name_simple (parser, id);
1991 parser->scope = old_scope;
1992 /* If the lookup found a template-name, it means that the user forgot
1993 to specify an argument list. Emit an useful error message. */
1994 if (TREE_CODE (decl) == TEMPLATE_DECL)
1995 error ("invalid use of template-name %qE without an argument list",
1996 decl);
1997 else if (!parser->scope)
1998 {
1999 /* Issue an error message. */
2000 error ("%qE does not name a type", id);
2001 /* If we're in a template class, it's possible that the user was
2002 referring to a type from a base class. For example:
2003
2004 template <typename T> struct A { typedef T X; };
2005 template <typename T> struct B : public A<T> { X x; };
2006
2007 The user should have said "typename A<T>::X". */
2008 if (processing_template_decl && current_class_type
2009 && TYPE_BINFO (current_class_type))
2010 {
2011 tree b;
2012
2013 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2014 b;
2015 b = TREE_CHAIN (b))
2016 {
2017 tree base_type = BINFO_TYPE (b);
2018 if (CLASS_TYPE_P (base_type)
2019 && dependent_type_p (base_type))
2020 {
2021 tree field;
2022 /* Go from a particular instantiation of the
2023 template (which will have an empty TYPE_FIELDs),
2024 to the main version. */
2025 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2026 for (field = TYPE_FIELDS (base_type);
2027 field;
2028 field = TREE_CHAIN (field))
2029 if (TREE_CODE (field) == TYPE_DECL
2030 && DECL_NAME (field) == id)
2031 {
2032 inform ("(perhaps %<typename %T::%E%> was intended)",
2033 BINFO_TYPE (b), id);
2034 break;
2035 }
2036 if (field)
2037 break;
2038 }
2039 }
2040 }
2041 }
2042 /* Here we diagnose qualified-ids where the scope is actually correct,
2043 but the identifier does not resolve to a valid type name. */
2044 else
2045 {
2046 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2047 error ("%qE in namespace %qE does not name a type",
2048 id, parser->scope);
2049 else if (TYPE_P (parser->scope))
2050 error ("%qE in class %qT does not name a type", id, parser->scope);
2051 else
2052 gcc_unreachable ();
2053 }
2054 cp_parser_commit_to_tentative_parse (parser);
2055 }
2056
2057 /* Check for a common situation where a type-name should be present,
2058 but is not, and issue a sensible error message. Returns true if an
2059 invalid type-name was detected.
2060
2061 The situation handled by this function are variable declarations of the
2062 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2063 Usually, `ID' should name a type, but if we got here it means that it
2064 does not. We try to emit the best possible error message depending on
2065 how exactly the id-expression looks like.
2066 */
2067
2068 static bool
2069 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2070 {
2071 tree id;
2072
2073 cp_parser_parse_tentatively (parser);
2074 id = cp_parser_id_expression (parser,
2075 /*template_keyword_p=*/false,
2076 /*check_dependency_p=*/true,
2077 /*template_p=*/NULL,
2078 /*declarator_p=*/true);
2079 /* After the id-expression, there should be a plain identifier,
2080 otherwise this is not a simple variable declaration. Also, if
2081 the scope is dependent, we cannot do much. */
2082 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2083 || (parser->scope && TYPE_P (parser->scope)
2084 && dependent_type_p (parser->scope)))
2085 {
2086 cp_parser_abort_tentative_parse (parser);
2087 return false;
2088 }
2089 if (!cp_parser_parse_definitely (parser)
2090 || TREE_CODE (id) != IDENTIFIER_NODE)
2091 return false;
2092
2093 /* Emit a diagnostic for the invalid type. */
2094 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2095 /* Skip to the end of the declaration; there's no point in
2096 trying to process it. */
2097 cp_parser_skip_to_end_of_block_or_statement (parser);
2098 return true;
2099 }
2100
2101 /* Consume tokens up to, and including, the next non-nested closing `)'.
2102 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2103 are doing error recovery. Returns -1 if OR_COMMA is true and we
2104 found an unnested comma. */
2105
2106 static int
2107 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2108 bool recovering,
2109 bool or_comma,
2110 bool consume_paren)
2111 {
2112 unsigned paren_depth = 0;
2113 unsigned brace_depth = 0;
2114 int result;
2115
2116 if (recovering && !or_comma
2117 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2118 return 0;
2119
2120 while (true)
2121 {
2122 cp_token *token;
2123
2124 /* If we've run out of tokens, then there is no closing `)'. */
2125 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2126 {
2127 result = 0;
2128 break;
2129 }
2130
2131 token = cp_lexer_peek_token (parser->lexer);
2132
2133 /* This matches the processing in skip_to_end_of_statement. */
2134 if (token->type == CPP_SEMICOLON && !brace_depth)
2135 {
2136 result = 0;
2137 break;
2138 }
2139 if (token->type == CPP_OPEN_BRACE)
2140 ++brace_depth;
2141 if (token->type == CPP_CLOSE_BRACE)
2142 {
2143 if (!brace_depth--)
2144 {
2145 result = 0;
2146 break;
2147 }
2148 }
2149 if (recovering && or_comma && token->type == CPP_COMMA
2150 && !brace_depth && !paren_depth)
2151 {
2152 result = -1;
2153 break;
2154 }
2155
2156 if (!brace_depth)
2157 {
2158 /* If it is an `(', we have entered another level of nesting. */
2159 if (token->type == CPP_OPEN_PAREN)
2160 ++paren_depth;
2161 /* If it is a `)', then we might be done. */
2162 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2163 {
2164 if (consume_paren)
2165 cp_lexer_consume_token (parser->lexer);
2166 {
2167 result = 1;
2168 break;
2169 }
2170 }
2171 }
2172
2173 /* Consume the token. */
2174 cp_lexer_consume_token (parser->lexer);
2175 }
2176
2177 return result;
2178 }
2179
2180 /* Consume tokens until we reach the end of the current statement.
2181 Normally, that will be just before consuming a `;'. However, if a
2182 non-nested `}' comes first, then we stop before consuming that. */
2183
2184 static void
2185 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2186 {
2187 unsigned nesting_depth = 0;
2188
2189 while (true)
2190 {
2191 cp_token *token;
2192
2193 /* Peek at the next token. */
2194 token = cp_lexer_peek_token (parser->lexer);
2195 /* If we've run out of tokens, stop. */
2196 if (token->type == CPP_EOF)
2197 break;
2198 /* If the next token is a `;', we have reached the end of the
2199 statement. */
2200 if (token->type == CPP_SEMICOLON && !nesting_depth)
2201 break;
2202 /* If the next token is a non-nested `}', then we have reached
2203 the end of the current block. */
2204 if (token->type == CPP_CLOSE_BRACE)
2205 {
2206 /* If this is a non-nested `}', stop before consuming it.
2207 That way, when confronted with something like:
2208
2209 { 3 + }
2210
2211 we stop before consuming the closing `}', even though we
2212 have not yet reached a `;'. */
2213 if (nesting_depth == 0)
2214 break;
2215 /* If it is the closing `}' for a block that we have
2216 scanned, stop -- but only after consuming the token.
2217 That way given:
2218
2219 void f g () { ... }
2220 typedef int I;
2221
2222 we will stop after the body of the erroneously declared
2223 function, but before consuming the following `typedef'
2224 declaration. */
2225 if (--nesting_depth == 0)
2226 {
2227 cp_lexer_consume_token (parser->lexer);
2228 break;
2229 }
2230 }
2231 /* If it the next token is a `{', then we are entering a new
2232 block. Consume the entire block. */
2233 else if (token->type == CPP_OPEN_BRACE)
2234 ++nesting_depth;
2235 /* Consume the token. */
2236 cp_lexer_consume_token (parser->lexer);
2237 }
2238 }
2239
2240 /* This function is called at the end of a statement or declaration.
2241 If the next token is a semicolon, it is consumed; otherwise, error
2242 recovery is attempted. */
2243
2244 static void
2245 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2246 {
2247 /* Look for the trailing `;'. */
2248 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2249 {
2250 /* If there is additional (erroneous) input, skip to the end of
2251 the statement. */
2252 cp_parser_skip_to_end_of_statement (parser);
2253 /* If the next token is now a `;', consume it. */
2254 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2255 cp_lexer_consume_token (parser->lexer);
2256 }
2257 }
2258
2259 /* Skip tokens until we have consumed an entire block, or until we
2260 have consumed a non-nested `;'. */
2261
2262 static void
2263 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2264 {
2265 unsigned nesting_depth = 0;
2266
2267 while (true)
2268 {
2269 cp_token *token;
2270
2271 /* Peek at the next token. */
2272 token = cp_lexer_peek_token (parser->lexer);
2273 /* If we've run out of tokens, stop. */
2274 if (token->type == CPP_EOF)
2275 break;
2276 /* If the next token is a `;', we have reached the end of the
2277 statement. */
2278 if (token->type == CPP_SEMICOLON && !nesting_depth)
2279 {
2280 /* Consume the `;'. */
2281 cp_lexer_consume_token (parser->lexer);
2282 break;
2283 }
2284 /* Consume the token. */
2285 token = cp_lexer_consume_token (parser->lexer);
2286 /* If the next token is a non-nested `}', then we have reached
2287 the end of the current block. */
2288 if (token->type == CPP_CLOSE_BRACE
2289 && (nesting_depth == 0 || --nesting_depth == 0))
2290 break;
2291 /* If it the next token is a `{', then we are entering a new
2292 block. Consume the entire block. */
2293 if (token->type == CPP_OPEN_BRACE)
2294 ++nesting_depth;
2295 }
2296 }
2297
2298 /* Skip tokens until a non-nested closing curly brace is the next
2299 token. */
2300
2301 static void
2302 cp_parser_skip_to_closing_brace (cp_parser *parser)
2303 {
2304 unsigned nesting_depth = 0;
2305
2306 while (true)
2307 {
2308 cp_token *token;
2309
2310 /* Peek at the next token. */
2311 token = cp_lexer_peek_token (parser->lexer);
2312 /* If we've run out of tokens, stop. */
2313 if (token->type == CPP_EOF)
2314 break;
2315 /* If the next token is a non-nested `}', then we have reached
2316 the end of the current block. */
2317 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2318 break;
2319 /* If it the next token is a `{', then we are entering a new
2320 block. Consume the entire block. */
2321 else if (token->type == CPP_OPEN_BRACE)
2322 ++nesting_depth;
2323 /* Consume the token. */
2324 cp_lexer_consume_token (parser->lexer);
2325 }
2326 }
2327
2328 /* This is a simple wrapper around make_typename_type. When the id is
2329 an unresolved identifier node, we can provide a superior diagnostic
2330 using cp_parser_diagnose_invalid_type_name. */
2331
2332 static tree
2333 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2334 {
2335 tree result;
2336 if (TREE_CODE (id) == IDENTIFIER_NODE)
2337 {
2338 result = make_typename_type (scope, id, typename_type,
2339 /*complain=*/0);
2340 if (result == error_mark_node)
2341 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2342 return result;
2343 }
2344 return make_typename_type (scope, id, typename_type, tf_error);
2345 }
2346
2347
2348 /* Create a new C++ parser. */
2349
2350 static cp_parser *
2351 cp_parser_new (void)
2352 {
2353 cp_parser *parser;
2354 cp_lexer *lexer;
2355 unsigned i;
2356
2357 /* cp_lexer_new_main is called before calling ggc_alloc because
2358 cp_lexer_new_main might load a PCH file. */
2359 lexer = cp_lexer_new_main ();
2360
2361 /* Initialize the binops_by_token so that we can get the tree
2362 directly from the token. */
2363 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2364 binops_by_token[binops[i].token_type] = binops[i];
2365
2366 parser = GGC_CNEW (cp_parser);
2367 parser->lexer = lexer;
2368 parser->context = cp_parser_context_new (NULL);
2369
2370 /* For now, we always accept GNU extensions. */
2371 parser->allow_gnu_extensions_p = 1;
2372
2373 /* The `>' token is a greater-than operator, not the end of a
2374 template-id. */
2375 parser->greater_than_is_operator_p = true;
2376
2377 parser->default_arg_ok_p = true;
2378
2379 /* We are not parsing a constant-expression. */
2380 parser->integral_constant_expression_p = false;
2381 parser->allow_non_integral_constant_expression_p = false;
2382 parser->non_integral_constant_expression_p = false;
2383
2384 /* Local variable names are not forbidden. */
2385 parser->local_variables_forbidden_p = false;
2386
2387 /* We are not processing an `extern "C"' declaration. */
2388 parser->in_unbraced_linkage_specification_p = false;
2389
2390 /* We are not processing a declarator. */
2391 parser->in_declarator_p = false;
2392
2393 /* We are not processing a template-argument-list. */
2394 parser->in_template_argument_list_p = false;
2395
2396 /* We are not in an iteration statement. */
2397 parser->in_iteration_statement_p = false;
2398
2399 /* We are not in a switch statement. */
2400 parser->in_switch_statement_p = false;
2401
2402 /* We are not parsing a type-id inside an expression. */
2403 parser->in_type_id_in_expr_p = false;
2404
2405 /* Declarations aren't implicitly extern "C". */
2406 parser->implicit_extern_c = false;
2407
2408 /* String literals should be translated to the execution character set. */
2409 parser->translate_strings_p = true;
2410
2411 /* The unparsed function queue is empty. */
2412 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2413
2414 /* There are no classes being defined. */
2415 parser->num_classes_being_defined = 0;
2416
2417 /* No template parameters apply. */
2418 parser->num_template_parameter_lists = 0;
2419
2420 return parser;
2421 }
2422
2423 /* Create a cp_lexer structure which will emit the tokens in CACHE
2424 and push it onto the parser's lexer stack. This is used for delayed
2425 parsing of in-class method bodies and default arguments, and should
2426 not be confused with tentative parsing. */
2427 static void
2428 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2429 {
2430 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2431 lexer->next = parser->lexer;
2432 parser->lexer = lexer;
2433
2434 /* Move the current source position to that of the first token in the
2435 new lexer. */
2436 cp_lexer_set_source_position_from_token (lexer->next_token);
2437 }
2438
2439 /* Pop the top lexer off the parser stack. This is never used for the
2440 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2441 static void
2442 cp_parser_pop_lexer (cp_parser *parser)
2443 {
2444 cp_lexer *lexer = parser->lexer;
2445 parser->lexer = lexer->next;
2446 cp_lexer_destroy (lexer);
2447
2448 /* Put the current source position back where it was before this
2449 lexer was pushed. */
2450 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2451 }
2452
2453 /* Lexical conventions [gram.lex] */
2454
2455 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2456 identifier. */
2457
2458 static tree
2459 cp_parser_identifier (cp_parser* parser)
2460 {
2461 cp_token *token;
2462
2463 /* Look for the identifier. */
2464 token = cp_parser_require (parser, CPP_NAME, "identifier");
2465 /* Return the value. */
2466 return token ? token->value : error_mark_node;
2467 }
2468
2469 /* Parse a sequence of adjacent string constants. Returns a
2470 TREE_STRING representing the combined, nul-terminated string
2471 constant. If TRANSLATE is true, translate the string to the
2472 execution character set. If WIDE_OK is true, a wide string is
2473 invalid here.
2474
2475 C++98 [lex.string] says that if a narrow string literal token is
2476 adjacent to a wide string literal token, the behavior is undefined.
2477 However, C99 6.4.5p4 says that this results in a wide string literal.
2478 We follow C99 here, for consistency with the C front end.
2479
2480 This code is largely lifted from lex_string() in c-lex.c.
2481
2482 FUTURE: ObjC++ will need to handle @-strings here. */
2483 static tree
2484 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2485 {
2486 tree value;
2487 bool wide = false;
2488 size_t count;
2489 struct obstack str_ob;
2490 cpp_string str, istr, *strs;
2491 cp_token *tok;
2492
2493 tok = cp_lexer_peek_token (parser->lexer);
2494 if (!cp_parser_is_string_literal (tok))
2495 {
2496 cp_parser_error (parser, "expected string-literal");
2497 return error_mark_node;
2498 }
2499
2500 /* Try to avoid the overhead of creating and destroying an obstack
2501 for the common case of just one string. */
2502 if (!cp_parser_is_string_literal
2503 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2504 {
2505 cp_lexer_consume_token (parser->lexer);
2506
2507 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2508 str.len = TREE_STRING_LENGTH (tok->value);
2509 count = 1;
2510 if (tok->type == CPP_WSTRING)
2511 wide = true;
2512
2513 strs = &str;
2514 }
2515 else
2516 {
2517 gcc_obstack_init (&str_ob);
2518 count = 0;
2519
2520 do
2521 {
2522 cp_lexer_consume_token (parser->lexer);
2523 count++;
2524 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2525 str.len = TREE_STRING_LENGTH (tok->value);
2526 if (tok->type == CPP_WSTRING)
2527 wide = true;
2528
2529 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2530
2531 tok = cp_lexer_peek_token (parser->lexer);
2532 }
2533 while (cp_parser_is_string_literal (tok));
2534
2535 strs = (cpp_string *) obstack_finish (&str_ob);
2536 }
2537
2538 if (wide && !wide_ok)
2539 {
2540 cp_parser_error (parser, "a wide string is invalid in this context");
2541 wide = false;
2542 }
2543
2544 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2545 (parse_in, strs, count, &istr, wide))
2546 {
2547 value = build_string (istr.len, (char *)istr.text);
2548 free ((void *)istr.text);
2549
2550 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2551 value = fix_string_type (value);
2552 }
2553 else
2554 /* cpp_interpret_string has issued an error. */
2555 value = error_mark_node;
2556
2557 if (count > 1)
2558 obstack_free (&str_ob, 0);
2559
2560 return value;
2561 }
2562
2563
2564 /* Basic concepts [gram.basic] */
2565
2566 /* Parse a translation-unit.
2567
2568 translation-unit:
2569 declaration-seq [opt]
2570
2571 Returns TRUE if all went well. */
2572
2573 static bool
2574 cp_parser_translation_unit (cp_parser* parser)
2575 {
2576 /* The address of the first non-permanent object on the declarator
2577 obstack. */
2578 static void *declarator_obstack_base;
2579
2580 bool success;
2581
2582 /* Create the declarator obstack, if necessary. */
2583 if (!cp_error_declarator)
2584 {
2585 gcc_obstack_init (&declarator_obstack);
2586 /* Create the error declarator. */
2587 cp_error_declarator = make_declarator (cdk_error);
2588 /* Create the empty parameter list. */
2589 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2590 /* Remember where the base of the declarator obstack lies. */
2591 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2592 }
2593
2594 while (true)
2595 {
2596 cp_parser_declaration_seq_opt (parser);
2597
2598 /* If there are no tokens left then all went well. */
2599 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2600 {
2601 /* Get rid of the token array; we don't need it any more. */
2602 cp_lexer_destroy (parser->lexer);
2603 parser->lexer = NULL;
2604
2605 /* This file might have been a context that's implicitly extern
2606 "C". If so, pop the lang context. (Only relevant for PCH.) */
2607 if (parser->implicit_extern_c)
2608 {
2609 pop_lang_context ();
2610 parser->implicit_extern_c = false;
2611 }
2612
2613 /* Finish up. */
2614 finish_translation_unit ();
2615
2616 success = true;
2617 break;
2618 }
2619 else
2620 {
2621 cp_parser_error (parser, "expected declaration");
2622 success = false;
2623 break;
2624 }
2625 }
2626
2627 /* Make sure the declarator obstack was fully cleaned up. */
2628 gcc_assert (obstack_next_free (&declarator_obstack)
2629 == declarator_obstack_base);
2630
2631 /* All went well. */
2632 return success;
2633 }
2634
2635 /* Expressions [gram.expr] */
2636
2637 /* Parse a primary-expression.
2638
2639 primary-expression:
2640 literal
2641 this
2642 ( expression )
2643 id-expression
2644
2645 GNU Extensions:
2646
2647 primary-expression:
2648 ( compound-statement )
2649 __builtin_va_arg ( assignment-expression , type-id )
2650
2651 literal:
2652 __null
2653
2654 CAST_P is true if this primary expression is the target of a cast.
2655
2656 Returns a representation of the expression.
2657
2658 *IDK indicates what kind of id-expression (if any) was present.
2659
2660 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2661 used as the operand of a pointer-to-member. In that case,
2662 *QUALIFYING_CLASS gives the class that is used as the qualifying
2663 class in the pointer-to-member. */
2664
2665 static tree
2666 cp_parser_primary_expression (cp_parser *parser,
2667 bool cast_p,
2668 cp_id_kind *idk,
2669 tree *qualifying_class)
2670 {
2671 cp_token *token;
2672
2673 /* Assume the primary expression is not an id-expression. */
2674 *idk = CP_ID_KIND_NONE;
2675 /* And that it cannot be used as pointer-to-member. */
2676 *qualifying_class = NULL_TREE;
2677
2678 /* Peek at the next token. */
2679 token = cp_lexer_peek_token (parser->lexer);
2680 switch (token->type)
2681 {
2682 /* literal:
2683 integer-literal
2684 character-literal
2685 floating-literal
2686 string-literal
2687 boolean-literal */
2688 case CPP_CHAR:
2689 case CPP_WCHAR:
2690 case CPP_NUMBER:
2691 token = cp_lexer_consume_token (parser->lexer);
2692 /* Floating-point literals are only allowed in an integral
2693 constant expression if they are cast to an integral or
2694 enumeration type. */
2695 if (TREE_CODE (token->value) == REAL_CST
2696 && parser->integral_constant_expression_p
2697 && pedantic)
2698 {
2699 /* CAST_P will be set even in invalid code like "int(2.7 +
2700 ...)". Therefore, we have to check that the next token
2701 is sure to end the cast. */
2702 if (cast_p)
2703 {
2704 cp_token *next_token;
2705
2706 next_token = cp_lexer_peek_token (parser->lexer);
2707 if (/* The comma at the end of an
2708 enumerator-definition. */
2709 next_token->type != CPP_COMMA
2710 /* The curly brace at the end of an enum-specifier. */
2711 && next_token->type != CPP_CLOSE_BRACE
2712 /* The end of a statement. */
2713 && next_token->type != CPP_SEMICOLON
2714 /* The end of the cast-expression. */
2715 && next_token->type != CPP_CLOSE_PAREN
2716 /* The end of an array bound. */
2717 && next_token->type != CPP_CLOSE_SQUARE)
2718 cast_p = false;
2719 }
2720
2721 /* If we are within a cast, then the constraint that the
2722 cast is to an integral or enumeration type will be
2723 checked at that point. If we are not within a cast, then
2724 this code is invalid. */
2725 if (!cast_p)
2726 cp_parser_non_integral_constant_expression
2727 (parser, "floating-point literal");
2728 }
2729 return token->value;
2730
2731 case CPP_STRING:
2732 case CPP_WSTRING:
2733 /* ??? Should wide strings be allowed when parser->translate_strings_p
2734 is false (i.e. in attributes)? If not, we can kill the third
2735 argument to cp_parser_string_literal. */
2736 return cp_parser_string_literal (parser,
2737 parser->translate_strings_p,
2738 true);
2739
2740 case CPP_OPEN_PAREN:
2741 {
2742 tree expr;
2743 bool saved_greater_than_is_operator_p;
2744
2745 /* Consume the `('. */
2746 cp_lexer_consume_token (parser->lexer);
2747 /* Within a parenthesized expression, a `>' token is always
2748 the greater-than operator. */
2749 saved_greater_than_is_operator_p
2750 = parser->greater_than_is_operator_p;
2751 parser->greater_than_is_operator_p = true;
2752 /* If we see `( { ' then we are looking at the beginning of
2753 a GNU statement-expression. */
2754 if (cp_parser_allow_gnu_extensions_p (parser)
2755 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2756 {
2757 /* Statement-expressions are not allowed by the standard. */
2758 if (pedantic)
2759 pedwarn ("ISO C++ forbids braced-groups within expressions");
2760
2761 /* And they're not allowed outside of a function-body; you
2762 cannot, for example, write:
2763
2764 int i = ({ int j = 3; j + 1; });
2765
2766 at class or namespace scope. */
2767 if (!at_function_scope_p ())
2768 error ("statement-expressions are allowed only inside functions");
2769 /* Start the statement-expression. */
2770 expr = begin_stmt_expr ();
2771 /* Parse the compound-statement. */
2772 cp_parser_compound_statement (parser, expr, false);
2773 /* Finish up. */
2774 expr = finish_stmt_expr (expr, false);
2775 }
2776 else
2777 {
2778 /* Parse the parenthesized expression. */
2779 expr = cp_parser_expression (parser, cast_p);
2780 /* Let the front end know that this expression was
2781 enclosed in parentheses. This matters in case, for
2782 example, the expression is of the form `A::B', since
2783 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2784 not. */
2785 finish_parenthesized_expr (expr);
2786 }
2787 /* The `>' token might be the end of a template-id or
2788 template-parameter-list now. */
2789 parser->greater_than_is_operator_p
2790 = saved_greater_than_is_operator_p;
2791 /* Consume the `)'. */
2792 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2793 cp_parser_skip_to_end_of_statement (parser);
2794
2795 return expr;
2796 }
2797
2798 case CPP_KEYWORD:
2799 switch (token->keyword)
2800 {
2801 /* These two are the boolean literals. */
2802 case RID_TRUE:
2803 cp_lexer_consume_token (parser->lexer);
2804 return boolean_true_node;
2805 case RID_FALSE:
2806 cp_lexer_consume_token (parser->lexer);
2807 return boolean_false_node;
2808
2809 /* The `__null' literal. */
2810 case RID_NULL:
2811 cp_lexer_consume_token (parser->lexer);
2812 return null_node;
2813
2814 /* Recognize the `this' keyword. */
2815 case RID_THIS:
2816 cp_lexer_consume_token (parser->lexer);
2817 if (parser->local_variables_forbidden_p)
2818 {
2819 error ("%<this%> may not be used in this context");
2820 return error_mark_node;
2821 }
2822 /* Pointers cannot appear in constant-expressions. */
2823 if (cp_parser_non_integral_constant_expression (parser,
2824 "`this'"))
2825 return error_mark_node;
2826 return finish_this_expr ();
2827
2828 /* The `operator' keyword can be the beginning of an
2829 id-expression. */
2830 case RID_OPERATOR:
2831 goto id_expression;
2832
2833 case RID_FUNCTION_NAME:
2834 case RID_PRETTY_FUNCTION_NAME:
2835 case RID_C99_FUNCTION_NAME:
2836 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2837 __func__ are the names of variables -- but they are
2838 treated specially. Therefore, they are handled here,
2839 rather than relying on the generic id-expression logic
2840 below. Grammatically, these names are id-expressions.
2841
2842 Consume the token. */
2843 token = cp_lexer_consume_token (parser->lexer);
2844 /* Look up the name. */
2845 return finish_fname (token->value);
2846
2847 case RID_VA_ARG:
2848 {
2849 tree expression;
2850 tree type;
2851
2852 /* The `__builtin_va_arg' construct is used to handle
2853 `va_arg'. Consume the `__builtin_va_arg' token. */
2854 cp_lexer_consume_token (parser->lexer);
2855 /* Look for the opening `('. */
2856 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2857 /* Now, parse the assignment-expression. */
2858 expression = cp_parser_assignment_expression (parser,
2859 /*cast_p=*/false);
2860 /* Look for the `,'. */
2861 cp_parser_require (parser, CPP_COMMA, "`,'");
2862 /* Parse the type-id. */
2863 type = cp_parser_type_id (parser);
2864 /* Look for the closing `)'. */
2865 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2866 /* Using `va_arg' in a constant-expression is not
2867 allowed. */
2868 if (cp_parser_non_integral_constant_expression (parser,
2869 "`va_arg'"))
2870 return error_mark_node;
2871 return build_x_va_arg (expression, type);
2872 }
2873
2874 case RID_OFFSETOF:
2875 return cp_parser_builtin_offsetof (parser);
2876
2877 default:
2878 cp_parser_error (parser, "expected primary-expression");
2879 return error_mark_node;
2880 }
2881
2882 /* An id-expression can start with either an identifier, a
2883 `::' as the beginning of a qualified-id, or the "operator"
2884 keyword. */
2885 case CPP_NAME:
2886 case CPP_SCOPE:
2887 case CPP_TEMPLATE_ID:
2888 case CPP_NESTED_NAME_SPECIFIER:
2889 {
2890 tree id_expression;
2891 tree decl;
2892 const char *error_msg;
2893
2894 id_expression:
2895 /* Parse the id-expression. */
2896 id_expression
2897 = cp_parser_id_expression (parser,
2898 /*template_keyword_p=*/false,
2899 /*check_dependency_p=*/true,
2900 /*template_p=*/NULL,
2901 /*declarator_p=*/false);
2902 if (id_expression == error_mark_node)
2903 return error_mark_node;
2904 /* If we have a template-id, then no further lookup is
2905 required. If the template-id was for a template-class, we
2906 will sometimes have a TYPE_DECL at this point. */
2907 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2908 || TREE_CODE (id_expression) == TYPE_DECL)
2909 decl = id_expression;
2910 /* Look up the name. */
2911 else
2912 {
2913 bool ambiguous_p;
2914
2915 decl = cp_parser_lookup_name (parser, id_expression,
2916 none_type,
2917 /*is_template=*/false,
2918 /*is_namespace=*/false,
2919 /*check_dependency=*/true,
2920 &ambiguous_p);
2921 /* If the lookup was ambiguous, an error will already have
2922 been issued. */
2923 if (ambiguous_p)
2924 return error_mark_node;
2925 /* If name lookup gives us a SCOPE_REF, then the
2926 qualifying scope was dependent. Just propagate the
2927 name. */
2928 if (TREE_CODE (decl) == SCOPE_REF)
2929 {
2930 if (TYPE_P (TREE_OPERAND (decl, 0)))
2931 *qualifying_class = TREE_OPERAND (decl, 0);
2932 return decl;
2933 }
2934 /* Check to see if DECL is a local variable in a context
2935 where that is forbidden. */
2936 if (parser->local_variables_forbidden_p
2937 && local_variable_p (decl))
2938 {
2939 /* It might be that we only found DECL because we are
2940 trying to be generous with pre-ISO scoping rules.
2941 For example, consider:
2942
2943 int i;
2944 void g() {
2945 for (int i = 0; i < 10; ++i) {}
2946 extern void f(int j = i);
2947 }
2948
2949 Here, name look up will originally find the out
2950 of scope `i'. We need to issue a warning message,
2951 but then use the global `i'. */
2952 decl = check_for_out_of_scope_variable (decl);
2953 if (local_variable_p (decl))
2954 {
2955 error ("local variable %qD may not appear in this context",
2956 decl);
2957 return error_mark_node;
2958 }
2959 }
2960 }
2961
2962 decl = finish_id_expression (id_expression, decl, parser->scope,
2963 idk, qualifying_class,
2964 parser->integral_constant_expression_p,
2965 parser->allow_non_integral_constant_expression_p,
2966 &parser->non_integral_constant_expression_p,
2967 &error_msg);
2968 if (error_msg)
2969 cp_parser_error (parser, error_msg);
2970 return decl;
2971 }
2972
2973 /* Anything else is an error. */
2974 default:
2975 cp_parser_error (parser, "expected primary-expression");
2976 return error_mark_node;
2977 }
2978 }
2979
2980 /* Parse an id-expression.
2981
2982 id-expression:
2983 unqualified-id
2984 qualified-id
2985
2986 qualified-id:
2987 :: [opt] nested-name-specifier template [opt] unqualified-id
2988 :: identifier
2989 :: operator-function-id
2990 :: template-id
2991
2992 Return a representation of the unqualified portion of the
2993 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2994 a `::' or nested-name-specifier.
2995
2996 Often, if the id-expression was a qualified-id, the caller will
2997 want to make a SCOPE_REF to represent the qualified-id. This
2998 function does not do this in order to avoid wastefully creating
2999 SCOPE_REFs when they are not required.
3000
3001 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3002 `template' keyword.
3003
3004 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3005 uninstantiated templates.
3006
3007 If *TEMPLATE_P is non-NULL, it is set to true iff the
3008 `template' keyword is used to explicitly indicate that the entity
3009 named is a template.
3010
3011 If DECLARATOR_P is true, the id-expression is appearing as part of
3012 a declarator, rather than as part of an expression. */
3013
3014 static tree
3015 cp_parser_id_expression (cp_parser *parser,
3016 bool template_keyword_p,
3017 bool check_dependency_p,
3018 bool *template_p,
3019 bool declarator_p)
3020 {
3021 bool global_scope_p;
3022 bool nested_name_specifier_p;
3023
3024 /* Assume the `template' keyword was not used. */
3025 if (template_p)
3026 *template_p = false;
3027
3028 /* Look for the optional `::' operator. */
3029 global_scope_p
3030 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3031 != NULL_TREE);
3032 /* Look for the optional nested-name-specifier. */
3033 nested_name_specifier_p
3034 = (cp_parser_nested_name_specifier_opt (parser,
3035 /*typename_keyword_p=*/false,
3036 check_dependency_p,
3037 /*type_p=*/false,
3038 declarator_p)
3039 != NULL_TREE);
3040 /* If there is a nested-name-specifier, then we are looking at
3041 the first qualified-id production. */
3042 if (nested_name_specifier_p)
3043 {
3044 tree saved_scope;
3045 tree saved_object_scope;
3046 tree saved_qualifying_scope;
3047 tree unqualified_id;
3048 bool is_template;
3049
3050 /* See if the next token is the `template' keyword. */
3051 if (!template_p)
3052 template_p = &is_template;
3053 *template_p = cp_parser_optional_template_keyword (parser);
3054 /* Name lookup we do during the processing of the
3055 unqualified-id might obliterate SCOPE. */
3056 saved_scope = parser->scope;
3057 saved_object_scope = parser->object_scope;
3058 saved_qualifying_scope = parser->qualifying_scope;
3059 /* Process the final unqualified-id. */
3060 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3061 check_dependency_p,
3062 declarator_p);
3063 /* Restore the SAVED_SCOPE for our caller. */
3064 parser->scope = saved_scope;
3065 parser->object_scope = saved_object_scope;
3066 parser->qualifying_scope = saved_qualifying_scope;
3067
3068 return unqualified_id;
3069 }
3070 /* Otherwise, if we are in global scope, then we are looking at one
3071 of the other qualified-id productions. */
3072 else if (global_scope_p)
3073 {
3074 cp_token *token;
3075 tree id;
3076
3077 /* Peek at the next token. */
3078 token = cp_lexer_peek_token (parser->lexer);
3079
3080 /* If it's an identifier, and the next token is not a "<", then
3081 we can avoid the template-id case. This is an optimization
3082 for this common case. */
3083 if (token->type == CPP_NAME
3084 && !cp_parser_nth_token_starts_template_argument_list_p
3085 (parser, 2))
3086 return cp_parser_identifier (parser);
3087
3088 cp_parser_parse_tentatively (parser);
3089 /* Try a template-id. */
3090 id = cp_parser_template_id (parser,
3091 /*template_keyword_p=*/false,
3092 /*check_dependency_p=*/true,
3093 declarator_p);
3094 /* If that worked, we're done. */
3095 if (cp_parser_parse_definitely (parser))
3096 return id;
3097
3098 /* Peek at the next token. (Changes in the token buffer may
3099 have invalidated the pointer obtained above.) */
3100 token = cp_lexer_peek_token (parser->lexer);
3101
3102 switch (token->type)
3103 {
3104 case CPP_NAME:
3105 return cp_parser_identifier (parser);
3106
3107 case CPP_KEYWORD:
3108 if (token->keyword == RID_OPERATOR)
3109 return cp_parser_operator_function_id (parser);
3110 /* Fall through. */
3111
3112 default:
3113 cp_parser_error (parser, "expected id-expression");
3114 return error_mark_node;
3115 }
3116 }
3117 else
3118 return cp_parser_unqualified_id (parser, template_keyword_p,
3119 /*check_dependency_p=*/true,
3120 declarator_p);
3121 }
3122
3123 /* Parse an unqualified-id.
3124
3125 unqualified-id:
3126 identifier
3127 operator-function-id
3128 conversion-function-id
3129 ~ class-name
3130 template-id
3131
3132 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3133 keyword, in a construct like `A::template ...'.
3134
3135 Returns a representation of unqualified-id. For the `identifier'
3136 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3137 production a BIT_NOT_EXPR is returned; the operand of the
3138 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3139 other productions, see the documentation accompanying the
3140 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3141 names are looked up in uninstantiated templates. If DECLARATOR_P
3142 is true, the unqualified-id is appearing as part of a declarator,
3143 rather than as part of an expression. */
3144
3145 static tree
3146 cp_parser_unqualified_id (cp_parser* parser,
3147 bool template_keyword_p,
3148 bool check_dependency_p,
3149 bool declarator_p)
3150 {
3151 cp_token *token;
3152
3153 /* Peek at the next token. */
3154 token = cp_lexer_peek_token (parser->lexer);
3155
3156 switch (token->type)
3157 {
3158 case CPP_NAME:
3159 {
3160 tree id;
3161
3162 /* We don't know yet whether or not this will be a
3163 template-id. */
3164 cp_parser_parse_tentatively (parser);
3165 /* Try a template-id. */
3166 id = cp_parser_template_id (parser, template_keyword_p,
3167 check_dependency_p,
3168 declarator_p);
3169 /* If it worked, we're done. */
3170 if (cp_parser_parse_definitely (parser))
3171 return id;
3172 /* Otherwise, it's an ordinary identifier. */
3173 return cp_parser_identifier (parser);
3174 }
3175
3176 case CPP_TEMPLATE_ID:
3177 return cp_parser_template_id (parser, template_keyword_p,
3178 check_dependency_p,
3179 declarator_p);
3180
3181 case CPP_COMPL:
3182 {
3183 tree type_decl;
3184 tree qualifying_scope;
3185 tree object_scope;
3186 tree scope;
3187 bool done;
3188
3189 /* Consume the `~' token. */
3190 cp_lexer_consume_token (parser->lexer);
3191 /* Parse the class-name. The standard, as written, seems to
3192 say that:
3193
3194 template <typename T> struct S { ~S (); };
3195 template <typename T> S<T>::~S() {}
3196
3197 is invalid, since `~' must be followed by a class-name, but
3198 `S<T>' is dependent, and so not known to be a class.
3199 That's not right; we need to look in uninstantiated
3200 templates. A further complication arises from:
3201
3202 template <typename T> void f(T t) {
3203 t.T::~T();
3204 }
3205
3206 Here, it is not possible to look up `T' in the scope of `T'
3207 itself. We must look in both the current scope, and the
3208 scope of the containing complete expression.
3209
3210 Yet another issue is:
3211
3212 struct S {
3213 int S;
3214 ~S();
3215 };
3216
3217 S::~S() {}
3218
3219 The standard does not seem to say that the `S' in `~S'
3220 should refer to the type `S' and not the data member
3221 `S::S'. */
3222
3223 /* DR 244 says that we look up the name after the "~" in the
3224 same scope as we looked up the qualifying name. That idea
3225 isn't fully worked out; it's more complicated than that. */
3226 scope = parser->scope;
3227 object_scope = parser->object_scope;
3228 qualifying_scope = parser->qualifying_scope;
3229
3230 /* If the name is of the form "X::~X" it's OK. */
3231 if (scope && TYPE_P (scope)
3232 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3233 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3234 == CPP_OPEN_PAREN)
3235 && (cp_lexer_peek_token (parser->lexer)->value
3236 == TYPE_IDENTIFIER (scope)))
3237 {
3238 cp_lexer_consume_token (parser->lexer);
3239 return build_nt (BIT_NOT_EXPR, scope);
3240 }
3241
3242 /* If there was an explicit qualification (S::~T), first look
3243 in the scope given by the qualification (i.e., S). */
3244 done = false;
3245 type_decl = NULL_TREE;
3246 if (scope)
3247 {
3248 cp_parser_parse_tentatively (parser);
3249 type_decl = cp_parser_class_name (parser,
3250 /*typename_keyword_p=*/false,
3251 /*template_keyword_p=*/false,
3252 none_type,
3253 /*check_dependency=*/false,
3254 /*class_head_p=*/false,
3255 declarator_p);
3256 if (cp_parser_parse_definitely (parser))
3257 done = true;
3258 }
3259 /* In "N::S::~S", look in "N" as well. */
3260 if (!done && scope && qualifying_scope)
3261 {
3262 cp_parser_parse_tentatively (parser);
3263 parser->scope = qualifying_scope;
3264 parser->object_scope = NULL_TREE;
3265 parser->qualifying_scope = NULL_TREE;
3266 type_decl
3267 = cp_parser_class_name (parser,
3268 /*typename_keyword_p=*/false,
3269 /*template_keyword_p=*/false,
3270 none_type,
3271 /*check_dependency=*/false,
3272 /*class_head_p=*/false,
3273 declarator_p);
3274 if (cp_parser_parse_definitely (parser))
3275 done = true;
3276 }
3277 /* In "p->S::~T", look in the scope given by "*p" as well. */
3278 else if (!done && object_scope)
3279 {
3280 cp_parser_parse_tentatively (parser);
3281 parser->scope = object_scope;
3282 parser->object_scope = NULL_TREE;
3283 parser->qualifying_scope = NULL_TREE;
3284 type_decl
3285 = cp_parser_class_name (parser,
3286 /*typename_keyword_p=*/false,
3287 /*template_keyword_p=*/false,
3288 none_type,
3289 /*check_dependency=*/false,
3290 /*class_head_p=*/false,
3291 declarator_p);
3292 if (cp_parser_parse_definitely (parser))
3293 done = true;
3294 }
3295 /* Look in the surrounding context. */
3296 if (!done)
3297 {
3298 parser->scope = NULL_TREE;
3299 parser->object_scope = NULL_TREE;
3300 parser->qualifying_scope = NULL_TREE;
3301 type_decl
3302 = cp_parser_class_name (parser,
3303 /*typename_keyword_p=*/false,
3304 /*template_keyword_p=*/false,
3305 none_type,
3306 /*check_dependency=*/false,
3307 /*class_head_p=*/false,
3308 declarator_p);
3309 }
3310 /* If an error occurred, assume that the name of the
3311 destructor is the same as the name of the qualifying
3312 class. That allows us to keep parsing after running
3313 into ill-formed destructor names. */
3314 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3315 return build_nt (BIT_NOT_EXPR, scope);
3316 else if (type_decl == error_mark_node)
3317 return error_mark_node;
3318
3319 /* [class.dtor]
3320
3321 A typedef-name that names a class shall not be used as the
3322 identifier in the declarator for a destructor declaration. */
3323 if (declarator_p
3324 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3325 && !DECL_SELF_REFERENCE_P (type_decl)
3326 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3327 error ("typedef-name %qD used as destructor declarator",
3328 type_decl);
3329
3330 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3331 }
3332
3333 case CPP_KEYWORD:
3334 if (token->keyword == RID_OPERATOR)
3335 {
3336 tree id;
3337
3338 /* This could be a template-id, so we try that first. */
3339 cp_parser_parse_tentatively (parser);
3340 /* Try a template-id. */
3341 id = cp_parser_template_id (parser, template_keyword_p,
3342 /*check_dependency_p=*/true,
3343 declarator_p);
3344 /* If that worked, we're done. */
3345 if (cp_parser_parse_definitely (parser))
3346 return id;
3347 /* We still don't know whether we're looking at an
3348 operator-function-id or a conversion-function-id. */
3349 cp_parser_parse_tentatively (parser);
3350 /* Try an operator-function-id. */
3351 id = cp_parser_operator_function_id (parser);
3352 /* If that didn't work, try a conversion-function-id. */
3353 if (!cp_parser_parse_definitely (parser))
3354 id = cp_parser_conversion_function_id (parser);
3355
3356 return id;
3357 }
3358 /* Fall through. */
3359
3360 default:
3361 cp_parser_error (parser, "expected unqualified-id");
3362 return error_mark_node;
3363 }
3364 }
3365
3366 /* Parse an (optional) nested-name-specifier.
3367
3368 nested-name-specifier:
3369 class-or-namespace-name :: nested-name-specifier [opt]
3370 class-or-namespace-name :: template nested-name-specifier [opt]
3371
3372 PARSER->SCOPE should be set appropriately before this function is
3373 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3374 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3375 in name lookups.
3376
3377 Sets PARSER->SCOPE to the class (TYPE) or namespace
3378 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3379 it unchanged if there is no nested-name-specifier. Returns the new
3380 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3381
3382 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3383 part of a declaration and/or decl-specifier. */
3384
3385 static tree
3386 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3387 bool typename_keyword_p,
3388 bool check_dependency_p,
3389 bool type_p,
3390 bool is_declaration)
3391 {
3392 bool success = false;
3393 tree access_check = NULL_TREE;
3394 cp_token_position start = 0;
3395 cp_token *token;
3396
3397 /* If the next token corresponds to a nested name specifier, there
3398 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3399 false, it may have been true before, in which case something
3400 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3401 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3402 CHECK_DEPENDENCY_P is false, we have to fall through into the
3403 main loop. */
3404 if (check_dependency_p
3405 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3406 {
3407 cp_parser_pre_parsed_nested_name_specifier (parser);
3408 return parser->scope;
3409 }
3410
3411 /* Remember where the nested-name-specifier starts. */
3412 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3413 start = cp_lexer_token_position (parser->lexer, false);
3414
3415 push_deferring_access_checks (dk_deferred);
3416
3417 while (true)
3418 {
3419 tree new_scope;
3420 tree old_scope;
3421 tree saved_qualifying_scope;
3422 bool template_keyword_p;
3423
3424 /* Spot cases that cannot be the beginning of a
3425 nested-name-specifier. */
3426 token = cp_lexer_peek_token (parser->lexer);
3427
3428 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3429 the already parsed nested-name-specifier. */
3430 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3431 {
3432 /* Grab the nested-name-specifier and continue the loop. */
3433 cp_parser_pre_parsed_nested_name_specifier (parser);
3434 success = true;
3435 continue;
3436 }
3437
3438 /* Spot cases that cannot be the beginning of a
3439 nested-name-specifier. On the second and subsequent times
3440 through the loop, we look for the `template' keyword. */
3441 if (success && token->keyword == RID_TEMPLATE)
3442 ;
3443 /* A template-id can start a nested-name-specifier. */
3444 else if (token->type == CPP_TEMPLATE_ID)
3445 ;
3446 else
3447 {
3448 /* If the next token is not an identifier, then it is
3449 definitely not a class-or-namespace-name. */
3450 if (token->type != CPP_NAME)
3451 break;
3452 /* If the following token is neither a `<' (to begin a
3453 template-id), nor a `::', then we are not looking at a
3454 nested-name-specifier. */
3455 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3456 if (token->type != CPP_SCOPE
3457 && !cp_parser_nth_token_starts_template_argument_list_p
3458 (parser, 2))
3459 break;
3460 }
3461
3462 /* The nested-name-specifier is optional, so we parse
3463 tentatively. */
3464 cp_parser_parse_tentatively (parser);
3465
3466 /* Look for the optional `template' keyword, if this isn't the
3467 first time through the loop. */
3468 if (success)
3469 template_keyword_p = cp_parser_optional_template_keyword (parser);
3470 else
3471 template_keyword_p = false;
3472
3473 /* Save the old scope since the name lookup we are about to do
3474 might destroy it. */
3475 old_scope = parser->scope;
3476 saved_qualifying_scope = parser->qualifying_scope;
3477 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3478 look up names in "X<T>::I" in order to determine that "Y" is
3479 a template. So, if we have a typename at this point, we make
3480 an effort to look through it. */
3481 if (is_declaration
3482 && !typename_keyword_p
3483 && parser->scope
3484 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3485 parser->scope = resolve_typename_type (parser->scope,
3486 /*only_current_p=*/false);
3487 /* Parse the qualifying entity. */
3488 new_scope
3489 = cp_parser_class_or_namespace_name (parser,
3490 typename_keyword_p,
3491 template_keyword_p,
3492 check_dependency_p,
3493 type_p,
3494 is_declaration);
3495 /* Look for the `::' token. */
3496 cp_parser_require (parser, CPP_SCOPE, "`::'");
3497
3498 /* If we found what we wanted, we keep going; otherwise, we're
3499 done. */
3500 if (!cp_parser_parse_definitely (parser))
3501 {
3502 bool error_p = false;
3503
3504 /* Restore the OLD_SCOPE since it was valid before the
3505 failed attempt at finding the last
3506 class-or-namespace-name. */
3507 parser->scope = old_scope;
3508 parser->qualifying_scope = saved_qualifying_scope;
3509 /* If the next token is an identifier, and the one after
3510 that is a `::', then any valid interpretation would have
3511 found a class-or-namespace-name. */
3512 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3513 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3514 == CPP_SCOPE)
3515 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3516 != CPP_COMPL))
3517 {
3518 token = cp_lexer_consume_token (parser->lexer);
3519 if (!error_p)
3520 {
3521 tree decl;
3522
3523 decl = cp_parser_lookup_name_simple (parser, token->value);
3524 if (TREE_CODE (decl) == TEMPLATE_DECL)
3525 error ("%qD used without template parameters", decl);
3526 else
3527 cp_parser_name_lookup_error
3528 (parser, token->value, decl,
3529 "is not a class or namespace");
3530 parser->scope = NULL_TREE;
3531 error_p = true;
3532 /* Treat this as a successful nested-name-specifier
3533 due to:
3534
3535 [basic.lookup.qual]
3536
3537 If the name found is not a class-name (clause
3538 _class_) or namespace-name (_namespace.def_), the
3539 program is ill-formed. */
3540 success = true;
3541 }
3542 cp_lexer_consume_token (parser->lexer);
3543 }
3544 break;
3545 }
3546
3547 /* We've found one valid nested-name-specifier. */
3548 success = true;
3549 /* Make sure we look in the right scope the next time through
3550 the loop. */
3551 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3552 ? TREE_TYPE (new_scope)
3553 : new_scope);
3554 /* If it is a class scope, try to complete it; we are about to
3555 be looking up names inside the class. */
3556 if (TYPE_P (parser->scope)
3557 /* Since checking types for dependency can be expensive,
3558 avoid doing it if the type is already complete. */
3559 && !COMPLETE_TYPE_P (parser->scope)
3560 /* Do not try to complete dependent types. */
3561 && !dependent_type_p (parser->scope))
3562 complete_type (parser->scope);
3563 }
3564
3565 /* Retrieve any deferred checks. Do not pop this access checks yet
3566 so the memory will not be reclaimed during token replacing below. */
3567 access_check = get_deferred_access_checks ();
3568
3569 /* If parsing tentatively, replace the sequence of tokens that makes
3570 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3571 token. That way, should we re-parse the token stream, we will
3572 not have to repeat the effort required to do the parse, nor will
3573 we issue duplicate error messages. */
3574 if (success && start)
3575 {
3576 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3577
3578 /* Reset the contents of the START token. */
3579 token->type = CPP_NESTED_NAME_SPECIFIER;
3580 token->value = build_tree_list (access_check, parser->scope);
3581 TREE_TYPE (token->value) = parser->qualifying_scope;
3582 token->keyword = RID_MAX;
3583
3584 /* Purge all subsequent tokens. */
3585 cp_lexer_purge_tokens_after (parser->lexer, start);
3586 }
3587
3588 pop_deferring_access_checks ();
3589 return success ? parser->scope : NULL_TREE;
3590 }
3591
3592 /* Parse a nested-name-specifier. See
3593 cp_parser_nested_name_specifier_opt for details. This function
3594 behaves identically, except that it will an issue an error if no
3595 nested-name-specifier is present, and it will return
3596 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3597 is present. */
3598
3599 static tree
3600 cp_parser_nested_name_specifier (cp_parser *parser,
3601 bool typename_keyword_p,
3602 bool check_dependency_p,
3603 bool type_p,
3604 bool is_declaration)
3605 {
3606 tree scope;
3607
3608 /* Look for the nested-name-specifier. */
3609 scope = cp_parser_nested_name_specifier_opt (parser,
3610 typename_keyword_p,
3611 check_dependency_p,
3612 type_p,
3613 is_declaration);
3614 /* If it was not present, issue an error message. */
3615 if (!scope)
3616 {
3617 cp_parser_error (parser, "expected nested-name-specifier");
3618 parser->scope = NULL_TREE;
3619 return error_mark_node;
3620 }
3621
3622 return scope;
3623 }
3624
3625 /* Parse a class-or-namespace-name.
3626
3627 class-or-namespace-name:
3628 class-name
3629 namespace-name
3630
3631 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3632 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3633 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3634 TYPE_P is TRUE iff the next name should be taken as a class-name,
3635 even the same name is declared to be another entity in the same
3636 scope.
3637
3638 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3639 specified by the class-or-namespace-name. If neither is found the
3640 ERROR_MARK_NODE is returned. */
3641
3642 static tree
3643 cp_parser_class_or_namespace_name (cp_parser *parser,
3644 bool typename_keyword_p,
3645 bool template_keyword_p,
3646 bool check_dependency_p,
3647 bool type_p,
3648 bool is_declaration)
3649 {
3650 tree saved_scope;
3651 tree saved_qualifying_scope;
3652 tree saved_object_scope;
3653 tree scope;
3654 bool only_class_p;
3655
3656 /* Before we try to parse the class-name, we must save away the
3657 current PARSER->SCOPE since cp_parser_class_name will destroy
3658 it. */
3659 saved_scope = parser->scope;
3660 saved_qualifying_scope = parser->qualifying_scope;
3661 saved_object_scope = parser->object_scope;
3662 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3663 there is no need to look for a namespace-name. */
3664 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3665 if (!only_class_p)
3666 cp_parser_parse_tentatively (parser);
3667 scope = cp_parser_class_name (parser,
3668 typename_keyword_p,
3669 template_keyword_p,
3670 type_p ? class_type : none_type,
3671 check_dependency_p,
3672 /*class_head_p=*/false,
3673 is_declaration);
3674 /* If that didn't work, try for a namespace-name. */
3675 if (!only_class_p && !cp_parser_parse_definitely (parser))
3676 {
3677 /* Restore the saved scope. */
3678 parser->scope = saved_scope;
3679 parser->qualifying_scope = saved_qualifying_scope;
3680 parser->object_scope = saved_object_scope;
3681 /* If we are not looking at an identifier followed by the scope
3682 resolution operator, then this is not part of a
3683 nested-name-specifier. (Note that this function is only used
3684 to parse the components of a nested-name-specifier.) */
3685 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3686 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3687 return error_mark_node;
3688 scope = cp_parser_namespace_name (parser);
3689 }
3690
3691 return scope;
3692 }
3693
3694 /* Parse a postfix-expression.
3695
3696 postfix-expression:
3697 primary-expression
3698 postfix-expression [ expression ]
3699 postfix-expression ( expression-list [opt] )
3700 simple-type-specifier ( expression-list [opt] )
3701 typename :: [opt] nested-name-specifier identifier
3702 ( expression-list [opt] )
3703 typename :: [opt] nested-name-specifier template [opt] template-id
3704 ( expression-list [opt] )
3705 postfix-expression . template [opt] id-expression
3706 postfix-expression -> template [opt] id-expression
3707 postfix-expression . pseudo-destructor-name
3708 postfix-expression -> pseudo-destructor-name
3709 postfix-expression ++
3710 postfix-expression --
3711 dynamic_cast < type-id > ( expression )
3712 static_cast < type-id > ( expression )
3713 reinterpret_cast < type-id > ( expression )
3714 const_cast < type-id > ( expression )
3715 typeid ( expression )
3716 typeid ( type-id )
3717
3718 GNU Extension:
3719
3720 postfix-expression:
3721 ( type-id ) { initializer-list , [opt] }
3722
3723 This extension is a GNU version of the C99 compound-literal
3724 construct. (The C99 grammar uses `type-name' instead of `type-id',
3725 but they are essentially the same concept.)
3726
3727 If ADDRESS_P is true, the postfix expression is the operand of the
3728 `&' operator. CAST_P is true if this expression is the target of a
3729 cast.
3730
3731 Returns a representation of the expression. */
3732
3733 static tree
3734 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3735 {
3736 cp_token *token;
3737 enum rid keyword;
3738 cp_id_kind idk = CP_ID_KIND_NONE;
3739 tree postfix_expression = NULL_TREE;
3740 /* Non-NULL only if the current postfix-expression can be used to
3741 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3742 class used to qualify the member. */
3743 tree qualifying_class = NULL_TREE;
3744
3745 /* Peek at the next token. */
3746 token = cp_lexer_peek_token (parser->lexer);
3747 /* Some of the productions are determined by keywords. */
3748 keyword = token->keyword;
3749 switch (keyword)
3750 {
3751 case RID_DYNCAST:
3752 case RID_STATCAST:
3753 case RID_REINTCAST:
3754 case RID_CONSTCAST:
3755 {
3756 tree type;
3757 tree expression;
3758 const char *saved_message;
3759
3760 /* All of these can be handled in the same way from the point
3761 of view of parsing. Begin by consuming the token
3762 identifying the cast. */
3763 cp_lexer_consume_token (parser->lexer);
3764
3765 /* New types cannot be defined in the cast. */
3766 saved_message = parser->type_definition_forbidden_message;
3767 parser->type_definition_forbidden_message
3768 = "types may not be defined in casts";
3769
3770 /* Look for the opening `<'. */
3771 cp_parser_require (parser, CPP_LESS, "`<'");
3772 /* Parse the type to which we are casting. */
3773 type = cp_parser_type_id (parser);
3774 /* Look for the closing `>'. */
3775 cp_parser_require (parser, CPP_GREATER, "`>'");
3776 /* Restore the old message. */
3777 parser->type_definition_forbidden_message = saved_message;
3778
3779 /* And the expression which is being cast. */
3780 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3781 expression = cp_parser_expression (parser, /*cast_p=*/true);
3782 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3783
3784 /* Only type conversions to integral or enumeration types
3785 can be used in constant-expressions. */
3786 if (parser->integral_constant_expression_p
3787 && !dependent_type_p (type)
3788 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3789 && (cp_parser_non_integral_constant_expression
3790 (parser,
3791 "a cast to a type other than an integral or "
3792 "enumeration type")))
3793 return error_mark_node;
3794
3795 switch (keyword)
3796 {
3797 case RID_DYNCAST:
3798 postfix_expression
3799 = build_dynamic_cast (type, expression);
3800 break;
3801 case RID_STATCAST:
3802 postfix_expression
3803 = build_static_cast (type, expression);
3804 break;
3805 case RID_REINTCAST:
3806 postfix_expression
3807 = build_reinterpret_cast (type, expression);
3808 break;
3809 case RID_CONSTCAST:
3810 postfix_expression
3811 = build_const_cast (type, expression);
3812 break;
3813 default:
3814 gcc_unreachable ();
3815 }
3816 }
3817 break;
3818
3819 case RID_TYPEID:
3820 {
3821 tree type;
3822 const char *saved_message;
3823 bool saved_in_type_id_in_expr_p;
3824
3825 /* Consume the `typeid' token. */
3826 cp_lexer_consume_token (parser->lexer);
3827 /* Look for the `(' token. */
3828 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3829 /* Types cannot be defined in a `typeid' expression. */
3830 saved_message = parser->type_definition_forbidden_message;
3831 parser->type_definition_forbidden_message
3832 = "types may not be defined in a `typeid\' expression";
3833 /* We can't be sure yet whether we're looking at a type-id or an
3834 expression. */
3835 cp_parser_parse_tentatively (parser);
3836 /* Try a type-id first. */
3837 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3838 parser->in_type_id_in_expr_p = true;
3839 type = cp_parser_type_id (parser);
3840 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3841 /* Look for the `)' token. Otherwise, we can't be sure that
3842 we're not looking at an expression: consider `typeid (int
3843 (3))', for example. */
3844 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3845 /* If all went well, simply lookup the type-id. */
3846 if (cp_parser_parse_definitely (parser))
3847 postfix_expression = get_typeid (type);
3848 /* Otherwise, fall back to the expression variant. */
3849 else
3850 {
3851 tree expression;
3852
3853 /* Look for an expression. */
3854 expression = cp_parser_expression (parser, /*cast_p=*/false);
3855 /* Compute its typeid. */
3856 postfix_expression = build_typeid (expression);
3857 /* Look for the `)' token. */
3858 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3859 }
3860 /* `typeid' may not appear in an integral constant expression. */
3861 if (cp_parser_non_integral_constant_expression(parser,
3862 "`typeid' operator"))
3863 return error_mark_node;
3864 /* Restore the saved message. */
3865 parser->type_definition_forbidden_message = saved_message;
3866 }
3867 break;
3868
3869 case RID_TYPENAME:
3870 {
3871 bool template_p = false;
3872 tree id;
3873 tree type;
3874 tree scope;
3875
3876 /* Consume the `typename' token. */
3877 cp_lexer_consume_token (parser->lexer);
3878 /* Look for the optional `::' operator. */
3879 cp_parser_global_scope_opt (parser,
3880 /*current_scope_valid_p=*/false);
3881 /* Look for the nested-name-specifier. In case of error here,
3882 consume the trailing id to avoid subsequent error messages
3883 for usual cases. */
3884 scope = cp_parser_nested_name_specifier (parser,
3885 /*typename_keyword_p=*/true,
3886 /*check_dependency_p=*/true,
3887 /*type_p=*/true,
3888 /*is_declaration=*/true);
3889
3890 /* Look for the optional `template' keyword. */
3891 template_p = cp_parser_optional_template_keyword (parser);
3892 /* We don't know whether we're looking at a template-id or an
3893 identifier. */
3894 cp_parser_parse_tentatively (parser);
3895 /* Try a template-id. */
3896 id = cp_parser_template_id (parser, template_p,
3897 /*check_dependency_p=*/true,
3898 /*is_declaration=*/true);
3899 /* If that didn't work, try an identifier. */
3900 if (!cp_parser_parse_definitely (parser))
3901 id = cp_parser_identifier (parser);
3902
3903 /* Don't process id if nested name specifier is invalid. */
3904 if (scope == error_mark_node)
3905 return error_mark_node;
3906 /* If we look up a template-id in a non-dependent qualifying
3907 scope, there's no need to create a dependent type. */
3908 else if (TREE_CODE (id) == TYPE_DECL
3909 && !dependent_type_p (parser->scope))
3910 type = TREE_TYPE (id);
3911 /* Create a TYPENAME_TYPE to represent the type to which the
3912 functional cast is being performed. */
3913 else
3914 type = make_typename_type (parser->scope, id,
3915 typename_type,
3916 /*complain=*/1);
3917
3918 postfix_expression = cp_parser_functional_cast (parser, type);
3919 }
3920 break;
3921
3922 default:
3923 {
3924 tree type;
3925
3926 /* If the next thing is a simple-type-specifier, we may be
3927 looking at a functional cast. We could also be looking at
3928 an id-expression. So, we try the functional cast, and if
3929 that doesn't work we fall back to the primary-expression. */
3930 cp_parser_parse_tentatively (parser);
3931 /* Look for the simple-type-specifier. */
3932 type = cp_parser_simple_type_specifier (parser,
3933 /*decl_specs=*/NULL,
3934 CP_PARSER_FLAGS_NONE);
3935 /* Parse the cast itself. */
3936 if (!cp_parser_error_occurred (parser))
3937 postfix_expression
3938 = cp_parser_functional_cast (parser, type);
3939 /* If that worked, we're done. */
3940 if (cp_parser_parse_definitely (parser))
3941 break;
3942
3943 /* If the functional-cast didn't work out, try a
3944 compound-literal. */
3945 if (cp_parser_allow_gnu_extensions_p (parser)
3946 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3947 {
3948 tree initializer_list = NULL_TREE;
3949 bool saved_in_type_id_in_expr_p;
3950
3951 cp_parser_parse_tentatively (parser);
3952 /* Consume the `('. */
3953 cp_lexer_consume_token (parser->lexer);
3954 /* Parse the type. */
3955 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3956 parser->in_type_id_in_expr_p = true;
3957 type = cp_parser_type_id (parser);
3958 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3959 /* Look for the `)'. */
3960 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3961 /* Look for the `{'. */
3962 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3963 /* If things aren't going well, there's no need to
3964 keep going. */
3965 if (!cp_parser_error_occurred (parser))
3966 {
3967 bool non_constant_p;
3968 /* Parse the initializer-list. */
3969 initializer_list
3970 = cp_parser_initializer_list (parser, &non_constant_p);
3971 /* Allow a trailing `,'. */
3972 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3973 cp_lexer_consume_token (parser->lexer);
3974 /* Look for the final `}'. */
3975 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3976 }
3977 /* If that worked, we're definitely looking at a
3978 compound-literal expression. */
3979 if (cp_parser_parse_definitely (parser))
3980 {
3981 /* Warn the user that a compound literal is not
3982 allowed in standard C++. */
3983 if (pedantic)
3984 pedwarn ("ISO C++ forbids compound-literals");
3985 /* Form the representation of the compound-literal. */
3986 postfix_expression
3987 = finish_compound_literal (type, initializer_list);
3988 break;
3989 }
3990 }
3991
3992 /* It must be a primary-expression. */
3993 postfix_expression = cp_parser_primary_expression (parser,
3994 cast_p,
3995 &idk,
3996 &qualifying_class);
3997 }
3998 break;
3999 }
4000
4001 /* If we were avoiding committing to the processing of a
4002 qualified-id until we knew whether or not we had a
4003 pointer-to-member, we now know. */
4004 if (qualifying_class)
4005 {
4006 bool done;
4007
4008 /* Peek at the next token. */
4009 token = cp_lexer_peek_token (parser->lexer);
4010 done = (token->type != CPP_OPEN_SQUARE
4011 && token->type != CPP_OPEN_PAREN
4012 && token->type != CPP_DOT
4013 && token->type != CPP_DEREF
4014 && token->type != CPP_PLUS_PLUS
4015 && token->type != CPP_MINUS_MINUS);
4016
4017 postfix_expression = finish_qualified_id_expr (qualifying_class,
4018 postfix_expression,
4019 done,
4020 address_p);
4021 if (done)
4022 return postfix_expression;
4023 }
4024
4025 /* Keep looping until the postfix-expression is complete. */
4026 while (true)
4027 {
4028 if (idk == CP_ID_KIND_UNQUALIFIED
4029 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4030 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4031 /* It is not a Koenig lookup function call. */
4032 postfix_expression
4033 = unqualified_name_lookup_error (postfix_expression);
4034
4035 /* Peek at the next token. */
4036 token = cp_lexer_peek_token (parser->lexer);
4037
4038 switch (token->type)
4039 {
4040 case CPP_OPEN_SQUARE:
4041 postfix_expression
4042 = cp_parser_postfix_open_square_expression (parser,
4043 postfix_expression,
4044 false);
4045 idk = CP_ID_KIND_NONE;
4046 break;
4047
4048 case CPP_OPEN_PAREN:
4049 /* postfix-expression ( expression-list [opt] ) */
4050 {
4051 bool koenig_p;
4052 tree args = (cp_parser_parenthesized_expression_list
4053 (parser, false,
4054 /*cast_p=*/false,
4055 /*non_constant_p=*/NULL));
4056
4057 if (args == error_mark_node)
4058 {
4059 postfix_expression = error_mark_node;
4060 break;
4061 }
4062
4063 /* Function calls are not permitted in
4064 constant-expressions. */
4065 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4066 && cp_parser_non_integral_constant_expression (parser,
4067 "a function call"))
4068 {
4069 postfix_expression = error_mark_node;
4070 break;
4071 }
4072
4073 koenig_p = false;
4074 if (idk == CP_ID_KIND_UNQUALIFIED)
4075 {
4076 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4077 {
4078 if (args)
4079 {
4080 koenig_p = true;
4081 postfix_expression
4082 = perform_koenig_lookup (postfix_expression, args);
4083 }
4084 else
4085 postfix_expression
4086 = unqualified_fn_lookup_error (postfix_expression);
4087 }
4088 /* We do not perform argument-dependent lookup if
4089 normal lookup finds a non-function, in accordance
4090 with the expected resolution of DR 218. */
4091 else if (args && is_overloaded_fn (postfix_expression))
4092 {
4093 tree fn = get_first_fn (postfix_expression);
4094
4095 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4096 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4097
4098 /* Only do argument dependent lookup if regular
4099 lookup does not find a set of member functions.
4100 [basic.lookup.koenig]/2a */
4101 if (!DECL_FUNCTION_MEMBER_P (fn))
4102 {
4103 koenig_p = true;
4104 postfix_expression
4105 = perform_koenig_lookup (postfix_expression, args);
4106 }
4107 }
4108 }
4109
4110 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4111 {
4112 tree instance = TREE_OPERAND (postfix_expression, 0);
4113 tree fn = TREE_OPERAND (postfix_expression, 1);
4114
4115 if (processing_template_decl
4116 && (type_dependent_expression_p (instance)
4117 || (!BASELINK_P (fn)
4118 && TREE_CODE (fn) != FIELD_DECL)
4119 || type_dependent_expression_p (fn)
4120 || any_type_dependent_arguments_p (args)))
4121 {
4122 postfix_expression
4123 = build_min_nt (CALL_EXPR, postfix_expression,
4124 args, NULL_TREE);
4125 break;
4126 }
4127
4128 if (BASELINK_P (fn))
4129 postfix_expression
4130 = (build_new_method_call
4131 (instance, fn, args, NULL_TREE,
4132 (idk == CP_ID_KIND_QUALIFIED
4133 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4134 else
4135 postfix_expression
4136 = finish_call_expr (postfix_expression, args,
4137 /*disallow_virtual=*/false,
4138 /*koenig_p=*/false);
4139 }
4140 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4141 || TREE_CODE (postfix_expression) == MEMBER_REF
4142 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4143 postfix_expression = (build_offset_ref_call_from_tree
4144 (postfix_expression, args));
4145 else if (idk == CP_ID_KIND_QUALIFIED)
4146 /* A call to a static class member, or a namespace-scope
4147 function. */
4148 postfix_expression
4149 = finish_call_expr (postfix_expression, args,
4150 /*disallow_virtual=*/true,
4151 koenig_p);
4152 else
4153 /* All other function calls. */
4154 postfix_expression
4155 = finish_call_expr (postfix_expression, args,
4156 /*disallow_virtual=*/false,
4157 koenig_p);
4158
4159 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4160 idk = CP_ID_KIND_NONE;
4161 }
4162 break;
4163
4164 case CPP_DOT:
4165 case CPP_DEREF:
4166 /* postfix-expression . template [opt] id-expression
4167 postfix-expression . pseudo-destructor-name
4168 postfix-expression -> template [opt] id-expression
4169 postfix-expression -> pseudo-destructor-name */
4170
4171 /* Consume the `.' or `->' operator. */
4172 cp_lexer_consume_token (parser->lexer);
4173
4174 postfix_expression
4175 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4176 postfix_expression,
4177 false, &idk);
4178 break;
4179
4180 case CPP_PLUS_PLUS:
4181 /* postfix-expression ++ */
4182 /* Consume the `++' token. */
4183 cp_lexer_consume_token (parser->lexer);
4184 /* Generate a representation for the complete expression. */
4185 postfix_expression
4186 = finish_increment_expr (postfix_expression,
4187 POSTINCREMENT_EXPR);
4188 /* Increments may not appear in constant-expressions. */
4189 if (cp_parser_non_integral_constant_expression (parser,
4190 "an increment"))
4191 postfix_expression = error_mark_node;
4192 idk = CP_ID_KIND_NONE;
4193 break;
4194
4195 case CPP_MINUS_MINUS:
4196 /* postfix-expression -- */
4197 /* Consume the `--' token. */
4198 cp_lexer_consume_token (parser->lexer);
4199 /* Generate a representation for the complete expression. */
4200 postfix_expression
4201 = finish_increment_expr (postfix_expression,
4202 POSTDECREMENT_EXPR);
4203 /* Decrements may not appear in constant-expressions. */
4204 if (cp_parser_non_integral_constant_expression (parser,
4205 "a decrement"))
4206 postfix_expression = error_mark_node;
4207 idk = CP_ID_KIND_NONE;
4208 break;
4209
4210 default:
4211 return postfix_expression;
4212 }
4213 }
4214
4215 /* We should never get here. */
4216 gcc_unreachable ();
4217 return error_mark_node;
4218 }
4219
4220 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4221 by cp_parser_builtin_offsetof. We're looking for
4222
4223 postfix-expression [ expression ]
4224
4225 FOR_OFFSETOF is set if we're being called in that context, which
4226 changes how we deal with integer constant expressions. */
4227
4228 static tree
4229 cp_parser_postfix_open_square_expression (cp_parser *parser,
4230 tree postfix_expression,
4231 bool for_offsetof)
4232 {
4233 tree index;
4234
4235 /* Consume the `[' token. */
4236 cp_lexer_consume_token (parser->lexer);
4237
4238 /* Parse the index expression. */
4239 /* ??? For offsetof, there is a question of what to allow here. If
4240 offsetof is not being used in an integral constant expression context,
4241 then we *could* get the right answer by computing the value at runtime.
4242 If we are in an integral constant expression context, then we might
4243 could accept any constant expression; hard to say without analysis.
4244 Rather than open the barn door too wide right away, allow only integer
4245 constant expressions here. */
4246 if (for_offsetof)
4247 index = cp_parser_constant_expression (parser, false, NULL);
4248 else
4249 index = cp_parser_expression (parser, /*cast_p=*/false);
4250
4251 /* Look for the closing `]'. */
4252 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4253
4254 /* Build the ARRAY_REF. */
4255 postfix_expression = grok_array_decl (postfix_expression, index);
4256
4257 /* When not doing offsetof, array references are not permitted in
4258 constant-expressions. */
4259 if (!for_offsetof
4260 && (cp_parser_non_integral_constant_expression
4261 (parser, "an array reference")))
4262 postfix_expression = error_mark_node;
4263
4264 return postfix_expression;
4265 }
4266
4267 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4268 by cp_parser_builtin_offsetof. We're looking for
4269
4270 postfix-expression . template [opt] id-expression
4271 postfix-expression . pseudo-destructor-name
4272 postfix-expression -> template [opt] id-expression
4273 postfix-expression -> pseudo-destructor-name
4274
4275 FOR_OFFSETOF is set if we're being called in that context. That sorta
4276 limits what of the above we'll actually accept, but nevermind.
4277 TOKEN_TYPE is the "." or "->" token, which will already have been
4278 removed from the stream. */
4279
4280 static tree
4281 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4282 enum cpp_ttype token_type,
4283 tree postfix_expression,
4284 bool for_offsetof, cp_id_kind *idk)
4285 {
4286 tree name;
4287 bool dependent_p;
4288 bool template_p;
4289 bool pseudo_destructor_p;
4290 tree scope = NULL_TREE;
4291
4292 /* If this is a `->' operator, dereference the pointer. */
4293 if (token_type == CPP_DEREF)
4294 postfix_expression = build_x_arrow (postfix_expression);
4295 /* Check to see whether or not the expression is type-dependent. */
4296 dependent_p = type_dependent_expression_p (postfix_expression);
4297 /* The identifier following the `->' or `.' is not qualified. */
4298 parser->scope = NULL_TREE;
4299 parser->qualifying_scope = NULL_TREE;
4300 parser->object_scope = NULL_TREE;
4301 *idk = CP_ID_KIND_NONE;
4302 /* Enter the scope corresponding to the type of the object
4303 given by the POSTFIX_EXPRESSION. */
4304 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4305 {
4306 scope = TREE_TYPE (postfix_expression);
4307 /* According to the standard, no expression should ever have
4308 reference type. Unfortunately, we do not currently match
4309 the standard in this respect in that our internal representation
4310 of an expression may have reference type even when the standard
4311 says it does not. Therefore, we have to manually obtain the
4312 underlying type here. */
4313 scope = non_reference (scope);
4314 /* The type of the POSTFIX_EXPRESSION must be complete. */
4315 scope = complete_type_or_else (scope, NULL_TREE);
4316 /* Let the name lookup machinery know that we are processing a
4317 class member access expression. */
4318 parser->context->object_type = scope;
4319 /* If something went wrong, we want to be able to discern that case,
4320 as opposed to the case where there was no SCOPE due to the type
4321 of expression being dependent. */
4322 if (!scope)
4323 scope = error_mark_node;
4324 /* If the SCOPE was erroneous, make the various semantic analysis
4325 functions exit quickly -- and without issuing additional error
4326 messages. */
4327 if (scope == error_mark_node)
4328 postfix_expression = error_mark_node;
4329 }
4330
4331 /* Assume this expression is not a pseudo-destructor access. */
4332 pseudo_destructor_p = false;
4333
4334 /* If the SCOPE is a scalar type, then, if this is a valid program,
4335 we must be looking at a pseudo-destructor-name. */
4336 if (scope && SCALAR_TYPE_P (scope))
4337 {
4338 tree s;
4339 tree type;
4340
4341 cp_parser_parse_tentatively (parser);
4342 /* Parse the pseudo-destructor-name. */
4343 s = NULL_TREE;
4344 cp_parser_pseudo_destructor_name (parser, &s, &type);
4345 if (cp_parser_parse_definitely (parser))
4346 {
4347 pseudo_destructor_p = true;
4348 postfix_expression
4349 = finish_pseudo_destructor_expr (postfix_expression,
4350 s, TREE_TYPE (type));
4351 }
4352 }
4353
4354 if (!pseudo_destructor_p)
4355 {
4356 /* If the SCOPE is not a scalar type, we are looking at an
4357 ordinary class member access expression, rather than a
4358 pseudo-destructor-name. */
4359 template_p = cp_parser_optional_template_keyword (parser);
4360 /* Parse the id-expression. */
4361 name = cp_parser_id_expression (parser, template_p,
4362 /*check_dependency_p=*/true,
4363 /*template_p=*/NULL,
4364 /*declarator_p=*/false);
4365 /* In general, build a SCOPE_REF if the member name is qualified.
4366 However, if the name was not dependent and has already been
4367 resolved; there is no need to build the SCOPE_REF. For example;
4368
4369 struct X { void f(); };
4370 template <typename T> void f(T* t) { t->X::f(); }
4371
4372 Even though "t" is dependent, "X::f" is not and has been resolved
4373 to a BASELINK; there is no need to include scope information. */
4374
4375 /* But we do need to remember that there was an explicit scope for
4376 virtual function calls. */
4377 if (parser->scope)
4378 *idk = CP_ID_KIND_QUALIFIED;
4379
4380 /* If the name is a template-id that names a type, we will get a
4381 TYPE_DECL here. That is invalid code. */
4382 if (TREE_CODE (name) == TYPE_DECL)
4383 {
4384 error ("invalid use of %qD", name);
4385 postfix_expression = error_mark_node;
4386 }
4387 else
4388 {
4389 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4390 {
4391 name = build_nt (SCOPE_REF, parser->scope, name);
4392 parser->scope = NULL_TREE;
4393 parser->qualifying_scope = NULL_TREE;
4394 parser->object_scope = NULL_TREE;
4395 }
4396 if (scope && name && BASELINK_P (name))
4397 adjust_result_of_qualified_name_lookup
4398 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4399 postfix_expression
4400 = finish_class_member_access_expr (postfix_expression, name);
4401 }
4402 }
4403
4404 /* We no longer need to look up names in the scope of the object on
4405 the left-hand side of the `.' or `->' operator. */
4406 parser->context->object_type = NULL_TREE;
4407
4408 /* Outside of offsetof, these operators may not appear in
4409 constant-expressions. */
4410 if (!for_offsetof
4411 && (cp_parser_non_integral_constant_expression
4412 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4413 postfix_expression = error_mark_node;
4414
4415 return postfix_expression;
4416 }
4417
4418 /* Parse a parenthesized expression-list.
4419
4420 expression-list:
4421 assignment-expression
4422 expression-list, assignment-expression
4423
4424 attribute-list:
4425 expression-list
4426 identifier
4427 identifier, expression-list
4428
4429 CAST_P is true if this expression is the target of a cast.
4430
4431 Returns a TREE_LIST. The TREE_VALUE of each node is a
4432 representation of an assignment-expression. Note that a TREE_LIST
4433 is returned even if there is only a single expression in the list.
4434 error_mark_node is returned if the ( and or ) are
4435 missing. NULL_TREE is returned on no expressions. The parentheses
4436 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4437 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4438 indicates whether or not all of the expressions in the list were
4439 constant. */
4440
4441 static tree
4442 cp_parser_parenthesized_expression_list (cp_parser* parser,
4443 bool is_attribute_list,
4444 bool cast_p,
4445 bool *non_constant_p)
4446 {
4447 tree expression_list = NULL_TREE;
4448 bool fold_expr_p = is_attribute_list;
4449 tree identifier = NULL_TREE;
4450
4451 /* Assume all the expressions will be constant. */
4452 if (non_constant_p)
4453 *non_constant_p = false;
4454
4455 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4456 return error_mark_node;
4457
4458 /* Consume expressions until there are no more. */
4459 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4460 while (true)
4461 {
4462 tree expr;
4463
4464 /* At the beginning of attribute lists, check to see if the
4465 next token is an identifier. */
4466 if (is_attribute_list
4467 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4468 {
4469 cp_token *token;
4470
4471 /* Consume the identifier. */
4472 token = cp_lexer_consume_token (parser->lexer);
4473 /* Save the identifier. */
4474 identifier = token->value;
4475 }
4476 else
4477 {
4478 /* Parse the next assignment-expression. */
4479 if (non_constant_p)
4480 {
4481 bool expr_non_constant_p;
4482 expr = (cp_parser_constant_expression
4483 (parser, /*allow_non_constant_p=*/true,
4484 &expr_non_constant_p));
4485 if (expr_non_constant_p)
4486 *non_constant_p = true;
4487 }
4488 else
4489 expr = cp_parser_assignment_expression (parser, cast_p);
4490
4491 if (fold_expr_p)
4492 expr = fold_non_dependent_expr (expr);
4493
4494 /* Add it to the list. We add error_mark_node
4495 expressions to the list, so that we can still tell if
4496 the correct form for a parenthesized expression-list
4497 is found. That gives better errors. */
4498 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4499
4500 if (expr == error_mark_node)
4501 goto skip_comma;
4502 }
4503
4504 /* After the first item, attribute lists look the same as
4505 expression lists. */
4506 is_attribute_list = false;
4507
4508 get_comma:;
4509 /* If the next token isn't a `,', then we are done. */
4510 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4511 break;
4512
4513 /* Otherwise, consume the `,' and keep going. */
4514 cp_lexer_consume_token (parser->lexer);
4515 }
4516
4517 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4518 {
4519 int ending;
4520
4521 skip_comma:;
4522 /* We try and resync to an unnested comma, as that will give the
4523 user better diagnostics. */
4524 ending = cp_parser_skip_to_closing_parenthesis (parser,
4525 /*recovering=*/true,
4526 /*or_comma=*/true,
4527 /*consume_paren=*/true);
4528 if (ending < 0)
4529 goto get_comma;
4530 if (!ending)
4531 return error_mark_node;
4532 }
4533
4534 /* We built up the list in reverse order so we must reverse it now. */
4535 expression_list = nreverse (expression_list);
4536 if (identifier)
4537 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4538
4539 return expression_list;
4540 }
4541
4542 /* Parse a pseudo-destructor-name.
4543
4544 pseudo-destructor-name:
4545 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4546 :: [opt] nested-name-specifier template template-id :: ~ type-name
4547 :: [opt] nested-name-specifier [opt] ~ type-name
4548
4549 If either of the first two productions is used, sets *SCOPE to the
4550 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4551 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4552 or ERROR_MARK_NODE if the parse fails. */
4553
4554 static void
4555 cp_parser_pseudo_destructor_name (cp_parser* parser,
4556 tree* scope,
4557 tree* type)
4558 {
4559 bool nested_name_specifier_p;
4560
4561 /* Assume that things will not work out. */
4562 *type = error_mark_node;
4563
4564 /* Look for the optional `::' operator. */
4565 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4566 /* Look for the optional nested-name-specifier. */
4567 nested_name_specifier_p
4568 = (cp_parser_nested_name_specifier_opt (parser,
4569 /*typename_keyword_p=*/false,
4570 /*check_dependency_p=*/true,
4571 /*type_p=*/false,
4572 /*is_declaration=*/true)
4573 != NULL_TREE);
4574 /* Now, if we saw a nested-name-specifier, we might be doing the
4575 second production. */
4576 if (nested_name_specifier_p
4577 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4578 {
4579 /* Consume the `template' keyword. */
4580 cp_lexer_consume_token (parser->lexer);
4581 /* Parse the template-id. */
4582 cp_parser_template_id (parser,
4583 /*template_keyword_p=*/true,
4584 /*check_dependency_p=*/false,
4585 /*is_declaration=*/true);
4586 /* Look for the `::' token. */
4587 cp_parser_require (parser, CPP_SCOPE, "`::'");
4588 }
4589 /* If the next token is not a `~', then there might be some
4590 additional qualification. */
4591 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4592 {
4593 /* Look for the type-name. */
4594 *scope = TREE_TYPE (cp_parser_type_name (parser));
4595
4596 if (*scope == error_mark_node)
4597 return;
4598
4599 /* If we don't have ::~, then something has gone wrong. Since
4600 the only caller of this function is looking for something
4601 after `.' or `->' after a scalar type, most likely the
4602 program is trying to get a member of a non-aggregate
4603 type. */
4604 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4605 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4606 {
4607 cp_parser_error (parser, "request for member of non-aggregate type");
4608 return;
4609 }
4610
4611 /* Look for the `::' token. */
4612 cp_parser_require (parser, CPP_SCOPE, "`::'");
4613 }
4614 else
4615 *scope = NULL_TREE;
4616
4617 /* Look for the `~'. */
4618 cp_parser_require (parser, CPP_COMPL, "`~'");
4619 /* Look for the type-name again. We are not responsible for
4620 checking that it matches the first type-name. */
4621 *type = cp_parser_type_name (parser);
4622 }
4623
4624 /* Parse a unary-expression.
4625
4626 unary-expression:
4627 postfix-expression
4628 ++ cast-expression
4629 -- cast-expression
4630 unary-operator cast-expression
4631 sizeof unary-expression
4632 sizeof ( type-id )
4633 new-expression
4634 delete-expression
4635
4636 GNU Extensions:
4637
4638 unary-expression:
4639 __extension__ cast-expression
4640 __alignof__ unary-expression
4641 __alignof__ ( type-id )
4642 __real__ cast-expression
4643 __imag__ cast-expression
4644 && identifier
4645
4646 ADDRESS_P is true iff the unary-expression is appearing as the
4647 operand of the `&' operator. CAST_P is true if this expression is
4648 the target of a cast.
4649
4650 Returns a representation of the expression. */
4651
4652 static tree
4653 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4654 {
4655 cp_token *token;
4656 enum tree_code unary_operator;
4657
4658 /* Peek at the next token. */
4659 token = cp_lexer_peek_token (parser->lexer);
4660 /* Some keywords give away the kind of expression. */
4661 if (token->type == CPP_KEYWORD)
4662 {
4663 enum rid keyword = token->keyword;
4664
4665 switch (keyword)
4666 {
4667 case RID_ALIGNOF:
4668 case RID_SIZEOF:
4669 {
4670 tree operand;
4671 enum tree_code op;
4672
4673 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4674 /* Consume the token. */
4675 cp_lexer_consume_token (parser->lexer);
4676 /* Parse the operand. */
4677 operand = cp_parser_sizeof_operand (parser, keyword);
4678
4679 if (TYPE_P (operand))
4680 return cxx_sizeof_or_alignof_type (operand, op, true);
4681 else
4682 return cxx_sizeof_or_alignof_expr (operand, op);
4683 }
4684
4685 case RID_NEW:
4686 return cp_parser_new_expression (parser);
4687
4688 case RID_DELETE:
4689 return cp_parser_delete_expression (parser);
4690
4691 case RID_EXTENSION:
4692 {
4693 /* The saved value of the PEDANTIC flag. */
4694 int saved_pedantic;
4695 tree expr;
4696
4697 /* Save away the PEDANTIC flag. */
4698 cp_parser_extension_opt (parser, &saved_pedantic);
4699 /* Parse the cast-expression. */
4700 expr = cp_parser_simple_cast_expression (parser);
4701 /* Restore the PEDANTIC flag. */
4702 pedantic = saved_pedantic;
4703
4704 return expr;
4705 }
4706
4707 case RID_REALPART:
4708 case RID_IMAGPART:
4709 {
4710 tree expression;
4711
4712 /* Consume the `__real__' or `__imag__' token. */
4713 cp_lexer_consume_token (parser->lexer);
4714 /* Parse the cast-expression. */
4715 expression = cp_parser_simple_cast_expression (parser);
4716 /* Create the complete representation. */
4717 return build_x_unary_op ((keyword == RID_REALPART
4718 ? REALPART_EXPR : IMAGPART_EXPR),
4719 expression);
4720 }
4721 break;
4722
4723 default:
4724 break;
4725 }
4726 }
4727
4728 /* Look for the `:: new' and `:: delete', which also signal the
4729 beginning of a new-expression, or delete-expression,
4730 respectively. If the next token is `::', then it might be one of
4731 these. */
4732 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4733 {
4734 enum rid keyword;
4735
4736 /* See if the token after the `::' is one of the keywords in
4737 which we're interested. */
4738 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4739 /* If it's `new', we have a new-expression. */
4740 if (keyword == RID_NEW)
4741 return cp_parser_new_expression (parser);
4742 /* Similarly, for `delete'. */
4743 else if (keyword == RID_DELETE)
4744 return cp_parser_delete_expression (parser);
4745 }
4746
4747 /* Look for a unary operator. */
4748 unary_operator = cp_parser_unary_operator (token);
4749 /* The `++' and `--' operators can be handled similarly, even though
4750 they are not technically unary-operators in the grammar. */
4751 if (unary_operator == ERROR_MARK)
4752 {
4753 if (token->type == CPP_PLUS_PLUS)
4754 unary_operator = PREINCREMENT_EXPR;
4755 else if (token->type == CPP_MINUS_MINUS)
4756 unary_operator = PREDECREMENT_EXPR;
4757 /* Handle the GNU address-of-label extension. */
4758 else if (cp_parser_allow_gnu_extensions_p (parser)
4759 && token->type == CPP_AND_AND)
4760 {
4761 tree identifier;
4762
4763 /* Consume the '&&' token. */
4764 cp_lexer_consume_token (parser->lexer);
4765 /* Look for the identifier. */
4766 identifier = cp_parser_identifier (parser);
4767 /* Create an expression representing the address. */
4768 return finish_label_address_expr (identifier);
4769 }
4770 }
4771 if (unary_operator != ERROR_MARK)
4772 {
4773 tree cast_expression;
4774 tree expression = error_mark_node;
4775 const char *non_constant_p = NULL;
4776
4777 /* Consume the operator token. */
4778 token = cp_lexer_consume_token (parser->lexer);
4779 /* Parse the cast-expression. */
4780 cast_expression
4781 = cp_parser_cast_expression (parser,
4782 unary_operator == ADDR_EXPR,
4783 /*cast_p=*/false);
4784 /* Now, build an appropriate representation. */
4785 switch (unary_operator)
4786 {
4787 case INDIRECT_REF:
4788 non_constant_p = "`*'";
4789 expression = build_x_indirect_ref (cast_expression, "unary *");
4790 break;
4791
4792 case ADDR_EXPR:
4793 non_constant_p = "`&'";
4794 /* Fall through. */
4795 case BIT_NOT_EXPR:
4796 expression = build_x_unary_op (unary_operator, cast_expression);
4797 break;
4798
4799 case PREINCREMENT_EXPR:
4800 case PREDECREMENT_EXPR:
4801 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4802 ? "`++'" : "`--'");
4803 /* Fall through. */
4804 case CONVERT_EXPR:
4805 case NEGATE_EXPR:
4806 case TRUTH_NOT_EXPR:
4807 expression = finish_unary_op_expr (unary_operator, cast_expression);
4808 break;
4809
4810 default:
4811 gcc_unreachable ();
4812 }
4813
4814 if (non_constant_p
4815 && cp_parser_non_integral_constant_expression (parser,
4816 non_constant_p))
4817 expression = error_mark_node;
4818
4819 return expression;
4820 }
4821
4822 return cp_parser_postfix_expression (parser, address_p, cast_p);
4823 }
4824
4825 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4826 unary-operator, the corresponding tree code is returned. */
4827
4828 static enum tree_code
4829 cp_parser_unary_operator (cp_token* token)
4830 {
4831 switch (token->type)
4832 {
4833 case CPP_MULT:
4834 return INDIRECT_REF;
4835
4836 case CPP_AND:
4837 return ADDR_EXPR;
4838
4839 case CPP_PLUS:
4840 return CONVERT_EXPR;
4841
4842 case CPP_MINUS:
4843 return NEGATE_EXPR;
4844
4845 case CPP_NOT:
4846 return TRUTH_NOT_EXPR;
4847
4848 case CPP_COMPL:
4849 return BIT_NOT_EXPR;
4850
4851 default:
4852 return ERROR_MARK;
4853 }
4854 }
4855
4856 /* Parse a new-expression.
4857
4858 new-expression:
4859 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4860 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4861
4862 Returns a representation of the expression. */
4863
4864 static tree
4865 cp_parser_new_expression (cp_parser* parser)
4866 {
4867 bool global_scope_p;
4868 tree placement;
4869 tree type;
4870 tree initializer;
4871 tree nelts;
4872
4873 /* Look for the optional `::' operator. */
4874 global_scope_p
4875 = (cp_parser_global_scope_opt (parser,
4876 /*current_scope_valid_p=*/false)
4877 != NULL_TREE);
4878 /* Look for the `new' operator. */
4879 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4880 /* There's no easy way to tell a new-placement from the
4881 `( type-id )' construct. */
4882 cp_parser_parse_tentatively (parser);
4883 /* Look for a new-placement. */
4884 placement = cp_parser_new_placement (parser);
4885 /* If that didn't work out, there's no new-placement. */
4886 if (!cp_parser_parse_definitely (parser))
4887 placement = NULL_TREE;
4888
4889 /* If the next token is a `(', then we have a parenthesized
4890 type-id. */
4891 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4892 {
4893 /* Consume the `('. */
4894 cp_lexer_consume_token (parser->lexer);
4895 /* Parse the type-id. */
4896 type = cp_parser_type_id (parser);
4897 /* Look for the closing `)'. */
4898 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4899 /* There should not be a direct-new-declarator in this production,
4900 but GCC used to allowed this, so we check and emit a sensible error
4901 message for this case. */
4902 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4903 {
4904 error ("array bound forbidden after parenthesized type-id");
4905 inform ("try removing the parentheses around the type-id");
4906 cp_parser_direct_new_declarator (parser);
4907 }
4908 nelts = NULL_TREE;
4909 }
4910 /* Otherwise, there must be a new-type-id. */
4911 else
4912 type = cp_parser_new_type_id (parser, &nelts);
4913
4914 /* If the next token is a `(', then we have a new-initializer. */
4915 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4916 initializer = cp_parser_new_initializer (parser);
4917 else
4918 initializer = NULL_TREE;
4919
4920 /* A new-expression may not appear in an integral constant
4921 expression. */
4922 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4923 return error_mark_node;
4924
4925 /* Create a representation of the new-expression. */
4926 return build_new (placement, type, nelts, initializer, global_scope_p);
4927 }
4928
4929 /* Parse a new-placement.
4930
4931 new-placement:
4932 ( expression-list )
4933
4934 Returns the same representation as for an expression-list. */
4935
4936 static tree
4937 cp_parser_new_placement (cp_parser* parser)
4938 {
4939 tree expression_list;
4940
4941 /* Parse the expression-list. */
4942 expression_list = (cp_parser_parenthesized_expression_list
4943 (parser, false, /*cast_p=*/false,
4944 /*non_constant_p=*/NULL));
4945
4946 return expression_list;
4947 }
4948
4949 /* Parse a new-type-id.
4950
4951 new-type-id:
4952 type-specifier-seq new-declarator [opt]
4953
4954 Returns the TYPE allocated. If the new-type-id indicates an array
4955 type, *NELTS is set to the number of elements in the last array
4956 bound; the TYPE will not include the last array bound. */
4957
4958 static tree
4959 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4960 {
4961 cp_decl_specifier_seq type_specifier_seq;
4962 cp_declarator *new_declarator;
4963 cp_declarator *declarator;
4964 cp_declarator *outer_declarator;
4965 const char *saved_message;
4966 tree type;
4967
4968 /* The type-specifier sequence must not contain type definitions.
4969 (It cannot contain declarations of new types either, but if they
4970 are not definitions we will catch that because they are not
4971 complete.) */
4972 saved_message = parser->type_definition_forbidden_message;
4973 parser->type_definition_forbidden_message
4974 = "types may not be defined in a new-type-id";
4975 /* Parse the type-specifier-seq. */
4976 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
4977 &type_specifier_seq);
4978 /* Restore the old message. */
4979 parser->type_definition_forbidden_message = saved_message;
4980 /* Parse the new-declarator. */
4981 new_declarator = cp_parser_new_declarator_opt (parser);
4982
4983 /* Determine the number of elements in the last array dimension, if
4984 any. */
4985 *nelts = NULL_TREE;
4986 /* Skip down to the last array dimension. */
4987 declarator = new_declarator;
4988 outer_declarator = NULL;
4989 while (declarator && (declarator->kind == cdk_pointer
4990 || declarator->kind == cdk_ptrmem))
4991 {
4992 outer_declarator = declarator;
4993 declarator = declarator->declarator;
4994 }
4995 while (declarator
4996 && declarator->kind == cdk_array
4997 && declarator->declarator
4998 && declarator->declarator->kind == cdk_array)
4999 {
5000 outer_declarator = declarator;
5001 declarator = declarator->declarator;
5002 }
5003
5004 if (declarator && declarator->kind == cdk_array)
5005 {
5006 *nelts = declarator->u.array.bounds;
5007 if (*nelts == error_mark_node)
5008 *nelts = integer_one_node;
5009
5010 if (outer_declarator)
5011 outer_declarator->declarator = declarator->declarator;
5012 else
5013 new_declarator = NULL;
5014 }
5015
5016 type = groktypename (&type_specifier_seq, new_declarator);
5017 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5018 {
5019 *nelts = array_type_nelts_top (type);
5020 type = TREE_TYPE (type);
5021 }
5022 return type;
5023 }
5024
5025 /* Parse an (optional) new-declarator.
5026
5027 new-declarator:
5028 ptr-operator new-declarator [opt]
5029 direct-new-declarator
5030
5031 Returns the declarator. */
5032
5033 static cp_declarator *
5034 cp_parser_new_declarator_opt (cp_parser* parser)
5035 {
5036 enum tree_code code;
5037 tree type;
5038 cp_cv_quals cv_quals;
5039
5040 /* We don't know if there's a ptr-operator next, or not. */
5041 cp_parser_parse_tentatively (parser);
5042 /* Look for a ptr-operator. */
5043 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5044 /* If that worked, look for more new-declarators. */
5045 if (cp_parser_parse_definitely (parser))
5046 {
5047 cp_declarator *declarator;
5048
5049 /* Parse another optional declarator. */
5050 declarator = cp_parser_new_declarator_opt (parser);
5051
5052 /* Create the representation of the declarator. */
5053 if (type)
5054 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5055 else if (code == INDIRECT_REF)
5056 declarator = make_pointer_declarator (cv_quals, declarator);
5057 else
5058 declarator = make_reference_declarator (cv_quals, declarator);
5059
5060 return declarator;
5061 }
5062
5063 /* If the next token is a `[', there is a direct-new-declarator. */
5064 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5065 return cp_parser_direct_new_declarator (parser);
5066
5067 return NULL;
5068 }
5069
5070 /* Parse a direct-new-declarator.
5071
5072 direct-new-declarator:
5073 [ expression ]
5074 direct-new-declarator [constant-expression]
5075
5076 */
5077
5078 static cp_declarator *
5079 cp_parser_direct_new_declarator (cp_parser* parser)
5080 {
5081 cp_declarator *declarator = NULL;
5082
5083 while (true)
5084 {
5085 tree expression;
5086
5087 /* Look for the opening `['. */
5088 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5089 /* The first expression is not required to be constant. */
5090 if (!declarator)
5091 {
5092 expression = cp_parser_expression (parser, /*cast_p=*/false);
5093 /* The standard requires that the expression have integral
5094 type. DR 74 adds enumeration types. We believe that the
5095 real intent is that these expressions be handled like the
5096 expression in a `switch' condition, which also allows
5097 classes with a single conversion to integral or
5098 enumeration type. */
5099 if (!processing_template_decl)
5100 {
5101 expression
5102 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5103 expression,
5104 /*complain=*/true);
5105 if (!expression)
5106 {
5107 error ("expression in new-declarator must have integral "
5108 "or enumeration type");
5109 expression = error_mark_node;
5110 }
5111 }
5112 }
5113 /* But all the other expressions must be. */
5114 else
5115 expression
5116 = cp_parser_constant_expression (parser,
5117 /*allow_non_constant=*/false,
5118 NULL);
5119 /* Look for the closing `]'. */
5120 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5121
5122 /* Add this bound to the declarator. */
5123 declarator = make_array_declarator (declarator, expression);
5124
5125 /* If the next token is not a `[', then there are no more
5126 bounds. */
5127 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5128 break;
5129 }
5130
5131 return declarator;
5132 }
5133
5134 /* Parse a new-initializer.
5135
5136 new-initializer:
5137 ( expression-list [opt] )
5138
5139 Returns a representation of the expression-list. If there is no
5140 expression-list, VOID_ZERO_NODE is returned. */
5141
5142 static tree
5143 cp_parser_new_initializer (cp_parser* parser)
5144 {
5145 tree expression_list;
5146
5147 expression_list = (cp_parser_parenthesized_expression_list
5148 (parser, false, /*cast_p=*/false,
5149 /*non_constant_p=*/NULL));
5150 if (!expression_list)
5151 expression_list = void_zero_node;
5152
5153 return expression_list;
5154 }
5155
5156 /* Parse a delete-expression.
5157
5158 delete-expression:
5159 :: [opt] delete cast-expression
5160 :: [opt] delete [ ] cast-expression
5161
5162 Returns a representation of the expression. */
5163
5164 static tree
5165 cp_parser_delete_expression (cp_parser* parser)
5166 {
5167 bool global_scope_p;
5168 bool array_p;
5169 tree expression;
5170
5171 /* Look for the optional `::' operator. */
5172 global_scope_p
5173 = (cp_parser_global_scope_opt (parser,
5174 /*current_scope_valid_p=*/false)
5175 != NULL_TREE);
5176 /* Look for the `delete' keyword. */
5177 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5178 /* See if the array syntax is in use. */
5179 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5180 {
5181 /* Consume the `[' token. */
5182 cp_lexer_consume_token (parser->lexer);
5183 /* Look for the `]' token. */
5184 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5185 /* Remember that this is the `[]' construct. */
5186 array_p = true;
5187 }
5188 else
5189 array_p = false;
5190
5191 /* Parse the cast-expression. */
5192 expression = cp_parser_simple_cast_expression (parser);
5193
5194 /* A delete-expression may not appear in an integral constant
5195 expression. */
5196 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5197 return error_mark_node;
5198
5199 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5200 }
5201
5202 /* Parse a cast-expression.
5203
5204 cast-expression:
5205 unary-expression
5206 ( type-id ) cast-expression
5207
5208 ADDRESS_P is true iff the unary-expression is appearing as the
5209 operand of the `&' operator. CAST_P is true if this expression is
5210 the target of a cast.
5211
5212 Returns a representation of the expression. */
5213
5214 static tree
5215 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5216 {
5217 /* If it's a `(', then we might be looking at a cast. */
5218 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5219 {
5220 tree type = NULL_TREE;
5221 tree expr = NULL_TREE;
5222 bool compound_literal_p;
5223 const char *saved_message;
5224
5225 /* There's no way to know yet whether or not this is a cast.
5226 For example, `(int (3))' is a unary-expression, while `(int)
5227 3' is a cast. So, we resort to parsing tentatively. */
5228 cp_parser_parse_tentatively (parser);
5229 /* Types may not be defined in a cast. */
5230 saved_message = parser->type_definition_forbidden_message;
5231 parser->type_definition_forbidden_message
5232 = "types may not be defined in casts";
5233 /* Consume the `('. */
5234 cp_lexer_consume_token (parser->lexer);
5235 /* A very tricky bit is that `(struct S) { 3 }' is a
5236 compound-literal (which we permit in C++ as an extension).
5237 But, that construct is not a cast-expression -- it is a
5238 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5239 is legal; if the compound-literal were a cast-expression,
5240 you'd need an extra set of parentheses.) But, if we parse
5241 the type-id, and it happens to be a class-specifier, then we
5242 will commit to the parse at that point, because we cannot
5243 undo the action that is done when creating a new class. So,
5244 then we cannot back up and do a postfix-expression.
5245
5246 Therefore, we scan ahead to the closing `)', and check to see
5247 if the token after the `)' is a `{'. If so, we are not
5248 looking at a cast-expression.
5249
5250 Save tokens so that we can put them back. */
5251 cp_lexer_save_tokens (parser->lexer);
5252 /* Skip tokens until the next token is a closing parenthesis.
5253 If we find the closing `)', and the next token is a `{', then
5254 we are looking at a compound-literal. */
5255 compound_literal_p
5256 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5257 /*consume_paren=*/true)
5258 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5259 /* Roll back the tokens we skipped. */
5260 cp_lexer_rollback_tokens (parser->lexer);
5261 /* If we were looking at a compound-literal, simulate an error
5262 so that the call to cp_parser_parse_definitely below will
5263 fail. */
5264 if (compound_literal_p)
5265 cp_parser_simulate_error (parser);
5266 else
5267 {
5268 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5269 parser->in_type_id_in_expr_p = true;
5270 /* Look for the type-id. */
5271 type = cp_parser_type_id (parser);
5272 /* Look for the closing `)'. */
5273 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5274 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5275 }
5276
5277 /* Restore the saved message. */
5278 parser->type_definition_forbidden_message = saved_message;
5279
5280 /* If ok so far, parse the dependent expression. We cannot be
5281 sure it is a cast. Consider `(T ())'. It is a parenthesized
5282 ctor of T, but looks like a cast to function returning T
5283 without a dependent expression. */
5284 if (!cp_parser_error_occurred (parser))
5285 expr = cp_parser_cast_expression (parser,
5286 /*address_p=*/false,
5287 /*cast_p=*/true);
5288
5289 if (cp_parser_parse_definitely (parser))
5290 {
5291 /* Warn about old-style casts, if so requested. */
5292 if (warn_old_style_cast
5293 && !in_system_header
5294 && !VOID_TYPE_P (type)
5295 && current_lang_name != lang_name_c)
5296 warning ("use of old-style cast");
5297
5298 /* Only type conversions to integral or enumeration types
5299 can be used in constant-expressions. */
5300 if (parser->integral_constant_expression_p
5301 && !dependent_type_p (type)
5302 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5303 && (cp_parser_non_integral_constant_expression
5304 (parser,
5305 "a cast to a type other than an integral or "
5306 "enumeration type")))
5307 return error_mark_node;
5308
5309 /* Perform the cast. */
5310 expr = build_c_cast (type, expr);
5311 return expr;
5312 }
5313 }
5314
5315 /* If we get here, then it's not a cast, so it must be a
5316 unary-expression. */
5317 return cp_parser_unary_expression (parser, address_p, cast_p);
5318 }
5319
5320 /* Parse a binary expression of the general form:
5321
5322 pm-expression:
5323 cast-expression
5324 pm-expression .* cast-expression
5325 pm-expression ->* cast-expression
5326
5327 multiplicative-expression:
5328 pm-expression
5329 multiplicative-expression * pm-expression
5330 multiplicative-expression / pm-expression
5331 multiplicative-expression % pm-expression
5332
5333 additive-expression:
5334 multiplicative-expression
5335 additive-expression + multiplicative-expression
5336 additive-expression - multiplicative-expression
5337
5338 shift-expression:
5339 additive-expression
5340 shift-expression << additive-expression
5341 shift-expression >> additive-expression
5342
5343 relational-expression:
5344 shift-expression
5345 relational-expression < shift-expression
5346 relational-expression > shift-expression
5347 relational-expression <= shift-expression
5348 relational-expression >= shift-expression
5349
5350 GNU Extension:
5351
5352 relational-expression:
5353 relational-expression <? shift-expression
5354 relational-expression >? shift-expression
5355
5356 equality-expression:
5357 relational-expression
5358 equality-expression == relational-expression
5359 equality-expression != relational-expression
5360
5361 and-expression:
5362 equality-expression
5363 and-expression & equality-expression
5364
5365 exclusive-or-expression:
5366 and-expression
5367 exclusive-or-expression ^ and-expression
5368
5369 inclusive-or-expression:
5370 exclusive-or-expression
5371 inclusive-or-expression | exclusive-or-expression
5372
5373 logical-and-expression:
5374 inclusive-or-expression
5375 logical-and-expression && inclusive-or-expression
5376
5377 logical-or-expression:
5378 logical-and-expression
5379 logical-or-expression || logical-and-expression
5380
5381 All these are implemented with a single function like:
5382
5383 binary-expression:
5384 simple-cast-expression
5385 binary-expression <token> binary-expression
5386
5387 CAST_P is true if this expression is the target of a cast.
5388
5389 The binops_by_token map is used to get the tree codes for each <token> type.
5390 binary-expressions are associated according to a precedence table. */
5391
5392 #define TOKEN_PRECEDENCE(token) \
5393 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5394 ? PREC_NOT_OPERATOR \
5395 : binops_by_token[token->type].prec)
5396
5397 static tree
5398 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5399 {
5400 cp_parser_expression_stack stack;
5401 cp_parser_expression_stack_entry *sp = &stack[0];
5402 tree lhs, rhs;
5403 cp_token *token;
5404 enum tree_code tree_type;
5405 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5406 bool overloaded_p;
5407
5408 /* Parse the first expression. */
5409 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5410
5411 for (;;)
5412 {
5413 /* Get an operator token. */
5414 token = cp_lexer_peek_token (parser->lexer);
5415 if (token->type == CPP_MIN || token->type == CPP_MAX)
5416 cp_parser_warn_min_max ();
5417
5418 new_prec = TOKEN_PRECEDENCE (token);
5419
5420 /* Popping an entry off the stack means we completed a subexpression:
5421 - either we found a token which is not an operator (`>' where it is not
5422 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5423 will happen repeatedly;
5424 - or, we found an operator which has lower priority. This is the case
5425 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5426 parsing `3 * 4'. */
5427 if (new_prec <= prec)
5428 {
5429 if (sp == stack)
5430 break;
5431 else
5432 goto pop;
5433 }
5434
5435 get_rhs:
5436 tree_type = binops_by_token[token->type].tree_type;
5437
5438 /* We used the operator token. */
5439 cp_lexer_consume_token (parser->lexer);
5440
5441 /* Extract another operand. It may be the RHS of this expression
5442 or the LHS of a new, higher priority expression. */
5443 rhs = cp_parser_simple_cast_expression (parser);
5444
5445 /* Get another operator token. Look up its precedence to avoid
5446 building a useless (immediately popped) stack entry for common
5447 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5448 token = cp_lexer_peek_token (parser->lexer);
5449 lookahead_prec = TOKEN_PRECEDENCE (token);
5450 if (lookahead_prec > new_prec)
5451 {
5452 /* ... and prepare to parse the RHS of the new, higher priority
5453 expression. Since precedence levels on the stack are
5454 monotonically increasing, we do not have to care about
5455 stack overflows. */
5456 sp->prec = prec;
5457 sp->tree_type = tree_type;
5458 sp->lhs = lhs;
5459 sp++;
5460 lhs = rhs;
5461 prec = new_prec;
5462 new_prec = lookahead_prec;
5463 goto get_rhs;
5464
5465 pop:
5466 /* If the stack is not empty, we have parsed into LHS the right side
5467 (`4' in the example above) of an expression we had suspended.
5468 We can use the information on the stack to recover the LHS (`3')
5469 from the stack together with the tree code (`MULT_EXPR'), and
5470 the precedence of the higher level subexpression
5471 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5472 which will be used to actually build the additive expression. */
5473 --sp;
5474 prec = sp->prec;
5475 tree_type = sp->tree_type;
5476 rhs = lhs;
5477 lhs = sp->lhs;
5478 }
5479
5480 overloaded_p = false;
5481 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5482
5483 /* If the binary operator required the use of an overloaded operator,
5484 then this expression cannot be an integral constant-expression.
5485 An overloaded operator can be used even if both operands are
5486 otherwise permissible in an integral constant-expression if at
5487 least one of the operands is of enumeration type. */
5488
5489 if (overloaded_p
5490 && (cp_parser_non_integral_constant_expression
5491 (parser, "calls to overloaded operators")))
5492 return error_mark_node;
5493 }
5494
5495 return lhs;
5496 }
5497
5498
5499 /* Parse the `? expression : assignment-expression' part of a
5500 conditional-expression. The LOGICAL_OR_EXPR is the
5501 logical-or-expression that started the conditional-expression.
5502 Returns a representation of the entire conditional-expression.
5503
5504 This routine is used by cp_parser_assignment_expression.
5505
5506 ? expression : assignment-expression
5507
5508 GNU Extensions:
5509
5510 ? : assignment-expression */
5511
5512 static tree
5513 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5514 {
5515 tree expr;
5516 tree assignment_expr;
5517
5518 /* Consume the `?' token. */
5519 cp_lexer_consume_token (parser->lexer);
5520 if (cp_parser_allow_gnu_extensions_p (parser)
5521 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5522 /* Implicit true clause. */
5523 expr = NULL_TREE;
5524 else
5525 /* Parse the expression. */
5526 expr = cp_parser_expression (parser, /*cast_p=*/false);
5527
5528 /* The next token should be a `:'. */
5529 cp_parser_require (parser, CPP_COLON, "`:'");
5530 /* Parse the assignment-expression. */
5531 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5532
5533 /* Build the conditional-expression. */
5534 return build_x_conditional_expr (logical_or_expr,
5535 expr,
5536 assignment_expr);
5537 }
5538
5539 /* Parse an assignment-expression.
5540
5541 assignment-expression:
5542 conditional-expression
5543 logical-or-expression assignment-operator assignment_expression
5544 throw-expression
5545
5546 CAST_P is true if this expression is the target of a cast.
5547
5548 Returns a representation for the expression. */
5549
5550 static tree
5551 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5552 {
5553 tree expr;
5554
5555 /* If the next token is the `throw' keyword, then we're looking at
5556 a throw-expression. */
5557 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5558 expr = cp_parser_throw_expression (parser);
5559 /* Otherwise, it must be that we are looking at a
5560 logical-or-expression. */
5561 else
5562 {
5563 /* Parse the binary expressions (logical-or-expression). */
5564 expr = cp_parser_binary_expression (parser, cast_p);
5565 /* If the next token is a `?' then we're actually looking at a
5566 conditional-expression. */
5567 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5568 return cp_parser_question_colon_clause (parser, expr);
5569 else
5570 {
5571 enum tree_code assignment_operator;
5572
5573 /* If it's an assignment-operator, we're using the second
5574 production. */
5575 assignment_operator
5576 = cp_parser_assignment_operator_opt (parser);
5577 if (assignment_operator != ERROR_MARK)
5578 {
5579 tree rhs;
5580
5581 /* Parse the right-hand side of the assignment. */
5582 rhs = cp_parser_assignment_expression (parser, cast_p);
5583 /* An assignment may not appear in a
5584 constant-expression. */
5585 if (cp_parser_non_integral_constant_expression (parser,
5586 "an assignment"))
5587 return error_mark_node;
5588 /* Build the assignment expression. */
5589 expr = build_x_modify_expr (expr,
5590 assignment_operator,
5591 rhs);
5592 }
5593 }
5594 }
5595
5596 return expr;
5597 }
5598
5599 /* Parse an (optional) assignment-operator.
5600
5601 assignment-operator: one of
5602 = *= /= %= += -= >>= <<= &= ^= |=
5603
5604 GNU Extension:
5605
5606 assignment-operator: one of
5607 <?= >?=
5608
5609 If the next token is an assignment operator, the corresponding tree
5610 code is returned, and the token is consumed. For example, for
5611 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5612 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5613 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5614 operator, ERROR_MARK is returned. */
5615
5616 static enum tree_code
5617 cp_parser_assignment_operator_opt (cp_parser* parser)
5618 {
5619 enum tree_code op;
5620 cp_token *token;
5621
5622 /* Peek at the next toen. */
5623 token = cp_lexer_peek_token (parser->lexer);
5624
5625 switch (token->type)
5626 {
5627 case CPP_EQ:
5628 op = NOP_EXPR;
5629 break;
5630
5631 case CPP_MULT_EQ:
5632 op = MULT_EXPR;
5633 break;
5634
5635 case CPP_DIV_EQ:
5636 op = TRUNC_DIV_EXPR;
5637 break;
5638
5639 case CPP_MOD_EQ:
5640 op = TRUNC_MOD_EXPR;
5641 break;
5642
5643 case CPP_PLUS_EQ:
5644 op = PLUS_EXPR;
5645 break;
5646
5647 case CPP_MINUS_EQ:
5648 op = MINUS_EXPR;
5649 break;
5650
5651 case CPP_RSHIFT_EQ:
5652 op = RSHIFT_EXPR;
5653 break;
5654
5655 case CPP_LSHIFT_EQ:
5656 op = LSHIFT_EXPR;
5657 break;
5658
5659 case CPP_AND_EQ:
5660 op = BIT_AND_EXPR;
5661 break;
5662
5663 case CPP_XOR_EQ:
5664 op = BIT_XOR_EXPR;
5665 break;
5666
5667 case CPP_OR_EQ:
5668 op = BIT_IOR_EXPR;
5669 break;
5670
5671 case CPP_MIN_EQ:
5672 op = MIN_EXPR;
5673 cp_parser_warn_min_max ();
5674 break;
5675
5676 case CPP_MAX_EQ:
5677 op = MAX_EXPR;
5678 cp_parser_warn_min_max ();
5679 break;
5680
5681 default:
5682 /* Nothing else is an assignment operator. */
5683 op = ERROR_MARK;
5684 }
5685
5686 /* If it was an assignment operator, consume it. */
5687 if (op != ERROR_MARK)
5688 cp_lexer_consume_token (parser->lexer);
5689
5690 return op;
5691 }
5692
5693 /* Parse an expression.
5694
5695 expression:
5696 assignment-expression
5697 expression , assignment-expression
5698
5699 CAST_P is true if this expression is the target of a cast.
5700
5701 Returns a representation of the expression. */
5702
5703 static tree
5704 cp_parser_expression (cp_parser* parser, bool cast_p)
5705 {
5706 tree expression = NULL_TREE;
5707
5708 while (true)
5709 {
5710 tree assignment_expression;
5711
5712 /* Parse the next assignment-expression. */
5713 assignment_expression
5714 = cp_parser_assignment_expression (parser, cast_p);
5715 /* If this is the first assignment-expression, we can just
5716 save it away. */
5717 if (!expression)
5718 expression = assignment_expression;
5719 else
5720 expression = build_x_compound_expr (expression,
5721 assignment_expression);
5722 /* If the next token is not a comma, then we are done with the
5723 expression. */
5724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5725 break;
5726 /* Consume the `,'. */
5727 cp_lexer_consume_token (parser->lexer);
5728 /* A comma operator cannot appear in a constant-expression. */
5729 if (cp_parser_non_integral_constant_expression (parser,
5730 "a comma operator"))
5731 expression = error_mark_node;
5732 }
5733
5734 return expression;
5735 }
5736
5737 /* Parse a constant-expression.
5738
5739 constant-expression:
5740 conditional-expression
5741
5742 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5743 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5744 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5745 is false, NON_CONSTANT_P should be NULL. */
5746
5747 static tree
5748 cp_parser_constant_expression (cp_parser* parser,
5749 bool allow_non_constant_p,
5750 bool *non_constant_p)
5751 {
5752 bool saved_integral_constant_expression_p;
5753 bool saved_allow_non_integral_constant_expression_p;
5754 bool saved_non_integral_constant_expression_p;
5755 tree expression;
5756
5757 /* It might seem that we could simply parse the
5758 conditional-expression, and then check to see if it were
5759 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5760 one that the compiler can figure out is constant, possibly after
5761 doing some simplifications or optimizations. The standard has a
5762 precise definition of constant-expression, and we must honor
5763 that, even though it is somewhat more restrictive.
5764
5765 For example:
5766
5767 int i[(2, 3)];
5768
5769 is not a legal declaration, because `(2, 3)' is not a
5770 constant-expression. The `,' operator is forbidden in a
5771 constant-expression. However, GCC's constant-folding machinery
5772 will fold this operation to an INTEGER_CST for `3'. */
5773
5774 /* Save the old settings. */
5775 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5776 saved_allow_non_integral_constant_expression_p
5777 = parser->allow_non_integral_constant_expression_p;
5778 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5779 /* We are now parsing a constant-expression. */
5780 parser->integral_constant_expression_p = true;
5781 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5782 parser->non_integral_constant_expression_p = false;
5783 /* Although the grammar says "conditional-expression", we parse an
5784 "assignment-expression", which also permits "throw-expression"
5785 and the use of assignment operators. In the case that
5786 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5787 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5788 actually essential that we look for an assignment-expression.
5789 For example, cp_parser_initializer_clauses uses this function to
5790 determine whether a particular assignment-expression is in fact
5791 constant. */
5792 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5793 /* Restore the old settings. */
5794 parser->integral_constant_expression_p
5795 = saved_integral_constant_expression_p;
5796 parser->allow_non_integral_constant_expression_p
5797 = saved_allow_non_integral_constant_expression_p;
5798 if (allow_non_constant_p)
5799 *non_constant_p = parser->non_integral_constant_expression_p;
5800 else if (parser->non_integral_constant_expression_p)
5801 expression = error_mark_node;
5802 parser->non_integral_constant_expression_p
5803 = saved_non_integral_constant_expression_p;
5804
5805 return expression;
5806 }
5807
5808 /* Parse __builtin_offsetof.
5809
5810 offsetof-expression:
5811 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5812
5813 offsetof-member-designator:
5814 id-expression
5815 | offsetof-member-designator "." id-expression
5816 | offsetof-member-designator "[" expression "]"
5817 */
5818
5819 static tree
5820 cp_parser_builtin_offsetof (cp_parser *parser)
5821 {
5822 int save_ice_p, save_non_ice_p;
5823 tree type, expr;
5824 cp_id_kind dummy;
5825
5826 /* We're about to accept non-integral-constant things, but will
5827 definitely yield an integral constant expression. Save and
5828 restore these values around our local parsing. */
5829 save_ice_p = parser->integral_constant_expression_p;
5830 save_non_ice_p = parser->non_integral_constant_expression_p;
5831
5832 /* Consume the "__builtin_offsetof" token. */
5833 cp_lexer_consume_token (parser->lexer);
5834 /* Consume the opening `('. */
5835 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5836 /* Parse the type-id. */
5837 type = cp_parser_type_id (parser);
5838 /* Look for the `,'. */
5839 cp_parser_require (parser, CPP_COMMA, "`,'");
5840
5841 /* Build the (type *)null that begins the traditional offsetof macro. */
5842 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5843
5844 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5845 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5846 true, &dummy);
5847 while (true)
5848 {
5849 cp_token *token = cp_lexer_peek_token (parser->lexer);
5850 switch (token->type)
5851 {
5852 case CPP_OPEN_SQUARE:
5853 /* offsetof-member-designator "[" expression "]" */
5854 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5855 break;
5856
5857 case CPP_DOT:
5858 /* offsetof-member-designator "." identifier */
5859 cp_lexer_consume_token (parser->lexer);
5860 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5861 true, &dummy);
5862 break;
5863
5864 case CPP_CLOSE_PAREN:
5865 /* Consume the ")" token. */
5866 cp_lexer_consume_token (parser->lexer);
5867 goto success;
5868
5869 default:
5870 /* Error. We know the following require will fail, but
5871 that gives the proper error message. */
5872 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5873 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5874 expr = error_mark_node;
5875 goto failure;
5876 }
5877 }
5878
5879 success:
5880 /* If we're processing a template, we can't finish the semantics yet.
5881 Otherwise we can fold the entire expression now. */
5882 if (processing_template_decl)
5883 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5884 else
5885 expr = fold_offsetof (expr);
5886
5887 failure:
5888 parser->integral_constant_expression_p = save_ice_p;
5889 parser->non_integral_constant_expression_p = save_non_ice_p;
5890
5891 return expr;
5892 }
5893
5894 /* Statements [gram.stmt.stmt] */
5895
5896 /* Parse a statement.
5897
5898 statement:
5899 labeled-statement
5900 expression-statement
5901 compound-statement
5902 selection-statement
5903 iteration-statement
5904 jump-statement
5905 declaration-statement
5906 try-block */
5907
5908 static void
5909 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5910 {
5911 tree statement;
5912 cp_token *token;
5913 location_t statement_location;
5914
5915 /* There is no statement yet. */
5916 statement = NULL_TREE;
5917 /* Peek at the next token. */
5918 token = cp_lexer_peek_token (parser->lexer);
5919 /* Remember the location of the first token in the statement. */
5920 statement_location = token->location;
5921 /* If this is a keyword, then that will often determine what kind of
5922 statement we have. */
5923 if (token->type == CPP_KEYWORD)
5924 {
5925 enum rid keyword = token->keyword;
5926
5927 switch (keyword)
5928 {
5929 case RID_CASE:
5930 case RID_DEFAULT:
5931 statement = cp_parser_labeled_statement (parser,
5932 in_statement_expr);
5933 break;
5934
5935 case RID_IF:
5936 case RID_SWITCH:
5937 statement = cp_parser_selection_statement (parser);
5938 break;
5939
5940 case RID_WHILE:
5941 case RID_DO:
5942 case RID_FOR:
5943 statement = cp_parser_iteration_statement (parser);
5944 break;
5945
5946 case RID_BREAK:
5947 case RID_CONTINUE:
5948 case RID_RETURN:
5949 case RID_GOTO:
5950 statement = cp_parser_jump_statement (parser);
5951 break;
5952
5953 case RID_TRY:
5954 statement = cp_parser_try_block (parser);
5955 break;
5956
5957 default:
5958 /* It might be a keyword like `int' that can start a
5959 declaration-statement. */
5960 break;
5961 }
5962 }
5963 else if (token->type == CPP_NAME)
5964 {
5965 /* If the next token is a `:', then we are looking at a
5966 labeled-statement. */
5967 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5968 if (token->type == CPP_COLON)
5969 statement = cp_parser_labeled_statement (parser, in_statement_expr);
5970 }
5971 /* Anything that starts with a `{' must be a compound-statement. */
5972 else if (token->type == CPP_OPEN_BRACE)
5973 statement = cp_parser_compound_statement (parser, NULL, false);
5974 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5975 a statement all its own. */
5976 else if (token->type == CPP_PRAGMA)
5977 {
5978 cp_lexer_handle_pragma (parser->lexer);
5979 return;
5980 }
5981
5982 /* Everything else must be a declaration-statement or an
5983 expression-statement. Try for the declaration-statement
5984 first, unless we are looking at a `;', in which case we know that
5985 we have an expression-statement. */
5986 if (!statement)
5987 {
5988 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5989 {
5990 cp_parser_parse_tentatively (parser);
5991 /* Try to parse the declaration-statement. */
5992 cp_parser_declaration_statement (parser);
5993 /* If that worked, we're done. */
5994 if (cp_parser_parse_definitely (parser))
5995 return;
5996 }
5997 /* Look for an expression-statement instead. */
5998 statement = cp_parser_expression_statement (parser, in_statement_expr);
5999 }
6000
6001 /* Set the line number for the statement. */
6002 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6003 SET_EXPR_LOCATION (statement, statement_location);
6004 }
6005
6006 /* Parse a labeled-statement.
6007
6008 labeled-statement:
6009 identifier : statement
6010 case constant-expression : statement
6011 default : statement
6012
6013 GNU Extension:
6014
6015 labeled-statement:
6016 case constant-expression ... constant-expression : statement
6017
6018 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6019 For an ordinary label, returns a LABEL_EXPR. */
6020
6021 static tree
6022 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6023 {
6024 cp_token *token;
6025 tree statement = error_mark_node;
6026
6027 /* The next token should be an identifier. */
6028 token = cp_lexer_peek_token (parser->lexer);
6029 if (token->type != CPP_NAME
6030 && token->type != CPP_KEYWORD)
6031 {
6032 cp_parser_error (parser, "expected labeled-statement");
6033 return error_mark_node;
6034 }
6035
6036 switch (token->keyword)
6037 {
6038 case RID_CASE:
6039 {
6040 tree expr, expr_hi;
6041 cp_token *ellipsis;
6042
6043 /* Consume the `case' token. */
6044 cp_lexer_consume_token (parser->lexer);
6045 /* Parse the constant-expression. */
6046 expr = cp_parser_constant_expression (parser,
6047 /*allow_non_constant_p=*/false,
6048 NULL);
6049
6050 ellipsis = cp_lexer_peek_token (parser->lexer);
6051 if (ellipsis->type == CPP_ELLIPSIS)
6052 {
6053 /* Consume the `...' token. */
6054 cp_lexer_consume_token (parser->lexer);
6055 expr_hi =
6056 cp_parser_constant_expression (parser,
6057 /*allow_non_constant_p=*/false,
6058 NULL);
6059 /* We don't need to emit warnings here, as the common code
6060 will do this for us. */
6061 }
6062 else
6063 expr_hi = NULL_TREE;
6064
6065 if (!parser->in_switch_statement_p)
6066 error ("case label %qE not within a switch statement", expr);
6067 else
6068 statement = finish_case_label (expr, expr_hi);
6069 }
6070 break;
6071
6072 case RID_DEFAULT:
6073 /* Consume the `default' token. */
6074 cp_lexer_consume_token (parser->lexer);
6075 if (!parser->in_switch_statement_p)
6076 error ("case label not within a switch statement");
6077 else
6078 statement = finish_case_label (NULL_TREE, NULL_TREE);
6079 break;
6080
6081 default:
6082 /* Anything else must be an ordinary label. */
6083 statement = finish_label_stmt (cp_parser_identifier (parser));
6084 break;
6085 }
6086
6087 /* Require the `:' token. */
6088 cp_parser_require (parser, CPP_COLON, "`:'");
6089 /* Parse the labeled statement. */
6090 cp_parser_statement (parser, in_statement_expr);
6091
6092 /* Return the label, in the case of a `case' or `default' label. */
6093 return statement;
6094 }
6095
6096 /* Parse an expression-statement.
6097
6098 expression-statement:
6099 expression [opt] ;
6100
6101 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6102 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6103 indicates whether this expression-statement is part of an
6104 expression statement. */
6105
6106 static tree
6107 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6108 {
6109 tree statement = NULL_TREE;
6110
6111 /* If the next token is a ';', then there is no expression
6112 statement. */
6113 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6114 statement = cp_parser_expression (parser, /*cast_p=*/false);
6115
6116 /* Consume the final `;'. */
6117 cp_parser_consume_semicolon_at_end_of_statement (parser);
6118
6119 if (in_statement_expr
6120 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6121 /* This is the final expression statement of a statement
6122 expression. */
6123 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6124 else if (statement)
6125 statement = finish_expr_stmt (statement);
6126 else
6127 finish_stmt ();
6128
6129 return statement;
6130 }
6131
6132 /* Parse a compound-statement.
6133
6134 compound-statement:
6135 { statement-seq [opt] }
6136
6137 Returns a tree representing the statement. */
6138
6139 static tree
6140 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6141 bool in_try)
6142 {
6143 tree compound_stmt;
6144
6145 /* Consume the `{'. */
6146 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6147 return error_mark_node;
6148 /* Begin the compound-statement. */
6149 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6150 /* Parse an (optional) statement-seq. */
6151 cp_parser_statement_seq_opt (parser, in_statement_expr);
6152 /* Finish the compound-statement. */
6153 finish_compound_stmt (compound_stmt);
6154 /* Consume the `}'. */
6155 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6156
6157 return compound_stmt;
6158 }
6159
6160 /* Parse an (optional) statement-seq.
6161
6162 statement-seq:
6163 statement
6164 statement-seq [opt] statement */
6165
6166 static void
6167 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6168 {
6169 /* Scan statements until there aren't any more. */
6170 while (true)
6171 {
6172 /* If we're looking at a `}', then we've run out of statements. */
6173 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6174 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6175 break;
6176
6177 /* Parse the statement. */
6178 cp_parser_statement (parser, in_statement_expr);
6179 }
6180 }
6181
6182 /* Parse a selection-statement.
6183
6184 selection-statement:
6185 if ( condition ) statement
6186 if ( condition ) statement else statement
6187 switch ( condition ) statement
6188
6189 Returns the new IF_STMT or SWITCH_STMT. */
6190
6191 static tree
6192 cp_parser_selection_statement (cp_parser* parser)
6193 {
6194 cp_token *token;
6195 enum rid keyword;
6196
6197 /* Peek at the next token. */
6198 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6199
6200 /* See what kind of keyword it is. */
6201 keyword = token->keyword;
6202 switch (keyword)
6203 {
6204 case RID_IF:
6205 case RID_SWITCH:
6206 {
6207 tree statement;
6208 tree condition;
6209
6210 /* Look for the `('. */
6211 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6212 {
6213 cp_parser_skip_to_end_of_statement (parser);
6214 return error_mark_node;
6215 }
6216
6217 /* Begin the selection-statement. */
6218 if (keyword == RID_IF)
6219 statement = begin_if_stmt ();
6220 else
6221 statement = begin_switch_stmt ();
6222
6223 /* Parse the condition. */
6224 condition = cp_parser_condition (parser);
6225 /* Look for the `)'. */
6226 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6227 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6228 /*consume_paren=*/true);
6229
6230 if (keyword == RID_IF)
6231 {
6232 /* Add the condition. */
6233 finish_if_stmt_cond (condition, statement);
6234
6235 /* Parse the then-clause. */
6236 cp_parser_implicitly_scoped_statement (parser);
6237 finish_then_clause (statement);
6238
6239 /* If the next token is `else', parse the else-clause. */
6240 if (cp_lexer_next_token_is_keyword (parser->lexer,
6241 RID_ELSE))
6242 {
6243 /* Consume the `else' keyword. */
6244 cp_lexer_consume_token (parser->lexer);
6245 begin_else_clause (statement);
6246 /* Parse the else-clause. */
6247 cp_parser_implicitly_scoped_statement (parser);
6248 finish_else_clause (statement);
6249 }
6250
6251 /* Now we're all done with the if-statement. */
6252 finish_if_stmt (statement);
6253 }
6254 else
6255 {
6256 bool in_switch_statement_p;
6257
6258 /* Add the condition. */
6259 finish_switch_cond (condition, statement);
6260
6261 /* Parse the body of the switch-statement. */
6262 in_switch_statement_p = parser->in_switch_statement_p;
6263 parser->in_switch_statement_p = true;
6264 cp_parser_implicitly_scoped_statement (parser);
6265 parser->in_switch_statement_p = in_switch_statement_p;
6266
6267 /* Now we're all done with the switch-statement. */
6268 finish_switch_stmt (statement);
6269 }
6270
6271 return statement;
6272 }
6273 break;
6274
6275 default:
6276 cp_parser_error (parser, "expected selection-statement");
6277 return error_mark_node;
6278 }
6279 }
6280
6281 /* Parse a condition.
6282
6283 condition:
6284 expression
6285 type-specifier-seq declarator = assignment-expression
6286
6287 GNU Extension:
6288
6289 condition:
6290 type-specifier-seq declarator asm-specification [opt]
6291 attributes [opt] = assignment-expression
6292
6293 Returns the expression that should be tested. */
6294
6295 static tree
6296 cp_parser_condition (cp_parser* parser)
6297 {
6298 cp_decl_specifier_seq type_specifiers;
6299 const char *saved_message;
6300
6301 /* Try the declaration first. */
6302 cp_parser_parse_tentatively (parser);
6303 /* New types are not allowed in the type-specifier-seq for a
6304 condition. */
6305 saved_message = parser->type_definition_forbidden_message;
6306 parser->type_definition_forbidden_message
6307 = "types may not be defined in conditions";
6308 /* Parse the type-specifier-seq. */
6309 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6310 &type_specifiers);
6311 /* Restore the saved message. */
6312 parser->type_definition_forbidden_message = saved_message;
6313 /* If all is well, we might be looking at a declaration. */
6314 if (!cp_parser_error_occurred (parser))
6315 {
6316 tree decl;
6317 tree asm_specification;
6318 tree attributes;
6319 cp_declarator *declarator;
6320 tree initializer = NULL_TREE;
6321
6322 /* Parse the declarator. */
6323 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6324 /*ctor_dtor_or_conv_p=*/NULL,
6325 /*parenthesized_p=*/NULL,
6326 /*member_p=*/false);
6327 /* Parse the attributes. */
6328 attributes = cp_parser_attributes_opt (parser);
6329 /* Parse the asm-specification. */
6330 asm_specification = cp_parser_asm_specification_opt (parser);
6331 /* If the next token is not an `=', then we might still be
6332 looking at an expression. For example:
6333
6334 if (A(a).x)
6335
6336 looks like a decl-specifier-seq and a declarator -- but then
6337 there is no `=', so this is an expression. */
6338 cp_parser_require (parser, CPP_EQ, "`='");
6339 /* If we did see an `=', then we are looking at a declaration
6340 for sure. */
6341 if (cp_parser_parse_definitely (parser))
6342 {
6343 tree pushed_scope;
6344
6345 /* Create the declaration. */
6346 decl = start_decl (declarator, &type_specifiers,
6347 /*initialized_p=*/true,
6348 attributes, /*prefix_attributes=*/NULL_TREE,
6349 &pushed_scope);
6350 /* Parse the assignment-expression. */
6351 initializer = cp_parser_assignment_expression (parser,
6352 /*cast_p=*/false);
6353
6354 /* Process the initializer. */
6355 cp_finish_decl (decl,
6356 initializer,
6357 asm_specification,
6358 LOOKUP_ONLYCONVERTING);
6359
6360 if (pushed_scope)
6361 pop_scope (pushed_scope);
6362
6363 return convert_from_reference (decl);
6364 }
6365 }
6366 /* If we didn't even get past the declarator successfully, we are
6367 definitely not looking at a declaration. */
6368 else
6369 cp_parser_abort_tentative_parse (parser);
6370
6371 /* Otherwise, we are looking at an expression. */
6372 return cp_parser_expression (parser, /*cast_p=*/false);
6373 }
6374
6375 /* Parse an iteration-statement.
6376
6377 iteration-statement:
6378 while ( condition ) statement
6379 do statement while ( expression ) ;
6380 for ( for-init-statement condition [opt] ; expression [opt] )
6381 statement
6382
6383 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6384
6385 static tree
6386 cp_parser_iteration_statement (cp_parser* parser)
6387 {
6388 cp_token *token;
6389 enum rid keyword;
6390 tree statement;
6391 bool in_iteration_statement_p;
6392
6393
6394 /* Peek at the next token. */
6395 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6396 if (!token)
6397 return error_mark_node;
6398
6399 /* Remember whether or not we are already within an iteration
6400 statement. */
6401 in_iteration_statement_p = parser->in_iteration_statement_p;
6402
6403 /* See what kind of keyword it is. */
6404 keyword = token->keyword;
6405 switch (keyword)
6406 {
6407 case RID_WHILE:
6408 {
6409 tree condition;
6410
6411 /* Begin the while-statement. */
6412 statement = begin_while_stmt ();
6413 /* Look for the `('. */
6414 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6415 /* Parse the condition. */
6416 condition = cp_parser_condition (parser);
6417 finish_while_stmt_cond (condition, statement);
6418 /* Look for the `)'. */
6419 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6420 /* Parse the dependent statement. */
6421 parser->in_iteration_statement_p = true;
6422 cp_parser_already_scoped_statement (parser);
6423 parser->in_iteration_statement_p = in_iteration_statement_p;
6424 /* We're done with the while-statement. */
6425 finish_while_stmt (statement);
6426 }
6427 break;
6428
6429 case RID_DO:
6430 {
6431 tree expression;
6432
6433 /* Begin the do-statement. */
6434 statement = begin_do_stmt ();
6435 /* Parse the body of the do-statement. */
6436 parser->in_iteration_statement_p = true;
6437 cp_parser_implicitly_scoped_statement (parser);
6438 parser->in_iteration_statement_p = in_iteration_statement_p;
6439 finish_do_body (statement);
6440 /* Look for the `while' keyword. */
6441 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6442 /* Look for the `('. */
6443 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6444 /* Parse the expression. */
6445 expression = cp_parser_expression (parser, /*cast_p=*/false);
6446 /* We're done with the do-statement. */
6447 finish_do_stmt (expression, statement);
6448 /* Look for the `)'. */
6449 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6450 /* Look for the `;'. */
6451 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6452 }
6453 break;
6454
6455 case RID_FOR:
6456 {
6457 tree condition = NULL_TREE;
6458 tree expression = NULL_TREE;
6459
6460 /* Begin the for-statement. */
6461 statement = begin_for_stmt ();
6462 /* Look for the `('. */
6463 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6464 /* Parse the initialization. */
6465 cp_parser_for_init_statement (parser);
6466 finish_for_init_stmt (statement);
6467
6468 /* If there's a condition, process it. */
6469 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6470 condition = cp_parser_condition (parser);
6471 finish_for_cond (condition, statement);
6472 /* Look for the `;'. */
6473 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6474
6475 /* If there's an expression, process it. */
6476 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6477 expression = cp_parser_expression (parser, /*cast_p=*/false);
6478 finish_for_expr (expression, statement);
6479 /* Look for the `)'. */
6480 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6481
6482 /* Parse the body of the for-statement. */
6483 parser->in_iteration_statement_p = true;
6484 cp_parser_already_scoped_statement (parser);
6485 parser->in_iteration_statement_p = in_iteration_statement_p;
6486
6487 /* We're done with the for-statement. */
6488 finish_for_stmt (statement);
6489 }
6490 break;
6491
6492 default:
6493 cp_parser_error (parser, "expected iteration-statement");
6494 statement = error_mark_node;
6495 break;
6496 }
6497
6498 return statement;
6499 }
6500
6501 /* Parse a for-init-statement.
6502
6503 for-init-statement:
6504 expression-statement
6505 simple-declaration */
6506
6507 static void
6508 cp_parser_for_init_statement (cp_parser* parser)
6509 {
6510 /* If the next token is a `;', then we have an empty
6511 expression-statement. Grammatically, this is also a
6512 simple-declaration, but an invalid one, because it does not
6513 declare anything. Therefore, if we did not handle this case
6514 specially, we would issue an error message about an invalid
6515 declaration. */
6516 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6517 {
6518 /* We're going to speculatively look for a declaration, falling back
6519 to an expression, if necessary. */
6520 cp_parser_parse_tentatively (parser);
6521 /* Parse the declaration. */
6522 cp_parser_simple_declaration (parser,
6523 /*function_definition_allowed_p=*/false);
6524 /* If the tentative parse failed, then we shall need to look for an
6525 expression-statement. */
6526 if (cp_parser_parse_definitely (parser))
6527 return;
6528 }
6529
6530 cp_parser_expression_statement (parser, false);
6531 }
6532
6533 /* Parse a jump-statement.
6534
6535 jump-statement:
6536 break ;
6537 continue ;
6538 return expression [opt] ;
6539 goto identifier ;
6540
6541 GNU extension:
6542
6543 jump-statement:
6544 goto * expression ;
6545
6546 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6547
6548 static tree
6549 cp_parser_jump_statement (cp_parser* parser)
6550 {
6551 tree statement = error_mark_node;
6552 cp_token *token;
6553 enum rid keyword;
6554
6555 /* Peek at the next token. */
6556 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6557 if (!token)
6558 return error_mark_node;
6559
6560 /* See what kind of keyword it is. */
6561 keyword = token->keyword;
6562 switch (keyword)
6563 {
6564 case RID_BREAK:
6565 if (!parser->in_switch_statement_p
6566 && !parser->in_iteration_statement_p)
6567 {
6568 error ("break statement not within loop or switch");
6569 statement = error_mark_node;
6570 }
6571 else
6572 statement = finish_break_stmt ();
6573 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6574 break;
6575
6576 case RID_CONTINUE:
6577 if (!parser->in_iteration_statement_p)
6578 {
6579 error ("continue statement not within a loop");
6580 statement = error_mark_node;
6581 }
6582 else
6583 statement = finish_continue_stmt ();
6584 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6585 break;
6586
6587 case RID_RETURN:
6588 {
6589 tree expr;
6590
6591 /* If the next token is a `;', then there is no
6592 expression. */
6593 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6594 expr = cp_parser_expression (parser, /*cast_p=*/false);
6595 else
6596 expr = NULL_TREE;
6597 /* Build the return-statement. */
6598 statement = finish_return_stmt (expr);
6599 /* Look for the final `;'. */
6600 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6601 }
6602 break;
6603
6604 case RID_GOTO:
6605 /* Create the goto-statement. */
6606 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6607 {
6608 /* Issue a warning about this use of a GNU extension. */
6609 if (pedantic)
6610 pedwarn ("ISO C++ forbids computed gotos");
6611 /* Consume the '*' token. */
6612 cp_lexer_consume_token (parser->lexer);
6613 /* Parse the dependent expression. */
6614 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6615 }
6616 else
6617 finish_goto_stmt (cp_parser_identifier (parser));
6618 /* Look for the final `;'. */
6619 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6620 break;
6621
6622 default:
6623 cp_parser_error (parser, "expected jump-statement");
6624 break;
6625 }
6626
6627 return statement;
6628 }
6629
6630 /* Parse a declaration-statement.
6631
6632 declaration-statement:
6633 block-declaration */
6634
6635 static void
6636 cp_parser_declaration_statement (cp_parser* parser)
6637 {
6638 void *p;
6639
6640 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6641 p = obstack_alloc (&declarator_obstack, 0);
6642
6643 /* Parse the block-declaration. */
6644 cp_parser_block_declaration (parser, /*statement_p=*/true);
6645
6646 /* Free any declarators allocated. */
6647 obstack_free (&declarator_obstack, p);
6648
6649 /* Finish off the statement. */
6650 finish_stmt ();
6651 }
6652
6653 /* Some dependent statements (like `if (cond) statement'), are
6654 implicitly in their own scope. In other words, if the statement is
6655 a single statement (as opposed to a compound-statement), it is
6656 none-the-less treated as if it were enclosed in braces. Any
6657 declarations appearing in the dependent statement are out of scope
6658 after control passes that point. This function parses a statement,
6659 but ensures that is in its own scope, even if it is not a
6660 compound-statement.
6661
6662 Returns the new statement. */
6663
6664 static tree
6665 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6666 {
6667 tree statement;
6668
6669 /* If the token is not a `{', then we must take special action. */
6670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6671 {
6672 /* Create a compound-statement. */
6673 statement = begin_compound_stmt (0);
6674 /* Parse the dependent-statement. */
6675 cp_parser_statement (parser, false);
6676 /* Finish the dummy compound-statement. */
6677 finish_compound_stmt (statement);
6678 }
6679 /* Otherwise, we simply parse the statement directly. */
6680 else
6681 statement = cp_parser_compound_statement (parser, NULL, false);
6682
6683 /* Return the statement. */
6684 return statement;
6685 }
6686
6687 /* For some dependent statements (like `while (cond) statement'), we
6688 have already created a scope. Therefore, even if the dependent
6689 statement is a compound-statement, we do not want to create another
6690 scope. */
6691
6692 static void
6693 cp_parser_already_scoped_statement (cp_parser* parser)
6694 {
6695 /* If the token is a `{', then we must take special action. */
6696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6697 cp_parser_statement (parser, false);
6698 else
6699 {
6700 /* Avoid calling cp_parser_compound_statement, so that we
6701 don't create a new scope. Do everything else by hand. */
6702 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6703 cp_parser_statement_seq_opt (parser, false);
6704 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6705 }
6706 }
6707
6708 /* Declarations [gram.dcl.dcl] */
6709
6710 /* Parse an optional declaration-sequence.
6711
6712 declaration-seq:
6713 declaration
6714 declaration-seq declaration */
6715
6716 static void
6717 cp_parser_declaration_seq_opt (cp_parser* parser)
6718 {
6719 while (true)
6720 {
6721 cp_token *token;
6722
6723 token = cp_lexer_peek_token (parser->lexer);
6724
6725 if (token->type == CPP_CLOSE_BRACE
6726 || token->type == CPP_EOF)
6727 break;
6728
6729 if (token->type == CPP_SEMICOLON)
6730 {
6731 /* A declaration consisting of a single semicolon is
6732 invalid. Allow it unless we're being pedantic. */
6733 cp_lexer_consume_token (parser->lexer);
6734 if (pedantic && !in_system_header)
6735 pedwarn ("extra %<;%>");
6736 continue;
6737 }
6738
6739 /* If we're entering or exiting a region that's implicitly
6740 extern "C", modify the lang context appropriately. */
6741 if (!parser->implicit_extern_c && token->implicit_extern_c)
6742 {
6743 push_lang_context (lang_name_c);
6744 parser->implicit_extern_c = true;
6745 }
6746 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6747 {
6748 pop_lang_context ();
6749 parser->implicit_extern_c = false;
6750 }
6751
6752 if (token->type == CPP_PRAGMA)
6753 {
6754 /* A top-level declaration can consist solely of a #pragma.
6755 A nested declaration cannot, so this is done here and not
6756 in cp_parser_declaration. (A #pragma at block scope is
6757 handled in cp_parser_statement.) */
6758 cp_lexer_handle_pragma (parser->lexer);
6759 continue;
6760 }
6761
6762 /* Parse the declaration itself. */
6763 cp_parser_declaration (parser);
6764 }
6765 }
6766
6767 /* Parse a declaration.
6768
6769 declaration:
6770 block-declaration
6771 function-definition
6772 template-declaration
6773 explicit-instantiation
6774 explicit-specialization
6775 linkage-specification
6776 namespace-definition
6777
6778 GNU extension:
6779
6780 declaration:
6781 __extension__ declaration */
6782
6783 static void
6784 cp_parser_declaration (cp_parser* parser)
6785 {
6786 cp_token token1;
6787 cp_token token2;
6788 int saved_pedantic;
6789 void *p;
6790
6791 /* Check for the `__extension__' keyword. */
6792 if (cp_parser_extension_opt (parser, &saved_pedantic))
6793 {
6794 /* Parse the qualified declaration. */
6795 cp_parser_declaration (parser);
6796 /* Restore the PEDANTIC flag. */
6797 pedantic = saved_pedantic;
6798
6799 return;
6800 }
6801
6802 /* Try to figure out what kind of declaration is present. */
6803 token1 = *cp_lexer_peek_token (parser->lexer);
6804
6805 if (token1.type != CPP_EOF)
6806 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6807
6808 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6809 p = obstack_alloc (&declarator_obstack, 0);
6810
6811 /* If the next token is `extern' and the following token is a string
6812 literal, then we have a linkage specification. */
6813 if (token1.keyword == RID_EXTERN
6814 && cp_parser_is_string_literal (&token2))
6815 cp_parser_linkage_specification (parser);
6816 /* If the next token is `template', then we have either a template
6817 declaration, an explicit instantiation, or an explicit
6818 specialization. */
6819 else if (token1.keyword == RID_TEMPLATE)
6820 {
6821 /* `template <>' indicates a template specialization. */
6822 if (token2.type == CPP_LESS
6823 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6824 cp_parser_explicit_specialization (parser);
6825 /* `template <' indicates a template declaration. */
6826 else if (token2.type == CPP_LESS)
6827 cp_parser_template_declaration (parser, /*member_p=*/false);
6828 /* Anything else must be an explicit instantiation. */
6829 else
6830 cp_parser_explicit_instantiation (parser);
6831 }
6832 /* If the next token is `export', then we have a template
6833 declaration. */
6834 else if (token1.keyword == RID_EXPORT)
6835 cp_parser_template_declaration (parser, /*member_p=*/false);
6836 /* If the next token is `extern', 'static' or 'inline' and the one
6837 after that is `template', we have a GNU extended explicit
6838 instantiation directive. */
6839 else if (cp_parser_allow_gnu_extensions_p (parser)
6840 && (token1.keyword == RID_EXTERN
6841 || token1.keyword == RID_STATIC
6842 || token1.keyword == RID_INLINE)
6843 && token2.keyword == RID_TEMPLATE)
6844 cp_parser_explicit_instantiation (parser);
6845 /* If the next token is `namespace', check for a named or unnamed
6846 namespace definition. */
6847 else if (token1.keyword == RID_NAMESPACE
6848 && (/* A named namespace definition. */
6849 (token2.type == CPP_NAME
6850 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6851 == CPP_OPEN_BRACE))
6852 /* An unnamed namespace definition. */
6853 || token2.type == CPP_OPEN_BRACE))
6854 cp_parser_namespace_definition (parser);
6855 /* We must have either a block declaration or a function
6856 definition. */
6857 else
6858 /* Try to parse a block-declaration, or a function-definition. */
6859 cp_parser_block_declaration (parser, /*statement_p=*/false);
6860
6861 /* Free any declarators allocated. */
6862 obstack_free (&declarator_obstack, p);
6863 }
6864
6865 /* Parse a block-declaration.
6866
6867 block-declaration:
6868 simple-declaration
6869 asm-definition
6870 namespace-alias-definition
6871 using-declaration
6872 using-directive
6873
6874 GNU Extension:
6875
6876 block-declaration:
6877 __extension__ block-declaration
6878 label-declaration
6879
6880 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6881 part of a declaration-statement. */
6882
6883 static void
6884 cp_parser_block_declaration (cp_parser *parser,
6885 bool statement_p)
6886 {
6887 cp_token *token1;
6888 int saved_pedantic;
6889
6890 /* Check for the `__extension__' keyword. */
6891 if (cp_parser_extension_opt (parser, &saved_pedantic))
6892 {
6893 /* Parse the qualified declaration. */
6894 cp_parser_block_declaration (parser, statement_p);
6895 /* Restore the PEDANTIC flag. */
6896 pedantic = saved_pedantic;
6897
6898 return;
6899 }
6900
6901 /* Peek at the next token to figure out which kind of declaration is
6902 present. */
6903 token1 = cp_lexer_peek_token (parser->lexer);
6904
6905 /* If the next keyword is `asm', we have an asm-definition. */
6906 if (token1->keyword == RID_ASM)
6907 {
6908 if (statement_p)
6909 cp_parser_commit_to_tentative_parse (parser);
6910 cp_parser_asm_definition (parser);
6911 }
6912 /* If the next keyword is `namespace', we have a
6913 namespace-alias-definition. */
6914 else if (token1->keyword == RID_NAMESPACE)
6915 cp_parser_namespace_alias_definition (parser);
6916 /* If the next keyword is `using', we have either a
6917 using-declaration or a using-directive. */
6918 else if (token1->keyword == RID_USING)
6919 {
6920 cp_token *token2;
6921
6922 if (statement_p)
6923 cp_parser_commit_to_tentative_parse (parser);
6924 /* If the token after `using' is `namespace', then we have a
6925 using-directive. */
6926 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6927 if (token2->keyword == RID_NAMESPACE)
6928 cp_parser_using_directive (parser);
6929 /* Otherwise, it's a using-declaration. */
6930 else
6931 cp_parser_using_declaration (parser);
6932 }
6933 /* If the next keyword is `__label__' we have a label declaration. */
6934 else if (token1->keyword == RID_LABEL)
6935 {
6936 if (statement_p)
6937 cp_parser_commit_to_tentative_parse (parser);
6938 cp_parser_label_declaration (parser);
6939 }
6940 /* Anything else must be a simple-declaration. */
6941 else
6942 cp_parser_simple_declaration (parser, !statement_p);
6943 }
6944
6945 /* Parse a simple-declaration.
6946
6947 simple-declaration:
6948 decl-specifier-seq [opt] init-declarator-list [opt] ;
6949
6950 init-declarator-list:
6951 init-declarator
6952 init-declarator-list , init-declarator
6953
6954 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6955 function-definition as a simple-declaration. */
6956
6957 static void
6958 cp_parser_simple_declaration (cp_parser* parser,
6959 bool function_definition_allowed_p)
6960 {
6961 cp_decl_specifier_seq decl_specifiers;
6962 int declares_class_or_enum;
6963 bool saw_declarator;
6964
6965 /* Defer access checks until we know what is being declared; the
6966 checks for names appearing in the decl-specifier-seq should be
6967 done as if we were in the scope of the thing being declared. */
6968 push_deferring_access_checks (dk_deferred);
6969
6970 /* Parse the decl-specifier-seq. We have to keep track of whether
6971 or not the decl-specifier-seq declares a named class or
6972 enumeration type, since that is the only case in which the
6973 init-declarator-list is allowed to be empty.
6974
6975 [dcl.dcl]
6976
6977 In a simple-declaration, the optional init-declarator-list can be
6978 omitted only when declaring a class or enumeration, that is when
6979 the decl-specifier-seq contains either a class-specifier, an
6980 elaborated-type-specifier, or an enum-specifier. */
6981 cp_parser_decl_specifier_seq (parser,
6982 CP_PARSER_FLAGS_OPTIONAL,
6983 &decl_specifiers,
6984 &declares_class_or_enum);
6985 /* We no longer need to defer access checks. */
6986 stop_deferring_access_checks ();
6987
6988 /* In a block scope, a valid declaration must always have a
6989 decl-specifier-seq. By not trying to parse declarators, we can
6990 resolve the declaration/expression ambiguity more quickly. */
6991 if (!function_definition_allowed_p
6992 && !decl_specifiers.any_specifiers_p)
6993 {
6994 cp_parser_error (parser, "expected declaration");
6995 goto done;
6996 }
6997
6998 /* If the next two tokens are both identifiers, the code is
6999 erroneous. The usual cause of this situation is code like:
7000
7001 T t;
7002
7003 where "T" should name a type -- but does not. */
7004 if (!decl_specifiers.type
7005 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7006 {
7007 /* If parsing tentatively, we should commit; we really are
7008 looking at a declaration. */
7009 cp_parser_commit_to_tentative_parse (parser);
7010 /* Give up. */
7011 goto done;
7012 }
7013
7014 /* If we have seen at least one decl-specifier, and the next token
7015 is not a parenthesis, then we must be looking at a declaration.
7016 (After "int (" we might be looking at a functional cast.) */
7017 if (decl_specifiers.any_specifiers_p
7018 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7019 cp_parser_commit_to_tentative_parse (parser);
7020
7021 /* Keep going until we hit the `;' at the end of the simple
7022 declaration. */
7023 saw_declarator = false;
7024 while (cp_lexer_next_token_is_not (parser->lexer,
7025 CPP_SEMICOLON))
7026 {
7027 cp_token *token;
7028 bool function_definition_p;
7029 tree decl;
7030
7031 saw_declarator = true;
7032 /* Parse the init-declarator. */
7033 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7034 function_definition_allowed_p,
7035 /*member_p=*/false,
7036 declares_class_or_enum,
7037 &function_definition_p);
7038 /* If an error occurred while parsing tentatively, exit quickly.
7039 (That usually happens when in the body of a function; each
7040 statement is treated as a declaration-statement until proven
7041 otherwise.) */
7042 if (cp_parser_error_occurred (parser))
7043 goto done;
7044 /* Handle function definitions specially. */
7045 if (function_definition_p)
7046 {
7047 /* If the next token is a `,', then we are probably
7048 processing something like:
7049
7050 void f() {}, *p;
7051
7052 which is erroneous. */
7053 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7054 error ("mixing declarations and function-definitions is forbidden");
7055 /* Otherwise, we're done with the list of declarators. */
7056 else
7057 {
7058 pop_deferring_access_checks ();
7059 return;
7060 }
7061 }
7062 /* The next token should be either a `,' or a `;'. */
7063 token = cp_lexer_peek_token (parser->lexer);
7064 /* If it's a `,', there are more declarators to come. */
7065 if (token->type == CPP_COMMA)
7066 cp_lexer_consume_token (parser->lexer);
7067 /* If it's a `;', we are done. */
7068 else if (token->type == CPP_SEMICOLON)
7069 break;
7070 /* Anything else is an error. */
7071 else
7072 {
7073 /* If we have already issued an error message we don't need
7074 to issue another one. */
7075 if (decl != error_mark_node
7076 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7077 cp_parser_error (parser, "expected %<,%> or %<;%>");
7078 /* Skip tokens until we reach the end of the statement. */
7079 cp_parser_skip_to_end_of_statement (parser);
7080 /* If the next token is now a `;', consume it. */
7081 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7082 cp_lexer_consume_token (parser->lexer);
7083 goto done;
7084 }
7085 /* After the first time around, a function-definition is not
7086 allowed -- even if it was OK at first. For example:
7087
7088 int i, f() {}
7089
7090 is not valid. */
7091 function_definition_allowed_p = false;
7092 }
7093
7094 /* Issue an error message if no declarators are present, and the
7095 decl-specifier-seq does not itself declare a class or
7096 enumeration. */
7097 if (!saw_declarator)
7098 {
7099 if (cp_parser_declares_only_class_p (parser))
7100 shadow_tag (&decl_specifiers);
7101 /* Perform any deferred access checks. */
7102 perform_deferred_access_checks ();
7103 }
7104
7105 /* Consume the `;'. */
7106 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7107
7108 done:
7109 pop_deferring_access_checks ();
7110 }
7111
7112 /* Parse a decl-specifier-seq.
7113
7114 decl-specifier-seq:
7115 decl-specifier-seq [opt] decl-specifier
7116
7117 decl-specifier:
7118 storage-class-specifier
7119 type-specifier
7120 function-specifier
7121 friend
7122 typedef
7123
7124 GNU Extension:
7125
7126 decl-specifier:
7127 attributes
7128
7129 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7130
7131 The parser flags FLAGS is used to control type-specifier parsing.
7132
7133 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7134 flags:
7135
7136 1: one of the decl-specifiers is an elaborated-type-specifier
7137 (i.e., a type declaration)
7138 2: one of the decl-specifiers is an enum-specifier or a
7139 class-specifier (i.e., a type definition)
7140
7141 */
7142
7143 static void
7144 cp_parser_decl_specifier_seq (cp_parser* parser,
7145 cp_parser_flags flags,
7146 cp_decl_specifier_seq *decl_specs,
7147 int* declares_class_or_enum)
7148 {
7149 bool constructor_possible_p = !parser->in_declarator_p;
7150
7151 /* Clear DECL_SPECS. */
7152 clear_decl_specs (decl_specs);
7153
7154 /* Assume no class or enumeration type is declared. */
7155 *declares_class_or_enum = 0;
7156
7157 /* Keep reading specifiers until there are no more to read. */
7158 while (true)
7159 {
7160 bool constructor_p;
7161 bool found_decl_spec;
7162 cp_token *token;
7163
7164 /* Peek at the next token. */
7165 token = cp_lexer_peek_token (parser->lexer);
7166 /* Handle attributes. */
7167 if (token->keyword == RID_ATTRIBUTE)
7168 {
7169 /* Parse the attributes. */
7170 decl_specs->attributes
7171 = chainon (decl_specs->attributes,
7172 cp_parser_attributes_opt (parser));
7173 continue;
7174 }
7175 /* Assume we will find a decl-specifier keyword. */
7176 found_decl_spec = true;
7177 /* If the next token is an appropriate keyword, we can simply
7178 add it to the list. */
7179 switch (token->keyword)
7180 {
7181 /* decl-specifier:
7182 friend */
7183 case RID_FRIEND:
7184 if (decl_specs->specs[(int) ds_friend]++)
7185 error ("duplicate %<friend%>");
7186 /* Consume the token. */
7187 cp_lexer_consume_token (parser->lexer);
7188 break;
7189
7190 /* function-specifier:
7191 inline
7192 virtual
7193 explicit */
7194 case RID_INLINE:
7195 case RID_VIRTUAL:
7196 case RID_EXPLICIT:
7197 cp_parser_function_specifier_opt (parser, decl_specs);
7198 break;
7199
7200 /* decl-specifier:
7201 typedef */
7202 case RID_TYPEDEF:
7203 ++decl_specs->specs[(int) ds_typedef];
7204 /* Consume the token. */
7205 cp_lexer_consume_token (parser->lexer);
7206 /* A constructor declarator cannot appear in a typedef. */
7207 constructor_possible_p = false;
7208 /* The "typedef" keyword can only occur in a declaration; we
7209 may as well commit at this point. */
7210 cp_parser_commit_to_tentative_parse (parser);
7211 break;
7212
7213 /* storage-class-specifier:
7214 auto
7215 register
7216 static
7217 extern
7218 mutable
7219
7220 GNU Extension:
7221 thread */
7222 case RID_AUTO:
7223 /* Consume the token. */
7224 cp_lexer_consume_token (parser->lexer);
7225 cp_parser_set_storage_class (decl_specs, sc_auto);
7226 break;
7227 case RID_REGISTER:
7228 /* Consume the token. */
7229 cp_lexer_consume_token (parser->lexer);
7230 cp_parser_set_storage_class (decl_specs, sc_register);
7231 break;
7232 case RID_STATIC:
7233 /* Consume the token. */
7234 cp_lexer_consume_token (parser->lexer);
7235 if (decl_specs->specs[(int) ds_thread])
7236 {
7237 error ("%<__thread%> before %<static%>");
7238 decl_specs->specs[(int) ds_thread] = 0;
7239 }
7240 cp_parser_set_storage_class (decl_specs, sc_static);
7241 break;
7242 case RID_EXTERN:
7243 /* Consume the token. */
7244 cp_lexer_consume_token (parser->lexer);
7245 if (decl_specs->specs[(int) ds_thread])
7246 {
7247 error ("%<__thread%> before %<extern%>");
7248 decl_specs->specs[(int) ds_thread] = 0;
7249 }
7250 cp_parser_set_storage_class (decl_specs, sc_extern);
7251 break;
7252 case RID_MUTABLE:
7253 /* Consume the token. */
7254 cp_lexer_consume_token (parser->lexer);
7255 cp_parser_set_storage_class (decl_specs, sc_mutable);
7256 break;
7257 case RID_THREAD:
7258 /* Consume the token. */
7259 cp_lexer_consume_token (parser->lexer);
7260 ++decl_specs->specs[(int) ds_thread];
7261 break;
7262
7263 default:
7264 /* We did not yet find a decl-specifier yet. */
7265 found_decl_spec = false;
7266 break;
7267 }
7268
7269 /* Constructors are a special case. The `S' in `S()' is not a
7270 decl-specifier; it is the beginning of the declarator. */
7271 constructor_p
7272 = (!found_decl_spec
7273 && constructor_possible_p
7274 && (cp_parser_constructor_declarator_p
7275 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7276
7277 /* If we don't have a DECL_SPEC yet, then we must be looking at
7278 a type-specifier. */
7279 if (!found_decl_spec && !constructor_p)
7280 {
7281 int decl_spec_declares_class_or_enum;
7282 bool is_cv_qualifier;
7283 tree type_spec;
7284
7285 type_spec
7286 = cp_parser_type_specifier (parser, flags,
7287 decl_specs,
7288 /*is_declaration=*/true,
7289 &decl_spec_declares_class_or_enum,
7290 &is_cv_qualifier);
7291
7292 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7293
7294 /* If this type-specifier referenced a user-defined type
7295 (a typedef, class-name, etc.), then we can't allow any
7296 more such type-specifiers henceforth.
7297
7298 [dcl.spec]
7299
7300 The longest sequence of decl-specifiers that could
7301 possibly be a type name is taken as the
7302 decl-specifier-seq of a declaration. The sequence shall
7303 be self-consistent as described below.
7304
7305 [dcl.type]
7306
7307 As a general rule, at most one type-specifier is allowed
7308 in the complete decl-specifier-seq of a declaration. The
7309 only exceptions are the following:
7310
7311 -- const or volatile can be combined with any other
7312 type-specifier.
7313
7314 -- signed or unsigned can be combined with char, long,
7315 short, or int.
7316
7317 -- ..
7318
7319 Example:
7320
7321 typedef char* Pc;
7322 void g (const int Pc);
7323
7324 Here, Pc is *not* part of the decl-specifier seq; it's
7325 the declarator. Therefore, once we see a type-specifier
7326 (other than a cv-qualifier), we forbid any additional
7327 user-defined types. We *do* still allow things like `int
7328 int' to be considered a decl-specifier-seq, and issue the
7329 error message later. */
7330 if (type_spec && !is_cv_qualifier)
7331 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7332 /* A constructor declarator cannot follow a type-specifier. */
7333 if (type_spec)
7334 {
7335 constructor_possible_p = false;
7336 found_decl_spec = true;
7337 }
7338 }
7339
7340 /* If we still do not have a DECL_SPEC, then there are no more
7341 decl-specifiers. */
7342 if (!found_decl_spec)
7343 break;
7344
7345 decl_specs->any_specifiers_p = true;
7346 /* After we see one decl-specifier, further decl-specifiers are
7347 always optional. */
7348 flags |= CP_PARSER_FLAGS_OPTIONAL;
7349 }
7350
7351 /* Don't allow a friend specifier with a class definition. */
7352 if (decl_specs->specs[(int) ds_friend] != 0
7353 && (*declares_class_or_enum & 2))
7354 error ("class definition may not be declared a friend");
7355 }
7356
7357 /* Parse an (optional) storage-class-specifier.
7358
7359 storage-class-specifier:
7360 auto
7361 register
7362 static
7363 extern
7364 mutable
7365
7366 GNU Extension:
7367
7368 storage-class-specifier:
7369 thread
7370
7371 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7372
7373 static tree
7374 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7375 {
7376 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7377 {
7378 case RID_AUTO:
7379 case RID_REGISTER:
7380 case RID_STATIC:
7381 case RID_EXTERN:
7382 case RID_MUTABLE:
7383 case RID_THREAD:
7384 /* Consume the token. */
7385 return cp_lexer_consume_token (parser->lexer)->value;
7386
7387 default:
7388 return NULL_TREE;
7389 }
7390 }
7391
7392 /* Parse an (optional) function-specifier.
7393
7394 function-specifier:
7395 inline
7396 virtual
7397 explicit
7398
7399 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7400 Updates DECL_SPECS, if it is non-NULL. */
7401
7402 static tree
7403 cp_parser_function_specifier_opt (cp_parser* parser,
7404 cp_decl_specifier_seq *decl_specs)
7405 {
7406 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7407 {
7408 case RID_INLINE:
7409 if (decl_specs)
7410 ++decl_specs->specs[(int) ds_inline];
7411 break;
7412
7413 case RID_VIRTUAL:
7414 if (decl_specs)
7415 ++decl_specs->specs[(int) ds_virtual];
7416 break;
7417
7418 case RID_EXPLICIT:
7419 if (decl_specs)
7420 ++decl_specs->specs[(int) ds_explicit];
7421 break;
7422
7423 default:
7424 return NULL_TREE;
7425 }
7426
7427 /* Consume the token. */
7428 return cp_lexer_consume_token (parser->lexer)->value;
7429 }
7430
7431 /* Parse a linkage-specification.
7432
7433 linkage-specification:
7434 extern string-literal { declaration-seq [opt] }
7435 extern string-literal declaration */
7436
7437 static void
7438 cp_parser_linkage_specification (cp_parser* parser)
7439 {
7440 tree linkage;
7441
7442 /* Look for the `extern' keyword. */
7443 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7444
7445 /* Look for the string-literal. */
7446 linkage = cp_parser_string_literal (parser, false, false);
7447
7448 /* Transform the literal into an identifier. If the literal is a
7449 wide-character string, or contains embedded NULs, then we can't
7450 handle it as the user wants. */
7451 if (strlen (TREE_STRING_POINTER (linkage))
7452 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7453 {
7454 cp_parser_error (parser, "invalid linkage-specification");
7455 /* Assume C++ linkage. */
7456 linkage = lang_name_cplusplus;
7457 }
7458 else
7459 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7460
7461 /* We're now using the new linkage. */
7462 push_lang_context (linkage);
7463
7464 /* If the next token is a `{', then we're using the first
7465 production. */
7466 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7467 {
7468 /* Consume the `{' token. */
7469 cp_lexer_consume_token (parser->lexer);
7470 /* Parse the declarations. */
7471 cp_parser_declaration_seq_opt (parser);
7472 /* Look for the closing `}'. */
7473 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7474 }
7475 /* Otherwise, there's just one declaration. */
7476 else
7477 {
7478 bool saved_in_unbraced_linkage_specification_p;
7479
7480 saved_in_unbraced_linkage_specification_p
7481 = parser->in_unbraced_linkage_specification_p;
7482 parser->in_unbraced_linkage_specification_p = true;
7483 have_extern_spec = true;
7484 cp_parser_declaration (parser);
7485 have_extern_spec = false;
7486 parser->in_unbraced_linkage_specification_p
7487 = saved_in_unbraced_linkage_specification_p;
7488 }
7489
7490 /* We're done with the linkage-specification. */
7491 pop_lang_context ();
7492 }
7493
7494 /* Special member functions [gram.special] */
7495
7496 /* Parse a conversion-function-id.
7497
7498 conversion-function-id:
7499 operator conversion-type-id
7500
7501 Returns an IDENTIFIER_NODE representing the operator. */
7502
7503 static tree
7504 cp_parser_conversion_function_id (cp_parser* parser)
7505 {
7506 tree type;
7507 tree saved_scope;
7508 tree saved_qualifying_scope;
7509 tree saved_object_scope;
7510 tree pushed_scope = NULL_TREE;
7511
7512 /* Look for the `operator' token. */
7513 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7514 return error_mark_node;
7515 /* When we parse the conversion-type-id, the current scope will be
7516 reset. However, we need that information in able to look up the
7517 conversion function later, so we save it here. */
7518 saved_scope = parser->scope;
7519 saved_qualifying_scope = parser->qualifying_scope;
7520 saved_object_scope = parser->object_scope;
7521 /* We must enter the scope of the class so that the names of
7522 entities declared within the class are available in the
7523 conversion-type-id. For example, consider:
7524
7525 struct S {
7526 typedef int I;
7527 operator I();
7528 };
7529
7530 S::operator I() { ... }
7531
7532 In order to see that `I' is a type-name in the definition, we
7533 must be in the scope of `S'. */
7534 if (saved_scope)
7535 pushed_scope = push_scope (saved_scope);
7536 /* Parse the conversion-type-id. */
7537 type = cp_parser_conversion_type_id (parser);
7538 /* Leave the scope of the class, if any. */
7539 if (pushed_scope)
7540 pop_scope (pushed_scope);
7541 /* Restore the saved scope. */
7542 parser->scope = saved_scope;
7543 parser->qualifying_scope = saved_qualifying_scope;
7544 parser->object_scope = saved_object_scope;
7545 /* If the TYPE is invalid, indicate failure. */
7546 if (type == error_mark_node)
7547 return error_mark_node;
7548 return mangle_conv_op_name_for_type (type);
7549 }
7550
7551 /* Parse a conversion-type-id:
7552
7553 conversion-type-id:
7554 type-specifier-seq conversion-declarator [opt]
7555
7556 Returns the TYPE specified. */
7557
7558 static tree
7559 cp_parser_conversion_type_id (cp_parser* parser)
7560 {
7561 tree attributes;
7562 cp_decl_specifier_seq type_specifiers;
7563 cp_declarator *declarator;
7564 tree type_specified;
7565
7566 /* Parse the attributes. */
7567 attributes = cp_parser_attributes_opt (parser);
7568 /* Parse the type-specifiers. */
7569 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7570 &type_specifiers);
7571 /* If that didn't work, stop. */
7572 if (type_specifiers.type == error_mark_node)
7573 return error_mark_node;
7574 /* Parse the conversion-declarator. */
7575 declarator = cp_parser_conversion_declarator_opt (parser);
7576
7577 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7578 /*initialized=*/0, &attributes);
7579 if (attributes)
7580 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7581 return type_specified;
7582 }
7583
7584 /* Parse an (optional) conversion-declarator.
7585
7586 conversion-declarator:
7587 ptr-operator conversion-declarator [opt]
7588
7589 */
7590
7591 static cp_declarator *
7592 cp_parser_conversion_declarator_opt (cp_parser* parser)
7593 {
7594 enum tree_code code;
7595 tree class_type;
7596 cp_cv_quals cv_quals;
7597
7598 /* We don't know if there's a ptr-operator next, or not. */
7599 cp_parser_parse_tentatively (parser);
7600 /* Try the ptr-operator. */
7601 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7602 /* If it worked, look for more conversion-declarators. */
7603 if (cp_parser_parse_definitely (parser))
7604 {
7605 cp_declarator *declarator;
7606
7607 /* Parse another optional declarator. */
7608 declarator = cp_parser_conversion_declarator_opt (parser);
7609
7610 /* Create the representation of the declarator. */
7611 if (class_type)
7612 declarator = make_ptrmem_declarator (cv_quals, class_type,
7613 declarator);
7614 else if (code == INDIRECT_REF)
7615 declarator = make_pointer_declarator (cv_quals, declarator);
7616 else
7617 declarator = make_reference_declarator (cv_quals, declarator);
7618
7619 return declarator;
7620 }
7621
7622 return NULL;
7623 }
7624
7625 /* Parse an (optional) ctor-initializer.
7626
7627 ctor-initializer:
7628 : mem-initializer-list
7629
7630 Returns TRUE iff the ctor-initializer was actually present. */
7631
7632 static bool
7633 cp_parser_ctor_initializer_opt (cp_parser* parser)
7634 {
7635 /* If the next token is not a `:', then there is no
7636 ctor-initializer. */
7637 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7638 {
7639 /* Do default initialization of any bases and members. */
7640 if (DECL_CONSTRUCTOR_P (current_function_decl))
7641 finish_mem_initializers (NULL_TREE);
7642
7643 return false;
7644 }
7645
7646 /* Consume the `:' token. */
7647 cp_lexer_consume_token (parser->lexer);
7648 /* And the mem-initializer-list. */
7649 cp_parser_mem_initializer_list (parser);
7650
7651 return true;
7652 }
7653
7654 /* Parse a mem-initializer-list.
7655
7656 mem-initializer-list:
7657 mem-initializer
7658 mem-initializer , mem-initializer-list */
7659
7660 static void
7661 cp_parser_mem_initializer_list (cp_parser* parser)
7662 {
7663 tree mem_initializer_list = NULL_TREE;
7664
7665 /* Let the semantic analysis code know that we are starting the
7666 mem-initializer-list. */
7667 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7668 error ("only constructors take base initializers");
7669
7670 /* Loop through the list. */
7671 while (true)
7672 {
7673 tree mem_initializer;
7674
7675 /* Parse the mem-initializer. */
7676 mem_initializer = cp_parser_mem_initializer (parser);
7677 /* Add it to the list, unless it was erroneous. */
7678 if (mem_initializer)
7679 {
7680 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7681 mem_initializer_list = mem_initializer;
7682 }
7683 /* If the next token is not a `,', we're done. */
7684 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7685 break;
7686 /* Consume the `,' token. */
7687 cp_lexer_consume_token (parser->lexer);
7688 }
7689
7690 /* Perform semantic analysis. */
7691 if (DECL_CONSTRUCTOR_P (current_function_decl))
7692 finish_mem_initializers (mem_initializer_list);
7693 }
7694
7695 /* Parse a mem-initializer.
7696
7697 mem-initializer:
7698 mem-initializer-id ( expression-list [opt] )
7699
7700 GNU extension:
7701
7702 mem-initializer:
7703 ( expression-list [opt] )
7704
7705 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7706 class) or FIELD_DECL (for a non-static data member) to initialize;
7707 the TREE_VALUE is the expression-list. */
7708
7709 static tree
7710 cp_parser_mem_initializer (cp_parser* parser)
7711 {
7712 tree mem_initializer_id;
7713 tree expression_list;
7714 tree member;
7715
7716 /* Find out what is being initialized. */
7717 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7718 {
7719 pedwarn ("anachronistic old-style base class initializer");
7720 mem_initializer_id = NULL_TREE;
7721 }
7722 else
7723 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7724 member = expand_member_init (mem_initializer_id);
7725 if (member && !DECL_P (member))
7726 in_base_initializer = 1;
7727
7728 expression_list
7729 = cp_parser_parenthesized_expression_list (parser, false,
7730 /*cast_p=*/false,
7731 /*non_constant_p=*/NULL);
7732 if (!expression_list)
7733 expression_list = void_type_node;
7734
7735 in_base_initializer = 0;
7736
7737 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7738 }
7739
7740 /* Parse a mem-initializer-id.
7741
7742 mem-initializer-id:
7743 :: [opt] nested-name-specifier [opt] class-name
7744 identifier
7745
7746 Returns a TYPE indicating the class to be initializer for the first
7747 production. Returns an IDENTIFIER_NODE indicating the data member
7748 to be initialized for the second production. */
7749
7750 static tree
7751 cp_parser_mem_initializer_id (cp_parser* parser)
7752 {
7753 bool global_scope_p;
7754 bool nested_name_specifier_p;
7755 bool template_p = false;
7756 tree id;
7757
7758 /* `typename' is not allowed in this context ([temp.res]). */
7759 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7760 {
7761 error ("keyword %<typename%> not allowed in this context (a qualified "
7762 "member initializer is implicitly a type)");
7763 cp_lexer_consume_token (parser->lexer);
7764 }
7765 /* Look for the optional `::' operator. */
7766 global_scope_p
7767 = (cp_parser_global_scope_opt (parser,
7768 /*current_scope_valid_p=*/false)
7769 != NULL_TREE);
7770 /* Look for the optional nested-name-specifier. The simplest way to
7771 implement:
7772
7773 [temp.res]
7774
7775 The keyword `typename' is not permitted in a base-specifier or
7776 mem-initializer; in these contexts a qualified name that
7777 depends on a template-parameter is implicitly assumed to be a
7778 type name.
7779
7780 is to assume that we have seen the `typename' keyword at this
7781 point. */
7782 nested_name_specifier_p
7783 = (cp_parser_nested_name_specifier_opt (parser,
7784 /*typename_keyword_p=*/true,
7785 /*check_dependency_p=*/true,
7786 /*type_p=*/true,
7787 /*is_declaration=*/true)
7788 != NULL_TREE);
7789 if (nested_name_specifier_p)
7790 template_p = cp_parser_optional_template_keyword (parser);
7791 /* If there is a `::' operator or a nested-name-specifier, then we
7792 are definitely looking for a class-name. */
7793 if (global_scope_p || nested_name_specifier_p)
7794 return cp_parser_class_name (parser,
7795 /*typename_keyword_p=*/true,
7796 /*template_keyword_p=*/template_p,
7797 none_type,
7798 /*check_dependency_p=*/true,
7799 /*class_head_p=*/false,
7800 /*is_declaration=*/true);
7801 /* Otherwise, we could also be looking for an ordinary identifier. */
7802 cp_parser_parse_tentatively (parser);
7803 /* Try a class-name. */
7804 id = cp_parser_class_name (parser,
7805 /*typename_keyword_p=*/true,
7806 /*template_keyword_p=*/false,
7807 none_type,
7808 /*check_dependency_p=*/true,
7809 /*class_head_p=*/false,
7810 /*is_declaration=*/true);
7811 /* If we found one, we're done. */
7812 if (cp_parser_parse_definitely (parser))
7813 return id;
7814 /* Otherwise, look for an ordinary identifier. */
7815 return cp_parser_identifier (parser);
7816 }
7817
7818 /* Overloading [gram.over] */
7819
7820 /* Parse an operator-function-id.
7821
7822 operator-function-id:
7823 operator operator
7824
7825 Returns an IDENTIFIER_NODE for the operator which is a
7826 human-readable spelling of the identifier, e.g., `operator +'. */
7827
7828 static tree
7829 cp_parser_operator_function_id (cp_parser* parser)
7830 {
7831 /* Look for the `operator' keyword. */
7832 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7833 return error_mark_node;
7834 /* And then the name of the operator itself. */
7835 return cp_parser_operator (parser);
7836 }
7837
7838 /* Parse an operator.
7839
7840 operator:
7841 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7842 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7843 || ++ -- , ->* -> () []
7844
7845 GNU Extensions:
7846
7847 operator:
7848 <? >? <?= >?=
7849
7850 Returns an IDENTIFIER_NODE for the operator which is a
7851 human-readable spelling of the identifier, e.g., `operator +'. */
7852
7853 static tree
7854 cp_parser_operator (cp_parser* parser)
7855 {
7856 tree id = NULL_TREE;
7857 cp_token *token;
7858
7859 /* Peek at the next token. */
7860 token = cp_lexer_peek_token (parser->lexer);
7861 /* Figure out which operator we have. */
7862 switch (token->type)
7863 {
7864 case CPP_KEYWORD:
7865 {
7866 enum tree_code op;
7867
7868 /* The keyword should be either `new' or `delete'. */
7869 if (token->keyword == RID_NEW)
7870 op = NEW_EXPR;
7871 else if (token->keyword == RID_DELETE)
7872 op = DELETE_EXPR;
7873 else
7874 break;
7875
7876 /* Consume the `new' or `delete' token. */
7877 cp_lexer_consume_token (parser->lexer);
7878
7879 /* Peek at the next token. */
7880 token = cp_lexer_peek_token (parser->lexer);
7881 /* If it's a `[' token then this is the array variant of the
7882 operator. */
7883 if (token->type == CPP_OPEN_SQUARE)
7884 {
7885 /* Consume the `[' token. */
7886 cp_lexer_consume_token (parser->lexer);
7887 /* Look for the `]' token. */
7888 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7889 id = ansi_opname (op == NEW_EXPR
7890 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7891 }
7892 /* Otherwise, we have the non-array variant. */
7893 else
7894 id = ansi_opname (op);
7895
7896 return id;
7897 }
7898
7899 case CPP_PLUS:
7900 id = ansi_opname (PLUS_EXPR);
7901 break;
7902
7903 case CPP_MINUS:
7904 id = ansi_opname (MINUS_EXPR);
7905 break;
7906
7907 case CPP_MULT:
7908 id = ansi_opname (MULT_EXPR);
7909 break;
7910
7911 case CPP_DIV:
7912 id = ansi_opname (TRUNC_DIV_EXPR);
7913 break;
7914
7915 case CPP_MOD:
7916 id = ansi_opname (TRUNC_MOD_EXPR);
7917 break;
7918
7919 case CPP_XOR:
7920 id = ansi_opname (BIT_XOR_EXPR);
7921 break;
7922
7923 case CPP_AND:
7924 id = ansi_opname (BIT_AND_EXPR);
7925 break;
7926
7927 case CPP_OR:
7928 id = ansi_opname (BIT_IOR_EXPR);
7929 break;
7930
7931 case CPP_COMPL:
7932 id = ansi_opname (BIT_NOT_EXPR);
7933 break;
7934
7935 case CPP_NOT:
7936 id = ansi_opname (TRUTH_NOT_EXPR);
7937 break;
7938
7939 case CPP_EQ:
7940 id = ansi_assopname (NOP_EXPR);
7941 break;
7942
7943 case CPP_LESS:
7944 id = ansi_opname (LT_EXPR);
7945 break;
7946
7947 case CPP_GREATER:
7948 id = ansi_opname (GT_EXPR);
7949 break;
7950
7951 case CPP_PLUS_EQ:
7952 id = ansi_assopname (PLUS_EXPR);
7953 break;
7954
7955 case CPP_MINUS_EQ:
7956 id = ansi_assopname (MINUS_EXPR);
7957 break;
7958
7959 case CPP_MULT_EQ:
7960 id = ansi_assopname (MULT_EXPR);
7961 break;
7962
7963 case CPP_DIV_EQ:
7964 id = ansi_assopname (TRUNC_DIV_EXPR);
7965 break;
7966
7967 case CPP_MOD_EQ:
7968 id = ansi_assopname (TRUNC_MOD_EXPR);
7969 break;
7970
7971 case CPP_XOR_EQ:
7972 id = ansi_assopname (BIT_XOR_EXPR);
7973 break;
7974
7975 case CPP_AND_EQ:
7976 id = ansi_assopname (BIT_AND_EXPR);
7977 break;
7978
7979 case CPP_OR_EQ:
7980 id = ansi_assopname (BIT_IOR_EXPR);
7981 break;
7982
7983 case CPP_LSHIFT:
7984 id = ansi_opname (LSHIFT_EXPR);
7985 break;
7986
7987 case CPP_RSHIFT:
7988 id = ansi_opname (RSHIFT_EXPR);
7989 break;
7990
7991 case CPP_LSHIFT_EQ:
7992 id = ansi_assopname (LSHIFT_EXPR);
7993 break;
7994
7995 case CPP_RSHIFT_EQ:
7996 id = ansi_assopname (RSHIFT_EXPR);
7997 break;
7998
7999 case CPP_EQ_EQ:
8000 id = ansi_opname (EQ_EXPR);
8001 break;
8002
8003 case CPP_NOT_EQ:
8004 id = ansi_opname (NE_EXPR);
8005 break;
8006
8007 case CPP_LESS_EQ:
8008 id = ansi_opname (LE_EXPR);
8009 break;
8010
8011 case CPP_GREATER_EQ:
8012 id = ansi_opname (GE_EXPR);
8013 break;
8014
8015 case CPP_AND_AND:
8016 id = ansi_opname (TRUTH_ANDIF_EXPR);
8017 break;
8018
8019 case CPP_OR_OR:
8020 id = ansi_opname (TRUTH_ORIF_EXPR);
8021 break;
8022
8023 case CPP_PLUS_PLUS:
8024 id = ansi_opname (POSTINCREMENT_EXPR);
8025 break;
8026
8027 case CPP_MINUS_MINUS:
8028 id = ansi_opname (PREDECREMENT_EXPR);
8029 break;
8030
8031 case CPP_COMMA:
8032 id = ansi_opname (COMPOUND_EXPR);
8033 break;
8034
8035 case CPP_DEREF_STAR:
8036 id = ansi_opname (MEMBER_REF);
8037 break;
8038
8039 case CPP_DEREF:
8040 id = ansi_opname (COMPONENT_REF);
8041 break;
8042
8043 case CPP_OPEN_PAREN:
8044 /* Consume the `('. */
8045 cp_lexer_consume_token (parser->lexer);
8046 /* Look for the matching `)'. */
8047 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8048 return ansi_opname (CALL_EXPR);
8049
8050 case CPP_OPEN_SQUARE:
8051 /* Consume the `['. */
8052 cp_lexer_consume_token (parser->lexer);
8053 /* Look for the matching `]'. */
8054 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8055 return ansi_opname (ARRAY_REF);
8056
8057 /* Extensions. */
8058 case CPP_MIN:
8059 id = ansi_opname (MIN_EXPR);
8060 cp_parser_warn_min_max ();
8061 break;
8062
8063 case CPP_MAX:
8064 id = ansi_opname (MAX_EXPR);
8065 cp_parser_warn_min_max ();
8066 break;
8067
8068 case CPP_MIN_EQ:
8069 id = ansi_assopname (MIN_EXPR);
8070 cp_parser_warn_min_max ();
8071 break;
8072
8073 case CPP_MAX_EQ:
8074 id = ansi_assopname (MAX_EXPR);
8075 cp_parser_warn_min_max ();
8076 break;
8077
8078 default:
8079 /* Anything else is an error. */
8080 break;
8081 }
8082
8083 /* If we have selected an identifier, we need to consume the
8084 operator token. */
8085 if (id)
8086 cp_lexer_consume_token (parser->lexer);
8087 /* Otherwise, no valid operator name was present. */
8088 else
8089 {
8090 cp_parser_error (parser, "expected operator");
8091 id = error_mark_node;
8092 }
8093
8094 return id;
8095 }
8096
8097 /* Parse a template-declaration.
8098
8099 template-declaration:
8100 export [opt] template < template-parameter-list > declaration
8101
8102 If MEMBER_P is TRUE, this template-declaration occurs within a
8103 class-specifier.
8104
8105 The grammar rule given by the standard isn't correct. What
8106 is really meant is:
8107
8108 template-declaration:
8109 export [opt] template-parameter-list-seq
8110 decl-specifier-seq [opt] init-declarator [opt] ;
8111 export [opt] template-parameter-list-seq
8112 function-definition
8113
8114 template-parameter-list-seq:
8115 template-parameter-list-seq [opt]
8116 template < template-parameter-list > */
8117
8118 static void
8119 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8120 {
8121 /* Check for `export'. */
8122 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8123 {
8124 /* Consume the `export' token. */
8125 cp_lexer_consume_token (parser->lexer);
8126 /* Warn that we do not support `export'. */
8127 warning ("keyword %<export%> not implemented, and will be ignored");
8128 }
8129
8130 cp_parser_template_declaration_after_export (parser, member_p);
8131 }
8132
8133 /* Parse a template-parameter-list.
8134
8135 template-parameter-list:
8136 template-parameter
8137 template-parameter-list , template-parameter
8138
8139 Returns a TREE_LIST. Each node represents a template parameter.
8140 The nodes are connected via their TREE_CHAINs. */
8141
8142 static tree
8143 cp_parser_template_parameter_list (cp_parser* parser)
8144 {
8145 tree parameter_list = NULL_TREE;
8146
8147 while (true)
8148 {
8149 tree parameter;
8150 cp_token *token;
8151 bool is_non_type;
8152
8153 /* Parse the template-parameter. */
8154 parameter = cp_parser_template_parameter (parser, &is_non_type);
8155 /* Add it to the list. */
8156 if (parameter != error_mark_node)
8157 parameter_list = process_template_parm (parameter_list,
8158 parameter,
8159 is_non_type);
8160 /* Peek at the next token. */
8161 token = cp_lexer_peek_token (parser->lexer);
8162 /* If it's not a `,', we're done. */
8163 if (token->type != CPP_COMMA)
8164 break;
8165 /* Otherwise, consume the `,' token. */
8166 cp_lexer_consume_token (parser->lexer);
8167 }
8168
8169 return parameter_list;
8170 }
8171
8172 /* Parse a template-parameter.
8173
8174 template-parameter:
8175 type-parameter
8176 parameter-declaration
8177
8178 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8179 the parameter. The TREE_PURPOSE is the default value, if any.
8180 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8181 iff this parameter is a non-type parameter. */
8182
8183 static tree
8184 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8185 {
8186 cp_token *token;
8187 cp_parameter_declarator *parameter_declarator;
8188 tree parm;
8189
8190 /* Assume it is a type parameter or a template parameter. */
8191 *is_non_type = false;
8192 /* Peek at the next token. */
8193 token = cp_lexer_peek_token (parser->lexer);
8194 /* If it is `class' or `template', we have a type-parameter. */
8195 if (token->keyword == RID_TEMPLATE)
8196 return cp_parser_type_parameter (parser);
8197 /* If it is `class' or `typename' we do not know yet whether it is a
8198 type parameter or a non-type parameter. Consider:
8199
8200 template <typename T, typename T::X X> ...
8201
8202 or:
8203
8204 template <class C, class D*> ...
8205
8206 Here, the first parameter is a type parameter, and the second is
8207 a non-type parameter. We can tell by looking at the token after
8208 the identifier -- if it is a `,', `=', or `>' then we have a type
8209 parameter. */
8210 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8211 {
8212 /* Peek at the token after `class' or `typename'. */
8213 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8214 /* If it's an identifier, skip it. */
8215 if (token->type == CPP_NAME)
8216 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8217 /* Now, see if the token looks like the end of a template
8218 parameter. */
8219 if (token->type == CPP_COMMA
8220 || token->type == CPP_EQ
8221 || token->type == CPP_GREATER)
8222 return cp_parser_type_parameter (parser);
8223 }
8224
8225 /* Otherwise, it is a non-type parameter.
8226
8227 [temp.param]
8228
8229 When parsing a default template-argument for a non-type
8230 template-parameter, the first non-nested `>' is taken as the end
8231 of the template parameter-list rather than a greater-than
8232 operator. */
8233 *is_non_type = true;
8234 parameter_declarator
8235 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8236 /*parenthesized_p=*/NULL);
8237 parm = grokdeclarator (parameter_declarator->declarator,
8238 &parameter_declarator->decl_specifiers,
8239 PARM, /*initialized=*/0,
8240 /*attrlist=*/NULL);
8241 if (parm == error_mark_node)
8242 return error_mark_node;
8243 return build_tree_list (parameter_declarator->default_argument, parm);
8244 }
8245
8246 /* Parse a type-parameter.
8247
8248 type-parameter:
8249 class identifier [opt]
8250 class identifier [opt] = type-id
8251 typename identifier [opt]
8252 typename identifier [opt] = type-id
8253 template < template-parameter-list > class identifier [opt]
8254 template < template-parameter-list > class identifier [opt]
8255 = id-expression
8256
8257 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8258 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8259 the declaration of the parameter. */
8260
8261 static tree
8262 cp_parser_type_parameter (cp_parser* parser)
8263 {
8264 cp_token *token;
8265 tree parameter;
8266
8267 /* Look for a keyword to tell us what kind of parameter this is. */
8268 token = cp_parser_require (parser, CPP_KEYWORD,
8269 "`class', `typename', or `template'");
8270 if (!token)
8271 return error_mark_node;
8272
8273 switch (token->keyword)
8274 {
8275 case RID_CLASS:
8276 case RID_TYPENAME:
8277 {
8278 tree identifier;
8279 tree default_argument;
8280
8281 /* If the next token is an identifier, then it names the
8282 parameter. */
8283 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8284 identifier = cp_parser_identifier (parser);
8285 else
8286 identifier = NULL_TREE;
8287
8288 /* Create the parameter. */
8289 parameter = finish_template_type_parm (class_type_node, identifier);
8290
8291 /* If the next token is an `=', we have a default argument. */
8292 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8293 {
8294 /* Consume the `=' token. */
8295 cp_lexer_consume_token (parser->lexer);
8296 /* Parse the default-argument. */
8297 default_argument = cp_parser_type_id (parser);
8298 }
8299 else
8300 default_argument = NULL_TREE;
8301
8302 /* Create the combined representation of the parameter and the
8303 default argument. */
8304 parameter = build_tree_list (default_argument, parameter);
8305 }
8306 break;
8307
8308 case RID_TEMPLATE:
8309 {
8310 tree parameter_list;
8311 tree identifier;
8312 tree default_argument;
8313
8314 /* Look for the `<'. */
8315 cp_parser_require (parser, CPP_LESS, "`<'");
8316 /* Parse the template-parameter-list. */
8317 begin_template_parm_list ();
8318 parameter_list
8319 = cp_parser_template_parameter_list (parser);
8320 parameter_list = end_template_parm_list (parameter_list);
8321 /* Look for the `>'. */
8322 cp_parser_require (parser, CPP_GREATER, "`>'");
8323 /* Look for the `class' keyword. */
8324 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8325 /* If the next token is an `=', then there is a
8326 default-argument. If the next token is a `>', we are at
8327 the end of the parameter-list. If the next token is a `,',
8328 then we are at the end of this parameter. */
8329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8330 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8331 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8332 {
8333 identifier = cp_parser_identifier (parser);
8334 /* Treat invalid names as if the parameter were nameless. */
8335 if (identifier == error_mark_node)
8336 identifier = NULL_TREE;
8337 }
8338 else
8339 identifier = NULL_TREE;
8340
8341 /* Create the template parameter. */
8342 parameter = finish_template_template_parm (class_type_node,
8343 identifier);
8344
8345 /* If the next token is an `=', then there is a
8346 default-argument. */
8347 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8348 {
8349 bool is_template;
8350
8351 /* Consume the `='. */
8352 cp_lexer_consume_token (parser->lexer);
8353 /* Parse the id-expression. */
8354 default_argument
8355 = cp_parser_id_expression (parser,
8356 /*template_keyword_p=*/false,
8357 /*check_dependency_p=*/true,
8358 /*template_p=*/&is_template,
8359 /*declarator_p=*/false);
8360 if (TREE_CODE (default_argument) == TYPE_DECL)
8361 /* If the id-expression was a template-id that refers to
8362 a template-class, we already have the declaration here,
8363 so no further lookup is needed. */
8364 ;
8365 else
8366 /* Look up the name. */
8367 default_argument
8368 = cp_parser_lookup_name (parser, default_argument,
8369 none_type,
8370 /*is_template=*/is_template,
8371 /*is_namespace=*/false,
8372 /*check_dependency=*/true,
8373 /*ambiguous_p=*/NULL);
8374 /* See if the default argument is valid. */
8375 default_argument
8376 = check_template_template_default_arg (default_argument);
8377 }
8378 else
8379 default_argument = NULL_TREE;
8380
8381 /* Create the combined representation of the parameter and the
8382 default argument. */
8383 parameter = build_tree_list (default_argument, parameter);
8384 }
8385 break;
8386
8387 default:
8388 gcc_unreachable ();
8389 break;
8390 }
8391
8392 return parameter;
8393 }
8394
8395 /* Parse a template-id.
8396
8397 template-id:
8398 template-name < template-argument-list [opt] >
8399
8400 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8401 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8402 returned. Otherwise, if the template-name names a function, or set
8403 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8404 names a class, returns a TYPE_DECL for the specialization.
8405
8406 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8407 uninstantiated templates. */
8408
8409 static tree
8410 cp_parser_template_id (cp_parser *parser,
8411 bool template_keyword_p,
8412 bool check_dependency_p,
8413 bool is_declaration)
8414 {
8415 tree template;
8416 tree arguments;
8417 tree template_id;
8418 cp_token_position start_of_id = 0;
8419 tree access_check = NULL_TREE;
8420 cp_token *next_token, *next_token_2;
8421 bool is_identifier;
8422
8423 /* If the next token corresponds to a template-id, there is no need
8424 to reparse it. */
8425 next_token = cp_lexer_peek_token (parser->lexer);
8426 if (next_token->type == CPP_TEMPLATE_ID)
8427 {
8428 tree value;
8429 tree check;
8430
8431 /* Get the stored value. */
8432 value = cp_lexer_consume_token (parser->lexer)->value;
8433 /* Perform any access checks that were deferred. */
8434 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8435 perform_or_defer_access_check (TREE_PURPOSE (check),
8436 TREE_VALUE (check));
8437 /* Return the stored value. */
8438 return TREE_VALUE (value);
8439 }
8440
8441 /* Avoid performing name lookup if there is no possibility of
8442 finding a template-id. */
8443 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8444 || (next_token->type == CPP_NAME
8445 && !cp_parser_nth_token_starts_template_argument_list_p
8446 (parser, 2)))
8447 {
8448 cp_parser_error (parser, "expected template-id");
8449 return error_mark_node;
8450 }
8451
8452 /* Remember where the template-id starts. */
8453 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8454 start_of_id = cp_lexer_token_position (parser->lexer, false);
8455
8456 push_deferring_access_checks (dk_deferred);
8457
8458 /* Parse the template-name. */
8459 is_identifier = false;
8460 template = cp_parser_template_name (parser, template_keyword_p,
8461 check_dependency_p,
8462 is_declaration,
8463 &is_identifier);
8464 if (template == error_mark_node || is_identifier)
8465 {
8466 pop_deferring_access_checks ();
8467 return template;
8468 }
8469
8470 /* If we find the sequence `[:' after a template-name, it's probably
8471 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8472 parse correctly the argument list. */
8473 next_token = cp_lexer_peek_token (parser->lexer);
8474 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8475 if (next_token->type == CPP_OPEN_SQUARE
8476 && next_token->flags & DIGRAPH
8477 && next_token_2->type == CPP_COLON
8478 && !(next_token_2->flags & PREV_WHITE))
8479 {
8480 cp_parser_parse_tentatively (parser);
8481 /* Change `:' into `::'. */
8482 next_token_2->type = CPP_SCOPE;
8483 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8484 CPP_LESS. */
8485 cp_lexer_consume_token (parser->lexer);
8486 /* Parse the arguments. */
8487 arguments = cp_parser_enclosed_template_argument_list (parser);
8488 if (!cp_parser_parse_definitely (parser))
8489 {
8490 /* If we couldn't parse an argument list, then we revert our changes
8491 and return simply an error. Maybe this is not a template-id
8492 after all. */
8493 next_token_2->type = CPP_COLON;
8494 cp_parser_error (parser, "expected %<<%>");
8495 pop_deferring_access_checks ();
8496 return error_mark_node;
8497 }
8498 /* Otherwise, emit an error about the invalid digraph, but continue
8499 parsing because we got our argument list. */
8500 pedwarn ("%<<::%> cannot begin a template-argument list");
8501 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8502 "between %<<%> and %<::%>");
8503 if (!flag_permissive)
8504 {
8505 static bool hint;
8506 if (!hint)
8507 {
8508 inform ("(if you use -fpermissive G++ will accept your code)");
8509 hint = true;
8510 }
8511 }
8512 }
8513 else
8514 {
8515 /* Look for the `<' that starts the template-argument-list. */
8516 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8517 {
8518 pop_deferring_access_checks ();
8519 return error_mark_node;
8520 }
8521 /* Parse the arguments. */
8522 arguments = cp_parser_enclosed_template_argument_list (parser);
8523 }
8524
8525 /* Build a representation of the specialization. */
8526 if (TREE_CODE (template) == IDENTIFIER_NODE)
8527 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8528 else if (DECL_CLASS_TEMPLATE_P (template)
8529 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8530 template_id
8531 = finish_template_type (template, arguments,
8532 cp_lexer_next_token_is (parser->lexer,
8533 CPP_SCOPE));
8534 else
8535 {
8536 /* If it's not a class-template or a template-template, it should be
8537 a function-template. */
8538 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8539 || TREE_CODE (template) == OVERLOAD
8540 || BASELINK_P (template)));
8541
8542 template_id = lookup_template_function (template, arguments);
8543 }
8544
8545 /* Retrieve any deferred checks. Do not pop this access checks yet
8546 so the memory will not be reclaimed during token replacing below. */
8547 access_check = get_deferred_access_checks ();
8548
8549 /* If parsing tentatively, replace the sequence of tokens that makes
8550 up the template-id with a CPP_TEMPLATE_ID token. That way,
8551 should we re-parse the token stream, we will not have to repeat
8552 the effort required to do the parse, nor will we issue duplicate
8553 error messages about problems during instantiation of the
8554 template. */
8555 if (start_of_id)
8556 {
8557 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8558
8559 /* Reset the contents of the START_OF_ID token. */
8560 token->type = CPP_TEMPLATE_ID;
8561 token->value = build_tree_list (access_check, template_id);
8562 token->keyword = RID_MAX;
8563
8564 /* Purge all subsequent tokens. */
8565 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8566
8567 /* ??? Can we actually assume that, if template_id ==
8568 error_mark_node, we will have issued a diagnostic to the
8569 user, as opposed to simply marking the tentative parse as
8570 failed? */
8571 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8572 error ("parse error in template argument list");
8573 }
8574
8575 pop_deferring_access_checks ();
8576 return template_id;
8577 }
8578
8579 /* Parse a template-name.
8580
8581 template-name:
8582 identifier
8583
8584 The standard should actually say:
8585
8586 template-name:
8587 identifier
8588 operator-function-id
8589
8590 A defect report has been filed about this issue.
8591
8592 A conversion-function-id cannot be a template name because they cannot
8593 be part of a template-id. In fact, looking at this code:
8594
8595 a.operator K<int>()
8596
8597 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8598 It is impossible to call a templated conversion-function-id with an
8599 explicit argument list, since the only allowed template parameter is
8600 the type to which it is converting.
8601
8602 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8603 `template' keyword, in a construction like:
8604
8605 T::template f<3>()
8606
8607 In that case `f' is taken to be a template-name, even though there
8608 is no way of knowing for sure.
8609
8610 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8611 name refers to a set of overloaded functions, at least one of which
8612 is a template, or an IDENTIFIER_NODE with the name of the template,
8613 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8614 names are looked up inside uninstantiated templates. */
8615
8616 static tree
8617 cp_parser_template_name (cp_parser* parser,
8618 bool template_keyword_p,
8619 bool check_dependency_p,
8620 bool is_declaration,
8621 bool *is_identifier)
8622 {
8623 tree identifier;
8624 tree decl;
8625 tree fns;
8626
8627 /* If the next token is `operator', then we have either an
8628 operator-function-id or a conversion-function-id. */
8629 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8630 {
8631 /* We don't know whether we're looking at an
8632 operator-function-id or a conversion-function-id. */
8633 cp_parser_parse_tentatively (parser);
8634 /* Try an operator-function-id. */
8635 identifier = cp_parser_operator_function_id (parser);
8636 /* If that didn't work, try a conversion-function-id. */
8637 if (!cp_parser_parse_definitely (parser))
8638 {
8639 cp_parser_error (parser, "expected template-name");
8640 return error_mark_node;
8641 }
8642 }
8643 /* Look for the identifier. */
8644 else
8645 identifier = cp_parser_identifier (parser);
8646
8647 /* If we didn't find an identifier, we don't have a template-id. */
8648 if (identifier == error_mark_node)
8649 return error_mark_node;
8650
8651 /* If the name immediately followed the `template' keyword, then it
8652 is a template-name. However, if the next token is not `<', then
8653 we do not treat it as a template-name, since it is not being used
8654 as part of a template-id. This enables us to handle constructs
8655 like:
8656
8657 template <typename T> struct S { S(); };
8658 template <typename T> S<T>::S();
8659
8660 correctly. We would treat `S' as a template -- if it were `S<T>'
8661 -- but we do not if there is no `<'. */
8662
8663 if (processing_template_decl
8664 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8665 {
8666 /* In a declaration, in a dependent context, we pretend that the
8667 "template" keyword was present in order to improve error
8668 recovery. For example, given:
8669
8670 template <typename T> void f(T::X<int>);
8671
8672 we want to treat "X<int>" as a template-id. */
8673 if (is_declaration
8674 && !template_keyword_p
8675 && parser->scope && TYPE_P (parser->scope)
8676 && check_dependency_p
8677 && dependent_type_p (parser->scope)
8678 /* Do not do this for dtors (or ctors), since they never
8679 need the template keyword before their name. */
8680 && !constructor_name_p (identifier, parser->scope))
8681 {
8682 cp_token_position start = 0;
8683
8684 /* Explain what went wrong. */
8685 error ("non-template %qD used as template", identifier);
8686 inform ("use %<%T::template %D%> to indicate that it is a template",
8687 parser->scope, identifier);
8688 /* If parsing tentatively, find the location of the "<" token. */
8689 if (cp_parser_simulate_error (parser))
8690 start = cp_lexer_token_position (parser->lexer, true);
8691 /* Parse the template arguments so that we can issue error
8692 messages about them. */
8693 cp_lexer_consume_token (parser->lexer);
8694 cp_parser_enclosed_template_argument_list (parser);
8695 /* Skip tokens until we find a good place from which to
8696 continue parsing. */
8697 cp_parser_skip_to_closing_parenthesis (parser,
8698 /*recovering=*/true,
8699 /*or_comma=*/true,
8700 /*consume_paren=*/false);
8701 /* If parsing tentatively, permanently remove the
8702 template argument list. That will prevent duplicate
8703 error messages from being issued about the missing
8704 "template" keyword. */
8705 if (start)
8706 cp_lexer_purge_tokens_after (parser->lexer, start);
8707 if (is_identifier)
8708 *is_identifier = true;
8709 return identifier;
8710 }
8711
8712 /* If the "template" keyword is present, then there is generally
8713 no point in doing name-lookup, so we just return IDENTIFIER.
8714 But, if the qualifying scope is non-dependent then we can
8715 (and must) do name-lookup normally. */
8716 if (template_keyword_p
8717 && (!parser->scope
8718 || (TYPE_P (parser->scope)
8719 && dependent_type_p (parser->scope))))
8720 return identifier;
8721 }
8722
8723 /* Look up the name. */
8724 decl = cp_parser_lookup_name (parser, identifier,
8725 none_type,
8726 /*is_template=*/false,
8727 /*is_namespace=*/false,
8728 check_dependency_p,
8729 /*ambiguous_p=*/NULL);
8730 decl = maybe_get_template_decl_from_type_decl (decl);
8731
8732 /* If DECL is a template, then the name was a template-name. */
8733 if (TREE_CODE (decl) == TEMPLATE_DECL)
8734 ;
8735 else
8736 {
8737 tree fn = NULL_TREE;
8738
8739 /* The standard does not explicitly indicate whether a name that
8740 names a set of overloaded declarations, some of which are
8741 templates, is a template-name. However, such a name should
8742 be a template-name; otherwise, there is no way to form a
8743 template-id for the overloaded templates. */
8744 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8745 if (TREE_CODE (fns) == OVERLOAD)
8746 for (fn = fns; fn; fn = OVL_NEXT (fn))
8747 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8748 break;
8749
8750 if (!fn)
8751 {
8752 /* The name does not name a template. */
8753 cp_parser_error (parser, "expected template-name");
8754 return error_mark_node;
8755 }
8756 }
8757
8758 /* If DECL is dependent, and refers to a function, then just return
8759 its name; we will look it up again during template instantiation. */
8760 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8761 {
8762 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8763 if (TYPE_P (scope) && dependent_type_p (scope))
8764 return identifier;
8765 }
8766
8767 return decl;
8768 }
8769
8770 /* Parse a template-argument-list.
8771
8772 template-argument-list:
8773 template-argument
8774 template-argument-list , template-argument
8775
8776 Returns a TREE_VEC containing the arguments. */
8777
8778 static tree
8779 cp_parser_template_argument_list (cp_parser* parser)
8780 {
8781 tree fixed_args[10];
8782 unsigned n_args = 0;
8783 unsigned alloced = 10;
8784 tree *arg_ary = fixed_args;
8785 tree vec;
8786 bool saved_in_template_argument_list_p;
8787
8788 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8789 parser->in_template_argument_list_p = true;
8790 do
8791 {
8792 tree argument;
8793
8794 if (n_args)
8795 /* Consume the comma. */
8796 cp_lexer_consume_token (parser->lexer);
8797
8798 /* Parse the template-argument. */
8799 argument = cp_parser_template_argument (parser);
8800 if (n_args == alloced)
8801 {
8802 alloced *= 2;
8803
8804 if (arg_ary == fixed_args)
8805 {
8806 arg_ary = xmalloc (sizeof (tree) * alloced);
8807 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8808 }
8809 else
8810 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8811 }
8812 arg_ary[n_args++] = argument;
8813 }
8814 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8815
8816 vec = make_tree_vec (n_args);
8817
8818 while (n_args--)
8819 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8820
8821 if (arg_ary != fixed_args)
8822 free (arg_ary);
8823 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8824 return vec;
8825 }
8826
8827 /* Parse a template-argument.
8828
8829 template-argument:
8830 assignment-expression
8831 type-id
8832 id-expression
8833
8834 The representation is that of an assignment-expression, type-id, or
8835 id-expression -- except that the qualified id-expression is
8836 evaluated, so that the value returned is either a DECL or an
8837 OVERLOAD.
8838
8839 Although the standard says "assignment-expression", it forbids
8840 throw-expressions or assignments in the template argument.
8841 Therefore, we use "conditional-expression" instead. */
8842
8843 static tree
8844 cp_parser_template_argument (cp_parser* parser)
8845 {
8846 tree argument;
8847 bool template_p;
8848 bool address_p;
8849 bool maybe_type_id = false;
8850 cp_token *token;
8851 cp_id_kind idk;
8852 tree qualifying_class;
8853
8854 /* There's really no way to know what we're looking at, so we just
8855 try each alternative in order.
8856
8857 [temp.arg]
8858
8859 In a template-argument, an ambiguity between a type-id and an
8860 expression is resolved to a type-id, regardless of the form of
8861 the corresponding template-parameter.
8862
8863 Therefore, we try a type-id first. */
8864 cp_parser_parse_tentatively (parser);
8865 argument = cp_parser_type_id (parser);
8866 /* If there was no error parsing the type-id but the next token is a '>>',
8867 we probably found a typo for '> >'. But there are type-id which are
8868 also valid expressions. For instance:
8869
8870 struct X { int operator >> (int); };
8871 template <int V> struct Foo {};
8872 Foo<X () >> 5> r;
8873
8874 Here 'X()' is a valid type-id of a function type, but the user just
8875 wanted to write the expression "X() >> 5". Thus, we remember that we
8876 found a valid type-id, but we still try to parse the argument as an
8877 expression to see what happens. */
8878 if (!cp_parser_error_occurred (parser)
8879 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8880 {
8881 maybe_type_id = true;
8882 cp_parser_abort_tentative_parse (parser);
8883 }
8884 else
8885 {
8886 /* If the next token isn't a `,' or a `>', then this argument wasn't
8887 really finished. This means that the argument is not a valid
8888 type-id. */
8889 if (!cp_parser_next_token_ends_template_argument_p (parser))
8890 cp_parser_error (parser, "expected template-argument");
8891 /* If that worked, we're done. */
8892 if (cp_parser_parse_definitely (parser))
8893 return argument;
8894 }
8895 /* We're still not sure what the argument will be. */
8896 cp_parser_parse_tentatively (parser);
8897 /* Try a template. */
8898 argument = cp_parser_id_expression (parser,
8899 /*template_keyword_p=*/false,
8900 /*check_dependency_p=*/true,
8901 &template_p,
8902 /*declarator_p=*/false);
8903 /* If the next token isn't a `,' or a `>', then this argument wasn't
8904 really finished. */
8905 if (!cp_parser_next_token_ends_template_argument_p (parser))
8906 cp_parser_error (parser, "expected template-argument");
8907 if (!cp_parser_error_occurred (parser))
8908 {
8909 /* Figure out what is being referred to. If the id-expression
8910 was for a class template specialization, then we will have a
8911 TYPE_DECL at this point. There is no need to do name lookup
8912 at this point in that case. */
8913 if (TREE_CODE (argument) != TYPE_DECL)
8914 argument = cp_parser_lookup_name (parser, argument,
8915 none_type,
8916 /*is_template=*/template_p,
8917 /*is_namespace=*/false,
8918 /*check_dependency=*/true,
8919 /*ambiguous_p=*/NULL);
8920 if (TREE_CODE (argument) != TEMPLATE_DECL
8921 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8922 cp_parser_error (parser, "expected template-name");
8923 }
8924 if (cp_parser_parse_definitely (parser))
8925 return argument;
8926 /* It must be a non-type argument. There permitted cases are given
8927 in [temp.arg.nontype]:
8928
8929 -- an integral constant-expression of integral or enumeration
8930 type; or
8931
8932 -- the name of a non-type template-parameter; or
8933
8934 -- the name of an object or function with external linkage...
8935
8936 -- the address of an object or function with external linkage...
8937
8938 -- a pointer to member... */
8939 /* Look for a non-type template parameter. */
8940 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8941 {
8942 cp_parser_parse_tentatively (parser);
8943 argument = cp_parser_primary_expression (parser,
8944 /*cast_p=*/false,
8945 &idk,
8946 &qualifying_class);
8947 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8948 || !cp_parser_next_token_ends_template_argument_p (parser))
8949 cp_parser_simulate_error (parser);
8950 if (cp_parser_parse_definitely (parser))
8951 return argument;
8952 }
8953
8954 /* If the next token is "&", the argument must be the address of an
8955 object or function with external linkage. */
8956 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8957 if (address_p)
8958 cp_lexer_consume_token (parser->lexer);
8959 /* See if we might have an id-expression. */
8960 token = cp_lexer_peek_token (parser->lexer);
8961 if (token->type == CPP_NAME
8962 || token->keyword == RID_OPERATOR
8963 || token->type == CPP_SCOPE
8964 || token->type == CPP_TEMPLATE_ID
8965 || token->type == CPP_NESTED_NAME_SPECIFIER)
8966 {
8967 cp_parser_parse_tentatively (parser);
8968 argument = cp_parser_primary_expression (parser,
8969 /*cast_p=*/false,
8970 &idk,
8971 &qualifying_class);
8972 if (cp_parser_error_occurred (parser)
8973 || !cp_parser_next_token_ends_template_argument_p (parser))
8974 cp_parser_abort_tentative_parse (parser);
8975 else
8976 {
8977 if (TREE_CODE (argument) == INDIRECT_REF)
8978 {
8979 gcc_assert (REFERENCE_REF_P (argument));
8980 argument = TREE_OPERAND (argument, 0);
8981 }
8982
8983 if (qualifying_class)
8984 argument = finish_qualified_id_expr (qualifying_class,
8985 argument,
8986 /*done=*/true,
8987 address_p);
8988 if (TREE_CODE (argument) == VAR_DECL)
8989 {
8990 /* A variable without external linkage might still be a
8991 valid constant-expression, so no error is issued here
8992 if the external-linkage check fails. */
8993 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8994 cp_parser_simulate_error (parser);
8995 }
8996 else if (is_overloaded_fn (argument))
8997 /* All overloaded functions are allowed; if the external
8998 linkage test does not pass, an error will be issued
8999 later. */
9000 ;
9001 else if (address_p
9002 && (TREE_CODE (argument) == OFFSET_REF
9003 || TREE_CODE (argument) == SCOPE_REF))
9004 /* A pointer-to-member. */
9005 ;
9006 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9007 ;
9008 else
9009 cp_parser_simulate_error (parser);
9010
9011 if (cp_parser_parse_definitely (parser))
9012 {
9013 if (address_p)
9014 argument = build_x_unary_op (ADDR_EXPR, argument);
9015 return argument;
9016 }
9017 }
9018 }
9019 /* If the argument started with "&", there are no other valid
9020 alternatives at this point. */
9021 if (address_p)
9022 {
9023 cp_parser_error (parser, "invalid non-type template argument");
9024 return error_mark_node;
9025 }
9026
9027 /* If the argument wasn't successfully parsed as a type-id followed
9028 by '>>', the argument can only be a constant expression now.
9029 Otherwise, we try parsing the constant-expression tentatively,
9030 because the argument could really be a type-id. */
9031 if (maybe_type_id)
9032 cp_parser_parse_tentatively (parser);
9033 argument = cp_parser_constant_expression (parser,
9034 /*allow_non_constant_p=*/false,
9035 /*non_constant_p=*/NULL);
9036 argument = fold_non_dependent_expr (argument);
9037 if (!maybe_type_id)
9038 return argument;
9039 if (!cp_parser_next_token_ends_template_argument_p (parser))
9040 cp_parser_error (parser, "expected template-argument");
9041 if (cp_parser_parse_definitely (parser))
9042 return argument;
9043 /* We did our best to parse the argument as a non type-id, but that
9044 was the only alternative that matched (albeit with a '>' after
9045 it). We can assume it's just a typo from the user, and a
9046 diagnostic will then be issued. */
9047 return cp_parser_type_id (parser);
9048 }
9049
9050 /* Parse an explicit-instantiation.
9051
9052 explicit-instantiation:
9053 template declaration
9054
9055 Although the standard says `declaration', what it really means is:
9056
9057 explicit-instantiation:
9058 template decl-specifier-seq [opt] declarator [opt] ;
9059
9060 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9061 supposed to be allowed. A defect report has been filed about this
9062 issue.
9063
9064 GNU Extension:
9065
9066 explicit-instantiation:
9067 storage-class-specifier template
9068 decl-specifier-seq [opt] declarator [opt] ;
9069 function-specifier template
9070 decl-specifier-seq [opt] declarator [opt] ; */
9071
9072 static void
9073 cp_parser_explicit_instantiation (cp_parser* parser)
9074 {
9075 int declares_class_or_enum;
9076 cp_decl_specifier_seq decl_specifiers;
9077 tree extension_specifier = NULL_TREE;
9078
9079 /* Look for an (optional) storage-class-specifier or
9080 function-specifier. */
9081 if (cp_parser_allow_gnu_extensions_p (parser))
9082 {
9083 extension_specifier
9084 = cp_parser_storage_class_specifier_opt (parser);
9085 if (!extension_specifier)
9086 extension_specifier
9087 = cp_parser_function_specifier_opt (parser,
9088 /*decl_specs=*/NULL);
9089 }
9090
9091 /* Look for the `template' keyword. */
9092 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9093 /* Let the front end know that we are processing an explicit
9094 instantiation. */
9095 begin_explicit_instantiation ();
9096 /* [temp.explicit] says that we are supposed to ignore access
9097 control while processing explicit instantiation directives. */
9098 push_deferring_access_checks (dk_no_check);
9099 /* Parse a decl-specifier-seq. */
9100 cp_parser_decl_specifier_seq (parser,
9101 CP_PARSER_FLAGS_OPTIONAL,
9102 &decl_specifiers,
9103 &declares_class_or_enum);
9104 /* If there was exactly one decl-specifier, and it declared a class,
9105 and there's no declarator, then we have an explicit type
9106 instantiation. */
9107 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9108 {
9109 tree type;
9110
9111 type = check_tag_decl (&decl_specifiers);
9112 /* Turn access control back on for names used during
9113 template instantiation. */
9114 pop_deferring_access_checks ();
9115 if (type)
9116 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9117 }
9118 else
9119 {
9120 cp_declarator *declarator;
9121 tree decl;
9122
9123 /* Parse the declarator. */
9124 declarator
9125 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9126 /*ctor_dtor_or_conv_p=*/NULL,
9127 /*parenthesized_p=*/NULL,
9128 /*member_p=*/false);
9129 if (declares_class_or_enum & 2)
9130 cp_parser_check_for_definition_in_return_type (declarator,
9131 decl_specifiers.type);
9132 if (declarator != cp_error_declarator)
9133 {
9134 decl = grokdeclarator (declarator, &decl_specifiers,
9135 NORMAL, 0, NULL);
9136 /* Turn access control back on for names used during
9137 template instantiation. */
9138 pop_deferring_access_checks ();
9139 /* Do the explicit instantiation. */
9140 do_decl_instantiation (decl, extension_specifier);
9141 }
9142 else
9143 {
9144 pop_deferring_access_checks ();
9145 /* Skip the body of the explicit instantiation. */
9146 cp_parser_skip_to_end_of_statement (parser);
9147 }
9148 }
9149 /* We're done with the instantiation. */
9150 end_explicit_instantiation ();
9151
9152 cp_parser_consume_semicolon_at_end_of_statement (parser);
9153 }
9154
9155 /* Parse an explicit-specialization.
9156
9157 explicit-specialization:
9158 template < > declaration
9159
9160 Although the standard says `declaration', what it really means is:
9161
9162 explicit-specialization:
9163 template <> decl-specifier [opt] init-declarator [opt] ;
9164 template <> function-definition
9165 template <> explicit-specialization
9166 template <> template-declaration */
9167
9168 static void
9169 cp_parser_explicit_specialization (cp_parser* parser)
9170 {
9171 /* Look for the `template' keyword. */
9172 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9173 /* Look for the `<'. */
9174 cp_parser_require (parser, CPP_LESS, "`<'");
9175 /* Look for the `>'. */
9176 cp_parser_require (parser, CPP_GREATER, "`>'");
9177 /* We have processed another parameter list. */
9178 ++parser->num_template_parameter_lists;
9179 /* Let the front end know that we are beginning a specialization. */
9180 begin_specialization ();
9181
9182 /* If the next keyword is `template', we need to figure out whether
9183 or not we're looking a template-declaration. */
9184 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9185 {
9186 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9187 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9188 cp_parser_template_declaration_after_export (parser,
9189 /*member_p=*/false);
9190 else
9191 cp_parser_explicit_specialization (parser);
9192 }
9193 else
9194 /* Parse the dependent declaration. */
9195 cp_parser_single_declaration (parser,
9196 /*member_p=*/false,
9197 /*friend_p=*/NULL);
9198
9199 /* We're done with the specialization. */
9200 end_specialization ();
9201 /* We're done with this parameter list. */
9202 --parser->num_template_parameter_lists;
9203 }
9204
9205 /* Parse a type-specifier.
9206
9207 type-specifier:
9208 simple-type-specifier
9209 class-specifier
9210 enum-specifier
9211 elaborated-type-specifier
9212 cv-qualifier
9213
9214 GNU Extension:
9215
9216 type-specifier:
9217 __complex__
9218
9219 Returns a representation of the type-specifier. For a
9220 class-specifier, enum-specifier, or elaborated-type-specifier, a
9221 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9222
9223 The parser flags FLAGS is used to control type-specifier parsing.
9224
9225 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9226 in a decl-specifier-seq.
9227
9228 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9229 class-specifier, enum-specifier, or elaborated-type-specifier, then
9230 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9231 if a type is declared; 2 if it is defined. Otherwise, it is set to
9232 zero.
9233
9234 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9235 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9236 is set to FALSE. */
9237
9238 static tree
9239 cp_parser_type_specifier (cp_parser* parser,
9240 cp_parser_flags flags,
9241 cp_decl_specifier_seq *decl_specs,
9242 bool is_declaration,
9243 int* declares_class_or_enum,
9244 bool* is_cv_qualifier)
9245 {
9246 tree type_spec = NULL_TREE;
9247 cp_token *token;
9248 enum rid keyword;
9249 cp_decl_spec ds = ds_last;
9250
9251 /* Assume this type-specifier does not declare a new type. */
9252 if (declares_class_or_enum)
9253 *declares_class_or_enum = 0;
9254 /* And that it does not specify a cv-qualifier. */
9255 if (is_cv_qualifier)
9256 *is_cv_qualifier = false;
9257 /* Peek at the next token. */
9258 token = cp_lexer_peek_token (parser->lexer);
9259
9260 /* If we're looking at a keyword, we can use that to guide the
9261 production we choose. */
9262 keyword = token->keyword;
9263 switch (keyword)
9264 {
9265 case RID_ENUM:
9266 /* 'enum' [identifier] '{' introduces an enum-specifier;
9267 'enum' <anything else> introduces an elaborated-type-specifier. */
9268 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9269 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9270 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9271 == CPP_OPEN_BRACE))
9272 {
9273 if (parser->num_template_parameter_lists)
9274 {
9275 error ("template declaration of %qs", "enum");
9276 cp_parser_skip_to_end_of_block_or_statement (parser);
9277 type_spec = error_mark_node;
9278 }
9279 else
9280 type_spec = cp_parser_enum_specifier (parser);
9281
9282 if (declares_class_or_enum)
9283 *declares_class_or_enum = 2;
9284 if (decl_specs)
9285 cp_parser_set_decl_spec_type (decl_specs,
9286 type_spec,
9287 /*user_defined_p=*/true);
9288 return type_spec;
9289 }
9290 else
9291 goto elaborated_type_specifier;
9292
9293 /* Any of these indicate either a class-specifier, or an
9294 elaborated-type-specifier. */
9295 case RID_CLASS:
9296 case RID_STRUCT:
9297 case RID_UNION:
9298 /* Parse tentatively so that we can back up if we don't find a
9299 class-specifier. */
9300 cp_parser_parse_tentatively (parser);
9301 /* Look for the class-specifier. */
9302 type_spec = cp_parser_class_specifier (parser);
9303 /* If that worked, we're done. */
9304 if (cp_parser_parse_definitely (parser))
9305 {
9306 if (declares_class_or_enum)
9307 *declares_class_or_enum = 2;
9308 if (decl_specs)
9309 cp_parser_set_decl_spec_type (decl_specs,
9310 type_spec,
9311 /*user_defined_p=*/true);
9312 return type_spec;
9313 }
9314
9315 /* Fall through. */
9316 elaborated_type_specifier:
9317 /* We're declaring (not defining) a class or enum. */
9318 if (declares_class_or_enum)
9319 *declares_class_or_enum = 1;
9320
9321 /* Fall through. */
9322 case RID_TYPENAME:
9323 /* Look for an elaborated-type-specifier. */
9324 type_spec
9325 = (cp_parser_elaborated_type_specifier
9326 (parser,
9327 decl_specs && decl_specs->specs[(int) ds_friend],
9328 is_declaration));
9329 if (decl_specs)
9330 cp_parser_set_decl_spec_type (decl_specs,
9331 type_spec,
9332 /*user_defined_p=*/true);
9333 return type_spec;
9334
9335 case RID_CONST:
9336 ds = ds_const;
9337 if (is_cv_qualifier)
9338 *is_cv_qualifier = true;
9339 break;
9340
9341 case RID_VOLATILE:
9342 ds = ds_volatile;
9343 if (is_cv_qualifier)
9344 *is_cv_qualifier = true;
9345 break;
9346
9347 case RID_RESTRICT:
9348 ds = ds_restrict;
9349 if (is_cv_qualifier)
9350 *is_cv_qualifier = true;
9351 break;
9352
9353 case RID_COMPLEX:
9354 /* The `__complex__' keyword is a GNU extension. */
9355 ds = ds_complex;
9356 break;
9357
9358 default:
9359 break;
9360 }
9361
9362 /* Handle simple keywords. */
9363 if (ds != ds_last)
9364 {
9365 if (decl_specs)
9366 {
9367 ++decl_specs->specs[(int)ds];
9368 decl_specs->any_specifiers_p = true;
9369 }
9370 return cp_lexer_consume_token (parser->lexer)->value;
9371 }
9372
9373 /* If we do not already have a type-specifier, assume we are looking
9374 at a simple-type-specifier. */
9375 type_spec = cp_parser_simple_type_specifier (parser,
9376 decl_specs,
9377 flags);
9378
9379 /* If we didn't find a type-specifier, and a type-specifier was not
9380 optional in this context, issue an error message. */
9381 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9382 {
9383 cp_parser_error (parser, "expected type specifier");
9384 return error_mark_node;
9385 }
9386
9387 return type_spec;
9388 }
9389
9390 /* Parse a simple-type-specifier.
9391
9392 simple-type-specifier:
9393 :: [opt] nested-name-specifier [opt] type-name
9394 :: [opt] nested-name-specifier template template-id
9395 char
9396 wchar_t
9397 bool
9398 short
9399 int
9400 long
9401 signed
9402 unsigned
9403 float
9404 double
9405 void
9406
9407 GNU Extension:
9408
9409 simple-type-specifier:
9410 __typeof__ unary-expression
9411 __typeof__ ( type-id )
9412
9413 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9414 appropriately updated. */
9415
9416 static tree
9417 cp_parser_simple_type_specifier (cp_parser* parser,
9418 cp_decl_specifier_seq *decl_specs,
9419 cp_parser_flags flags)
9420 {
9421 tree type = NULL_TREE;
9422 cp_token *token;
9423
9424 /* Peek at the next token. */
9425 token = cp_lexer_peek_token (parser->lexer);
9426
9427 /* If we're looking at a keyword, things are easy. */
9428 switch (token->keyword)
9429 {
9430 case RID_CHAR:
9431 if (decl_specs)
9432 decl_specs->explicit_char_p = true;
9433 type = char_type_node;
9434 break;
9435 case RID_WCHAR:
9436 type = wchar_type_node;
9437 break;
9438 case RID_BOOL:
9439 type = boolean_type_node;
9440 break;
9441 case RID_SHORT:
9442 if (decl_specs)
9443 ++decl_specs->specs[(int) ds_short];
9444 type = short_integer_type_node;
9445 break;
9446 case RID_INT:
9447 if (decl_specs)
9448 decl_specs->explicit_int_p = true;
9449 type = integer_type_node;
9450 break;
9451 case RID_LONG:
9452 if (decl_specs)
9453 ++decl_specs->specs[(int) ds_long];
9454 type = long_integer_type_node;
9455 break;
9456 case RID_SIGNED:
9457 if (decl_specs)
9458 ++decl_specs->specs[(int) ds_signed];
9459 type = integer_type_node;
9460 break;
9461 case RID_UNSIGNED:
9462 if (decl_specs)
9463 ++decl_specs->specs[(int) ds_unsigned];
9464 type = unsigned_type_node;
9465 break;
9466 case RID_FLOAT:
9467 type = float_type_node;
9468 break;
9469 case RID_DOUBLE:
9470 type = double_type_node;
9471 break;
9472 case RID_VOID:
9473 type = void_type_node;
9474 break;
9475
9476 case RID_TYPEOF:
9477 /* Consume the `typeof' token. */
9478 cp_lexer_consume_token (parser->lexer);
9479 /* Parse the operand to `typeof'. */
9480 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9481 /* If it is not already a TYPE, take its type. */
9482 if (!TYPE_P (type))
9483 type = finish_typeof (type);
9484
9485 if (decl_specs)
9486 cp_parser_set_decl_spec_type (decl_specs, type,
9487 /*user_defined_p=*/true);
9488
9489 return type;
9490
9491 default:
9492 break;
9493 }
9494
9495 /* If the type-specifier was for a built-in type, we're done. */
9496 if (type)
9497 {
9498 tree id;
9499
9500 /* Record the type. */
9501 if (decl_specs
9502 && (token->keyword != RID_SIGNED
9503 && token->keyword != RID_UNSIGNED
9504 && token->keyword != RID_SHORT
9505 && token->keyword != RID_LONG))
9506 cp_parser_set_decl_spec_type (decl_specs,
9507 type,
9508 /*user_defined=*/false);
9509 if (decl_specs)
9510 decl_specs->any_specifiers_p = true;
9511
9512 /* Consume the token. */
9513 id = cp_lexer_consume_token (parser->lexer)->value;
9514
9515 /* There is no valid C++ program where a non-template type is
9516 followed by a "<". That usually indicates that the user thought
9517 that the type was a template. */
9518 cp_parser_check_for_invalid_template_id (parser, type);
9519
9520 return TYPE_NAME (type);
9521 }
9522
9523 /* The type-specifier must be a user-defined type. */
9524 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9525 {
9526 bool qualified_p;
9527 bool global_p;
9528
9529 /* Don't gobble tokens or issue error messages if this is an
9530 optional type-specifier. */
9531 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9532 cp_parser_parse_tentatively (parser);
9533
9534 /* Look for the optional `::' operator. */
9535 global_p
9536 = (cp_parser_global_scope_opt (parser,
9537 /*current_scope_valid_p=*/false)
9538 != NULL_TREE);
9539 /* Look for the nested-name specifier. */
9540 qualified_p
9541 = (cp_parser_nested_name_specifier_opt (parser,
9542 /*typename_keyword_p=*/false,
9543 /*check_dependency_p=*/true,
9544 /*type_p=*/false,
9545 /*is_declaration=*/false)
9546 != NULL_TREE);
9547 /* If we have seen a nested-name-specifier, and the next token
9548 is `template', then we are using the template-id production. */
9549 if (parser->scope
9550 && cp_parser_optional_template_keyword (parser))
9551 {
9552 /* Look for the template-id. */
9553 type = cp_parser_template_id (parser,
9554 /*template_keyword_p=*/true,
9555 /*check_dependency_p=*/true,
9556 /*is_declaration=*/false);
9557 /* If the template-id did not name a type, we are out of
9558 luck. */
9559 if (TREE_CODE (type) != TYPE_DECL)
9560 {
9561 cp_parser_error (parser, "expected template-id for type");
9562 type = NULL_TREE;
9563 }
9564 }
9565 /* Otherwise, look for a type-name. */
9566 else
9567 type = cp_parser_type_name (parser);
9568 /* Keep track of all name-lookups performed in class scopes. */
9569 if (type
9570 && !global_p
9571 && !qualified_p
9572 && TREE_CODE (type) == TYPE_DECL
9573 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9574 maybe_note_name_used_in_class (DECL_NAME (type), type);
9575 /* If it didn't work out, we don't have a TYPE. */
9576 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9577 && !cp_parser_parse_definitely (parser))
9578 type = NULL_TREE;
9579 if (type && decl_specs)
9580 cp_parser_set_decl_spec_type (decl_specs, type,
9581 /*user_defined=*/true);
9582 }
9583
9584 /* If we didn't get a type-name, issue an error message. */
9585 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9586 {
9587 cp_parser_error (parser, "expected type-name");
9588 return error_mark_node;
9589 }
9590
9591 /* There is no valid C++ program where a non-template type is
9592 followed by a "<". That usually indicates that the user thought
9593 that the type was a template. */
9594 if (type && type != error_mark_node)
9595 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9596
9597 return type;
9598 }
9599
9600 /* Parse a type-name.
9601
9602 type-name:
9603 class-name
9604 enum-name
9605 typedef-name
9606
9607 enum-name:
9608 identifier
9609
9610 typedef-name:
9611 identifier
9612
9613 Returns a TYPE_DECL for the type. */
9614
9615 static tree
9616 cp_parser_type_name (cp_parser* parser)
9617 {
9618 tree type_decl;
9619 tree identifier;
9620
9621 /* We can't know yet whether it is a class-name or not. */
9622 cp_parser_parse_tentatively (parser);
9623 /* Try a class-name. */
9624 type_decl = cp_parser_class_name (parser,
9625 /*typename_keyword_p=*/false,
9626 /*template_keyword_p=*/false,
9627 none_type,
9628 /*check_dependency_p=*/true,
9629 /*class_head_p=*/false,
9630 /*is_declaration=*/false);
9631 /* If it's not a class-name, keep looking. */
9632 if (!cp_parser_parse_definitely (parser))
9633 {
9634 /* It must be a typedef-name or an enum-name. */
9635 identifier = cp_parser_identifier (parser);
9636 if (identifier == error_mark_node)
9637 return error_mark_node;
9638
9639 /* Look up the type-name. */
9640 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9641 /* Issue an error if we did not find a type-name. */
9642 if (TREE_CODE (type_decl) != TYPE_DECL)
9643 {
9644 if (!cp_parser_simulate_error (parser))
9645 cp_parser_name_lookup_error (parser, identifier, type_decl,
9646 "is not a type");
9647 type_decl = error_mark_node;
9648 }
9649 /* Remember that the name was used in the definition of the
9650 current class so that we can check later to see if the
9651 meaning would have been different after the class was
9652 entirely defined. */
9653 else if (type_decl != error_mark_node
9654 && !parser->scope)
9655 maybe_note_name_used_in_class (identifier, type_decl);
9656 }
9657
9658 return type_decl;
9659 }
9660
9661
9662 /* Parse an elaborated-type-specifier. Note that the grammar given
9663 here incorporates the resolution to DR68.
9664
9665 elaborated-type-specifier:
9666 class-key :: [opt] nested-name-specifier [opt] identifier
9667 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9668 enum :: [opt] nested-name-specifier [opt] identifier
9669 typename :: [opt] nested-name-specifier identifier
9670 typename :: [opt] nested-name-specifier template [opt]
9671 template-id
9672
9673 GNU extension:
9674
9675 elaborated-type-specifier:
9676 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9677 class-key attributes :: [opt] nested-name-specifier [opt]
9678 template [opt] template-id
9679 enum attributes :: [opt] nested-name-specifier [opt] identifier
9680
9681 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9682 declared `friend'. If IS_DECLARATION is TRUE, then this
9683 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9684 something is being declared.
9685
9686 Returns the TYPE specified. */
9687
9688 static tree
9689 cp_parser_elaborated_type_specifier (cp_parser* parser,
9690 bool is_friend,
9691 bool is_declaration)
9692 {
9693 enum tag_types tag_type;
9694 tree identifier;
9695 tree type = NULL_TREE;
9696 tree attributes = NULL_TREE;
9697
9698 /* See if we're looking at the `enum' keyword. */
9699 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9700 {
9701 /* Consume the `enum' token. */
9702 cp_lexer_consume_token (parser->lexer);
9703 /* Remember that it's an enumeration type. */
9704 tag_type = enum_type;
9705 /* Parse the attributes. */
9706 attributes = cp_parser_attributes_opt (parser);
9707 }
9708 /* Or, it might be `typename'. */
9709 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9710 RID_TYPENAME))
9711 {
9712 /* Consume the `typename' token. */
9713 cp_lexer_consume_token (parser->lexer);
9714 /* Remember that it's a `typename' type. */
9715 tag_type = typename_type;
9716 /* The `typename' keyword is only allowed in templates. */
9717 if (!processing_template_decl)
9718 pedwarn ("using %<typename%> outside of template");
9719 }
9720 /* Otherwise it must be a class-key. */
9721 else
9722 {
9723 tag_type = cp_parser_class_key (parser);
9724 if (tag_type == none_type)
9725 return error_mark_node;
9726 /* Parse the attributes. */
9727 attributes = cp_parser_attributes_opt (parser);
9728 }
9729
9730 /* Look for the `::' operator. */
9731 cp_parser_global_scope_opt (parser,
9732 /*current_scope_valid_p=*/false);
9733 /* Look for the nested-name-specifier. */
9734 if (tag_type == typename_type)
9735 {
9736 if (cp_parser_nested_name_specifier (parser,
9737 /*typename_keyword_p=*/true,
9738 /*check_dependency_p=*/true,
9739 /*type_p=*/true,
9740 is_declaration)
9741 == error_mark_node)
9742 return error_mark_node;
9743 }
9744 else
9745 /* Even though `typename' is not present, the proposed resolution
9746 to Core Issue 180 says that in `class A<T>::B', `B' should be
9747 considered a type-name, even if `A<T>' is dependent. */
9748 cp_parser_nested_name_specifier_opt (parser,
9749 /*typename_keyword_p=*/true,
9750 /*check_dependency_p=*/true,
9751 /*type_p=*/true,
9752 is_declaration);
9753 /* For everything but enumeration types, consider a template-id. */
9754 if (tag_type != enum_type)
9755 {
9756 bool template_p = false;
9757 tree decl;
9758
9759 /* Allow the `template' keyword. */
9760 template_p = cp_parser_optional_template_keyword (parser);
9761 /* If we didn't see `template', we don't know if there's a
9762 template-id or not. */
9763 if (!template_p)
9764 cp_parser_parse_tentatively (parser);
9765 /* Parse the template-id. */
9766 decl = cp_parser_template_id (parser, template_p,
9767 /*check_dependency_p=*/true,
9768 is_declaration);
9769 /* If we didn't find a template-id, look for an ordinary
9770 identifier. */
9771 if (!template_p && !cp_parser_parse_definitely (parser))
9772 ;
9773 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9774 in effect, then we must assume that, upon instantiation, the
9775 template will correspond to a class. */
9776 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9777 && tag_type == typename_type)
9778 type = make_typename_type (parser->scope, decl,
9779 typename_type,
9780 /*complain=*/1);
9781 else
9782 type = TREE_TYPE (decl);
9783 }
9784
9785 /* For an enumeration type, consider only a plain identifier. */
9786 if (!type)
9787 {
9788 identifier = cp_parser_identifier (parser);
9789
9790 if (identifier == error_mark_node)
9791 {
9792 parser->scope = NULL_TREE;
9793 return error_mark_node;
9794 }
9795
9796 /* For a `typename', we needn't call xref_tag. */
9797 if (tag_type == typename_type
9798 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9799 return cp_parser_make_typename_type (parser, parser->scope,
9800 identifier);
9801 /* Look up a qualified name in the usual way. */
9802 if (parser->scope)
9803 {
9804 tree decl;
9805
9806 decl = cp_parser_lookup_name (parser, identifier,
9807 tag_type,
9808 /*is_template=*/false,
9809 /*is_namespace=*/false,
9810 /*check_dependency=*/true,
9811 /*ambiguous_p=*/NULL);
9812
9813 /* If we are parsing friend declaration, DECL may be a
9814 TEMPLATE_DECL tree node here. However, we need to check
9815 whether this TEMPLATE_DECL results in valid code. Consider
9816 the following example:
9817
9818 namespace N {
9819 template <class T> class C {};
9820 }
9821 class X {
9822 template <class T> friend class N::C; // #1, valid code
9823 };
9824 template <class T> class Y {
9825 friend class N::C; // #2, invalid code
9826 };
9827
9828 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9829 name lookup of `N::C'. We see that friend declaration must
9830 be template for the code to be valid. Note that
9831 processing_template_decl does not work here since it is
9832 always 1 for the above two cases. */
9833
9834 decl = (cp_parser_maybe_treat_template_as_class
9835 (decl, /*tag_name_p=*/is_friend
9836 && parser->num_template_parameter_lists));
9837
9838 if (TREE_CODE (decl) != TYPE_DECL)
9839 {
9840 cp_parser_diagnose_invalid_type_name (parser,
9841 parser->scope,
9842 identifier);
9843 return error_mark_node;
9844 }
9845
9846 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9847 check_elaborated_type_specifier
9848 (tag_type, decl,
9849 (parser->num_template_parameter_lists
9850 || DECL_SELF_REFERENCE_P (decl)));
9851
9852 type = TREE_TYPE (decl);
9853 }
9854 else
9855 {
9856 /* An elaborated-type-specifier sometimes introduces a new type and
9857 sometimes names an existing type. Normally, the rule is that it
9858 introduces a new type only if there is not an existing type of
9859 the same name already in scope. For example, given:
9860
9861 struct S {};
9862 void f() { struct S s; }
9863
9864 the `struct S' in the body of `f' is the same `struct S' as in
9865 the global scope; the existing definition is used. However, if
9866 there were no global declaration, this would introduce a new
9867 local class named `S'.
9868
9869 An exception to this rule applies to the following code:
9870
9871 namespace N { struct S; }
9872
9873 Here, the elaborated-type-specifier names a new type
9874 unconditionally; even if there is already an `S' in the
9875 containing scope this declaration names a new type.
9876 This exception only applies if the elaborated-type-specifier
9877 forms the complete declaration:
9878
9879 [class.name]
9880
9881 A declaration consisting solely of `class-key identifier ;' is
9882 either a redeclaration of the name in the current scope or a
9883 forward declaration of the identifier as a class name. It
9884 introduces the name into the current scope.
9885
9886 We are in this situation precisely when the next token is a `;'.
9887
9888 An exception to the exception is that a `friend' declaration does
9889 *not* name a new type; i.e., given:
9890
9891 struct S { friend struct T; };
9892
9893 `T' is not a new type in the scope of `S'.
9894
9895 Also, `new struct S' or `sizeof (struct S)' never results in the
9896 definition of a new type; a new type can only be declared in a
9897 declaration context. */
9898
9899 tag_scope ts;
9900 if (is_friend)
9901 /* Friends have special name lookup rules. */
9902 ts = ts_within_enclosing_non_class;
9903 else if (is_declaration
9904 && cp_lexer_next_token_is (parser->lexer,
9905 CPP_SEMICOLON))
9906 /* This is a `class-key identifier ;' */
9907 ts = ts_current;
9908 else
9909 ts = ts_global;
9910
9911 /* Warn about attributes. They are ignored. */
9912 if (attributes)
9913 warning ("type attributes are honored only at type definition");
9914
9915 type = xref_tag (tag_type, identifier, ts,
9916 parser->num_template_parameter_lists);
9917 }
9918 }
9919 if (tag_type != enum_type)
9920 cp_parser_check_class_key (tag_type, type);
9921
9922 /* A "<" cannot follow an elaborated type specifier. If that
9923 happens, the user was probably trying to form a template-id. */
9924 cp_parser_check_for_invalid_template_id (parser, type);
9925
9926 return type;
9927 }
9928
9929 /* Parse an enum-specifier.
9930
9931 enum-specifier:
9932 enum identifier [opt] { enumerator-list [opt] }
9933
9934 GNU Extensions:
9935 enum identifier [opt] { enumerator-list [opt] } attributes
9936
9937 Returns an ENUM_TYPE representing the enumeration. */
9938
9939 static tree
9940 cp_parser_enum_specifier (cp_parser* parser)
9941 {
9942 tree identifier;
9943 tree type;
9944
9945 /* Caller guarantees that the current token is 'enum', an identifier
9946 possibly follows, and the token after that is an opening brace.
9947 If we don't have an identifier, fabricate an anonymous name for
9948 the enumeration being defined. */
9949 cp_lexer_consume_token (parser->lexer);
9950
9951 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9952 identifier = cp_parser_identifier (parser);
9953 else
9954 identifier = make_anon_name ();
9955
9956 /* Issue an error message if type-definitions are forbidden here. */
9957 cp_parser_check_type_definition (parser);
9958
9959 /* Create the new type. We do this before consuming the opening brace
9960 so the enum will be recorded as being on the line of its tag (or the
9961 'enum' keyword, if there is no tag). */
9962 type = start_enum (identifier);
9963
9964 /* Consume the opening brace. */
9965 cp_lexer_consume_token (parser->lexer);
9966
9967 /* If the next token is not '}', then there are some enumerators. */
9968 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9969 cp_parser_enumerator_list (parser, type);
9970
9971 /* Consume the final '}'. */
9972 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9973
9974 /* Look for trailing attributes to apply to this enumeration, and
9975 apply them if appropriate. */
9976 if (cp_parser_allow_gnu_extensions_p (parser))
9977 {
9978 tree trailing_attr = cp_parser_attributes_opt (parser);
9979 cplus_decl_attributes (&type,
9980 trailing_attr,
9981 (int) ATTR_FLAG_TYPE_IN_PLACE);
9982 }
9983
9984 /* Finish up the enumeration. */
9985 finish_enum (type);
9986
9987 return type;
9988 }
9989
9990 /* Parse an enumerator-list. The enumerators all have the indicated
9991 TYPE.
9992
9993 enumerator-list:
9994 enumerator-definition
9995 enumerator-list , enumerator-definition */
9996
9997 static void
9998 cp_parser_enumerator_list (cp_parser* parser, tree type)
9999 {
10000 while (true)
10001 {
10002 /* Parse an enumerator-definition. */
10003 cp_parser_enumerator_definition (parser, type);
10004
10005 /* If the next token is not a ',', we've reached the end of
10006 the list. */
10007 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10008 break;
10009 /* Otherwise, consume the `,' and keep going. */
10010 cp_lexer_consume_token (parser->lexer);
10011 /* If the next token is a `}', there is a trailing comma. */
10012 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10013 {
10014 if (pedantic && !in_system_header)
10015 pedwarn ("comma at end of enumerator list");
10016 break;
10017 }
10018 }
10019 }
10020
10021 /* Parse an enumerator-definition. The enumerator has the indicated
10022 TYPE.
10023
10024 enumerator-definition:
10025 enumerator
10026 enumerator = constant-expression
10027
10028 enumerator:
10029 identifier */
10030
10031 static void
10032 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10033 {
10034 tree identifier;
10035 tree value;
10036
10037 /* Look for the identifier. */
10038 identifier = cp_parser_identifier (parser);
10039 if (identifier == error_mark_node)
10040 return;
10041
10042 /* If the next token is an '=', then there is an explicit value. */
10043 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10044 {
10045 /* Consume the `=' token. */
10046 cp_lexer_consume_token (parser->lexer);
10047 /* Parse the value. */
10048 value = cp_parser_constant_expression (parser,
10049 /*allow_non_constant_p=*/false,
10050 NULL);
10051 }
10052 else
10053 value = NULL_TREE;
10054
10055 /* Create the enumerator. */
10056 build_enumerator (identifier, value, type);
10057 }
10058
10059 /* Parse a namespace-name.
10060
10061 namespace-name:
10062 original-namespace-name
10063 namespace-alias
10064
10065 Returns the NAMESPACE_DECL for the namespace. */
10066
10067 static tree
10068 cp_parser_namespace_name (cp_parser* parser)
10069 {
10070 tree identifier;
10071 tree namespace_decl;
10072
10073 /* Get the name of the namespace. */
10074 identifier = cp_parser_identifier (parser);
10075 if (identifier == error_mark_node)
10076 return error_mark_node;
10077
10078 /* Look up the identifier in the currently active scope. Look only
10079 for namespaces, due to:
10080
10081 [basic.lookup.udir]
10082
10083 When looking up a namespace-name in a using-directive or alias
10084 definition, only namespace names are considered.
10085
10086 And:
10087
10088 [basic.lookup.qual]
10089
10090 During the lookup of a name preceding the :: scope resolution
10091 operator, object, function, and enumerator names are ignored.
10092
10093 (Note that cp_parser_class_or_namespace_name only calls this
10094 function if the token after the name is the scope resolution
10095 operator.) */
10096 namespace_decl = cp_parser_lookup_name (parser, identifier,
10097 none_type,
10098 /*is_template=*/false,
10099 /*is_namespace=*/true,
10100 /*check_dependency=*/true,
10101 /*ambiguous_p=*/NULL);
10102 /* If it's not a namespace, issue an error. */
10103 if (namespace_decl == error_mark_node
10104 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10105 {
10106 cp_parser_error (parser, "expected namespace-name");
10107 namespace_decl = error_mark_node;
10108 }
10109
10110 return namespace_decl;
10111 }
10112
10113 /* Parse a namespace-definition.
10114
10115 namespace-definition:
10116 named-namespace-definition
10117 unnamed-namespace-definition
10118
10119 named-namespace-definition:
10120 original-namespace-definition
10121 extension-namespace-definition
10122
10123 original-namespace-definition:
10124 namespace identifier { namespace-body }
10125
10126 extension-namespace-definition:
10127 namespace original-namespace-name { namespace-body }
10128
10129 unnamed-namespace-definition:
10130 namespace { namespace-body } */
10131
10132 static void
10133 cp_parser_namespace_definition (cp_parser* parser)
10134 {
10135 tree identifier;
10136
10137 /* Look for the `namespace' keyword. */
10138 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10139
10140 /* Get the name of the namespace. We do not attempt to distinguish
10141 between an original-namespace-definition and an
10142 extension-namespace-definition at this point. The semantic
10143 analysis routines are responsible for that. */
10144 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10145 identifier = cp_parser_identifier (parser);
10146 else
10147 identifier = NULL_TREE;
10148
10149 /* Look for the `{' to start the namespace. */
10150 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10151 /* Start the namespace. */
10152 push_namespace (identifier);
10153 /* Parse the body of the namespace. */
10154 cp_parser_namespace_body (parser);
10155 /* Finish the namespace. */
10156 pop_namespace ();
10157 /* Look for the final `}'. */
10158 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10159 }
10160
10161 /* Parse a namespace-body.
10162
10163 namespace-body:
10164 declaration-seq [opt] */
10165
10166 static void
10167 cp_parser_namespace_body (cp_parser* parser)
10168 {
10169 cp_parser_declaration_seq_opt (parser);
10170 }
10171
10172 /* Parse a namespace-alias-definition.
10173
10174 namespace-alias-definition:
10175 namespace identifier = qualified-namespace-specifier ; */
10176
10177 static void
10178 cp_parser_namespace_alias_definition (cp_parser* parser)
10179 {
10180 tree identifier;
10181 tree namespace_specifier;
10182
10183 /* Look for the `namespace' keyword. */
10184 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10185 /* Look for the identifier. */
10186 identifier = cp_parser_identifier (parser);
10187 if (identifier == error_mark_node)
10188 return;
10189 /* Look for the `=' token. */
10190 cp_parser_require (parser, CPP_EQ, "`='");
10191 /* Look for the qualified-namespace-specifier. */
10192 namespace_specifier
10193 = cp_parser_qualified_namespace_specifier (parser);
10194 /* Look for the `;' token. */
10195 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10196
10197 /* Register the alias in the symbol table. */
10198 do_namespace_alias (identifier, namespace_specifier);
10199 }
10200
10201 /* Parse a qualified-namespace-specifier.
10202
10203 qualified-namespace-specifier:
10204 :: [opt] nested-name-specifier [opt] namespace-name
10205
10206 Returns a NAMESPACE_DECL corresponding to the specified
10207 namespace. */
10208
10209 static tree
10210 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10211 {
10212 /* Look for the optional `::'. */
10213 cp_parser_global_scope_opt (parser,
10214 /*current_scope_valid_p=*/false);
10215
10216 /* Look for the optional nested-name-specifier. */
10217 cp_parser_nested_name_specifier_opt (parser,
10218 /*typename_keyword_p=*/false,
10219 /*check_dependency_p=*/true,
10220 /*type_p=*/false,
10221 /*is_declaration=*/true);
10222
10223 return cp_parser_namespace_name (parser);
10224 }
10225
10226 /* Parse a using-declaration.
10227
10228 using-declaration:
10229 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10230 using :: unqualified-id ; */
10231
10232 static void
10233 cp_parser_using_declaration (cp_parser* parser)
10234 {
10235 cp_token *token;
10236 bool typename_p = false;
10237 bool global_scope_p;
10238 tree decl;
10239 tree identifier;
10240 tree qscope;
10241
10242 /* Look for the `using' keyword. */
10243 cp_parser_require_keyword (parser, RID_USING, "`using'");
10244
10245 /* Peek at the next token. */
10246 token = cp_lexer_peek_token (parser->lexer);
10247 /* See if it's `typename'. */
10248 if (token->keyword == RID_TYPENAME)
10249 {
10250 /* Remember that we've seen it. */
10251 typename_p = true;
10252 /* Consume the `typename' token. */
10253 cp_lexer_consume_token (parser->lexer);
10254 }
10255
10256 /* Look for the optional global scope qualification. */
10257 global_scope_p
10258 = (cp_parser_global_scope_opt (parser,
10259 /*current_scope_valid_p=*/false)
10260 != NULL_TREE);
10261
10262 /* If we saw `typename', or didn't see `::', then there must be a
10263 nested-name-specifier present. */
10264 if (typename_p || !global_scope_p)
10265 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10266 /*check_dependency_p=*/true,
10267 /*type_p=*/false,
10268 /*is_declaration=*/true);
10269 /* Otherwise, we could be in either of the two productions. In that
10270 case, treat the nested-name-specifier as optional. */
10271 else
10272 qscope = cp_parser_nested_name_specifier_opt (parser,
10273 /*typename_keyword_p=*/false,
10274 /*check_dependency_p=*/true,
10275 /*type_p=*/false,
10276 /*is_declaration=*/true);
10277 if (!qscope)
10278 qscope = global_namespace;
10279
10280 /* Parse the unqualified-id. */
10281 identifier = cp_parser_unqualified_id (parser,
10282 /*template_keyword_p=*/false,
10283 /*check_dependency_p=*/true,
10284 /*declarator_p=*/true);
10285
10286 /* The function we call to handle a using-declaration is different
10287 depending on what scope we are in. */
10288 if (identifier == error_mark_node)
10289 ;
10290 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10291 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10292 /* [namespace.udecl]
10293
10294 A using declaration shall not name a template-id. */
10295 error ("a template-id may not appear in a using-declaration");
10296 else
10297 {
10298 if (at_class_scope_p ())
10299 {
10300 /* Create the USING_DECL. */
10301 decl = do_class_using_decl (parser->scope, identifier);
10302 /* Add it to the list of members in this class. */
10303 finish_member_declaration (decl);
10304 }
10305 else
10306 {
10307 decl = cp_parser_lookup_name_simple (parser, identifier);
10308 if (decl == error_mark_node)
10309 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10310 else if (!at_namespace_scope_p ())
10311 do_local_using_decl (decl, qscope, identifier);
10312 else
10313 do_toplevel_using_decl (decl, qscope, identifier);
10314 }
10315 }
10316
10317 /* Look for the final `;'. */
10318 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10319 }
10320
10321 /* Parse a using-directive.
10322
10323 using-directive:
10324 using namespace :: [opt] nested-name-specifier [opt]
10325 namespace-name ; */
10326
10327 static void
10328 cp_parser_using_directive (cp_parser* parser)
10329 {
10330 tree namespace_decl;
10331 tree attribs;
10332
10333 /* Look for the `using' keyword. */
10334 cp_parser_require_keyword (parser, RID_USING, "`using'");
10335 /* And the `namespace' keyword. */
10336 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10337 /* Look for the optional `::' operator. */
10338 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10339 /* And the optional nested-name-specifier. */
10340 cp_parser_nested_name_specifier_opt (parser,
10341 /*typename_keyword_p=*/false,
10342 /*check_dependency_p=*/true,
10343 /*type_p=*/false,
10344 /*is_declaration=*/true);
10345 /* Get the namespace being used. */
10346 namespace_decl = cp_parser_namespace_name (parser);
10347 /* And any specified attributes. */
10348 attribs = cp_parser_attributes_opt (parser);
10349 /* Update the symbol table. */
10350 parse_using_directive (namespace_decl, attribs);
10351 /* Look for the final `;'. */
10352 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10353 }
10354
10355 /* Parse an asm-definition.
10356
10357 asm-definition:
10358 asm ( string-literal ) ;
10359
10360 GNU Extension:
10361
10362 asm-definition:
10363 asm volatile [opt] ( string-literal ) ;
10364 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10365 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10366 : asm-operand-list [opt] ) ;
10367 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10368 : asm-operand-list [opt]
10369 : asm-operand-list [opt] ) ; */
10370
10371 static void
10372 cp_parser_asm_definition (cp_parser* parser)
10373 {
10374 tree string;
10375 tree outputs = NULL_TREE;
10376 tree inputs = NULL_TREE;
10377 tree clobbers = NULL_TREE;
10378 tree asm_stmt;
10379 bool volatile_p = false;
10380 bool extended_p = false;
10381
10382 /* Look for the `asm' keyword. */
10383 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10384 /* See if the next token is `volatile'. */
10385 if (cp_parser_allow_gnu_extensions_p (parser)
10386 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10387 {
10388 /* Remember that we saw the `volatile' keyword. */
10389 volatile_p = true;
10390 /* Consume the token. */
10391 cp_lexer_consume_token (parser->lexer);
10392 }
10393 /* Look for the opening `('. */
10394 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10395 return;
10396 /* Look for the string. */
10397 string = cp_parser_string_literal (parser, false, false);
10398 if (string == error_mark_node)
10399 {
10400 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10401 /*consume_paren=*/true);
10402 return;
10403 }
10404
10405 /* If we're allowing GNU extensions, check for the extended assembly
10406 syntax. Unfortunately, the `:' tokens need not be separated by
10407 a space in C, and so, for compatibility, we tolerate that here
10408 too. Doing that means that we have to treat the `::' operator as
10409 two `:' tokens. */
10410 if (cp_parser_allow_gnu_extensions_p (parser)
10411 && at_function_scope_p ()
10412 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10413 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10414 {
10415 bool inputs_p = false;
10416 bool clobbers_p = false;
10417
10418 /* The extended syntax was used. */
10419 extended_p = true;
10420
10421 /* Look for outputs. */
10422 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10423 {
10424 /* Consume the `:'. */
10425 cp_lexer_consume_token (parser->lexer);
10426 /* Parse the output-operands. */
10427 if (cp_lexer_next_token_is_not (parser->lexer,
10428 CPP_COLON)
10429 && cp_lexer_next_token_is_not (parser->lexer,
10430 CPP_SCOPE)
10431 && cp_lexer_next_token_is_not (parser->lexer,
10432 CPP_CLOSE_PAREN))
10433 outputs = cp_parser_asm_operand_list (parser);
10434 }
10435 /* If the next token is `::', there are no outputs, and the
10436 next token is the beginning of the inputs. */
10437 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10438 /* The inputs are coming next. */
10439 inputs_p = true;
10440
10441 /* Look for inputs. */
10442 if (inputs_p
10443 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10444 {
10445 /* Consume the `:' or `::'. */
10446 cp_lexer_consume_token (parser->lexer);
10447 /* Parse the output-operands. */
10448 if (cp_lexer_next_token_is_not (parser->lexer,
10449 CPP_COLON)
10450 && cp_lexer_next_token_is_not (parser->lexer,
10451 CPP_CLOSE_PAREN))
10452 inputs = cp_parser_asm_operand_list (parser);
10453 }
10454 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10455 /* The clobbers are coming next. */
10456 clobbers_p = true;
10457
10458 /* Look for clobbers. */
10459 if (clobbers_p
10460 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10461 {
10462 /* Consume the `:' or `::'. */
10463 cp_lexer_consume_token (parser->lexer);
10464 /* Parse the clobbers. */
10465 if (cp_lexer_next_token_is_not (parser->lexer,
10466 CPP_CLOSE_PAREN))
10467 clobbers = cp_parser_asm_clobber_list (parser);
10468 }
10469 }
10470 /* Look for the closing `)'. */
10471 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10472 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10473 /*consume_paren=*/true);
10474 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10475
10476 /* Create the ASM_EXPR. */
10477 if (at_function_scope_p ())
10478 {
10479 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10480 inputs, clobbers);
10481 /* If the extended syntax was not used, mark the ASM_EXPR. */
10482 if (!extended_p)
10483 {
10484 tree temp = asm_stmt;
10485 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10486 temp = TREE_OPERAND (temp, 0);
10487
10488 ASM_INPUT_P (temp) = 1;
10489 }
10490 }
10491 else
10492 assemble_asm (string);
10493 }
10494
10495 /* Declarators [gram.dcl.decl] */
10496
10497 /* Parse an init-declarator.
10498
10499 init-declarator:
10500 declarator initializer [opt]
10501
10502 GNU Extension:
10503
10504 init-declarator:
10505 declarator asm-specification [opt] attributes [opt] initializer [opt]
10506
10507 function-definition:
10508 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10509 function-body
10510 decl-specifier-seq [opt] declarator function-try-block
10511
10512 GNU Extension:
10513
10514 function-definition:
10515 __extension__ function-definition
10516
10517 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10518 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10519 then this declarator appears in a class scope. The new DECL created
10520 by this declarator is returned.
10521
10522 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10523 for a function-definition here as well. If the declarator is a
10524 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10525 be TRUE upon return. By that point, the function-definition will
10526 have been completely parsed.
10527
10528 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10529 is FALSE. */
10530
10531 static tree
10532 cp_parser_init_declarator (cp_parser* parser,
10533 cp_decl_specifier_seq *decl_specifiers,
10534 bool function_definition_allowed_p,
10535 bool member_p,
10536 int declares_class_or_enum,
10537 bool* function_definition_p)
10538 {
10539 cp_token *token;
10540 cp_declarator *declarator;
10541 tree prefix_attributes;
10542 tree attributes;
10543 tree asm_specification;
10544 tree initializer;
10545 tree decl = NULL_TREE;
10546 tree scope;
10547 bool is_initialized;
10548 bool is_parenthesized_init;
10549 bool is_non_constant_init;
10550 int ctor_dtor_or_conv_p;
10551 bool friend_p;
10552 tree pushed_scope = NULL;
10553
10554 /* Gather the attributes that were provided with the
10555 decl-specifiers. */
10556 prefix_attributes = decl_specifiers->attributes;
10557
10558 /* Assume that this is not the declarator for a function
10559 definition. */
10560 if (function_definition_p)
10561 *function_definition_p = false;
10562
10563 /* Defer access checks while parsing the declarator; we cannot know
10564 what names are accessible until we know what is being
10565 declared. */
10566 resume_deferring_access_checks ();
10567
10568 /* Parse the declarator. */
10569 declarator
10570 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10571 &ctor_dtor_or_conv_p,
10572 /*parenthesized_p=*/NULL,
10573 /*member_p=*/false);
10574 /* Gather up the deferred checks. */
10575 stop_deferring_access_checks ();
10576
10577 /* If the DECLARATOR was erroneous, there's no need to go
10578 further. */
10579 if (declarator == cp_error_declarator)
10580 return error_mark_node;
10581
10582 if (declares_class_or_enum & 2)
10583 cp_parser_check_for_definition_in_return_type (declarator,
10584 decl_specifiers->type);
10585
10586 /* Figure out what scope the entity declared by the DECLARATOR is
10587 located in. `grokdeclarator' sometimes changes the scope, so
10588 we compute it now. */
10589 scope = get_scope_of_declarator (declarator);
10590
10591 /* If we're allowing GNU extensions, look for an asm-specification
10592 and attributes. */
10593 if (cp_parser_allow_gnu_extensions_p (parser))
10594 {
10595 /* Look for an asm-specification. */
10596 asm_specification = cp_parser_asm_specification_opt (parser);
10597 /* And attributes. */
10598 attributes = cp_parser_attributes_opt (parser);
10599 }
10600 else
10601 {
10602 asm_specification = NULL_TREE;
10603 attributes = NULL_TREE;
10604 }
10605
10606 /* Peek at the next token. */
10607 token = cp_lexer_peek_token (parser->lexer);
10608 /* Check to see if the token indicates the start of a
10609 function-definition. */
10610 if (cp_parser_token_starts_function_definition_p (token))
10611 {
10612 if (!function_definition_allowed_p)
10613 {
10614 /* If a function-definition should not appear here, issue an
10615 error message. */
10616 cp_parser_error (parser,
10617 "a function-definition is not allowed here");
10618 return error_mark_node;
10619 }
10620 else
10621 {
10622 /* Neither attributes nor an asm-specification are allowed
10623 on a function-definition. */
10624 if (asm_specification)
10625 error ("an asm-specification is not allowed on a function-definition");
10626 if (attributes)
10627 error ("attributes are not allowed on a function-definition");
10628 /* This is a function-definition. */
10629 *function_definition_p = true;
10630
10631 /* Parse the function definition. */
10632 if (member_p)
10633 decl = cp_parser_save_member_function_body (parser,
10634 decl_specifiers,
10635 declarator,
10636 prefix_attributes);
10637 else
10638 decl
10639 = (cp_parser_function_definition_from_specifiers_and_declarator
10640 (parser, decl_specifiers, prefix_attributes, declarator));
10641
10642 return decl;
10643 }
10644 }
10645
10646 /* [dcl.dcl]
10647
10648 Only in function declarations for constructors, destructors, and
10649 type conversions can the decl-specifier-seq be omitted.
10650
10651 We explicitly postpone this check past the point where we handle
10652 function-definitions because we tolerate function-definitions
10653 that are missing their return types in some modes. */
10654 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10655 {
10656 cp_parser_error (parser,
10657 "expected constructor, destructor, or type conversion");
10658 return error_mark_node;
10659 }
10660
10661 /* An `=' or an `(' indicates an initializer. */
10662 is_initialized = (token->type == CPP_EQ
10663 || token->type == CPP_OPEN_PAREN);
10664 /* If the init-declarator isn't initialized and isn't followed by a
10665 `,' or `;', it's not a valid init-declarator. */
10666 if (!is_initialized
10667 && token->type != CPP_COMMA
10668 && token->type != CPP_SEMICOLON)
10669 {
10670 cp_parser_error (parser, "expected initializer");
10671 return error_mark_node;
10672 }
10673
10674 /* Because start_decl has side-effects, we should only call it if we
10675 know we're going ahead. By this point, we know that we cannot
10676 possibly be looking at any other construct. */
10677 cp_parser_commit_to_tentative_parse (parser);
10678
10679 /* If the decl specifiers were bad, issue an error now that we're
10680 sure this was intended to be a declarator. Then continue
10681 declaring the variable(s), as int, to try to cut down on further
10682 errors. */
10683 if (decl_specifiers->any_specifiers_p
10684 && decl_specifiers->type == error_mark_node)
10685 {
10686 cp_parser_error (parser, "invalid type in declaration");
10687 decl_specifiers->type = integer_type_node;
10688 }
10689
10690 /* Check to see whether or not this declaration is a friend. */
10691 friend_p = cp_parser_friend_p (decl_specifiers);
10692
10693 /* Check that the number of template-parameter-lists is OK. */
10694 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10695 return error_mark_node;
10696
10697 /* Enter the newly declared entry in the symbol table. If we're
10698 processing a declaration in a class-specifier, we wait until
10699 after processing the initializer. */
10700 if (!member_p)
10701 {
10702 if (parser->in_unbraced_linkage_specification_p)
10703 {
10704 decl_specifiers->storage_class = sc_extern;
10705 have_extern_spec = false;
10706 }
10707 decl = start_decl (declarator, decl_specifiers,
10708 is_initialized, attributes, prefix_attributes,
10709 &pushed_scope);
10710 }
10711 else if (scope)
10712 /* Enter the SCOPE. That way unqualified names appearing in the
10713 initializer will be looked up in SCOPE. */
10714 pushed_scope = push_scope (scope);
10715
10716 /* Perform deferred access control checks, now that we know in which
10717 SCOPE the declared entity resides. */
10718 if (!member_p && decl)
10719 {
10720 tree saved_current_function_decl = NULL_TREE;
10721
10722 /* If the entity being declared is a function, pretend that we
10723 are in its scope. If it is a `friend', it may have access to
10724 things that would not otherwise be accessible. */
10725 if (TREE_CODE (decl) == FUNCTION_DECL)
10726 {
10727 saved_current_function_decl = current_function_decl;
10728 current_function_decl = decl;
10729 }
10730
10731 /* Perform the access control checks for the declarator and the
10732 the decl-specifiers. */
10733 perform_deferred_access_checks ();
10734
10735 /* Restore the saved value. */
10736 if (TREE_CODE (decl) == FUNCTION_DECL)
10737 current_function_decl = saved_current_function_decl;
10738 }
10739
10740 /* Parse the initializer. */
10741 if (is_initialized)
10742 initializer = cp_parser_initializer (parser,
10743 &is_parenthesized_init,
10744 &is_non_constant_init);
10745 else
10746 {
10747 initializer = NULL_TREE;
10748 is_parenthesized_init = false;
10749 is_non_constant_init = true;
10750 }
10751
10752 /* The old parser allows attributes to appear after a parenthesized
10753 initializer. Mark Mitchell proposed removing this functionality
10754 on the GCC mailing lists on 2002-08-13. This parser accepts the
10755 attributes -- but ignores them. */
10756 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10757 if (cp_parser_attributes_opt (parser))
10758 warning ("attributes after parenthesized initializer ignored");
10759
10760 /* For an in-class declaration, use `grokfield' to create the
10761 declaration. */
10762 if (member_p)
10763 {
10764 if (pushed_scope)
10765 {
10766 pop_scope (pushed_scope);
10767 pushed_scope = false;
10768 }
10769 decl = grokfield (declarator, decl_specifiers,
10770 initializer, /*asmspec=*/NULL_TREE,
10771 /*attributes=*/NULL_TREE);
10772 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10773 cp_parser_save_default_args (parser, decl);
10774 }
10775
10776 /* Finish processing the declaration. But, skip friend
10777 declarations. */
10778 if (!friend_p && decl && decl != error_mark_node)
10779 {
10780 cp_finish_decl (decl,
10781 initializer,
10782 asm_specification,
10783 /* If the initializer is in parentheses, then this is
10784 a direct-initialization, which means that an
10785 `explicit' constructor is OK. Otherwise, an
10786 `explicit' constructor cannot be used. */
10787 ((is_parenthesized_init || !is_initialized)
10788 ? 0 : LOOKUP_ONLYCONVERTING));
10789 }
10790 if (!friend_p && pushed_scope)
10791 pop_scope (pushed_scope);
10792
10793 /* Remember whether or not variables were initialized by
10794 constant-expressions. */
10795 if (decl && TREE_CODE (decl) == VAR_DECL
10796 && is_initialized && !is_non_constant_init)
10797 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10798
10799 return decl;
10800 }
10801
10802 /* Parse a declarator.
10803
10804 declarator:
10805 direct-declarator
10806 ptr-operator declarator
10807
10808 abstract-declarator:
10809 ptr-operator abstract-declarator [opt]
10810 direct-abstract-declarator
10811
10812 GNU Extensions:
10813
10814 declarator:
10815 attributes [opt] direct-declarator
10816 attributes [opt] ptr-operator declarator
10817
10818 abstract-declarator:
10819 attributes [opt] ptr-operator abstract-declarator [opt]
10820 attributes [opt] direct-abstract-declarator
10821
10822 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10823 detect constructor, destructor or conversion operators. It is set
10824 to -1 if the declarator is a name, and +1 if it is a
10825 function. Otherwise it is set to zero. Usually you just want to
10826 test for >0, but internally the negative value is used.
10827
10828 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10829 a decl-specifier-seq unless it declares a constructor, destructor,
10830 or conversion. It might seem that we could check this condition in
10831 semantic analysis, rather than parsing, but that makes it difficult
10832 to handle something like `f()'. We want to notice that there are
10833 no decl-specifiers, and therefore realize that this is an
10834 expression, not a declaration.)
10835
10836 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10837 the declarator is a direct-declarator of the form "(...)".
10838
10839 MEMBER_P is true iff this declarator is a member-declarator. */
10840
10841 static cp_declarator *
10842 cp_parser_declarator (cp_parser* parser,
10843 cp_parser_declarator_kind dcl_kind,
10844 int* ctor_dtor_or_conv_p,
10845 bool* parenthesized_p,
10846 bool member_p)
10847 {
10848 cp_token *token;
10849 cp_declarator *declarator;
10850 enum tree_code code;
10851 cp_cv_quals cv_quals;
10852 tree class_type;
10853 tree attributes = NULL_TREE;
10854
10855 /* Assume this is not a constructor, destructor, or type-conversion
10856 operator. */
10857 if (ctor_dtor_or_conv_p)
10858 *ctor_dtor_or_conv_p = 0;
10859
10860 if (cp_parser_allow_gnu_extensions_p (parser))
10861 attributes = cp_parser_attributes_opt (parser);
10862
10863 /* Peek at the next token. */
10864 token = cp_lexer_peek_token (parser->lexer);
10865
10866 /* Check for the ptr-operator production. */
10867 cp_parser_parse_tentatively (parser);
10868 /* Parse the ptr-operator. */
10869 code = cp_parser_ptr_operator (parser,
10870 &class_type,
10871 &cv_quals);
10872 /* If that worked, then we have a ptr-operator. */
10873 if (cp_parser_parse_definitely (parser))
10874 {
10875 /* If a ptr-operator was found, then this declarator was not
10876 parenthesized. */
10877 if (parenthesized_p)
10878 *parenthesized_p = true;
10879 /* The dependent declarator is optional if we are parsing an
10880 abstract-declarator. */
10881 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10882 cp_parser_parse_tentatively (parser);
10883
10884 /* Parse the dependent declarator. */
10885 declarator = cp_parser_declarator (parser, dcl_kind,
10886 /*ctor_dtor_or_conv_p=*/NULL,
10887 /*parenthesized_p=*/NULL,
10888 /*member_p=*/false);
10889
10890 /* If we are parsing an abstract-declarator, we must handle the
10891 case where the dependent declarator is absent. */
10892 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10893 && !cp_parser_parse_definitely (parser))
10894 declarator = NULL;
10895
10896 /* Build the representation of the ptr-operator. */
10897 if (class_type)
10898 declarator = make_ptrmem_declarator (cv_quals,
10899 class_type,
10900 declarator);
10901 else if (code == INDIRECT_REF)
10902 declarator = make_pointer_declarator (cv_quals, declarator);
10903 else
10904 declarator = make_reference_declarator (cv_quals, declarator);
10905 }
10906 /* Everything else is a direct-declarator. */
10907 else
10908 {
10909 if (parenthesized_p)
10910 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10911 CPP_OPEN_PAREN);
10912 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10913 ctor_dtor_or_conv_p,
10914 member_p);
10915 }
10916
10917 if (attributes && declarator != cp_error_declarator)
10918 declarator->attributes = attributes;
10919
10920 return declarator;
10921 }
10922
10923 /* Parse a direct-declarator or direct-abstract-declarator.
10924
10925 direct-declarator:
10926 declarator-id
10927 direct-declarator ( parameter-declaration-clause )
10928 cv-qualifier-seq [opt]
10929 exception-specification [opt]
10930 direct-declarator [ constant-expression [opt] ]
10931 ( declarator )
10932
10933 direct-abstract-declarator:
10934 direct-abstract-declarator [opt]
10935 ( parameter-declaration-clause )
10936 cv-qualifier-seq [opt]
10937 exception-specification [opt]
10938 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10939 ( abstract-declarator )
10940
10941 Returns a representation of the declarator. DCL_KIND is
10942 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10943 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10944 we are parsing a direct-declarator. It is
10945 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10946 of ambiguity we prefer an abstract declarator, as per
10947 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10948 cp_parser_declarator. */
10949
10950 static cp_declarator *
10951 cp_parser_direct_declarator (cp_parser* parser,
10952 cp_parser_declarator_kind dcl_kind,
10953 int* ctor_dtor_or_conv_p,
10954 bool member_p)
10955 {
10956 cp_token *token;
10957 cp_declarator *declarator = NULL;
10958 tree scope = NULL_TREE;
10959 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10960 bool saved_in_declarator_p = parser->in_declarator_p;
10961 bool first = true;
10962 tree pushed_scope = NULL_TREE;
10963
10964 while (true)
10965 {
10966 /* Peek at the next token. */
10967 token = cp_lexer_peek_token (parser->lexer);
10968 if (token->type == CPP_OPEN_PAREN)
10969 {
10970 /* This is either a parameter-declaration-clause, or a
10971 parenthesized declarator. When we know we are parsing a
10972 named declarator, it must be a parenthesized declarator
10973 if FIRST is true. For instance, `(int)' is a
10974 parameter-declaration-clause, with an omitted
10975 direct-abstract-declarator. But `((*))', is a
10976 parenthesized abstract declarator. Finally, when T is a
10977 template parameter `(T)' is a
10978 parameter-declaration-clause, and not a parenthesized
10979 named declarator.
10980
10981 We first try and parse a parameter-declaration-clause,
10982 and then try a nested declarator (if FIRST is true).
10983
10984 It is not an error for it not to be a
10985 parameter-declaration-clause, even when FIRST is
10986 false. Consider,
10987
10988 int i (int);
10989 int i (3);
10990
10991 The first is the declaration of a function while the
10992 second is a the definition of a variable, including its
10993 initializer.
10994
10995 Having seen only the parenthesis, we cannot know which of
10996 these two alternatives should be selected. Even more
10997 complex are examples like:
10998
10999 int i (int (a));
11000 int i (int (3));
11001
11002 The former is a function-declaration; the latter is a
11003 variable initialization.
11004
11005 Thus again, we try a parameter-declaration-clause, and if
11006 that fails, we back out and return. */
11007
11008 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11009 {
11010 cp_parameter_declarator *params;
11011 unsigned saved_num_template_parameter_lists;
11012
11013 /* In a member-declarator, the only valid interpretation
11014 of a parenthesis is the start of a
11015 parameter-declaration-clause. (It is invalid to
11016 initialize a static data member with a parenthesized
11017 initializer; only the "=" form of initialization is
11018 permitted.) */
11019 if (!member_p)
11020 cp_parser_parse_tentatively (parser);
11021
11022 /* Consume the `('. */
11023 cp_lexer_consume_token (parser->lexer);
11024 if (first)
11025 {
11026 /* If this is going to be an abstract declarator, we're
11027 in a declarator and we can't have default args. */
11028 parser->default_arg_ok_p = false;
11029 parser->in_declarator_p = true;
11030 }
11031
11032 /* Inside the function parameter list, surrounding
11033 template-parameter-lists do not apply. */
11034 saved_num_template_parameter_lists
11035 = parser->num_template_parameter_lists;
11036 parser->num_template_parameter_lists = 0;
11037
11038 /* Parse the parameter-declaration-clause. */
11039 params = cp_parser_parameter_declaration_clause (parser);
11040
11041 parser->num_template_parameter_lists
11042 = saved_num_template_parameter_lists;
11043
11044 /* If all went well, parse the cv-qualifier-seq and the
11045 exception-specification. */
11046 if (member_p || cp_parser_parse_definitely (parser))
11047 {
11048 cp_cv_quals cv_quals;
11049 tree exception_specification;
11050
11051 if (ctor_dtor_or_conv_p)
11052 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11053 first = false;
11054 /* Consume the `)'. */
11055 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11056
11057 /* Parse the cv-qualifier-seq. */
11058 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11059 /* And the exception-specification. */
11060 exception_specification
11061 = cp_parser_exception_specification_opt (parser);
11062
11063 /* Create the function-declarator. */
11064 declarator = make_call_declarator (declarator,
11065 params,
11066 cv_quals,
11067 exception_specification);
11068 /* Any subsequent parameter lists are to do with
11069 return type, so are not those of the declared
11070 function. */
11071 parser->default_arg_ok_p = false;
11072
11073 /* Repeat the main loop. */
11074 continue;
11075 }
11076 }
11077
11078 /* If this is the first, we can try a parenthesized
11079 declarator. */
11080 if (first)
11081 {
11082 bool saved_in_type_id_in_expr_p;
11083
11084 parser->default_arg_ok_p = saved_default_arg_ok_p;
11085 parser->in_declarator_p = saved_in_declarator_p;
11086
11087 /* Consume the `('. */
11088 cp_lexer_consume_token (parser->lexer);
11089 /* Parse the nested declarator. */
11090 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11091 parser->in_type_id_in_expr_p = true;
11092 declarator
11093 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11094 /*parenthesized_p=*/NULL,
11095 member_p);
11096 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11097 first = false;
11098 /* Expect a `)'. */
11099 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11100 declarator = cp_error_declarator;
11101 if (declarator == cp_error_declarator)
11102 break;
11103
11104 goto handle_declarator;
11105 }
11106 /* Otherwise, we must be done. */
11107 else
11108 break;
11109 }
11110 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11111 && token->type == CPP_OPEN_SQUARE)
11112 {
11113 /* Parse an array-declarator. */
11114 tree bounds;
11115
11116 if (ctor_dtor_or_conv_p)
11117 *ctor_dtor_or_conv_p = 0;
11118
11119 first = false;
11120 parser->default_arg_ok_p = false;
11121 parser->in_declarator_p = true;
11122 /* Consume the `['. */
11123 cp_lexer_consume_token (parser->lexer);
11124 /* Peek at the next token. */
11125 token = cp_lexer_peek_token (parser->lexer);
11126 /* If the next token is `]', then there is no
11127 constant-expression. */
11128 if (token->type != CPP_CLOSE_SQUARE)
11129 {
11130 bool non_constant_p;
11131
11132 bounds
11133 = cp_parser_constant_expression (parser,
11134 /*allow_non_constant=*/true,
11135 &non_constant_p);
11136 if (!non_constant_p)
11137 bounds = fold_non_dependent_expr (bounds);
11138 /* Normally, the array bound must be an integral constant
11139 expression. However, as an extension, we allow VLAs
11140 in function scopes. */
11141 else if (!at_function_scope_p ())
11142 {
11143 error ("array bound is not an integer constant");
11144 bounds = error_mark_node;
11145 }
11146 }
11147 else
11148 bounds = NULL_TREE;
11149 /* Look for the closing `]'. */
11150 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11151 {
11152 declarator = cp_error_declarator;
11153 break;
11154 }
11155
11156 declarator = make_array_declarator (declarator, bounds);
11157 }
11158 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11159 {
11160 tree qualifying_scope;
11161 tree unqualified_name;
11162
11163 /* Parse a declarator-id */
11164 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11165 cp_parser_parse_tentatively (parser);
11166 unqualified_name = cp_parser_declarator_id (parser);
11167 qualifying_scope = parser->scope;
11168 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11169 {
11170 if (!cp_parser_parse_definitely (parser))
11171 unqualified_name = error_mark_node;
11172 else if (qualifying_scope
11173 || (TREE_CODE (unqualified_name)
11174 != IDENTIFIER_NODE))
11175 {
11176 cp_parser_error (parser, "expected unqualified-id");
11177 unqualified_name = error_mark_node;
11178 }
11179 }
11180
11181 if (unqualified_name == error_mark_node)
11182 {
11183 declarator = cp_error_declarator;
11184 break;
11185 }
11186
11187 if (qualifying_scope && at_namespace_scope_p ()
11188 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11189 {
11190 /* In the declaration of a member of a template class
11191 outside of the class itself, the SCOPE will sometimes
11192 be a TYPENAME_TYPE. For example, given:
11193
11194 template <typename T>
11195 int S<T>::R::i = 3;
11196
11197 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11198 this context, we must resolve S<T>::R to an ordinary
11199 type, rather than a typename type.
11200
11201 The reason we normally avoid resolving TYPENAME_TYPEs
11202 is that a specialization of `S' might render
11203 `S<T>::R' not a type. However, if `S' is
11204 specialized, then this `i' will not be used, so there
11205 is no harm in resolving the types here. */
11206 tree type;
11207
11208 /* Resolve the TYPENAME_TYPE. */
11209 type = resolve_typename_type (qualifying_scope,
11210 /*only_current_p=*/false);
11211 /* If that failed, the declarator is invalid. */
11212 if (type == error_mark_node)
11213 error ("%<%T::%D%> is not a type",
11214 TYPE_CONTEXT (qualifying_scope),
11215 TYPE_IDENTIFIER (qualifying_scope));
11216 qualifying_scope = type;
11217 }
11218
11219 declarator = make_id_declarator (qualifying_scope,
11220 unqualified_name);
11221 declarator->id_loc = token->location;
11222 if (unqualified_name)
11223 {
11224 tree class_type;
11225
11226 if (qualifying_scope
11227 && CLASS_TYPE_P (qualifying_scope))
11228 class_type = qualifying_scope;
11229 else
11230 class_type = current_class_type;
11231
11232 if (class_type)
11233 {
11234 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11235 declarator->u.id.sfk = sfk_destructor;
11236 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11237 declarator->u.id.sfk = sfk_conversion;
11238 else if (/* There's no way to declare a constructor
11239 for an anonymous type, even if the type
11240 got a name for linkage purposes. */
11241 !TYPE_WAS_ANONYMOUS (class_type)
11242 && (constructor_name_p (unqualified_name,
11243 class_type)
11244 || (TREE_CODE (unqualified_name) == TYPE_DECL
11245 && (same_type_p
11246 (TREE_TYPE (unqualified_name),
11247 class_type)))))
11248 declarator->u.id.sfk = sfk_constructor;
11249
11250 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11251 *ctor_dtor_or_conv_p = -1;
11252 if (qualifying_scope
11253 && TREE_CODE (unqualified_name) == TYPE_DECL
11254 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11255 {
11256 error ("invalid use of constructor as a template");
11257 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11258 "the constructor in a qualified name",
11259 class_type,
11260 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11261 class_type, class_type);
11262 }
11263 }
11264 }
11265
11266 handle_declarator:;
11267 scope = get_scope_of_declarator (declarator);
11268 if (scope)
11269 /* Any names that appear after the declarator-id for a
11270 member are looked up in the containing scope. */
11271 pushed_scope = push_scope (scope);
11272 parser->in_declarator_p = true;
11273 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11274 || (declarator && declarator->kind == cdk_id))
11275 /* Default args are only allowed on function
11276 declarations. */
11277 parser->default_arg_ok_p = saved_default_arg_ok_p;
11278 else
11279 parser->default_arg_ok_p = false;
11280
11281 first = false;
11282 }
11283 /* We're done. */
11284 else
11285 break;
11286 }
11287
11288 /* For an abstract declarator, we might wind up with nothing at this
11289 point. That's an error; the declarator is not optional. */
11290 if (!declarator)
11291 cp_parser_error (parser, "expected declarator");
11292
11293 /* If we entered a scope, we must exit it now. */
11294 if (pushed_scope)
11295 pop_scope (pushed_scope);
11296
11297 parser->default_arg_ok_p = saved_default_arg_ok_p;
11298 parser->in_declarator_p = saved_in_declarator_p;
11299
11300 return declarator;
11301 }
11302
11303 /* Parse a ptr-operator.
11304
11305 ptr-operator:
11306 * cv-qualifier-seq [opt]
11307 &
11308 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11309
11310 GNU Extension:
11311
11312 ptr-operator:
11313 & cv-qualifier-seq [opt]
11314
11315 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11316 Returns ADDR_EXPR if a reference was used. In the case of a
11317 pointer-to-member, *TYPE is filled in with the TYPE containing the
11318 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11319 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11320 ERROR_MARK if an error occurred. */
11321
11322 static enum tree_code
11323 cp_parser_ptr_operator (cp_parser* parser,
11324 tree* type,
11325 cp_cv_quals *cv_quals)
11326 {
11327 enum tree_code code = ERROR_MARK;
11328 cp_token *token;
11329
11330 /* Assume that it's not a pointer-to-member. */
11331 *type = NULL_TREE;
11332 /* And that there are no cv-qualifiers. */
11333 *cv_quals = TYPE_UNQUALIFIED;
11334
11335 /* Peek at the next token. */
11336 token = cp_lexer_peek_token (parser->lexer);
11337 /* If it's a `*' or `&' we have a pointer or reference. */
11338 if (token->type == CPP_MULT || token->type == CPP_AND)
11339 {
11340 /* Remember which ptr-operator we were processing. */
11341 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11342
11343 /* Consume the `*' or `&'. */
11344 cp_lexer_consume_token (parser->lexer);
11345
11346 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11347 `&', if we are allowing GNU extensions. (The only qualifier
11348 that can legally appear after `&' is `restrict', but that is
11349 enforced during semantic analysis. */
11350 if (code == INDIRECT_REF
11351 || cp_parser_allow_gnu_extensions_p (parser))
11352 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11353 }
11354 else
11355 {
11356 /* Try the pointer-to-member case. */
11357 cp_parser_parse_tentatively (parser);
11358 /* Look for the optional `::' operator. */
11359 cp_parser_global_scope_opt (parser,
11360 /*current_scope_valid_p=*/false);
11361 /* Look for the nested-name specifier. */
11362 cp_parser_nested_name_specifier (parser,
11363 /*typename_keyword_p=*/false,
11364 /*check_dependency_p=*/true,
11365 /*type_p=*/false,
11366 /*is_declaration=*/false);
11367 /* If we found it, and the next token is a `*', then we are
11368 indeed looking at a pointer-to-member operator. */
11369 if (!cp_parser_error_occurred (parser)
11370 && cp_parser_require (parser, CPP_MULT, "`*'"))
11371 {
11372 /* The type of which the member is a member is given by the
11373 current SCOPE. */
11374 *type = parser->scope;
11375 /* The next name will not be qualified. */
11376 parser->scope = NULL_TREE;
11377 parser->qualifying_scope = NULL_TREE;
11378 parser->object_scope = NULL_TREE;
11379 /* Indicate that the `*' operator was used. */
11380 code = INDIRECT_REF;
11381 /* Look for the optional cv-qualifier-seq. */
11382 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11383 }
11384 /* If that didn't work we don't have a ptr-operator. */
11385 if (!cp_parser_parse_definitely (parser))
11386 cp_parser_error (parser, "expected ptr-operator");
11387 }
11388
11389 return code;
11390 }
11391
11392 /* Parse an (optional) cv-qualifier-seq.
11393
11394 cv-qualifier-seq:
11395 cv-qualifier cv-qualifier-seq [opt]
11396
11397 cv-qualifier:
11398 const
11399 volatile
11400
11401 GNU Extension:
11402
11403 cv-qualifier:
11404 __restrict__
11405
11406 Returns a bitmask representing the cv-qualifiers. */
11407
11408 static cp_cv_quals
11409 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11410 {
11411 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11412
11413 while (true)
11414 {
11415 cp_token *token;
11416 cp_cv_quals cv_qualifier;
11417
11418 /* Peek at the next token. */
11419 token = cp_lexer_peek_token (parser->lexer);
11420 /* See if it's a cv-qualifier. */
11421 switch (token->keyword)
11422 {
11423 case RID_CONST:
11424 cv_qualifier = TYPE_QUAL_CONST;
11425 break;
11426
11427 case RID_VOLATILE:
11428 cv_qualifier = TYPE_QUAL_VOLATILE;
11429 break;
11430
11431 case RID_RESTRICT:
11432 cv_qualifier = TYPE_QUAL_RESTRICT;
11433 break;
11434
11435 default:
11436 cv_qualifier = TYPE_UNQUALIFIED;
11437 break;
11438 }
11439
11440 if (!cv_qualifier)
11441 break;
11442
11443 if (cv_quals & cv_qualifier)
11444 {
11445 error ("duplicate cv-qualifier");
11446 cp_lexer_purge_token (parser->lexer);
11447 }
11448 else
11449 {
11450 cp_lexer_consume_token (parser->lexer);
11451 cv_quals |= cv_qualifier;
11452 }
11453 }
11454
11455 return cv_quals;
11456 }
11457
11458 /* Parse a declarator-id.
11459
11460 declarator-id:
11461 id-expression
11462 :: [opt] nested-name-specifier [opt] type-name
11463
11464 In the `id-expression' case, the value returned is as for
11465 cp_parser_id_expression if the id-expression was an unqualified-id.
11466 If the id-expression was a qualified-id, then a SCOPE_REF is
11467 returned. The first operand is the scope (either a NAMESPACE_DECL
11468 or TREE_TYPE), but the second is still just a representation of an
11469 unqualified-id. */
11470
11471 static tree
11472 cp_parser_declarator_id (cp_parser* parser)
11473 {
11474 /* The expression must be an id-expression. Assume that qualified
11475 names are the names of types so that:
11476
11477 template <class T>
11478 int S<T>::R::i = 3;
11479
11480 will work; we must treat `S<T>::R' as the name of a type.
11481 Similarly, assume that qualified names are templates, where
11482 required, so that:
11483
11484 template <class T>
11485 int S<T>::R<T>::i = 3;
11486
11487 will work, too. */
11488 return cp_parser_id_expression (parser,
11489 /*template_keyword_p=*/false,
11490 /*check_dependency_p=*/false,
11491 /*template_p=*/NULL,
11492 /*declarator_p=*/true);
11493 }
11494
11495 /* Parse a type-id.
11496
11497 type-id:
11498 type-specifier-seq abstract-declarator [opt]
11499
11500 Returns the TYPE specified. */
11501
11502 static tree
11503 cp_parser_type_id (cp_parser* parser)
11504 {
11505 cp_decl_specifier_seq type_specifier_seq;
11506 cp_declarator *abstract_declarator;
11507
11508 /* Parse the type-specifier-seq. */
11509 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11510 &type_specifier_seq);
11511 if (type_specifier_seq.type == error_mark_node)
11512 return error_mark_node;
11513
11514 /* There might or might not be an abstract declarator. */
11515 cp_parser_parse_tentatively (parser);
11516 /* Look for the declarator. */
11517 abstract_declarator
11518 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11519 /*parenthesized_p=*/NULL,
11520 /*member_p=*/false);
11521 /* Check to see if there really was a declarator. */
11522 if (!cp_parser_parse_definitely (parser))
11523 abstract_declarator = NULL;
11524
11525 return groktypename (&type_specifier_seq, abstract_declarator);
11526 }
11527
11528 /* Parse a type-specifier-seq.
11529
11530 type-specifier-seq:
11531 type-specifier type-specifier-seq [opt]
11532
11533 GNU extension:
11534
11535 type-specifier-seq:
11536 attributes type-specifier-seq [opt]
11537
11538 If IS_CONDITION is true, we are at the start of a "condition",
11539 e.g., we've just seen "if (".
11540
11541 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11542
11543 static void
11544 cp_parser_type_specifier_seq (cp_parser* parser,
11545 bool is_condition,
11546 cp_decl_specifier_seq *type_specifier_seq)
11547 {
11548 bool seen_type_specifier = false;
11549 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11550
11551 /* Clear the TYPE_SPECIFIER_SEQ. */
11552 clear_decl_specs (type_specifier_seq);
11553
11554 /* Parse the type-specifiers and attributes. */
11555 while (true)
11556 {
11557 tree type_specifier;
11558 bool is_cv_qualifier;
11559
11560 /* Check for attributes first. */
11561 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11562 {
11563 type_specifier_seq->attributes =
11564 chainon (type_specifier_seq->attributes,
11565 cp_parser_attributes_opt (parser));
11566 continue;
11567 }
11568
11569 /* Look for the type-specifier. */
11570 type_specifier = cp_parser_type_specifier (parser,
11571 flags,
11572 type_specifier_seq,
11573 /*is_declaration=*/false,
11574 NULL,
11575 &is_cv_qualifier);
11576 if (!type_specifier)
11577 {
11578 /* If the first type-specifier could not be found, this is not a
11579 type-specifier-seq at all. */
11580 if (!seen_type_specifier)
11581 {
11582 cp_parser_error (parser, "expected type-specifier");
11583 type_specifier_seq->type = error_mark_node;
11584 return;
11585 }
11586 /* If subsequent type-specifiers could not be found, the
11587 type-specifier-seq is complete. */
11588 break;
11589 }
11590
11591 seen_type_specifier = true;
11592 /* The standard says that a condition can be:
11593
11594 type-specifier-seq declarator = assignment-expression
11595
11596 However, given:
11597
11598 struct S {};
11599 if (int S = ...)
11600
11601 we should treat the "S" as a declarator, not as a
11602 type-specifier. The standard doesn't say that explicitly for
11603 type-specifier-seq, but it does say that for
11604 decl-specifier-seq in an ordinary declaration. Perhaps it
11605 would be clearer just to allow a decl-specifier-seq here, and
11606 then add a semantic restriction that if any decl-specifiers
11607 that are not type-specifiers appear, the program is invalid. */
11608 if (is_condition && !is_cv_qualifier)
11609 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11610 }
11611
11612 return;
11613 }
11614
11615 /* Parse a parameter-declaration-clause.
11616
11617 parameter-declaration-clause:
11618 parameter-declaration-list [opt] ... [opt]
11619 parameter-declaration-list , ...
11620
11621 Returns a representation for the parameter declarations. A return
11622 value of NULL indicates a parameter-declaration-clause consisting
11623 only of an ellipsis. */
11624
11625 static cp_parameter_declarator *
11626 cp_parser_parameter_declaration_clause (cp_parser* parser)
11627 {
11628 cp_parameter_declarator *parameters;
11629 cp_token *token;
11630 bool ellipsis_p;
11631 bool is_error;
11632
11633 /* Peek at the next token. */
11634 token = cp_lexer_peek_token (parser->lexer);
11635 /* Check for trivial parameter-declaration-clauses. */
11636 if (token->type == CPP_ELLIPSIS)
11637 {
11638 /* Consume the `...' token. */
11639 cp_lexer_consume_token (parser->lexer);
11640 return NULL;
11641 }
11642 else if (token->type == CPP_CLOSE_PAREN)
11643 /* There are no parameters. */
11644 {
11645 #ifndef NO_IMPLICIT_EXTERN_C
11646 if (in_system_header && current_class_type == NULL
11647 && current_lang_name == lang_name_c)
11648 return NULL;
11649 else
11650 #endif
11651 return no_parameters;
11652 }
11653 /* Check for `(void)', too, which is a special case. */
11654 else if (token->keyword == RID_VOID
11655 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11656 == CPP_CLOSE_PAREN))
11657 {
11658 /* Consume the `void' token. */
11659 cp_lexer_consume_token (parser->lexer);
11660 /* There are no parameters. */
11661 return no_parameters;
11662 }
11663
11664 /* Parse the parameter-declaration-list. */
11665 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11666 /* If a parse error occurred while parsing the
11667 parameter-declaration-list, then the entire
11668 parameter-declaration-clause is erroneous. */
11669 if (is_error)
11670 return NULL;
11671
11672 /* Peek at the next token. */
11673 token = cp_lexer_peek_token (parser->lexer);
11674 /* If it's a `,', the clause should terminate with an ellipsis. */
11675 if (token->type == CPP_COMMA)
11676 {
11677 /* Consume the `,'. */
11678 cp_lexer_consume_token (parser->lexer);
11679 /* Expect an ellipsis. */
11680 ellipsis_p
11681 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11682 }
11683 /* It might also be `...' if the optional trailing `,' was
11684 omitted. */
11685 else if (token->type == CPP_ELLIPSIS)
11686 {
11687 /* Consume the `...' token. */
11688 cp_lexer_consume_token (parser->lexer);
11689 /* And remember that we saw it. */
11690 ellipsis_p = true;
11691 }
11692 else
11693 ellipsis_p = false;
11694
11695 /* Finish the parameter list. */
11696 if (parameters && ellipsis_p)
11697 parameters->ellipsis_p = true;
11698
11699 return parameters;
11700 }
11701
11702 /* Parse a parameter-declaration-list.
11703
11704 parameter-declaration-list:
11705 parameter-declaration
11706 parameter-declaration-list , parameter-declaration
11707
11708 Returns a representation of the parameter-declaration-list, as for
11709 cp_parser_parameter_declaration_clause. However, the
11710 `void_list_node' is never appended to the list. Upon return,
11711 *IS_ERROR will be true iff an error occurred. */
11712
11713 static cp_parameter_declarator *
11714 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11715 {
11716 cp_parameter_declarator *parameters = NULL;
11717 cp_parameter_declarator **tail = &parameters;
11718
11719 /* Assume all will go well. */
11720 *is_error = false;
11721
11722 /* Look for more parameters. */
11723 while (true)
11724 {
11725 cp_parameter_declarator *parameter;
11726 bool parenthesized_p;
11727 /* Parse the parameter. */
11728 parameter
11729 = cp_parser_parameter_declaration (parser,
11730 /*template_parm_p=*/false,
11731 &parenthesized_p);
11732
11733 /* If a parse error occurred parsing the parameter declaration,
11734 then the entire parameter-declaration-list is erroneous. */
11735 if (!parameter)
11736 {
11737 *is_error = true;
11738 parameters = NULL;
11739 break;
11740 }
11741 /* Add the new parameter to the list. */
11742 *tail = parameter;
11743 tail = &parameter->next;
11744
11745 /* Peek at the next token. */
11746 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11747 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11748 /* The parameter-declaration-list is complete. */
11749 break;
11750 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11751 {
11752 cp_token *token;
11753
11754 /* Peek at the next token. */
11755 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11756 /* If it's an ellipsis, then the list is complete. */
11757 if (token->type == CPP_ELLIPSIS)
11758 break;
11759 /* Otherwise, there must be more parameters. Consume the
11760 `,'. */
11761 cp_lexer_consume_token (parser->lexer);
11762 /* When parsing something like:
11763
11764 int i(float f, double d)
11765
11766 we can tell after seeing the declaration for "f" that we
11767 are not looking at an initialization of a variable "i",
11768 but rather at the declaration of a function "i".
11769
11770 Due to the fact that the parsing of template arguments
11771 (as specified to a template-id) requires backtracking we
11772 cannot use this technique when inside a template argument
11773 list. */
11774 if (!parser->in_template_argument_list_p
11775 && !parser->in_type_id_in_expr_p
11776 && cp_parser_uncommitted_to_tentative_parse_p (parser)
11777 /* However, a parameter-declaration of the form
11778 "foat(f)" (which is a valid declaration of a
11779 parameter "f") can also be interpreted as an
11780 expression (the conversion of "f" to "float"). */
11781 && !parenthesized_p)
11782 cp_parser_commit_to_tentative_parse (parser);
11783 }
11784 else
11785 {
11786 cp_parser_error (parser, "expected %<,%> or %<...%>");
11787 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11788 cp_parser_skip_to_closing_parenthesis (parser,
11789 /*recovering=*/true,
11790 /*or_comma=*/false,
11791 /*consume_paren=*/false);
11792 break;
11793 }
11794 }
11795
11796 return parameters;
11797 }
11798
11799 /* Parse a parameter declaration.
11800
11801 parameter-declaration:
11802 decl-specifier-seq declarator
11803 decl-specifier-seq declarator = assignment-expression
11804 decl-specifier-seq abstract-declarator [opt]
11805 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11806
11807 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11808 declares a template parameter. (In that case, a non-nested `>'
11809 token encountered during the parsing of the assignment-expression
11810 is not interpreted as a greater-than operator.)
11811
11812 Returns a representation of the parameter, or NULL if an error
11813 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11814 true iff the declarator is of the form "(p)". */
11815
11816 static cp_parameter_declarator *
11817 cp_parser_parameter_declaration (cp_parser *parser,
11818 bool template_parm_p,
11819 bool *parenthesized_p)
11820 {
11821 int declares_class_or_enum;
11822 bool greater_than_is_operator_p;
11823 cp_decl_specifier_seq decl_specifiers;
11824 cp_declarator *declarator;
11825 tree default_argument;
11826 cp_token *token;
11827 const char *saved_message;
11828
11829 /* In a template parameter, `>' is not an operator.
11830
11831 [temp.param]
11832
11833 When parsing a default template-argument for a non-type
11834 template-parameter, the first non-nested `>' is taken as the end
11835 of the template parameter-list rather than a greater-than
11836 operator. */
11837 greater_than_is_operator_p = !template_parm_p;
11838
11839 /* Type definitions may not appear in parameter types. */
11840 saved_message = parser->type_definition_forbidden_message;
11841 parser->type_definition_forbidden_message
11842 = "types may not be defined in parameter types";
11843
11844 /* Parse the declaration-specifiers. */
11845 cp_parser_decl_specifier_seq (parser,
11846 CP_PARSER_FLAGS_NONE,
11847 &decl_specifiers,
11848 &declares_class_or_enum);
11849 /* If an error occurred, there's no reason to attempt to parse the
11850 rest of the declaration. */
11851 if (cp_parser_error_occurred (parser))
11852 {
11853 parser->type_definition_forbidden_message = saved_message;
11854 return NULL;
11855 }
11856
11857 /* Peek at the next token. */
11858 token = cp_lexer_peek_token (parser->lexer);
11859 /* If the next token is a `)', `,', `=', `>', or `...', then there
11860 is no declarator. */
11861 if (token->type == CPP_CLOSE_PAREN
11862 || token->type == CPP_COMMA
11863 || token->type == CPP_EQ
11864 || token->type == CPP_ELLIPSIS
11865 || token->type == CPP_GREATER)
11866 {
11867 declarator = NULL;
11868 if (parenthesized_p)
11869 *parenthesized_p = false;
11870 }
11871 /* Otherwise, there should be a declarator. */
11872 else
11873 {
11874 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11875 parser->default_arg_ok_p = false;
11876
11877 /* After seeing a decl-specifier-seq, if the next token is not a
11878 "(", there is no possibility that the code is a valid
11879 expression. Therefore, if parsing tentatively, we commit at
11880 this point. */
11881 if (!parser->in_template_argument_list_p
11882 /* In an expression context, having seen:
11883
11884 (int((char ...
11885
11886 we cannot be sure whether we are looking at a
11887 function-type (taking a "char" as a parameter) or a cast
11888 of some object of type "char" to "int". */
11889 && !parser->in_type_id_in_expr_p
11890 && cp_parser_uncommitted_to_tentative_parse_p (parser)
11891 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11892 cp_parser_commit_to_tentative_parse (parser);
11893 /* Parse the declarator. */
11894 declarator = cp_parser_declarator (parser,
11895 CP_PARSER_DECLARATOR_EITHER,
11896 /*ctor_dtor_or_conv_p=*/NULL,
11897 parenthesized_p,
11898 /*member_p=*/false);
11899 parser->default_arg_ok_p = saved_default_arg_ok_p;
11900 /* After the declarator, allow more attributes. */
11901 decl_specifiers.attributes
11902 = chainon (decl_specifiers.attributes,
11903 cp_parser_attributes_opt (parser));
11904 }
11905
11906 /* The restriction on defining new types applies only to the type
11907 of the parameter, not to the default argument. */
11908 parser->type_definition_forbidden_message = saved_message;
11909
11910 /* If the next token is `=', then process a default argument. */
11911 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11912 {
11913 bool saved_greater_than_is_operator_p;
11914 /* Consume the `='. */
11915 cp_lexer_consume_token (parser->lexer);
11916
11917 /* If we are defining a class, then the tokens that make up the
11918 default argument must be saved and processed later. */
11919 if (!template_parm_p && at_class_scope_p ()
11920 && TYPE_BEING_DEFINED (current_class_type))
11921 {
11922 unsigned depth = 0;
11923 cp_token *first_token;
11924 cp_token *token;
11925
11926 /* Add tokens until we have processed the entire default
11927 argument. We add the range [first_token, token). */
11928 first_token = cp_lexer_peek_token (parser->lexer);
11929 while (true)
11930 {
11931 bool done = false;
11932
11933 /* Peek at the next token. */
11934 token = cp_lexer_peek_token (parser->lexer);
11935 /* What we do depends on what token we have. */
11936 switch (token->type)
11937 {
11938 /* In valid code, a default argument must be
11939 immediately followed by a `,' `)', or `...'. */
11940 case CPP_COMMA:
11941 case CPP_CLOSE_PAREN:
11942 case CPP_ELLIPSIS:
11943 /* If we run into a non-nested `;', `}', or `]',
11944 then the code is invalid -- but the default
11945 argument is certainly over. */
11946 case CPP_SEMICOLON:
11947 case CPP_CLOSE_BRACE:
11948 case CPP_CLOSE_SQUARE:
11949 if (depth == 0)
11950 done = true;
11951 /* Update DEPTH, if necessary. */
11952 else if (token->type == CPP_CLOSE_PAREN
11953 || token->type == CPP_CLOSE_BRACE
11954 || token->type == CPP_CLOSE_SQUARE)
11955 --depth;
11956 break;
11957
11958 case CPP_OPEN_PAREN:
11959 case CPP_OPEN_SQUARE:
11960 case CPP_OPEN_BRACE:
11961 ++depth;
11962 break;
11963
11964 case CPP_GREATER:
11965 /* If we see a non-nested `>', and `>' is not an
11966 operator, then it marks the end of the default
11967 argument. */
11968 if (!depth && !greater_than_is_operator_p)
11969 done = true;
11970 break;
11971
11972 /* If we run out of tokens, issue an error message. */
11973 case CPP_EOF:
11974 error ("file ends in default argument");
11975 done = true;
11976 break;
11977
11978 case CPP_NAME:
11979 case CPP_SCOPE:
11980 /* In these cases, we should look for template-ids.
11981 For example, if the default argument is
11982 `X<int, double>()', we need to do name lookup to
11983 figure out whether or not `X' is a template; if
11984 so, the `,' does not end the default argument.
11985
11986 That is not yet done. */
11987 break;
11988
11989 default:
11990 break;
11991 }
11992
11993 /* If we've reached the end, stop. */
11994 if (done)
11995 break;
11996
11997 /* Add the token to the token block. */
11998 token = cp_lexer_consume_token (parser->lexer);
11999 }
12000
12001 /* Create a DEFAULT_ARG to represented the unparsed default
12002 argument. */
12003 default_argument = make_node (DEFAULT_ARG);
12004 DEFARG_TOKENS (default_argument)
12005 = cp_token_cache_new (first_token, token);
12006 }
12007 /* Outside of a class definition, we can just parse the
12008 assignment-expression. */
12009 else
12010 {
12011 bool saved_local_variables_forbidden_p;
12012
12013 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12014 set correctly. */
12015 saved_greater_than_is_operator_p
12016 = parser->greater_than_is_operator_p;
12017 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12018 /* Local variable names (and the `this' keyword) may not
12019 appear in a default argument. */
12020 saved_local_variables_forbidden_p
12021 = parser->local_variables_forbidden_p;
12022 parser->local_variables_forbidden_p = true;
12023 /* Parse the assignment-expression. */
12024 default_argument
12025 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12026 /* Restore saved state. */
12027 parser->greater_than_is_operator_p
12028 = saved_greater_than_is_operator_p;
12029 parser->local_variables_forbidden_p
12030 = saved_local_variables_forbidden_p;
12031 }
12032 if (!parser->default_arg_ok_p)
12033 {
12034 if (!flag_pedantic_errors)
12035 warning ("deprecated use of default argument for parameter of non-function");
12036 else
12037 {
12038 error ("default arguments are only permitted for function parameters");
12039 default_argument = NULL_TREE;
12040 }
12041 }
12042 }
12043 else
12044 default_argument = NULL_TREE;
12045
12046 return make_parameter_declarator (&decl_specifiers,
12047 declarator,
12048 default_argument);
12049 }
12050
12051 /* Parse a function-body.
12052
12053 function-body:
12054 compound_statement */
12055
12056 static void
12057 cp_parser_function_body (cp_parser *parser)
12058 {
12059 cp_parser_compound_statement (parser, NULL, false);
12060 }
12061
12062 /* Parse a ctor-initializer-opt followed by a function-body. Return
12063 true if a ctor-initializer was present. */
12064
12065 static bool
12066 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12067 {
12068 tree body;
12069 bool ctor_initializer_p;
12070
12071 /* Begin the function body. */
12072 body = begin_function_body ();
12073 /* Parse the optional ctor-initializer. */
12074 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12075 /* Parse the function-body. */
12076 cp_parser_function_body (parser);
12077 /* Finish the function body. */
12078 finish_function_body (body);
12079
12080 return ctor_initializer_p;
12081 }
12082
12083 /* Parse an initializer.
12084
12085 initializer:
12086 = initializer-clause
12087 ( expression-list )
12088
12089 Returns a expression representing the initializer. If no
12090 initializer is present, NULL_TREE is returned.
12091
12092 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12093 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12094 set to FALSE if there is no initializer present. If there is an
12095 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12096 is set to true; otherwise it is set to false. */
12097
12098 static tree
12099 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12100 bool* non_constant_p)
12101 {
12102 cp_token *token;
12103 tree init;
12104
12105 /* Peek at the next token. */
12106 token = cp_lexer_peek_token (parser->lexer);
12107
12108 /* Let our caller know whether or not this initializer was
12109 parenthesized. */
12110 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12111 /* Assume that the initializer is constant. */
12112 *non_constant_p = false;
12113
12114 if (token->type == CPP_EQ)
12115 {
12116 /* Consume the `='. */
12117 cp_lexer_consume_token (parser->lexer);
12118 /* Parse the initializer-clause. */
12119 init = cp_parser_initializer_clause (parser, non_constant_p);
12120 }
12121 else if (token->type == CPP_OPEN_PAREN)
12122 init = cp_parser_parenthesized_expression_list (parser, false,
12123 /*cast_p=*/false,
12124 non_constant_p);
12125 else
12126 {
12127 /* Anything else is an error. */
12128 cp_parser_error (parser, "expected initializer");
12129 init = error_mark_node;
12130 }
12131
12132 return init;
12133 }
12134
12135 /* Parse an initializer-clause.
12136
12137 initializer-clause:
12138 assignment-expression
12139 { initializer-list , [opt] }
12140 { }
12141
12142 Returns an expression representing the initializer.
12143
12144 If the `assignment-expression' production is used the value
12145 returned is simply a representation for the expression.
12146
12147 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12148 the elements of the initializer-list (or NULL_TREE, if the last
12149 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12150 NULL_TREE. There is no way to detect whether or not the optional
12151 trailing `,' was provided. NON_CONSTANT_P is as for
12152 cp_parser_initializer. */
12153
12154 static tree
12155 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12156 {
12157 tree initializer;
12158
12159 /* Assume the expression is constant. */
12160 *non_constant_p = false;
12161
12162 /* If it is not a `{', then we are looking at an
12163 assignment-expression. */
12164 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12165 {
12166 initializer
12167 = cp_parser_constant_expression (parser,
12168 /*allow_non_constant_p=*/true,
12169 non_constant_p);
12170 if (!*non_constant_p)
12171 initializer = fold_non_dependent_expr (initializer);
12172 }
12173 else
12174 {
12175 /* Consume the `{' token. */
12176 cp_lexer_consume_token (parser->lexer);
12177 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12178 initializer = make_node (CONSTRUCTOR);
12179 /* If it's not a `}', then there is a non-trivial initializer. */
12180 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12181 {
12182 /* Parse the initializer list. */
12183 CONSTRUCTOR_ELTS (initializer)
12184 = cp_parser_initializer_list (parser, non_constant_p);
12185 /* A trailing `,' token is allowed. */
12186 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12187 cp_lexer_consume_token (parser->lexer);
12188 }
12189 /* Now, there should be a trailing `}'. */
12190 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12191 }
12192
12193 return initializer;
12194 }
12195
12196 /* Parse an initializer-list.
12197
12198 initializer-list:
12199 initializer-clause
12200 initializer-list , initializer-clause
12201
12202 GNU Extension:
12203
12204 initializer-list:
12205 identifier : initializer-clause
12206 initializer-list, identifier : initializer-clause
12207
12208 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12209 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12210 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12211 as for cp_parser_initializer. */
12212
12213 static tree
12214 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12215 {
12216 tree initializers = NULL_TREE;
12217
12218 /* Assume all of the expressions are constant. */
12219 *non_constant_p = false;
12220
12221 /* Parse the rest of the list. */
12222 while (true)
12223 {
12224 cp_token *token;
12225 tree identifier;
12226 tree initializer;
12227 bool clause_non_constant_p;
12228
12229 /* If the next token is an identifier and the following one is a
12230 colon, we are looking at the GNU designated-initializer
12231 syntax. */
12232 if (cp_parser_allow_gnu_extensions_p (parser)
12233 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12234 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12235 {
12236 /* Consume the identifier. */
12237 identifier = cp_lexer_consume_token (parser->lexer)->value;
12238 /* Consume the `:'. */
12239 cp_lexer_consume_token (parser->lexer);
12240 }
12241 else
12242 identifier = NULL_TREE;
12243
12244 /* Parse the initializer. */
12245 initializer = cp_parser_initializer_clause (parser,
12246 &clause_non_constant_p);
12247 /* If any clause is non-constant, so is the entire initializer. */
12248 if (clause_non_constant_p)
12249 *non_constant_p = true;
12250 /* Add it to the list. */
12251 initializers = tree_cons (identifier, initializer, initializers);
12252
12253 /* If the next token is not a comma, we have reached the end of
12254 the list. */
12255 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12256 break;
12257
12258 /* Peek at the next token. */
12259 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12260 /* If the next token is a `}', then we're still done. An
12261 initializer-clause can have a trailing `,' after the
12262 initializer-list and before the closing `}'. */
12263 if (token->type == CPP_CLOSE_BRACE)
12264 break;
12265
12266 /* Consume the `,' token. */
12267 cp_lexer_consume_token (parser->lexer);
12268 }
12269
12270 /* The initializers were built up in reverse order, so we need to
12271 reverse them now. */
12272 return nreverse (initializers);
12273 }
12274
12275 /* Classes [gram.class] */
12276
12277 /* Parse a class-name.
12278
12279 class-name:
12280 identifier
12281 template-id
12282
12283 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12284 to indicate that names looked up in dependent types should be
12285 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12286 keyword has been used to indicate that the name that appears next
12287 is a template. TAG_TYPE indicates the explicit tag given before
12288 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12289 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12290 is the class being defined in a class-head.
12291
12292 Returns the TYPE_DECL representing the class. */
12293
12294 static tree
12295 cp_parser_class_name (cp_parser *parser,
12296 bool typename_keyword_p,
12297 bool template_keyword_p,
12298 enum tag_types tag_type,
12299 bool check_dependency_p,
12300 bool class_head_p,
12301 bool is_declaration)
12302 {
12303 tree decl;
12304 tree scope;
12305 bool typename_p;
12306 cp_token *token;
12307
12308 /* All class-names start with an identifier. */
12309 token = cp_lexer_peek_token (parser->lexer);
12310 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12311 {
12312 cp_parser_error (parser, "expected class-name");
12313 return error_mark_node;
12314 }
12315
12316 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12317 to a template-id, so we save it here. */
12318 scope = parser->scope;
12319 if (scope == error_mark_node)
12320 return error_mark_node;
12321
12322 /* Any name names a type if we're following the `typename' keyword
12323 in a qualified name where the enclosing scope is type-dependent. */
12324 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12325 && dependent_type_p (scope));
12326 /* Handle the common case (an identifier, but not a template-id)
12327 efficiently. */
12328 if (token->type == CPP_NAME
12329 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12330 {
12331 tree identifier;
12332
12333 /* Look for the identifier. */
12334 identifier = cp_parser_identifier (parser);
12335 /* If the next token isn't an identifier, we are certainly not
12336 looking at a class-name. */
12337 if (identifier == error_mark_node)
12338 decl = error_mark_node;
12339 /* If we know this is a type-name, there's no need to look it
12340 up. */
12341 else if (typename_p)
12342 decl = identifier;
12343 else
12344 {
12345 /* If the next token is a `::', then the name must be a type
12346 name.
12347
12348 [basic.lookup.qual]
12349
12350 During the lookup for a name preceding the :: scope
12351 resolution operator, object, function, and enumerator
12352 names are ignored. */
12353 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12354 tag_type = typename_type;
12355 /* Look up the name. */
12356 decl = cp_parser_lookup_name (parser, identifier,
12357 tag_type,
12358 /*is_template=*/false,
12359 /*is_namespace=*/false,
12360 check_dependency_p,
12361 /*ambiguous_p=*/NULL);
12362 }
12363 }
12364 else
12365 {
12366 /* Try a template-id. */
12367 decl = cp_parser_template_id (parser, template_keyword_p,
12368 check_dependency_p,
12369 is_declaration);
12370 if (decl == error_mark_node)
12371 return error_mark_node;
12372 }
12373
12374 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12375
12376 /* If this is a typename, create a TYPENAME_TYPE. */
12377 if (typename_p && decl != error_mark_node)
12378 {
12379 decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12380 if (decl != error_mark_node)
12381 decl = TYPE_NAME (decl);
12382 }
12383
12384 /* Check to see that it is really the name of a class. */
12385 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12386 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12387 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12388 /* Situations like this:
12389
12390 template <typename T> struct A {
12391 typename T::template X<int>::I i;
12392 };
12393
12394 are problematic. Is `T::template X<int>' a class-name? The
12395 standard does not seem to be definitive, but there is no other
12396 valid interpretation of the following `::'. Therefore, those
12397 names are considered class-names. */
12398 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12399 else if (decl == error_mark_node
12400 || TREE_CODE (decl) != TYPE_DECL
12401 || TREE_TYPE (decl) == error_mark_node
12402 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12403 {
12404 cp_parser_error (parser, "expected class-name");
12405 return error_mark_node;
12406 }
12407
12408 return decl;
12409 }
12410
12411 /* Parse a class-specifier.
12412
12413 class-specifier:
12414 class-head { member-specification [opt] }
12415
12416 Returns the TREE_TYPE representing the class. */
12417
12418 static tree
12419 cp_parser_class_specifier (cp_parser* parser)
12420 {
12421 cp_token *token;
12422 tree type;
12423 tree attributes = NULL_TREE;
12424 int has_trailing_semicolon;
12425 bool nested_name_specifier_p;
12426 unsigned saved_num_template_parameter_lists;
12427 tree old_scope = NULL_TREE;
12428 tree scope = NULL_TREE;
12429
12430 push_deferring_access_checks (dk_no_deferred);
12431
12432 /* Parse the class-head. */
12433 type = cp_parser_class_head (parser,
12434 &nested_name_specifier_p,
12435 &attributes);
12436 /* If the class-head was a semantic disaster, skip the entire body
12437 of the class. */
12438 if (!type)
12439 {
12440 cp_parser_skip_to_end_of_block_or_statement (parser);
12441 pop_deferring_access_checks ();
12442 return error_mark_node;
12443 }
12444
12445 /* Look for the `{'. */
12446 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12447 {
12448 pop_deferring_access_checks ();
12449 return error_mark_node;
12450 }
12451
12452 /* Issue an error message if type-definitions are forbidden here. */
12453 cp_parser_check_type_definition (parser);
12454 /* Remember that we are defining one more class. */
12455 ++parser->num_classes_being_defined;
12456 /* Inside the class, surrounding template-parameter-lists do not
12457 apply. */
12458 saved_num_template_parameter_lists
12459 = parser->num_template_parameter_lists;
12460 parser->num_template_parameter_lists = 0;
12461
12462 /* Start the class. */
12463 if (nested_name_specifier_p)
12464 {
12465 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12466 old_scope = push_inner_scope (scope);
12467 }
12468 type = begin_class_definition (type);
12469
12470 if (type == error_mark_node)
12471 /* If the type is erroneous, skip the entire body of the class. */
12472 cp_parser_skip_to_closing_brace (parser);
12473 else
12474 /* Parse the member-specification. */
12475 cp_parser_member_specification_opt (parser);
12476
12477 /* Look for the trailing `}'. */
12478 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12479 /* We get better error messages by noticing a common problem: a
12480 missing trailing `;'. */
12481 token = cp_lexer_peek_token (parser->lexer);
12482 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12483 /* Look for trailing attributes to apply to this class. */
12484 if (cp_parser_allow_gnu_extensions_p (parser))
12485 {
12486 tree sub_attr = cp_parser_attributes_opt (parser);
12487 attributes = chainon (attributes, sub_attr);
12488 }
12489 if (type != error_mark_node)
12490 type = finish_struct (type, attributes);
12491 if (nested_name_specifier_p)
12492 pop_inner_scope (old_scope, scope);
12493 /* If this class is not itself within the scope of another class,
12494 then we need to parse the bodies of all of the queued function
12495 definitions. Note that the queued functions defined in a class
12496 are not always processed immediately following the
12497 class-specifier for that class. Consider:
12498
12499 struct A {
12500 struct B { void f() { sizeof (A); } };
12501 };
12502
12503 If `f' were processed before the processing of `A' were
12504 completed, there would be no way to compute the size of `A'.
12505 Note that the nesting we are interested in here is lexical --
12506 not the semantic nesting given by TYPE_CONTEXT. In particular,
12507 for:
12508
12509 struct A { struct B; };
12510 struct A::B { void f() { } };
12511
12512 there is no need to delay the parsing of `A::B::f'. */
12513 if (--parser->num_classes_being_defined == 0)
12514 {
12515 tree queue_entry;
12516 tree fn;
12517 tree class_type = NULL_TREE;
12518 tree pushed_scope = NULL_TREE;
12519
12520 /* In a first pass, parse default arguments to the functions.
12521 Then, in a second pass, parse the bodies of the functions.
12522 This two-phased approach handles cases like:
12523
12524 struct S {
12525 void f() { g(); }
12526 void g(int i = 3);
12527 };
12528
12529 */
12530 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12531 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12532 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12533 TREE_PURPOSE (parser->unparsed_functions_queues)
12534 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12535 {
12536 fn = TREE_VALUE (queue_entry);
12537 /* If there are default arguments that have not yet been processed,
12538 take care of them now. */
12539 if (class_type != TREE_PURPOSE (queue_entry))
12540 {
12541 if (pushed_scope)
12542 pop_scope (pushed_scope);
12543 class_type = TREE_PURPOSE (queue_entry);
12544 pushed_scope = push_scope (class_type);
12545 }
12546 /* Make sure that any template parameters are in scope. */
12547 maybe_begin_member_template_processing (fn);
12548 /* Parse the default argument expressions. */
12549 cp_parser_late_parsing_default_args (parser, fn);
12550 /* Remove any template parameters from the symbol table. */
12551 maybe_end_member_template_processing ();
12552 }
12553 if (pushed_scope)
12554 pop_scope (pushed_scope);
12555 /* Now parse the body of the functions. */
12556 for (TREE_VALUE (parser->unparsed_functions_queues)
12557 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12558 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12559 TREE_VALUE (parser->unparsed_functions_queues)
12560 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12561 {
12562 /* Figure out which function we need to process. */
12563 fn = TREE_VALUE (queue_entry);
12564
12565 /* A hack to prevent garbage collection. */
12566 function_depth++;
12567
12568 /* Parse the function. */
12569 cp_parser_late_parsing_for_member (parser, fn);
12570 function_depth--;
12571 }
12572 }
12573
12574 /* Put back any saved access checks. */
12575 pop_deferring_access_checks ();
12576
12577 /* Restore the count of active template-parameter-lists. */
12578 parser->num_template_parameter_lists
12579 = saved_num_template_parameter_lists;
12580
12581 return type;
12582 }
12583
12584 /* Parse a class-head.
12585
12586 class-head:
12587 class-key identifier [opt] base-clause [opt]
12588 class-key nested-name-specifier identifier base-clause [opt]
12589 class-key nested-name-specifier [opt] template-id
12590 base-clause [opt]
12591
12592 GNU Extensions:
12593 class-key attributes identifier [opt] base-clause [opt]
12594 class-key attributes nested-name-specifier identifier base-clause [opt]
12595 class-key attributes nested-name-specifier [opt] template-id
12596 base-clause [opt]
12597
12598 Returns the TYPE of the indicated class. Sets
12599 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12600 involving a nested-name-specifier was used, and FALSE otherwise.
12601
12602 Returns error_mark_node if this is not a class-head.
12603
12604 Returns NULL_TREE if the class-head is syntactically valid, but
12605 semantically invalid in a way that means we should skip the entire
12606 body of the class. */
12607
12608 static tree
12609 cp_parser_class_head (cp_parser* parser,
12610 bool* nested_name_specifier_p,
12611 tree *attributes_p)
12612 {
12613 tree nested_name_specifier;
12614 enum tag_types class_key;
12615 tree id = NULL_TREE;
12616 tree type = NULL_TREE;
12617 tree attributes;
12618 bool template_id_p = false;
12619 bool qualified_p = false;
12620 bool invalid_nested_name_p = false;
12621 bool invalid_explicit_specialization_p = false;
12622 tree pushed_scope = NULL_TREE;
12623 unsigned num_templates;
12624 tree bases;
12625
12626 /* Assume no nested-name-specifier will be present. */
12627 *nested_name_specifier_p = false;
12628 /* Assume no template parameter lists will be used in defining the
12629 type. */
12630 num_templates = 0;
12631
12632 /* Look for the class-key. */
12633 class_key = cp_parser_class_key (parser);
12634 if (class_key == none_type)
12635 return error_mark_node;
12636
12637 /* Parse the attributes. */
12638 attributes = cp_parser_attributes_opt (parser);
12639
12640 /* If the next token is `::', that is invalid -- but sometimes
12641 people do try to write:
12642
12643 struct ::S {};
12644
12645 Handle this gracefully by accepting the extra qualifier, and then
12646 issuing an error about it later if this really is a
12647 class-head. If it turns out just to be an elaborated type
12648 specifier, remain silent. */
12649 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12650 qualified_p = true;
12651
12652 push_deferring_access_checks (dk_no_check);
12653
12654 /* Determine the name of the class. Begin by looking for an
12655 optional nested-name-specifier. */
12656 nested_name_specifier
12657 = cp_parser_nested_name_specifier_opt (parser,
12658 /*typename_keyword_p=*/false,
12659 /*check_dependency_p=*/false,
12660 /*type_p=*/false,
12661 /*is_declaration=*/false);
12662 /* If there was a nested-name-specifier, then there *must* be an
12663 identifier. */
12664 if (nested_name_specifier)
12665 {
12666 /* Although the grammar says `identifier', it really means
12667 `class-name' or `template-name'. You are only allowed to
12668 define a class that has already been declared with this
12669 syntax.
12670
12671 The proposed resolution for Core Issue 180 says that whever
12672 you see `class T::X' you should treat `X' as a type-name.
12673
12674 It is OK to define an inaccessible class; for example:
12675
12676 class A { class B; };
12677 class A::B {};
12678
12679 We do not know if we will see a class-name, or a
12680 template-name. We look for a class-name first, in case the
12681 class-name is a template-id; if we looked for the
12682 template-name first we would stop after the template-name. */
12683 cp_parser_parse_tentatively (parser);
12684 type = cp_parser_class_name (parser,
12685 /*typename_keyword_p=*/false,
12686 /*template_keyword_p=*/false,
12687 class_type,
12688 /*check_dependency_p=*/false,
12689 /*class_head_p=*/true,
12690 /*is_declaration=*/false);
12691 /* If that didn't work, ignore the nested-name-specifier. */
12692 if (!cp_parser_parse_definitely (parser))
12693 {
12694 invalid_nested_name_p = true;
12695 id = cp_parser_identifier (parser);
12696 if (id == error_mark_node)
12697 id = NULL_TREE;
12698 }
12699 /* If we could not find a corresponding TYPE, treat this
12700 declaration like an unqualified declaration. */
12701 if (type == error_mark_node)
12702 nested_name_specifier = NULL_TREE;
12703 /* Otherwise, count the number of templates used in TYPE and its
12704 containing scopes. */
12705 else
12706 {
12707 tree scope;
12708
12709 for (scope = TREE_TYPE (type);
12710 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12711 scope = (TYPE_P (scope)
12712 ? TYPE_CONTEXT (scope)
12713 : DECL_CONTEXT (scope)))
12714 if (TYPE_P (scope)
12715 && CLASS_TYPE_P (scope)
12716 && CLASSTYPE_TEMPLATE_INFO (scope)
12717 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12718 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12719 ++num_templates;
12720 }
12721 }
12722 /* Otherwise, the identifier is optional. */
12723 else
12724 {
12725 /* We don't know whether what comes next is a template-id,
12726 an identifier, or nothing at all. */
12727 cp_parser_parse_tentatively (parser);
12728 /* Check for a template-id. */
12729 id = cp_parser_template_id (parser,
12730 /*template_keyword_p=*/false,
12731 /*check_dependency_p=*/true,
12732 /*is_declaration=*/true);
12733 /* If that didn't work, it could still be an identifier. */
12734 if (!cp_parser_parse_definitely (parser))
12735 {
12736 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12737 id = cp_parser_identifier (parser);
12738 else
12739 id = NULL_TREE;
12740 }
12741 else
12742 {
12743 template_id_p = true;
12744 ++num_templates;
12745 }
12746 }
12747
12748 pop_deferring_access_checks ();
12749
12750 if (id)
12751 cp_parser_check_for_invalid_template_id (parser, id);
12752
12753 /* If it's not a `:' or a `{' then we can't really be looking at a
12754 class-head, since a class-head only appears as part of a
12755 class-specifier. We have to detect this situation before calling
12756 xref_tag, since that has irreversible side-effects. */
12757 if (!cp_parser_next_token_starts_class_definition_p (parser))
12758 {
12759 cp_parser_error (parser, "expected %<{%> or %<:%>");
12760 return error_mark_node;
12761 }
12762
12763 /* At this point, we're going ahead with the class-specifier, even
12764 if some other problem occurs. */
12765 cp_parser_commit_to_tentative_parse (parser);
12766 /* Issue the error about the overly-qualified name now. */
12767 if (qualified_p)
12768 cp_parser_error (parser,
12769 "global qualification of class name is invalid");
12770 else if (invalid_nested_name_p)
12771 cp_parser_error (parser,
12772 "qualified name does not name a class");
12773 else if (nested_name_specifier)
12774 {
12775 tree scope;
12776
12777 /* Reject typedef-names in class heads. */
12778 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12779 {
12780 error ("invalid class name in declaration of %qD", type);
12781 type = NULL_TREE;
12782 goto done;
12783 }
12784
12785 /* Figure out in what scope the declaration is being placed. */
12786 scope = current_scope ();
12787 /* If that scope does not contain the scope in which the
12788 class was originally declared, the program is invalid. */
12789 if (scope && !is_ancestor (scope, nested_name_specifier))
12790 {
12791 error ("declaration of %qD in %qD which does not enclose %qD",
12792 type, scope, nested_name_specifier);
12793 type = NULL_TREE;
12794 goto done;
12795 }
12796 /* [dcl.meaning]
12797
12798 A declarator-id shall not be qualified exception of the
12799 definition of a ... nested class outside of its class
12800 ... [or] a the definition or explicit instantiation of a
12801 class member of a namespace outside of its namespace. */
12802 if (scope == nested_name_specifier)
12803 {
12804 pedwarn ("extra qualification ignored");
12805 nested_name_specifier = NULL_TREE;
12806 num_templates = 0;
12807 }
12808 }
12809 /* An explicit-specialization must be preceded by "template <>". If
12810 it is not, try to recover gracefully. */
12811 if (at_namespace_scope_p ()
12812 && parser->num_template_parameter_lists == 0
12813 && template_id_p)
12814 {
12815 error ("an explicit specialization must be preceded by %<template <>%>");
12816 invalid_explicit_specialization_p = true;
12817 /* Take the same action that would have been taken by
12818 cp_parser_explicit_specialization. */
12819 ++parser->num_template_parameter_lists;
12820 begin_specialization ();
12821 }
12822 /* There must be no "return" statements between this point and the
12823 end of this function; set "type "to the correct return value and
12824 use "goto done;" to return. */
12825 /* Make sure that the right number of template parameters were
12826 present. */
12827 if (!cp_parser_check_template_parameters (parser, num_templates))
12828 {
12829 /* If something went wrong, there is no point in even trying to
12830 process the class-definition. */
12831 type = NULL_TREE;
12832 goto done;
12833 }
12834
12835 /* Look up the type. */
12836 if (template_id_p)
12837 {
12838 type = TREE_TYPE (id);
12839 maybe_process_partial_specialization (type);
12840 if (nested_name_specifier)
12841 pushed_scope = push_scope (nested_name_specifier);
12842 }
12843 else if (nested_name_specifier)
12844 {
12845 tree class_type;
12846
12847 /* Given:
12848
12849 template <typename T> struct S { struct T };
12850 template <typename T> struct S<T>::T { };
12851
12852 we will get a TYPENAME_TYPE when processing the definition of
12853 `S::T'. We need to resolve it to the actual type before we
12854 try to define it. */
12855 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12856 {
12857 class_type = resolve_typename_type (TREE_TYPE (type),
12858 /*only_current_p=*/false);
12859 if (class_type != error_mark_node)
12860 type = TYPE_NAME (class_type);
12861 else
12862 {
12863 cp_parser_error (parser, "could not resolve typename type");
12864 type = error_mark_node;
12865 }
12866 }
12867
12868 maybe_process_partial_specialization (TREE_TYPE (type));
12869 class_type = current_class_type;
12870 /* Enter the scope indicated by the nested-name-specifier. */
12871 pushed_scope = push_scope (nested_name_specifier);
12872 /* Get the canonical version of this type. */
12873 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12874 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12875 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12876 {
12877 type = push_template_decl (type);
12878 if (type == error_mark_node)
12879 {
12880 type = NULL_TREE;
12881 goto done;
12882 }
12883 }
12884
12885 type = TREE_TYPE (type);
12886 *nested_name_specifier_p = true;
12887 }
12888 else /* The name is not a nested name. */
12889 {
12890 /* If the class was unnamed, create a dummy name. */
12891 if (!id)
12892 id = make_anon_name ();
12893 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
12894 parser->num_template_parameter_lists);
12895 }
12896
12897 /* Indicate whether this class was declared as a `class' or as a
12898 `struct'. */
12899 if (TREE_CODE (type) == RECORD_TYPE)
12900 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12901 cp_parser_check_class_key (class_key, type);
12902
12903 /* If this type was already complete, and we see another definition,
12904 that's an error. */
12905 if (type != error_mark_node && COMPLETE_TYPE_P (type))
12906 {
12907 error ("redefinition of %q#T", type);
12908 cp_error_at ("previous definition of %q#T", type);
12909 type = NULL_TREE;
12910 goto done;
12911 }
12912
12913 /* We will have entered the scope containing the class; the names of
12914 base classes should be looked up in that context. For example:
12915
12916 struct A { struct B {}; struct C; };
12917 struct A::C : B {};
12918
12919 is valid. */
12920 bases = NULL_TREE;
12921
12922 /* Get the list of base-classes, if there is one. */
12923 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12924 bases = cp_parser_base_clause (parser);
12925
12926 /* Process the base classes. */
12927 xref_basetypes (type, bases);
12928
12929 done:
12930 /* Leave the scope given by the nested-name-specifier. We will
12931 enter the class scope itself while processing the members. */
12932 if (pushed_scope)
12933 pop_scope (pushed_scope);
12934
12935 if (invalid_explicit_specialization_p)
12936 {
12937 end_specialization ();
12938 --parser->num_template_parameter_lists;
12939 }
12940 *attributes_p = attributes;
12941 return type;
12942 }
12943
12944 /* Parse a class-key.
12945
12946 class-key:
12947 class
12948 struct
12949 union
12950
12951 Returns the kind of class-key specified, or none_type to indicate
12952 error. */
12953
12954 static enum tag_types
12955 cp_parser_class_key (cp_parser* parser)
12956 {
12957 cp_token *token;
12958 enum tag_types tag_type;
12959
12960 /* Look for the class-key. */
12961 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12962 if (!token)
12963 return none_type;
12964
12965 /* Check to see if the TOKEN is a class-key. */
12966 tag_type = cp_parser_token_is_class_key (token);
12967 if (!tag_type)
12968 cp_parser_error (parser, "expected class-key");
12969 return tag_type;
12970 }
12971
12972 /* Parse an (optional) member-specification.
12973
12974 member-specification:
12975 member-declaration member-specification [opt]
12976 access-specifier : member-specification [opt] */
12977
12978 static void
12979 cp_parser_member_specification_opt (cp_parser* parser)
12980 {
12981 while (true)
12982 {
12983 cp_token *token;
12984 enum rid keyword;
12985
12986 /* Peek at the next token. */
12987 token = cp_lexer_peek_token (parser->lexer);
12988 /* If it's a `}', or EOF then we've seen all the members. */
12989 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12990 break;
12991
12992 /* See if this token is a keyword. */
12993 keyword = token->keyword;
12994 switch (keyword)
12995 {
12996 case RID_PUBLIC:
12997 case RID_PROTECTED:
12998 case RID_PRIVATE:
12999 /* Consume the access-specifier. */
13000 cp_lexer_consume_token (parser->lexer);
13001 /* Remember which access-specifier is active. */
13002 current_access_specifier = token->value;
13003 /* Look for the `:'. */
13004 cp_parser_require (parser, CPP_COLON, "`:'");
13005 break;
13006
13007 default:
13008 /* Accept #pragmas at class scope. */
13009 if (token->type == CPP_PRAGMA)
13010 {
13011 cp_lexer_handle_pragma (parser->lexer);
13012 break;
13013 }
13014
13015 /* Otherwise, the next construction must be a
13016 member-declaration. */
13017 cp_parser_member_declaration (parser);
13018 }
13019 }
13020 }
13021
13022 /* Parse a member-declaration.
13023
13024 member-declaration:
13025 decl-specifier-seq [opt] member-declarator-list [opt] ;
13026 function-definition ; [opt]
13027 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13028 using-declaration
13029 template-declaration
13030
13031 member-declarator-list:
13032 member-declarator
13033 member-declarator-list , member-declarator
13034
13035 member-declarator:
13036 declarator pure-specifier [opt]
13037 declarator constant-initializer [opt]
13038 identifier [opt] : constant-expression
13039
13040 GNU Extensions:
13041
13042 member-declaration:
13043 __extension__ member-declaration
13044
13045 member-declarator:
13046 declarator attributes [opt] pure-specifier [opt]
13047 declarator attributes [opt] constant-initializer [opt]
13048 identifier [opt] attributes [opt] : constant-expression */
13049
13050 static void
13051 cp_parser_member_declaration (cp_parser* parser)
13052 {
13053 cp_decl_specifier_seq decl_specifiers;
13054 tree prefix_attributes;
13055 tree decl;
13056 int declares_class_or_enum;
13057 bool friend_p;
13058 cp_token *token;
13059 int saved_pedantic;
13060
13061 /* Check for the `__extension__' keyword. */
13062 if (cp_parser_extension_opt (parser, &saved_pedantic))
13063 {
13064 /* Recurse. */
13065 cp_parser_member_declaration (parser);
13066 /* Restore the old value of the PEDANTIC flag. */
13067 pedantic = saved_pedantic;
13068
13069 return;
13070 }
13071
13072 /* Check for a template-declaration. */
13073 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13074 {
13075 /* Parse the template-declaration. */
13076 cp_parser_template_declaration (parser, /*member_p=*/true);
13077
13078 return;
13079 }
13080
13081 /* Check for a using-declaration. */
13082 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13083 {
13084 /* Parse the using-declaration. */
13085 cp_parser_using_declaration (parser);
13086
13087 return;
13088 }
13089
13090 /* Parse the decl-specifier-seq. */
13091 cp_parser_decl_specifier_seq (parser,
13092 CP_PARSER_FLAGS_OPTIONAL,
13093 &decl_specifiers,
13094 &declares_class_or_enum);
13095 prefix_attributes = decl_specifiers.attributes;
13096 decl_specifiers.attributes = NULL_TREE;
13097 /* Check for an invalid type-name. */
13098 if (!decl_specifiers.type
13099 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13100 return;
13101 /* If there is no declarator, then the decl-specifier-seq should
13102 specify a type. */
13103 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13104 {
13105 /* If there was no decl-specifier-seq, and the next token is a
13106 `;', then we have something like:
13107
13108 struct S { ; };
13109
13110 [class.mem]
13111
13112 Each member-declaration shall declare at least one member
13113 name of the class. */
13114 if (!decl_specifiers.any_specifiers_p)
13115 {
13116 cp_token *token = cp_lexer_peek_token (parser->lexer);
13117 if (pedantic && !token->in_system_header)
13118 pedwarn ("%Hextra %<;%>", &token->location);
13119 }
13120 else
13121 {
13122 tree type;
13123
13124 /* See if this declaration is a friend. */
13125 friend_p = cp_parser_friend_p (&decl_specifiers);
13126 /* If there were decl-specifiers, check to see if there was
13127 a class-declaration. */
13128 type = check_tag_decl (&decl_specifiers);
13129 /* Nested classes have already been added to the class, but
13130 a `friend' needs to be explicitly registered. */
13131 if (friend_p)
13132 {
13133 /* If the `friend' keyword was present, the friend must
13134 be introduced with a class-key. */
13135 if (!declares_class_or_enum)
13136 error ("a class-key must be used when declaring a friend");
13137 /* In this case:
13138
13139 template <typename T> struct A {
13140 friend struct A<T>::B;
13141 };
13142
13143 A<T>::B will be represented by a TYPENAME_TYPE, and
13144 therefore not recognized by check_tag_decl. */
13145 if (!type
13146 && decl_specifiers.type
13147 && TYPE_P (decl_specifiers.type))
13148 type = decl_specifiers.type;
13149 if (!type || !TYPE_P (type))
13150 error ("friend declaration does not name a class or "
13151 "function");
13152 else
13153 make_friend_class (current_class_type, type,
13154 /*complain=*/true);
13155 }
13156 /* If there is no TYPE, an error message will already have
13157 been issued. */
13158 else if (!type || type == error_mark_node)
13159 ;
13160 /* An anonymous aggregate has to be handled specially; such
13161 a declaration really declares a data member (with a
13162 particular type), as opposed to a nested class. */
13163 else if (ANON_AGGR_TYPE_P (type))
13164 {
13165 /* Remove constructors and such from TYPE, now that we
13166 know it is an anonymous aggregate. */
13167 fixup_anonymous_aggr (type);
13168 /* And make the corresponding data member. */
13169 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13170 /* Add it to the class. */
13171 finish_member_declaration (decl);
13172 }
13173 else
13174 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13175 }
13176 }
13177 else
13178 {
13179 /* See if these declarations will be friends. */
13180 friend_p = cp_parser_friend_p (&decl_specifiers);
13181
13182 /* Keep going until we hit the `;' at the end of the
13183 declaration. */
13184 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13185 {
13186 tree attributes = NULL_TREE;
13187 tree first_attribute;
13188
13189 /* Peek at the next token. */
13190 token = cp_lexer_peek_token (parser->lexer);
13191
13192 /* Check for a bitfield declaration. */
13193 if (token->type == CPP_COLON
13194 || (token->type == CPP_NAME
13195 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13196 == CPP_COLON))
13197 {
13198 tree identifier;
13199 tree width;
13200
13201 /* Get the name of the bitfield. Note that we cannot just
13202 check TOKEN here because it may have been invalidated by
13203 the call to cp_lexer_peek_nth_token above. */
13204 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13205 identifier = cp_parser_identifier (parser);
13206 else
13207 identifier = NULL_TREE;
13208
13209 /* Consume the `:' token. */
13210 cp_lexer_consume_token (parser->lexer);
13211 /* Get the width of the bitfield. */
13212 width
13213 = cp_parser_constant_expression (parser,
13214 /*allow_non_constant=*/false,
13215 NULL);
13216
13217 /* Look for attributes that apply to the bitfield. */
13218 attributes = cp_parser_attributes_opt (parser);
13219 /* Remember which attributes are prefix attributes and
13220 which are not. */
13221 first_attribute = attributes;
13222 /* Combine the attributes. */
13223 attributes = chainon (prefix_attributes, attributes);
13224
13225 /* Create the bitfield declaration. */
13226 decl = grokbitfield (identifier
13227 ? make_id_declarator (NULL_TREE,
13228 identifier)
13229 : NULL,
13230 &decl_specifiers,
13231 width);
13232 /* Apply the attributes. */
13233 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13234 }
13235 else
13236 {
13237 cp_declarator *declarator;
13238 tree initializer;
13239 tree asm_specification;
13240 int ctor_dtor_or_conv_p;
13241
13242 /* Parse the declarator. */
13243 declarator
13244 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13245 &ctor_dtor_or_conv_p,
13246 /*parenthesized_p=*/NULL,
13247 /*member_p=*/true);
13248
13249 /* If something went wrong parsing the declarator, make sure
13250 that we at least consume some tokens. */
13251 if (declarator == cp_error_declarator)
13252 {
13253 /* Skip to the end of the statement. */
13254 cp_parser_skip_to_end_of_statement (parser);
13255 /* If the next token is not a semicolon, that is
13256 probably because we just skipped over the body of
13257 a function. So, we consume a semicolon if
13258 present, but do not issue an error message if it
13259 is not present. */
13260 if (cp_lexer_next_token_is (parser->lexer,
13261 CPP_SEMICOLON))
13262 cp_lexer_consume_token (parser->lexer);
13263 return;
13264 }
13265
13266 if (declares_class_or_enum & 2)
13267 cp_parser_check_for_definition_in_return_type
13268 (declarator, decl_specifiers.type);
13269
13270 /* Look for an asm-specification. */
13271 asm_specification = cp_parser_asm_specification_opt (parser);
13272 /* Look for attributes that apply to the declaration. */
13273 attributes = cp_parser_attributes_opt (parser);
13274 /* Remember which attributes are prefix attributes and
13275 which are not. */
13276 first_attribute = attributes;
13277 /* Combine the attributes. */
13278 attributes = chainon (prefix_attributes, attributes);
13279
13280 /* If it's an `=', then we have a constant-initializer or a
13281 pure-specifier. It is not correct to parse the
13282 initializer before registering the member declaration
13283 since the member declaration should be in scope while
13284 its initializer is processed. However, the rest of the
13285 front end does not yet provide an interface that allows
13286 us to handle this correctly. */
13287 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13288 {
13289 /* In [class.mem]:
13290
13291 A pure-specifier shall be used only in the declaration of
13292 a virtual function.
13293
13294 A member-declarator can contain a constant-initializer
13295 only if it declares a static member of integral or
13296 enumeration type.
13297
13298 Therefore, if the DECLARATOR is for a function, we look
13299 for a pure-specifier; otherwise, we look for a
13300 constant-initializer. When we call `grokfield', it will
13301 perform more stringent semantics checks. */
13302 if (declarator->kind == cdk_function)
13303 initializer = cp_parser_pure_specifier (parser);
13304 else
13305 /* Parse the initializer. */
13306 initializer = cp_parser_constant_initializer (parser);
13307 }
13308 /* Otherwise, there is no initializer. */
13309 else
13310 initializer = NULL_TREE;
13311
13312 /* See if we are probably looking at a function
13313 definition. We are certainly not looking at a
13314 member-declarator. Calling `grokfield' has
13315 side-effects, so we must not do it unless we are sure
13316 that we are looking at a member-declarator. */
13317 if (cp_parser_token_starts_function_definition_p
13318 (cp_lexer_peek_token (parser->lexer)))
13319 {
13320 /* The grammar does not allow a pure-specifier to be
13321 used when a member function is defined. (It is
13322 possible that this fact is an oversight in the
13323 standard, since a pure function may be defined
13324 outside of the class-specifier. */
13325 if (initializer)
13326 error ("pure-specifier on function-definition");
13327 decl = cp_parser_save_member_function_body (parser,
13328 &decl_specifiers,
13329 declarator,
13330 attributes);
13331 /* If the member was not a friend, declare it here. */
13332 if (!friend_p)
13333 finish_member_declaration (decl);
13334 /* Peek at the next token. */
13335 token = cp_lexer_peek_token (parser->lexer);
13336 /* If the next token is a semicolon, consume it. */
13337 if (token->type == CPP_SEMICOLON)
13338 cp_lexer_consume_token (parser->lexer);
13339 return;
13340 }
13341 else
13342 {
13343 /* Create the declaration. */
13344 decl = grokfield (declarator, &decl_specifiers,
13345 initializer, asm_specification,
13346 attributes);
13347 /* Any initialization must have been from a
13348 constant-expression. */
13349 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13350 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13351 }
13352 }
13353
13354 /* Reset PREFIX_ATTRIBUTES. */
13355 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13356 attributes = TREE_CHAIN (attributes);
13357 if (attributes)
13358 TREE_CHAIN (attributes) = NULL_TREE;
13359
13360 /* If there is any qualification still in effect, clear it
13361 now; we will be starting fresh with the next declarator. */
13362 parser->scope = NULL_TREE;
13363 parser->qualifying_scope = NULL_TREE;
13364 parser->object_scope = NULL_TREE;
13365 /* If it's a `,', then there are more declarators. */
13366 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13367 cp_lexer_consume_token (parser->lexer);
13368 /* If the next token isn't a `;', then we have a parse error. */
13369 else if (cp_lexer_next_token_is_not (parser->lexer,
13370 CPP_SEMICOLON))
13371 {
13372 cp_parser_error (parser, "expected %<;%>");
13373 /* Skip tokens until we find a `;'. */
13374 cp_parser_skip_to_end_of_statement (parser);
13375
13376 break;
13377 }
13378
13379 if (decl)
13380 {
13381 /* Add DECL to the list of members. */
13382 if (!friend_p)
13383 finish_member_declaration (decl);
13384
13385 if (TREE_CODE (decl) == FUNCTION_DECL)
13386 cp_parser_save_default_args (parser, decl);
13387 }
13388 }
13389 }
13390
13391 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13392 }
13393
13394 /* Parse a pure-specifier.
13395
13396 pure-specifier:
13397 = 0
13398
13399 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13400 Otherwise, ERROR_MARK_NODE is returned. */
13401
13402 static tree
13403 cp_parser_pure_specifier (cp_parser* parser)
13404 {
13405 cp_token *token;
13406
13407 /* Look for the `=' token. */
13408 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13409 return error_mark_node;
13410 /* Look for the `0' token. */
13411 token = cp_lexer_consume_token (parser->lexer);
13412 if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13413 {
13414 cp_parser_error (parser,
13415 "invalid pure specifier (only `= 0' is allowed)");
13416 cp_parser_skip_to_end_of_statement (parser);
13417 return error_mark_node;
13418 }
13419
13420 /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13421 We need to get information from the lexer about how the number
13422 was spelled in order to fix this problem. */
13423 return integer_zero_node;
13424 }
13425
13426 /* Parse a constant-initializer.
13427
13428 constant-initializer:
13429 = constant-expression
13430
13431 Returns a representation of the constant-expression. */
13432
13433 static tree
13434 cp_parser_constant_initializer (cp_parser* parser)
13435 {
13436 /* Look for the `=' token. */
13437 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13438 return error_mark_node;
13439
13440 /* It is invalid to write:
13441
13442 struct S { static const int i = { 7 }; };
13443
13444 */
13445 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13446 {
13447 cp_parser_error (parser,
13448 "a brace-enclosed initializer is not allowed here");
13449 /* Consume the opening brace. */
13450 cp_lexer_consume_token (parser->lexer);
13451 /* Skip the initializer. */
13452 cp_parser_skip_to_closing_brace (parser);
13453 /* Look for the trailing `}'. */
13454 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13455
13456 return error_mark_node;
13457 }
13458
13459 return cp_parser_constant_expression (parser,
13460 /*allow_non_constant=*/false,
13461 NULL);
13462 }
13463
13464 /* Derived classes [gram.class.derived] */
13465
13466 /* Parse a base-clause.
13467
13468 base-clause:
13469 : base-specifier-list
13470
13471 base-specifier-list:
13472 base-specifier
13473 base-specifier-list , base-specifier
13474
13475 Returns a TREE_LIST representing the base-classes, in the order in
13476 which they were declared. The representation of each node is as
13477 described by cp_parser_base_specifier.
13478
13479 In the case that no bases are specified, this function will return
13480 NULL_TREE, not ERROR_MARK_NODE. */
13481
13482 static tree
13483 cp_parser_base_clause (cp_parser* parser)
13484 {
13485 tree bases = NULL_TREE;
13486
13487 /* Look for the `:' that begins the list. */
13488 cp_parser_require (parser, CPP_COLON, "`:'");
13489
13490 /* Scan the base-specifier-list. */
13491 while (true)
13492 {
13493 cp_token *token;
13494 tree base;
13495
13496 /* Look for the base-specifier. */
13497 base = cp_parser_base_specifier (parser);
13498 /* Add BASE to the front of the list. */
13499 if (base != error_mark_node)
13500 {
13501 TREE_CHAIN (base) = bases;
13502 bases = base;
13503 }
13504 /* Peek at the next token. */
13505 token = cp_lexer_peek_token (parser->lexer);
13506 /* If it's not a comma, then the list is complete. */
13507 if (token->type != CPP_COMMA)
13508 break;
13509 /* Consume the `,'. */
13510 cp_lexer_consume_token (parser->lexer);
13511 }
13512
13513 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13514 base class had a qualified name. However, the next name that
13515 appears is certainly not qualified. */
13516 parser->scope = NULL_TREE;
13517 parser->qualifying_scope = NULL_TREE;
13518 parser->object_scope = NULL_TREE;
13519
13520 return nreverse (bases);
13521 }
13522
13523 /* Parse a base-specifier.
13524
13525 base-specifier:
13526 :: [opt] nested-name-specifier [opt] class-name
13527 virtual access-specifier [opt] :: [opt] nested-name-specifier
13528 [opt] class-name
13529 access-specifier virtual [opt] :: [opt] nested-name-specifier
13530 [opt] class-name
13531
13532 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13533 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13534 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13535 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13536
13537 static tree
13538 cp_parser_base_specifier (cp_parser* parser)
13539 {
13540 cp_token *token;
13541 bool done = false;
13542 bool virtual_p = false;
13543 bool duplicate_virtual_error_issued_p = false;
13544 bool duplicate_access_error_issued_p = false;
13545 bool class_scope_p, template_p;
13546 tree access = access_default_node;
13547 tree type;
13548
13549 /* Process the optional `virtual' and `access-specifier'. */
13550 while (!done)
13551 {
13552 /* Peek at the next token. */
13553 token = cp_lexer_peek_token (parser->lexer);
13554 /* Process `virtual'. */
13555 switch (token->keyword)
13556 {
13557 case RID_VIRTUAL:
13558 /* If `virtual' appears more than once, issue an error. */
13559 if (virtual_p && !duplicate_virtual_error_issued_p)
13560 {
13561 cp_parser_error (parser,
13562 "%<virtual%> specified more than once in base-specified");
13563 duplicate_virtual_error_issued_p = true;
13564 }
13565
13566 virtual_p = true;
13567
13568 /* Consume the `virtual' token. */
13569 cp_lexer_consume_token (parser->lexer);
13570
13571 break;
13572
13573 case RID_PUBLIC:
13574 case RID_PROTECTED:
13575 case RID_PRIVATE:
13576 /* If more than one access specifier appears, issue an
13577 error. */
13578 if (access != access_default_node
13579 && !duplicate_access_error_issued_p)
13580 {
13581 cp_parser_error (parser,
13582 "more than one access specifier in base-specified");
13583 duplicate_access_error_issued_p = true;
13584 }
13585
13586 access = ridpointers[(int) token->keyword];
13587
13588 /* Consume the access-specifier. */
13589 cp_lexer_consume_token (parser->lexer);
13590
13591 break;
13592
13593 default:
13594 done = true;
13595 break;
13596 }
13597 }
13598 /* It is not uncommon to see programs mechanically, erroneously, use
13599 the 'typename' keyword to denote (dependent) qualified types
13600 as base classes. */
13601 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13602 {
13603 if (!processing_template_decl)
13604 error ("keyword %<typename%> not allowed outside of templates");
13605 else
13606 error ("keyword %<typename%> not allowed in this context "
13607 "(the base class is implicitly a type)");
13608 cp_lexer_consume_token (parser->lexer);
13609 }
13610
13611 /* Look for the optional `::' operator. */
13612 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13613 /* Look for the nested-name-specifier. The simplest way to
13614 implement:
13615
13616 [temp.res]
13617
13618 The keyword `typename' is not permitted in a base-specifier or
13619 mem-initializer; in these contexts a qualified name that
13620 depends on a template-parameter is implicitly assumed to be a
13621 type name.
13622
13623 is to pretend that we have seen the `typename' keyword at this
13624 point. */
13625 cp_parser_nested_name_specifier_opt (parser,
13626 /*typename_keyword_p=*/true,
13627 /*check_dependency_p=*/true,
13628 typename_type,
13629 /*is_declaration=*/true);
13630 /* If the base class is given by a qualified name, assume that names
13631 we see are type names or templates, as appropriate. */
13632 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13633 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13634
13635 /* Finally, look for the class-name. */
13636 type = cp_parser_class_name (parser,
13637 class_scope_p,
13638 template_p,
13639 typename_type,
13640 /*check_dependency_p=*/true,
13641 /*class_head_p=*/false,
13642 /*is_declaration=*/true);
13643
13644 if (type == error_mark_node)
13645 return error_mark_node;
13646
13647 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13648 }
13649
13650 /* Exception handling [gram.exception] */
13651
13652 /* Parse an (optional) exception-specification.
13653
13654 exception-specification:
13655 throw ( type-id-list [opt] )
13656
13657 Returns a TREE_LIST representing the exception-specification. The
13658 TREE_VALUE of each node is a type. */
13659
13660 static tree
13661 cp_parser_exception_specification_opt (cp_parser* parser)
13662 {
13663 cp_token *token;
13664 tree type_id_list;
13665
13666 /* Peek at the next token. */
13667 token = cp_lexer_peek_token (parser->lexer);
13668 /* If it's not `throw', then there's no exception-specification. */
13669 if (!cp_parser_is_keyword (token, RID_THROW))
13670 return NULL_TREE;
13671
13672 /* Consume the `throw'. */
13673 cp_lexer_consume_token (parser->lexer);
13674
13675 /* Look for the `('. */
13676 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13677
13678 /* Peek at the next token. */
13679 token = cp_lexer_peek_token (parser->lexer);
13680 /* If it's not a `)', then there is a type-id-list. */
13681 if (token->type != CPP_CLOSE_PAREN)
13682 {
13683 const char *saved_message;
13684
13685 /* Types may not be defined in an exception-specification. */
13686 saved_message = parser->type_definition_forbidden_message;
13687 parser->type_definition_forbidden_message
13688 = "types may not be defined in an exception-specification";
13689 /* Parse the type-id-list. */
13690 type_id_list = cp_parser_type_id_list (parser);
13691 /* Restore the saved message. */
13692 parser->type_definition_forbidden_message = saved_message;
13693 }
13694 else
13695 type_id_list = empty_except_spec;
13696
13697 /* Look for the `)'. */
13698 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13699
13700 return type_id_list;
13701 }
13702
13703 /* Parse an (optional) type-id-list.
13704
13705 type-id-list:
13706 type-id
13707 type-id-list , type-id
13708
13709 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13710 in the order that the types were presented. */
13711
13712 static tree
13713 cp_parser_type_id_list (cp_parser* parser)
13714 {
13715 tree types = NULL_TREE;
13716
13717 while (true)
13718 {
13719 cp_token *token;
13720 tree type;
13721
13722 /* Get the next type-id. */
13723 type = cp_parser_type_id (parser);
13724 /* Add it to the list. */
13725 types = add_exception_specifier (types, type, /*complain=*/1);
13726 /* Peek at the next token. */
13727 token = cp_lexer_peek_token (parser->lexer);
13728 /* If it is not a `,', we are done. */
13729 if (token->type != CPP_COMMA)
13730 break;
13731 /* Consume the `,'. */
13732 cp_lexer_consume_token (parser->lexer);
13733 }
13734
13735 return nreverse (types);
13736 }
13737
13738 /* Parse a try-block.
13739
13740 try-block:
13741 try compound-statement handler-seq */
13742
13743 static tree
13744 cp_parser_try_block (cp_parser* parser)
13745 {
13746 tree try_block;
13747
13748 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13749 try_block = begin_try_block ();
13750 cp_parser_compound_statement (parser, NULL, true);
13751 finish_try_block (try_block);
13752 cp_parser_handler_seq (parser);
13753 finish_handler_sequence (try_block);
13754
13755 return try_block;
13756 }
13757
13758 /* Parse a function-try-block.
13759
13760 function-try-block:
13761 try ctor-initializer [opt] function-body handler-seq */
13762
13763 static bool
13764 cp_parser_function_try_block (cp_parser* parser)
13765 {
13766 tree try_block;
13767 bool ctor_initializer_p;
13768
13769 /* Look for the `try' keyword. */
13770 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13771 return false;
13772 /* Let the rest of the front-end know where we are. */
13773 try_block = begin_function_try_block ();
13774 /* Parse the function-body. */
13775 ctor_initializer_p
13776 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13777 /* We're done with the `try' part. */
13778 finish_function_try_block (try_block);
13779 /* Parse the handlers. */
13780 cp_parser_handler_seq (parser);
13781 /* We're done with the handlers. */
13782 finish_function_handler_sequence (try_block);
13783
13784 return ctor_initializer_p;
13785 }
13786
13787 /* Parse a handler-seq.
13788
13789 handler-seq:
13790 handler handler-seq [opt] */
13791
13792 static void
13793 cp_parser_handler_seq (cp_parser* parser)
13794 {
13795 while (true)
13796 {
13797 cp_token *token;
13798
13799 /* Parse the handler. */
13800 cp_parser_handler (parser);
13801 /* Peek at the next token. */
13802 token = cp_lexer_peek_token (parser->lexer);
13803 /* If it's not `catch' then there are no more handlers. */
13804 if (!cp_parser_is_keyword (token, RID_CATCH))
13805 break;
13806 }
13807 }
13808
13809 /* Parse a handler.
13810
13811 handler:
13812 catch ( exception-declaration ) compound-statement */
13813
13814 static void
13815 cp_parser_handler (cp_parser* parser)
13816 {
13817 tree handler;
13818 tree declaration;
13819
13820 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13821 handler = begin_handler ();
13822 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13823 declaration = cp_parser_exception_declaration (parser);
13824 finish_handler_parms (declaration, handler);
13825 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13826 cp_parser_compound_statement (parser, NULL, false);
13827 finish_handler (handler);
13828 }
13829
13830 /* Parse an exception-declaration.
13831
13832 exception-declaration:
13833 type-specifier-seq declarator
13834 type-specifier-seq abstract-declarator
13835 type-specifier-seq
13836 ...
13837
13838 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13839 ellipsis variant is used. */
13840
13841 static tree
13842 cp_parser_exception_declaration (cp_parser* parser)
13843 {
13844 tree decl;
13845 cp_decl_specifier_seq type_specifiers;
13846 cp_declarator *declarator;
13847 const char *saved_message;
13848
13849 /* If it's an ellipsis, it's easy to handle. */
13850 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13851 {
13852 /* Consume the `...' token. */
13853 cp_lexer_consume_token (parser->lexer);
13854 return NULL_TREE;
13855 }
13856
13857 /* Types may not be defined in exception-declarations. */
13858 saved_message = parser->type_definition_forbidden_message;
13859 parser->type_definition_forbidden_message
13860 = "types may not be defined in exception-declarations";
13861
13862 /* Parse the type-specifier-seq. */
13863 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13864 &type_specifiers);
13865 /* If it's a `)', then there is no declarator. */
13866 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13867 declarator = NULL;
13868 else
13869 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13870 /*ctor_dtor_or_conv_p=*/NULL,
13871 /*parenthesized_p=*/NULL,
13872 /*member_p=*/false);
13873
13874 /* Restore the saved message. */
13875 parser->type_definition_forbidden_message = saved_message;
13876
13877 if (type_specifiers.any_specifiers_p)
13878 {
13879 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13880 if (decl == NULL_TREE)
13881 error ("invalid catch parameter");
13882 }
13883 else
13884 decl = NULL_TREE;
13885
13886 return decl;
13887 }
13888
13889 /* Parse a throw-expression.
13890
13891 throw-expression:
13892 throw assignment-expression [opt]
13893
13894 Returns a THROW_EXPR representing the throw-expression. */
13895
13896 static tree
13897 cp_parser_throw_expression (cp_parser* parser)
13898 {
13899 tree expression;
13900 cp_token* token;
13901
13902 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13903 token = cp_lexer_peek_token (parser->lexer);
13904 /* Figure out whether or not there is an assignment-expression
13905 following the "throw" keyword. */
13906 if (token->type == CPP_COMMA
13907 || token->type == CPP_SEMICOLON
13908 || token->type == CPP_CLOSE_PAREN
13909 || token->type == CPP_CLOSE_SQUARE
13910 || token->type == CPP_CLOSE_BRACE
13911 || token->type == CPP_COLON)
13912 expression = NULL_TREE;
13913 else
13914 expression = cp_parser_assignment_expression (parser,
13915 /*cast_p=*/false);
13916
13917 return build_throw (expression);
13918 }
13919
13920 /* GNU Extensions */
13921
13922 /* Parse an (optional) asm-specification.
13923
13924 asm-specification:
13925 asm ( string-literal )
13926
13927 If the asm-specification is present, returns a STRING_CST
13928 corresponding to the string-literal. Otherwise, returns
13929 NULL_TREE. */
13930
13931 static tree
13932 cp_parser_asm_specification_opt (cp_parser* parser)
13933 {
13934 cp_token *token;
13935 tree asm_specification;
13936
13937 /* Peek at the next token. */
13938 token = cp_lexer_peek_token (parser->lexer);
13939 /* If the next token isn't the `asm' keyword, then there's no
13940 asm-specification. */
13941 if (!cp_parser_is_keyword (token, RID_ASM))
13942 return NULL_TREE;
13943
13944 /* Consume the `asm' token. */
13945 cp_lexer_consume_token (parser->lexer);
13946 /* Look for the `('. */
13947 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13948
13949 /* Look for the string-literal. */
13950 asm_specification = cp_parser_string_literal (parser, false, false);
13951
13952 /* Look for the `)'. */
13953 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13954
13955 return asm_specification;
13956 }
13957
13958 /* Parse an asm-operand-list.
13959
13960 asm-operand-list:
13961 asm-operand
13962 asm-operand-list , asm-operand
13963
13964 asm-operand:
13965 string-literal ( expression )
13966 [ string-literal ] string-literal ( expression )
13967
13968 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13969 each node is the expression. The TREE_PURPOSE is itself a
13970 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13971 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13972 is a STRING_CST for the string literal before the parenthesis. */
13973
13974 static tree
13975 cp_parser_asm_operand_list (cp_parser* parser)
13976 {
13977 tree asm_operands = NULL_TREE;
13978
13979 while (true)
13980 {
13981 tree string_literal;
13982 tree expression;
13983 tree name;
13984
13985 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13986 {
13987 /* Consume the `[' token. */
13988 cp_lexer_consume_token (parser->lexer);
13989 /* Read the operand name. */
13990 name = cp_parser_identifier (parser);
13991 if (name != error_mark_node)
13992 name = build_string (IDENTIFIER_LENGTH (name),
13993 IDENTIFIER_POINTER (name));
13994 /* Look for the closing `]'. */
13995 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13996 }
13997 else
13998 name = NULL_TREE;
13999 /* Look for the string-literal. */
14000 string_literal = cp_parser_string_literal (parser, false, false);
14001
14002 /* Look for the `('. */
14003 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14004 /* Parse the expression. */
14005 expression = cp_parser_expression (parser, /*cast_p=*/false);
14006 /* Look for the `)'. */
14007 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14008
14009 /* Add this operand to the list. */
14010 asm_operands = tree_cons (build_tree_list (name, string_literal),
14011 expression,
14012 asm_operands);
14013 /* If the next token is not a `,', there are no more
14014 operands. */
14015 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14016 break;
14017 /* Consume the `,'. */
14018 cp_lexer_consume_token (parser->lexer);
14019 }
14020
14021 return nreverse (asm_operands);
14022 }
14023
14024 /* Parse an asm-clobber-list.
14025
14026 asm-clobber-list:
14027 string-literal
14028 asm-clobber-list , string-literal
14029
14030 Returns a TREE_LIST, indicating the clobbers in the order that they
14031 appeared. The TREE_VALUE of each node is a STRING_CST. */
14032
14033 static tree
14034 cp_parser_asm_clobber_list (cp_parser* parser)
14035 {
14036 tree clobbers = NULL_TREE;
14037
14038 while (true)
14039 {
14040 tree string_literal;
14041
14042 /* Look for the string literal. */
14043 string_literal = cp_parser_string_literal (parser, false, false);
14044 /* Add it to the list. */
14045 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14046 /* If the next token is not a `,', then the list is
14047 complete. */
14048 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14049 break;
14050 /* Consume the `,' token. */
14051 cp_lexer_consume_token (parser->lexer);
14052 }
14053
14054 return clobbers;
14055 }
14056
14057 /* Parse an (optional) series of attributes.
14058
14059 attributes:
14060 attributes attribute
14061
14062 attribute:
14063 __attribute__ (( attribute-list [opt] ))
14064
14065 The return value is as for cp_parser_attribute_list. */
14066
14067 static tree
14068 cp_parser_attributes_opt (cp_parser* parser)
14069 {
14070 tree attributes = NULL_TREE;
14071
14072 while (true)
14073 {
14074 cp_token *token;
14075 tree attribute_list;
14076
14077 /* Peek at the next token. */
14078 token = cp_lexer_peek_token (parser->lexer);
14079 /* If it's not `__attribute__', then we're done. */
14080 if (token->keyword != RID_ATTRIBUTE)
14081 break;
14082
14083 /* Consume the `__attribute__' keyword. */
14084 cp_lexer_consume_token (parser->lexer);
14085 /* Look for the two `(' tokens. */
14086 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14087 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14088
14089 /* Peek at the next token. */
14090 token = cp_lexer_peek_token (parser->lexer);
14091 if (token->type != CPP_CLOSE_PAREN)
14092 /* Parse the attribute-list. */
14093 attribute_list = cp_parser_attribute_list (parser);
14094 else
14095 /* If the next token is a `)', then there is no attribute
14096 list. */
14097 attribute_list = NULL;
14098
14099 /* Look for the two `)' tokens. */
14100 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14101 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14102
14103 /* Add these new attributes to the list. */
14104 attributes = chainon (attributes, attribute_list);
14105 }
14106
14107 return attributes;
14108 }
14109
14110 /* Parse an attribute-list.
14111
14112 attribute-list:
14113 attribute
14114 attribute-list , attribute
14115
14116 attribute:
14117 identifier
14118 identifier ( identifier )
14119 identifier ( identifier , expression-list )
14120 identifier ( expression-list )
14121
14122 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14123 to an attribute. The TREE_PURPOSE of each node is the identifier
14124 indicating which attribute is in use. The TREE_VALUE represents
14125 the arguments, if any. */
14126
14127 static tree
14128 cp_parser_attribute_list (cp_parser* parser)
14129 {
14130 tree attribute_list = NULL_TREE;
14131 bool save_translate_strings_p = parser->translate_strings_p;
14132
14133 parser->translate_strings_p = false;
14134 while (true)
14135 {
14136 cp_token *token;
14137 tree identifier;
14138 tree attribute;
14139
14140 /* Look for the identifier. We also allow keywords here; for
14141 example `__attribute__ ((const))' is legal. */
14142 token = cp_lexer_peek_token (parser->lexer);
14143 if (token->type == CPP_NAME
14144 || token->type == CPP_KEYWORD)
14145 {
14146 /* Consume the token. */
14147 token = cp_lexer_consume_token (parser->lexer);
14148
14149 /* Save away the identifier that indicates which attribute
14150 this is. */
14151 identifier = token->value;
14152 attribute = build_tree_list (identifier, NULL_TREE);
14153
14154 /* Peek at the next token. */
14155 token = cp_lexer_peek_token (parser->lexer);
14156 /* If it's an `(', then parse the attribute arguments. */
14157 if (token->type == CPP_OPEN_PAREN)
14158 {
14159 tree arguments;
14160
14161 arguments = (cp_parser_parenthesized_expression_list
14162 (parser, true, /*cast_p=*/false,
14163 /*non_constant_p=*/NULL));
14164 /* Save the identifier and arguments away. */
14165 TREE_VALUE (attribute) = arguments;
14166 }
14167
14168 /* Add this attribute to the list. */
14169 TREE_CHAIN (attribute) = attribute_list;
14170 attribute_list = attribute;
14171
14172 token = cp_lexer_peek_token (parser->lexer);
14173 }
14174 /* Now, look for more attributes. If the next token isn't a
14175 `,', we're done. */
14176 if (token->type != CPP_COMMA)
14177 break;
14178
14179 /* Consume the comma and keep going. */
14180 cp_lexer_consume_token (parser->lexer);
14181 }
14182 parser->translate_strings_p = save_translate_strings_p;
14183
14184 /* We built up the list in reverse order. */
14185 return nreverse (attribute_list);
14186 }
14187
14188 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14189 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14190 current value of the PEDANTIC flag, regardless of whether or not
14191 the `__extension__' keyword is present. The caller is responsible
14192 for restoring the value of the PEDANTIC flag. */
14193
14194 static bool
14195 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14196 {
14197 /* Save the old value of the PEDANTIC flag. */
14198 *saved_pedantic = pedantic;
14199
14200 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14201 {
14202 /* Consume the `__extension__' token. */
14203 cp_lexer_consume_token (parser->lexer);
14204 /* We're not being pedantic while the `__extension__' keyword is
14205 in effect. */
14206 pedantic = 0;
14207
14208 return true;
14209 }
14210
14211 return false;
14212 }
14213
14214 /* Parse a label declaration.
14215
14216 label-declaration:
14217 __label__ label-declarator-seq ;
14218
14219 label-declarator-seq:
14220 identifier , label-declarator-seq
14221 identifier */
14222
14223 static void
14224 cp_parser_label_declaration (cp_parser* parser)
14225 {
14226 /* Look for the `__label__' keyword. */
14227 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14228
14229 while (true)
14230 {
14231 tree identifier;
14232
14233 /* Look for an identifier. */
14234 identifier = cp_parser_identifier (parser);
14235 /* Declare it as a lobel. */
14236 finish_label_decl (identifier);
14237 /* If the next token is a `;', stop. */
14238 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14239 break;
14240 /* Look for the `,' separating the label declarations. */
14241 cp_parser_require (parser, CPP_COMMA, "`,'");
14242 }
14243
14244 /* Look for the final `;'. */
14245 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14246 }
14247
14248 /* Support Functions */
14249
14250 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14251 NAME should have one of the representations used for an
14252 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14253 is returned. If PARSER->SCOPE is a dependent type, then a
14254 SCOPE_REF is returned.
14255
14256 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14257 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14258 was formed. Abstractly, such entities should not be passed to this
14259 function, because they do not need to be looked up, but it is
14260 simpler to check for this special case here, rather than at the
14261 call-sites.
14262
14263 In cases not explicitly covered above, this function returns a
14264 DECL, OVERLOAD, or baselink representing the result of the lookup.
14265 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14266 is returned.
14267
14268 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14269 (e.g., "struct") that was used. In that case bindings that do not
14270 refer to types are ignored.
14271
14272 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14273 ignored.
14274
14275 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14276 are ignored.
14277
14278 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14279 types.
14280
14281 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14282 results in an ambiguity, and false otherwise. */
14283
14284 static tree
14285 cp_parser_lookup_name (cp_parser *parser, tree name,
14286 enum tag_types tag_type,
14287 bool is_template, bool is_namespace,
14288 bool check_dependency,
14289 bool *ambiguous_p)
14290 {
14291 tree decl;
14292 tree object_type = parser->context->object_type;
14293
14294 /* Assume that the lookup will be unambiguous. */
14295 if (ambiguous_p)
14296 *ambiguous_p = false;
14297
14298 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14299 no longer valid. Note that if we are parsing tentatively, and
14300 the parse fails, OBJECT_TYPE will be automatically restored. */
14301 parser->context->object_type = NULL_TREE;
14302
14303 if (name == error_mark_node)
14304 return error_mark_node;
14305
14306 /* A template-id has already been resolved; there is no lookup to
14307 do. */
14308 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14309 return name;
14310 if (BASELINK_P (name))
14311 {
14312 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14313 == TEMPLATE_ID_EXPR);
14314 return name;
14315 }
14316
14317 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14318 it should already have been checked to make sure that the name
14319 used matches the type being destroyed. */
14320 if (TREE_CODE (name) == BIT_NOT_EXPR)
14321 {
14322 tree type;
14323
14324 /* Figure out to which type this destructor applies. */
14325 if (parser->scope)
14326 type = parser->scope;
14327 else if (object_type)
14328 type = object_type;
14329 else
14330 type = current_class_type;
14331 /* If that's not a class type, there is no destructor. */
14332 if (!type || !CLASS_TYPE_P (type))
14333 return error_mark_node;
14334 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14335 lazily_declare_fn (sfk_destructor, type);
14336 if (!CLASSTYPE_DESTRUCTORS (type))
14337 return error_mark_node;
14338 /* If it was a class type, return the destructor. */
14339 return CLASSTYPE_DESTRUCTORS (type);
14340 }
14341
14342 /* By this point, the NAME should be an ordinary identifier. If
14343 the id-expression was a qualified name, the qualifying scope is
14344 stored in PARSER->SCOPE at this point. */
14345 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14346
14347 /* Perform the lookup. */
14348 if (parser->scope)
14349 {
14350 bool dependent_p;
14351
14352 if (parser->scope == error_mark_node)
14353 return error_mark_node;
14354
14355 /* If the SCOPE is dependent, the lookup must be deferred until
14356 the template is instantiated -- unless we are explicitly
14357 looking up names in uninstantiated templates. Even then, we
14358 cannot look up the name if the scope is not a class type; it
14359 might, for example, be a template type parameter. */
14360 dependent_p = (TYPE_P (parser->scope)
14361 && !(parser->in_declarator_p
14362 && currently_open_class (parser->scope))
14363 && dependent_type_p (parser->scope));
14364 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14365 && dependent_p)
14366 {
14367 if (tag_type)
14368 {
14369 tree type;
14370
14371 /* The resolution to Core Issue 180 says that `struct
14372 A::B' should be considered a type-name, even if `A'
14373 is dependent. */
14374 type = make_typename_type (parser->scope, name, tag_type,
14375 /*complain=*/1);
14376 decl = TYPE_NAME (type);
14377 }
14378 else if (is_template)
14379 decl = make_unbound_class_template (parser->scope,
14380 name, NULL_TREE,
14381 /*complain=*/1);
14382 else
14383 decl = build_nt (SCOPE_REF, parser->scope, name);
14384 }
14385 else
14386 {
14387 tree pushed_scope = NULL_TREE;
14388
14389 /* If PARSER->SCOPE is a dependent type, then it must be a
14390 class type, and we must not be checking dependencies;
14391 otherwise, we would have processed this lookup above. So
14392 that PARSER->SCOPE is not considered a dependent base by
14393 lookup_member, we must enter the scope here. */
14394 if (dependent_p)
14395 pushed_scope = push_scope (parser->scope);
14396 /* If the PARSER->SCOPE is a template specialization, it
14397 may be instantiated during name lookup. In that case,
14398 errors may be issued. Even if we rollback the current
14399 tentative parse, those errors are valid. */
14400 decl = lookup_qualified_name (parser->scope, name,
14401 tag_type != none_type,
14402 /*complain=*/true);
14403 if (pushed_scope)
14404 pop_scope (pushed_scope);
14405 }
14406 parser->qualifying_scope = parser->scope;
14407 parser->object_scope = NULL_TREE;
14408 }
14409 else if (object_type)
14410 {
14411 tree object_decl = NULL_TREE;
14412 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14413 OBJECT_TYPE is not a class. */
14414 if (CLASS_TYPE_P (object_type))
14415 /* If the OBJECT_TYPE is a template specialization, it may
14416 be instantiated during name lookup. In that case, errors
14417 may be issued. Even if we rollback the current tentative
14418 parse, those errors are valid. */
14419 object_decl = lookup_member (object_type,
14420 name,
14421 /*protect=*/0,
14422 tag_type != none_type);
14423 /* Look it up in the enclosing context, too. */
14424 decl = lookup_name_real (name, tag_type != none_type,
14425 /*nonclass=*/0,
14426 /*block_p=*/true, is_namespace,
14427 /*flags=*/0);
14428 parser->object_scope = object_type;
14429 parser->qualifying_scope = NULL_TREE;
14430 if (object_decl)
14431 decl = object_decl;
14432 }
14433 else
14434 {
14435 decl = lookup_name_real (name, tag_type != none_type,
14436 /*nonclass=*/0,
14437 /*block_p=*/true, is_namespace,
14438 /*flags=*/0);
14439 parser->qualifying_scope = NULL_TREE;
14440 parser->object_scope = NULL_TREE;
14441 }
14442
14443 /* If the lookup failed, let our caller know. */
14444 if (!decl || decl == error_mark_node)
14445 return error_mark_node;
14446
14447 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14448 if (TREE_CODE (decl) == TREE_LIST)
14449 {
14450 if (ambiguous_p)
14451 *ambiguous_p = true;
14452 /* The error message we have to print is too complicated for
14453 cp_parser_error, so we incorporate its actions directly. */
14454 if (!cp_parser_simulate_error (parser))
14455 {
14456 error ("reference to %qD is ambiguous", name);
14457 print_candidates (decl);
14458 }
14459 return error_mark_node;
14460 }
14461
14462 gcc_assert (DECL_P (decl)
14463 || TREE_CODE (decl) == OVERLOAD
14464 || TREE_CODE (decl) == SCOPE_REF
14465 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14466 || BASELINK_P (decl));
14467
14468 /* If we have resolved the name of a member declaration, check to
14469 see if the declaration is accessible. When the name resolves to
14470 set of overloaded functions, accessibility is checked when
14471 overload resolution is done.
14472
14473 During an explicit instantiation, access is not checked at all,
14474 as per [temp.explicit]. */
14475 if (DECL_P (decl))
14476 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14477
14478 return decl;
14479 }
14480
14481 /* Like cp_parser_lookup_name, but for use in the typical case where
14482 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14483 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14484
14485 static tree
14486 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14487 {
14488 return cp_parser_lookup_name (parser, name,
14489 none_type,
14490 /*is_template=*/false,
14491 /*is_namespace=*/false,
14492 /*check_dependency=*/true,
14493 /*ambiguous_p=*/NULL);
14494 }
14495
14496 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14497 the current context, return the TYPE_DECL. If TAG_NAME_P is
14498 true, the DECL indicates the class being defined in a class-head,
14499 or declared in an elaborated-type-specifier.
14500
14501 Otherwise, return DECL. */
14502
14503 static tree
14504 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14505 {
14506 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14507 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14508
14509 struct A {
14510 template <typename T> struct B;
14511 };
14512
14513 template <typename T> struct A::B {};
14514
14515 Similarly, in a elaborated-type-specifier:
14516
14517 namespace N { struct X{}; }
14518
14519 struct A {
14520 template <typename T> friend struct N::X;
14521 };
14522
14523 However, if the DECL refers to a class type, and we are in
14524 the scope of the class, then the name lookup automatically
14525 finds the TYPE_DECL created by build_self_reference rather
14526 than a TEMPLATE_DECL. For example, in:
14527
14528 template <class T> struct S {
14529 S s;
14530 };
14531
14532 there is no need to handle such case. */
14533
14534 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14535 return DECL_TEMPLATE_RESULT (decl);
14536
14537 return decl;
14538 }
14539
14540 /* If too many, or too few, template-parameter lists apply to the
14541 declarator, issue an error message. Returns TRUE if all went well,
14542 and FALSE otherwise. */
14543
14544 static bool
14545 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14546 cp_declarator *declarator)
14547 {
14548 unsigned num_templates;
14549
14550 /* We haven't seen any classes that involve template parameters yet. */
14551 num_templates = 0;
14552
14553 switch (declarator->kind)
14554 {
14555 case cdk_id:
14556 if (declarator->u.id.qualifying_scope)
14557 {
14558 tree scope;
14559 tree member;
14560
14561 scope = declarator->u.id.qualifying_scope;
14562 member = declarator->u.id.unqualified_name;
14563
14564 while (scope && CLASS_TYPE_P (scope))
14565 {
14566 /* You're supposed to have one `template <...>'
14567 for every template class, but you don't need one
14568 for a full specialization. For example:
14569
14570 template <class T> struct S{};
14571 template <> struct S<int> { void f(); };
14572 void S<int>::f () {}
14573
14574 is correct; there shouldn't be a `template <>' for
14575 the definition of `S<int>::f'. */
14576 if (CLASSTYPE_TEMPLATE_INFO (scope)
14577 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14578 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14579 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14580 ++num_templates;
14581
14582 scope = TYPE_CONTEXT (scope);
14583 }
14584 }
14585 else if (TREE_CODE (declarator->u.id.unqualified_name)
14586 == TEMPLATE_ID_EXPR)
14587 /* If the DECLARATOR has the form `X<y>' then it uses one
14588 additional level of template parameters. */
14589 ++num_templates;
14590
14591 return cp_parser_check_template_parameters (parser,
14592 num_templates);
14593
14594 case cdk_function:
14595 case cdk_array:
14596 case cdk_pointer:
14597 case cdk_reference:
14598 case cdk_ptrmem:
14599 return (cp_parser_check_declarator_template_parameters
14600 (parser, declarator->declarator));
14601
14602 case cdk_error:
14603 return true;
14604
14605 default:
14606 gcc_unreachable ();
14607 }
14608 return false;
14609 }
14610
14611 /* NUM_TEMPLATES were used in the current declaration. If that is
14612 invalid, return FALSE and issue an error messages. Otherwise,
14613 return TRUE. */
14614
14615 static bool
14616 cp_parser_check_template_parameters (cp_parser* parser,
14617 unsigned num_templates)
14618 {
14619 /* If there are more template classes than parameter lists, we have
14620 something like:
14621
14622 template <class T> void S<T>::R<T>::f (); */
14623 if (parser->num_template_parameter_lists < num_templates)
14624 {
14625 error ("too few template-parameter-lists");
14626 return false;
14627 }
14628 /* If there are the same number of template classes and parameter
14629 lists, that's OK. */
14630 if (parser->num_template_parameter_lists == num_templates)
14631 return true;
14632 /* If there are more, but only one more, then we are referring to a
14633 member template. That's OK too. */
14634 if (parser->num_template_parameter_lists == num_templates + 1)
14635 return true;
14636 /* Otherwise, there are too many template parameter lists. We have
14637 something like:
14638
14639 template <class T> template <class U> void S::f(); */
14640 error ("too many template-parameter-lists");
14641 return false;
14642 }
14643
14644 /* Parse an optional `::' token indicating that the following name is
14645 from the global namespace. If so, PARSER->SCOPE is set to the
14646 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14647 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14648 Returns the new value of PARSER->SCOPE, if the `::' token is
14649 present, and NULL_TREE otherwise. */
14650
14651 static tree
14652 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14653 {
14654 cp_token *token;
14655
14656 /* Peek at the next token. */
14657 token = cp_lexer_peek_token (parser->lexer);
14658 /* If we're looking at a `::' token then we're starting from the
14659 global namespace, not our current location. */
14660 if (token->type == CPP_SCOPE)
14661 {
14662 /* Consume the `::' token. */
14663 cp_lexer_consume_token (parser->lexer);
14664 /* Set the SCOPE so that we know where to start the lookup. */
14665 parser->scope = global_namespace;
14666 parser->qualifying_scope = global_namespace;
14667 parser->object_scope = NULL_TREE;
14668
14669 return parser->scope;
14670 }
14671 else if (!current_scope_valid_p)
14672 {
14673 parser->scope = NULL_TREE;
14674 parser->qualifying_scope = NULL_TREE;
14675 parser->object_scope = NULL_TREE;
14676 }
14677
14678 return NULL_TREE;
14679 }
14680
14681 /* Returns TRUE if the upcoming token sequence is the start of a
14682 constructor declarator. If FRIEND_P is true, the declarator is
14683 preceded by the `friend' specifier. */
14684
14685 static bool
14686 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14687 {
14688 bool constructor_p;
14689 tree type_decl = NULL_TREE;
14690 bool nested_name_p;
14691 cp_token *next_token;
14692
14693 /* The common case is that this is not a constructor declarator, so
14694 try to avoid doing lots of work if at all possible. It's not
14695 valid declare a constructor at function scope. */
14696 if (at_function_scope_p ())
14697 return false;
14698 /* And only certain tokens can begin a constructor declarator. */
14699 next_token = cp_lexer_peek_token (parser->lexer);
14700 if (next_token->type != CPP_NAME
14701 && next_token->type != CPP_SCOPE
14702 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14703 && next_token->type != CPP_TEMPLATE_ID)
14704 return false;
14705
14706 /* Parse tentatively; we are going to roll back all of the tokens
14707 consumed here. */
14708 cp_parser_parse_tentatively (parser);
14709 /* Assume that we are looking at a constructor declarator. */
14710 constructor_p = true;
14711
14712 /* Look for the optional `::' operator. */
14713 cp_parser_global_scope_opt (parser,
14714 /*current_scope_valid_p=*/false);
14715 /* Look for the nested-name-specifier. */
14716 nested_name_p
14717 = (cp_parser_nested_name_specifier_opt (parser,
14718 /*typename_keyword_p=*/false,
14719 /*check_dependency_p=*/false,
14720 /*type_p=*/false,
14721 /*is_declaration=*/false)
14722 != NULL_TREE);
14723 /* Outside of a class-specifier, there must be a
14724 nested-name-specifier. */
14725 if (!nested_name_p &&
14726 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14727 || friend_p))
14728 constructor_p = false;
14729 /* If we still think that this might be a constructor-declarator,
14730 look for a class-name. */
14731 if (constructor_p)
14732 {
14733 /* If we have:
14734
14735 template <typename T> struct S { S(); };
14736 template <typename T> S<T>::S ();
14737
14738 we must recognize that the nested `S' names a class.
14739 Similarly, for:
14740
14741 template <typename T> S<T>::S<T> ();
14742
14743 we must recognize that the nested `S' names a template. */
14744 type_decl = cp_parser_class_name (parser,
14745 /*typename_keyword_p=*/false,
14746 /*template_keyword_p=*/false,
14747 none_type,
14748 /*check_dependency_p=*/false,
14749 /*class_head_p=*/false,
14750 /*is_declaration=*/false);
14751 /* If there was no class-name, then this is not a constructor. */
14752 constructor_p = !cp_parser_error_occurred (parser);
14753 }
14754
14755 /* If we're still considering a constructor, we have to see a `(',
14756 to begin the parameter-declaration-clause, followed by either a
14757 `)', an `...', or a decl-specifier. We need to check for a
14758 type-specifier to avoid being fooled into thinking that:
14759
14760 S::S (f) (int);
14761
14762 is a constructor. (It is actually a function named `f' that
14763 takes one parameter (of type `int') and returns a value of type
14764 `S::S'. */
14765 if (constructor_p
14766 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14767 {
14768 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14769 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14770 /* A parameter declaration begins with a decl-specifier,
14771 which is either the "attribute" keyword, a storage class
14772 specifier, or (usually) a type-specifier. */
14773 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14774 && !cp_parser_storage_class_specifier_opt (parser))
14775 {
14776 tree type;
14777 tree pushed_scope = NULL_TREE;
14778 unsigned saved_num_template_parameter_lists;
14779
14780 /* Names appearing in the type-specifier should be looked up
14781 in the scope of the class. */
14782 if (current_class_type)
14783 type = NULL_TREE;
14784 else
14785 {
14786 type = TREE_TYPE (type_decl);
14787 if (TREE_CODE (type) == TYPENAME_TYPE)
14788 {
14789 type = resolve_typename_type (type,
14790 /*only_current_p=*/false);
14791 if (type == error_mark_node)
14792 {
14793 cp_parser_abort_tentative_parse (parser);
14794 return false;
14795 }
14796 }
14797 pushed_scope = push_scope (type);
14798 }
14799
14800 /* Inside the constructor parameter list, surrounding
14801 template-parameter-lists do not apply. */
14802 saved_num_template_parameter_lists
14803 = parser->num_template_parameter_lists;
14804 parser->num_template_parameter_lists = 0;
14805
14806 /* Look for the type-specifier. */
14807 cp_parser_type_specifier (parser,
14808 CP_PARSER_FLAGS_NONE,
14809 /*decl_specs=*/NULL,
14810 /*is_declarator=*/true,
14811 /*declares_class_or_enum=*/NULL,
14812 /*is_cv_qualifier=*/NULL);
14813
14814 parser->num_template_parameter_lists
14815 = saved_num_template_parameter_lists;
14816
14817 /* Leave the scope of the class. */
14818 if (pushed_scope)
14819 pop_scope (pushed_scope);
14820
14821 constructor_p = !cp_parser_error_occurred (parser);
14822 }
14823 }
14824 else
14825 constructor_p = false;
14826 /* We did not really want to consume any tokens. */
14827 cp_parser_abort_tentative_parse (parser);
14828
14829 return constructor_p;
14830 }
14831
14832 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14833 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14834 they must be performed once we are in the scope of the function.
14835
14836 Returns the function defined. */
14837
14838 static tree
14839 cp_parser_function_definition_from_specifiers_and_declarator
14840 (cp_parser* parser,
14841 cp_decl_specifier_seq *decl_specifiers,
14842 tree attributes,
14843 const cp_declarator *declarator)
14844 {
14845 tree fn;
14846 bool success_p;
14847
14848 /* Begin the function-definition. */
14849 success_p = start_function (decl_specifiers, declarator, attributes);
14850
14851 /* The things we're about to see are not directly qualified by any
14852 template headers we've seen thus far. */
14853 reset_specialization ();
14854
14855 /* If there were names looked up in the decl-specifier-seq that we
14856 did not check, check them now. We must wait until we are in the
14857 scope of the function to perform the checks, since the function
14858 might be a friend. */
14859 perform_deferred_access_checks ();
14860
14861 if (!success_p)
14862 {
14863 /* Skip the entire function. */
14864 error ("invalid function declaration");
14865 cp_parser_skip_to_end_of_block_or_statement (parser);
14866 fn = error_mark_node;
14867 }
14868 else
14869 fn = cp_parser_function_definition_after_declarator (parser,
14870 /*inline_p=*/false);
14871
14872 return fn;
14873 }
14874
14875 /* Parse the part of a function-definition that follows the
14876 declarator. INLINE_P is TRUE iff this function is an inline
14877 function defined with a class-specifier.
14878
14879 Returns the function defined. */
14880
14881 static tree
14882 cp_parser_function_definition_after_declarator (cp_parser* parser,
14883 bool inline_p)
14884 {
14885 tree fn;
14886 bool ctor_initializer_p = false;
14887 bool saved_in_unbraced_linkage_specification_p;
14888 unsigned saved_num_template_parameter_lists;
14889
14890 /* If the next token is `return', then the code may be trying to
14891 make use of the "named return value" extension that G++ used to
14892 support. */
14893 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14894 {
14895 /* Consume the `return' keyword. */
14896 cp_lexer_consume_token (parser->lexer);
14897 /* Look for the identifier that indicates what value is to be
14898 returned. */
14899 cp_parser_identifier (parser);
14900 /* Issue an error message. */
14901 error ("named return values are no longer supported");
14902 /* Skip tokens until we reach the start of the function body. */
14903 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14904 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14905 cp_lexer_consume_token (parser->lexer);
14906 }
14907 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14908 anything declared inside `f'. */
14909 saved_in_unbraced_linkage_specification_p
14910 = parser->in_unbraced_linkage_specification_p;
14911 parser->in_unbraced_linkage_specification_p = false;
14912 /* Inside the function, surrounding template-parameter-lists do not
14913 apply. */
14914 saved_num_template_parameter_lists
14915 = parser->num_template_parameter_lists;
14916 parser->num_template_parameter_lists = 0;
14917 /* If the next token is `try', then we are looking at a
14918 function-try-block. */
14919 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14920 ctor_initializer_p = cp_parser_function_try_block (parser);
14921 /* A function-try-block includes the function-body, so we only do
14922 this next part if we're not processing a function-try-block. */
14923 else
14924 ctor_initializer_p
14925 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14926
14927 /* Finish the function. */
14928 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14929 (inline_p ? 2 : 0));
14930 /* Generate code for it, if necessary. */
14931 expand_or_defer_fn (fn);
14932 /* Restore the saved values. */
14933 parser->in_unbraced_linkage_specification_p
14934 = saved_in_unbraced_linkage_specification_p;
14935 parser->num_template_parameter_lists
14936 = saved_num_template_parameter_lists;
14937
14938 return fn;
14939 }
14940
14941 /* Parse a template-declaration, assuming that the `export' (and
14942 `extern') keywords, if present, has already been scanned. MEMBER_P
14943 is as for cp_parser_template_declaration. */
14944
14945 static void
14946 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14947 {
14948 tree decl = NULL_TREE;
14949 tree parameter_list;
14950 bool friend_p = false;
14951
14952 /* Look for the `template' keyword. */
14953 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14954 return;
14955
14956 /* And the `<'. */
14957 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14958 return;
14959
14960 /* If the next token is `>', then we have an invalid
14961 specialization. Rather than complain about an invalid template
14962 parameter, issue an error message here. */
14963 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14964 {
14965 cp_parser_error (parser, "invalid explicit specialization");
14966 begin_specialization ();
14967 parameter_list = NULL_TREE;
14968 }
14969 else
14970 {
14971 /* Parse the template parameters. */
14972 begin_template_parm_list ();
14973 parameter_list = cp_parser_template_parameter_list (parser);
14974 parameter_list = end_template_parm_list (parameter_list);
14975 }
14976
14977 /* Look for the `>'. */
14978 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14979 /* We just processed one more parameter list. */
14980 ++parser->num_template_parameter_lists;
14981 /* If the next token is `template', there are more template
14982 parameters. */
14983 if (cp_lexer_next_token_is_keyword (parser->lexer,
14984 RID_TEMPLATE))
14985 cp_parser_template_declaration_after_export (parser, member_p);
14986 else
14987 {
14988 /* There are no access checks when parsing a template, as we do not
14989 know if a specialization will be a friend. */
14990 push_deferring_access_checks (dk_no_check);
14991
14992 decl = cp_parser_single_declaration (parser,
14993 member_p,
14994 &friend_p);
14995
14996 pop_deferring_access_checks ();
14997
14998 /* If this is a member template declaration, let the front
14999 end know. */
15000 if (member_p && !friend_p && decl)
15001 {
15002 if (TREE_CODE (decl) == TYPE_DECL)
15003 cp_parser_check_access_in_redeclaration (decl);
15004
15005 decl = finish_member_template_decl (decl);
15006 }
15007 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15008 make_friend_class (current_class_type, TREE_TYPE (decl),
15009 /*complain=*/true);
15010 }
15011 /* We are done with the current parameter list. */
15012 --parser->num_template_parameter_lists;
15013
15014 /* Finish up. */
15015 finish_template_decl (parameter_list);
15016
15017 /* Register member declarations. */
15018 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15019 finish_member_declaration (decl);
15020
15021 /* If DECL is a function template, we must return to parse it later.
15022 (Even though there is no definition, there might be default
15023 arguments that need handling.) */
15024 if (member_p && decl
15025 && (TREE_CODE (decl) == FUNCTION_DECL
15026 || DECL_FUNCTION_TEMPLATE_P (decl)))
15027 TREE_VALUE (parser->unparsed_functions_queues)
15028 = tree_cons (NULL_TREE, decl,
15029 TREE_VALUE (parser->unparsed_functions_queues));
15030 }
15031
15032 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15033 `function-definition' sequence. MEMBER_P is true, this declaration
15034 appears in a class scope.
15035
15036 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15037 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15038
15039 static tree
15040 cp_parser_single_declaration (cp_parser* parser,
15041 bool member_p,
15042 bool* friend_p)
15043 {
15044 int declares_class_or_enum;
15045 tree decl = NULL_TREE;
15046 cp_decl_specifier_seq decl_specifiers;
15047 bool function_definition_p = false;
15048
15049 /* This function is only used when processing a template
15050 declaration. */
15051 gcc_assert (innermost_scope_kind () == sk_template_parms
15052 || innermost_scope_kind () == sk_template_spec);
15053
15054 /* Defer access checks until we know what is being declared. */
15055 push_deferring_access_checks (dk_deferred);
15056
15057 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15058 alternative. */
15059 cp_parser_decl_specifier_seq (parser,
15060 CP_PARSER_FLAGS_OPTIONAL,
15061 &decl_specifiers,
15062 &declares_class_or_enum);
15063 if (friend_p)
15064 *friend_p = cp_parser_friend_p (&decl_specifiers);
15065
15066 /* There are no template typedefs. */
15067 if (decl_specifiers.specs[(int) ds_typedef])
15068 {
15069 error ("template declaration of %qs", "typedef");
15070 decl = error_mark_node;
15071 }
15072
15073 /* Gather up the access checks that occurred the
15074 decl-specifier-seq. */
15075 stop_deferring_access_checks ();
15076
15077 /* Check for the declaration of a template class. */
15078 if (declares_class_or_enum)
15079 {
15080 if (cp_parser_declares_only_class_p (parser))
15081 {
15082 decl = shadow_tag (&decl_specifiers);
15083
15084 /* In this case:
15085
15086 struct C {
15087 friend template <typename T> struct A<T>::B;
15088 };
15089
15090 A<T>::B will be represented by a TYPENAME_TYPE, and
15091 therefore not recognized by shadow_tag. */
15092 if (friend_p && *friend_p
15093 && !decl
15094 && decl_specifiers.type
15095 && TYPE_P (decl_specifiers.type))
15096 decl = decl_specifiers.type;
15097
15098 if (decl && decl != error_mark_node)
15099 decl = TYPE_NAME (decl);
15100 else
15101 decl = error_mark_node;
15102 }
15103 }
15104 /* If it's not a template class, try for a template function. If
15105 the next token is a `;', then this declaration does not declare
15106 anything. But, if there were errors in the decl-specifiers, then
15107 the error might well have come from an attempted class-specifier.
15108 In that case, there's no need to warn about a missing declarator. */
15109 if (!decl
15110 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15111 || decl_specifiers.type != error_mark_node))
15112 decl = cp_parser_init_declarator (parser,
15113 &decl_specifiers,
15114 /*function_definition_allowed_p=*/true,
15115 member_p,
15116 declares_class_or_enum,
15117 &function_definition_p);
15118
15119 pop_deferring_access_checks ();
15120
15121 /* Clear any current qualification; whatever comes next is the start
15122 of something new. */
15123 parser->scope = NULL_TREE;
15124 parser->qualifying_scope = NULL_TREE;
15125 parser->object_scope = NULL_TREE;
15126 /* Look for a trailing `;' after the declaration. */
15127 if (!function_definition_p
15128 && (decl == error_mark_node
15129 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15130 cp_parser_skip_to_end_of_block_or_statement (parser);
15131
15132 return decl;
15133 }
15134
15135 /* Parse a cast-expression that is not the operand of a unary "&". */
15136
15137 static tree
15138 cp_parser_simple_cast_expression (cp_parser *parser)
15139 {
15140 return cp_parser_cast_expression (parser, /*address_p=*/false,
15141 /*cast_p=*/false);
15142 }
15143
15144 /* Parse a functional cast to TYPE. Returns an expression
15145 representing the cast. */
15146
15147 static tree
15148 cp_parser_functional_cast (cp_parser* parser, tree type)
15149 {
15150 tree expression_list;
15151 tree cast;
15152
15153 expression_list
15154 = cp_parser_parenthesized_expression_list (parser, false,
15155 /*cast_p=*/true,
15156 /*non_constant_p=*/NULL);
15157
15158 cast = build_functional_cast (type, expression_list);
15159 /* [expr.const]/1: In an integral constant expression "only type
15160 conversions to integral or enumeration type can be used". */
15161 if (cast != error_mark_node && !type_dependent_expression_p (type)
15162 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15163 {
15164 if (cp_parser_non_integral_constant_expression
15165 (parser, "a call to a constructor"))
15166 return error_mark_node;
15167 }
15168 return cast;
15169 }
15170
15171 /* Save the tokens that make up the body of a member function defined
15172 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15173 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15174 specifiers applied to the declaration. Returns the FUNCTION_DECL
15175 for the member function. */
15176
15177 static tree
15178 cp_parser_save_member_function_body (cp_parser* parser,
15179 cp_decl_specifier_seq *decl_specifiers,
15180 cp_declarator *declarator,
15181 tree attributes)
15182 {
15183 cp_token *first;
15184 cp_token *last;
15185 tree fn;
15186
15187 /* Create the function-declaration. */
15188 fn = start_method (decl_specifiers, declarator, attributes);
15189 /* If something went badly wrong, bail out now. */
15190 if (fn == error_mark_node)
15191 {
15192 /* If there's a function-body, skip it. */
15193 if (cp_parser_token_starts_function_definition_p
15194 (cp_lexer_peek_token (parser->lexer)))
15195 cp_parser_skip_to_end_of_block_or_statement (parser);
15196 return error_mark_node;
15197 }
15198
15199 /* Remember it, if there default args to post process. */
15200 cp_parser_save_default_args (parser, fn);
15201
15202 /* Save away the tokens that make up the body of the
15203 function. */
15204 first = parser->lexer->next_token;
15205 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15206 /* Handle function try blocks. */
15207 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15208 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15209 last = parser->lexer->next_token;
15210
15211 /* Save away the inline definition; we will process it when the
15212 class is complete. */
15213 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15214 DECL_PENDING_INLINE_P (fn) = 1;
15215
15216 /* We need to know that this was defined in the class, so that
15217 friend templates are handled correctly. */
15218 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15219
15220 /* We're done with the inline definition. */
15221 finish_method (fn);
15222
15223 /* Add FN to the queue of functions to be parsed later. */
15224 TREE_VALUE (parser->unparsed_functions_queues)
15225 = tree_cons (NULL_TREE, fn,
15226 TREE_VALUE (parser->unparsed_functions_queues));
15227
15228 return fn;
15229 }
15230
15231 /* Parse a template-argument-list, as well as the trailing ">" (but
15232 not the opening ">"). See cp_parser_template_argument_list for the
15233 return value. */
15234
15235 static tree
15236 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15237 {
15238 tree arguments;
15239 tree saved_scope;
15240 tree saved_qualifying_scope;
15241 tree saved_object_scope;
15242 bool saved_greater_than_is_operator_p;
15243
15244 /* [temp.names]
15245
15246 When parsing a template-id, the first non-nested `>' is taken as
15247 the end of the template-argument-list rather than a greater-than
15248 operator. */
15249 saved_greater_than_is_operator_p
15250 = parser->greater_than_is_operator_p;
15251 parser->greater_than_is_operator_p = false;
15252 /* Parsing the argument list may modify SCOPE, so we save it
15253 here. */
15254 saved_scope = parser->scope;
15255 saved_qualifying_scope = parser->qualifying_scope;
15256 saved_object_scope = parser->object_scope;
15257 /* Parse the template-argument-list itself. */
15258 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15259 arguments = NULL_TREE;
15260 else
15261 arguments = cp_parser_template_argument_list (parser);
15262 /* Look for the `>' that ends the template-argument-list. If we find
15263 a '>>' instead, it's probably just a typo. */
15264 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15265 {
15266 if (!saved_greater_than_is_operator_p)
15267 {
15268 /* If we're in a nested template argument list, the '>>' has
15269 to be a typo for '> >'. We emit the error message, but we
15270 continue parsing and we push a '>' as next token, so that
15271 the argument list will be parsed correctly. Note that the
15272 global source location is still on the token before the
15273 '>>', so we need to say explicitly where we want it. */
15274 cp_token *token = cp_lexer_peek_token (parser->lexer);
15275 error ("%H%<>>%> should be %<> >%> "
15276 "within a nested template argument list",
15277 &token->location);
15278
15279 /* ??? Proper recovery should terminate two levels of
15280 template argument list here. */
15281 token->type = CPP_GREATER;
15282 }
15283 else
15284 {
15285 /* If this is not a nested template argument list, the '>>'
15286 is a typo for '>'. Emit an error message and continue.
15287 Same deal about the token location, but here we can get it
15288 right by consuming the '>>' before issuing the diagnostic. */
15289 cp_lexer_consume_token (parser->lexer);
15290 error ("spurious %<>>%>, use %<>%> to terminate "
15291 "a template argument list");
15292 }
15293 }
15294 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15295 error ("missing %<>%> to terminate the template argument list");
15296 else
15297 /* It's what we want, a '>'; consume it. */
15298 cp_lexer_consume_token (parser->lexer);
15299 /* The `>' token might be a greater-than operator again now. */
15300 parser->greater_than_is_operator_p
15301 = saved_greater_than_is_operator_p;
15302 /* Restore the SAVED_SCOPE. */
15303 parser->scope = saved_scope;
15304 parser->qualifying_scope = saved_qualifying_scope;
15305 parser->object_scope = saved_object_scope;
15306
15307 return arguments;
15308 }
15309
15310 /* MEMBER_FUNCTION is a member function, or a friend. If default
15311 arguments, or the body of the function have not yet been parsed,
15312 parse them now. */
15313
15314 static void
15315 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15316 {
15317 /* If this member is a template, get the underlying
15318 FUNCTION_DECL. */
15319 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15320 member_function = DECL_TEMPLATE_RESULT (member_function);
15321
15322 /* There should not be any class definitions in progress at this
15323 point; the bodies of members are only parsed outside of all class
15324 definitions. */
15325 gcc_assert (parser->num_classes_being_defined == 0);
15326 /* While we're parsing the member functions we might encounter more
15327 classes. We want to handle them right away, but we don't want
15328 them getting mixed up with functions that are currently in the
15329 queue. */
15330 parser->unparsed_functions_queues
15331 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15332
15333 /* Make sure that any template parameters are in scope. */
15334 maybe_begin_member_template_processing (member_function);
15335
15336 /* If the body of the function has not yet been parsed, parse it
15337 now. */
15338 if (DECL_PENDING_INLINE_P (member_function))
15339 {
15340 tree function_scope;
15341 cp_token_cache *tokens;
15342
15343 /* The function is no longer pending; we are processing it. */
15344 tokens = DECL_PENDING_INLINE_INFO (member_function);
15345 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15346 DECL_PENDING_INLINE_P (member_function) = 0;
15347
15348 /* If this is a local class, enter the scope of the containing
15349 function. */
15350 function_scope = current_function_decl;
15351 if (function_scope)
15352 push_function_context_to (function_scope);
15353
15354 /* Push the body of the function onto the lexer stack. */
15355 cp_parser_push_lexer_for_tokens (parser, tokens);
15356
15357 /* Let the front end know that we going to be defining this
15358 function. */
15359 start_preparsed_function (member_function, NULL_TREE,
15360 SF_PRE_PARSED | SF_INCLASS_INLINE);
15361
15362 /* Now, parse the body of the function. */
15363 cp_parser_function_definition_after_declarator (parser,
15364 /*inline_p=*/true);
15365
15366 /* Leave the scope of the containing function. */
15367 if (function_scope)
15368 pop_function_context_from (function_scope);
15369 cp_parser_pop_lexer (parser);
15370 }
15371
15372 /* Remove any template parameters from the symbol table. */
15373 maybe_end_member_template_processing ();
15374
15375 /* Restore the queue. */
15376 parser->unparsed_functions_queues
15377 = TREE_CHAIN (parser->unparsed_functions_queues);
15378 }
15379
15380 /* If DECL contains any default args, remember it on the unparsed
15381 functions queue. */
15382
15383 static void
15384 cp_parser_save_default_args (cp_parser* parser, tree decl)
15385 {
15386 tree probe;
15387
15388 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15389 probe;
15390 probe = TREE_CHAIN (probe))
15391 if (TREE_PURPOSE (probe))
15392 {
15393 TREE_PURPOSE (parser->unparsed_functions_queues)
15394 = tree_cons (current_class_type, decl,
15395 TREE_PURPOSE (parser->unparsed_functions_queues));
15396 break;
15397 }
15398 return;
15399 }
15400
15401 /* FN is a FUNCTION_DECL which may contains a parameter with an
15402 unparsed DEFAULT_ARG. Parse the default args now. This function
15403 assumes that the current scope is the scope in which the default
15404 argument should be processed. */
15405
15406 static void
15407 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15408 {
15409 bool saved_local_variables_forbidden_p;
15410 tree parm;
15411
15412 /* While we're parsing the default args, we might (due to the
15413 statement expression extension) encounter more classes. We want
15414 to handle them right away, but we don't want them getting mixed
15415 up with default args that are currently in the queue. */
15416 parser->unparsed_functions_queues
15417 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15418
15419 /* Local variable names (and the `this' keyword) may not appear
15420 in a default argument. */
15421 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15422 parser->local_variables_forbidden_p = true;
15423
15424 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15425 parm;
15426 parm = TREE_CHAIN (parm))
15427 {
15428 cp_token_cache *tokens;
15429
15430 if (!TREE_PURPOSE (parm)
15431 || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15432 continue;
15433
15434 /* Push the saved tokens for the default argument onto the parser's
15435 lexer stack. */
15436 tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15437 cp_parser_push_lexer_for_tokens (parser, tokens);
15438
15439 /* Parse the assignment-expression. */
15440 TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser,
15441 /*cast_p=*/false);
15442
15443 /* If the token stream has not been completely used up, then
15444 there was extra junk after the end of the default
15445 argument. */
15446 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15447 cp_parser_error (parser, "expected %<,%>");
15448
15449 /* Revert to the main lexer. */
15450 cp_parser_pop_lexer (parser);
15451 }
15452
15453 /* Restore the state of local_variables_forbidden_p. */
15454 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15455
15456 /* Restore the queue. */
15457 parser->unparsed_functions_queues
15458 = TREE_CHAIN (parser->unparsed_functions_queues);
15459 }
15460
15461 /* Parse the operand of `sizeof' (or a similar operator). Returns
15462 either a TYPE or an expression, depending on the form of the
15463 input. The KEYWORD indicates which kind of expression we have
15464 encountered. */
15465
15466 static tree
15467 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15468 {
15469 static const char *format;
15470 tree expr = NULL_TREE;
15471 const char *saved_message;
15472 bool saved_integral_constant_expression_p;
15473 bool saved_non_integral_constant_expression_p;
15474
15475 /* Initialize FORMAT the first time we get here. */
15476 if (!format)
15477 format = "types may not be defined in '%s' expressions";
15478
15479 /* Types cannot be defined in a `sizeof' expression. Save away the
15480 old message. */
15481 saved_message = parser->type_definition_forbidden_message;
15482 /* And create the new one. */
15483 parser->type_definition_forbidden_message
15484 = xmalloc (strlen (format)
15485 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15486 + 1 /* `\0' */);
15487 sprintf ((char *) parser->type_definition_forbidden_message,
15488 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15489
15490 /* The restrictions on constant-expressions do not apply inside
15491 sizeof expressions. */
15492 saved_integral_constant_expression_p
15493 = parser->integral_constant_expression_p;
15494 saved_non_integral_constant_expression_p
15495 = parser->non_integral_constant_expression_p;
15496 parser->integral_constant_expression_p = false;
15497
15498 /* Do not actually evaluate the expression. */
15499 ++skip_evaluation;
15500 /* If it's a `(', then we might be looking at the type-id
15501 construction. */
15502 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15503 {
15504 tree type;
15505 bool saved_in_type_id_in_expr_p;
15506
15507 /* We can't be sure yet whether we're looking at a type-id or an
15508 expression. */
15509 cp_parser_parse_tentatively (parser);
15510 /* Consume the `('. */
15511 cp_lexer_consume_token (parser->lexer);
15512 /* Parse the type-id. */
15513 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15514 parser->in_type_id_in_expr_p = true;
15515 type = cp_parser_type_id (parser);
15516 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15517 /* Now, look for the trailing `)'. */
15518 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15519 /* If all went well, then we're done. */
15520 if (cp_parser_parse_definitely (parser))
15521 {
15522 cp_decl_specifier_seq decl_specs;
15523
15524 /* Build a trivial decl-specifier-seq. */
15525 clear_decl_specs (&decl_specs);
15526 decl_specs.type = type;
15527
15528 /* Call grokdeclarator to figure out what type this is. */
15529 expr = grokdeclarator (NULL,
15530 &decl_specs,
15531 TYPENAME,
15532 /*initialized=*/0,
15533 /*attrlist=*/NULL);
15534 }
15535 }
15536
15537 /* If the type-id production did not work out, then we must be
15538 looking at the unary-expression production. */
15539 if (!expr)
15540 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15541 /*cast_p=*/false);
15542 /* Go back to evaluating expressions. */
15543 --skip_evaluation;
15544
15545 /* Free the message we created. */
15546 free ((char *) parser->type_definition_forbidden_message);
15547 /* And restore the old one. */
15548 parser->type_definition_forbidden_message = saved_message;
15549 parser->integral_constant_expression_p
15550 = saved_integral_constant_expression_p;
15551 parser->non_integral_constant_expression_p
15552 = saved_non_integral_constant_expression_p;
15553
15554 return expr;
15555 }
15556
15557 /* If the current declaration has no declarator, return true. */
15558
15559 static bool
15560 cp_parser_declares_only_class_p (cp_parser *parser)
15561 {
15562 /* If the next token is a `;' or a `,' then there is no
15563 declarator. */
15564 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15565 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15566 }
15567
15568 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15569
15570 static void
15571 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15572 cp_storage_class storage_class)
15573 {
15574 if (decl_specs->storage_class != sc_none)
15575 decl_specs->multiple_storage_classes_p = true;
15576 else
15577 decl_specs->storage_class = storage_class;
15578 }
15579
15580 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15581 is true, the type is a user-defined type; otherwise it is a
15582 built-in type specified by a keyword. */
15583
15584 static void
15585 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15586 tree type_spec,
15587 bool user_defined_p)
15588 {
15589 decl_specs->any_specifiers_p = true;
15590
15591 /* If the user tries to redeclare bool or wchar_t (with, for
15592 example, in "typedef int wchar_t;") we remember that this is what
15593 happened. In system headers, we ignore these declarations so
15594 that G++ can work with system headers that are not C++-safe. */
15595 if (decl_specs->specs[(int) ds_typedef]
15596 && !user_defined_p
15597 && (type_spec == boolean_type_node
15598 || type_spec == wchar_type_node)
15599 && (decl_specs->type
15600 || decl_specs->specs[(int) ds_long]
15601 || decl_specs->specs[(int) ds_short]
15602 || decl_specs->specs[(int) ds_unsigned]
15603 || decl_specs->specs[(int) ds_signed]))
15604 {
15605 decl_specs->redefined_builtin_type = type_spec;
15606 if (!decl_specs->type)
15607 {
15608 decl_specs->type = type_spec;
15609 decl_specs->user_defined_type_p = false;
15610 }
15611 }
15612 else if (decl_specs->type)
15613 decl_specs->multiple_types_p = true;
15614 else
15615 {
15616 decl_specs->type = type_spec;
15617 decl_specs->user_defined_type_p = user_defined_p;
15618 decl_specs->redefined_builtin_type = NULL_TREE;
15619 }
15620 }
15621
15622 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15623 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15624
15625 static bool
15626 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15627 {
15628 return decl_specifiers->specs[(int) ds_friend] != 0;
15629 }
15630
15631 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15632 issue an error message indicating that TOKEN_DESC was expected.
15633
15634 Returns the token consumed, if the token had the appropriate type.
15635 Otherwise, returns NULL. */
15636
15637 static cp_token *
15638 cp_parser_require (cp_parser* parser,
15639 enum cpp_ttype type,
15640 const char* token_desc)
15641 {
15642 if (cp_lexer_next_token_is (parser->lexer, type))
15643 return cp_lexer_consume_token (parser->lexer);
15644 else
15645 {
15646 /* Output the MESSAGE -- unless we're parsing tentatively. */
15647 if (!cp_parser_simulate_error (parser))
15648 {
15649 char *message = concat ("expected ", token_desc, NULL);
15650 cp_parser_error (parser, message);
15651 free (message);
15652 }
15653 return NULL;
15654 }
15655 }
15656
15657 /* Like cp_parser_require, except that tokens will be skipped until
15658 the desired token is found. An error message is still produced if
15659 the next token is not as expected. */
15660
15661 static void
15662 cp_parser_skip_until_found (cp_parser* parser,
15663 enum cpp_ttype type,
15664 const char* token_desc)
15665 {
15666 cp_token *token;
15667 unsigned nesting_depth = 0;
15668
15669 if (cp_parser_require (parser, type, token_desc))
15670 return;
15671
15672 /* Skip tokens until the desired token is found. */
15673 while (true)
15674 {
15675 /* Peek at the next token. */
15676 token = cp_lexer_peek_token (parser->lexer);
15677 /* If we've reached the token we want, consume it and
15678 stop. */
15679 if (token->type == type && !nesting_depth)
15680 {
15681 cp_lexer_consume_token (parser->lexer);
15682 return;
15683 }
15684 /* If we've run out of tokens, stop. */
15685 if (token->type == CPP_EOF)
15686 return;
15687 if (token->type == CPP_OPEN_BRACE
15688 || token->type == CPP_OPEN_PAREN
15689 || token->type == CPP_OPEN_SQUARE)
15690 ++nesting_depth;
15691 else if (token->type == CPP_CLOSE_BRACE
15692 || token->type == CPP_CLOSE_PAREN
15693 || token->type == CPP_CLOSE_SQUARE)
15694 {
15695 if (nesting_depth-- == 0)
15696 return;
15697 }
15698 /* Consume this token. */
15699 cp_lexer_consume_token (parser->lexer);
15700 }
15701 }
15702
15703 /* If the next token is the indicated keyword, consume it. Otherwise,
15704 issue an error message indicating that TOKEN_DESC was expected.
15705
15706 Returns the token consumed, if the token had the appropriate type.
15707 Otherwise, returns NULL. */
15708
15709 static cp_token *
15710 cp_parser_require_keyword (cp_parser* parser,
15711 enum rid keyword,
15712 const char* token_desc)
15713 {
15714 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15715
15716 if (token && token->keyword != keyword)
15717 {
15718 dyn_string_t error_msg;
15719
15720 /* Format the error message. */
15721 error_msg = dyn_string_new (0);
15722 dyn_string_append_cstr (error_msg, "expected ");
15723 dyn_string_append_cstr (error_msg, token_desc);
15724 cp_parser_error (parser, error_msg->s);
15725 dyn_string_delete (error_msg);
15726 return NULL;
15727 }
15728
15729 return token;
15730 }
15731
15732 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15733 function-definition. */
15734
15735 static bool
15736 cp_parser_token_starts_function_definition_p (cp_token* token)
15737 {
15738 return (/* An ordinary function-body begins with an `{'. */
15739 token->type == CPP_OPEN_BRACE
15740 /* A ctor-initializer begins with a `:'. */
15741 || token->type == CPP_COLON
15742 /* A function-try-block begins with `try'. */
15743 || token->keyword == RID_TRY
15744 /* The named return value extension begins with `return'. */
15745 || token->keyword == RID_RETURN);
15746 }
15747
15748 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15749 definition. */
15750
15751 static bool
15752 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15753 {
15754 cp_token *token;
15755
15756 token = cp_lexer_peek_token (parser->lexer);
15757 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15758 }
15759
15760 /* Returns TRUE iff the next token is the "," or ">" ending a
15761 template-argument. */
15762
15763 static bool
15764 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15765 {
15766 cp_token *token;
15767
15768 token = cp_lexer_peek_token (parser->lexer);
15769 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15770 }
15771
15772 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15773 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15774
15775 static bool
15776 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15777 size_t n)
15778 {
15779 cp_token *token;
15780
15781 token = cp_lexer_peek_nth_token (parser->lexer, n);
15782 if (token->type == CPP_LESS)
15783 return true;
15784 /* Check for the sequence `<::' in the original code. It would be lexed as
15785 `[:', where `[' is a digraph, and there is no whitespace before
15786 `:'. */
15787 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15788 {
15789 cp_token *token2;
15790 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15791 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15792 return true;
15793 }
15794 return false;
15795 }
15796
15797 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15798 or none_type otherwise. */
15799
15800 static enum tag_types
15801 cp_parser_token_is_class_key (cp_token* token)
15802 {
15803 switch (token->keyword)
15804 {
15805 case RID_CLASS:
15806 return class_type;
15807 case RID_STRUCT:
15808 return record_type;
15809 case RID_UNION:
15810 return union_type;
15811
15812 default:
15813 return none_type;
15814 }
15815 }
15816
15817 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15818
15819 static void
15820 cp_parser_check_class_key (enum tag_types class_key, tree type)
15821 {
15822 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15823 pedwarn ("%qs tag used in naming %q#T",
15824 class_key == union_type ? "union"
15825 : class_key == record_type ? "struct" : "class",
15826 type);
15827 }
15828
15829 /* Issue an error message if DECL is redeclared with different
15830 access than its original declaration [class.access.spec/3].
15831 This applies to nested classes and nested class templates.
15832 [class.mem/1]. */
15833
15834 static void
15835 cp_parser_check_access_in_redeclaration (tree decl)
15836 {
15837 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15838 return;
15839
15840 if ((TREE_PRIVATE (decl)
15841 != (current_access_specifier == access_private_node))
15842 || (TREE_PROTECTED (decl)
15843 != (current_access_specifier == access_protected_node)))
15844 error ("%qD redeclared with different access", decl);
15845 }
15846
15847 /* Look for the `template' keyword, as a syntactic disambiguator.
15848 Return TRUE iff it is present, in which case it will be
15849 consumed. */
15850
15851 static bool
15852 cp_parser_optional_template_keyword (cp_parser *parser)
15853 {
15854 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15855 {
15856 /* The `template' keyword can only be used within templates;
15857 outside templates the parser can always figure out what is a
15858 template and what is not. */
15859 if (!processing_template_decl)
15860 {
15861 error ("%<template%> (as a disambiguator) is only allowed "
15862 "within templates");
15863 /* If this part of the token stream is rescanned, the same
15864 error message would be generated. So, we purge the token
15865 from the stream. */
15866 cp_lexer_purge_token (parser->lexer);
15867 return false;
15868 }
15869 else
15870 {
15871 /* Consume the `template' keyword. */
15872 cp_lexer_consume_token (parser->lexer);
15873 return true;
15874 }
15875 }
15876
15877 return false;
15878 }
15879
15880 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15881 set PARSER->SCOPE, and perform other related actions. */
15882
15883 static void
15884 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15885 {
15886 tree value;
15887 tree check;
15888
15889 /* Get the stored value. */
15890 value = cp_lexer_consume_token (parser->lexer)->value;
15891 /* Perform any access checks that were deferred. */
15892 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15893 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15894 /* Set the scope from the stored value. */
15895 parser->scope = TREE_VALUE (value);
15896 parser->qualifying_scope = TREE_TYPE (value);
15897 parser->object_scope = NULL_TREE;
15898 }
15899
15900 /* Consume tokens up through a non-nested END token. */
15901
15902 static void
15903 cp_parser_cache_group (cp_parser *parser,
15904 enum cpp_ttype end,
15905 unsigned depth)
15906 {
15907 while (true)
15908 {
15909 cp_token *token;
15910
15911 /* Abort a parenthesized expression if we encounter a brace. */
15912 if ((end == CPP_CLOSE_PAREN || depth == 0)
15913 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15914 return;
15915 /* If we've reached the end of the file, stop. */
15916 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15917 return;
15918 /* Consume the next token. */
15919 token = cp_lexer_consume_token (parser->lexer);
15920 /* See if it starts a new group. */
15921 if (token->type == CPP_OPEN_BRACE)
15922 {
15923 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15924 if (depth == 0)
15925 return;
15926 }
15927 else if (token->type == CPP_OPEN_PAREN)
15928 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15929 else if (token->type == end)
15930 return;
15931 }
15932 }
15933
15934 /* Begin parsing tentatively. We always save tokens while parsing
15935 tentatively so that if the tentative parsing fails we can restore the
15936 tokens. */
15937
15938 static void
15939 cp_parser_parse_tentatively (cp_parser* parser)
15940 {
15941 /* Enter a new parsing context. */
15942 parser->context = cp_parser_context_new (parser->context);
15943 /* Begin saving tokens. */
15944 cp_lexer_save_tokens (parser->lexer);
15945 /* In order to avoid repetitive access control error messages,
15946 access checks are queued up until we are no longer parsing
15947 tentatively. */
15948 push_deferring_access_checks (dk_deferred);
15949 }
15950
15951 /* Commit to the currently active tentative parse. */
15952
15953 static void
15954 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15955 {
15956 cp_parser_context *context;
15957 cp_lexer *lexer;
15958
15959 /* Mark all of the levels as committed. */
15960 lexer = parser->lexer;
15961 for (context = parser->context; context->next; context = context->next)
15962 {
15963 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15964 break;
15965 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15966 while (!cp_lexer_saving_tokens (lexer))
15967 lexer = lexer->next;
15968 cp_lexer_commit_tokens (lexer);
15969 }
15970 }
15971
15972 /* Abort the currently active tentative parse. All consumed tokens
15973 will be rolled back, and no diagnostics will be issued. */
15974
15975 static void
15976 cp_parser_abort_tentative_parse (cp_parser* parser)
15977 {
15978 cp_parser_simulate_error (parser);
15979 /* Now, pretend that we want to see if the construct was
15980 successfully parsed. */
15981 cp_parser_parse_definitely (parser);
15982 }
15983
15984 /* Stop parsing tentatively. If a parse error has occurred, restore the
15985 token stream. Otherwise, commit to the tokens we have consumed.
15986 Returns true if no error occurred; false otherwise. */
15987
15988 static bool
15989 cp_parser_parse_definitely (cp_parser* parser)
15990 {
15991 bool error_occurred;
15992 cp_parser_context *context;
15993
15994 /* Remember whether or not an error occurred, since we are about to
15995 destroy that information. */
15996 error_occurred = cp_parser_error_occurred (parser);
15997 /* Remove the topmost context from the stack. */
15998 context = parser->context;
15999 parser->context = context->next;
16000 /* If no parse errors occurred, commit to the tentative parse. */
16001 if (!error_occurred)
16002 {
16003 /* Commit to the tokens read tentatively, unless that was
16004 already done. */
16005 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16006 cp_lexer_commit_tokens (parser->lexer);
16007
16008 pop_to_parent_deferring_access_checks ();
16009 }
16010 /* Otherwise, if errors occurred, roll back our state so that things
16011 are just as they were before we began the tentative parse. */
16012 else
16013 {
16014 cp_lexer_rollback_tokens (parser->lexer);
16015 pop_deferring_access_checks ();
16016 }
16017 /* Add the context to the front of the free list. */
16018 context->next = cp_parser_context_free_list;
16019 cp_parser_context_free_list = context;
16020
16021 return !error_occurred;
16022 }
16023
16024 /* Returns true if we are parsing tentatively and are not committed to
16025 this tentative parse. */
16026
16027 static bool
16028 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16029 {
16030 return (cp_parser_parsing_tentatively (parser)
16031 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16032 }
16033
16034 /* Returns nonzero iff an error has occurred during the most recent
16035 tentative parse. */
16036
16037 static bool
16038 cp_parser_error_occurred (cp_parser* parser)
16039 {
16040 return (cp_parser_parsing_tentatively (parser)
16041 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16042 }
16043
16044 /* Returns nonzero if GNU extensions are allowed. */
16045
16046 static bool
16047 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16048 {
16049 return parser->allow_gnu_extensions_p;
16050 }
16051
16052 \f
16053 /* The parser. */
16054
16055 static GTY (()) cp_parser *the_parser;
16056
16057 /* External interface. */
16058
16059 /* Parse one entire translation unit. */
16060
16061 void
16062 c_parse_file (void)
16063 {
16064 bool error_occurred;
16065 static bool already_called = false;
16066
16067 if (already_called)
16068 {
16069 sorry ("inter-module optimizations not implemented for C++");
16070 return;
16071 }
16072 already_called = true;
16073
16074 the_parser = cp_parser_new ();
16075 push_deferring_access_checks (flag_access_control
16076 ? dk_no_deferred : dk_no_check);
16077 error_occurred = cp_parser_translation_unit (the_parser);
16078 the_parser = NULL;
16079 }
16080
16081 /* This variable must be provided by every front end. */
16082
16083 int yydebug;
16084
16085 #include "gt-cp-parser.h"