]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
diagnostic.c (warning): Accept parameter to classify warning option.
[thirdparty/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_P (cp_token_position);
70 DEF_VEC_ALLOC_P (cp_token_position,heap);
71
72 static const cp_token eof_token =
73 {
74 CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
75 #if USE_MAPPED_LOCATION
76 0
77 #else
78 {0, 0}
79 #endif
80 };
81
82 /* The cp_lexer structure represents the C++ lexer. It is responsible
83 for managing the token stream from the preprocessor and supplying
84 it to the parser. Tokens are never added to the cp_lexer after
85 it is created. */
86
87 typedef struct cp_lexer GTY (())
88 {
89 /* The memory allocated for the buffer. NULL if this lexer does not
90 own the token buffer. */
91 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
92 /* If the lexer owns the buffer, this is the number of tokens in the
93 buffer. */
94 size_t buffer_length;
95
96 /* A pointer just past the last available token. The tokens
97 in this lexer are [buffer, last_token). */
98 cp_token_position GTY ((skip)) last_token;
99
100 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
101 no more available tokens. */
102 cp_token_position GTY ((skip)) next_token;
103
104 /* A stack indicating positions at which cp_lexer_save_tokens was
105 called. The top entry is the most recent position at which we
106 began saving tokens. If the stack is non-empty, we are saving
107 tokens. */
108 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
109
110 /* True if we should output debugging information. */
111 bool debugging_p;
112
113 /* The next lexer in a linked list of lexers. */
114 struct cp_lexer *next;
115 } cp_lexer;
116
117 /* cp_token_cache is a range of tokens. There is no need to represent
118 allocate heap memory for it, since tokens are never removed from the
119 lexer's array. There is also no need for the GC to walk through
120 a cp_token_cache, since everything in here is referenced through
121 a lexer. */
122
123 typedef struct cp_token_cache GTY(())
124 {
125 /* The beginning of the token range. */
126 cp_token * GTY((skip)) first;
127
128 /* Points immediately after the last token in the range. */
129 cp_token * GTY ((skip)) last;
130 } cp_token_cache;
131
132 /* Prototypes. */
133
134 static cp_lexer *cp_lexer_new_main
135 (void);
136 static cp_lexer *cp_lexer_new_from_tokens
137 (cp_token_cache *tokens);
138 static void cp_lexer_destroy
139 (cp_lexer *);
140 static int cp_lexer_saving_tokens
141 (const cp_lexer *);
142 static cp_token_position cp_lexer_token_position
143 (cp_lexer *, bool);
144 static cp_token *cp_lexer_token_at
145 (cp_lexer *, cp_token_position);
146 static void cp_lexer_get_preprocessor_token
147 (cp_lexer *, cp_token *);
148 static inline cp_token *cp_lexer_peek_token
149 (cp_lexer *);
150 static cp_token *cp_lexer_peek_nth_token
151 (cp_lexer *, size_t);
152 static inline bool cp_lexer_next_token_is
153 (cp_lexer *, enum cpp_ttype);
154 static bool cp_lexer_next_token_is_not
155 (cp_lexer *, enum cpp_ttype);
156 static bool cp_lexer_next_token_is_keyword
157 (cp_lexer *, enum rid);
158 static cp_token *cp_lexer_consume_token
159 (cp_lexer *);
160 static void cp_lexer_purge_token
161 (cp_lexer *);
162 static void cp_lexer_purge_tokens_after
163 (cp_lexer *, cp_token_position);
164 static void cp_lexer_handle_pragma
165 (cp_lexer *);
166 static void cp_lexer_save_tokens
167 (cp_lexer *);
168 static void cp_lexer_commit_tokens
169 (cp_lexer *);
170 static void cp_lexer_rollback_tokens
171 (cp_lexer *);
172 #ifdef ENABLE_CHECKING
173 static void cp_lexer_print_token
174 (FILE *, cp_token *);
175 static inline bool cp_lexer_debugging_p
176 (cp_lexer *);
177 static void cp_lexer_start_debugging
178 (cp_lexer *) ATTRIBUTE_UNUSED;
179 static void cp_lexer_stop_debugging
180 (cp_lexer *) ATTRIBUTE_UNUSED;
181 #else
182 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
183 about passing NULL to functions that require non-NULL arguments
184 (fputs, fprintf). It will never be used, so all we need is a value
185 of the right type that's guaranteed not to be NULL. */
186 #define cp_lexer_debug_stream stdout
187 #define cp_lexer_print_token(str, tok) (void) 0
188 #define cp_lexer_debugging_p(lexer) 0
189 #endif /* ENABLE_CHECKING */
190
191 static cp_token_cache *cp_token_cache_new
192 (cp_token *, cp_token *);
193
194 /* Manifest constants. */
195 #define CP_LEXER_BUFFER_SIZE 10000
196 #define CP_SAVED_TOKEN_STACK 5
197
198 /* A token type for keywords, as opposed to ordinary identifiers. */
199 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
200
201 /* A token type for template-ids. If a template-id is processed while
202 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
203 the value of the CPP_TEMPLATE_ID is whatever was returned by
204 cp_parser_template_id. */
205 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
206
207 /* A token type for nested-name-specifiers. If a
208 nested-name-specifier is processed while parsing tentatively, it is
209 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
210 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
211 cp_parser_nested_name_specifier_opt. */
212 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
213
214 /* A token type for tokens that are not tokens at all; these are used
215 to represent slots in the array where there used to be a token
216 that has now been deleted. */
217 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
218
219 /* The number of token types, including C++-specific ones. */
220 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
221
222 /* Variables. */
223
224 #ifdef ENABLE_CHECKING
225 /* The stream to which debugging output should be written. */
226 static FILE *cp_lexer_debug_stream;
227 #endif /* ENABLE_CHECKING */
228
229 /* Create a new main C++ lexer, the lexer that gets tokens from the
230 preprocessor. */
231
232 static cp_lexer *
233 cp_lexer_new_main (void)
234 {
235 cp_token first_token;
236 cp_lexer *lexer;
237 cp_token *pos;
238 size_t alloc;
239 size_t space;
240 cp_token *buffer;
241
242 /* It's possible that lexing the first token will load a PCH file,
243 which is a GC collection point. So we have to grab the first
244 token before allocating any memory. Pragmas must not be deferred
245 as -fpch-preprocess can generate a pragma to load the PCH file in
246 the preprocessed output used by -save-temps. */
247 cp_lexer_get_preprocessor_token (NULL, &first_token);
248
249 /* Tell cpplib we want CPP_PRAGMA tokens. */
250 cpp_get_options (parse_in)->defer_pragmas = true;
251
252 /* Tell c_lex not to merge string constants. */
253 c_lex_return_raw_strings = true;
254
255 c_common_no_more_pch ();
256
257 /* Allocate the memory. */
258 lexer = GGC_CNEW (cp_lexer);
259
260 #ifdef ENABLE_CHECKING
261 /* Initially we are not debugging. */
262 lexer->debugging_p = false;
263 #endif /* ENABLE_CHECKING */
264 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
265 CP_SAVED_TOKEN_STACK);
266
267 /* Create the buffer. */
268 alloc = CP_LEXER_BUFFER_SIZE;
269 buffer = ggc_alloc (alloc * sizeof (cp_token));
270
271 /* Put the first token in the buffer. */
272 space = alloc;
273 pos = buffer;
274 *pos = first_token;
275
276 /* Get the remaining tokens from the preprocessor. */
277 while (pos->type != CPP_EOF)
278 {
279 pos++;
280 if (!--space)
281 {
282 space = alloc;
283 alloc *= 2;
284 buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
285 pos = buffer + space;
286 }
287 cp_lexer_get_preprocessor_token (lexer, pos);
288 }
289 lexer->buffer = buffer;
290 lexer->buffer_length = alloc - space;
291 lexer->last_token = pos;
292 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
293
294 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
295 direct calls to c_lex. Those callers all expect c_lex to do
296 string constant concatenation. */
297 c_lex_return_raw_strings = false;
298
299 gcc_assert (lexer->next_token->type != CPP_PURGED);
300 return lexer;
301 }
302
303 /* Create a new lexer whose token stream is primed with the tokens in
304 CACHE. When these tokens are exhausted, no new tokens will be read. */
305
306 static cp_lexer *
307 cp_lexer_new_from_tokens (cp_token_cache *cache)
308 {
309 cp_token *first = cache->first;
310 cp_token *last = cache->last;
311 cp_lexer *lexer = GGC_CNEW (cp_lexer);
312
313 /* We do not own the buffer. */
314 lexer->buffer = NULL;
315 lexer->buffer_length = 0;
316 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
317 lexer->last_token = last;
318
319 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
320 CP_SAVED_TOKEN_STACK);
321
322 #ifdef ENABLE_CHECKING
323 /* Initially we are not debugging. */
324 lexer->debugging_p = false;
325 #endif
326
327 gcc_assert (lexer->next_token->type != CPP_PURGED);
328 return lexer;
329 }
330
331 /* Frees all resources associated with LEXER. */
332
333 static void
334 cp_lexer_destroy (cp_lexer *lexer)
335 {
336 if (lexer->buffer)
337 ggc_free (lexer->buffer);
338 VEC_free (cp_token_position, heap, lexer->saved_tokens);
339 ggc_free (lexer);
340 }
341
342 /* Returns nonzero if debugging information should be output. */
343
344 #ifdef ENABLE_CHECKING
345
346 static inline bool
347 cp_lexer_debugging_p (cp_lexer *lexer)
348 {
349 return lexer->debugging_p;
350 }
351
352 #endif /* ENABLE_CHECKING */
353
354 static inline cp_token_position
355 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
356 {
357 gcc_assert (!previous_p || lexer->next_token != &eof_token);
358
359 return lexer->next_token - previous_p;
360 }
361
362 static inline cp_token *
363 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
364 {
365 return pos;
366 }
367
368 /* nonzero if we are presently saving tokens. */
369
370 static inline int
371 cp_lexer_saving_tokens (const cp_lexer* lexer)
372 {
373 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
374 }
375
376 /* Store the next token from the preprocessor in *TOKEN. Return true
377 if we reach EOF. */
378
379 static void
380 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
381 cp_token *token)
382 {
383 static int is_extern_c = 0;
384
385 /* Get a new token from the preprocessor. */
386 token->type
387 = c_lex_with_flags (&token->value, &token->location, &token->flags);
388 token->in_system_header = in_system_header;
389
390 /* On some systems, some header files are surrounded by an
391 implicit extern "C" block. Set a flag in the token if it
392 comes from such a header. */
393 is_extern_c += pending_lang_change;
394 pending_lang_change = 0;
395 token->implicit_extern_c = is_extern_c > 0;
396
397 /* Check to see if this token is a keyword. */
398 if (token->type == CPP_NAME
399 && C_IS_RESERVED_WORD (token->value))
400 {
401 /* Mark this token as a keyword. */
402 token->type = CPP_KEYWORD;
403 /* Record which keyword. */
404 token->keyword = C_RID_CODE (token->value);
405 /* Update the value. Some keywords are mapped to particular
406 entities, rather than simply having the value of the
407 corresponding IDENTIFIER_NODE. For example, `__const' is
408 mapped to `const'. */
409 token->value = ridpointers[token->keyword];
410 }
411 else
412 token->keyword = RID_MAX;
413 }
414
415 /* Update the globals input_location and in_system_header from TOKEN. */
416 static inline void
417 cp_lexer_set_source_position_from_token (cp_token *token)
418 {
419 if (token->type != CPP_EOF)
420 {
421 input_location = token->location;
422 in_system_header = token->in_system_header;
423 }
424 }
425
426 /* Return a pointer to the next token in the token stream, but do not
427 consume it. */
428
429 static inline cp_token *
430 cp_lexer_peek_token (cp_lexer *lexer)
431 {
432 if (cp_lexer_debugging_p (lexer))
433 {
434 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
435 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
436 putc ('\n', cp_lexer_debug_stream);
437 }
438 return lexer->next_token;
439 }
440
441 /* Return true if the next token has the indicated TYPE. */
442
443 static inline bool
444 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
445 {
446 return cp_lexer_peek_token (lexer)->type == type;
447 }
448
449 /* Return true if the next token does not have the indicated TYPE. */
450
451 static inline bool
452 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
453 {
454 return !cp_lexer_next_token_is (lexer, type);
455 }
456
457 /* Return true if the next token is the indicated KEYWORD. */
458
459 static inline bool
460 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
461 {
462 cp_token *token;
463
464 /* Peek at the next token. */
465 token = cp_lexer_peek_token (lexer);
466 /* Check to see if it is the indicated keyword. */
467 return token->keyword == keyword;
468 }
469
470 /* Return a pointer to the Nth token in the token stream. If N is 1,
471 then this is precisely equivalent to cp_lexer_peek_token (except
472 that it is not inline). One would like to disallow that case, but
473 there is one case (cp_parser_nth_token_starts_template_id) where
474 the caller passes a variable for N and it might be 1. */
475
476 static cp_token *
477 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
478 {
479 cp_token *token;
480
481 /* N is 1-based, not zero-based. */
482 gcc_assert (n > 0 && lexer->next_token != &eof_token);
483
484 if (cp_lexer_debugging_p (lexer))
485 fprintf (cp_lexer_debug_stream,
486 "cp_lexer: peeking ahead %ld at token: ", (long)n);
487
488 --n;
489 token = lexer->next_token;
490 while (n != 0)
491 {
492 ++token;
493 if (token == lexer->last_token)
494 {
495 token = (cp_token *)&eof_token;
496 break;
497 }
498
499 if (token->type != CPP_PURGED)
500 --n;
501 }
502
503 if (cp_lexer_debugging_p (lexer))
504 {
505 cp_lexer_print_token (cp_lexer_debug_stream, token);
506 putc ('\n', cp_lexer_debug_stream);
507 }
508
509 return token;
510 }
511
512 /* Return the next token, and advance the lexer's next_token pointer
513 to point to the next non-purged token. */
514
515 static cp_token *
516 cp_lexer_consume_token (cp_lexer* lexer)
517 {
518 cp_token *token = lexer->next_token;
519
520 gcc_assert (token != &eof_token);
521
522 do
523 {
524 lexer->next_token++;
525 if (lexer->next_token == lexer->last_token)
526 {
527 lexer->next_token = (cp_token *)&eof_token;
528 break;
529 }
530
531 }
532 while (lexer->next_token->type == CPP_PURGED);
533
534 cp_lexer_set_source_position_from_token (token);
535
536 /* Provide debugging output. */
537 if (cp_lexer_debugging_p (lexer))
538 {
539 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
540 cp_lexer_print_token (cp_lexer_debug_stream, token);
541 putc ('\n', cp_lexer_debug_stream);
542 }
543
544 return token;
545 }
546
547 /* Permanently remove the next token from the token stream, and
548 advance the next_token pointer to refer to the next non-purged
549 token. */
550
551 static void
552 cp_lexer_purge_token (cp_lexer *lexer)
553 {
554 cp_token *tok = lexer->next_token;
555
556 gcc_assert (tok != &eof_token);
557 tok->type = CPP_PURGED;
558 tok->location = UNKNOWN_LOCATION;
559 tok->value = NULL_TREE;
560 tok->keyword = RID_MAX;
561
562 do
563 {
564 tok++;
565 if (tok == lexer->last_token)
566 {
567 tok = (cp_token *)&eof_token;
568 break;
569 }
570 }
571 while (tok->type == CPP_PURGED);
572 lexer->next_token = tok;
573 }
574
575 /* Permanently remove all tokens after TOK, up to, but not
576 including, the token that will be returned next by
577 cp_lexer_peek_token. */
578
579 static void
580 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
581 {
582 cp_token *peek = lexer->next_token;
583
584 if (peek == &eof_token)
585 peek = lexer->last_token;
586
587 gcc_assert (tok < peek);
588
589 for ( tok += 1; tok != peek; tok += 1)
590 {
591 tok->type = CPP_PURGED;
592 tok->location = UNKNOWN_LOCATION;
593 tok->value = NULL_TREE;
594 tok->keyword = RID_MAX;
595 }
596 }
597
598 /* Consume and handle a pragma token. */
599 static void
600 cp_lexer_handle_pragma (cp_lexer *lexer)
601 {
602 cpp_string s;
603 cp_token *token = cp_lexer_consume_token (lexer);
604 gcc_assert (token->type == CPP_PRAGMA);
605 gcc_assert (token->value);
606
607 s.len = TREE_STRING_LENGTH (token->value);
608 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
609
610 cpp_handle_deferred_pragma (parse_in, &s);
611
612 /* Clearing token->value here means that we will get an ICE if we
613 try to process this #pragma again (which should be impossible). */
614 token->value = NULL;
615 }
616
617 /* Begin saving tokens. All tokens consumed after this point will be
618 preserved. */
619
620 static void
621 cp_lexer_save_tokens (cp_lexer* lexer)
622 {
623 /* Provide debugging output. */
624 if (cp_lexer_debugging_p (lexer))
625 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
626
627 VEC_safe_push (cp_token_position, heap,
628 lexer->saved_tokens, lexer->next_token);
629 }
630
631 /* Commit to the portion of the token stream most recently saved. */
632
633 static void
634 cp_lexer_commit_tokens (cp_lexer* lexer)
635 {
636 /* Provide debugging output. */
637 if (cp_lexer_debugging_p (lexer))
638 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
639
640 VEC_pop (cp_token_position, lexer->saved_tokens);
641 }
642
643 /* Return all tokens saved since the last call to cp_lexer_save_tokens
644 to the token stream. Stop saving tokens. */
645
646 static void
647 cp_lexer_rollback_tokens (cp_lexer* lexer)
648 {
649 /* Provide debugging output. */
650 if (cp_lexer_debugging_p (lexer))
651 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
652
653 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
654 }
655
656 /* Print a representation of the TOKEN on the STREAM. */
657
658 #ifdef ENABLE_CHECKING
659
660 static void
661 cp_lexer_print_token (FILE * stream, cp_token *token)
662 {
663 /* We don't use cpp_type2name here because the parser defines
664 a few tokens of its own. */
665 static const char *const token_names[] = {
666 /* cpplib-defined token types */
667 #define OP(e, s) #e,
668 #define TK(e, s) #e,
669 TTYPE_TABLE
670 #undef OP
671 #undef TK
672 /* C++ parser token types - see "Manifest constants", above. */
673 "KEYWORD",
674 "TEMPLATE_ID",
675 "NESTED_NAME_SPECIFIER",
676 "PURGED"
677 };
678
679 /* If we have a name for the token, print it out. Otherwise, we
680 simply give the numeric code. */
681 gcc_assert (token->type < ARRAY_SIZE(token_names));
682 fputs (token_names[token->type], stream);
683
684 /* For some tokens, print the associated data. */
685 switch (token->type)
686 {
687 case CPP_KEYWORD:
688 /* Some keywords have a value that is not an IDENTIFIER_NODE.
689 For example, `struct' is mapped to an INTEGER_CST. */
690 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
691 break;
692 /* else fall through */
693 case CPP_NAME:
694 fputs (IDENTIFIER_POINTER (token->value), stream);
695 break;
696
697 case CPP_STRING:
698 case CPP_WSTRING:
699 case CPP_PRAGMA:
700 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
701 break;
702
703 default:
704 break;
705 }
706 }
707
708 /* Start emitting debugging information. */
709
710 static void
711 cp_lexer_start_debugging (cp_lexer* lexer)
712 {
713 lexer->debugging_p = true;
714 }
715
716 /* Stop emitting debugging information. */
717
718 static void
719 cp_lexer_stop_debugging (cp_lexer* lexer)
720 {
721 lexer->debugging_p = false;
722 }
723
724 #endif /* ENABLE_CHECKING */
725
726 /* Create a new cp_token_cache, representing a range of tokens. */
727
728 static cp_token_cache *
729 cp_token_cache_new (cp_token *first, cp_token *last)
730 {
731 cp_token_cache *cache = GGC_NEW (cp_token_cache);
732 cache->first = first;
733 cache->last = last;
734 return cache;
735 }
736
737 \f
738 /* Decl-specifiers. */
739
740 static void clear_decl_specs
741 (cp_decl_specifier_seq *);
742
743 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
744
745 static void
746 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
747 {
748 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
749 }
750
751 /* Declarators. */
752
753 /* Nothing other than the parser should be creating declarators;
754 declarators are a semi-syntactic representation of C++ entities.
755 Other parts of the front end that need to create entities (like
756 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
757
758 static cp_declarator *make_call_declarator
759 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
760 static cp_declarator *make_array_declarator
761 (cp_declarator *, tree);
762 static cp_declarator *make_pointer_declarator
763 (cp_cv_quals, cp_declarator *);
764 static cp_declarator *make_reference_declarator
765 (cp_cv_quals, cp_declarator *);
766 static cp_parameter_declarator *make_parameter_declarator
767 (cp_decl_specifier_seq *, cp_declarator *, tree);
768 static cp_declarator *make_ptrmem_declarator
769 (cp_cv_quals, tree, cp_declarator *);
770
771 cp_declarator *cp_error_declarator;
772
773 /* The obstack on which declarators and related data structures are
774 allocated. */
775 static struct obstack declarator_obstack;
776
777 /* Alloc BYTES from the declarator memory pool. */
778
779 static inline void *
780 alloc_declarator (size_t bytes)
781 {
782 return obstack_alloc (&declarator_obstack, bytes);
783 }
784
785 /* Allocate a declarator of the indicated KIND. Clear fields that are
786 common to all declarators. */
787
788 static cp_declarator *
789 make_declarator (cp_declarator_kind kind)
790 {
791 cp_declarator *declarator;
792
793 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
794 declarator->kind = kind;
795 declarator->attributes = NULL_TREE;
796 declarator->declarator = NULL;
797
798 return declarator;
799 }
800
801 /* Make a declarator for a generalized identifier. If non-NULL, the
802 identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
803 just UNQUALIFIED_NAME. */
804
805 static cp_declarator *
806 make_id_declarator (tree qualifying_scope, tree unqualified_name)
807 {
808 cp_declarator *declarator;
809
810 /* It is valid to write:
811
812 class C { void f(); };
813 typedef C D;
814 void D::f();
815
816 The standard is not clear about whether `typedef const C D' is
817 legal; as of 2002-09-15 the committee is considering that
818 question. EDG 3.0 allows that syntax. Therefore, we do as
819 well. */
820 if (qualifying_scope && TYPE_P (qualifying_scope))
821 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
822
823 declarator = make_declarator (cdk_id);
824 declarator->u.id.qualifying_scope = qualifying_scope;
825 declarator->u.id.unqualified_name = unqualified_name;
826 declarator->u.id.sfk = sfk_none;
827
828 return declarator;
829 }
830
831 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
832 of modifiers such as const or volatile to apply to the pointer
833 type, represented as identifiers. */
834
835 cp_declarator *
836 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
837 {
838 cp_declarator *declarator;
839
840 declarator = make_declarator (cdk_pointer);
841 declarator->declarator = target;
842 declarator->u.pointer.qualifiers = cv_qualifiers;
843 declarator->u.pointer.class_type = NULL_TREE;
844
845 return declarator;
846 }
847
848 /* Like make_pointer_declarator -- but for references. */
849
850 cp_declarator *
851 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
852 {
853 cp_declarator *declarator;
854
855 declarator = make_declarator (cdk_reference);
856 declarator->declarator = target;
857 declarator->u.pointer.qualifiers = cv_qualifiers;
858 declarator->u.pointer.class_type = NULL_TREE;
859
860 return declarator;
861 }
862
863 /* Like make_pointer_declarator -- but for a pointer to a non-static
864 member of CLASS_TYPE. */
865
866 cp_declarator *
867 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
868 cp_declarator *pointee)
869 {
870 cp_declarator *declarator;
871
872 declarator = make_declarator (cdk_ptrmem);
873 declarator->declarator = pointee;
874 declarator->u.pointer.qualifiers = cv_qualifiers;
875 declarator->u.pointer.class_type = class_type;
876
877 return declarator;
878 }
879
880 /* Make a declarator for the function given by TARGET, with the
881 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
882 "const"-qualified member function. The EXCEPTION_SPECIFICATION
883 indicates what exceptions can be thrown. */
884
885 cp_declarator *
886 make_call_declarator (cp_declarator *target,
887 cp_parameter_declarator *parms,
888 cp_cv_quals cv_qualifiers,
889 tree exception_specification)
890 {
891 cp_declarator *declarator;
892
893 declarator = make_declarator (cdk_function);
894 declarator->declarator = target;
895 declarator->u.function.parameters = parms;
896 declarator->u.function.qualifiers = cv_qualifiers;
897 declarator->u.function.exception_specification = exception_specification;
898
899 return declarator;
900 }
901
902 /* Make a declarator for an array of BOUNDS elements, each of which is
903 defined by ELEMENT. */
904
905 cp_declarator *
906 make_array_declarator (cp_declarator *element, tree bounds)
907 {
908 cp_declarator *declarator;
909
910 declarator = make_declarator (cdk_array);
911 declarator->declarator = element;
912 declarator->u.array.bounds = bounds;
913
914 return declarator;
915 }
916
917 cp_parameter_declarator *no_parameters;
918
919 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
920 DECLARATOR and DEFAULT_ARGUMENT. */
921
922 cp_parameter_declarator *
923 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
924 cp_declarator *declarator,
925 tree default_argument)
926 {
927 cp_parameter_declarator *parameter;
928
929 parameter = ((cp_parameter_declarator *)
930 alloc_declarator (sizeof (cp_parameter_declarator)));
931 parameter->next = NULL;
932 if (decl_specifiers)
933 parameter->decl_specifiers = *decl_specifiers;
934 else
935 clear_decl_specs (&parameter->decl_specifiers);
936 parameter->declarator = declarator;
937 parameter->default_argument = default_argument;
938 parameter->ellipsis_p = false;
939
940 return parameter;
941 }
942
943 /* The parser. */
944
945 /* Overview
946 --------
947
948 A cp_parser parses the token stream as specified by the C++
949 grammar. Its job is purely parsing, not semantic analysis. For
950 example, the parser breaks the token stream into declarators,
951 expressions, statements, and other similar syntactic constructs.
952 It does not check that the types of the expressions on either side
953 of an assignment-statement are compatible, or that a function is
954 not declared with a parameter of type `void'.
955
956 The parser invokes routines elsewhere in the compiler to perform
957 semantic analysis and to build up the abstract syntax tree for the
958 code processed.
959
960 The parser (and the template instantiation code, which is, in a
961 way, a close relative of parsing) are the only parts of the
962 compiler that should be calling push_scope and pop_scope, or
963 related functions. The parser (and template instantiation code)
964 keeps track of what scope is presently active; everything else
965 should simply honor that. (The code that generates static
966 initializers may also need to set the scope, in order to check
967 access control correctly when emitting the initializers.)
968
969 Methodology
970 -----------
971
972 The parser is of the standard recursive-descent variety. Upcoming
973 tokens in the token stream are examined in order to determine which
974 production to use when parsing a non-terminal. Some C++ constructs
975 require arbitrary look ahead to disambiguate. For example, it is
976 impossible, in the general case, to tell whether a statement is an
977 expression or declaration without scanning the entire statement.
978 Therefore, the parser is capable of "parsing tentatively." When the
979 parser is not sure what construct comes next, it enters this mode.
980 Then, while we attempt to parse the construct, the parser queues up
981 error messages, rather than issuing them immediately, and saves the
982 tokens it consumes. If the construct is parsed successfully, the
983 parser "commits", i.e., it issues any queued error messages and
984 the tokens that were being preserved are permanently discarded.
985 If, however, the construct is not parsed successfully, the parser
986 rolls back its state completely so that it can resume parsing using
987 a different alternative.
988
989 Future Improvements
990 -------------------
991
992 The performance of the parser could probably be improved substantially.
993 We could often eliminate the need to parse tentatively by looking ahead
994 a little bit. In some places, this approach might not entirely eliminate
995 the need to parse tentatively, but it might still speed up the average
996 case. */
997
998 /* Flags that are passed to some parsing functions. These values can
999 be bitwise-ored together. */
1000
1001 typedef enum cp_parser_flags
1002 {
1003 /* No flags. */
1004 CP_PARSER_FLAGS_NONE = 0x0,
1005 /* The construct is optional. If it is not present, then no error
1006 should be issued. */
1007 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1008 /* When parsing a type-specifier, do not allow user-defined types. */
1009 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1010 } cp_parser_flags;
1011
1012 /* The different kinds of declarators we want to parse. */
1013
1014 typedef enum cp_parser_declarator_kind
1015 {
1016 /* We want an abstract declarator. */
1017 CP_PARSER_DECLARATOR_ABSTRACT,
1018 /* We want a named declarator. */
1019 CP_PARSER_DECLARATOR_NAMED,
1020 /* We don't mind, but the name must be an unqualified-id. */
1021 CP_PARSER_DECLARATOR_EITHER
1022 } cp_parser_declarator_kind;
1023
1024 /* The precedence values used to parse binary expressions. The minimum value
1025 of PREC must be 1, because zero is reserved to quickly discriminate
1026 binary operators from other tokens. */
1027
1028 enum cp_parser_prec
1029 {
1030 PREC_NOT_OPERATOR,
1031 PREC_LOGICAL_OR_EXPRESSION,
1032 PREC_LOGICAL_AND_EXPRESSION,
1033 PREC_INCLUSIVE_OR_EXPRESSION,
1034 PREC_EXCLUSIVE_OR_EXPRESSION,
1035 PREC_AND_EXPRESSION,
1036 PREC_EQUALITY_EXPRESSION,
1037 PREC_RELATIONAL_EXPRESSION,
1038 PREC_SHIFT_EXPRESSION,
1039 PREC_ADDITIVE_EXPRESSION,
1040 PREC_MULTIPLICATIVE_EXPRESSION,
1041 PREC_PM_EXPRESSION,
1042 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1043 };
1044
1045 /* A mapping from a token type to a corresponding tree node type, with a
1046 precedence value. */
1047
1048 typedef struct cp_parser_binary_operations_map_node
1049 {
1050 /* The token type. */
1051 enum cpp_ttype token_type;
1052 /* The corresponding tree code. */
1053 enum tree_code tree_type;
1054 /* The precedence of this operator. */
1055 enum cp_parser_prec prec;
1056 } cp_parser_binary_operations_map_node;
1057
1058 /* The status of a tentative parse. */
1059
1060 typedef enum cp_parser_status_kind
1061 {
1062 /* No errors have occurred. */
1063 CP_PARSER_STATUS_KIND_NO_ERROR,
1064 /* An error has occurred. */
1065 CP_PARSER_STATUS_KIND_ERROR,
1066 /* We are committed to this tentative parse, whether or not an error
1067 has occurred. */
1068 CP_PARSER_STATUS_KIND_COMMITTED
1069 } cp_parser_status_kind;
1070
1071 typedef struct cp_parser_expression_stack_entry
1072 {
1073 tree lhs;
1074 enum tree_code tree_type;
1075 int prec;
1076 } cp_parser_expression_stack_entry;
1077
1078 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1079 entries because precedence levels on the stack are monotonically
1080 increasing. */
1081 typedef struct cp_parser_expression_stack_entry
1082 cp_parser_expression_stack[NUM_PREC_VALUES];
1083
1084 /* Context that is saved and restored when parsing tentatively. */
1085 typedef struct cp_parser_context GTY (())
1086 {
1087 /* If this is a tentative parsing context, the status of the
1088 tentative parse. */
1089 enum cp_parser_status_kind status;
1090 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1091 that are looked up in this context must be looked up both in the
1092 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1093 the context of the containing expression. */
1094 tree object_type;
1095
1096 /* The next parsing context in the stack. */
1097 struct cp_parser_context *next;
1098 } cp_parser_context;
1099
1100 /* Prototypes. */
1101
1102 /* Constructors and destructors. */
1103
1104 static cp_parser_context *cp_parser_context_new
1105 (cp_parser_context *);
1106
1107 /* Class variables. */
1108
1109 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1110
1111 /* The operator-precedence table used by cp_parser_binary_expression.
1112 Transformed into an associative array (binops_by_token) by
1113 cp_parser_new. */
1114
1115 static const cp_parser_binary_operations_map_node binops[] = {
1116 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1117 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1118
1119 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1120 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1121 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1122
1123 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1124 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1125
1126 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1127 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1128
1129 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1130 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1131 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1132 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1133 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1134 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1135
1136 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1137 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1138
1139 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1140
1141 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1142
1143 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1144
1145 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1146
1147 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1148 };
1149
1150 /* The same as binops, but initialized by cp_parser_new so that
1151 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1152 for speed. */
1153 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1154
1155 /* Constructors and destructors. */
1156
1157 /* Construct a new context. The context below this one on the stack
1158 is given by NEXT. */
1159
1160 static cp_parser_context *
1161 cp_parser_context_new (cp_parser_context* next)
1162 {
1163 cp_parser_context *context;
1164
1165 /* Allocate the storage. */
1166 if (cp_parser_context_free_list != NULL)
1167 {
1168 /* Pull the first entry from the free list. */
1169 context = cp_parser_context_free_list;
1170 cp_parser_context_free_list = context->next;
1171 memset (context, 0, sizeof (*context));
1172 }
1173 else
1174 context = GGC_CNEW (cp_parser_context);
1175
1176 /* No errors have occurred yet in this context. */
1177 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1178 /* If this is not the bottomost context, copy information that we
1179 need from the previous context. */
1180 if (next)
1181 {
1182 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1183 expression, then we are parsing one in this context, too. */
1184 context->object_type = next->object_type;
1185 /* Thread the stack. */
1186 context->next = next;
1187 }
1188
1189 return context;
1190 }
1191
1192 /* The cp_parser structure represents the C++ parser. */
1193
1194 typedef struct cp_parser GTY(())
1195 {
1196 /* The lexer from which we are obtaining tokens. */
1197 cp_lexer *lexer;
1198
1199 /* The scope in which names should be looked up. If NULL_TREE, then
1200 we look up names in the scope that is currently open in the
1201 source program. If non-NULL, this is either a TYPE or
1202 NAMESPACE_DECL for the scope in which we should look.
1203
1204 This value is not cleared automatically after a name is looked
1205 up, so we must be careful to clear it before starting a new look
1206 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1207 will look up `Z' in the scope of `X', rather than the current
1208 scope.) Unfortunately, it is difficult to tell when name lookup
1209 is complete, because we sometimes peek at a token, look it up,
1210 and then decide not to consume it. */
1211 tree scope;
1212
1213 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1214 last lookup took place. OBJECT_SCOPE is used if an expression
1215 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1216 respectively. QUALIFYING_SCOPE is used for an expression of the
1217 form "X::Y"; it refers to X. */
1218 tree object_scope;
1219 tree qualifying_scope;
1220
1221 /* A stack of parsing contexts. All but the bottom entry on the
1222 stack will be tentative contexts.
1223
1224 We parse tentatively in order to determine which construct is in
1225 use in some situations. For example, in order to determine
1226 whether a statement is an expression-statement or a
1227 declaration-statement we parse it tentatively as a
1228 declaration-statement. If that fails, we then reparse the same
1229 token stream as an expression-statement. */
1230 cp_parser_context *context;
1231
1232 /* True if we are parsing GNU C++. If this flag is not set, then
1233 GNU extensions are not recognized. */
1234 bool allow_gnu_extensions_p;
1235
1236 /* TRUE if the `>' token should be interpreted as the greater-than
1237 operator. FALSE if it is the end of a template-id or
1238 template-parameter-list. */
1239 bool greater_than_is_operator_p;
1240
1241 /* TRUE if default arguments are allowed within a parameter list
1242 that starts at this point. FALSE if only a gnu extension makes
1243 them permissible. */
1244 bool default_arg_ok_p;
1245
1246 /* TRUE if we are parsing an integral constant-expression. See
1247 [expr.const] for a precise definition. */
1248 bool integral_constant_expression_p;
1249
1250 /* TRUE if we are parsing an integral constant-expression -- but a
1251 non-constant expression should be permitted as well. This flag
1252 is used when parsing an array bound so that GNU variable-length
1253 arrays are tolerated. */
1254 bool allow_non_integral_constant_expression_p;
1255
1256 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1257 been seen that makes the expression non-constant. */
1258 bool non_integral_constant_expression_p;
1259
1260 /* TRUE if local variable names and `this' are forbidden in the
1261 current context. */
1262 bool local_variables_forbidden_p;
1263
1264 /* TRUE if the declaration we are parsing is part of a
1265 linkage-specification of the form `extern string-literal
1266 declaration'. */
1267 bool in_unbraced_linkage_specification_p;
1268
1269 /* TRUE if we are presently parsing a declarator, after the
1270 direct-declarator. */
1271 bool in_declarator_p;
1272
1273 /* TRUE if we are presently parsing a template-argument-list. */
1274 bool in_template_argument_list_p;
1275
1276 /* TRUE if we are presently parsing the body of an
1277 iteration-statement. */
1278 bool in_iteration_statement_p;
1279
1280 /* TRUE if we are presently parsing the body of a switch
1281 statement. */
1282 bool in_switch_statement_p;
1283
1284 /* TRUE if we are parsing a type-id in an expression context. In
1285 such a situation, both "type (expr)" and "type (type)" are valid
1286 alternatives. */
1287 bool in_type_id_in_expr_p;
1288
1289 /* TRUE if we are currently in a header file where declarations are
1290 implicitly extern "C". */
1291 bool implicit_extern_c;
1292
1293 /* TRUE if strings in expressions should be translated to the execution
1294 character set. */
1295 bool translate_strings_p;
1296
1297 /* If non-NULL, then we are parsing a construct where new type
1298 definitions are not permitted. The string stored here will be
1299 issued as an error message if a type is defined. */
1300 const char *type_definition_forbidden_message;
1301
1302 /* A list of lists. The outer list is a stack, used for member
1303 functions of local classes. At each level there are two sub-list,
1304 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1305 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1306 TREE_VALUE's. The functions are chained in reverse declaration
1307 order.
1308
1309 The TREE_PURPOSE sublist contains those functions with default
1310 arguments that need post processing, and the TREE_VALUE sublist
1311 contains those functions with definitions that need post
1312 processing.
1313
1314 These lists can only be processed once the outermost class being
1315 defined is complete. */
1316 tree unparsed_functions_queues;
1317
1318 /* The number of classes whose definitions are currently in
1319 progress. */
1320 unsigned num_classes_being_defined;
1321
1322 /* The number of template parameter lists that apply directly to the
1323 current declaration. */
1324 unsigned num_template_parameter_lists;
1325 } cp_parser;
1326
1327 /* The type of a function that parses some kind of expression. */
1328 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1329
1330 /* Prototypes. */
1331
1332 /* Constructors and destructors. */
1333
1334 static cp_parser *cp_parser_new
1335 (void);
1336
1337 /* Routines to parse various constructs.
1338
1339 Those that return `tree' will return the error_mark_node (rather
1340 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1341 Sometimes, they will return an ordinary node if error-recovery was
1342 attempted, even though a parse error occurred. So, to check
1343 whether or not a parse error occurred, you should always use
1344 cp_parser_error_occurred. If the construct is optional (indicated
1345 either by an `_opt' in the name of the function that does the
1346 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1347 the construct is not present. */
1348
1349 /* Lexical conventions [gram.lex] */
1350
1351 static tree cp_parser_identifier
1352 (cp_parser *);
1353 static tree cp_parser_string_literal
1354 (cp_parser *, bool, bool);
1355
1356 /* Basic concepts [gram.basic] */
1357
1358 static bool cp_parser_translation_unit
1359 (cp_parser *);
1360
1361 /* Expressions [gram.expr] */
1362
1363 static tree cp_parser_primary_expression
1364 (cp_parser *, bool, cp_id_kind *, tree *);
1365 static tree cp_parser_id_expression
1366 (cp_parser *, bool, bool, bool *, bool);
1367 static tree cp_parser_unqualified_id
1368 (cp_parser *, bool, bool, bool);
1369 static tree cp_parser_nested_name_specifier_opt
1370 (cp_parser *, bool, bool, bool, bool);
1371 static tree cp_parser_nested_name_specifier
1372 (cp_parser *, bool, bool, bool, bool);
1373 static tree cp_parser_class_or_namespace_name
1374 (cp_parser *, bool, bool, bool, bool, bool);
1375 static tree cp_parser_postfix_expression
1376 (cp_parser *, bool, bool);
1377 static tree cp_parser_postfix_open_square_expression
1378 (cp_parser *, tree, bool);
1379 static tree cp_parser_postfix_dot_deref_expression
1380 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1381 static tree cp_parser_parenthesized_expression_list
1382 (cp_parser *, bool, bool, bool *);
1383 static void cp_parser_pseudo_destructor_name
1384 (cp_parser *, tree *, tree *);
1385 static tree cp_parser_unary_expression
1386 (cp_parser *, bool, bool);
1387 static enum tree_code cp_parser_unary_operator
1388 (cp_token *);
1389 static tree cp_parser_new_expression
1390 (cp_parser *);
1391 static tree cp_parser_new_placement
1392 (cp_parser *);
1393 static tree cp_parser_new_type_id
1394 (cp_parser *, tree *);
1395 static cp_declarator *cp_parser_new_declarator_opt
1396 (cp_parser *);
1397 static cp_declarator *cp_parser_direct_new_declarator
1398 (cp_parser *);
1399 static tree cp_parser_new_initializer
1400 (cp_parser *);
1401 static tree cp_parser_delete_expression
1402 (cp_parser *);
1403 static tree cp_parser_cast_expression
1404 (cp_parser *, bool, bool);
1405 static tree cp_parser_binary_expression
1406 (cp_parser *, bool);
1407 static tree cp_parser_question_colon_clause
1408 (cp_parser *, tree);
1409 static tree cp_parser_assignment_expression
1410 (cp_parser *, bool);
1411 static enum tree_code cp_parser_assignment_operator_opt
1412 (cp_parser *);
1413 static tree cp_parser_expression
1414 (cp_parser *, bool);
1415 static tree cp_parser_constant_expression
1416 (cp_parser *, bool, bool *);
1417 static tree cp_parser_builtin_offsetof
1418 (cp_parser *);
1419
1420 /* Statements [gram.stmt.stmt] */
1421
1422 static void cp_parser_statement
1423 (cp_parser *, tree);
1424 static tree cp_parser_labeled_statement
1425 (cp_parser *, tree);
1426 static tree cp_parser_expression_statement
1427 (cp_parser *, tree);
1428 static tree cp_parser_compound_statement
1429 (cp_parser *, tree, bool);
1430 static void cp_parser_statement_seq_opt
1431 (cp_parser *, tree);
1432 static tree cp_parser_selection_statement
1433 (cp_parser *);
1434 static tree cp_parser_condition
1435 (cp_parser *);
1436 static tree cp_parser_iteration_statement
1437 (cp_parser *);
1438 static void cp_parser_for_init_statement
1439 (cp_parser *);
1440 static tree cp_parser_jump_statement
1441 (cp_parser *);
1442 static void cp_parser_declaration_statement
1443 (cp_parser *);
1444
1445 static tree cp_parser_implicitly_scoped_statement
1446 (cp_parser *);
1447 static void cp_parser_already_scoped_statement
1448 (cp_parser *);
1449
1450 /* Declarations [gram.dcl.dcl] */
1451
1452 static void cp_parser_declaration_seq_opt
1453 (cp_parser *);
1454 static void cp_parser_declaration
1455 (cp_parser *);
1456 static void cp_parser_block_declaration
1457 (cp_parser *, bool);
1458 static void cp_parser_simple_declaration
1459 (cp_parser *, bool);
1460 static void cp_parser_decl_specifier_seq
1461 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1462 static tree cp_parser_storage_class_specifier_opt
1463 (cp_parser *);
1464 static tree cp_parser_function_specifier_opt
1465 (cp_parser *, cp_decl_specifier_seq *);
1466 static tree cp_parser_type_specifier
1467 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1468 int *, bool *);
1469 static tree cp_parser_simple_type_specifier
1470 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1471 static tree cp_parser_type_name
1472 (cp_parser *);
1473 static tree cp_parser_elaborated_type_specifier
1474 (cp_parser *, bool, bool);
1475 static tree cp_parser_enum_specifier
1476 (cp_parser *);
1477 static void cp_parser_enumerator_list
1478 (cp_parser *, tree);
1479 static void cp_parser_enumerator_definition
1480 (cp_parser *, tree);
1481 static tree cp_parser_namespace_name
1482 (cp_parser *);
1483 static void cp_parser_namespace_definition
1484 (cp_parser *);
1485 static void cp_parser_namespace_body
1486 (cp_parser *);
1487 static tree cp_parser_qualified_namespace_specifier
1488 (cp_parser *);
1489 static void cp_parser_namespace_alias_definition
1490 (cp_parser *);
1491 static void cp_parser_using_declaration
1492 (cp_parser *);
1493 static void cp_parser_using_directive
1494 (cp_parser *);
1495 static void cp_parser_asm_definition
1496 (cp_parser *);
1497 static void cp_parser_linkage_specification
1498 (cp_parser *);
1499
1500 /* Declarators [gram.dcl.decl] */
1501
1502 static tree cp_parser_init_declarator
1503 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1504 static cp_declarator *cp_parser_declarator
1505 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1506 static cp_declarator *cp_parser_direct_declarator
1507 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1508 static enum tree_code cp_parser_ptr_operator
1509 (cp_parser *, tree *, cp_cv_quals *);
1510 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1511 (cp_parser *);
1512 static tree cp_parser_declarator_id
1513 (cp_parser *);
1514 static tree cp_parser_type_id
1515 (cp_parser *);
1516 static void cp_parser_type_specifier_seq
1517 (cp_parser *, bool, cp_decl_specifier_seq *);
1518 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1519 (cp_parser *);
1520 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1521 (cp_parser *, bool *);
1522 static cp_parameter_declarator *cp_parser_parameter_declaration
1523 (cp_parser *, bool, bool *);
1524 static void cp_parser_function_body
1525 (cp_parser *);
1526 static tree cp_parser_initializer
1527 (cp_parser *, bool *, bool *);
1528 static tree cp_parser_initializer_clause
1529 (cp_parser *, bool *);
1530 static tree cp_parser_initializer_list
1531 (cp_parser *, bool *);
1532
1533 static bool cp_parser_ctor_initializer_opt_and_function_body
1534 (cp_parser *);
1535
1536 /* Classes [gram.class] */
1537
1538 static tree cp_parser_class_name
1539 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1540 static tree cp_parser_class_specifier
1541 (cp_parser *);
1542 static tree cp_parser_class_head
1543 (cp_parser *, bool *, tree *);
1544 static enum tag_types cp_parser_class_key
1545 (cp_parser *);
1546 static void cp_parser_member_specification_opt
1547 (cp_parser *);
1548 static void cp_parser_member_declaration
1549 (cp_parser *);
1550 static tree cp_parser_pure_specifier
1551 (cp_parser *);
1552 static tree cp_parser_constant_initializer
1553 (cp_parser *);
1554
1555 /* Derived classes [gram.class.derived] */
1556
1557 static tree cp_parser_base_clause
1558 (cp_parser *);
1559 static tree cp_parser_base_specifier
1560 (cp_parser *);
1561
1562 /* Special member functions [gram.special] */
1563
1564 static tree cp_parser_conversion_function_id
1565 (cp_parser *);
1566 static tree cp_parser_conversion_type_id
1567 (cp_parser *);
1568 static cp_declarator *cp_parser_conversion_declarator_opt
1569 (cp_parser *);
1570 static bool cp_parser_ctor_initializer_opt
1571 (cp_parser *);
1572 static void cp_parser_mem_initializer_list
1573 (cp_parser *);
1574 static tree cp_parser_mem_initializer
1575 (cp_parser *);
1576 static tree cp_parser_mem_initializer_id
1577 (cp_parser *);
1578
1579 /* Overloading [gram.over] */
1580
1581 static tree cp_parser_operator_function_id
1582 (cp_parser *);
1583 static tree cp_parser_operator
1584 (cp_parser *);
1585
1586 /* Templates [gram.temp] */
1587
1588 static void cp_parser_template_declaration
1589 (cp_parser *, bool);
1590 static tree cp_parser_template_parameter_list
1591 (cp_parser *);
1592 static tree cp_parser_template_parameter
1593 (cp_parser *, bool *);
1594 static tree cp_parser_type_parameter
1595 (cp_parser *);
1596 static tree cp_parser_template_id
1597 (cp_parser *, bool, bool, bool);
1598 static tree cp_parser_template_name
1599 (cp_parser *, bool, bool, bool, bool *);
1600 static tree cp_parser_template_argument_list
1601 (cp_parser *);
1602 static tree cp_parser_template_argument
1603 (cp_parser *);
1604 static void cp_parser_explicit_instantiation
1605 (cp_parser *);
1606 static void cp_parser_explicit_specialization
1607 (cp_parser *);
1608
1609 /* Exception handling [gram.exception] */
1610
1611 static tree cp_parser_try_block
1612 (cp_parser *);
1613 static bool cp_parser_function_try_block
1614 (cp_parser *);
1615 static void cp_parser_handler_seq
1616 (cp_parser *);
1617 static void cp_parser_handler
1618 (cp_parser *);
1619 static tree cp_parser_exception_declaration
1620 (cp_parser *);
1621 static tree cp_parser_throw_expression
1622 (cp_parser *);
1623 static tree cp_parser_exception_specification_opt
1624 (cp_parser *);
1625 static tree cp_parser_type_id_list
1626 (cp_parser *);
1627
1628 /* GNU Extensions */
1629
1630 static tree cp_parser_asm_specification_opt
1631 (cp_parser *);
1632 static tree cp_parser_asm_operand_list
1633 (cp_parser *);
1634 static tree cp_parser_asm_clobber_list
1635 (cp_parser *);
1636 static tree cp_parser_attributes_opt
1637 (cp_parser *);
1638 static tree cp_parser_attribute_list
1639 (cp_parser *);
1640 static bool cp_parser_extension_opt
1641 (cp_parser *, int *);
1642 static void cp_parser_label_declaration
1643 (cp_parser *);
1644
1645 /* Utility Routines */
1646
1647 static tree cp_parser_lookup_name
1648 (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
1649 static tree cp_parser_lookup_name_simple
1650 (cp_parser *, tree);
1651 static tree cp_parser_maybe_treat_template_as_class
1652 (tree, bool);
1653 static bool cp_parser_check_declarator_template_parameters
1654 (cp_parser *, cp_declarator *);
1655 static bool cp_parser_check_template_parameters
1656 (cp_parser *, unsigned);
1657 static tree cp_parser_simple_cast_expression
1658 (cp_parser *);
1659 static tree cp_parser_global_scope_opt
1660 (cp_parser *, bool);
1661 static bool cp_parser_constructor_declarator_p
1662 (cp_parser *, bool);
1663 static tree cp_parser_function_definition_from_specifiers_and_declarator
1664 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1665 static tree cp_parser_function_definition_after_declarator
1666 (cp_parser *, bool);
1667 static void cp_parser_template_declaration_after_export
1668 (cp_parser *, bool);
1669 static tree cp_parser_single_declaration
1670 (cp_parser *, bool, bool *);
1671 static tree cp_parser_functional_cast
1672 (cp_parser *, tree);
1673 static tree cp_parser_save_member_function_body
1674 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1675 static tree cp_parser_enclosed_template_argument_list
1676 (cp_parser *);
1677 static void cp_parser_save_default_args
1678 (cp_parser *, tree);
1679 static void cp_parser_late_parsing_for_member
1680 (cp_parser *, tree);
1681 static void cp_parser_late_parsing_default_args
1682 (cp_parser *, tree);
1683 static tree cp_parser_sizeof_operand
1684 (cp_parser *, enum rid);
1685 static bool cp_parser_declares_only_class_p
1686 (cp_parser *);
1687 static void cp_parser_set_storage_class
1688 (cp_decl_specifier_seq *, cp_storage_class);
1689 static void cp_parser_set_decl_spec_type
1690 (cp_decl_specifier_seq *, tree, bool);
1691 static bool cp_parser_friend_p
1692 (const cp_decl_specifier_seq *);
1693 static cp_token *cp_parser_require
1694 (cp_parser *, enum cpp_ttype, const char *);
1695 static cp_token *cp_parser_require_keyword
1696 (cp_parser *, enum rid, const char *);
1697 static bool cp_parser_token_starts_function_definition_p
1698 (cp_token *);
1699 static bool cp_parser_next_token_starts_class_definition_p
1700 (cp_parser *);
1701 static bool cp_parser_next_token_ends_template_argument_p
1702 (cp_parser *);
1703 static bool cp_parser_nth_token_starts_template_argument_list_p
1704 (cp_parser *, size_t);
1705 static enum tag_types cp_parser_token_is_class_key
1706 (cp_token *);
1707 static void cp_parser_check_class_key
1708 (enum tag_types, tree type);
1709 static void cp_parser_check_access_in_redeclaration
1710 (tree type);
1711 static bool cp_parser_optional_template_keyword
1712 (cp_parser *);
1713 static void cp_parser_pre_parsed_nested_name_specifier
1714 (cp_parser *);
1715 static void cp_parser_cache_group
1716 (cp_parser *, enum cpp_ttype, unsigned);
1717 static void cp_parser_parse_tentatively
1718 (cp_parser *);
1719 static void cp_parser_commit_to_tentative_parse
1720 (cp_parser *);
1721 static void cp_parser_abort_tentative_parse
1722 (cp_parser *);
1723 static bool cp_parser_parse_definitely
1724 (cp_parser *);
1725 static inline bool cp_parser_parsing_tentatively
1726 (cp_parser *);
1727 static bool cp_parser_uncommitted_to_tentative_parse_p
1728 (cp_parser *);
1729 static void cp_parser_error
1730 (cp_parser *, const char *);
1731 static void cp_parser_name_lookup_error
1732 (cp_parser *, tree, tree, const char *);
1733 static bool cp_parser_simulate_error
1734 (cp_parser *);
1735 static void cp_parser_check_type_definition
1736 (cp_parser *);
1737 static void cp_parser_check_for_definition_in_return_type
1738 (cp_declarator *, tree);
1739 static void cp_parser_check_for_invalid_template_id
1740 (cp_parser *, tree);
1741 static bool cp_parser_non_integral_constant_expression
1742 (cp_parser *, const char *);
1743 static void cp_parser_diagnose_invalid_type_name
1744 (cp_parser *, tree, tree);
1745 static bool cp_parser_parse_and_diagnose_invalid_type_name
1746 (cp_parser *);
1747 static int cp_parser_skip_to_closing_parenthesis
1748 (cp_parser *, bool, bool, bool);
1749 static void cp_parser_skip_to_end_of_statement
1750 (cp_parser *);
1751 static void cp_parser_consume_semicolon_at_end_of_statement
1752 (cp_parser *);
1753 static void cp_parser_skip_to_end_of_block_or_statement
1754 (cp_parser *);
1755 static void cp_parser_skip_to_closing_brace
1756 (cp_parser *);
1757 static void cp_parser_skip_until_found
1758 (cp_parser *, enum cpp_ttype, const char *);
1759 static bool cp_parser_error_occurred
1760 (cp_parser *);
1761 static bool cp_parser_allow_gnu_extensions_p
1762 (cp_parser *);
1763 static bool cp_parser_is_string_literal
1764 (cp_token *);
1765 static bool cp_parser_is_keyword
1766 (cp_token *, enum rid);
1767 static tree cp_parser_make_typename_type
1768 (cp_parser *, tree, tree);
1769
1770 /* Returns nonzero if we are parsing tentatively. */
1771
1772 static inline bool
1773 cp_parser_parsing_tentatively (cp_parser* parser)
1774 {
1775 return parser->context->next != NULL;
1776 }
1777
1778 /* Returns nonzero if TOKEN is a string literal. */
1779
1780 static bool
1781 cp_parser_is_string_literal (cp_token* token)
1782 {
1783 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1784 }
1785
1786 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1787
1788 static bool
1789 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1790 {
1791 return token->keyword == keyword;
1792 }
1793
1794 /* A minimum or maximum operator has been seen. As these are
1795 deprecated, issue a warning. */
1796
1797 static inline void
1798 cp_parser_warn_min_max (void)
1799 {
1800 if (warn_deprecated && !in_system_header)
1801 warning (0, "minimum/maximum operators are deprecated");
1802 }
1803
1804 /* If not parsing tentatively, issue a diagnostic of the form
1805 FILE:LINE: MESSAGE before TOKEN
1806 where TOKEN is the next token in the input stream. MESSAGE
1807 (specified by the caller) is usually of the form "expected
1808 OTHER-TOKEN". */
1809
1810 static void
1811 cp_parser_error (cp_parser* parser, const char* message)
1812 {
1813 if (!cp_parser_simulate_error (parser))
1814 {
1815 cp_token *token = cp_lexer_peek_token (parser->lexer);
1816 /* This diagnostic makes more sense if it is tagged to the line
1817 of the token we just peeked at. */
1818 cp_lexer_set_source_position_from_token (token);
1819 if (token->type == CPP_PRAGMA)
1820 {
1821 error ("%<#pragma%> is not allowed here");
1822 cp_lexer_purge_token (parser->lexer);
1823 return;
1824 }
1825 c_parse_error (message,
1826 /* Because c_parser_error does not understand
1827 CPP_KEYWORD, keywords are treated like
1828 identifiers. */
1829 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1830 token->value);
1831 }
1832 }
1833
1834 /* Issue an error about name-lookup failing. NAME is the
1835 IDENTIFIER_NODE DECL is the result of
1836 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1837 the thing that we hoped to find. */
1838
1839 static void
1840 cp_parser_name_lookup_error (cp_parser* parser,
1841 tree name,
1842 tree decl,
1843 const char* desired)
1844 {
1845 /* If name lookup completely failed, tell the user that NAME was not
1846 declared. */
1847 if (decl == error_mark_node)
1848 {
1849 if (parser->scope && parser->scope != global_namespace)
1850 error ("%<%D::%D%> has not been declared",
1851 parser->scope, name);
1852 else if (parser->scope == global_namespace)
1853 error ("%<::%D%> has not been declared", name);
1854 else if (parser->object_scope
1855 && !CLASS_TYPE_P (parser->object_scope))
1856 error ("request for member %qD in non-class type %qT",
1857 name, parser->object_scope);
1858 else if (parser->object_scope)
1859 error ("%<%T::%D%> has not been declared",
1860 parser->object_scope, name);
1861 else
1862 error ("%qD has not been declared", name);
1863 }
1864 else if (parser->scope && parser->scope != global_namespace)
1865 error ("%<%D::%D%> %s", parser->scope, name, desired);
1866 else if (parser->scope == global_namespace)
1867 error ("%<::%D%> %s", name, desired);
1868 else
1869 error ("%qD %s", name, desired);
1870 }
1871
1872 /* If we are parsing tentatively, remember that an error has occurred
1873 during this tentative parse. Returns true if the error was
1874 simulated; false if a message should be issued by the caller. */
1875
1876 static bool
1877 cp_parser_simulate_error (cp_parser* parser)
1878 {
1879 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1880 {
1881 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1882 return true;
1883 }
1884 return false;
1885 }
1886
1887 /* This function is called when a type is defined. If type
1888 definitions are forbidden at this point, an error message is
1889 issued. */
1890
1891 static void
1892 cp_parser_check_type_definition (cp_parser* parser)
1893 {
1894 /* If types are forbidden here, issue a message. */
1895 if (parser->type_definition_forbidden_message)
1896 /* Use `%s' to print the string in case there are any escape
1897 characters in the message. */
1898 error ("%s", parser->type_definition_forbidden_message);
1899 }
1900
1901 /* This function is called when the DECLARATOR is processed. The TYPE
1902 was a type defined in the decl-specifiers. If it is invalid to
1903 define a type in the decl-specifiers for DECLARATOR, an error is
1904 issued. */
1905
1906 static void
1907 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1908 tree type)
1909 {
1910 /* [dcl.fct] forbids type definitions in return types.
1911 Unfortunately, it's not easy to know whether or not we are
1912 processing a return type until after the fact. */
1913 while (declarator
1914 && (declarator->kind == cdk_pointer
1915 || declarator->kind == cdk_reference
1916 || declarator->kind == cdk_ptrmem))
1917 declarator = declarator->declarator;
1918 if (declarator
1919 && declarator->kind == cdk_function)
1920 {
1921 error ("new types may not be defined in a return type");
1922 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1923 type);
1924 }
1925 }
1926
1927 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1928 "<" in any valid C++ program. If the next token is indeed "<",
1929 issue a message warning the user about what appears to be an
1930 invalid attempt to form a template-id. */
1931
1932 static void
1933 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1934 tree type)
1935 {
1936 cp_token_position start = 0;
1937
1938 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1939 {
1940 if (TYPE_P (type))
1941 error ("%qT is not a template", type);
1942 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1943 error ("%qE is not a template", type);
1944 else
1945 error ("invalid template-id");
1946 /* Remember the location of the invalid "<". */
1947 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1948 start = cp_lexer_token_position (parser->lexer, true);
1949 /* Consume the "<". */
1950 cp_lexer_consume_token (parser->lexer);
1951 /* Parse the template arguments. */
1952 cp_parser_enclosed_template_argument_list (parser);
1953 /* Permanently remove the invalid template arguments so that
1954 this error message is not issued again. */
1955 if (start)
1956 cp_lexer_purge_tokens_after (parser->lexer, start);
1957 }
1958 }
1959
1960 /* If parsing an integral constant-expression, issue an error message
1961 about the fact that THING appeared and return true. Otherwise,
1962 return false. In either case, set
1963 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
1964
1965 static bool
1966 cp_parser_non_integral_constant_expression (cp_parser *parser,
1967 const char *thing)
1968 {
1969 parser->non_integral_constant_expression_p = true;
1970 if (parser->integral_constant_expression_p)
1971 {
1972 if (!parser->allow_non_integral_constant_expression_p)
1973 {
1974 error ("%s cannot appear in a constant-expression", thing);
1975 return true;
1976 }
1977 }
1978 return false;
1979 }
1980
1981 /* Emit a diagnostic for an invalid type name. SCOPE is the
1982 qualifying scope (or NULL, if none) for ID. This function commits
1983 to the current active tentative parse, if any. (Otherwise, the
1984 problematic construct might be encountered again later, resulting
1985 in duplicate error messages.) */
1986
1987 static void
1988 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1989 {
1990 tree decl, old_scope;
1991 /* Try to lookup the identifier. */
1992 old_scope = parser->scope;
1993 parser->scope = scope;
1994 decl = cp_parser_lookup_name_simple (parser, id);
1995 parser->scope = old_scope;
1996 /* If the lookup found a template-name, it means that the user forgot
1997 to specify an argument list. Emit an useful error message. */
1998 if (TREE_CODE (decl) == TEMPLATE_DECL)
1999 error ("invalid use of template-name %qE without an argument list",
2000 decl);
2001 else if (!parser->scope)
2002 {
2003 /* Issue an error message. */
2004 error ("%qE does not name a type", id);
2005 /* If we're in a template class, it's possible that the user was
2006 referring to a type from a base class. For example:
2007
2008 template <typename T> struct A { typedef T X; };
2009 template <typename T> struct B : public A<T> { X x; };
2010
2011 The user should have said "typename A<T>::X". */
2012 if (processing_template_decl && current_class_type
2013 && TYPE_BINFO (current_class_type))
2014 {
2015 tree b;
2016
2017 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2018 b;
2019 b = TREE_CHAIN (b))
2020 {
2021 tree base_type = BINFO_TYPE (b);
2022 if (CLASS_TYPE_P (base_type)
2023 && dependent_type_p (base_type))
2024 {
2025 tree field;
2026 /* Go from a particular instantiation of the
2027 template (which will have an empty TYPE_FIELDs),
2028 to the main version. */
2029 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2030 for (field = TYPE_FIELDS (base_type);
2031 field;
2032 field = TREE_CHAIN (field))
2033 if (TREE_CODE (field) == TYPE_DECL
2034 && DECL_NAME (field) == id)
2035 {
2036 inform ("(perhaps %<typename %T::%E%> was intended)",
2037 BINFO_TYPE (b), id);
2038 break;
2039 }
2040 if (field)
2041 break;
2042 }
2043 }
2044 }
2045 }
2046 /* Here we diagnose qualified-ids where the scope is actually correct,
2047 but the identifier does not resolve to a valid type name. */
2048 else
2049 {
2050 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2051 error ("%qE in namespace %qE does not name a type",
2052 id, parser->scope);
2053 else if (TYPE_P (parser->scope))
2054 error ("%qE in class %qT does not name a type", id, parser->scope);
2055 else
2056 gcc_unreachable ();
2057 }
2058 cp_parser_commit_to_tentative_parse (parser);
2059 }
2060
2061 /* Check for a common situation where a type-name should be present,
2062 but is not, and issue a sensible error message. Returns true if an
2063 invalid type-name was detected.
2064
2065 The situation handled by this function are variable declarations of the
2066 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2067 Usually, `ID' should name a type, but if we got here it means that it
2068 does not. We try to emit the best possible error message depending on
2069 how exactly the id-expression looks like.
2070 */
2071
2072 static bool
2073 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2074 {
2075 tree id;
2076
2077 cp_parser_parse_tentatively (parser);
2078 id = cp_parser_id_expression (parser,
2079 /*template_keyword_p=*/false,
2080 /*check_dependency_p=*/true,
2081 /*template_p=*/NULL,
2082 /*declarator_p=*/true);
2083 /* After the id-expression, there should be a plain identifier,
2084 otherwise this is not a simple variable declaration. Also, if
2085 the scope is dependent, we cannot do much. */
2086 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2087 || (parser->scope && TYPE_P (parser->scope)
2088 && dependent_type_p (parser->scope)))
2089 {
2090 cp_parser_abort_tentative_parse (parser);
2091 return false;
2092 }
2093 if (!cp_parser_parse_definitely (parser)
2094 || TREE_CODE (id) != IDENTIFIER_NODE)
2095 return false;
2096
2097 /* Emit a diagnostic for the invalid type. */
2098 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2099 /* Skip to the end of the declaration; there's no point in
2100 trying to process it. */
2101 cp_parser_skip_to_end_of_block_or_statement (parser);
2102 return true;
2103 }
2104
2105 /* Consume tokens up to, and including, the next non-nested closing `)'.
2106 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2107 are doing error recovery. Returns -1 if OR_COMMA is true and we
2108 found an unnested comma. */
2109
2110 static int
2111 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2112 bool recovering,
2113 bool or_comma,
2114 bool consume_paren)
2115 {
2116 unsigned paren_depth = 0;
2117 unsigned brace_depth = 0;
2118 int result;
2119
2120 if (recovering && !or_comma
2121 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2122 return 0;
2123
2124 while (true)
2125 {
2126 cp_token *token;
2127
2128 /* If we've run out of tokens, then there is no closing `)'. */
2129 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2130 {
2131 result = 0;
2132 break;
2133 }
2134
2135 token = cp_lexer_peek_token (parser->lexer);
2136
2137 /* This matches the processing in skip_to_end_of_statement. */
2138 if (token->type == CPP_SEMICOLON && !brace_depth)
2139 {
2140 result = 0;
2141 break;
2142 }
2143 if (token->type == CPP_OPEN_BRACE)
2144 ++brace_depth;
2145 if (token->type == CPP_CLOSE_BRACE)
2146 {
2147 if (!brace_depth--)
2148 {
2149 result = 0;
2150 break;
2151 }
2152 }
2153 if (recovering && or_comma && token->type == CPP_COMMA
2154 && !brace_depth && !paren_depth)
2155 {
2156 result = -1;
2157 break;
2158 }
2159
2160 if (!brace_depth)
2161 {
2162 /* If it is an `(', we have entered another level of nesting. */
2163 if (token->type == CPP_OPEN_PAREN)
2164 ++paren_depth;
2165 /* If it is a `)', then we might be done. */
2166 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2167 {
2168 if (consume_paren)
2169 cp_lexer_consume_token (parser->lexer);
2170 {
2171 result = 1;
2172 break;
2173 }
2174 }
2175 }
2176
2177 /* Consume the token. */
2178 cp_lexer_consume_token (parser->lexer);
2179 }
2180
2181 return result;
2182 }
2183
2184 /* Consume tokens until we reach the end of the current statement.
2185 Normally, that will be just before consuming a `;'. However, if a
2186 non-nested `}' comes first, then we stop before consuming that. */
2187
2188 static void
2189 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2190 {
2191 unsigned nesting_depth = 0;
2192
2193 while (true)
2194 {
2195 cp_token *token;
2196
2197 /* Peek at the next token. */
2198 token = cp_lexer_peek_token (parser->lexer);
2199 /* If we've run out of tokens, stop. */
2200 if (token->type == CPP_EOF)
2201 break;
2202 /* If the next token is a `;', we have reached the end of the
2203 statement. */
2204 if (token->type == CPP_SEMICOLON && !nesting_depth)
2205 break;
2206 /* If the next token is a non-nested `}', then we have reached
2207 the end of the current block. */
2208 if (token->type == CPP_CLOSE_BRACE)
2209 {
2210 /* If this is a non-nested `}', stop before consuming it.
2211 That way, when confronted with something like:
2212
2213 { 3 + }
2214
2215 we stop before consuming the closing `}', even though we
2216 have not yet reached a `;'. */
2217 if (nesting_depth == 0)
2218 break;
2219 /* If it is the closing `}' for a block that we have
2220 scanned, stop -- but only after consuming the token.
2221 That way given:
2222
2223 void f g () { ... }
2224 typedef int I;
2225
2226 we will stop after the body of the erroneously declared
2227 function, but before consuming the following `typedef'
2228 declaration. */
2229 if (--nesting_depth == 0)
2230 {
2231 cp_lexer_consume_token (parser->lexer);
2232 break;
2233 }
2234 }
2235 /* If it the next token is a `{', then we are entering a new
2236 block. Consume the entire block. */
2237 else if (token->type == CPP_OPEN_BRACE)
2238 ++nesting_depth;
2239 /* Consume the token. */
2240 cp_lexer_consume_token (parser->lexer);
2241 }
2242 }
2243
2244 /* This function is called at the end of a statement or declaration.
2245 If the next token is a semicolon, it is consumed; otherwise, error
2246 recovery is attempted. */
2247
2248 static void
2249 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2250 {
2251 /* Look for the trailing `;'. */
2252 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2253 {
2254 /* If there is additional (erroneous) input, skip to the end of
2255 the statement. */
2256 cp_parser_skip_to_end_of_statement (parser);
2257 /* If the next token is now a `;', consume it. */
2258 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2259 cp_lexer_consume_token (parser->lexer);
2260 }
2261 }
2262
2263 /* Skip tokens until we have consumed an entire block, or until we
2264 have consumed a non-nested `;'. */
2265
2266 static void
2267 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2268 {
2269 unsigned nesting_depth = 0;
2270
2271 while (true)
2272 {
2273 cp_token *token;
2274
2275 /* Peek at the next token. */
2276 token = cp_lexer_peek_token (parser->lexer);
2277 /* If we've run out of tokens, stop. */
2278 if (token->type == CPP_EOF)
2279 break;
2280 /* If the next token is a `;', we have reached the end of the
2281 statement. */
2282 if (token->type == CPP_SEMICOLON && !nesting_depth)
2283 {
2284 /* Consume the `;'. */
2285 cp_lexer_consume_token (parser->lexer);
2286 break;
2287 }
2288 /* Consume the token. */
2289 token = cp_lexer_consume_token (parser->lexer);
2290 /* If the next token is a non-nested `}', then we have reached
2291 the end of the current block. */
2292 if (token->type == CPP_CLOSE_BRACE
2293 && (nesting_depth == 0 || --nesting_depth == 0))
2294 break;
2295 /* If it the next token is a `{', then we are entering a new
2296 block. Consume the entire block. */
2297 if (token->type == CPP_OPEN_BRACE)
2298 ++nesting_depth;
2299 }
2300 }
2301
2302 /* Skip tokens until a non-nested closing curly brace is the next
2303 token. */
2304
2305 static void
2306 cp_parser_skip_to_closing_brace (cp_parser *parser)
2307 {
2308 unsigned nesting_depth = 0;
2309
2310 while (true)
2311 {
2312 cp_token *token;
2313
2314 /* Peek at the next token. */
2315 token = cp_lexer_peek_token (parser->lexer);
2316 /* If we've run out of tokens, stop. */
2317 if (token->type == CPP_EOF)
2318 break;
2319 /* If the next token is a non-nested `}', then we have reached
2320 the end of the current block. */
2321 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2322 break;
2323 /* If it the next token is a `{', then we are entering a new
2324 block. Consume the entire block. */
2325 else if (token->type == CPP_OPEN_BRACE)
2326 ++nesting_depth;
2327 /* Consume the token. */
2328 cp_lexer_consume_token (parser->lexer);
2329 }
2330 }
2331
2332 /* This is a simple wrapper around make_typename_type. When the id is
2333 an unresolved identifier node, we can provide a superior diagnostic
2334 using cp_parser_diagnose_invalid_type_name. */
2335
2336 static tree
2337 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2338 {
2339 tree result;
2340 if (TREE_CODE (id) == IDENTIFIER_NODE)
2341 {
2342 result = make_typename_type (scope, id, typename_type,
2343 /*complain=*/0);
2344 if (result == error_mark_node)
2345 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2346 return result;
2347 }
2348 return make_typename_type (scope, id, typename_type, tf_error);
2349 }
2350
2351
2352 /* Create a new C++ parser. */
2353
2354 static cp_parser *
2355 cp_parser_new (void)
2356 {
2357 cp_parser *parser;
2358 cp_lexer *lexer;
2359 unsigned i;
2360
2361 /* cp_lexer_new_main is called before calling ggc_alloc because
2362 cp_lexer_new_main might load a PCH file. */
2363 lexer = cp_lexer_new_main ();
2364
2365 /* Initialize the binops_by_token so that we can get the tree
2366 directly from the token. */
2367 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2368 binops_by_token[binops[i].token_type] = binops[i];
2369
2370 parser = GGC_CNEW (cp_parser);
2371 parser->lexer = lexer;
2372 parser->context = cp_parser_context_new (NULL);
2373
2374 /* For now, we always accept GNU extensions. */
2375 parser->allow_gnu_extensions_p = 1;
2376
2377 /* The `>' token is a greater-than operator, not the end of a
2378 template-id. */
2379 parser->greater_than_is_operator_p = true;
2380
2381 parser->default_arg_ok_p = true;
2382
2383 /* We are not parsing a constant-expression. */
2384 parser->integral_constant_expression_p = false;
2385 parser->allow_non_integral_constant_expression_p = false;
2386 parser->non_integral_constant_expression_p = false;
2387
2388 /* Local variable names are not forbidden. */
2389 parser->local_variables_forbidden_p = false;
2390
2391 /* We are not processing an `extern "C"' declaration. */
2392 parser->in_unbraced_linkage_specification_p = false;
2393
2394 /* We are not processing a declarator. */
2395 parser->in_declarator_p = false;
2396
2397 /* We are not processing a template-argument-list. */
2398 parser->in_template_argument_list_p = false;
2399
2400 /* We are not in an iteration statement. */
2401 parser->in_iteration_statement_p = false;
2402
2403 /* We are not in a switch statement. */
2404 parser->in_switch_statement_p = false;
2405
2406 /* We are not parsing a type-id inside an expression. */
2407 parser->in_type_id_in_expr_p = false;
2408
2409 /* Declarations aren't implicitly extern "C". */
2410 parser->implicit_extern_c = false;
2411
2412 /* String literals should be translated to the execution character set. */
2413 parser->translate_strings_p = true;
2414
2415 /* The unparsed function queue is empty. */
2416 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2417
2418 /* There are no classes being defined. */
2419 parser->num_classes_being_defined = 0;
2420
2421 /* No template parameters apply. */
2422 parser->num_template_parameter_lists = 0;
2423
2424 return parser;
2425 }
2426
2427 /* Create a cp_lexer structure which will emit the tokens in CACHE
2428 and push it onto the parser's lexer stack. This is used for delayed
2429 parsing of in-class method bodies and default arguments, and should
2430 not be confused with tentative parsing. */
2431 static void
2432 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2433 {
2434 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2435 lexer->next = parser->lexer;
2436 parser->lexer = lexer;
2437
2438 /* Move the current source position to that of the first token in the
2439 new lexer. */
2440 cp_lexer_set_source_position_from_token (lexer->next_token);
2441 }
2442
2443 /* Pop the top lexer off the parser stack. This is never used for the
2444 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2445 static void
2446 cp_parser_pop_lexer (cp_parser *parser)
2447 {
2448 cp_lexer *lexer = parser->lexer;
2449 parser->lexer = lexer->next;
2450 cp_lexer_destroy (lexer);
2451
2452 /* Put the current source position back where it was before this
2453 lexer was pushed. */
2454 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2455 }
2456
2457 /* Lexical conventions [gram.lex] */
2458
2459 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2460 identifier. */
2461
2462 static tree
2463 cp_parser_identifier (cp_parser* parser)
2464 {
2465 cp_token *token;
2466
2467 /* Look for the identifier. */
2468 token = cp_parser_require (parser, CPP_NAME, "identifier");
2469 /* Return the value. */
2470 return token ? token->value : error_mark_node;
2471 }
2472
2473 /* Parse a sequence of adjacent string constants. Returns a
2474 TREE_STRING representing the combined, nul-terminated string
2475 constant. If TRANSLATE is true, translate the string to the
2476 execution character set. If WIDE_OK is true, a wide string is
2477 invalid here.
2478
2479 C++98 [lex.string] says that if a narrow string literal token is
2480 adjacent to a wide string literal token, the behavior is undefined.
2481 However, C99 6.4.5p4 says that this results in a wide string literal.
2482 We follow C99 here, for consistency with the C front end.
2483
2484 This code is largely lifted from lex_string() in c-lex.c.
2485
2486 FUTURE: ObjC++ will need to handle @-strings here. */
2487 static tree
2488 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2489 {
2490 tree value;
2491 bool wide = false;
2492 size_t count;
2493 struct obstack str_ob;
2494 cpp_string str, istr, *strs;
2495 cp_token *tok;
2496
2497 tok = cp_lexer_peek_token (parser->lexer);
2498 if (!cp_parser_is_string_literal (tok))
2499 {
2500 cp_parser_error (parser, "expected string-literal");
2501 return error_mark_node;
2502 }
2503
2504 /* Try to avoid the overhead of creating and destroying an obstack
2505 for the common case of just one string. */
2506 if (!cp_parser_is_string_literal
2507 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2508 {
2509 cp_lexer_consume_token (parser->lexer);
2510
2511 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2512 str.len = TREE_STRING_LENGTH (tok->value);
2513 count = 1;
2514 if (tok->type == CPP_WSTRING)
2515 wide = true;
2516
2517 strs = &str;
2518 }
2519 else
2520 {
2521 gcc_obstack_init (&str_ob);
2522 count = 0;
2523
2524 do
2525 {
2526 cp_lexer_consume_token (parser->lexer);
2527 count++;
2528 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2529 str.len = TREE_STRING_LENGTH (tok->value);
2530 if (tok->type == CPP_WSTRING)
2531 wide = true;
2532
2533 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2534
2535 tok = cp_lexer_peek_token (parser->lexer);
2536 }
2537 while (cp_parser_is_string_literal (tok));
2538
2539 strs = (cpp_string *) obstack_finish (&str_ob);
2540 }
2541
2542 if (wide && !wide_ok)
2543 {
2544 cp_parser_error (parser, "a wide string is invalid in this context");
2545 wide = false;
2546 }
2547
2548 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2549 (parse_in, strs, count, &istr, wide))
2550 {
2551 value = build_string (istr.len, (char *)istr.text);
2552 free ((void *)istr.text);
2553
2554 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2555 value = fix_string_type (value);
2556 }
2557 else
2558 /* cpp_interpret_string has issued an error. */
2559 value = error_mark_node;
2560
2561 if (count > 1)
2562 obstack_free (&str_ob, 0);
2563
2564 return value;
2565 }
2566
2567
2568 /* Basic concepts [gram.basic] */
2569
2570 /* Parse a translation-unit.
2571
2572 translation-unit:
2573 declaration-seq [opt]
2574
2575 Returns TRUE if all went well. */
2576
2577 static bool
2578 cp_parser_translation_unit (cp_parser* parser)
2579 {
2580 /* The address of the first non-permanent object on the declarator
2581 obstack. */
2582 static void *declarator_obstack_base;
2583
2584 bool success;
2585
2586 /* Create the declarator obstack, if necessary. */
2587 if (!cp_error_declarator)
2588 {
2589 gcc_obstack_init (&declarator_obstack);
2590 /* Create the error declarator. */
2591 cp_error_declarator = make_declarator (cdk_error);
2592 /* Create the empty parameter list. */
2593 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2594 /* Remember where the base of the declarator obstack lies. */
2595 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2596 }
2597
2598 while (true)
2599 {
2600 cp_parser_declaration_seq_opt (parser);
2601
2602 /* If there are no tokens left then all went well. */
2603 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2604 {
2605 /* Get rid of the token array; we don't need it any more. */
2606 cp_lexer_destroy (parser->lexer);
2607 parser->lexer = NULL;
2608
2609 /* This file might have been a context that's implicitly extern
2610 "C". If so, pop the lang context. (Only relevant for PCH.) */
2611 if (parser->implicit_extern_c)
2612 {
2613 pop_lang_context ();
2614 parser->implicit_extern_c = false;
2615 }
2616
2617 /* Finish up. */
2618 finish_translation_unit ();
2619
2620 success = true;
2621 break;
2622 }
2623 else
2624 {
2625 cp_parser_error (parser, "expected declaration");
2626 success = false;
2627 break;
2628 }
2629 }
2630
2631 /* Make sure the declarator obstack was fully cleaned up. */
2632 gcc_assert (obstack_next_free (&declarator_obstack)
2633 == declarator_obstack_base);
2634
2635 /* All went well. */
2636 return success;
2637 }
2638
2639 /* Expressions [gram.expr] */
2640
2641 /* Parse a primary-expression.
2642
2643 primary-expression:
2644 literal
2645 this
2646 ( expression )
2647 id-expression
2648
2649 GNU Extensions:
2650
2651 primary-expression:
2652 ( compound-statement )
2653 __builtin_va_arg ( assignment-expression , type-id )
2654
2655 literal:
2656 __null
2657
2658 CAST_P is true if this primary expression is the target of a cast.
2659
2660 Returns a representation of the expression.
2661
2662 *IDK indicates what kind of id-expression (if any) was present.
2663
2664 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2665 used as the operand of a pointer-to-member. In that case,
2666 *QUALIFYING_CLASS gives the class that is used as the qualifying
2667 class in the pointer-to-member. */
2668
2669 static tree
2670 cp_parser_primary_expression (cp_parser *parser,
2671 bool cast_p,
2672 cp_id_kind *idk,
2673 tree *qualifying_class)
2674 {
2675 cp_token *token;
2676
2677 /* Assume the primary expression is not an id-expression. */
2678 *idk = CP_ID_KIND_NONE;
2679 /* And that it cannot be used as pointer-to-member. */
2680 *qualifying_class = NULL_TREE;
2681
2682 /* Peek at the next token. */
2683 token = cp_lexer_peek_token (parser->lexer);
2684 switch (token->type)
2685 {
2686 /* literal:
2687 integer-literal
2688 character-literal
2689 floating-literal
2690 string-literal
2691 boolean-literal */
2692 case CPP_CHAR:
2693 case CPP_WCHAR:
2694 case CPP_NUMBER:
2695 token = cp_lexer_consume_token (parser->lexer);
2696 /* Floating-point literals are only allowed in an integral
2697 constant expression if they are cast to an integral or
2698 enumeration type. */
2699 if (TREE_CODE (token->value) == REAL_CST
2700 && parser->integral_constant_expression_p
2701 && pedantic)
2702 {
2703 /* CAST_P will be set even in invalid code like "int(2.7 +
2704 ...)". Therefore, we have to check that the next token
2705 is sure to end the cast. */
2706 if (cast_p)
2707 {
2708 cp_token *next_token;
2709
2710 next_token = cp_lexer_peek_token (parser->lexer);
2711 if (/* The comma at the end of an
2712 enumerator-definition. */
2713 next_token->type != CPP_COMMA
2714 /* The curly brace at the end of an enum-specifier. */
2715 && next_token->type != CPP_CLOSE_BRACE
2716 /* The end of a statement. */
2717 && next_token->type != CPP_SEMICOLON
2718 /* The end of the cast-expression. */
2719 && next_token->type != CPP_CLOSE_PAREN
2720 /* The end of an array bound. */
2721 && next_token->type != CPP_CLOSE_SQUARE)
2722 cast_p = false;
2723 }
2724
2725 /* If we are within a cast, then the constraint that the
2726 cast is to an integral or enumeration type will be
2727 checked at that point. If we are not within a cast, then
2728 this code is invalid. */
2729 if (!cast_p)
2730 cp_parser_non_integral_constant_expression
2731 (parser, "floating-point literal");
2732 }
2733 return token->value;
2734
2735 case CPP_STRING:
2736 case CPP_WSTRING:
2737 /* ??? Should wide strings be allowed when parser->translate_strings_p
2738 is false (i.e. in attributes)? If not, we can kill the third
2739 argument to cp_parser_string_literal. */
2740 return cp_parser_string_literal (parser,
2741 parser->translate_strings_p,
2742 true);
2743
2744 case CPP_OPEN_PAREN:
2745 {
2746 tree expr;
2747 bool saved_greater_than_is_operator_p;
2748
2749 /* Consume the `('. */
2750 cp_lexer_consume_token (parser->lexer);
2751 /* Within a parenthesized expression, a `>' token is always
2752 the greater-than operator. */
2753 saved_greater_than_is_operator_p
2754 = parser->greater_than_is_operator_p;
2755 parser->greater_than_is_operator_p = true;
2756 /* If we see `( { ' then we are looking at the beginning of
2757 a GNU statement-expression. */
2758 if (cp_parser_allow_gnu_extensions_p (parser)
2759 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2760 {
2761 /* Statement-expressions are not allowed by the standard. */
2762 if (pedantic)
2763 pedwarn ("ISO C++ forbids braced-groups within expressions");
2764
2765 /* And they're not allowed outside of a function-body; you
2766 cannot, for example, write:
2767
2768 int i = ({ int j = 3; j + 1; });
2769
2770 at class or namespace scope. */
2771 if (!at_function_scope_p ())
2772 error ("statement-expressions are allowed only inside functions");
2773 /* Start the statement-expression. */
2774 expr = begin_stmt_expr ();
2775 /* Parse the compound-statement. */
2776 cp_parser_compound_statement (parser, expr, false);
2777 /* Finish up. */
2778 expr = finish_stmt_expr (expr, false);
2779 }
2780 else
2781 {
2782 /* Parse the parenthesized expression. */
2783 expr = cp_parser_expression (parser, cast_p);
2784 /* Let the front end know that this expression was
2785 enclosed in parentheses. This matters in case, for
2786 example, the expression is of the form `A::B', since
2787 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2788 not. */
2789 finish_parenthesized_expr (expr);
2790 }
2791 /* The `>' token might be the end of a template-id or
2792 template-parameter-list now. */
2793 parser->greater_than_is_operator_p
2794 = saved_greater_than_is_operator_p;
2795 /* Consume the `)'. */
2796 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2797 cp_parser_skip_to_end_of_statement (parser);
2798
2799 return expr;
2800 }
2801
2802 case CPP_KEYWORD:
2803 switch (token->keyword)
2804 {
2805 /* These two are the boolean literals. */
2806 case RID_TRUE:
2807 cp_lexer_consume_token (parser->lexer);
2808 return boolean_true_node;
2809 case RID_FALSE:
2810 cp_lexer_consume_token (parser->lexer);
2811 return boolean_false_node;
2812
2813 /* The `__null' literal. */
2814 case RID_NULL:
2815 cp_lexer_consume_token (parser->lexer);
2816 return null_node;
2817
2818 /* Recognize the `this' keyword. */
2819 case RID_THIS:
2820 cp_lexer_consume_token (parser->lexer);
2821 if (parser->local_variables_forbidden_p)
2822 {
2823 error ("%<this%> may not be used in this context");
2824 return error_mark_node;
2825 }
2826 /* Pointers cannot appear in constant-expressions. */
2827 if (cp_parser_non_integral_constant_expression (parser,
2828 "`this'"))
2829 return error_mark_node;
2830 return finish_this_expr ();
2831
2832 /* The `operator' keyword can be the beginning of an
2833 id-expression. */
2834 case RID_OPERATOR:
2835 goto id_expression;
2836
2837 case RID_FUNCTION_NAME:
2838 case RID_PRETTY_FUNCTION_NAME:
2839 case RID_C99_FUNCTION_NAME:
2840 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2841 __func__ are the names of variables -- but they are
2842 treated specially. Therefore, they are handled here,
2843 rather than relying on the generic id-expression logic
2844 below. Grammatically, these names are id-expressions.
2845
2846 Consume the token. */
2847 token = cp_lexer_consume_token (parser->lexer);
2848 /* Look up the name. */
2849 return finish_fname (token->value);
2850
2851 case RID_VA_ARG:
2852 {
2853 tree expression;
2854 tree type;
2855
2856 /* The `__builtin_va_arg' construct is used to handle
2857 `va_arg'. Consume the `__builtin_va_arg' token. */
2858 cp_lexer_consume_token (parser->lexer);
2859 /* Look for the opening `('. */
2860 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2861 /* Now, parse the assignment-expression. */
2862 expression = cp_parser_assignment_expression (parser,
2863 /*cast_p=*/false);
2864 /* Look for the `,'. */
2865 cp_parser_require (parser, CPP_COMMA, "`,'");
2866 /* Parse the type-id. */
2867 type = cp_parser_type_id (parser);
2868 /* Look for the closing `)'. */
2869 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2870 /* Using `va_arg' in a constant-expression is not
2871 allowed. */
2872 if (cp_parser_non_integral_constant_expression (parser,
2873 "`va_arg'"))
2874 return error_mark_node;
2875 return build_x_va_arg (expression, type);
2876 }
2877
2878 case RID_OFFSETOF:
2879 return cp_parser_builtin_offsetof (parser);
2880
2881 default:
2882 cp_parser_error (parser, "expected primary-expression");
2883 return error_mark_node;
2884 }
2885
2886 /* An id-expression can start with either an identifier, a
2887 `::' as the beginning of a qualified-id, or the "operator"
2888 keyword. */
2889 case CPP_NAME:
2890 case CPP_SCOPE:
2891 case CPP_TEMPLATE_ID:
2892 case CPP_NESTED_NAME_SPECIFIER:
2893 {
2894 tree id_expression;
2895 tree decl;
2896 const char *error_msg;
2897
2898 id_expression:
2899 /* Parse the id-expression. */
2900 id_expression
2901 = cp_parser_id_expression (parser,
2902 /*template_keyword_p=*/false,
2903 /*check_dependency_p=*/true,
2904 /*template_p=*/NULL,
2905 /*declarator_p=*/false);
2906 if (id_expression == error_mark_node)
2907 return error_mark_node;
2908 /* If we have a template-id, then no further lookup is
2909 required. If the template-id was for a template-class, we
2910 will sometimes have a TYPE_DECL at this point. */
2911 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2912 || TREE_CODE (id_expression) == TYPE_DECL)
2913 decl = id_expression;
2914 /* Look up the name. */
2915 else
2916 {
2917 bool ambiguous_p;
2918
2919 decl = cp_parser_lookup_name (parser, id_expression,
2920 none_type,
2921 /*is_template=*/false,
2922 /*is_namespace=*/false,
2923 /*check_dependency=*/true,
2924 &ambiguous_p);
2925 /* If the lookup was ambiguous, an error will already have
2926 been issued. */
2927 if (ambiguous_p)
2928 return error_mark_node;
2929 /* If name lookup gives us a SCOPE_REF, then the
2930 qualifying scope was dependent. Just propagate the
2931 name. */
2932 if (TREE_CODE (decl) == SCOPE_REF)
2933 {
2934 if (TYPE_P (TREE_OPERAND (decl, 0)))
2935 *qualifying_class = TREE_OPERAND (decl, 0);
2936 return decl;
2937 }
2938 /* Check to see if DECL is a local variable in a context
2939 where that is forbidden. */
2940 if (parser->local_variables_forbidden_p
2941 && local_variable_p (decl))
2942 {
2943 /* It might be that we only found DECL because we are
2944 trying to be generous with pre-ISO scoping rules.
2945 For example, consider:
2946
2947 int i;
2948 void g() {
2949 for (int i = 0; i < 10; ++i) {}
2950 extern void f(int j = i);
2951 }
2952
2953 Here, name look up will originally find the out
2954 of scope `i'. We need to issue a warning message,
2955 but then use the global `i'. */
2956 decl = check_for_out_of_scope_variable (decl);
2957 if (local_variable_p (decl))
2958 {
2959 error ("local variable %qD may not appear in this context",
2960 decl);
2961 return error_mark_node;
2962 }
2963 }
2964 }
2965
2966 decl = finish_id_expression (id_expression, decl, parser->scope,
2967 idk, qualifying_class,
2968 parser->integral_constant_expression_p,
2969 parser->allow_non_integral_constant_expression_p,
2970 &parser->non_integral_constant_expression_p,
2971 &error_msg);
2972 if (error_msg)
2973 cp_parser_error (parser, error_msg);
2974 return decl;
2975 }
2976
2977 /* Anything else is an error. */
2978 default:
2979 cp_parser_error (parser, "expected primary-expression");
2980 return error_mark_node;
2981 }
2982 }
2983
2984 /* Parse an id-expression.
2985
2986 id-expression:
2987 unqualified-id
2988 qualified-id
2989
2990 qualified-id:
2991 :: [opt] nested-name-specifier template [opt] unqualified-id
2992 :: identifier
2993 :: operator-function-id
2994 :: template-id
2995
2996 Return a representation of the unqualified portion of the
2997 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2998 a `::' or nested-name-specifier.
2999
3000 Often, if the id-expression was a qualified-id, the caller will
3001 want to make a SCOPE_REF to represent the qualified-id. This
3002 function does not do this in order to avoid wastefully creating
3003 SCOPE_REFs when they are not required.
3004
3005 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3006 `template' keyword.
3007
3008 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3009 uninstantiated templates.
3010
3011 If *TEMPLATE_P is non-NULL, it is set to true iff the
3012 `template' keyword is used to explicitly indicate that the entity
3013 named is a template.
3014
3015 If DECLARATOR_P is true, the id-expression is appearing as part of
3016 a declarator, rather than as part of an expression. */
3017
3018 static tree
3019 cp_parser_id_expression (cp_parser *parser,
3020 bool template_keyword_p,
3021 bool check_dependency_p,
3022 bool *template_p,
3023 bool declarator_p)
3024 {
3025 bool global_scope_p;
3026 bool nested_name_specifier_p;
3027
3028 /* Assume the `template' keyword was not used. */
3029 if (template_p)
3030 *template_p = false;
3031
3032 /* Look for the optional `::' operator. */
3033 global_scope_p
3034 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3035 != NULL_TREE);
3036 /* Look for the optional nested-name-specifier. */
3037 nested_name_specifier_p
3038 = (cp_parser_nested_name_specifier_opt (parser,
3039 /*typename_keyword_p=*/false,
3040 check_dependency_p,
3041 /*type_p=*/false,
3042 declarator_p)
3043 != NULL_TREE);
3044 /* If there is a nested-name-specifier, then we are looking at
3045 the first qualified-id production. */
3046 if (nested_name_specifier_p)
3047 {
3048 tree saved_scope;
3049 tree saved_object_scope;
3050 tree saved_qualifying_scope;
3051 tree unqualified_id;
3052 bool is_template;
3053
3054 /* See if the next token is the `template' keyword. */
3055 if (!template_p)
3056 template_p = &is_template;
3057 *template_p = cp_parser_optional_template_keyword (parser);
3058 /* Name lookup we do during the processing of the
3059 unqualified-id might obliterate SCOPE. */
3060 saved_scope = parser->scope;
3061 saved_object_scope = parser->object_scope;
3062 saved_qualifying_scope = parser->qualifying_scope;
3063 /* Process the final unqualified-id. */
3064 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3065 check_dependency_p,
3066 declarator_p);
3067 /* Restore the SAVED_SCOPE for our caller. */
3068 parser->scope = saved_scope;
3069 parser->object_scope = saved_object_scope;
3070 parser->qualifying_scope = saved_qualifying_scope;
3071
3072 return unqualified_id;
3073 }
3074 /* Otherwise, if we are in global scope, then we are looking at one
3075 of the other qualified-id productions. */
3076 else if (global_scope_p)
3077 {
3078 cp_token *token;
3079 tree id;
3080
3081 /* Peek at the next token. */
3082 token = cp_lexer_peek_token (parser->lexer);
3083
3084 /* If it's an identifier, and the next token is not a "<", then
3085 we can avoid the template-id case. This is an optimization
3086 for this common case. */
3087 if (token->type == CPP_NAME
3088 && !cp_parser_nth_token_starts_template_argument_list_p
3089 (parser, 2))
3090 return cp_parser_identifier (parser);
3091
3092 cp_parser_parse_tentatively (parser);
3093 /* Try a template-id. */
3094 id = cp_parser_template_id (parser,
3095 /*template_keyword_p=*/false,
3096 /*check_dependency_p=*/true,
3097 declarator_p);
3098 /* If that worked, we're done. */
3099 if (cp_parser_parse_definitely (parser))
3100 return id;
3101
3102 /* Peek at the next token. (Changes in the token buffer may
3103 have invalidated the pointer obtained above.) */
3104 token = cp_lexer_peek_token (parser->lexer);
3105
3106 switch (token->type)
3107 {
3108 case CPP_NAME:
3109 return cp_parser_identifier (parser);
3110
3111 case CPP_KEYWORD:
3112 if (token->keyword == RID_OPERATOR)
3113 return cp_parser_operator_function_id (parser);
3114 /* Fall through. */
3115
3116 default:
3117 cp_parser_error (parser, "expected id-expression");
3118 return error_mark_node;
3119 }
3120 }
3121 else
3122 return cp_parser_unqualified_id (parser, template_keyword_p,
3123 /*check_dependency_p=*/true,
3124 declarator_p);
3125 }
3126
3127 /* Parse an unqualified-id.
3128
3129 unqualified-id:
3130 identifier
3131 operator-function-id
3132 conversion-function-id
3133 ~ class-name
3134 template-id
3135
3136 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3137 keyword, in a construct like `A::template ...'.
3138
3139 Returns a representation of unqualified-id. For the `identifier'
3140 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3141 production a BIT_NOT_EXPR is returned; the operand of the
3142 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3143 other productions, see the documentation accompanying the
3144 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3145 names are looked up in uninstantiated templates. If DECLARATOR_P
3146 is true, the unqualified-id is appearing as part of a declarator,
3147 rather than as part of an expression. */
3148
3149 static tree
3150 cp_parser_unqualified_id (cp_parser* parser,
3151 bool template_keyword_p,
3152 bool check_dependency_p,
3153 bool declarator_p)
3154 {
3155 cp_token *token;
3156
3157 /* Peek at the next token. */
3158 token = cp_lexer_peek_token (parser->lexer);
3159
3160 switch (token->type)
3161 {
3162 case CPP_NAME:
3163 {
3164 tree id;
3165
3166 /* We don't know yet whether or not this will be a
3167 template-id. */
3168 cp_parser_parse_tentatively (parser);
3169 /* Try a template-id. */
3170 id = cp_parser_template_id (parser, template_keyword_p,
3171 check_dependency_p,
3172 declarator_p);
3173 /* If it worked, we're done. */
3174 if (cp_parser_parse_definitely (parser))
3175 return id;
3176 /* Otherwise, it's an ordinary identifier. */
3177 return cp_parser_identifier (parser);
3178 }
3179
3180 case CPP_TEMPLATE_ID:
3181 return cp_parser_template_id (parser, template_keyword_p,
3182 check_dependency_p,
3183 declarator_p);
3184
3185 case CPP_COMPL:
3186 {
3187 tree type_decl;
3188 tree qualifying_scope;
3189 tree object_scope;
3190 tree scope;
3191 bool done;
3192
3193 /* Consume the `~' token. */
3194 cp_lexer_consume_token (parser->lexer);
3195 /* Parse the class-name. The standard, as written, seems to
3196 say that:
3197
3198 template <typename T> struct S { ~S (); };
3199 template <typename T> S<T>::~S() {}
3200
3201 is invalid, since `~' must be followed by a class-name, but
3202 `S<T>' is dependent, and so not known to be a class.
3203 That's not right; we need to look in uninstantiated
3204 templates. A further complication arises from:
3205
3206 template <typename T> void f(T t) {
3207 t.T::~T();
3208 }
3209
3210 Here, it is not possible to look up `T' in the scope of `T'
3211 itself. We must look in both the current scope, and the
3212 scope of the containing complete expression.
3213
3214 Yet another issue is:
3215
3216 struct S {
3217 int S;
3218 ~S();
3219 };
3220
3221 S::~S() {}
3222
3223 The standard does not seem to say that the `S' in `~S'
3224 should refer to the type `S' and not the data member
3225 `S::S'. */
3226
3227 /* DR 244 says that we look up the name after the "~" in the
3228 same scope as we looked up the qualifying name. That idea
3229 isn't fully worked out; it's more complicated than that. */
3230 scope = parser->scope;
3231 object_scope = parser->object_scope;
3232 qualifying_scope = parser->qualifying_scope;
3233
3234 /* If the name is of the form "X::~X" it's OK. */
3235 if (scope && TYPE_P (scope)
3236 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3237 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3238 == CPP_OPEN_PAREN)
3239 && (cp_lexer_peek_token (parser->lexer)->value
3240 == TYPE_IDENTIFIER (scope)))
3241 {
3242 cp_lexer_consume_token (parser->lexer);
3243 return build_nt (BIT_NOT_EXPR, scope);
3244 }
3245
3246 /* If there was an explicit qualification (S::~T), first look
3247 in the scope given by the qualification (i.e., S). */
3248 done = false;
3249 type_decl = NULL_TREE;
3250 if (scope)
3251 {
3252 cp_parser_parse_tentatively (parser);
3253 type_decl = cp_parser_class_name (parser,
3254 /*typename_keyword_p=*/false,
3255 /*template_keyword_p=*/false,
3256 none_type,
3257 /*check_dependency=*/false,
3258 /*class_head_p=*/false,
3259 declarator_p);
3260 if (cp_parser_parse_definitely (parser))
3261 done = true;
3262 }
3263 /* In "N::S::~S", look in "N" as well. */
3264 if (!done && scope && qualifying_scope)
3265 {
3266 cp_parser_parse_tentatively (parser);
3267 parser->scope = qualifying_scope;
3268 parser->object_scope = NULL_TREE;
3269 parser->qualifying_scope = NULL_TREE;
3270 type_decl
3271 = cp_parser_class_name (parser,
3272 /*typename_keyword_p=*/false,
3273 /*template_keyword_p=*/false,
3274 none_type,
3275 /*check_dependency=*/false,
3276 /*class_head_p=*/false,
3277 declarator_p);
3278 if (cp_parser_parse_definitely (parser))
3279 done = true;
3280 }
3281 /* In "p->S::~T", look in the scope given by "*p" as well. */
3282 else if (!done && object_scope)
3283 {
3284 cp_parser_parse_tentatively (parser);
3285 parser->scope = object_scope;
3286 parser->object_scope = NULL_TREE;
3287 parser->qualifying_scope = NULL_TREE;
3288 type_decl
3289 = cp_parser_class_name (parser,
3290 /*typename_keyword_p=*/false,
3291 /*template_keyword_p=*/false,
3292 none_type,
3293 /*check_dependency=*/false,
3294 /*class_head_p=*/false,
3295 declarator_p);
3296 if (cp_parser_parse_definitely (parser))
3297 done = true;
3298 }
3299 /* Look in the surrounding context. */
3300 if (!done)
3301 {
3302 parser->scope = NULL_TREE;
3303 parser->object_scope = NULL_TREE;
3304 parser->qualifying_scope = NULL_TREE;
3305 type_decl
3306 = cp_parser_class_name (parser,
3307 /*typename_keyword_p=*/false,
3308 /*template_keyword_p=*/false,
3309 none_type,
3310 /*check_dependency=*/false,
3311 /*class_head_p=*/false,
3312 declarator_p);
3313 }
3314 /* If an error occurred, assume that the name of the
3315 destructor is the same as the name of the qualifying
3316 class. That allows us to keep parsing after running
3317 into ill-formed destructor names. */
3318 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3319 return build_nt (BIT_NOT_EXPR, scope);
3320 else if (type_decl == error_mark_node)
3321 return error_mark_node;
3322
3323 /* [class.dtor]
3324
3325 A typedef-name that names a class shall not be used as the
3326 identifier in the declarator for a destructor declaration. */
3327 if (declarator_p
3328 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3329 && !DECL_SELF_REFERENCE_P (type_decl)
3330 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3331 error ("typedef-name %qD used as destructor declarator",
3332 type_decl);
3333
3334 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3335 }
3336
3337 case CPP_KEYWORD:
3338 if (token->keyword == RID_OPERATOR)
3339 {
3340 tree id;
3341
3342 /* This could be a template-id, so we try that first. */
3343 cp_parser_parse_tentatively (parser);
3344 /* Try a template-id. */
3345 id = cp_parser_template_id (parser, template_keyword_p,
3346 /*check_dependency_p=*/true,
3347 declarator_p);
3348 /* If that worked, we're done. */
3349 if (cp_parser_parse_definitely (parser))
3350 return id;
3351 /* We still don't know whether we're looking at an
3352 operator-function-id or a conversion-function-id. */
3353 cp_parser_parse_tentatively (parser);
3354 /* Try an operator-function-id. */
3355 id = cp_parser_operator_function_id (parser);
3356 /* If that didn't work, try a conversion-function-id. */
3357 if (!cp_parser_parse_definitely (parser))
3358 id = cp_parser_conversion_function_id (parser);
3359
3360 return id;
3361 }
3362 /* Fall through. */
3363
3364 default:
3365 cp_parser_error (parser, "expected unqualified-id");
3366 return error_mark_node;
3367 }
3368 }
3369
3370 /* Parse an (optional) nested-name-specifier.
3371
3372 nested-name-specifier:
3373 class-or-namespace-name :: nested-name-specifier [opt]
3374 class-or-namespace-name :: template nested-name-specifier [opt]
3375
3376 PARSER->SCOPE should be set appropriately before this function is
3377 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3378 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3379 in name lookups.
3380
3381 Sets PARSER->SCOPE to the class (TYPE) or namespace
3382 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3383 it unchanged if there is no nested-name-specifier. Returns the new
3384 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3385
3386 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3387 part of a declaration and/or decl-specifier. */
3388
3389 static tree
3390 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3391 bool typename_keyword_p,
3392 bool check_dependency_p,
3393 bool type_p,
3394 bool is_declaration)
3395 {
3396 bool success = false;
3397 tree access_check = NULL_TREE;
3398 cp_token_position start = 0;
3399 cp_token *token;
3400
3401 /* If the next token corresponds to a nested name specifier, there
3402 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3403 false, it may have been true before, in which case something
3404 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3405 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3406 CHECK_DEPENDENCY_P is false, we have to fall through into the
3407 main loop. */
3408 if (check_dependency_p
3409 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3410 {
3411 cp_parser_pre_parsed_nested_name_specifier (parser);
3412 return parser->scope;
3413 }
3414
3415 /* Remember where the nested-name-specifier starts. */
3416 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3417 start = cp_lexer_token_position (parser->lexer, false);
3418
3419 push_deferring_access_checks (dk_deferred);
3420
3421 while (true)
3422 {
3423 tree new_scope;
3424 tree old_scope;
3425 tree saved_qualifying_scope;
3426 bool template_keyword_p;
3427
3428 /* Spot cases that cannot be the beginning of a
3429 nested-name-specifier. */
3430 token = cp_lexer_peek_token (parser->lexer);
3431
3432 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3433 the already parsed nested-name-specifier. */
3434 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3435 {
3436 /* Grab the nested-name-specifier and continue the loop. */
3437 cp_parser_pre_parsed_nested_name_specifier (parser);
3438 success = true;
3439 continue;
3440 }
3441
3442 /* Spot cases that cannot be the beginning of a
3443 nested-name-specifier. On the second and subsequent times
3444 through the loop, we look for the `template' keyword. */
3445 if (success && token->keyword == RID_TEMPLATE)
3446 ;
3447 /* A template-id can start a nested-name-specifier. */
3448 else if (token->type == CPP_TEMPLATE_ID)
3449 ;
3450 else
3451 {
3452 /* If the next token is not an identifier, then it is
3453 definitely not a class-or-namespace-name. */
3454 if (token->type != CPP_NAME)
3455 break;
3456 /* If the following token is neither a `<' (to begin a
3457 template-id), nor a `::', then we are not looking at a
3458 nested-name-specifier. */
3459 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3460 if (token->type != CPP_SCOPE
3461 && !cp_parser_nth_token_starts_template_argument_list_p
3462 (parser, 2))
3463 break;
3464 }
3465
3466 /* The nested-name-specifier is optional, so we parse
3467 tentatively. */
3468 cp_parser_parse_tentatively (parser);
3469
3470 /* Look for the optional `template' keyword, if this isn't the
3471 first time through the loop. */
3472 if (success)
3473 template_keyword_p = cp_parser_optional_template_keyword (parser);
3474 else
3475 template_keyword_p = false;
3476
3477 /* Save the old scope since the name lookup we are about to do
3478 might destroy it. */
3479 old_scope = parser->scope;
3480 saved_qualifying_scope = parser->qualifying_scope;
3481 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3482 look up names in "X<T>::I" in order to determine that "Y" is
3483 a template. So, if we have a typename at this point, we make
3484 an effort to look through it. */
3485 if (is_declaration
3486 && !typename_keyword_p
3487 && parser->scope
3488 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3489 parser->scope = resolve_typename_type (parser->scope,
3490 /*only_current_p=*/false);
3491 /* Parse the qualifying entity. */
3492 new_scope
3493 = cp_parser_class_or_namespace_name (parser,
3494 typename_keyword_p,
3495 template_keyword_p,
3496 check_dependency_p,
3497 type_p,
3498 is_declaration);
3499 /* Look for the `::' token. */
3500 cp_parser_require (parser, CPP_SCOPE, "`::'");
3501
3502 /* If we found what we wanted, we keep going; otherwise, we're
3503 done. */
3504 if (!cp_parser_parse_definitely (parser))
3505 {
3506 bool error_p = false;
3507
3508 /* Restore the OLD_SCOPE since it was valid before the
3509 failed attempt at finding the last
3510 class-or-namespace-name. */
3511 parser->scope = old_scope;
3512 parser->qualifying_scope = saved_qualifying_scope;
3513 /* If the next token is an identifier, and the one after
3514 that is a `::', then any valid interpretation would have
3515 found a class-or-namespace-name. */
3516 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3517 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3518 == CPP_SCOPE)
3519 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3520 != CPP_COMPL))
3521 {
3522 token = cp_lexer_consume_token (parser->lexer);
3523 if (!error_p)
3524 {
3525 tree decl;
3526
3527 decl = cp_parser_lookup_name_simple (parser, token->value);
3528 if (TREE_CODE (decl) == TEMPLATE_DECL)
3529 error ("%qD used without template parameters", decl);
3530 else
3531 cp_parser_name_lookup_error
3532 (parser, token->value, decl,
3533 "is not a class or namespace");
3534 parser->scope = NULL_TREE;
3535 error_p = true;
3536 /* Treat this as a successful nested-name-specifier
3537 due to:
3538
3539 [basic.lookup.qual]
3540
3541 If the name found is not a class-name (clause
3542 _class_) or namespace-name (_namespace.def_), the
3543 program is ill-formed. */
3544 success = true;
3545 }
3546 cp_lexer_consume_token (parser->lexer);
3547 }
3548 break;
3549 }
3550
3551 /* We've found one valid nested-name-specifier. */
3552 success = true;
3553 /* Make sure we look in the right scope the next time through
3554 the loop. */
3555 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3556 ? TREE_TYPE (new_scope)
3557 : new_scope);
3558 /* If it is a class scope, try to complete it; we are about to
3559 be looking up names inside the class. */
3560 if (TYPE_P (parser->scope)
3561 /* Since checking types for dependency can be expensive,
3562 avoid doing it if the type is already complete. */
3563 && !COMPLETE_TYPE_P (parser->scope)
3564 /* Do not try to complete dependent types. */
3565 && !dependent_type_p (parser->scope))
3566 complete_type (parser->scope);
3567 }
3568
3569 /* Retrieve any deferred checks. Do not pop this access checks yet
3570 so the memory will not be reclaimed during token replacing below. */
3571 access_check = get_deferred_access_checks ();
3572
3573 /* If parsing tentatively, replace the sequence of tokens that makes
3574 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3575 token. That way, should we re-parse the token stream, we will
3576 not have to repeat the effort required to do the parse, nor will
3577 we issue duplicate error messages. */
3578 if (success && start)
3579 {
3580 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3581
3582 /* Reset the contents of the START token. */
3583 token->type = CPP_NESTED_NAME_SPECIFIER;
3584 token->value = build_tree_list (access_check, parser->scope);
3585 TREE_TYPE (token->value) = parser->qualifying_scope;
3586 token->keyword = RID_MAX;
3587
3588 /* Purge all subsequent tokens. */
3589 cp_lexer_purge_tokens_after (parser->lexer, start);
3590 }
3591
3592 pop_deferring_access_checks ();
3593 return success ? parser->scope : NULL_TREE;
3594 }
3595
3596 /* Parse a nested-name-specifier. See
3597 cp_parser_nested_name_specifier_opt for details. This function
3598 behaves identically, except that it will an issue an error if no
3599 nested-name-specifier is present, and it will return
3600 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3601 is present. */
3602
3603 static tree
3604 cp_parser_nested_name_specifier (cp_parser *parser,
3605 bool typename_keyword_p,
3606 bool check_dependency_p,
3607 bool type_p,
3608 bool is_declaration)
3609 {
3610 tree scope;
3611
3612 /* Look for the nested-name-specifier. */
3613 scope = cp_parser_nested_name_specifier_opt (parser,
3614 typename_keyword_p,
3615 check_dependency_p,
3616 type_p,
3617 is_declaration);
3618 /* If it was not present, issue an error message. */
3619 if (!scope)
3620 {
3621 cp_parser_error (parser, "expected nested-name-specifier");
3622 parser->scope = NULL_TREE;
3623 return error_mark_node;
3624 }
3625
3626 return scope;
3627 }
3628
3629 /* Parse a class-or-namespace-name.
3630
3631 class-or-namespace-name:
3632 class-name
3633 namespace-name
3634
3635 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3636 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3637 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3638 TYPE_P is TRUE iff the next name should be taken as a class-name,
3639 even the same name is declared to be another entity in the same
3640 scope.
3641
3642 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3643 specified by the class-or-namespace-name. If neither is found the
3644 ERROR_MARK_NODE is returned. */
3645
3646 static tree
3647 cp_parser_class_or_namespace_name (cp_parser *parser,
3648 bool typename_keyword_p,
3649 bool template_keyword_p,
3650 bool check_dependency_p,
3651 bool type_p,
3652 bool is_declaration)
3653 {
3654 tree saved_scope;
3655 tree saved_qualifying_scope;
3656 tree saved_object_scope;
3657 tree scope;
3658 bool only_class_p;
3659
3660 /* Before we try to parse the class-name, we must save away the
3661 current PARSER->SCOPE since cp_parser_class_name will destroy
3662 it. */
3663 saved_scope = parser->scope;
3664 saved_qualifying_scope = parser->qualifying_scope;
3665 saved_object_scope = parser->object_scope;
3666 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3667 there is no need to look for a namespace-name. */
3668 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3669 if (!only_class_p)
3670 cp_parser_parse_tentatively (parser);
3671 scope = cp_parser_class_name (parser,
3672 typename_keyword_p,
3673 template_keyword_p,
3674 type_p ? class_type : none_type,
3675 check_dependency_p,
3676 /*class_head_p=*/false,
3677 is_declaration);
3678 /* If that didn't work, try for a namespace-name. */
3679 if (!only_class_p && !cp_parser_parse_definitely (parser))
3680 {
3681 /* Restore the saved scope. */
3682 parser->scope = saved_scope;
3683 parser->qualifying_scope = saved_qualifying_scope;
3684 parser->object_scope = saved_object_scope;
3685 /* If we are not looking at an identifier followed by the scope
3686 resolution operator, then this is not part of a
3687 nested-name-specifier. (Note that this function is only used
3688 to parse the components of a nested-name-specifier.) */
3689 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3690 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3691 return error_mark_node;
3692 scope = cp_parser_namespace_name (parser);
3693 }
3694
3695 return scope;
3696 }
3697
3698 /* Parse a postfix-expression.
3699
3700 postfix-expression:
3701 primary-expression
3702 postfix-expression [ expression ]
3703 postfix-expression ( expression-list [opt] )
3704 simple-type-specifier ( expression-list [opt] )
3705 typename :: [opt] nested-name-specifier identifier
3706 ( expression-list [opt] )
3707 typename :: [opt] nested-name-specifier template [opt] template-id
3708 ( expression-list [opt] )
3709 postfix-expression . template [opt] id-expression
3710 postfix-expression -> template [opt] id-expression
3711 postfix-expression . pseudo-destructor-name
3712 postfix-expression -> pseudo-destructor-name
3713 postfix-expression ++
3714 postfix-expression --
3715 dynamic_cast < type-id > ( expression )
3716 static_cast < type-id > ( expression )
3717 reinterpret_cast < type-id > ( expression )
3718 const_cast < type-id > ( expression )
3719 typeid ( expression )
3720 typeid ( type-id )
3721
3722 GNU Extension:
3723
3724 postfix-expression:
3725 ( type-id ) { initializer-list , [opt] }
3726
3727 This extension is a GNU version of the C99 compound-literal
3728 construct. (The C99 grammar uses `type-name' instead of `type-id',
3729 but they are essentially the same concept.)
3730
3731 If ADDRESS_P is true, the postfix expression is the operand of the
3732 `&' operator. CAST_P is true if this expression is the target of a
3733 cast.
3734
3735 Returns a representation of the expression. */
3736
3737 static tree
3738 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3739 {
3740 cp_token *token;
3741 enum rid keyword;
3742 cp_id_kind idk = CP_ID_KIND_NONE;
3743 tree postfix_expression = NULL_TREE;
3744 /* Non-NULL only if the current postfix-expression can be used to
3745 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3746 class used to qualify the member. */
3747 tree qualifying_class = NULL_TREE;
3748
3749 /* Peek at the next token. */
3750 token = cp_lexer_peek_token (parser->lexer);
3751 /* Some of the productions are determined by keywords. */
3752 keyword = token->keyword;
3753 switch (keyword)
3754 {
3755 case RID_DYNCAST:
3756 case RID_STATCAST:
3757 case RID_REINTCAST:
3758 case RID_CONSTCAST:
3759 {
3760 tree type;
3761 tree expression;
3762 const char *saved_message;
3763
3764 /* All of these can be handled in the same way from the point
3765 of view of parsing. Begin by consuming the token
3766 identifying the cast. */
3767 cp_lexer_consume_token (parser->lexer);
3768
3769 /* New types cannot be defined in the cast. */
3770 saved_message = parser->type_definition_forbidden_message;
3771 parser->type_definition_forbidden_message
3772 = "types may not be defined in casts";
3773
3774 /* Look for the opening `<'. */
3775 cp_parser_require (parser, CPP_LESS, "`<'");
3776 /* Parse the type to which we are casting. */
3777 type = cp_parser_type_id (parser);
3778 /* Look for the closing `>'. */
3779 cp_parser_require (parser, CPP_GREATER, "`>'");
3780 /* Restore the old message. */
3781 parser->type_definition_forbidden_message = saved_message;
3782
3783 /* And the expression which is being cast. */
3784 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3785 expression = cp_parser_expression (parser, /*cast_p=*/true);
3786 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3787
3788 /* Only type conversions to integral or enumeration types
3789 can be used in constant-expressions. */
3790 if (parser->integral_constant_expression_p
3791 && !dependent_type_p (type)
3792 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3793 && (cp_parser_non_integral_constant_expression
3794 (parser,
3795 "a cast to a type other than an integral or "
3796 "enumeration type")))
3797 return error_mark_node;
3798
3799 switch (keyword)
3800 {
3801 case RID_DYNCAST:
3802 postfix_expression
3803 = build_dynamic_cast (type, expression);
3804 break;
3805 case RID_STATCAST:
3806 postfix_expression
3807 = build_static_cast (type, expression);
3808 break;
3809 case RID_REINTCAST:
3810 postfix_expression
3811 = build_reinterpret_cast (type, expression);
3812 break;
3813 case RID_CONSTCAST:
3814 postfix_expression
3815 = build_const_cast (type, expression);
3816 break;
3817 default:
3818 gcc_unreachable ();
3819 }
3820 }
3821 break;
3822
3823 case RID_TYPEID:
3824 {
3825 tree type;
3826 const char *saved_message;
3827 bool saved_in_type_id_in_expr_p;
3828
3829 /* Consume the `typeid' token. */
3830 cp_lexer_consume_token (parser->lexer);
3831 /* Look for the `(' token. */
3832 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3833 /* Types cannot be defined in a `typeid' expression. */
3834 saved_message = parser->type_definition_forbidden_message;
3835 parser->type_definition_forbidden_message
3836 = "types may not be defined in a `typeid\' expression";
3837 /* We can't be sure yet whether we're looking at a type-id or an
3838 expression. */
3839 cp_parser_parse_tentatively (parser);
3840 /* Try a type-id first. */
3841 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3842 parser->in_type_id_in_expr_p = true;
3843 type = cp_parser_type_id (parser);
3844 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3845 /* Look for the `)' token. Otherwise, we can't be sure that
3846 we're not looking at an expression: consider `typeid (int
3847 (3))', for example. */
3848 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3849 /* If all went well, simply lookup the type-id. */
3850 if (cp_parser_parse_definitely (parser))
3851 postfix_expression = get_typeid (type);
3852 /* Otherwise, fall back to the expression variant. */
3853 else
3854 {
3855 tree expression;
3856
3857 /* Look for an expression. */
3858 expression = cp_parser_expression (parser, /*cast_p=*/false);
3859 /* Compute its typeid. */
3860 postfix_expression = build_typeid (expression);
3861 /* Look for the `)' token. */
3862 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3863 }
3864 /* `typeid' may not appear in an integral constant expression. */
3865 if (cp_parser_non_integral_constant_expression(parser,
3866 "`typeid' operator"))
3867 return error_mark_node;
3868 /* Restore the saved message. */
3869 parser->type_definition_forbidden_message = saved_message;
3870 }
3871 break;
3872
3873 case RID_TYPENAME:
3874 {
3875 bool template_p = false;
3876 tree id;
3877 tree type;
3878 tree scope;
3879
3880 /* Consume the `typename' token. */
3881 cp_lexer_consume_token (parser->lexer);
3882 /* Look for the optional `::' operator. */
3883 cp_parser_global_scope_opt (parser,
3884 /*current_scope_valid_p=*/false);
3885 /* Look for the nested-name-specifier. In case of error here,
3886 consume the trailing id to avoid subsequent error messages
3887 for usual cases. */
3888 scope = cp_parser_nested_name_specifier (parser,
3889 /*typename_keyword_p=*/true,
3890 /*check_dependency_p=*/true,
3891 /*type_p=*/true,
3892 /*is_declaration=*/true);
3893
3894 /* Look for the optional `template' keyword. */
3895 template_p = cp_parser_optional_template_keyword (parser);
3896 /* We don't know whether we're looking at a template-id or an
3897 identifier. */
3898 cp_parser_parse_tentatively (parser);
3899 /* Try a template-id. */
3900 id = cp_parser_template_id (parser, template_p,
3901 /*check_dependency_p=*/true,
3902 /*is_declaration=*/true);
3903 /* If that didn't work, try an identifier. */
3904 if (!cp_parser_parse_definitely (parser))
3905 id = cp_parser_identifier (parser);
3906
3907 /* Don't process id if nested name specifier is invalid. */
3908 if (scope == error_mark_node)
3909 return error_mark_node;
3910 /* If we look up a template-id in a non-dependent qualifying
3911 scope, there's no need to create a dependent type. */
3912 else if (TREE_CODE (id) == TYPE_DECL
3913 && !dependent_type_p (parser->scope))
3914 type = TREE_TYPE (id);
3915 /* Create a TYPENAME_TYPE to represent the type to which the
3916 functional cast is being performed. */
3917 else
3918 type = make_typename_type (parser->scope, id,
3919 typename_type,
3920 /*complain=*/1);
3921
3922 postfix_expression = cp_parser_functional_cast (parser, type);
3923 }
3924 break;
3925
3926 default:
3927 {
3928 tree type;
3929
3930 /* If the next thing is a simple-type-specifier, we may be
3931 looking at a functional cast. We could also be looking at
3932 an id-expression. So, we try the functional cast, and if
3933 that doesn't work we fall back to the primary-expression. */
3934 cp_parser_parse_tentatively (parser);
3935 /* Look for the simple-type-specifier. */
3936 type = cp_parser_simple_type_specifier (parser,
3937 /*decl_specs=*/NULL,
3938 CP_PARSER_FLAGS_NONE);
3939 /* Parse the cast itself. */
3940 if (!cp_parser_error_occurred (parser))
3941 postfix_expression
3942 = cp_parser_functional_cast (parser, type);
3943 /* If that worked, we're done. */
3944 if (cp_parser_parse_definitely (parser))
3945 break;
3946
3947 /* If the functional-cast didn't work out, try a
3948 compound-literal. */
3949 if (cp_parser_allow_gnu_extensions_p (parser)
3950 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3951 {
3952 tree initializer_list = NULL_TREE;
3953 bool saved_in_type_id_in_expr_p;
3954
3955 cp_parser_parse_tentatively (parser);
3956 /* Consume the `('. */
3957 cp_lexer_consume_token (parser->lexer);
3958 /* Parse the type. */
3959 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3960 parser->in_type_id_in_expr_p = true;
3961 type = cp_parser_type_id (parser);
3962 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3963 /* Look for the `)'. */
3964 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3965 /* Look for the `{'. */
3966 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3967 /* If things aren't going well, there's no need to
3968 keep going. */
3969 if (!cp_parser_error_occurred (parser))
3970 {
3971 bool non_constant_p;
3972 /* Parse the initializer-list. */
3973 initializer_list
3974 = cp_parser_initializer_list (parser, &non_constant_p);
3975 /* Allow a trailing `,'. */
3976 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3977 cp_lexer_consume_token (parser->lexer);
3978 /* Look for the final `}'. */
3979 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3980 }
3981 /* If that worked, we're definitely looking at a
3982 compound-literal expression. */
3983 if (cp_parser_parse_definitely (parser))
3984 {
3985 /* Warn the user that a compound literal is not
3986 allowed in standard C++. */
3987 if (pedantic)
3988 pedwarn ("ISO C++ forbids compound-literals");
3989 /* Form the representation of the compound-literal. */
3990 postfix_expression
3991 = finish_compound_literal (type, initializer_list);
3992 break;
3993 }
3994 }
3995
3996 /* It must be a primary-expression. */
3997 postfix_expression = cp_parser_primary_expression (parser,
3998 cast_p,
3999 &idk,
4000 &qualifying_class);
4001 }
4002 break;
4003 }
4004
4005 /* If we were avoiding committing to the processing of a
4006 qualified-id until we knew whether or not we had a
4007 pointer-to-member, we now know. */
4008 if (qualifying_class)
4009 {
4010 bool done;
4011
4012 /* Peek at the next token. */
4013 token = cp_lexer_peek_token (parser->lexer);
4014 done = (token->type != CPP_OPEN_SQUARE
4015 && token->type != CPP_OPEN_PAREN
4016 && token->type != CPP_DOT
4017 && token->type != CPP_DEREF
4018 && token->type != CPP_PLUS_PLUS
4019 && token->type != CPP_MINUS_MINUS);
4020
4021 postfix_expression = finish_qualified_id_expr (qualifying_class,
4022 postfix_expression,
4023 done,
4024 address_p);
4025 if (done)
4026 return postfix_expression;
4027 }
4028
4029 /* Keep looping until the postfix-expression is complete. */
4030 while (true)
4031 {
4032 if (idk == CP_ID_KIND_UNQUALIFIED
4033 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4034 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4035 /* It is not a Koenig lookup function call. */
4036 postfix_expression
4037 = unqualified_name_lookup_error (postfix_expression);
4038
4039 /* Peek at the next token. */
4040 token = cp_lexer_peek_token (parser->lexer);
4041
4042 switch (token->type)
4043 {
4044 case CPP_OPEN_SQUARE:
4045 postfix_expression
4046 = cp_parser_postfix_open_square_expression (parser,
4047 postfix_expression,
4048 false);
4049 idk = CP_ID_KIND_NONE;
4050 break;
4051
4052 case CPP_OPEN_PAREN:
4053 /* postfix-expression ( expression-list [opt] ) */
4054 {
4055 bool koenig_p;
4056 tree args = (cp_parser_parenthesized_expression_list
4057 (parser, false,
4058 /*cast_p=*/false,
4059 /*non_constant_p=*/NULL));
4060
4061 if (args == error_mark_node)
4062 {
4063 postfix_expression = error_mark_node;
4064 break;
4065 }
4066
4067 /* Function calls are not permitted in
4068 constant-expressions. */
4069 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4070 && cp_parser_non_integral_constant_expression (parser,
4071 "a function call"))
4072 {
4073 postfix_expression = error_mark_node;
4074 break;
4075 }
4076
4077 koenig_p = false;
4078 if (idk == CP_ID_KIND_UNQUALIFIED)
4079 {
4080 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4081 {
4082 if (args)
4083 {
4084 koenig_p = true;
4085 postfix_expression
4086 = perform_koenig_lookup (postfix_expression, args);
4087 }
4088 else
4089 postfix_expression
4090 = unqualified_fn_lookup_error (postfix_expression);
4091 }
4092 /* We do not perform argument-dependent lookup if
4093 normal lookup finds a non-function, in accordance
4094 with the expected resolution of DR 218. */
4095 else if (args && is_overloaded_fn (postfix_expression))
4096 {
4097 tree fn = get_first_fn (postfix_expression);
4098
4099 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4100 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4101
4102 /* Only do argument dependent lookup if regular
4103 lookup does not find a set of member functions.
4104 [basic.lookup.koenig]/2a */
4105 if (!DECL_FUNCTION_MEMBER_P (fn))
4106 {
4107 koenig_p = true;
4108 postfix_expression
4109 = perform_koenig_lookup (postfix_expression, args);
4110 }
4111 }
4112 }
4113
4114 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4115 {
4116 tree instance = TREE_OPERAND (postfix_expression, 0);
4117 tree fn = TREE_OPERAND (postfix_expression, 1);
4118
4119 if (processing_template_decl
4120 && (type_dependent_expression_p (instance)
4121 || (!BASELINK_P (fn)
4122 && TREE_CODE (fn) != FIELD_DECL)
4123 || type_dependent_expression_p (fn)
4124 || any_type_dependent_arguments_p (args)))
4125 {
4126 postfix_expression
4127 = build_min_nt (CALL_EXPR, postfix_expression,
4128 args, NULL_TREE);
4129 break;
4130 }
4131
4132 if (BASELINK_P (fn))
4133 postfix_expression
4134 = (build_new_method_call
4135 (instance, fn, args, NULL_TREE,
4136 (idk == CP_ID_KIND_QUALIFIED
4137 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4138 else
4139 postfix_expression
4140 = finish_call_expr (postfix_expression, args,
4141 /*disallow_virtual=*/false,
4142 /*koenig_p=*/false);
4143 }
4144 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4145 || TREE_CODE (postfix_expression) == MEMBER_REF
4146 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4147 postfix_expression = (build_offset_ref_call_from_tree
4148 (postfix_expression, args));
4149 else if (idk == CP_ID_KIND_QUALIFIED)
4150 /* A call to a static class member, or a namespace-scope
4151 function. */
4152 postfix_expression
4153 = finish_call_expr (postfix_expression, args,
4154 /*disallow_virtual=*/true,
4155 koenig_p);
4156 else
4157 /* All other function calls. */
4158 postfix_expression
4159 = finish_call_expr (postfix_expression, args,
4160 /*disallow_virtual=*/false,
4161 koenig_p);
4162
4163 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4164 idk = CP_ID_KIND_NONE;
4165 }
4166 break;
4167
4168 case CPP_DOT:
4169 case CPP_DEREF:
4170 /* postfix-expression . template [opt] id-expression
4171 postfix-expression . pseudo-destructor-name
4172 postfix-expression -> template [opt] id-expression
4173 postfix-expression -> pseudo-destructor-name */
4174
4175 /* Consume the `.' or `->' operator. */
4176 cp_lexer_consume_token (parser->lexer);
4177
4178 postfix_expression
4179 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4180 postfix_expression,
4181 false, &idk);
4182 break;
4183
4184 case CPP_PLUS_PLUS:
4185 /* postfix-expression ++ */
4186 /* Consume the `++' token. */
4187 cp_lexer_consume_token (parser->lexer);
4188 /* Generate a representation for the complete expression. */
4189 postfix_expression
4190 = finish_increment_expr (postfix_expression,
4191 POSTINCREMENT_EXPR);
4192 /* Increments may not appear in constant-expressions. */
4193 if (cp_parser_non_integral_constant_expression (parser,
4194 "an increment"))
4195 postfix_expression = error_mark_node;
4196 idk = CP_ID_KIND_NONE;
4197 break;
4198
4199 case CPP_MINUS_MINUS:
4200 /* postfix-expression -- */
4201 /* Consume the `--' token. */
4202 cp_lexer_consume_token (parser->lexer);
4203 /* Generate a representation for the complete expression. */
4204 postfix_expression
4205 = finish_increment_expr (postfix_expression,
4206 POSTDECREMENT_EXPR);
4207 /* Decrements may not appear in constant-expressions. */
4208 if (cp_parser_non_integral_constant_expression (parser,
4209 "a decrement"))
4210 postfix_expression = error_mark_node;
4211 idk = CP_ID_KIND_NONE;
4212 break;
4213
4214 default:
4215 return postfix_expression;
4216 }
4217 }
4218
4219 /* We should never get here. */
4220 gcc_unreachable ();
4221 return error_mark_node;
4222 }
4223
4224 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4225 by cp_parser_builtin_offsetof. We're looking for
4226
4227 postfix-expression [ expression ]
4228
4229 FOR_OFFSETOF is set if we're being called in that context, which
4230 changes how we deal with integer constant expressions. */
4231
4232 static tree
4233 cp_parser_postfix_open_square_expression (cp_parser *parser,
4234 tree postfix_expression,
4235 bool for_offsetof)
4236 {
4237 tree index;
4238
4239 /* Consume the `[' token. */
4240 cp_lexer_consume_token (parser->lexer);
4241
4242 /* Parse the index expression. */
4243 /* ??? For offsetof, there is a question of what to allow here. If
4244 offsetof is not being used in an integral constant expression context,
4245 then we *could* get the right answer by computing the value at runtime.
4246 If we are in an integral constant expression context, then we might
4247 could accept any constant expression; hard to say without analysis.
4248 Rather than open the barn door too wide right away, allow only integer
4249 constant expressions here. */
4250 if (for_offsetof)
4251 index = cp_parser_constant_expression (parser, false, NULL);
4252 else
4253 index = cp_parser_expression (parser, /*cast_p=*/false);
4254
4255 /* Look for the closing `]'. */
4256 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4257
4258 /* Build the ARRAY_REF. */
4259 postfix_expression = grok_array_decl (postfix_expression, index);
4260
4261 /* When not doing offsetof, array references are not permitted in
4262 constant-expressions. */
4263 if (!for_offsetof
4264 && (cp_parser_non_integral_constant_expression
4265 (parser, "an array reference")))
4266 postfix_expression = error_mark_node;
4267
4268 return postfix_expression;
4269 }
4270
4271 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4272 by cp_parser_builtin_offsetof. We're looking for
4273
4274 postfix-expression . template [opt] id-expression
4275 postfix-expression . pseudo-destructor-name
4276 postfix-expression -> template [opt] id-expression
4277 postfix-expression -> pseudo-destructor-name
4278
4279 FOR_OFFSETOF is set if we're being called in that context. That sorta
4280 limits what of the above we'll actually accept, but nevermind.
4281 TOKEN_TYPE is the "." or "->" token, which will already have been
4282 removed from the stream. */
4283
4284 static tree
4285 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4286 enum cpp_ttype token_type,
4287 tree postfix_expression,
4288 bool for_offsetof, cp_id_kind *idk)
4289 {
4290 tree name;
4291 bool dependent_p;
4292 bool template_p;
4293 bool pseudo_destructor_p;
4294 tree scope = NULL_TREE;
4295
4296 /* If this is a `->' operator, dereference the pointer. */
4297 if (token_type == CPP_DEREF)
4298 postfix_expression = build_x_arrow (postfix_expression);
4299 /* Check to see whether or not the expression is type-dependent. */
4300 dependent_p = type_dependent_expression_p (postfix_expression);
4301 /* The identifier following the `->' or `.' is not qualified. */
4302 parser->scope = NULL_TREE;
4303 parser->qualifying_scope = NULL_TREE;
4304 parser->object_scope = NULL_TREE;
4305 *idk = CP_ID_KIND_NONE;
4306 /* Enter the scope corresponding to the type of the object
4307 given by the POSTFIX_EXPRESSION. */
4308 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4309 {
4310 scope = TREE_TYPE (postfix_expression);
4311 /* According to the standard, no expression should ever have
4312 reference type. Unfortunately, we do not currently match
4313 the standard in this respect in that our internal representation
4314 of an expression may have reference type even when the standard
4315 says it does not. Therefore, we have to manually obtain the
4316 underlying type here. */
4317 scope = non_reference (scope);
4318 /* The type of the POSTFIX_EXPRESSION must be complete. */
4319 scope = complete_type_or_else (scope, NULL_TREE);
4320 /* Let the name lookup machinery know that we are processing a
4321 class member access expression. */
4322 parser->context->object_type = scope;
4323 /* If something went wrong, we want to be able to discern that case,
4324 as opposed to the case where there was no SCOPE due to the type
4325 of expression being dependent. */
4326 if (!scope)
4327 scope = error_mark_node;
4328 /* If the SCOPE was erroneous, make the various semantic analysis
4329 functions exit quickly -- and without issuing additional error
4330 messages. */
4331 if (scope == error_mark_node)
4332 postfix_expression = error_mark_node;
4333 }
4334
4335 /* Assume this expression is not a pseudo-destructor access. */
4336 pseudo_destructor_p = false;
4337
4338 /* If the SCOPE is a scalar type, then, if this is a valid program,
4339 we must be looking at a pseudo-destructor-name. */
4340 if (scope && SCALAR_TYPE_P (scope))
4341 {
4342 tree s;
4343 tree type;
4344
4345 cp_parser_parse_tentatively (parser);
4346 /* Parse the pseudo-destructor-name. */
4347 s = NULL_TREE;
4348 cp_parser_pseudo_destructor_name (parser, &s, &type);
4349 if (cp_parser_parse_definitely (parser))
4350 {
4351 pseudo_destructor_p = true;
4352 postfix_expression
4353 = finish_pseudo_destructor_expr (postfix_expression,
4354 s, TREE_TYPE (type));
4355 }
4356 }
4357
4358 if (!pseudo_destructor_p)
4359 {
4360 /* If the SCOPE is not a scalar type, we are looking at an
4361 ordinary class member access expression, rather than a
4362 pseudo-destructor-name. */
4363 template_p = cp_parser_optional_template_keyword (parser);
4364 /* Parse the id-expression. */
4365 name = cp_parser_id_expression (parser, template_p,
4366 /*check_dependency_p=*/true,
4367 /*template_p=*/NULL,
4368 /*declarator_p=*/false);
4369 /* In general, build a SCOPE_REF if the member name is qualified.
4370 However, if the name was not dependent and has already been
4371 resolved; there is no need to build the SCOPE_REF. For example;
4372
4373 struct X { void f(); };
4374 template <typename T> void f(T* t) { t->X::f(); }
4375
4376 Even though "t" is dependent, "X::f" is not and has been resolved
4377 to a BASELINK; there is no need to include scope information. */
4378
4379 /* But we do need to remember that there was an explicit scope for
4380 virtual function calls. */
4381 if (parser->scope)
4382 *idk = CP_ID_KIND_QUALIFIED;
4383
4384 /* If the name is a template-id that names a type, we will get a
4385 TYPE_DECL here. That is invalid code. */
4386 if (TREE_CODE (name) == TYPE_DECL)
4387 {
4388 error ("invalid use of %qD", name);
4389 postfix_expression = error_mark_node;
4390 }
4391 else
4392 {
4393 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4394 {
4395 name = build_nt (SCOPE_REF, parser->scope, name);
4396 parser->scope = NULL_TREE;
4397 parser->qualifying_scope = NULL_TREE;
4398 parser->object_scope = NULL_TREE;
4399 }
4400 if (scope && name && BASELINK_P (name))
4401 adjust_result_of_qualified_name_lookup
4402 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4403 postfix_expression
4404 = finish_class_member_access_expr (postfix_expression, name);
4405 }
4406 }
4407
4408 /* We no longer need to look up names in the scope of the object on
4409 the left-hand side of the `.' or `->' operator. */
4410 parser->context->object_type = NULL_TREE;
4411
4412 /* Outside of offsetof, these operators may not appear in
4413 constant-expressions. */
4414 if (!for_offsetof
4415 && (cp_parser_non_integral_constant_expression
4416 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4417 postfix_expression = error_mark_node;
4418
4419 return postfix_expression;
4420 }
4421
4422 /* Parse a parenthesized expression-list.
4423
4424 expression-list:
4425 assignment-expression
4426 expression-list, assignment-expression
4427
4428 attribute-list:
4429 expression-list
4430 identifier
4431 identifier, expression-list
4432
4433 CAST_P is true if this expression is the target of a cast.
4434
4435 Returns a TREE_LIST. The TREE_VALUE of each node is a
4436 representation of an assignment-expression. Note that a TREE_LIST
4437 is returned even if there is only a single expression in the list.
4438 error_mark_node is returned if the ( and or ) are
4439 missing. NULL_TREE is returned on no expressions. The parentheses
4440 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4441 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4442 indicates whether or not all of the expressions in the list were
4443 constant. */
4444
4445 static tree
4446 cp_parser_parenthesized_expression_list (cp_parser* parser,
4447 bool is_attribute_list,
4448 bool cast_p,
4449 bool *non_constant_p)
4450 {
4451 tree expression_list = NULL_TREE;
4452 bool fold_expr_p = is_attribute_list;
4453 tree identifier = NULL_TREE;
4454
4455 /* Assume all the expressions will be constant. */
4456 if (non_constant_p)
4457 *non_constant_p = false;
4458
4459 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4460 return error_mark_node;
4461
4462 /* Consume expressions until there are no more. */
4463 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4464 while (true)
4465 {
4466 tree expr;
4467
4468 /* At the beginning of attribute lists, check to see if the
4469 next token is an identifier. */
4470 if (is_attribute_list
4471 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4472 {
4473 cp_token *token;
4474
4475 /* Consume the identifier. */
4476 token = cp_lexer_consume_token (parser->lexer);
4477 /* Save the identifier. */
4478 identifier = token->value;
4479 }
4480 else
4481 {
4482 /* Parse the next assignment-expression. */
4483 if (non_constant_p)
4484 {
4485 bool expr_non_constant_p;
4486 expr = (cp_parser_constant_expression
4487 (parser, /*allow_non_constant_p=*/true,
4488 &expr_non_constant_p));
4489 if (expr_non_constant_p)
4490 *non_constant_p = true;
4491 }
4492 else
4493 expr = cp_parser_assignment_expression (parser, cast_p);
4494
4495 if (fold_expr_p)
4496 expr = fold_non_dependent_expr (expr);
4497
4498 /* Add it to the list. We add error_mark_node
4499 expressions to the list, so that we can still tell if
4500 the correct form for a parenthesized expression-list
4501 is found. That gives better errors. */
4502 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4503
4504 if (expr == error_mark_node)
4505 goto skip_comma;
4506 }
4507
4508 /* After the first item, attribute lists look the same as
4509 expression lists. */
4510 is_attribute_list = false;
4511
4512 get_comma:;
4513 /* If the next token isn't a `,', then we are done. */
4514 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4515 break;
4516
4517 /* Otherwise, consume the `,' and keep going. */
4518 cp_lexer_consume_token (parser->lexer);
4519 }
4520
4521 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4522 {
4523 int ending;
4524
4525 skip_comma:;
4526 /* We try and resync to an unnested comma, as that will give the
4527 user better diagnostics. */
4528 ending = cp_parser_skip_to_closing_parenthesis (parser,
4529 /*recovering=*/true,
4530 /*or_comma=*/true,
4531 /*consume_paren=*/true);
4532 if (ending < 0)
4533 goto get_comma;
4534 if (!ending)
4535 return error_mark_node;
4536 }
4537
4538 /* We built up the list in reverse order so we must reverse it now. */
4539 expression_list = nreverse (expression_list);
4540 if (identifier)
4541 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4542
4543 return expression_list;
4544 }
4545
4546 /* Parse a pseudo-destructor-name.
4547
4548 pseudo-destructor-name:
4549 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4550 :: [opt] nested-name-specifier template template-id :: ~ type-name
4551 :: [opt] nested-name-specifier [opt] ~ type-name
4552
4553 If either of the first two productions is used, sets *SCOPE to the
4554 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4555 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4556 or ERROR_MARK_NODE if the parse fails. */
4557
4558 static void
4559 cp_parser_pseudo_destructor_name (cp_parser* parser,
4560 tree* scope,
4561 tree* type)
4562 {
4563 bool nested_name_specifier_p;
4564
4565 /* Assume that things will not work out. */
4566 *type = error_mark_node;
4567
4568 /* Look for the optional `::' operator. */
4569 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4570 /* Look for the optional nested-name-specifier. */
4571 nested_name_specifier_p
4572 = (cp_parser_nested_name_specifier_opt (parser,
4573 /*typename_keyword_p=*/false,
4574 /*check_dependency_p=*/true,
4575 /*type_p=*/false,
4576 /*is_declaration=*/true)
4577 != NULL_TREE);
4578 /* Now, if we saw a nested-name-specifier, we might be doing the
4579 second production. */
4580 if (nested_name_specifier_p
4581 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4582 {
4583 /* Consume the `template' keyword. */
4584 cp_lexer_consume_token (parser->lexer);
4585 /* Parse the template-id. */
4586 cp_parser_template_id (parser,
4587 /*template_keyword_p=*/true,
4588 /*check_dependency_p=*/false,
4589 /*is_declaration=*/true);
4590 /* Look for the `::' token. */
4591 cp_parser_require (parser, CPP_SCOPE, "`::'");
4592 }
4593 /* If the next token is not a `~', then there might be some
4594 additional qualification. */
4595 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4596 {
4597 /* Look for the type-name. */
4598 *scope = TREE_TYPE (cp_parser_type_name (parser));
4599
4600 if (*scope == error_mark_node)
4601 return;
4602
4603 /* If we don't have ::~, then something has gone wrong. Since
4604 the only caller of this function is looking for something
4605 after `.' or `->' after a scalar type, most likely the
4606 program is trying to get a member of a non-aggregate
4607 type. */
4608 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4609 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4610 {
4611 cp_parser_error (parser, "request for member of non-aggregate type");
4612 return;
4613 }
4614
4615 /* Look for the `::' token. */
4616 cp_parser_require (parser, CPP_SCOPE, "`::'");
4617 }
4618 else
4619 *scope = NULL_TREE;
4620
4621 /* Look for the `~'. */
4622 cp_parser_require (parser, CPP_COMPL, "`~'");
4623 /* Look for the type-name again. We are not responsible for
4624 checking that it matches the first type-name. */
4625 *type = cp_parser_type_name (parser);
4626 }
4627
4628 /* Parse a unary-expression.
4629
4630 unary-expression:
4631 postfix-expression
4632 ++ cast-expression
4633 -- cast-expression
4634 unary-operator cast-expression
4635 sizeof unary-expression
4636 sizeof ( type-id )
4637 new-expression
4638 delete-expression
4639
4640 GNU Extensions:
4641
4642 unary-expression:
4643 __extension__ cast-expression
4644 __alignof__ unary-expression
4645 __alignof__ ( type-id )
4646 __real__ cast-expression
4647 __imag__ cast-expression
4648 && identifier
4649
4650 ADDRESS_P is true iff the unary-expression is appearing as the
4651 operand of the `&' operator. CAST_P is true if this expression is
4652 the target of a cast.
4653
4654 Returns a representation of the expression. */
4655
4656 static tree
4657 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4658 {
4659 cp_token *token;
4660 enum tree_code unary_operator;
4661
4662 /* Peek at the next token. */
4663 token = cp_lexer_peek_token (parser->lexer);
4664 /* Some keywords give away the kind of expression. */
4665 if (token->type == CPP_KEYWORD)
4666 {
4667 enum rid keyword = token->keyword;
4668
4669 switch (keyword)
4670 {
4671 case RID_ALIGNOF:
4672 case RID_SIZEOF:
4673 {
4674 tree operand;
4675 enum tree_code op;
4676
4677 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4678 /* Consume the token. */
4679 cp_lexer_consume_token (parser->lexer);
4680 /* Parse the operand. */
4681 operand = cp_parser_sizeof_operand (parser, keyword);
4682
4683 if (TYPE_P (operand))
4684 return cxx_sizeof_or_alignof_type (operand, op, true);
4685 else
4686 return cxx_sizeof_or_alignof_expr (operand, op);
4687 }
4688
4689 case RID_NEW:
4690 return cp_parser_new_expression (parser);
4691
4692 case RID_DELETE:
4693 return cp_parser_delete_expression (parser);
4694
4695 case RID_EXTENSION:
4696 {
4697 /* The saved value of the PEDANTIC flag. */
4698 int saved_pedantic;
4699 tree expr;
4700
4701 /* Save away the PEDANTIC flag. */
4702 cp_parser_extension_opt (parser, &saved_pedantic);
4703 /* Parse the cast-expression. */
4704 expr = cp_parser_simple_cast_expression (parser);
4705 /* Restore the PEDANTIC flag. */
4706 pedantic = saved_pedantic;
4707
4708 return expr;
4709 }
4710
4711 case RID_REALPART:
4712 case RID_IMAGPART:
4713 {
4714 tree expression;
4715
4716 /* Consume the `__real__' or `__imag__' token. */
4717 cp_lexer_consume_token (parser->lexer);
4718 /* Parse the cast-expression. */
4719 expression = cp_parser_simple_cast_expression (parser);
4720 /* Create the complete representation. */
4721 return build_x_unary_op ((keyword == RID_REALPART
4722 ? REALPART_EXPR : IMAGPART_EXPR),
4723 expression);
4724 }
4725 break;
4726
4727 default:
4728 break;
4729 }
4730 }
4731
4732 /* Look for the `:: new' and `:: delete', which also signal the
4733 beginning of a new-expression, or delete-expression,
4734 respectively. If the next token is `::', then it might be one of
4735 these. */
4736 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4737 {
4738 enum rid keyword;
4739
4740 /* See if the token after the `::' is one of the keywords in
4741 which we're interested. */
4742 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4743 /* If it's `new', we have a new-expression. */
4744 if (keyword == RID_NEW)
4745 return cp_parser_new_expression (parser);
4746 /* Similarly, for `delete'. */
4747 else if (keyword == RID_DELETE)
4748 return cp_parser_delete_expression (parser);
4749 }
4750
4751 /* Look for a unary operator. */
4752 unary_operator = cp_parser_unary_operator (token);
4753 /* The `++' and `--' operators can be handled similarly, even though
4754 they are not technically unary-operators in the grammar. */
4755 if (unary_operator == ERROR_MARK)
4756 {
4757 if (token->type == CPP_PLUS_PLUS)
4758 unary_operator = PREINCREMENT_EXPR;
4759 else if (token->type == CPP_MINUS_MINUS)
4760 unary_operator = PREDECREMENT_EXPR;
4761 /* Handle the GNU address-of-label extension. */
4762 else if (cp_parser_allow_gnu_extensions_p (parser)
4763 && token->type == CPP_AND_AND)
4764 {
4765 tree identifier;
4766
4767 /* Consume the '&&' token. */
4768 cp_lexer_consume_token (parser->lexer);
4769 /* Look for the identifier. */
4770 identifier = cp_parser_identifier (parser);
4771 /* Create an expression representing the address. */
4772 return finish_label_address_expr (identifier);
4773 }
4774 }
4775 if (unary_operator != ERROR_MARK)
4776 {
4777 tree cast_expression;
4778 tree expression = error_mark_node;
4779 const char *non_constant_p = NULL;
4780
4781 /* Consume the operator token. */
4782 token = cp_lexer_consume_token (parser->lexer);
4783 /* Parse the cast-expression. */
4784 cast_expression
4785 = cp_parser_cast_expression (parser,
4786 unary_operator == ADDR_EXPR,
4787 /*cast_p=*/false);
4788 /* Now, build an appropriate representation. */
4789 switch (unary_operator)
4790 {
4791 case INDIRECT_REF:
4792 non_constant_p = "`*'";
4793 expression = build_x_indirect_ref (cast_expression, "unary *");
4794 break;
4795
4796 case ADDR_EXPR:
4797 non_constant_p = "`&'";
4798 /* Fall through. */
4799 case BIT_NOT_EXPR:
4800 expression = build_x_unary_op (unary_operator, cast_expression);
4801 break;
4802
4803 case PREINCREMENT_EXPR:
4804 case PREDECREMENT_EXPR:
4805 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4806 ? "`++'" : "`--'");
4807 /* Fall through. */
4808 case CONVERT_EXPR:
4809 case NEGATE_EXPR:
4810 case TRUTH_NOT_EXPR:
4811 expression = finish_unary_op_expr (unary_operator, cast_expression);
4812 break;
4813
4814 default:
4815 gcc_unreachable ();
4816 }
4817
4818 if (non_constant_p
4819 && cp_parser_non_integral_constant_expression (parser,
4820 non_constant_p))
4821 expression = error_mark_node;
4822
4823 return expression;
4824 }
4825
4826 return cp_parser_postfix_expression (parser, address_p, cast_p);
4827 }
4828
4829 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4830 unary-operator, the corresponding tree code is returned. */
4831
4832 static enum tree_code
4833 cp_parser_unary_operator (cp_token* token)
4834 {
4835 switch (token->type)
4836 {
4837 case CPP_MULT:
4838 return INDIRECT_REF;
4839
4840 case CPP_AND:
4841 return ADDR_EXPR;
4842
4843 case CPP_PLUS:
4844 return CONVERT_EXPR;
4845
4846 case CPP_MINUS:
4847 return NEGATE_EXPR;
4848
4849 case CPP_NOT:
4850 return TRUTH_NOT_EXPR;
4851
4852 case CPP_COMPL:
4853 return BIT_NOT_EXPR;
4854
4855 default:
4856 return ERROR_MARK;
4857 }
4858 }
4859
4860 /* Parse a new-expression.
4861
4862 new-expression:
4863 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4864 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4865
4866 Returns a representation of the expression. */
4867
4868 static tree
4869 cp_parser_new_expression (cp_parser* parser)
4870 {
4871 bool global_scope_p;
4872 tree placement;
4873 tree type;
4874 tree initializer;
4875 tree nelts;
4876
4877 /* Look for the optional `::' operator. */
4878 global_scope_p
4879 = (cp_parser_global_scope_opt (parser,
4880 /*current_scope_valid_p=*/false)
4881 != NULL_TREE);
4882 /* Look for the `new' operator. */
4883 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4884 /* There's no easy way to tell a new-placement from the
4885 `( type-id )' construct. */
4886 cp_parser_parse_tentatively (parser);
4887 /* Look for a new-placement. */
4888 placement = cp_parser_new_placement (parser);
4889 /* If that didn't work out, there's no new-placement. */
4890 if (!cp_parser_parse_definitely (parser))
4891 placement = NULL_TREE;
4892
4893 /* If the next token is a `(', then we have a parenthesized
4894 type-id. */
4895 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4896 {
4897 /* Consume the `('. */
4898 cp_lexer_consume_token (parser->lexer);
4899 /* Parse the type-id. */
4900 type = cp_parser_type_id (parser);
4901 /* Look for the closing `)'. */
4902 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4903 /* There should not be a direct-new-declarator in this production,
4904 but GCC used to allowed this, so we check and emit a sensible error
4905 message for this case. */
4906 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4907 {
4908 error ("array bound forbidden after parenthesized type-id");
4909 inform ("try removing the parentheses around the type-id");
4910 cp_parser_direct_new_declarator (parser);
4911 }
4912 nelts = NULL_TREE;
4913 }
4914 /* Otherwise, there must be a new-type-id. */
4915 else
4916 type = cp_parser_new_type_id (parser, &nelts);
4917
4918 /* If the next token is a `(', then we have a new-initializer. */
4919 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4920 initializer = cp_parser_new_initializer (parser);
4921 else
4922 initializer = NULL_TREE;
4923
4924 /* A new-expression may not appear in an integral constant
4925 expression. */
4926 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4927 return error_mark_node;
4928
4929 /* Create a representation of the new-expression. */
4930 return build_new (placement, type, nelts, initializer, global_scope_p);
4931 }
4932
4933 /* Parse a new-placement.
4934
4935 new-placement:
4936 ( expression-list )
4937
4938 Returns the same representation as for an expression-list. */
4939
4940 static tree
4941 cp_parser_new_placement (cp_parser* parser)
4942 {
4943 tree expression_list;
4944
4945 /* Parse the expression-list. */
4946 expression_list = (cp_parser_parenthesized_expression_list
4947 (parser, false, /*cast_p=*/false,
4948 /*non_constant_p=*/NULL));
4949
4950 return expression_list;
4951 }
4952
4953 /* Parse a new-type-id.
4954
4955 new-type-id:
4956 type-specifier-seq new-declarator [opt]
4957
4958 Returns the TYPE allocated. If the new-type-id indicates an array
4959 type, *NELTS is set to the number of elements in the last array
4960 bound; the TYPE will not include the last array bound. */
4961
4962 static tree
4963 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4964 {
4965 cp_decl_specifier_seq type_specifier_seq;
4966 cp_declarator *new_declarator;
4967 cp_declarator *declarator;
4968 cp_declarator *outer_declarator;
4969 const char *saved_message;
4970 tree type;
4971
4972 /* The type-specifier sequence must not contain type definitions.
4973 (It cannot contain declarations of new types either, but if they
4974 are not definitions we will catch that because they are not
4975 complete.) */
4976 saved_message = parser->type_definition_forbidden_message;
4977 parser->type_definition_forbidden_message
4978 = "types may not be defined in a new-type-id";
4979 /* Parse the type-specifier-seq. */
4980 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
4981 &type_specifier_seq);
4982 /* Restore the old message. */
4983 parser->type_definition_forbidden_message = saved_message;
4984 /* Parse the new-declarator. */
4985 new_declarator = cp_parser_new_declarator_opt (parser);
4986
4987 /* Determine the number of elements in the last array dimension, if
4988 any. */
4989 *nelts = NULL_TREE;
4990 /* Skip down to the last array dimension. */
4991 declarator = new_declarator;
4992 outer_declarator = NULL;
4993 while (declarator && (declarator->kind == cdk_pointer
4994 || declarator->kind == cdk_ptrmem))
4995 {
4996 outer_declarator = declarator;
4997 declarator = declarator->declarator;
4998 }
4999 while (declarator
5000 && declarator->kind == cdk_array
5001 && declarator->declarator
5002 && declarator->declarator->kind == cdk_array)
5003 {
5004 outer_declarator = declarator;
5005 declarator = declarator->declarator;
5006 }
5007
5008 if (declarator && declarator->kind == cdk_array)
5009 {
5010 *nelts = declarator->u.array.bounds;
5011 if (*nelts == error_mark_node)
5012 *nelts = integer_one_node;
5013
5014 if (outer_declarator)
5015 outer_declarator->declarator = declarator->declarator;
5016 else
5017 new_declarator = NULL;
5018 }
5019
5020 type = groktypename (&type_specifier_seq, new_declarator);
5021 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5022 {
5023 *nelts = array_type_nelts_top (type);
5024 type = TREE_TYPE (type);
5025 }
5026 return type;
5027 }
5028
5029 /* Parse an (optional) new-declarator.
5030
5031 new-declarator:
5032 ptr-operator new-declarator [opt]
5033 direct-new-declarator
5034
5035 Returns the declarator. */
5036
5037 static cp_declarator *
5038 cp_parser_new_declarator_opt (cp_parser* parser)
5039 {
5040 enum tree_code code;
5041 tree type;
5042 cp_cv_quals cv_quals;
5043
5044 /* We don't know if there's a ptr-operator next, or not. */
5045 cp_parser_parse_tentatively (parser);
5046 /* Look for a ptr-operator. */
5047 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5048 /* If that worked, look for more new-declarators. */
5049 if (cp_parser_parse_definitely (parser))
5050 {
5051 cp_declarator *declarator;
5052
5053 /* Parse another optional declarator. */
5054 declarator = cp_parser_new_declarator_opt (parser);
5055
5056 /* Create the representation of the declarator. */
5057 if (type)
5058 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5059 else if (code == INDIRECT_REF)
5060 declarator = make_pointer_declarator (cv_quals, declarator);
5061 else
5062 declarator = make_reference_declarator (cv_quals, declarator);
5063
5064 return declarator;
5065 }
5066
5067 /* If the next token is a `[', there is a direct-new-declarator. */
5068 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5069 return cp_parser_direct_new_declarator (parser);
5070
5071 return NULL;
5072 }
5073
5074 /* Parse a direct-new-declarator.
5075
5076 direct-new-declarator:
5077 [ expression ]
5078 direct-new-declarator [constant-expression]
5079
5080 */
5081
5082 static cp_declarator *
5083 cp_parser_direct_new_declarator (cp_parser* parser)
5084 {
5085 cp_declarator *declarator = NULL;
5086
5087 while (true)
5088 {
5089 tree expression;
5090
5091 /* Look for the opening `['. */
5092 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5093 /* The first expression is not required to be constant. */
5094 if (!declarator)
5095 {
5096 expression = cp_parser_expression (parser, /*cast_p=*/false);
5097 /* The standard requires that the expression have integral
5098 type. DR 74 adds enumeration types. We believe that the
5099 real intent is that these expressions be handled like the
5100 expression in a `switch' condition, which also allows
5101 classes with a single conversion to integral or
5102 enumeration type. */
5103 if (!processing_template_decl)
5104 {
5105 expression
5106 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5107 expression,
5108 /*complain=*/true);
5109 if (!expression)
5110 {
5111 error ("expression in new-declarator must have integral "
5112 "or enumeration type");
5113 expression = error_mark_node;
5114 }
5115 }
5116 }
5117 /* But all the other expressions must be. */
5118 else
5119 expression
5120 = cp_parser_constant_expression (parser,
5121 /*allow_non_constant=*/false,
5122 NULL);
5123 /* Look for the closing `]'. */
5124 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5125
5126 /* Add this bound to the declarator. */
5127 declarator = make_array_declarator (declarator, expression);
5128
5129 /* If the next token is not a `[', then there are no more
5130 bounds. */
5131 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5132 break;
5133 }
5134
5135 return declarator;
5136 }
5137
5138 /* Parse a new-initializer.
5139
5140 new-initializer:
5141 ( expression-list [opt] )
5142
5143 Returns a representation of the expression-list. If there is no
5144 expression-list, VOID_ZERO_NODE is returned. */
5145
5146 static tree
5147 cp_parser_new_initializer (cp_parser* parser)
5148 {
5149 tree expression_list;
5150
5151 expression_list = (cp_parser_parenthesized_expression_list
5152 (parser, false, /*cast_p=*/false,
5153 /*non_constant_p=*/NULL));
5154 if (!expression_list)
5155 expression_list = void_zero_node;
5156
5157 return expression_list;
5158 }
5159
5160 /* Parse a delete-expression.
5161
5162 delete-expression:
5163 :: [opt] delete cast-expression
5164 :: [opt] delete [ ] cast-expression
5165
5166 Returns a representation of the expression. */
5167
5168 static tree
5169 cp_parser_delete_expression (cp_parser* parser)
5170 {
5171 bool global_scope_p;
5172 bool array_p;
5173 tree expression;
5174
5175 /* Look for the optional `::' operator. */
5176 global_scope_p
5177 = (cp_parser_global_scope_opt (parser,
5178 /*current_scope_valid_p=*/false)
5179 != NULL_TREE);
5180 /* Look for the `delete' keyword. */
5181 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5182 /* See if the array syntax is in use. */
5183 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5184 {
5185 /* Consume the `[' token. */
5186 cp_lexer_consume_token (parser->lexer);
5187 /* Look for the `]' token. */
5188 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5189 /* Remember that this is the `[]' construct. */
5190 array_p = true;
5191 }
5192 else
5193 array_p = false;
5194
5195 /* Parse the cast-expression. */
5196 expression = cp_parser_simple_cast_expression (parser);
5197
5198 /* A delete-expression may not appear in an integral constant
5199 expression. */
5200 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5201 return error_mark_node;
5202
5203 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5204 }
5205
5206 /* Parse a cast-expression.
5207
5208 cast-expression:
5209 unary-expression
5210 ( type-id ) cast-expression
5211
5212 ADDRESS_P is true iff the unary-expression is appearing as the
5213 operand of the `&' operator. CAST_P is true if this expression is
5214 the target of a cast.
5215
5216 Returns a representation of the expression. */
5217
5218 static tree
5219 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5220 {
5221 /* If it's a `(', then we might be looking at a cast. */
5222 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5223 {
5224 tree type = NULL_TREE;
5225 tree expr = NULL_TREE;
5226 bool compound_literal_p;
5227 const char *saved_message;
5228
5229 /* There's no way to know yet whether or not this is a cast.
5230 For example, `(int (3))' is a unary-expression, while `(int)
5231 3' is a cast. So, we resort to parsing tentatively. */
5232 cp_parser_parse_tentatively (parser);
5233 /* Types may not be defined in a cast. */
5234 saved_message = parser->type_definition_forbidden_message;
5235 parser->type_definition_forbidden_message
5236 = "types may not be defined in casts";
5237 /* Consume the `('. */
5238 cp_lexer_consume_token (parser->lexer);
5239 /* A very tricky bit is that `(struct S) { 3 }' is a
5240 compound-literal (which we permit in C++ as an extension).
5241 But, that construct is not a cast-expression -- it is a
5242 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5243 is legal; if the compound-literal were a cast-expression,
5244 you'd need an extra set of parentheses.) But, if we parse
5245 the type-id, and it happens to be a class-specifier, then we
5246 will commit to the parse at that point, because we cannot
5247 undo the action that is done when creating a new class. So,
5248 then we cannot back up and do a postfix-expression.
5249
5250 Therefore, we scan ahead to the closing `)', and check to see
5251 if the token after the `)' is a `{'. If so, we are not
5252 looking at a cast-expression.
5253
5254 Save tokens so that we can put them back. */
5255 cp_lexer_save_tokens (parser->lexer);
5256 /* Skip tokens until the next token is a closing parenthesis.
5257 If we find the closing `)', and the next token is a `{', then
5258 we are looking at a compound-literal. */
5259 compound_literal_p
5260 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5261 /*consume_paren=*/true)
5262 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5263 /* Roll back the tokens we skipped. */
5264 cp_lexer_rollback_tokens (parser->lexer);
5265 /* If we were looking at a compound-literal, simulate an error
5266 so that the call to cp_parser_parse_definitely below will
5267 fail. */
5268 if (compound_literal_p)
5269 cp_parser_simulate_error (parser);
5270 else
5271 {
5272 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5273 parser->in_type_id_in_expr_p = true;
5274 /* Look for the type-id. */
5275 type = cp_parser_type_id (parser);
5276 /* Look for the closing `)'. */
5277 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5278 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5279 }
5280
5281 /* Restore the saved message. */
5282 parser->type_definition_forbidden_message = saved_message;
5283
5284 /* If ok so far, parse the dependent expression. We cannot be
5285 sure it is a cast. Consider `(T ())'. It is a parenthesized
5286 ctor of T, but looks like a cast to function returning T
5287 without a dependent expression. */
5288 if (!cp_parser_error_occurred (parser))
5289 expr = cp_parser_cast_expression (parser,
5290 /*address_p=*/false,
5291 /*cast_p=*/true);
5292
5293 if (cp_parser_parse_definitely (parser))
5294 {
5295 /* Warn about old-style casts, if so requested. */
5296 if (warn_old_style_cast
5297 && !in_system_header
5298 && !VOID_TYPE_P (type)
5299 && current_lang_name != lang_name_c)
5300 warning (0, "use of old-style cast");
5301
5302 /* Only type conversions to integral or enumeration types
5303 can be used in constant-expressions. */
5304 if (parser->integral_constant_expression_p
5305 && !dependent_type_p (type)
5306 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5307 && (cp_parser_non_integral_constant_expression
5308 (parser,
5309 "a cast to a type other than an integral or "
5310 "enumeration type")))
5311 return error_mark_node;
5312
5313 /* Perform the cast. */
5314 expr = build_c_cast (type, expr);
5315 return expr;
5316 }
5317 }
5318
5319 /* If we get here, then it's not a cast, so it must be a
5320 unary-expression. */
5321 return cp_parser_unary_expression (parser, address_p, cast_p);
5322 }
5323
5324 /* Parse a binary expression of the general form:
5325
5326 pm-expression:
5327 cast-expression
5328 pm-expression .* cast-expression
5329 pm-expression ->* cast-expression
5330
5331 multiplicative-expression:
5332 pm-expression
5333 multiplicative-expression * pm-expression
5334 multiplicative-expression / pm-expression
5335 multiplicative-expression % pm-expression
5336
5337 additive-expression:
5338 multiplicative-expression
5339 additive-expression + multiplicative-expression
5340 additive-expression - multiplicative-expression
5341
5342 shift-expression:
5343 additive-expression
5344 shift-expression << additive-expression
5345 shift-expression >> additive-expression
5346
5347 relational-expression:
5348 shift-expression
5349 relational-expression < shift-expression
5350 relational-expression > shift-expression
5351 relational-expression <= shift-expression
5352 relational-expression >= shift-expression
5353
5354 GNU Extension:
5355
5356 relational-expression:
5357 relational-expression <? shift-expression
5358 relational-expression >? shift-expression
5359
5360 equality-expression:
5361 relational-expression
5362 equality-expression == relational-expression
5363 equality-expression != relational-expression
5364
5365 and-expression:
5366 equality-expression
5367 and-expression & equality-expression
5368
5369 exclusive-or-expression:
5370 and-expression
5371 exclusive-or-expression ^ and-expression
5372
5373 inclusive-or-expression:
5374 exclusive-or-expression
5375 inclusive-or-expression | exclusive-or-expression
5376
5377 logical-and-expression:
5378 inclusive-or-expression
5379 logical-and-expression && inclusive-or-expression
5380
5381 logical-or-expression:
5382 logical-and-expression
5383 logical-or-expression || logical-and-expression
5384
5385 All these are implemented with a single function like:
5386
5387 binary-expression:
5388 simple-cast-expression
5389 binary-expression <token> binary-expression
5390
5391 CAST_P is true if this expression is the target of a cast.
5392
5393 The binops_by_token map is used to get the tree codes for each <token> type.
5394 binary-expressions are associated according to a precedence table. */
5395
5396 #define TOKEN_PRECEDENCE(token) \
5397 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5398 ? PREC_NOT_OPERATOR \
5399 : binops_by_token[token->type].prec)
5400
5401 static tree
5402 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5403 {
5404 cp_parser_expression_stack stack;
5405 cp_parser_expression_stack_entry *sp = &stack[0];
5406 tree lhs, rhs;
5407 cp_token *token;
5408 enum tree_code tree_type;
5409 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5410 bool overloaded_p;
5411
5412 /* Parse the first expression. */
5413 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5414
5415 for (;;)
5416 {
5417 /* Get an operator token. */
5418 token = cp_lexer_peek_token (parser->lexer);
5419 if (token->type == CPP_MIN || token->type == CPP_MAX)
5420 cp_parser_warn_min_max ();
5421
5422 new_prec = TOKEN_PRECEDENCE (token);
5423
5424 /* Popping an entry off the stack means we completed a subexpression:
5425 - either we found a token which is not an operator (`>' where it is not
5426 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5427 will happen repeatedly;
5428 - or, we found an operator which has lower priority. This is the case
5429 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5430 parsing `3 * 4'. */
5431 if (new_prec <= prec)
5432 {
5433 if (sp == stack)
5434 break;
5435 else
5436 goto pop;
5437 }
5438
5439 get_rhs:
5440 tree_type = binops_by_token[token->type].tree_type;
5441
5442 /* We used the operator token. */
5443 cp_lexer_consume_token (parser->lexer);
5444
5445 /* Extract another operand. It may be the RHS of this expression
5446 or the LHS of a new, higher priority expression. */
5447 rhs = cp_parser_simple_cast_expression (parser);
5448
5449 /* Get another operator token. Look up its precedence to avoid
5450 building a useless (immediately popped) stack entry for common
5451 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5452 token = cp_lexer_peek_token (parser->lexer);
5453 lookahead_prec = TOKEN_PRECEDENCE (token);
5454 if (lookahead_prec > new_prec)
5455 {
5456 /* ... and prepare to parse the RHS of the new, higher priority
5457 expression. Since precedence levels on the stack are
5458 monotonically increasing, we do not have to care about
5459 stack overflows. */
5460 sp->prec = prec;
5461 sp->tree_type = tree_type;
5462 sp->lhs = lhs;
5463 sp++;
5464 lhs = rhs;
5465 prec = new_prec;
5466 new_prec = lookahead_prec;
5467 goto get_rhs;
5468
5469 pop:
5470 /* If the stack is not empty, we have parsed into LHS the right side
5471 (`4' in the example above) of an expression we had suspended.
5472 We can use the information on the stack to recover the LHS (`3')
5473 from the stack together with the tree code (`MULT_EXPR'), and
5474 the precedence of the higher level subexpression
5475 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5476 which will be used to actually build the additive expression. */
5477 --sp;
5478 prec = sp->prec;
5479 tree_type = sp->tree_type;
5480 rhs = lhs;
5481 lhs = sp->lhs;
5482 }
5483
5484 overloaded_p = false;
5485 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5486
5487 /* If the binary operator required the use of an overloaded operator,
5488 then this expression cannot be an integral constant-expression.
5489 An overloaded operator can be used even if both operands are
5490 otherwise permissible in an integral constant-expression if at
5491 least one of the operands is of enumeration type. */
5492
5493 if (overloaded_p
5494 && (cp_parser_non_integral_constant_expression
5495 (parser, "calls to overloaded operators")))
5496 return error_mark_node;
5497 }
5498
5499 return lhs;
5500 }
5501
5502
5503 /* Parse the `? expression : assignment-expression' part of a
5504 conditional-expression. The LOGICAL_OR_EXPR is the
5505 logical-or-expression that started the conditional-expression.
5506 Returns a representation of the entire conditional-expression.
5507
5508 This routine is used by cp_parser_assignment_expression.
5509
5510 ? expression : assignment-expression
5511
5512 GNU Extensions:
5513
5514 ? : assignment-expression */
5515
5516 static tree
5517 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5518 {
5519 tree expr;
5520 tree assignment_expr;
5521
5522 /* Consume the `?' token. */
5523 cp_lexer_consume_token (parser->lexer);
5524 if (cp_parser_allow_gnu_extensions_p (parser)
5525 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5526 /* Implicit true clause. */
5527 expr = NULL_TREE;
5528 else
5529 /* Parse the expression. */
5530 expr = cp_parser_expression (parser, /*cast_p=*/false);
5531
5532 /* The next token should be a `:'. */
5533 cp_parser_require (parser, CPP_COLON, "`:'");
5534 /* Parse the assignment-expression. */
5535 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5536
5537 /* Build the conditional-expression. */
5538 return build_x_conditional_expr (logical_or_expr,
5539 expr,
5540 assignment_expr);
5541 }
5542
5543 /* Parse an assignment-expression.
5544
5545 assignment-expression:
5546 conditional-expression
5547 logical-or-expression assignment-operator assignment_expression
5548 throw-expression
5549
5550 CAST_P is true if this expression is the target of a cast.
5551
5552 Returns a representation for the expression. */
5553
5554 static tree
5555 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5556 {
5557 tree expr;
5558
5559 /* If the next token is the `throw' keyword, then we're looking at
5560 a throw-expression. */
5561 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5562 expr = cp_parser_throw_expression (parser);
5563 /* Otherwise, it must be that we are looking at a
5564 logical-or-expression. */
5565 else
5566 {
5567 /* Parse the binary expressions (logical-or-expression). */
5568 expr = cp_parser_binary_expression (parser, cast_p);
5569 /* If the next token is a `?' then we're actually looking at a
5570 conditional-expression. */
5571 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5572 return cp_parser_question_colon_clause (parser, expr);
5573 else
5574 {
5575 enum tree_code assignment_operator;
5576
5577 /* If it's an assignment-operator, we're using the second
5578 production. */
5579 assignment_operator
5580 = cp_parser_assignment_operator_opt (parser);
5581 if (assignment_operator != ERROR_MARK)
5582 {
5583 tree rhs;
5584
5585 /* Parse the right-hand side of the assignment. */
5586 rhs = cp_parser_assignment_expression (parser, cast_p);
5587 /* An assignment may not appear in a
5588 constant-expression. */
5589 if (cp_parser_non_integral_constant_expression (parser,
5590 "an assignment"))
5591 return error_mark_node;
5592 /* Build the assignment expression. */
5593 expr = build_x_modify_expr (expr,
5594 assignment_operator,
5595 rhs);
5596 }
5597 }
5598 }
5599
5600 return expr;
5601 }
5602
5603 /* Parse an (optional) assignment-operator.
5604
5605 assignment-operator: one of
5606 = *= /= %= += -= >>= <<= &= ^= |=
5607
5608 GNU Extension:
5609
5610 assignment-operator: one of
5611 <?= >?=
5612
5613 If the next token is an assignment operator, the corresponding tree
5614 code is returned, and the token is consumed. For example, for
5615 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5616 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5617 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5618 operator, ERROR_MARK is returned. */
5619
5620 static enum tree_code
5621 cp_parser_assignment_operator_opt (cp_parser* parser)
5622 {
5623 enum tree_code op;
5624 cp_token *token;
5625
5626 /* Peek at the next toen. */
5627 token = cp_lexer_peek_token (parser->lexer);
5628
5629 switch (token->type)
5630 {
5631 case CPP_EQ:
5632 op = NOP_EXPR;
5633 break;
5634
5635 case CPP_MULT_EQ:
5636 op = MULT_EXPR;
5637 break;
5638
5639 case CPP_DIV_EQ:
5640 op = TRUNC_DIV_EXPR;
5641 break;
5642
5643 case CPP_MOD_EQ:
5644 op = TRUNC_MOD_EXPR;
5645 break;
5646
5647 case CPP_PLUS_EQ:
5648 op = PLUS_EXPR;
5649 break;
5650
5651 case CPP_MINUS_EQ:
5652 op = MINUS_EXPR;
5653 break;
5654
5655 case CPP_RSHIFT_EQ:
5656 op = RSHIFT_EXPR;
5657 break;
5658
5659 case CPP_LSHIFT_EQ:
5660 op = LSHIFT_EXPR;
5661 break;
5662
5663 case CPP_AND_EQ:
5664 op = BIT_AND_EXPR;
5665 break;
5666
5667 case CPP_XOR_EQ:
5668 op = BIT_XOR_EXPR;
5669 break;
5670
5671 case CPP_OR_EQ:
5672 op = BIT_IOR_EXPR;
5673 break;
5674
5675 case CPP_MIN_EQ:
5676 op = MIN_EXPR;
5677 cp_parser_warn_min_max ();
5678 break;
5679
5680 case CPP_MAX_EQ:
5681 op = MAX_EXPR;
5682 cp_parser_warn_min_max ();
5683 break;
5684
5685 default:
5686 /* Nothing else is an assignment operator. */
5687 op = ERROR_MARK;
5688 }
5689
5690 /* If it was an assignment operator, consume it. */
5691 if (op != ERROR_MARK)
5692 cp_lexer_consume_token (parser->lexer);
5693
5694 return op;
5695 }
5696
5697 /* Parse an expression.
5698
5699 expression:
5700 assignment-expression
5701 expression , assignment-expression
5702
5703 CAST_P is true if this expression is the target of a cast.
5704
5705 Returns a representation of the expression. */
5706
5707 static tree
5708 cp_parser_expression (cp_parser* parser, bool cast_p)
5709 {
5710 tree expression = NULL_TREE;
5711
5712 while (true)
5713 {
5714 tree assignment_expression;
5715
5716 /* Parse the next assignment-expression. */
5717 assignment_expression
5718 = cp_parser_assignment_expression (parser, cast_p);
5719 /* If this is the first assignment-expression, we can just
5720 save it away. */
5721 if (!expression)
5722 expression = assignment_expression;
5723 else
5724 expression = build_x_compound_expr (expression,
5725 assignment_expression);
5726 /* If the next token is not a comma, then we are done with the
5727 expression. */
5728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5729 break;
5730 /* Consume the `,'. */
5731 cp_lexer_consume_token (parser->lexer);
5732 /* A comma operator cannot appear in a constant-expression. */
5733 if (cp_parser_non_integral_constant_expression (parser,
5734 "a comma operator"))
5735 expression = error_mark_node;
5736 }
5737
5738 return expression;
5739 }
5740
5741 /* Parse a constant-expression.
5742
5743 constant-expression:
5744 conditional-expression
5745
5746 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5747 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5748 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5749 is false, NON_CONSTANT_P should be NULL. */
5750
5751 static tree
5752 cp_parser_constant_expression (cp_parser* parser,
5753 bool allow_non_constant_p,
5754 bool *non_constant_p)
5755 {
5756 bool saved_integral_constant_expression_p;
5757 bool saved_allow_non_integral_constant_expression_p;
5758 bool saved_non_integral_constant_expression_p;
5759 tree expression;
5760
5761 /* It might seem that we could simply parse the
5762 conditional-expression, and then check to see if it were
5763 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5764 one that the compiler can figure out is constant, possibly after
5765 doing some simplifications or optimizations. The standard has a
5766 precise definition of constant-expression, and we must honor
5767 that, even though it is somewhat more restrictive.
5768
5769 For example:
5770
5771 int i[(2, 3)];
5772
5773 is not a legal declaration, because `(2, 3)' is not a
5774 constant-expression. The `,' operator is forbidden in a
5775 constant-expression. However, GCC's constant-folding machinery
5776 will fold this operation to an INTEGER_CST for `3'. */
5777
5778 /* Save the old settings. */
5779 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5780 saved_allow_non_integral_constant_expression_p
5781 = parser->allow_non_integral_constant_expression_p;
5782 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5783 /* We are now parsing a constant-expression. */
5784 parser->integral_constant_expression_p = true;
5785 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5786 parser->non_integral_constant_expression_p = false;
5787 /* Although the grammar says "conditional-expression", we parse an
5788 "assignment-expression", which also permits "throw-expression"
5789 and the use of assignment operators. In the case that
5790 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5791 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5792 actually essential that we look for an assignment-expression.
5793 For example, cp_parser_initializer_clauses uses this function to
5794 determine whether a particular assignment-expression is in fact
5795 constant. */
5796 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5797 /* Restore the old settings. */
5798 parser->integral_constant_expression_p
5799 = saved_integral_constant_expression_p;
5800 parser->allow_non_integral_constant_expression_p
5801 = saved_allow_non_integral_constant_expression_p;
5802 if (allow_non_constant_p)
5803 *non_constant_p = parser->non_integral_constant_expression_p;
5804 else if (parser->non_integral_constant_expression_p)
5805 expression = error_mark_node;
5806 parser->non_integral_constant_expression_p
5807 = saved_non_integral_constant_expression_p;
5808
5809 return expression;
5810 }
5811
5812 /* Parse __builtin_offsetof.
5813
5814 offsetof-expression:
5815 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5816
5817 offsetof-member-designator:
5818 id-expression
5819 | offsetof-member-designator "." id-expression
5820 | offsetof-member-designator "[" expression "]"
5821 */
5822
5823 static tree
5824 cp_parser_builtin_offsetof (cp_parser *parser)
5825 {
5826 int save_ice_p, save_non_ice_p;
5827 tree type, expr;
5828 cp_id_kind dummy;
5829
5830 /* We're about to accept non-integral-constant things, but will
5831 definitely yield an integral constant expression. Save and
5832 restore these values around our local parsing. */
5833 save_ice_p = parser->integral_constant_expression_p;
5834 save_non_ice_p = parser->non_integral_constant_expression_p;
5835
5836 /* Consume the "__builtin_offsetof" token. */
5837 cp_lexer_consume_token (parser->lexer);
5838 /* Consume the opening `('. */
5839 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5840 /* Parse the type-id. */
5841 type = cp_parser_type_id (parser);
5842 /* Look for the `,'. */
5843 cp_parser_require (parser, CPP_COMMA, "`,'");
5844
5845 /* Build the (type *)null that begins the traditional offsetof macro. */
5846 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5847
5848 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5849 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5850 true, &dummy);
5851 while (true)
5852 {
5853 cp_token *token = cp_lexer_peek_token (parser->lexer);
5854 switch (token->type)
5855 {
5856 case CPP_OPEN_SQUARE:
5857 /* offsetof-member-designator "[" expression "]" */
5858 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5859 break;
5860
5861 case CPP_DOT:
5862 /* offsetof-member-designator "." identifier */
5863 cp_lexer_consume_token (parser->lexer);
5864 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5865 true, &dummy);
5866 break;
5867
5868 case CPP_CLOSE_PAREN:
5869 /* Consume the ")" token. */
5870 cp_lexer_consume_token (parser->lexer);
5871 goto success;
5872
5873 default:
5874 /* Error. We know the following require will fail, but
5875 that gives the proper error message. */
5876 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5877 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5878 expr = error_mark_node;
5879 goto failure;
5880 }
5881 }
5882
5883 success:
5884 /* If we're processing a template, we can't finish the semantics yet.
5885 Otherwise we can fold the entire expression now. */
5886 if (processing_template_decl)
5887 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5888 else
5889 expr = fold_offsetof (expr);
5890
5891 failure:
5892 parser->integral_constant_expression_p = save_ice_p;
5893 parser->non_integral_constant_expression_p = save_non_ice_p;
5894
5895 return expr;
5896 }
5897
5898 /* Statements [gram.stmt.stmt] */
5899
5900 /* Parse a statement.
5901
5902 statement:
5903 labeled-statement
5904 expression-statement
5905 compound-statement
5906 selection-statement
5907 iteration-statement
5908 jump-statement
5909 declaration-statement
5910 try-block */
5911
5912 static void
5913 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5914 {
5915 tree statement;
5916 cp_token *token;
5917 location_t statement_location;
5918
5919 /* There is no statement yet. */
5920 statement = NULL_TREE;
5921 /* Peek at the next token. */
5922 token = cp_lexer_peek_token (parser->lexer);
5923 /* Remember the location of the first token in the statement. */
5924 statement_location = token->location;
5925 /* If this is a keyword, then that will often determine what kind of
5926 statement we have. */
5927 if (token->type == CPP_KEYWORD)
5928 {
5929 enum rid keyword = token->keyword;
5930
5931 switch (keyword)
5932 {
5933 case RID_CASE:
5934 case RID_DEFAULT:
5935 statement = cp_parser_labeled_statement (parser,
5936 in_statement_expr);
5937 break;
5938
5939 case RID_IF:
5940 case RID_SWITCH:
5941 statement = cp_parser_selection_statement (parser);
5942 break;
5943
5944 case RID_WHILE:
5945 case RID_DO:
5946 case RID_FOR:
5947 statement = cp_parser_iteration_statement (parser);
5948 break;
5949
5950 case RID_BREAK:
5951 case RID_CONTINUE:
5952 case RID_RETURN:
5953 case RID_GOTO:
5954 statement = cp_parser_jump_statement (parser);
5955 break;
5956
5957 case RID_TRY:
5958 statement = cp_parser_try_block (parser);
5959 break;
5960
5961 default:
5962 /* It might be a keyword like `int' that can start a
5963 declaration-statement. */
5964 break;
5965 }
5966 }
5967 else if (token->type == CPP_NAME)
5968 {
5969 /* If the next token is a `:', then we are looking at a
5970 labeled-statement. */
5971 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5972 if (token->type == CPP_COLON)
5973 statement = cp_parser_labeled_statement (parser, in_statement_expr);
5974 }
5975 /* Anything that starts with a `{' must be a compound-statement. */
5976 else if (token->type == CPP_OPEN_BRACE)
5977 statement = cp_parser_compound_statement (parser, NULL, false);
5978 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5979 a statement all its own. */
5980 else if (token->type == CPP_PRAGMA)
5981 {
5982 cp_lexer_handle_pragma (parser->lexer);
5983 return;
5984 }
5985
5986 /* Everything else must be a declaration-statement or an
5987 expression-statement. Try for the declaration-statement
5988 first, unless we are looking at a `;', in which case we know that
5989 we have an expression-statement. */
5990 if (!statement)
5991 {
5992 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5993 {
5994 cp_parser_parse_tentatively (parser);
5995 /* Try to parse the declaration-statement. */
5996 cp_parser_declaration_statement (parser);
5997 /* If that worked, we're done. */
5998 if (cp_parser_parse_definitely (parser))
5999 return;
6000 }
6001 /* Look for an expression-statement instead. */
6002 statement = cp_parser_expression_statement (parser, in_statement_expr);
6003 }
6004
6005 /* Set the line number for the statement. */
6006 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6007 SET_EXPR_LOCATION (statement, statement_location);
6008 }
6009
6010 /* Parse a labeled-statement.
6011
6012 labeled-statement:
6013 identifier : statement
6014 case constant-expression : statement
6015 default : statement
6016
6017 GNU Extension:
6018
6019 labeled-statement:
6020 case constant-expression ... constant-expression : statement
6021
6022 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6023 For an ordinary label, returns a LABEL_EXPR. */
6024
6025 static tree
6026 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6027 {
6028 cp_token *token;
6029 tree statement = error_mark_node;
6030
6031 /* The next token should be an identifier. */
6032 token = cp_lexer_peek_token (parser->lexer);
6033 if (token->type != CPP_NAME
6034 && token->type != CPP_KEYWORD)
6035 {
6036 cp_parser_error (parser, "expected labeled-statement");
6037 return error_mark_node;
6038 }
6039
6040 switch (token->keyword)
6041 {
6042 case RID_CASE:
6043 {
6044 tree expr, expr_hi;
6045 cp_token *ellipsis;
6046
6047 /* Consume the `case' token. */
6048 cp_lexer_consume_token (parser->lexer);
6049 /* Parse the constant-expression. */
6050 expr = cp_parser_constant_expression (parser,
6051 /*allow_non_constant_p=*/false,
6052 NULL);
6053
6054 ellipsis = cp_lexer_peek_token (parser->lexer);
6055 if (ellipsis->type == CPP_ELLIPSIS)
6056 {
6057 /* Consume the `...' token. */
6058 cp_lexer_consume_token (parser->lexer);
6059 expr_hi =
6060 cp_parser_constant_expression (parser,
6061 /*allow_non_constant_p=*/false,
6062 NULL);
6063 /* We don't need to emit warnings here, as the common code
6064 will do this for us. */
6065 }
6066 else
6067 expr_hi = NULL_TREE;
6068
6069 if (!parser->in_switch_statement_p)
6070 error ("case label %qE not within a switch statement", expr);
6071 else
6072 statement = finish_case_label (expr, expr_hi);
6073 }
6074 break;
6075
6076 case RID_DEFAULT:
6077 /* Consume the `default' token. */
6078 cp_lexer_consume_token (parser->lexer);
6079 if (!parser->in_switch_statement_p)
6080 error ("case label not within a switch statement");
6081 else
6082 statement = finish_case_label (NULL_TREE, NULL_TREE);
6083 break;
6084
6085 default:
6086 /* Anything else must be an ordinary label. */
6087 statement = finish_label_stmt (cp_parser_identifier (parser));
6088 break;
6089 }
6090
6091 /* Require the `:' token. */
6092 cp_parser_require (parser, CPP_COLON, "`:'");
6093 /* Parse the labeled statement. */
6094 cp_parser_statement (parser, in_statement_expr);
6095
6096 /* Return the label, in the case of a `case' or `default' label. */
6097 return statement;
6098 }
6099
6100 /* Parse an expression-statement.
6101
6102 expression-statement:
6103 expression [opt] ;
6104
6105 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6106 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6107 indicates whether this expression-statement is part of an
6108 expression statement. */
6109
6110 static tree
6111 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6112 {
6113 tree statement = NULL_TREE;
6114
6115 /* If the next token is a ';', then there is no expression
6116 statement. */
6117 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6118 statement = cp_parser_expression (parser, /*cast_p=*/false);
6119
6120 /* Consume the final `;'. */
6121 cp_parser_consume_semicolon_at_end_of_statement (parser);
6122
6123 if (in_statement_expr
6124 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6125 /* This is the final expression statement of a statement
6126 expression. */
6127 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6128 else if (statement)
6129 statement = finish_expr_stmt (statement);
6130 else
6131 finish_stmt ();
6132
6133 return statement;
6134 }
6135
6136 /* Parse a compound-statement.
6137
6138 compound-statement:
6139 { statement-seq [opt] }
6140
6141 Returns a tree representing the statement. */
6142
6143 static tree
6144 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6145 bool in_try)
6146 {
6147 tree compound_stmt;
6148
6149 /* Consume the `{'. */
6150 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6151 return error_mark_node;
6152 /* Begin the compound-statement. */
6153 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6154 /* Parse an (optional) statement-seq. */
6155 cp_parser_statement_seq_opt (parser, in_statement_expr);
6156 /* Finish the compound-statement. */
6157 finish_compound_stmt (compound_stmt);
6158 /* Consume the `}'. */
6159 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6160
6161 return compound_stmt;
6162 }
6163
6164 /* Parse an (optional) statement-seq.
6165
6166 statement-seq:
6167 statement
6168 statement-seq [opt] statement */
6169
6170 static void
6171 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6172 {
6173 /* Scan statements until there aren't any more. */
6174 while (true)
6175 {
6176 /* If we're looking at a `}', then we've run out of statements. */
6177 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6178 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6179 break;
6180
6181 /* Parse the statement. */
6182 cp_parser_statement (parser, in_statement_expr);
6183 }
6184 }
6185
6186 /* Parse a selection-statement.
6187
6188 selection-statement:
6189 if ( condition ) statement
6190 if ( condition ) statement else statement
6191 switch ( condition ) statement
6192
6193 Returns the new IF_STMT or SWITCH_STMT. */
6194
6195 static tree
6196 cp_parser_selection_statement (cp_parser* parser)
6197 {
6198 cp_token *token;
6199 enum rid keyword;
6200
6201 /* Peek at the next token. */
6202 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6203
6204 /* See what kind of keyword it is. */
6205 keyword = token->keyword;
6206 switch (keyword)
6207 {
6208 case RID_IF:
6209 case RID_SWITCH:
6210 {
6211 tree statement;
6212 tree condition;
6213
6214 /* Look for the `('. */
6215 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6216 {
6217 cp_parser_skip_to_end_of_statement (parser);
6218 return error_mark_node;
6219 }
6220
6221 /* Begin the selection-statement. */
6222 if (keyword == RID_IF)
6223 statement = begin_if_stmt ();
6224 else
6225 statement = begin_switch_stmt ();
6226
6227 /* Parse the condition. */
6228 condition = cp_parser_condition (parser);
6229 /* Look for the `)'. */
6230 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6231 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6232 /*consume_paren=*/true);
6233
6234 if (keyword == RID_IF)
6235 {
6236 /* Add the condition. */
6237 finish_if_stmt_cond (condition, statement);
6238
6239 /* Parse the then-clause. */
6240 cp_parser_implicitly_scoped_statement (parser);
6241 finish_then_clause (statement);
6242
6243 /* If the next token is `else', parse the else-clause. */
6244 if (cp_lexer_next_token_is_keyword (parser->lexer,
6245 RID_ELSE))
6246 {
6247 /* Consume the `else' keyword. */
6248 cp_lexer_consume_token (parser->lexer);
6249 begin_else_clause (statement);
6250 /* Parse the else-clause. */
6251 cp_parser_implicitly_scoped_statement (parser);
6252 finish_else_clause (statement);
6253 }
6254
6255 /* Now we're all done with the if-statement. */
6256 finish_if_stmt (statement);
6257 }
6258 else
6259 {
6260 bool in_switch_statement_p;
6261
6262 /* Add the condition. */
6263 finish_switch_cond (condition, statement);
6264
6265 /* Parse the body of the switch-statement. */
6266 in_switch_statement_p = parser->in_switch_statement_p;
6267 parser->in_switch_statement_p = true;
6268 cp_parser_implicitly_scoped_statement (parser);
6269 parser->in_switch_statement_p = in_switch_statement_p;
6270
6271 /* Now we're all done with the switch-statement. */
6272 finish_switch_stmt (statement);
6273 }
6274
6275 return statement;
6276 }
6277 break;
6278
6279 default:
6280 cp_parser_error (parser, "expected selection-statement");
6281 return error_mark_node;
6282 }
6283 }
6284
6285 /* Parse a condition.
6286
6287 condition:
6288 expression
6289 type-specifier-seq declarator = assignment-expression
6290
6291 GNU Extension:
6292
6293 condition:
6294 type-specifier-seq declarator asm-specification [opt]
6295 attributes [opt] = assignment-expression
6296
6297 Returns the expression that should be tested. */
6298
6299 static tree
6300 cp_parser_condition (cp_parser* parser)
6301 {
6302 cp_decl_specifier_seq type_specifiers;
6303 const char *saved_message;
6304
6305 /* Try the declaration first. */
6306 cp_parser_parse_tentatively (parser);
6307 /* New types are not allowed in the type-specifier-seq for a
6308 condition. */
6309 saved_message = parser->type_definition_forbidden_message;
6310 parser->type_definition_forbidden_message
6311 = "types may not be defined in conditions";
6312 /* Parse the type-specifier-seq. */
6313 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6314 &type_specifiers);
6315 /* Restore the saved message. */
6316 parser->type_definition_forbidden_message = saved_message;
6317 /* If all is well, we might be looking at a declaration. */
6318 if (!cp_parser_error_occurred (parser))
6319 {
6320 tree decl;
6321 tree asm_specification;
6322 tree attributes;
6323 cp_declarator *declarator;
6324 tree initializer = NULL_TREE;
6325
6326 /* Parse the declarator. */
6327 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6328 /*ctor_dtor_or_conv_p=*/NULL,
6329 /*parenthesized_p=*/NULL,
6330 /*member_p=*/false);
6331 /* Parse the attributes. */
6332 attributes = cp_parser_attributes_opt (parser);
6333 /* Parse the asm-specification. */
6334 asm_specification = cp_parser_asm_specification_opt (parser);
6335 /* If the next token is not an `=', then we might still be
6336 looking at an expression. For example:
6337
6338 if (A(a).x)
6339
6340 looks like a decl-specifier-seq and a declarator -- but then
6341 there is no `=', so this is an expression. */
6342 cp_parser_require (parser, CPP_EQ, "`='");
6343 /* If we did see an `=', then we are looking at a declaration
6344 for sure. */
6345 if (cp_parser_parse_definitely (parser))
6346 {
6347 tree pushed_scope;
6348
6349 /* Create the declaration. */
6350 decl = start_decl (declarator, &type_specifiers,
6351 /*initialized_p=*/true,
6352 attributes, /*prefix_attributes=*/NULL_TREE,
6353 &pushed_scope);
6354 /* Parse the assignment-expression. */
6355 initializer = cp_parser_assignment_expression (parser,
6356 /*cast_p=*/false);
6357
6358 /* Process the initializer. */
6359 cp_finish_decl (decl,
6360 initializer,
6361 asm_specification,
6362 LOOKUP_ONLYCONVERTING);
6363
6364 if (pushed_scope)
6365 pop_scope (pushed_scope);
6366
6367 return convert_from_reference (decl);
6368 }
6369 }
6370 /* If we didn't even get past the declarator successfully, we are
6371 definitely not looking at a declaration. */
6372 else
6373 cp_parser_abort_tentative_parse (parser);
6374
6375 /* Otherwise, we are looking at an expression. */
6376 return cp_parser_expression (parser, /*cast_p=*/false);
6377 }
6378
6379 /* Parse an iteration-statement.
6380
6381 iteration-statement:
6382 while ( condition ) statement
6383 do statement while ( expression ) ;
6384 for ( for-init-statement condition [opt] ; expression [opt] )
6385 statement
6386
6387 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6388
6389 static tree
6390 cp_parser_iteration_statement (cp_parser* parser)
6391 {
6392 cp_token *token;
6393 enum rid keyword;
6394 tree statement;
6395 bool in_iteration_statement_p;
6396
6397
6398 /* Peek at the next token. */
6399 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6400 if (!token)
6401 return error_mark_node;
6402
6403 /* Remember whether or not we are already within an iteration
6404 statement. */
6405 in_iteration_statement_p = parser->in_iteration_statement_p;
6406
6407 /* See what kind of keyword it is. */
6408 keyword = token->keyword;
6409 switch (keyword)
6410 {
6411 case RID_WHILE:
6412 {
6413 tree condition;
6414
6415 /* Begin the while-statement. */
6416 statement = begin_while_stmt ();
6417 /* Look for the `('. */
6418 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6419 /* Parse the condition. */
6420 condition = cp_parser_condition (parser);
6421 finish_while_stmt_cond (condition, statement);
6422 /* Look for the `)'. */
6423 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6424 /* Parse the dependent statement. */
6425 parser->in_iteration_statement_p = true;
6426 cp_parser_already_scoped_statement (parser);
6427 parser->in_iteration_statement_p = in_iteration_statement_p;
6428 /* We're done with the while-statement. */
6429 finish_while_stmt (statement);
6430 }
6431 break;
6432
6433 case RID_DO:
6434 {
6435 tree expression;
6436
6437 /* Begin the do-statement. */
6438 statement = begin_do_stmt ();
6439 /* Parse the body of the do-statement. */
6440 parser->in_iteration_statement_p = true;
6441 cp_parser_implicitly_scoped_statement (parser);
6442 parser->in_iteration_statement_p = in_iteration_statement_p;
6443 finish_do_body (statement);
6444 /* Look for the `while' keyword. */
6445 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6446 /* Look for the `('. */
6447 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6448 /* Parse the expression. */
6449 expression = cp_parser_expression (parser, /*cast_p=*/false);
6450 /* We're done with the do-statement. */
6451 finish_do_stmt (expression, statement);
6452 /* Look for the `)'. */
6453 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6454 /* Look for the `;'. */
6455 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6456 }
6457 break;
6458
6459 case RID_FOR:
6460 {
6461 tree condition = NULL_TREE;
6462 tree expression = NULL_TREE;
6463
6464 /* Begin the for-statement. */
6465 statement = begin_for_stmt ();
6466 /* Look for the `('. */
6467 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6468 /* Parse the initialization. */
6469 cp_parser_for_init_statement (parser);
6470 finish_for_init_stmt (statement);
6471
6472 /* If there's a condition, process it. */
6473 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6474 condition = cp_parser_condition (parser);
6475 finish_for_cond (condition, statement);
6476 /* Look for the `;'. */
6477 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6478
6479 /* If there's an expression, process it. */
6480 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6481 expression = cp_parser_expression (parser, /*cast_p=*/false);
6482 finish_for_expr (expression, statement);
6483 /* Look for the `)'. */
6484 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6485
6486 /* Parse the body of the for-statement. */
6487 parser->in_iteration_statement_p = true;
6488 cp_parser_already_scoped_statement (parser);
6489 parser->in_iteration_statement_p = in_iteration_statement_p;
6490
6491 /* We're done with the for-statement. */
6492 finish_for_stmt (statement);
6493 }
6494 break;
6495
6496 default:
6497 cp_parser_error (parser, "expected iteration-statement");
6498 statement = error_mark_node;
6499 break;
6500 }
6501
6502 return statement;
6503 }
6504
6505 /* Parse a for-init-statement.
6506
6507 for-init-statement:
6508 expression-statement
6509 simple-declaration */
6510
6511 static void
6512 cp_parser_for_init_statement (cp_parser* parser)
6513 {
6514 /* If the next token is a `;', then we have an empty
6515 expression-statement. Grammatically, this is also a
6516 simple-declaration, but an invalid one, because it does not
6517 declare anything. Therefore, if we did not handle this case
6518 specially, we would issue an error message about an invalid
6519 declaration. */
6520 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6521 {
6522 /* We're going to speculatively look for a declaration, falling back
6523 to an expression, if necessary. */
6524 cp_parser_parse_tentatively (parser);
6525 /* Parse the declaration. */
6526 cp_parser_simple_declaration (parser,
6527 /*function_definition_allowed_p=*/false);
6528 /* If the tentative parse failed, then we shall need to look for an
6529 expression-statement. */
6530 if (cp_parser_parse_definitely (parser))
6531 return;
6532 }
6533
6534 cp_parser_expression_statement (parser, false);
6535 }
6536
6537 /* Parse a jump-statement.
6538
6539 jump-statement:
6540 break ;
6541 continue ;
6542 return expression [opt] ;
6543 goto identifier ;
6544
6545 GNU extension:
6546
6547 jump-statement:
6548 goto * expression ;
6549
6550 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6551
6552 static tree
6553 cp_parser_jump_statement (cp_parser* parser)
6554 {
6555 tree statement = error_mark_node;
6556 cp_token *token;
6557 enum rid keyword;
6558
6559 /* Peek at the next token. */
6560 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6561 if (!token)
6562 return error_mark_node;
6563
6564 /* See what kind of keyword it is. */
6565 keyword = token->keyword;
6566 switch (keyword)
6567 {
6568 case RID_BREAK:
6569 if (!parser->in_switch_statement_p
6570 && !parser->in_iteration_statement_p)
6571 {
6572 error ("break statement not within loop or switch");
6573 statement = error_mark_node;
6574 }
6575 else
6576 statement = finish_break_stmt ();
6577 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6578 break;
6579
6580 case RID_CONTINUE:
6581 if (!parser->in_iteration_statement_p)
6582 {
6583 error ("continue statement not within a loop");
6584 statement = error_mark_node;
6585 }
6586 else
6587 statement = finish_continue_stmt ();
6588 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6589 break;
6590
6591 case RID_RETURN:
6592 {
6593 tree expr;
6594
6595 /* If the next token is a `;', then there is no
6596 expression. */
6597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6598 expr = cp_parser_expression (parser, /*cast_p=*/false);
6599 else
6600 expr = NULL_TREE;
6601 /* Build the return-statement. */
6602 statement = finish_return_stmt (expr);
6603 /* Look for the final `;'. */
6604 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6605 }
6606 break;
6607
6608 case RID_GOTO:
6609 /* Create the goto-statement. */
6610 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6611 {
6612 /* Issue a warning about this use of a GNU extension. */
6613 if (pedantic)
6614 pedwarn ("ISO C++ forbids computed gotos");
6615 /* Consume the '*' token. */
6616 cp_lexer_consume_token (parser->lexer);
6617 /* Parse the dependent expression. */
6618 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6619 }
6620 else
6621 finish_goto_stmt (cp_parser_identifier (parser));
6622 /* Look for the final `;'. */
6623 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6624 break;
6625
6626 default:
6627 cp_parser_error (parser, "expected jump-statement");
6628 break;
6629 }
6630
6631 return statement;
6632 }
6633
6634 /* Parse a declaration-statement.
6635
6636 declaration-statement:
6637 block-declaration */
6638
6639 static void
6640 cp_parser_declaration_statement (cp_parser* parser)
6641 {
6642 void *p;
6643
6644 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6645 p = obstack_alloc (&declarator_obstack, 0);
6646
6647 /* Parse the block-declaration. */
6648 cp_parser_block_declaration (parser, /*statement_p=*/true);
6649
6650 /* Free any declarators allocated. */
6651 obstack_free (&declarator_obstack, p);
6652
6653 /* Finish off the statement. */
6654 finish_stmt ();
6655 }
6656
6657 /* Some dependent statements (like `if (cond) statement'), are
6658 implicitly in their own scope. In other words, if the statement is
6659 a single statement (as opposed to a compound-statement), it is
6660 none-the-less treated as if it were enclosed in braces. Any
6661 declarations appearing in the dependent statement are out of scope
6662 after control passes that point. This function parses a statement,
6663 but ensures that is in its own scope, even if it is not a
6664 compound-statement.
6665
6666 Returns the new statement. */
6667
6668 static tree
6669 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6670 {
6671 tree statement;
6672
6673 /* If the token is not a `{', then we must take special action. */
6674 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6675 {
6676 /* Create a compound-statement. */
6677 statement = begin_compound_stmt (0);
6678 /* Parse the dependent-statement. */
6679 cp_parser_statement (parser, false);
6680 /* Finish the dummy compound-statement. */
6681 finish_compound_stmt (statement);
6682 }
6683 /* Otherwise, we simply parse the statement directly. */
6684 else
6685 statement = cp_parser_compound_statement (parser, NULL, false);
6686
6687 /* Return the statement. */
6688 return statement;
6689 }
6690
6691 /* For some dependent statements (like `while (cond) statement'), we
6692 have already created a scope. Therefore, even if the dependent
6693 statement is a compound-statement, we do not want to create another
6694 scope. */
6695
6696 static void
6697 cp_parser_already_scoped_statement (cp_parser* parser)
6698 {
6699 /* If the token is a `{', then we must take special action. */
6700 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6701 cp_parser_statement (parser, false);
6702 else
6703 {
6704 /* Avoid calling cp_parser_compound_statement, so that we
6705 don't create a new scope. Do everything else by hand. */
6706 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6707 cp_parser_statement_seq_opt (parser, false);
6708 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6709 }
6710 }
6711
6712 /* Declarations [gram.dcl.dcl] */
6713
6714 /* Parse an optional declaration-sequence.
6715
6716 declaration-seq:
6717 declaration
6718 declaration-seq declaration */
6719
6720 static void
6721 cp_parser_declaration_seq_opt (cp_parser* parser)
6722 {
6723 while (true)
6724 {
6725 cp_token *token;
6726
6727 token = cp_lexer_peek_token (parser->lexer);
6728
6729 if (token->type == CPP_CLOSE_BRACE
6730 || token->type == CPP_EOF)
6731 break;
6732
6733 if (token->type == CPP_SEMICOLON)
6734 {
6735 /* A declaration consisting of a single semicolon is
6736 invalid. Allow it unless we're being pedantic. */
6737 cp_lexer_consume_token (parser->lexer);
6738 if (pedantic && !in_system_header)
6739 pedwarn ("extra %<;%>");
6740 continue;
6741 }
6742
6743 /* If we're entering or exiting a region that's implicitly
6744 extern "C", modify the lang context appropriately. */
6745 if (!parser->implicit_extern_c && token->implicit_extern_c)
6746 {
6747 push_lang_context (lang_name_c);
6748 parser->implicit_extern_c = true;
6749 }
6750 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6751 {
6752 pop_lang_context ();
6753 parser->implicit_extern_c = false;
6754 }
6755
6756 if (token->type == CPP_PRAGMA)
6757 {
6758 /* A top-level declaration can consist solely of a #pragma.
6759 A nested declaration cannot, so this is done here and not
6760 in cp_parser_declaration. (A #pragma at block scope is
6761 handled in cp_parser_statement.) */
6762 cp_lexer_handle_pragma (parser->lexer);
6763 continue;
6764 }
6765
6766 /* Parse the declaration itself. */
6767 cp_parser_declaration (parser);
6768 }
6769 }
6770
6771 /* Parse a declaration.
6772
6773 declaration:
6774 block-declaration
6775 function-definition
6776 template-declaration
6777 explicit-instantiation
6778 explicit-specialization
6779 linkage-specification
6780 namespace-definition
6781
6782 GNU extension:
6783
6784 declaration:
6785 __extension__ declaration */
6786
6787 static void
6788 cp_parser_declaration (cp_parser* parser)
6789 {
6790 cp_token token1;
6791 cp_token token2;
6792 int saved_pedantic;
6793 void *p;
6794
6795 /* Check for the `__extension__' keyword. */
6796 if (cp_parser_extension_opt (parser, &saved_pedantic))
6797 {
6798 /* Parse the qualified declaration. */
6799 cp_parser_declaration (parser);
6800 /* Restore the PEDANTIC flag. */
6801 pedantic = saved_pedantic;
6802
6803 return;
6804 }
6805
6806 /* Try to figure out what kind of declaration is present. */
6807 token1 = *cp_lexer_peek_token (parser->lexer);
6808
6809 if (token1.type != CPP_EOF)
6810 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6811
6812 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6813 p = obstack_alloc (&declarator_obstack, 0);
6814
6815 /* If the next token is `extern' and the following token is a string
6816 literal, then we have a linkage specification. */
6817 if (token1.keyword == RID_EXTERN
6818 && cp_parser_is_string_literal (&token2))
6819 cp_parser_linkage_specification (parser);
6820 /* If the next token is `template', then we have either a template
6821 declaration, an explicit instantiation, or an explicit
6822 specialization. */
6823 else if (token1.keyword == RID_TEMPLATE)
6824 {
6825 /* `template <>' indicates a template specialization. */
6826 if (token2.type == CPP_LESS
6827 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6828 cp_parser_explicit_specialization (parser);
6829 /* `template <' indicates a template declaration. */
6830 else if (token2.type == CPP_LESS)
6831 cp_parser_template_declaration (parser, /*member_p=*/false);
6832 /* Anything else must be an explicit instantiation. */
6833 else
6834 cp_parser_explicit_instantiation (parser);
6835 }
6836 /* If the next token is `export', then we have a template
6837 declaration. */
6838 else if (token1.keyword == RID_EXPORT)
6839 cp_parser_template_declaration (parser, /*member_p=*/false);
6840 /* If the next token is `extern', 'static' or 'inline' and the one
6841 after that is `template', we have a GNU extended explicit
6842 instantiation directive. */
6843 else if (cp_parser_allow_gnu_extensions_p (parser)
6844 && (token1.keyword == RID_EXTERN
6845 || token1.keyword == RID_STATIC
6846 || token1.keyword == RID_INLINE)
6847 && token2.keyword == RID_TEMPLATE)
6848 cp_parser_explicit_instantiation (parser);
6849 /* If the next token is `namespace', check for a named or unnamed
6850 namespace definition. */
6851 else if (token1.keyword == RID_NAMESPACE
6852 && (/* A named namespace definition. */
6853 (token2.type == CPP_NAME
6854 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6855 == CPP_OPEN_BRACE))
6856 /* An unnamed namespace definition. */
6857 || token2.type == CPP_OPEN_BRACE))
6858 cp_parser_namespace_definition (parser);
6859 /* We must have either a block declaration or a function
6860 definition. */
6861 else
6862 /* Try to parse a block-declaration, or a function-definition. */
6863 cp_parser_block_declaration (parser, /*statement_p=*/false);
6864
6865 /* Free any declarators allocated. */
6866 obstack_free (&declarator_obstack, p);
6867 }
6868
6869 /* Parse a block-declaration.
6870
6871 block-declaration:
6872 simple-declaration
6873 asm-definition
6874 namespace-alias-definition
6875 using-declaration
6876 using-directive
6877
6878 GNU Extension:
6879
6880 block-declaration:
6881 __extension__ block-declaration
6882 label-declaration
6883
6884 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6885 part of a declaration-statement. */
6886
6887 static void
6888 cp_parser_block_declaration (cp_parser *parser,
6889 bool statement_p)
6890 {
6891 cp_token *token1;
6892 int saved_pedantic;
6893
6894 /* Check for the `__extension__' keyword. */
6895 if (cp_parser_extension_opt (parser, &saved_pedantic))
6896 {
6897 /* Parse the qualified declaration. */
6898 cp_parser_block_declaration (parser, statement_p);
6899 /* Restore the PEDANTIC flag. */
6900 pedantic = saved_pedantic;
6901
6902 return;
6903 }
6904
6905 /* Peek at the next token to figure out which kind of declaration is
6906 present. */
6907 token1 = cp_lexer_peek_token (parser->lexer);
6908
6909 /* If the next keyword is `asm', we have an asm-definition. */
6910 if (token1->keyword == RID_ASM)
6911 {
6912 if (statement_p)
6913 cp_parser_commit_to_tentative_parse (parser);
6914 cp_parser_asm_definition (parser);
6915 }
6916 /* If the next keyword is `namespace', we have a
6917 namespace-alias-definition. */
6918 else if (token1->keyword == RID_NAMESPACE)
6919 cp_parser_namespace_alias_definition (parser);
6920 /* If the next keyword is `using', we have either a
6921 using-declaration or a using-directive. */
6922 else if (token1->keyword == RID_USING)
6923 {
6924 cp_token *token2;
6925
6926 if (statement_p)
6927 cp_parser_commit_to_tentative_parse (parser);
6928 /* If the token after `using' is `namespace', then we have a
6929 using-directive. */
6930 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6931 if (token2->keyword == RID_NAMESPACE)
6932 cp_parser_using_directive (parser);
6933 /* Otherwise, it's a using-declaration. */
6934 else
6935 cp_parser_using_declaration (parser);
6936 }
6937 /* If the next keyword is `__label__' we have a label declaration. */
6938 else if (token1->keyword == RID_LABEL)
6939 {
6940 if (statement_p)
6941 cp_parser_commit_to_tentative_parse (parser);
6942 cp_parser_label_declaration (parser);
6943 }
6944 /* Anything else must be a simple-declaration. */
6945 else
6946 cp_parser_simple_declaration (parser, !statement_p);
6947 }
6948
6949 /* Parse a simple-declaration.
6950
6951 simple-declaration:
6952 decl-specifier-seq [opt] init-declarator-list [opt] ;
6953
6954 init-declarator-list:
6955 init-declarator
6956 init-declarator-list , init-declarator
6957
6958 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6959 function-definition as a simple-declaration. */
6960
6961 static void
6962 cp_parser_simple_declaration (cp_parser* parser,
6963 bool function_definition_allowed_p)
6964 {
6965 cp_decl_specifier_seq decl_specifiers;
6966 int declares_class_or_enum;
6967 bool saw_declarator;
6968
6969 /* Defer access checks until we know what is being declared; the
6970 checks for names appearing in the decl-specifier-seq should be
6971 done as if we were in the scope of the thing being declared. */
6972 push_deferring_access_checks (dk_deferred);
6973
6974 /* Parse the decl-specifier-seq. We have to keep track of whether
6975 or not the decl-specifier-seq declares a named class or
6976 enumeration type, since that is the only case in which the
6977 init-declarator-list is allowed to be empty.
6978
6979 [dcl.dcl]
6980
6981 In a simple-declaration, the optional init-declarator-list can be
6982 omitted only when declaring a class or enumeration, that is when
6983 the decl-specifier-seq contains either a class-specifier, an
6984 elaborated-type-specifier, or an enum-specifier. */
6985 cp_parser_decl_specifier_seq (parser,
6986 CP_PARSER_FLAGS_OPTIONAL,
6987 &decl_specifiers,
6988 &declares_class_or_enum);
6989 /* We no longer need to defer access checks. */
6990 stop_deferring_access_checks ();
6991
6992 /* In a block scope, a valid declaration must always have a
6993 decl-specifier-seq. By not trying to parse declarators, we can
6994 resolve the declaration/expression ambiguity more quickly. */
6995 if (!function_definition_allowed_p
6996 && !decl_specifiers.any_specifiers_p)
6997 {
6998 cp_parser_error (parser, "expected declaration");
6999 goto done;
7000 }
7001
7002 /* If the next two tokens are both identifiers, the code is
7003 erroneous. The usual cause of this situation is code like:
7004
7005 T t;
7006
7007 where "T" should name a type -- but does not. */
7008 if (!decl_specifiers.type
7009 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7010 {
7011 /* If parsing tentatively, we should commit; we really are
7012 looking at a declaration. */
7013 cp_parser_commit_to_tentative_parse (parser);
7014 /* Give up. */
7015 goto done;
7016 }
7017
7018 /* If we have seen at least one decl-specifier, and the next token
7019 is not a parenthesis, then we must be looking at a declaration.
7020 (After "int (" we might be looking at a functional cast.) */
7021 if (decl_specifiers.any_specifiers_p
7022 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7023 cp_parser_commit_to_tentative_parse (parser);
7024
7025 /* Keep going until we hit the `;' at the end of the simple
7026 declaration. */
7027 saw_declarator = false;
7028 while (cp_lexer_next_token_is_not (parser->lexer,
7029 CPP_SEMICOLON))
7030 {
7031 cp_token *token;
7032 bool function_definition_p;
7033 tree decl;
7034
7035 saw_declarator = true;
7036 /* Parse the init-declarator. */
7037 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7038 function_definition_allowed_p,
7039 /*member_p=*/false,
7040 declares_class_or_enum,
7041 &function_definition_p);
7042 /* If an error occurred while parsing tentatively, exit quickly.
7043 (That usually happens when in the body of a function; each
7044 statement is treated as a declaration-statement until proven
7045 otherwise.) */
7046 if (cp_parser_error_occurred (parser))
7047 goto done;
7048 /* Handle function definitions specially. */
7049 if (function_definition_p)
7050 {
7051 /* If the next token is a `,', then we are probably
7052 processing something like:
7053
7054 void f() {}, *p;
7055
7056 which is erroneous. */
7057 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7058 error ("mixing declarations and function-definitions is forbidden");
7059 /* Otherwise, we're done with the list of declarators. */
7060 else
7061 {
7062 pop_deferring_access_checks ();
7063 return;
7064 }
7065 }
7066 /* The next token should be either a `,' or a `;'. */
7067 token = cp_lexer_peek_token (parser->lexer);
7068 /* If it's a `,', there are more declarators to come. */
7069 if (token->type == CPP_COMMA)
7070 cp_lexer_consume_token (parser->lexer);
7071 /* If it's a `;', we are done. */
7072 else if (token->type == CPP_SEMICOLON)
7073 break;
7074 /* Anything else is an error. */
7075 else
7076 {
7077 /* If we have already issued an error message we don't need
7078 to issue another one. */
7079 if (decl != error_mark_node
7080 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7081 cp_parser_error (parser, "expected %<,%> or %<;%>");
7082 /* Skip tokens until we reach the end of the statement. */
7083 cp_parser_skip_to_end_of_statement (parser);
7084 /* If the next token is now a `;', consume it. */
7085 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7086 cp_lexer_consume_token (parser->lexer);
7087 goto done;
7088 }
7089 /* After the first time around, a function-definition is not
7090 allowed -- even if it was OK at first. For example:
7091
7092 int i, f() {}
7093
7094 is not valid. */
7095 function_definition_allowed_p = false;
7096 }
7097
7098 /* Issue an error message if no declarators are present, and the
7099 decl-specifier-seq does not itself declare a class or
7100 enumeration. */
7101 if (!saw_declarator)
7102 {
7103 if (cp_parser_declares_only_class_p (parser))
7104 shadow_tag (&decl_specifiers);
7105 /* Perform any deferred access checks. */
7106 perform_deferred_access_checks ();
7107 }
7108
7109 /* Consume the `;'. */
7110 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7111
7112 done:
7113 pop_deferring_access_checks ();
7114 }
7115
7116 /* Parse a decl-specifier-seq.
7117
7118 decl-specifier-seq:
7119 decl-specifier-seq [opt] decl-specifier
7120
7121 decl-specifier:
7122 storage-class-specifier
7123 type-specifier
7124 function-specifier
7125 friend
7126 typedef
7127
7128 GNU Extension:
7129
7130 decl-specifier:
7131 attributes
7132
7133 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7134
7135 The parser flags FLAGS is used to control type-specifier parsing.
7136
7137 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7138 flags:
7139
7140 1: one of the decl-specifiers is an elaborated-type-specifier
7141 (i.e., a type declaration)
7142 2: one of the decl-specifiers is an enum-specifier or a
7143 class-specifier (i.e., a type definition)
7144
7145 */
7146
7147 static void
7148 cp_parser_decl_specifier_seq (cp_parser* parser,
7149 cp_parser_flags flags,
7150 cp_decl_specifier_seq *decl_specs,
7151 int* declares_class_or_enum)
7152 {
7153 bool constructor_possible_p = !parser->in_declarator_p;
7154
7155 /* Clear DECL_SPECS. */
7156 clear_decl_specs (decl_specs);
7157
7158 /* Assume no class or enumeration type is declared. */
7159 *declares_class_or_enum = 0;
7160
7161 /* Keep reading specifiers until there are no more to read. */
7162 while (true)
7163 {
7164 bool constructor_p;
7165 bool found_decl_spec;
7166 cp_token *token;
7167
7168 /* Peek at the next token. */
7169 token = cp_lexer_peek_token (parser->lexer);
7170 /* Handle attributes. */
7171 if (token->keyword == RID_ATTRIBUTE)
7172 {
7173 /* Parse the attributes. */
7174 decl_specs->attributes
7175 = chainon (decl_specs->attributes,
7176 cp_parser_attributes_opt (parser));
7177 continue;
7178 }
7179 /* Assume we will find a decl-specifier keyword. */
7180 found_decl_spec = true;
7181 /* If the next token is an appropriate keyword, we can simply
7182 add it to the list. */
7183 switch (token->keyword)
7184 {
7185 /* decl-specifier:
7186 friend */
7187 case RID_FRIEND:
7188 if (decl_specs->specs[(int) ds_friend]++)
7189 error ("duplicate %<friend%>");
7190 /* Consume the token. */
7191 cp_lexer_consume_token (parser->lexer);
7192 break;
7193
7194 /* function-specifier:
7195 inline
7196 virtual
7197 explicit */
7198 case RID_INLINE:
7199 case RID_VIRTUAL:
7200 case RID_EXPLICIT:
7201 cp_parser_function_specifier_opt (parser, decl_specs);
7202 break;
7203
7204 /* decl-specifier:
7205 typedef */
7206 case RID_TYPEDEF:
7207 ++decl_specs->specs[(int) ds_typedef];
7208 /* Consume the token. */
7209 cp_lexer_consume_token (parser->lexer);
7210 /* A constructor declarator cannot appear in a typedef. */
7211 constructor_possible_p = false;
7212 /* The "typedef" keyword can only occur in a declaration; we
7213 may as well commit at this point. */
7214 cp_parser_commit_to_tentative_parse (parser);
7215 break;
7216
7217 /* storage-class-specifier:
7218 auto
7219 register
7220 static
7221 extern
7222 mutable
7223
7224 GNU Extension:
7225 thread */
7226 case RID_AUTO:
7227 /* Consume the token. */
7228 cp_lexer_consume_token (parser->lexer);
7229 cp_parser_set_storage_class (decl_specs, sc_auto);
7230 break;
7231 case RID_REGISTER:
7232 /* Consume the token. */
7233 cp_lexer_consume_token (parser->lexer);
7234 cp_parser_set_storage_class (decl_specs, sc_register);
7235 break;
7236 case RID_STATIC:
7237 /* Consume the token. */
7238 cp_lexer_consume_token (parser->lexer);
7239 if (decl_specs->specs[(int) ds_thread])
7240 {
7241 error ("%<__thread%> before %<static%>");
7242 decl_specs->specs[(int) ds_thread] = 0;
7243 }
7244 cp_parser_set_storage_class (decl_specs, sc_static);
7245 break;
7246 case RID_EXTERN:
7247 /* Consume the token. */
7248 cp_lexer_consume_token (parser->lexer);
7249 if (decl_specs->specs[(int) ds_thread])
7250 {
7251 error ("%<__thread%> before %<extern%>");
7252 decl_specs->specs[(int) ds_thread] = 0;
7253 }
7254 cp_parser_set_storage_class (decl_specs, sc_extern);
7255 break;
7256 case RID_MUTABLE:
7257 /* Consume the token. */
7258 cp_lexer_consume_token (parser->lexer);
7259 cp_parser_set_storage_class (decl_specs, sc_mutable);
7260 break;
7261 case RID_THREAD:
7262 /* Consume the token. */
7263 cp_lexer_consume_token (parser->lexer);
7264 ++decl_specs->specs[(int) ds_thread];
7265 break;
7266
7267 default:
7268 /* We did not yet find a decl-specifier yet. */
7269 found_decl_spec = false;
7270 break;
7271 }
7272
7273 /* Constructors are a special case. The `S' in `S()' is not a
7274 decl-specifier; it is the beginning of the declarator. */
7275 constructor_p
7276 = (!found_decl_spec
7277 && constructor_possible_p
7278 && (cp_parser_constructor_declarator_p
7279 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7280
7281 /* If we don't have a DECL_SPEC yet, then we must be looking at
7282 a type-specifier. */
7283 if (!found_decl_spec && !constructor_p)
7284 {
7285 int decl_spec_declares_class_or_enum;
7286 bool is_cv_qualifier;
7287 tree type_spec;
7288
7289 type_spec
7290 = cp_parser_type_specifier (parser, flags,
7291 decl_specs,
7292 /*is_declaration=*/true,
7293 &decl_spec_declares_class_or_enum,
7294 &is_cv_qualifier);
7295
7296 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7297
7298 /* If this type-specifier referenced a user-defined type
7299 (a typedef, class-name, etc.), then we can't allow any
7300 more such type-specifiers henceforth.
7301
7302 [dcl.spec]
7303
7304 The longest sequence of decl-specifiers that could
7305 possibly be a type name is taken as the
7306 decl-specifier-seq of a declaration. The sequence shall
7307 be self-consistent as described below.
7308
7309 [dcl.type]
7310
7311 As a general rule, at most one type-specifier is allowed
7312 in the complete decl-specifier-seq of a declaration. The
7313 only exceptions are the following:
7314
7315 -- const or volatile can be combined with any other
7316 type-specifier.
7317
7318 -- signed or unsigned can be combined with char, long,
7319 short, or int.
7320
7321 -- ..
7322
7323 Example:
7324
7325 typedef char* Pc;
7326 void g (const int Pc);
7327
7328 Here, Pc is *not* part of the decl-specifier seq; it's
7329 the declarator. Therefore, once we see a type-specifier
7330 (other than a cv-qualifier), we forbid any additional
7331 user-defined types. We *do* still allow things like `int
7332 int' to be considered a decl-specifier-seq, and issue the
7333 error message later. */
7334 if (type_spec && !is_cv_qualifier)
7335 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7336 /* A constructor declarator cannot follow a type-specifier. */
7337 if (type_spec)
7338 {
7339 constructor_possible_p = false;
7340 found_decl_spec = true;
7341 }
7342 }
7343
7344 /* If we still do not have a DECL_SPEC, then there are no more
7345 decl-specifiers. */
7346 if (!found_decl_spec)
7347 break;
7348
7349 decl_specs->any_specifiers_p = true;
7350 /* After we see one decl-specifier, further decl-specifiers are
7351 always optional. */
7352 flags |= CP_PARSER_FLAGS_OPTIONAL;
7353 }
7354
7355 /* Don't allow a friend specifier with a class definition. */
7356 if (decl_specs->specs[(int) ds_friend] != 0
7357 && (*declares_class_or_enum & 2))
7358 error ("class definition may not be declared a friend");
7359 }
7360
7361 /* Parse an (optional) storage-class-specifier.
7362
7363 storage-class-specifier:
7364 auto
7365 register
7366 static
7367 extern
7368 mutable
7369
7370 GNU Extension:
7371
7372 storage-class-specifier:
7373 thread
7374
7375 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7376
7377 static tree
7378 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7379 {
7380 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7381 {
7382 case RID_AUTO:
7383 case RID_REGISTER:
7384 case RID_STATIC:
7385 case RID_EXTERN:
7386 case RID_MUTABLE:
7387 case RID_THREAD:
7388 /* Consume the token. */
7389 return cp_lexer_consume_token (parser->lexer)->value;
7390
7391 default:
7392 return NULL_TREE;
7393 }
7394 }
7395
7396 /* Parse an (optional) function-specifier.
7397
7398 function-specifier:
7399 inline
7400 virtual
7401 explicit
7402
7403 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7404 Updates DECL_SPECS, if it is non-NULL. */
7405
7406 static tree
7407 cp_parser_function_specifier_opt (cp_parser* parser,
7408 cp_decl_specifier_seq *decl_specs)
7409 {
7410 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7411 {
7412 case RID_INLINE:
7413 if (decl_specs)
7414 ++decl_specs->specs[(int) ds_inline];
7415 break;
7416
7417 case RID_VIRTUAL:
7418 if (decl_specs)
7419 ++decl_specs->specs[(int) ds_virtual];
7420 break;
7421
7422 case RID_EXPLICIT:
7423 if (decl_specs)
7424 ++decl_specs->specs[(int) ds_explicit];
7425 break;
7426
7427 default:
7428 return NULL_TREE;
7429 }
7430
7431 /* Consume the token. */
7432 return cp_lexer_consume_token (parser->lexer)->value;
7433 }
7434
7435 /* Parse a linkage-specification.
7436
7437 linkage-specification:
7438 extern string-literal { declaration-seq [opt] }
7439 extern string-literal declaration */
7440
7441 static void
7442 cp_parser_linkage_specification (cp_parser* parser)
7443 {
7444 tree linkage;
7445
7446 /* Look for the `extern' keyword. */
7447 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7448
7449 /* Look for the string-literal. */
7450 linkage = cp_parser_string_literal (parser, false, false);
7451
7452 /* Transform the literal into an identifier. If the literal is a
7453 wide-character string, or contains embedded NULs, then we can't
7454 handle it as the user wants. */
7455 if (strlen (TREE_STRING_POINTER (linkage))
7456 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7457 {
7458 cp_parser_error (parser, "invalid linkage-specification");
7459 /* Assume C++ linkage. */
7460 linkage = lang_name_cplusplus;
7461 }
7462 else
7463 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7464
7465 /* We're now using the new linkage. */
7466 push_lang_context (linkage);
7467
7468 /* If the next token is a `{', then we're using the first
7469 production. */
7470 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7471 {
7472 /* Consume the `{' token. */
7473 cp_lexer_consume_token (parser->lexer);
7474 /* Parse the declarations. */
7475 cp_parser_declaration_seq_opt (parser);
7476 /* Look for the closing `}'. */
7477 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7478 }
7479 /* Otherwise, there's just one declaration. */
7480 else
7481 {
7482 bool saved_in_unbraced_linkage_specification_p;
7483
7484 saved_in_unbraced_linkage_specification_p
7485 = parser->in_unbraced_linkage_specification_p;
7486 parser->in_unbraced_linkage_specification_p = true;
7487 have_extern_spec = true;
7488 cp_parser_declaration (parser);
7489 have_extern_spec = false;
7490 parser->in_unbraced_linkage_specification_p
7491 = saved_in_unbraced_linkage_specification_p;
7492 }
7493
7494 /* We're done with the linkage-specification. */
7495 pop_lang_context ();
7496 }
7497
7498 /* Special member functions [gram.special] */
7499
7500 /* Parse a conversion-function-id.
7501
7502 conversion-function-id:
7503 operator conversion-type-id
7504
7505 Returns an IDENTIFIER_NODE representing the operator. */
7506
7507 static tree
7508 cp_parser_conversion_function_id (cp_parser* parser)
7509 {
7510 tree type;
7511 tree saved_scope;
7512 tree saved_qualifying_scope;
7513 tree saved_object_scope;
7514 tree pushed_scope = NULL_TREE;
7515
7516 /* Look for the `operator' token. */
7517 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7518 return error_mark_node;
7519 /* When we parse the conversion-type-id, the current scope will be
7520 reset. However, we need that information in able to look up the
7521 conversion function later, so we save it here. */
7522 saved_scope = parser->scope;
7523 saved_qualifying_scope = parser->qualifying_scope;
7524 saved_object_scope = parser->object_scope;
7525 /* We must enter the scope of the class so that the names of
7526 entities declared within the class are available in the
7527 conversion-type-id. For example, consider:
7528
7529 struct S {
7530 typedef int I;
7531 operator I();
7532 };
7533
7534 S::operator I() { ... }
7535
7536 In order to see that `I' is a type-name in the definition, we
7537 must be in the scope of `S'. */
7538 if (saved_scope)
7539 pushed_scope = push_scope (saved_scope);
7540 /* Parse the conversion-type-id. */
7541 type = cp_parser_conversion_type_id (parser);
7542 /* Leave the scope of the class, if any. */
7543 if (pushed_scope)
7544 pop_scope (pushed_scope);
7545 /* Restore the saved scope. */
7546 parser->scope = saved_scope;
7547 parser->qualifying_scope = saved_qualifying_scope;
7548 parser->object_scope = saved_object_scope;
7549 /* If the TYPE is invalid, indicate failure. */
7550 if (type == error_mark_node)
7551 return error_mark_node;
7552 return mangle_conv_op_name_for_type (type);
7553 }
7554
7555 /* Parse a conversion-type-id:
7556
7557 conversion-type-id:
7558 type-specifier-seq conversion-declarator [opt]
7559
7560 Returns the TYPE specified. */
7561
7562 static tree
7563 cp_parser_conversion_type_id (cp_parser* parser)
7564 {
7565 tree attributes;
7566 cp_decl_specifier_seq type_specifiers;
7567 cp_declarator *declarator;
7568 tree type_specified;
7569
7570 /* Parse the attributes. */
7571 attributes = cp_parser_attributes_opt (parser);
7572 /* Parse the type-specifiers. */
7573 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7574 &type_specifiers);
7575 /* If that didn't work, stop. */
7576 if (type_specifiers.type == error_mark_node)
7577 return error_mark_node;
7578 /* Parse the conversion-declarator. */
7579 declarator = cp_parser_conversion_declarator_opt (parser);
7580
7581 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7582 /*initialized=*/0, &attributes);
7583 if (attributes)
7584 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7585 return type_specified;
7586 }
7587
7588 /* Parse an (optional) conversion-declarator.
7589
7590 conversion-declarator:
7591 ptr-operator conversion-declarator [opt]
7592
7593 */
7594
7595 static cp_declarator *
7596 cp_parser_conversion_declarator_opt (cp_parser* parser)
7597 {
7598 enum tree_code code;
7599 tree class_type;
7600 cp_cv_quals cv_quals;
7601
7602 /* We don't know if there's a ptr-operator next, or not. */
7603 cp_parser_parse_tentatively (parser);
7604 /* Try the ptr-operator. */
7605 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7606 /* If it worked, look for more conversion-declarators. */
7607 if (cp_parser_parse_definitely (parser))
7608 {
7609 cp_declarator *declarator;
7610
7611 /* Parse another optional declarator. */
7612 declarator = cp_parser_conversion_declarator_opt (parser);
7613
7614 /* Create the representation of the declarator. */
7615 if (class_type)
7616 declarator = make_ptrmem_declarator (cv_quals, class_type,
7617 declarator);
7618 else if (code == INDIRECT_REF)
7619 declarator = make_pointer_declarator (cv_quals, declarator);
7620 else
7621 declarator = make_reference_declarator (cv_quals, declarator);
7622
7623 return declarator;
7624 }
7625
7626 return NULL;
7627 }
7628
7629 /* Parse an (optional) ctor-initializer.
7630
7631 ctor-initializer:
7632 : mem-initializer-list
7633
7634 Returns TRUE iff the ctor-initializer was actually present. */
7635
7636 static bool
7637 cp_parser_ctor_initializer_opt (cp_parser* parser)
7638 {
7639 /* If the next token is not a `:', then there is no
7640 ctor-initializer. */
7641 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7642 {
7643 /* Do default initialization of any bases and members. */
7644 if (DECL_CONSTRUCTOR_P (current_function_decl))
7645 finish_mem_initializers (NULL_TREE);
7646
7647 return false;
7648 }
7649
7650 /* Consume the `:' token. */
7651 cp_lexer_consume_token (parser->lexer);
7652 /* And the mem-initializer-list. */
7653 cp_parser_mem_initializer_list (parser);
7654
7655 return true;
7656 }
7657
7658 /* Parse a mem-initializer-list.
7659
7660 mem-initializer-list:
7661 mem-initializer
7662 mem-initializer , mem-initializer-list */
7663
7664 static void
7665 cp_parser_mem_initializer_list (cp_parser* parser)
7666 {
7667 tree mem_initializer_list = NULL_TREE;
7668
7669 /* Let the semantic analysis code know that we are starting the
7670 mem-initializer-list. */
7671 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7672 error ("only constructors take base initializers");
7673
7674 /* Loop through the list. */
7675 while (true)
7676 {
7677 tree mem_initializer;
7678
7679 /* Parse the mem-initializer. */
7680 mem_initializer = cp_parser_mem_initializer (parser);
7681 /* Add it to the list, unless it was erroneous. */
7682 if (mem_initializer)
7683 {
7684 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7685 mem_initializer_list = mem_initializer;
7686 }
7687 /* If the next token is not a `,', we're done. */
7688 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7689 break;
7690 /* Consume the `,' token. */
7691 cp_lexer_consume_token (parser->lexer);
7692 }
7693
7694 /* Perform semantic analysis. */
7695 if (DECL_CONSTRUCTOR_P (current_function_decl))
7696 finish_mem_initializers (mem_initializer_list);
7697 }
7698
7699 /* Parse a mem-initializer.
7700
7701 mem-initializer:
7702 mem-initializer-id ( expression-list [opt] )
7703
7704 GNU extension:
7705
7706 mem-initializer:
7707 ( expression-list [opt] )
7708
7709 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7710 class) or FIELD_DECL (for a non-static data member) to initialize;
7711 the TREE_VALUE is the expression-list. */
7712
7713 static tree
7714 cp_parser_mem_initializer (cp_parser* parser)
7715 {
7716 tree mem_initializer_id;
7717 tree expression_list;
7718 tree member;
7719
7720 /* Find out what is being initialized. */
7721 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7722 {
7723 pedwarn ("anachronistic old-style base class initializer");
7724 mem_initializer_id = NULL_TREE;
7725 }
7726 else
7727 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7728 member = expand_member_init (mem_initializer_id);
7729 if (member && !DECL_P (member))
7730 in_base_initializer = 1;
7731
7732 expression_list
7733 = cp_parser_parenthesized_expression_list (parser, false,
7734 /*cast_p=*/false,
7735 /*non_constant_p=*/NULL);
7736 if (!expression_list)
7737 expression_list = void_type_node;
7738
7739 in_base_initializer = 0;
7740
7741 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7742 }
7743
7744 /* Parse a mem-initializer-id.
7745
7746 mem-initializer-id:
7747 :: [opt] nested-name-specifier [opt] class-name
7748 identifier
7749
7750 Returns a TYPE indicating the class to be initializer for the first
7751 production. Returns an IDENTIFIER_NODE indicating the data member
7752 to be initialized for the second production. */
7753
7754 static tree
7755 cp_parser_mem_initializer_id (cp_parser* parser)
7756 {
7757 bool global_scope_p;
7758 bool nested_name_specifier_p;
7759 bool template_p = false;
7760 tree id;
7761
7762 /* `typename' is not allowed in this context ([temp.res]). */
7763 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7764 {
7765 error ("keyword %<typename%> not allowed in this context (a qualified "
7766 "member initializer is implicitly a type)");
7767 cp_lexer_consume_token (parser->lexer);
7768 }
7769 /* Look for the optional `::' operator. */
7770 global_scope_p
7771 = (cp_parser_global_scope_opt (parser,
7772 /*current_scope_valid_p=*/false)
7773 != NULL_TREE);
7774 /* Look for the optional nested-name-specifier. The simplest way to
7775 implement:
7776
7777 [temp.res]
7778
7779 The keyword `typename' is not permitted in a base-specifier or
7780 mem-initializer; in these contexts a qualified name that
7781 depends on a template-parameter is implicitly assumed to be a
7782 type name.
7783
7784 is to assume that we have seen the `typename' keyword at this
7785 point. */
7786 nested_name_specifier_p
7787 = (cp_parser_nested_name_specifier_opt (parser,
7788 /*typename_keyword_p=*/true,
7789 /*check_dependency_p=*/true,
7790 /*type_p=*/true,
7791 /*is_declaration=*/true)
7792 != NULL_TREE);
7793 if (nested_name_specifier_p)
7794 template_p = cp_parser_optional_template_keyword (parser);
7795 /* If there is a `::' operator or a nested-name-specifier, then we
7796 are definitely looking for a class-name. */
7797 if (global_scope_p || nested_name_specifier_p)
7798 return cp_parser_class_name (parser,
7799 /*typename_keyword_p=*/true,
7800 /*template_keyword_p=*/template_p,
7801 none_type,
7802 /*check_dependency_p=*/true,
7803 /*class_head_p=*/false,
7804 /*is_declaration=*/true);
7805 /* Otherwise, we could also be looking for an ordinary identifier. */
7806 cp_parser_parse_tentatively (parser);
7807 /* Try a class-name. */
7808 id = cp_parser_class_name (parser,
7809 /*typename_keyword_p=*/true,
7810 /*template_keyword_p=*/false,
7811 none_type,
7812 /*check_dependency_p=*/true,
7813 /*class_head_p=*/false,
7814 /*is_declaration=*/true);
7815 /* If we found one, we're done. */
7816 if (cp_parser_parse_definitely (parser))
7817 return id;
7818 /* Otherwise, look for an ordinary identifier. */
7819 return cp_parser_identifier (parser);
7820 }
7821
7822 /* Overloading [gram.over] */
7823
7824 /* Parse an operator-function-id.
7825
7826 operator-function-id:
7827 operator operator
7828
7829 Returns an IDENTIFIER_NODE for the operator which is a
7830 human-readable spelling of the identifier, e.g., `operator +'. */
7831
7832 static tree
7833 cp_parser_operator_function_id (cp_parser* parser)
7834 {
7835 /* Look for the `operator' keyword. */
7836 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7837 return error_mark_node;
7838 /* And then the name of the operator itself. */
7839 return cp_parser_operator (parser);
7840 }
7841
7842 /* Parse an operator.
7843
7844 operator:
7845 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7846 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7847 || ++ -- , ->* -> () []
7848
7849 GNU Extensions:
7850
7851 operator:
7852 <? >? <?= >?=
7853
7854 Returns an IDENTIFIER_NODE for the operator which is a
7855 human-readable spelling of the identifier, e.g., `operator +'. */
7856
7857 static tree
7858 cp_parser_operator (cp_parser* parser)
7859 {
7860 tree id = NULL_TREE;
7861 cp_token *token;
7862
7863 /* Peek at the next token. */
7864 token = cp_lexer_peek_token (parser->lexer);
7865 /* Figure out which operator we have. */
7866 switch (token->type)
7867 {
7868 case CPP_KEYWORD:
7869 {
7870 enum tree_code op;
7871
7872 /* The keyword should be either `new' or `delete'. */
7873 if (token->keyword == RID_NEW)
7874 op = NEW_EXPR;
7875 else if (token->keyword == RID_DELETE)
7876 op = DELETE_EXPR;
7877 else
7878 break;
7879
7880 /* Consume the `new' or `delete' token. */
7881 cp_lexer_consume_token (parser->lexer);
7882
7883 /* Peek at the next token. */
7884 token = cp_lexer_peek_token (parser->lexer);
7885 /* If it's a `[' token then this is the array variant of the
7886 operator. */
7887 if (token->type == CPP_OPEN_SQUARE)
7888 {
7889 /* Consume the `[' token. */
7890 cp_lexer_consume_token (parser->lexer);
7891 /* Look for the `]' token. */
7892 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7893 id = ansi_opname (op == NEW_EXPR
7894 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7895 }
7896 /* Otherwise, we have the non-array variant. */
7897 else
7898 id = ansi_opname (op);
7899
7900 return id;
7901 }
7902
7903 case CPP_PLUS:
7904 id = ansi_opname (PLUS_EXPR);
7905 break;
7906
7907 case CPP_MINUS:
7908 id = ansi_opname (MINUS_EXPR);
7909 break;
7910
7911 case CPP_MULT:
7912 id = ansi_opname (MULT_EXPR);
7913 break;
7914
7915 case CPP_DIV:
7916 id = ansi_opname (TRUNC_DIV_EXPR);
7917 break;
7918
7919 case CPP_MOD:
7920 id = ansi_opname (TRUNC_MOD_EXPR);
7921 break;
7922
7923 case CPP_XOR:
7924 id = ansi_opname (BIT_XOR_EXPR);
7925 break;
7926
7927 case CPP_AND:
7928 id = ansi_opname (BIT_AND_EXPR);
7929 break;
7930
7931 case CPP_OR:
7932 id = ansi_opname (BIT_IOR_EXPR);
7933 break;
7934
7935 case CPP_COMPL:
7936 id = ansi_opname (BIT_NOT_EXPR);
7937 break;
7938
7939 case CPP_NOT:
7940 id = ansi_opname (TRUTH_NOT_EXPR);
7941 break;
7942
7943 case CPP_EQ:
7944 id = ansi_assopname (NOP_EXPR);
7945 break;
7946
7947 case CPP_LESS:
7948 id = ansi_opname (LT_EXPR);
7949 break;
7950
7951 case CPP_GREATER:
7952 id = ansi_opname (GT_EXPR);
7953 break;
7954
7955 case CPP_PLUS_EQ:
7956 id = ansi_assopname (PLUS_EXPR);
7957 break;
7958
7959 case CPP_MINUS_EQ:
7960 id = ansi_assopname (MINUS_EXPR);
7961 break;
7962
7963 case CPP_MULT_EQ:
7964 id = ansi_assopname (MULT_EXPR);
7965 break;
7966
7967 case CPP_DIV_EQ:
7968 id = ansi_assopname (TRUNC_DIV_EXPR);
7969 break;
7970
7971 case CPP_MOD_EQ:
7972 id = ansi_assopname (TRUNC_MOD_EXPR);
7973 break;
7974
7975 case CPP_XOR_EQ:
7976 id = ansi_assopname (BIT_XOR_EXPR);
7977 break;
7978
7979 case CPP_AND_EQ:
7980 id = ansi_assopname (BIT_AND_EXPR);
7981 break;
7982
7983 case CPP_OR_EQ:
7984 id = ansi_assopname (BIT_IOR_EXPR);
7985 break;
7986
7987 case CPP_LSHIFT:
7988 id = ansi_opname (LSHIFT_EXPR);
7989 break;
7990
7991 case CPP_RSHIFT:
7992 id = ansi_opname (RSHIFT_EXPR);
7993 break;
7994
7995 case CPP_LSHIFT_EQ:
7996 id = ansi_assopname (LSHIFT_EXPR);
7997 break;
7998
7999 case CPP_RSHIFT_EQ:
8000 id = ansi_assopname (RSHIFT_EXPR);
8001 break;
8002
8003 case CPP_EQ_EQ:
8004 id = ansi_opname (EQ_EXPR);
8005 break;
8006
8007 case CPP_NOT_EQ:
8008 id = ansi_opname (NE_EXPR);
8009 break;
8010
8011 case CPP_LESS_EQ:
8012 id = ansi_opname (LE_EXPR);
8013 break;
8014
8015 case CPP_GREATER_EQ:
8016 id = ansi_opname (GE_EXPR);
8017 break;
8018
8019 case CPP_AND_AND:
8020 id = ansi_opname (TRUTH_ANDIF_EXPR);
8021 break;
8022
8023 case CPP_OR_OR:
8024 id = ansi_opname (TRUTH_ORIF_EXPR);
8025 break;
8026
8027 case CPP_PLUS_PLUS:
8028 id = ansi_opname (POSTINCREMENT_EXPR);
8029 break;
8030
8031 case CPP_MINUS_MINUS:
8032 id = ansi_opname (PREDECREMENT_EXPR);
8033 break;
8034
8035 case CPP_COMMA:
8036 id = ansi_opname (COMPOUND_EXPR);
8037 break;
8038
8039 case CPP_DEREF_STAR:
8040 id = ansi_opname (MEMBER_REF);
8041 break;
8042
8043 case CPP_DEREF:
8044 id = ansi_opname (COMPONENT_REF);
8045 break;
8046
8047 case CPP_OPEN_PAREN:
8048 /* Consume the `('. */
8049 cp_lexer_consume_token (parser->lexer);
8050 /* Look for the matching `)'. */
8051 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8052 return ansi_opname (CALL_EXPR);
8053
8054 case CPP_OPEN_SQUARE:
8055 /* Consume the `['. */
8056 cp_lexer_consume_token (parser->lexer);
8057 /* Look for the matching `]'. */
8058 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8059 return ansi_opname (ARRAY_REF);
8060
8061 /* Extensions. */
8062 case CPP_MIN:
8063 id = ansi_opname (MIN_EXPR);
8064 cp_parser_warn_min_max ();
8065 break;
8066
8067 case CPP_MAX:
8068 id = ansi_opname (MAX_EXPR);
8069 cp_parser_warn_min_max ();
8070 break;
8071
8072 case CPP_MIN_EQ:
8073 id = ansi_assopname (MIN_EXPR);
8074 cp_parser_warn_min_max ();
8075 break;
8076
8077 case CPP_MAX_EQ:
8078 id = ansi_assopname (MAX_EXPR);
8079 cp_parser_warn_min_max ();
8080 break;
8081
8082 default:
8083 /* Anything else is an error. */
8084 break;
8085 }
8086
8087 /* If we have selected an identifier, we need to consume the
8088 operator token. */
8089 if (id)
8090 cp_lexer_consume_token (parser->lexer);
8091 /* Otherwise, no valid operator name was present. */
8092 else
8093 {
8094 cp_parser_error (parser, "expected operator");
8095 id = error_mark_node;
8096 }
8097
8098 return id;
8099 }
8100
8101 /* Parse a template-declaration.
8102
8103 template-declaration:
8104 export [opt] template < template-parameter-list > declaration
8105
8106 If MEMBER_P is TRUE, this template-declaration occurs within a
8107 class-specifier.
8108
8109 The grammar rule given by the standard isn't correct. What
8110 is really meant is:
8111
8112 template-declaration:
8113 export [opt] template-parameter-list-seq
8114 decl-specifier-seq [opt] init-declarator [opt] ;
8115 export [opt] template-parameter-list-seq
8116 function-definition
8117
8118 template-parameter-list-seq:
8119 template-parameter-list-seq [opt]
8120 template < template-parameter-list > */
8121
8122 static void
8123 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8124 {
8125 /* Check for `export'. */
8126 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8127 {
8128 /* Consume the `export' token. */
8129 cp_lexer_consume_token (parser->lexer);
8130 /* Warn that we do not support `export'. */
8131 warning (0, "keyword %<export%> not implemented, and will be ignored");
8132 }
8133
8134 cp_parser_template_declaration_after_export (parser, member_p);
8135 }
8136
8137 /* Parse a template-parameter-list.
8138
8139 template-parameter-list:
8140 template-parameter
8141 template-parameter-list , template-parameter
8142
8143 Returns a TREE_LIST. Each node represents a template parameter.
8144 The nodes are connected via their TREE_CHAINs. */
8145
8146 static tree
8147 cp_parser_template_parameter_list (cp_parser* parser)
8148 {
8149 tree parameter_list = NULL_TREE;
8150
8151 while (true)
8152 {
8153 tree parameter;
8154 cp_token *token;
8155 bool is_non_type;
8156
8157 /* Parse the template-parameter. */
8158 parameter = cp_parser_template_parameter (parser, &is_non_type);
8159 /* Add it to the list. */
8160 if (parameter != error_mark_node)
8161 parameter_list = process_template_parm (parameter_list,
8162 parameter,
8163 is_non_type);
8164 /* Peek at the next token. */
8165 token = cp_lexer_peek_token (parser->lexer);
8166 /* If it's not a `,', we're done. */
8167 if (token->type != CPP_COMMA)
8168 break;
8169 /* Otherwise, consume the `,' token. */
8170 cp_lexer_consume_token (parser->lexer);
8171 }
8172
8173 return parameter_list;
8174 }
8175
8176 /* Parse a template-parameter.
8177
8178 template-parameter:
8179 type-parameter
8180 parameter-declaration
8181
8182 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8183 the parameter. The TREE_PURPOSE is the default value, if any.
8184 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8185 iff this parameter is a non-type parameter. */
8186
8187 static tree
8188 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8189 {
8190 cp_token *token;
8191 cp_parameter_declarator *parameter_declarator;
8192 tree parm;
8193
8194 /* Assume it is a type parameter or a template parameter. */
8195 *is_non_type = false;
8196 /* Peek at the next token. */
8197 token = cp_lexer_peek_token (parser->lexer);
8198 /* If it is `class' or `template', we have a type-parameter. */
8199 if (token->keyword == RID_TEMPLATE)
8200 return cp_parser_type_parameter (parser);
8201 /* If it is `class' or `typename' we do not know yet whether it is a
8202 type parameter or a non-type parameter. Consider:
8203
8204 template <typename T, typename T::X X> ...
8205
8206 or:
8207
8208 template <class C, class D*> ...
8209
8210 Here, the first parameter is a type parameter, and the second is
8211 a non-type parameter. We can tell by looking at the token after
8212 the identifier -- if it is a `,', `=', or `>' then we have a type
8213 parameter. */
8214 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8215 {
8216 /* Peek at the token after `class' or `typename'. */
8217 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8218 /* If it's an identifier, skip it. */
8219 if (token->type == CPP_NAME)
8220 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8221 /* Now, see if the token looks like the end of a template
8222 parameter. */
8223 if (token->type == CPP_COMMA
8224 || token->type == CPP_EQ
8225 || token->type == CPP_GREATER)
8226 return cp_parser_type_parameter (parser);
8227 }
8228
8229 /* Otherwise, it is a non-type parameter.
8230
8231 [temp.param]
8232
8233 When parsing a default template-argument for a non-type
8234 template-parameter, the first non-nested `>' is taken as the end
8235 of the template parameter-list rather than a greater-than
8236 operator. */
8237 *is_non_type = true;
8238 parameter_declarator
8239 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8240 /*parenthesized_p=*/NULL);
8241 parm = grokdeclarator (parameter_declarator->declarator,
8242 &parameter_declarator->decl_specifiers,
8243 PARM, /*initialized=*/0,
8244 /*attrlist=*/NULL);
8245 if (parm == error_mark_node)
8246 return error_mark_node;
8247 return build_tree_list (parameter_declarator->default_argument, parm);
8248 }
8249
8250 /* Parse a type-parameter.
8251
8252 type-parameter:
8253 class identifier [opt]
8254 class identifier [opt] = type-id
8255 typename identifier [opt]
8256 typename identifier [opt] = type-id
8257 template < template-parameter-list > class identifier [opt]
8258 template < template-parameter-list > class identifier [opt]
8259 = id-expression
8260
8261 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8262 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8263 the declaration of the parameter. */
8264
8265 static tree
8266 cp_parser_type_parameter (cp_parser* parser)
8267 {
8268 cp_token *token;
8269 tree parameter;
8270
8271 /* Look for a keyword to tell us what kind of parameter this is. */
8272 token = cp_parser_require (parser, CPP_KEYWORD,
8273 "`class', `typename', or `template'");
8274 if (!token)
8275 return error_mark_node;
8276
8277 switch (token->keyword)
8278 {
8279 case RID_CLASS:
8280 case RID_TYPENAME:
8281 {
8282 tree identifier;
8283 tree default_argument;
8284
8285 /* If the next token is an identifier, then it names the
8286 parameter. */
8287 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8288 identifier = cp_parser_identifier (parser);
8289 else
8290 identifier = NULL_TREE;
8291
8292 /* Create the parameter. */
8293 parameter = finish_template_type_parm (class_type_node, identifier);
8294
8295 /* If the next token is an `=', we have a default argument. */
8296 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8297 {
8298 /* Consume the `=' token. */
8299 cp_lexer_consume_token (parser->lexer);
8300 /* Parse the default-argument. */
8301 default_argument = cp_parser_type_id (parser);
8302 }
8303 else
8304 default_argument = NULL_TREE;
8305
8306 /* Create the combined representation of the parameter and the
8307 default argument. */
8308 parameter = build_tree_list (default_argument, parameter);
8309 }
8310 break;
8311
8312 case RID_TEMPLATE:
8313 {
8314 tree parameter_list;
8315 tree identifier;
8316 tree default_argument;
8317
8318 /* Look for the `<'. */
8319 cp_parser_require (parser, CPP_LESS, "`<'");
8320 /* Parse the template-parameter-list. */
8321 begin_template_parm_list ();
8322 parameter_list
8323 = cp_parser_template_parameter_list (parser);
8324 parameter_list = end_template_parm_list (parameter_list);
8325 /* Look for the `>'. */
8326 cp_parser_require (parser, CPP_GREATER, "`>'");
8327 /* Look for the `class' keyword. */
8328 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8329 /* If the next token is an `=', then there is a
8330 default-argument. If the next token is a `>', we are at
8331 the end of the parameter-list. If the next token is a `,',
8332 then we are at the end of this parameter. */
8333 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8334 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8335 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8336 {
8337 identifier = cp_parser_identifier (parser);
8338 /* Treat invalid names as if the parameter were nameless. */
8339 if (identifier == error_mark_node)
8340 identifier = NULL_TREE;
8341 }
8342 else
8343 identifier = NULL_TREE;
8344
8345 /* Create the template parameter. */
8346 parameter = finish_template_template_parm (class_type_node,
8347 identifier);
8348
8349 /* If the next token is an `=', then there is a
8350 default-argument. */
8351 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8352 {
8353 bool is_template;
8354
8355 /* Consume the `='. */
8356 cp_lexer_consume_token (parser->lexer);
8357 /* Parse the id-expression. */
8358 default_argument
8359 = cp_parser_id_expression (parser,
8360 /*template_keyword_p=*/false,
8361 /*check_dependency_p=*/true,
8362 /*template_p=*/&is_template,
8363 /*declarator_p=*/false);
8364 if (TREE_CODE (default_argument) == TYPE_DECL)
8365 /* If the id-expression was a template-id that refers to
8366 a template-class, we already have the declaration here,
8367 so no further lookup is needed. */
8368 ;
8369 else
8370 /* Look up the name. */
8371 default_argument
8372 = cp_parser_lookup_name (parser, default_argument,
8373 none_type,
8374 /*is_template=*/is_template,
8375 /*is_namespace=*/false,
8376 /*check_dependency=*/true,
8377 /*ambiguous_p=*/NULL);
8378 /* See if the default argument is valid. */
8379 default_argument
8380 = check_template_template_default_arg (default_argument);
8381 }
8382 else
8383 default_argument = NULL_TREE;
8384
8385 /* Create the combined representation of the parameter and the
8386 default argument. */
8387 parameter = build_tree_list (default_argument, parameter);
8388 }
8389 break;
8390
8391 default:
8392 gcc_unreachable ();
8393 break;
8394 }
8395
8396 return parameter;
8397 }
8398
8399 /* Parse a template-id.
8400
8401 template-id:
8402 template-name < template-argument-list [opt] >
8403
8404 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8405 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8406 returned. Otherwise, if the template-name names a function, or set
8407 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8408 names a class, returns a TYPE_DECL for the specialization.
8409
8410 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8411 uninstantiated templates. */
8412
8413 static tree
8414 cp_parser_template_id (cp_parser *parser,
8415 bool template_keyword_p,
8416 bool check_dependency_p,
8417 bool is_declaration)
8418 {
8419 tree template;
8420 tree arguments;
8421 tree template_id;
8422 cp_token_position start_of_id = 0;
8423 tree access_check = NULL_TREE;
8424 cp_token *next_token, *next_token_2;
8425 bool is_identifier;
8426
8427 /* If the next token corresponds to a template-id, there is no need
8428 to reparse it. */
8429 next_token = cp_lexer_peek_token (parser->lexer);
8430 if (next_token->type == CPP_TEMPLATE_ID)
8431 {
8432 tree value;
8433 tree check;
8434
8435 /* Get the stored value. */
8436 value = cp_lexer_consume_token (parser->lexer)->value;
8437 /* Perform any access checks that were deferred. */
8438 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8439 perform_or_defer_access_check (TREE_PURPOSE (check),
8440 TREE_VALUE (check));
8441 /* Return the stored value. */
8442 return TREE_VALUE (value);
8443 }
8444
8445 /* Avoid performing name lookup if there is no possibility of
8446 finding a template-id. */
8447 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8448 || (next_token->type == CPP_NAME
8449 && !cp_parser_nth_token_starts_template_argument_list_p
8450 (parser, 2)))
8451 {
8452 cp_parser_error (parser, "expected template-id");
8453 return error_mark_node;
8454 }
8455
8456 /* Remember where the template-id starts. */
8457 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8458 start_of_id = cp_lexer_token_position (parser->lexer, false);
8459
8460 push_deferring_access_checks (dk_deferred);
8461
8462 /* Parse the template-name. */
8463 is_identifier = false;
8464 template = cp_parser_template_name (parser, template_keyword_p,
8465 check_dependency_p,
8466 is_declaration,
8467 &is_identifier);
8468 if (template == error_mark_node || is_identifier)
8469 {
8470 pop_deferring_access_checks ();
8471 return template;
8472 }
8473
8474 /* If we find the sequence `[:' after a template-name, it's probably
8475 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8476 parse correctly the argument list. */
8477 next_token = cp_lexer_peek_token (parser->lexer);
8478 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8479 if (next_token->type == CPP_OPEN_SQUARE
8480 && next_token->flags & DIGRAPH
8481 && next_token_2->type == CPP_COLON
8482 && !(next_token_2->flags & PREV_WHITE))
8483 {
8484 cp_parser_parse_tentatively (parser);
8485 /* Change `:' into `::'. */
8486 next_token_2->type = CPP_SCOPE;
8487 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8488 CPP_LESS. */
8489 cp_lexer_consume_token (parser->lexer);
8490 /* Parse the arguments. */
8491 arguments = cp_parser_enclosed_template_argument_list (parser);
8492 if (!cp_parser_parse_definitely (parser))
8493 {
8494 /* If we couldn't parse an argument list, then we revert our changes
8495 and return simply an error. Maybe this is not a template-id
8496 after all. */
8497 next_token_2->type = CPP_COLON;
8498 cp_parser_error (parser, "expected %<<%>");
8499 pop_deferring_access_checks ();
8500 return error_mark_node;
8501 }
8502 /* Otherwise, emit an error about the invalid digraph, but continue
8503 parsing because we got our argument list. */
8504 pedwarn ("%<<::%> cannot begin a template-argument list");
8505 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8506 "between %<<%> and %<::%>");
8507 if (!flag_permissive)
8508 {
8509 static bool hint;
8510 if (!hint)
8511 {
8512 inform ("(if you use -fpermissive G++ will accept your code)");
8513 hint = true;
8514 }
8515 }
8516 }
8517 else
8518 {
8519 /* Look for the `<' that starts the template-argument-list. */
8520 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8521 {
8522 pop_deferring_access_checks ();
8523 return error_mark_node;
8524 }
8525 /* Parse the arguments. */
8526 arguments = cp_parser_enclosed_template_argument_list (parser);
8527 }
8528
8529 /* Build a representation of the specialization. */
8530 if (TREE_CODE (template) == IDENTIFIER_NODE)
8531 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8532 else if (DECL_CLASS_TEMPLATE_P (template)
8533 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8534 template_id
8535 = finish_template_type (template, arguments,
8536 cp_lexer_next_token_is (parser->lexer,
8537 CPP_SCOPE));
8538 else
8539 {
8540 /* If it's not a class-template or a template-template, it should be
8541 a function-template. */
8542 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8543 || TREE_CODE (template) == OVERLOAD
8544 || BASELINK_P (template)));
8545
8546 template_id = lookup_template_function (template, arguments);
8547 }
8548
8549 /* Retrieve any deferred checks. Do not pop this access checks yet
8550 so the memory will not be reclaimed during token replacing below. */
8551 access_check = get_deferred_access_checks ();
8552
8553 /* If parsing tentatively, replace the sequence of tokens that makes
8554 up the template-id with a CPP_TEMPLATE_ID token. That way,
8555 should we re-parse the token stream, we will not have to repeat
8556 the effort required to do the parse, nor will we issue duplicate
8557 error messages about problems during instantiation of the
8558 template. */
8559 if (start_of_id)
8560 {
8561 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8562
8563 /* Reset the contents of the START_OF_ID token. */
8564 token->type = CPP_TEMPLATE_ID;
8565 token->value = build_tree_list (access_check, template_id);
8566 token->keyword = RID_MAX;
8567
8568 /* Purge all subsequent tokens. */
8569 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8570
8571 /* ??? Can we actually assume that, if template_id ==
8572 error_mark_node, we will have issued a diagnostic to the
8573 user, as opposed to simply marking the tentative parse as
8574 failed? */
8575 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8576 error ("parse error in template argument list");
8577 }
8578
8579 pop_deferring_access_checks ();
8580 return template_id;
8581 }
8582
8583 /* Parse a template-name.
8584
8585 template-name:
8586 identifier
8587
8588 The standard should actually say:
8589
8590 template-name:
8591 identifier
8592 operator-function-id
8593
8594 A defect report has been filed about this issue.
8595
8596 A conversion-function-id cannot be a template name because they cannot
8597 be part of a template-id. In fact, looking at this code:
8598
8599 a.operator K<int>()
8600
8601 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8602 It is impossible to call a templated conversion-function-id with an
8603 explicit argument list, since the only allowed template parameter is
8604 the type to which it is converting.
8605
8606 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8607 `template' keyword, in a construction like:
8608
8609 T::template f<3>()
8610
8611 In that case `f' is taken to be a template-name, even though there
8612 is no way of knowing for sure.
8613
8614 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8615 name refers to a set of overloaded functions, at least one of which
8616 is a template, or an IDENTIFIER_NODE with the name of the template,
8617 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8618 names are looked up inside uninstantiated templates. */
8619
8620 static tree
8621 cp_parser_template_name (cp_parser* parser,
8622 bool template_keyword_p,
8623 bool check_dependency_p,
8624 bool is_declaration,
8625 bool *is_identifier)
8626 {
8627 tree identifier;
8628 tree decl;
8629 tree fns;
8630
8631 /* If the next token is `operator', then we have either an
8632 operator-function-id or a conversion-function-id. */
8633 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8634 {
8635 /* We don't know whether we're looking at an
8636 operator-function-id or a conversion-function-id. */
8637 cp_parser_parse_tentatively (parser);
8638 /* Try an operator-function-id. */
8639 identifier = cp_parser_operator_function_id (parser);
8640 /* If that didn't work, try a conversion-function-id. */
8641 if (!cp_parser_parse_definitely (parser))
8642 {
8643 cp_parser_error (parser, "expected template-name");
8644 return error_mark_node;
8645 }
8646 }
8647 /* Look for the identifier. */
8648 else
8649 identifier = cp_parser_identifier (parser);
8650
8651 /* If we didn't find an identifier, we don't have a template-id. */
8652 if (identifier == error_mark_node)
8653 return error_mark_node;
8654
8655 /* If the name immediately followed the `template' keyword, then it
8656 is a template-name. However, if the next token is not `<', then
8657 we do not treat it as a template-name, since it is not being used
8658 as part of a template-id. This enables us to handle constructs
8659 like:
8660
8661 template <typename T> struct S { S(); };
8662 template <typename T> S<T>::S();
8663
8664 correctly. We would treat `S' as a template -- if it were `S<T>'
8665 -- but we do not if there is no `<'. */
8666
8667 if (processing_template_decl
8668 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8669 {
8670 /* In a declaration, in a dependent context, we pretend that the
8671 "template" keyword was present in order to improve error
8672 recovery. For example, given:
8673
8674 template <typename T> void f(T::X<int>);
8675
8676 we want to treat "X<int>" as a template-id. */
8677 if (is_declaration
8678 && !template_keyword_p
8679 && parser->scope && TYPE_P (parser->scope)
8680 && check_dependency_p
8681 && dependent_type_p (parser->scope)
8682 /* Do not do this for dtors (or ctors), since they never
8683 need the template keyword before their name. */
8684 && !constructor_name_p (identifier, parser->scope))
8685 {
8686 cp_token_position start = 0;
8687
8688 /* Explain what went wrong. */
8689 error ("non-template %qD used as template", identifier);
8690 inform ("use %<%T::template %D%> to indicate that it is a template",
8691 parser->scope, identifier);
8692 /* If parsing tentatively, find the location of the "<" token. */
8693 if (cp_parser_simulate_error (parser))
8694 start = cp_lexer_token_position (parser->lexer, true);
8695 /* Parse the template arguments so that we can issue error
8696 messages about them. */
8697 cp_lexer_consume_token (parser->lexer);
8698 cp_parser_enclosed_template_argument_list (parser);
8699 /* Skip tokens until we find a good place from which to
8700 continue parsing. */
8701 cp_parser_skip_to_closing_parenthesis (parser,
8702 /*recovering=*/true,
8703 /*or_comma=*/true,
8704 /*consume_paren=*/false);
8705 /* If parsing tentatively, permanently remove the
8706 template argument list. That will prevent duplicate
8707 error messages from being issued about the missing
8708 "template" keyword. */
8709 if (start)
8710 cp_lexer_purge_tokens_after (parser->lexer, start);
8711 if (is_identifier)
8712 *is_identifier = true;
8713 return identifier;
8714 }
8715
8716 /* If the "template" keyword is present, then there is generally
8717 no point in doing name-lookup, so we just return IDENTIFIER.
8718 But, if the qualifying scope is non-dependent then we can
8719 (and must) do name-lookup normally. */
8720 if (template_keyword_p
8721 && (!parser->scope
8722 || (TYPE_P (parser->scope)
8723 && dependent_type_p (parser->scope))))
8724 return identifier;
8725 }
8726
8727 /* Look up the name. */
8728 decl = cp_parser_lookup_name (parser, identifier,
8729 none_type,
8730 /*is_template=*/false,
8731 /*is_namespace=*/false,
8732 check_dependency_p,
8733 /*ambiguous_p=*/NULL);
8734 decl = maybe_get_template_decl_from_type_decl (decl);
8735
8736 /* If DECL is a template, then the name was a template-name. */
8737 if (TREE_CODE (decl) == TEMPLATE_DECL)
8738 ;
8739 else
8740 {
8741 tree fn = NULL_TREE;
8742
8743 /* The standard does not explicitly indicate whether a name that
8744 names a set of overloaded declarations, some of which are
8745 templates, is a template-name. However, such a name should
8746 be a template-name; otherwise, there is no way to form a
8747 template-id for the overloaded templates. */
8748 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8749 if (TREE_CODE (fns) == OVERLOAD)
8750 for (fn = fns; fn; fn = OVL_NEXT (fn))
8751 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8752 break;
8753
8754 if (!fn)
8755 {
8756 /* The name does not name a template. */
8757 cp_parser_error (parser, "expected template-name");
8758 return error_mark_node;
8759 }
8760 }
8761
8762 /* If DECL is dependent, and refers to a function, then just return
8763 its name; we will look it up again during template instantiation. */
8764 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8765 {
8766 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8767 if (TYPE_P (scope) && dependent_type_p (scope))
8768 return identifier;
8769 }
8770
8771 return decl;
8772 }
8773
8774 /* Parse a template-argument-list.
8775
8776 template-argument-list:
8777 template-argument
8778 template-argument-list , template-argument
8779
8780 Returns a TREE_VEC containing the arguments. */
8781
8782 static tree
8783 cp_parser_template_argument_list (cp_parser* parser)
8784 {
8785 tree fixed_args[10];
8786 unsigned n_args = 0;
8787 unsigned alloced = 10;
8788 tree *arg_ary = fixed_args;
8789 tree vec;
8790 bool saved_in_template_argument_list_p;
8791
8792 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8793 parser->in_template_argument_list_p = true;
8794 do
8795 {
8796 tree argument;
8797
8798 if (n_args)
8799 /* Consume the comma. */
8800 cp_lexer_consume_token (parser->lexer);
8801
8802 /* Parse the template-argument. */
8803 argument = cp_parser_template_argument (parser);
8804 if (n_args == alloced)
8805 {
8806 alloced *= 2;
8807
8808 if (arg_ary == fixed_args)
8809 {
8810 arg_ary = xmalloc (sizeof (tree) * alloced);
8811 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8812 }
8813 else
8814 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8815 }
8816 arg_ary[n_args++] = argument;
8817 }
8818 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8819
8820 vec = make_tree_vec (n_args);
8821
8822 while (n_args--)
8823 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8824
8825 if (arg_ary != fixed_args)
8826 free (arg_ary);
8827 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8828 return vec;
8829 }
8830
8831 /* Parse a template-argument.
8832
8833 template-argument:
8834 assignment-expression
8835 type-id
8836 id-expression
8837
8838 The representation is that of an assignment-expression, type-id, or
8839 id-expression -- except that the qualified id-expression is
8840 evaluated, so that the value returned is either a DECL or an
8841 OVERLOAD.
8842
8843 Although the standard says "assignment-expression", it forbids
8844 throw-expressions or assignments in the template argument.
8845 Therefore, we use "conditional-expression" instead. */
8846
8847 static tree
8848 cp_parser_template_argument (cp_parser* parser)
8849 {
8850 tree argument;
8851 bool template_p;
8852 bool address_p;
8853 bool maybe_type_id = false;
8854 cp_token *token;
8855 cp_id_kind idk;
8856 tree qualifying_class;
8857
8858 /* There's really no way to know what we're looking at, so we just
8859 try each alternative in order.
8860
8861 [temp.arg]
8862
8863 In a template-argument, an ambiguity between a type-id and an
8864 expression is resolved to a type-id, regardless of the form of
8865 the corresponding template-parameter.
8866
8867 Therefore, we try a type-id first. */
8868 cp_parser_parse_tentatively (parser);
8869 argument = cp_parser_type_id (parser);
8870 /* If there was no error parsing the type-id but the next token is a '>>',
8871 we probably found a typo for '> >'. But there are type-id which are
8872 also valid expressions. For instance:
8873
8874 struct X { int operator >> (int); };
8875 template <int V> struct Foo {};
8876 Foo<X () >> 5> r;
8877
8878 Here 'X()' is a valid type-id of a function type, but the user just
8879 wanted to write the expression "X() >> 5". Thus, we remember that we
8880 found a valid type-id, but we still try to parse the argument as an
8881 expression to see what happens. */
8882 if (!cp_parser_error_occurred (parser)
8883 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8884 {
8885 maybe_type_id = true;
8886 cp_parser_abort_tentative_parse (parser);
8887 }
8888 else
8889 {
8890 /* If the next token isn't a `,' or a `>', then this argument wasn't
8891 really finished. This means that the argument is not a valid
8892 type-id. */
8893 if (!cp_parser_next_token_ends_template_argument_p (parser))
8894 cp_parser_error (parser, "expected template-argument");
8895 /* If that worked, we're done. */
8896 if (cp_parser_parse_definitely (parser))
8897 return argument;
8898 }
8899 /* We're still not sure what the argument will be. */
8900 cp_parser_parse_tentatively (parser);
8901 /* Try a template. */
8902 argument = cp_parser_id_expression (parser,
8903 /*template_keyword_p=*/false,
8904 /*check_dependency_p=*/true,
8905 &template_p,
8906 /*declarator_p=*/false);
8907 /* If the next token isn't a `,' or a `>', then this argument wasn't
8908 really finished. */
8909 if (!cp_parser_next_token_ends_template_argument_p (parser))
8910 cp_parser_error (parser, "expected template-argument");
8911 if (!cp_parser_error_occurred (parser))
8912 {
8913 /* Figure out what is being referred to. If the id-expression
8914 was for a class template specialization, then we will have a
8915 TYPE_DECL at this point. There is no need to do name lookup
8916 at this point in that case. */
8917 if (TREE_CODE (argument) != TYPE_DECL)
8918 argument = cp_parser_lookup_name (parser, argument,
8919 none_type,
8920 /*is_template=*/template_p,
8921 /*is_namespace=*/false,
8922 /*check_dependency=*/true,
8923 /*ambiguous_p=*/NULL);
8924 if (TREE_CODE (argument) != TEMPLATE_DECL
8925 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8926 cp_parser_error (parser, "expected template-name");
8927 }
8928 if (cp_parser_parse_definitely (parser))
8929 return argument;
8930 /* It must be a non-type argument. There permitted cases are given
8931 in [temp.arg.nontype]:
8932
8933 -- an integral constant-expression of integral or enumeration
8934 type; or
8935
8936 -- the name of a non-type template-parameter; or
8937
8938 -- the name of an object or function with external linkage...
8939
8940 -- the address of an object or function with external linkage...
8941
8942 -- a pointer to member... */
8943 /* Look for a non-type template parameter. */
8944 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8945 {
8946 cp_parser_parse_tentatively (parser);
8947 argument = cp_parser_primary_expression (parser,
8948 /*cast_p=*/false,
8949 &idk,
8950 &qualifying_class);
8951 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8952 || !cp_parser_next_token_ends_template_argument_p (parser))
8953 cp_parser_simulate_error (parser);
8954 if (cp_parser_parse_definitely (parser))
8955 return argument;
8956 }
8957
8958 /* If the next token is "&", the argument must be the address of an
8959 object or function with external linkage. */
8960 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8961 if (address_p)
8962 cp_lexer_consume_token (parser->lexer);
8963 /* See if we might have an id-expression. */
8964 token = cp_lexer_peek_token (parser->lexer);
8965 if (token->type == CPP_NAME
8966 || token->keyword == RID_OPERATOR
8967 || token->type == CPP_SCOPE
8968 || token->type == CPP_TEMPLATE_ID
8969 || token->type == CPP_NESTED_NAME_SPECIFIER)
8970 {
8971 cp_parser_parse_tentatively (parser);
8972 argument = cp_parser_primary_expression (parser,
8973 /*cast_p=*/false,
8974 &idk,
8975 &qualifying_class);
8976 if (cp_parser_error_occurred (parser)
8977 || !cp_parser_next_token_ends_template_argument_p (parser))
8978 cp_parser_abort_tentative_parse (parser);
8979 else
8980 {
8981 if (TREE_CODE (argument) == INDIRECT_REF)
8982 {
8983 gcc_assert (REFERENCE_REF_P (argument));
8984 argument = TREE_OPERAND (argument, 0);
8985 }
8986
8987 if (qualifying_class)
8988 argument = finish_qualified_id_expr (qualifying_class,
8989 argument,
8990 /*done=*/true,
8991 address_p);
8992 if (TREE_CODE (argument) == VAR_DECL)
8993 {
8994 /* A variable without external linkage might still be a
8995 valid constant-expression, so no error is issued here
8996 if the external-linkage check fails. */
8997 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8998 cp_parser_simulate_error (parser);
8999 }
9000 else if (is_overloaded_fn (argument))
9001 /* All overloaded functions are allowed; if the external
9002 linkage test does not pass, an error will be issued
9003 later. */
9004 ;
9005 else if (address_p
9006 && (TREE_CODE (argument) == OFFSET_REF
9007 || TREE_CODE (argument) == SCOPE_REF))
9008 /* A pointer-to-member. */
9009 ;
9010 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9011 ;
9012 else
9013 cp_parser_simulate_error (parser);
9014
9015 if (cp_parser_parse_definitely (parser))
9016 {
9017 if (address_p)
9018 argument = build_x_unary_op (ADDR_EXPR, argument);
9019 return argument;
9020 }
9021 }
9022 }
9023 /* If the argument started with "&", there are no other valid
9024 alternatives at this point. */
9025 if (address_p)
9026 {
9027 cp_parser_error (parser, "invalid non-type template argument");
9028 return error_mark_node;
9029 }
9030
9031 /* If the argument wasn't successfully parsed as a type-id followed
9032 by '>>', the argument can only be a constant expression now.
9033 Otherwise, we try parsing the constant-expression tentatively,
9034 because the argument could really be a type-id. */
9035 if (maybe_type_id)
9036 cp_parser_parse_tentatively (parser);
9037 argument = cp_parser_constant_expression (parser,
9038 /*allow_non_constant_p=*/false,
9039 /*non_constant_p=*/NULL);
9040 argument = fold_non_dependent_expr (argument);
9041 if (!maybe_type_id)
9042 return argument;
9043 if (!cp_parser_next_token_ends_template_argument_p (parser))
9044 cp_parser_error (parser, "expected template-argument");
9045 if (cp_parser_parse_definitely (parser))
9046 return argument;
9047 /* We did our best to parse the argument as a non type-id, but that
9048 was the only alternative that matched (albeit with a '>' after
9049 it). We can assume it's just a typo from the user, and a
9050 diagnostic will then be issued. */
9051 return cp_parser_type_id (parser);
9052 }
9053
9054 /* Parse an explicit-instantiation.
9055
9056 explicit-instantiation:
9057 template declaration
9058
9059 Although the standard says `declaration', what it really means is:
9060
9061 explicit-instantiation:
9062 template decl-specifier-seq [opt] declarator [opt] ;
9063
9064 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9065 supposed to be allowed. A defect report has been filed about this
9066 issue.
9067
9068 GNU Extension:
9069
9070 explicit-instantiation:
9071 storage-class-specifier template
9072 decl-specifier-seq [opt] declarator [opt] ;
9073 function-specifier template
9074 decl-specifier-seq [opt] declarator [opt] ; */
9075
9076 static void
9077 cp_parser_explicit_instantiation (cp_parser* parser)
9078 {
9079 int declares_class_or_enum;
9080 cp_decl_specifier_seq decl_specifiers;
9081 tree extension_specifier = NULL_TREE;
9082
9083 /* Look for an (optional) storage-class-specifier or
9084 function-specifier. */
9085 if (cp_parser_allow_gnu_extensions_p (parser))
9086 {
9087 extension_specifier
9088 = cp_parser_storage_class_specifier_opt (parser);
9089 if (!extension_specifier)
9090 extension_specifier
9091 = cp_parser_function_specifier_opt (parser,
9092 /*decl_specs=*/NULL);
9093 }
9094
9095 /* Look for the `template' keyword. */
9096 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9097 /* Let the front end know that we are processing an explicit
9098 instantiation. */
9099 begin_explicit_instantiation ();
9100 /* [temp.explicit] says that we are supposed to ignore access
9101 control while processing explicit instantiation directives. */
9102 push_deferring_access_checks (dk_no_check);
9103 /* Parse a decl-specifier-seq. */
9104 cp_parser_decl_specifier_seq (parser,
9105 CP_PARSER_FLAGS_OPTIONAL,
9106 &decl_specifiers,
9107 &declares_class_or_enum);
9108 /* If there was exactly one decl-specifier, and it declared a class,
9109 and there's no declarator, then we have an explicit type
9110 instantiation. */
9111 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9112 {
9113 tree type;
9114
9115 type = check_tag_decl (&decl_specifiers);
9116 /* Turn access control back on for names used during
9117 template instantiation. */
9118 pop_deferring_access_checks ();
9119 if (type)
9120 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9121 }
9122 else
9123 {
9124 cp_declarator *declarator;
9125 tree decl;
9126
9127 /* Parse the declarator. */
9128 declarator
9129 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9130 /*ctor_dtor_or_conv_p=*/NULL,
9131 /*parenthesized_p=*/NULL,
9132 /*member_p=*/false);
9133 if (declares_class_or_enum & 2)
9134 cp_parser_check_for_definition_in_return_type (declarator,
9135 decl_specifiers.type);
9136 if (declarator != cp_error_declarator)
9137 {
9138 decl = grokdeclarator (declarator, &decl_specifiers,
9139 NORMAL, 0, NULL);
9140 /* Turn access control back on for names used during
9141 template instantiation. */
9142 pop_deferring_access_checks ();
9143 /* Do the explicit instantiation. */
9144 do_decl_instantiation (decl, extension_specifier);
9145 }
9146 else
9147 {
9148 pop_deferring_access_checks ();
9149 /* Skip the body of the explicit instantiation. */
9150 cp_parser_skip_to_end_of_statement (parser);
9151 }
9152 }
9153 /* We're done with the instantiation. */
9154 end_explicit_instantiation ();
9155
9156 cp_parser_consume_semicolon_at_end_of_statement (parser);
9157 }
9158
9159 /* Parse an explicit-specialization.
9160
9161 explicit-specialization:
9162 template < > declaration
9163
9164 Although the standard says `declaration', what it really means is:
9165
9166 explicit-specialization:
9167 template <> decl-specifier [opt] init-declarator [opt] ;
9168 template <> function-definition
9169 template <> explicit-specialization
9170 template <> template-declaration */
9171
9172 static void
9173 cp_parser_explicit_specialization (cp_parser* parser)
9174 {
9175 /* Look for the `template' keyword. */
9176 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9177 /* Look for the `<'. */
9178 cp_parser_require (parser, CPP_LESS, "`<'");
9179 /* Look for the `>'. */
9180 cp_parser_require (parser, CPP_GREATER, "`>'");
9181 /* We have processed another parameter list. */
9182 ++parser->num_template_parameter_lists;
9183 /* Let the front end know that we are beginning a specialization. */
9184 begin_specialization ();
9185
9186 /* If the next keyword is `template', we need to figure out whether
9187 or not we're looking a template-declaration. */
9188 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9189 {
9190 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9191 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9192 cp_parser_template_declaration_after_export (parser,
9193 /*member_p=*/false);
9194 else
9195 cp_parser_explicit_specialization (parser);
9196 }
9197 else
9198 /* Parse the dependent declaration. */
9199 cp_parser_single_declaration (parser,
9200 /*member_p=*/false,
9201 /*friend_p=*/NULL);
9202
9203 /* We're done with the specialization. */
9204 end_specialization ();
9205 /* We're done with this parameter list. */
9206 --parser->num_template_parameter_lists;
9207 }
9208
9209 /* Parse a type-specifier.
9210
9211 type-specifier:
9212 simple-type-specifier
9213 class-specifier
9214 enum-specifier
9215 elaborated-type-specifier
9216 cv-qualifier
9217
9218 GNU Extension:
9219
9220 type-specifier:
9221 __complex__
9222
9223 Returns a representation of the type-specifier. For a
9224 class-specifier, enum-specifier, or elaborated-type-specifier, a
9225 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9226
9227 The parser flags FLAGS is used to control type-specifier parsing.
9228
9229 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9230 in a decl-specifier-seq.
9231
9232 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9233 class-specifier, enum-specifier, or elaborated-type-specifier, then
9234 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9235 if a type is declared; 2 if it is defined. Otherwise, it is set to
9236 zero.
9237
9238 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9239 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9240 is set to FALSE. */
9241
9242 static tree
9243 cp_parser_type_specifier (cp_parser* parser,
9244 cp_parser_flags flags,
9245 cp_decl_specifier_seq *decl_specs,
9246 bool is_declaration,
9247 int* declares_class_or_enum,
9248 bool* is_cv_qualifier)
9249 {
9250 tree type_spec = NULL_TREE;
9251 cp_token *token;
9252 enum rid keyword;
9253 cp_decl_spec ds = ds_last;
9254
9255 /* Assume this type-specifier does not declare a new type. */
9256 if (declares_class_or_enum)
9257 *declares_class_or_enum = 0;
9258 /* And that it does not specify a cv-qualifier. */
9259 if (is_cv_qualifier)
9260 *is_cv_qualifier = false;
9261 /* Peek at the next token. */
9262 token = cp_lexer_peek_token (parser->lexer);
9263
9264 /* If we're looking at a keyword, we can use that to guide the
9265 production we choose. */
9266 keyword = token->keyword;
9267 switch (keyword)
9268 {
9269 case RID_ENUM:
9270 /* 'enum' [identifier] '{' introduces an enum-specifier;
9271 'enum' <anything else> introduces an elaborated-type-specifier. */
9272 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9273 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9274 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9275 == CPP_OPEN_BRACE))
9276 {
9277 if (parser->num_template_parameter_lists)
9278 {
9279 error ("template declaration of %qs", "enum");
9280 cp_parser_skip_to_end_of_block_or_statement (parser);
9281 type_spec = error_mark_node;
9282 }
9283 else
9284 type_spec = cp_parser_enum_specifier (parser);
9285
9286 if (declares_class_or_enum)
9287 *declares_class_or_enum = 2;
9288 if (decl_specs)
9289 cp_parser_set_decl_spec_type (decl_specs,
9290 type_spec,
9291 /*user_defined_p=*/true);
9292 return type_spec;
9293 }
9294 else
9295 goto elaborated_type_specifier;
9296
9297 /* Any of these indicate either a class-specifier, or an
9298 elaborated-type-specifier. */
9299 case RID_CLASS:
9300 case RID_STRUCT:
9301 case RID_UNION:
9302 /* Parse tentatively so that we can back up if we don't find a
9303 class-specifier. */
9304 cp_parser_parse_tentatively (parser);
9305 /* Look for the class-specifier. */
9306 type_spec = cp_parser_class_specifier (parser);
9307 /* If that worked, we're done. */
9308 if (cp_parser_parse_definitely (parser))
9309 {
9310 if (declares_class_or_enum)
9311 *declares_class_or_enum = 2;
9312 if (decl_specs)
9313 cp_parser_set_decl_spec_type (decl_specs,
9314 type_spec,
9315 /*user_defined_p=*/true);
9316 return type_spec;
9317 }
9318
9319 /* Fall through. */
9320 elaborated_type_specifier:
9321 /* We're declaring (not defining) a class or enum. */
9322 if (declares_class_or_enum)
9323 *declares_class_or_enum = 1;
9324
9325 /* Fall through. */
9326 case RID_TYPENAME:
9327 /* Look for an elaborated-type-specifier. */
9328 type_spec
9329 = (cp_parser_elaborated_type_specifier
9330 (parser,
9331 decl_specs && decl_specs->specs[(int) ds_friend],
9332 is_declaration));
9333 if (decl_specs)
9334 cp_parser_set_decl_spec_type (decl_specs,
9335 type_spec,
9336 /*user_defined_p=*/true);
9337 return type_spec;
9338
9339 case RID_CONST:
9340 ds = ds_const;
9341 if (is_cv_qualifier)
9342 *is_cv_qualifier = true;
9343 break;
9344
9345 case RID_VOLATILE:
9346 ds = ds_volatile;
9347 if (is_cv_qualifier)
9348 *is_cv_qualifier = true;
9349 break;
9350
9351 case RID_RESTRICT:
9352 ds = ds_restrict;
9353 if (is_cv_qualifier)
9354 *is_cv_qualifier = true;
9355 break;
9356
9357 case RID_COMPLEX:
9358 /* The `__complex__' keyword is a GNU extension. */
9359 ds = ds_complex;
9360 break;
9361
9362 default:
9363 break;
9364 }
9365
9366 /* Handle simple keywords. */
9367 if (ds != ds_last)
9368 {
9369 if (decl_specs)
9370 {
9371 ++decl_specs->specs[(int)ds];
9372 decl_specs->any_specifiers_p = true;
9373 }
9374 return cp_lexer_consume_token (parser->lexer)->value;
9375 }
9376
9377 /* If we do not already have a type-specifier, assume we are looking
9378 at a simple-type-specifier. */
9379 type_spec = cp_parser_simple_type_specifier (parser,
9380 decl_specs,
9381 flags);
9382
9383 /* If we didn't find a type-specifier, and a type-specifier was not
9384 optional in this context, issue an error message. */
9385 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9386 {
9387 cp_parser_error (parser, "expected type specifier");
9388 return error_mark_node;
9389 }
9390
9391 return type_spec;
9392 }
9393
9394 /* Parse a simple-type-specifier.
9395
9396 simple-type-specifier:
9397 :: [opt] nested-name-specifier [opt] type-name
9398 :: [opt] nested-name-specifier template template-id
9399 char
9400 wchar_t
9401 bool
9402 short
9403 int
9404 long
9405 signed
9406 unsigned
9407 float
9408 double
9409 void
9410
9411 GNU Extension:
9412
9413 simple-type-specifier:
9414 __typeof__ unary-expression
9415 __typeof__ ( type-id )
9416
9417 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9418 appropriately updated. */
9419
9420 static tree
9421 cp_parser_simple_type_specifier (cp_parser* parser,
9422 cp_decl_specifier_seq *decl_specs,
9423 cp_parser_flags flags)
9424 {
9425 tree type = NULL_TREE;
9426 cp_token *token;
9427
9428 /* Peek at the next token. */
9429 token = cp_lexer_peek_token (parser->lexer);
9430
9431 /* If we're looking at a keyword, things are easy. */
9432 switch (token->keyword)
9433 {
9434 case RID_CHAR:
9435 if (decl_specs)
9436 decl_specs->explicit_char_p = true;
9437 type = char_type_node;
9438 break;
9439 case RID_WCHAR:
9440 type = wchar_type_node;
9441 break;
9442 case RID_BOOL:
9443 type = boolean_type_node;
9444 break;
9445 case RID_SHORT:
9446 if (decl_specs)
9447 ++decl_specs->specs[(int) ds_short];
9448 type = short_integer_type_node;
9449 break;
9450 case RID_INT:
9451 if (decl_specs)
9452 decl_specs->explicit_int_p = true;
9453 type = integer_type_node;
9454 break;
9455 case RID_LONG:
9456 if (decl_specs)
9457 ++decl_specs->specs[(int) ds_long];
9458 type = long_integer_type_node;
9459 break;
9460 case RID_SIGNED:
9461 if (decl_specs)
9462 ++decl_specs->specs[(int) ds_signed];
9463 type = integer_type_node;
9464 break;
9465 case RID_UNSIGNED:
9466 if (decl_specs)
9467 ++decl_specs->specs[(int) ds_unsigned];
9468 type = unsigned_type_node;
9469 break;
9470 case RID_FLOAT:
9471 type = float_type_node;
9472 break;
9473 case RID_DOUBLE:
9474 type = double_type_node;
9475 break;
9476 case RID_VOID:
9477 type = void_type_node;
9478 break;
9479
9480 case RID_TYPEOF:
9481 /* Consume the `typeof' token. */
9482 cp_lexer_consume_token (parser->lexer);
9483 /* Parse the operand to `typeof'. */
9484 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9485 /* If it is not already a TYPE, take its type. */
9486 if (!TYPE_P (type))
9487 type = finish_typeof (type);
9488
9489 if (decl_specs)
9490 cp_parser_set_decl_spec_type (decl_specs, type,
9491 /*user_defined_p=*/true);
9492
9493 return type;
9494
9495 default:
9496 break;
9497 }
9498
9499 /* If the type-specifier was for a built-in type, we're done. */
9500 if (type)
9501 {
9502 tree id;
9503
9504 /* Record the type. */
9505 if (decl_specs
9506 && (token->keyword != RID_SIGNED
9507 && token->keyword != RID_UNSIGNED
9508 && token->keyword != RID_SHORT
9509 && token->keyword != RID_LONG))
9510 cp_parser_set_decl_spec_type (decl_specs,
9511 type,
9512 /*user_defined=*/false);
9513 if (decl_specs)
9514 decl_specs->any_specifiers_p = true;
9515
9516 /* Consume the token. */
9517 id = cp_lexer_consume_token (parser->lexer)->value;
9518
9519 /* There is no valid C++ program where a non-template type is
9520 followed by a "<". That usually indicates that the user thought
9521 that the type was a template. */
9522 cp_parser_check_for_invalid_template_id (parser, type);
9523
9524 return TYPE_NAME (type);
9525 }
9526
9527 /* The type-specifier must be a user-defined type. */
9528 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9529 {
9530 bool qualified_p;
9531 bool global_p;
9532
9533 /* Don't gobble tokens or issue error messages if this is an
9534 optional type-specifier. */
9535 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9536 cp_parser_parse_tentatively (parser);
9537
9538 /* Look for the optional `::' operator. */
9539 global_p
9540 = (cp_parser_global_scope_opt (parser,
9541 /*current_scope_valid_p=*/false)
9542 != NULL_TREE);
9543 /* Look for the nested-name specifier. */
9544 qualified_p
9545 = (cp_parser_nested_name_specifier_opt (parser,
9546 /*typename_keyword_p=*/false,
9547 /*check_dependency_p=*/true,
9548 /*type_p=*/false,
9549 /*is_declaration=*/false)
9550 != NULL_TREE);
9551 /* If we have seen a nested-name-specifier, and the next token
9552 is `template', then we are using the template-id production. */
9553 if (parser->scope
9554 && cp_parser_optional_template_keyword (parser))
9555 {
9556 /* Look for the template-id. */
9557 type = cp_parser_template_id (parser,
9558 /*template_keyword_p=*/true,
9559 /*check_dependency_p=*/true,
9560 /*is_declaration=*/false);
9561 /* If the template-id did not name a type, we are out of
9562 luck. */
9563 if (TREE_CODE (type) != TYPE_DECL)
9564 {
9565 cp_parser_error (parser, "expected template-id for type");
9566 type = NULL_TREE;
9567 }
9568 }
9569 /* Otherwise, look for a type-name. */
9570 else
9571 type = cp_parser_type_name (parser);
9572 /* Keep track of all name-lookups performed in class scopes. */
9573 if (type
9574 && !global_p
9575 && !qualified_p
9576 && TREE_CODE (type) == TYPE_DECL
9577 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9578 maybe_note_name_used_in_class (DECL_NAME (type), type);
9579 /* If it didn't work out, we don't have a TYPE. */
9580 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9581 && !cp_parser_parse_definitely (parser))
9582 type = NULL_TREE;
9583 if (type && decl_specs)
9584 cp_parser_set_decl_spec_type (decl_specs, type,
9585 /*user_defined=*/true);
9586 }
9587
9588 /* If we didn't get a type-name, issue an error message. */
9589 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9590 {
9591 cp_parser_error (parser, "expected type-name");
9592 return error_mark_node;
9593 }
9594
9595 /* There is no valid C++ program where a non-template type is
9596 followed by a "<". That usually indicates that the user thought
9597 that the type was a template. */
9598 if (type && type != error_mark_node)
9599 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9600
9601 return type;
9602 }
9603
9604 /* Parse a type-name.
9605
9606 type-name:
9607 class-name
9608 enum-name
9609 typedef-name
9610
9611 enum-name:
9612 identifier
9613
9614 typedef-name:
9615 identifier
9616
9617 Returns a TYPE_DECL for the type. */
9618
9619 static tree
9620 cp_parser_type_name (cp_parser* parser)
9621 {
9622 tree type_decl;
9623 tree identifier;
9624
9625 /* We can't know yet whether it is a class-name or not. */
9626 cp_parser_parse_tentatively (parser);
9627 /* Try a class-name. */
9628 type_decl = cp_parser_class_name (parser,
9629 /*typename_keyword_p=*/false,
9630 /*template_keyword_p=*/false,
9631 none_type,
9632 /*check_dependency_p=*/true,
9633 /*class_head_p=*/false,
9634 /*is_declaration=*/false);
9635 /* If it's not a class-name, keep looking. */
9636 if (!cp_parser_parse_definitely (parser))
9637 {
9638 /* It must be a typedef-name or an enum-name. */
9639 identifier = cp_parser_identifier (parser);
9640 if (identifier == error_mark_node)
9641 return error_mark_node;
9642
9643 /* Look up the type-name. */
9644 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9645 /* Issue an error if we did not find a type-name. */
9646 if (TREE_CODE (type_decl) != TYPE_DECL)
9647 {
9648 if (!cp_parser_simulate_error (parser))
9649 cp_parser_name_lookup_error (parser, identifier, type_decl,
9650 "is not a type");
9651 type_decl = error_mark_node;
9652 }
9653 /* Remember that the name was used in the definition of the
9654 current class so that we can check later to see if the
9655 meaning would have been different after the class was
9656 entirely defined. */
9657 else if (type_decl != error_mark_node
9658 && !parser->scope)
9659 maybe_note_name_used_in_class (identifier, type_decl);
9660 }
9661
9662 return type_decl;
9663 }
9664
9665
9666 /* Parse an elaborated-type-specifier. Note that the grammar given
9667 here incorporates the resolution to DR68.
9668
9669 elaborated-type-specifier:
9670 class-key :: [opt] nested-name-specifier [opt] identifier
9671 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9672 enum :: [opt] nested-name-specifier [opt] identifier
9673 typename :: [opt] nested-name-specifier identifier
9674 typename :: [opt] nested-name-specifier template [opt]
9675 template-id
9676
9677 GNU extension:
9678
9679 elaborated-type-specifier:
9680 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9681 class-key attributes :: [opt] nested-name-specifier [opt]
9682 template [opt] template-id
9683 enum attributes :: [opt] nested-name-specifier [opt] identifier
9684
9685 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9686 declared `friend'. If IS_DECLARATION is TRUE, then this
9687 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9688 something is being declared.
9689
9690 Returns the TYPE specified. */
9691
9692 static tree
9693 cp_parser_elaborated_type_specifier (cp_parser* parser,
9694 bool is_friend,
9695 bool is_declaration)
9696 {
9697 enum tag_types tag_type;
9698 tree identifier;
9699 tree type = NULL_TREE;
9700 tree attributes = NULL_TREE;
9701
9702 /* See if we're looking at the `enum' keyword. */
9703 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9704 {
9705 /* Consume the `enum' token. */
9706 cp_lexer_consume_token (parser->lexer);
9707 /* Remember that it's an enumeration type. */
9708 tag_type = enum_type;
9709 /* Parse the attributes. */
9710 attributes = cp_parser_attributes_opt (parser);
9711 }
9712 /* Or, it might be `typename'. */
9713 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9714 RID_TYPENAME))
9715 {
9716 /* Consume the `typename' token. */
9717 cp_lexer_consume_token (parser->lexer);
9718 /* Remember that it's a `typename' type. */
9719 tag_type = typename_type;
9720 /* The `typename' keyword is only allowed in templates. */
9721 if (!processing_template_decl)
9722 pedwarn ("using %<typename%> outside of template");
9723 }
9724 /* Otherwise it must be a class-key. */
9725 else
9726 {
9727 tag_type = cp_parser_class_key (parser);
9728 if (tag_type == none_type)
9729 return error_mark_node;
9730 /* Parse the attributes. */
9731 attributes = cp_parser_attributes_opt (parser);
9732 }
9733
9734 /* Look for the `::' operator. */
9735 cp_parser_global_scope_opt (parser,
9736 /*current_scope_valid_p=*/false);
9737 /* Look for the nested-name-specifier. */
9738 if (tag_type == typename_type)
9739 {
9740 if (cp_parser_nested_name_specifier (parser,
9741 /*typename_keyword_p=*/true,
9742 /*check_dependency_p=*/true,
9743 /*type_p=*/true,
9744 is_declaration)
9745 == error_mark_node)
9746 return error_mark_node;
9747 }
9748 else
9749 /* Even though `typename' is not present, the proposed resolution
9750 to Core Issue 180 says that in `class A<T>::B', `B' should be
9751 considered a type-name, even if `A<T>' is dependent. */
9752 cp_parser_nested_name_specifier_opt (parser,
9753 /*typename_keyword_p=*/true,
9754 /*check_dependency_p=*/true,
9755 /*type_p=*/true,
9756 is_declaration);
9757 /* For everything but enumeration types, consider a template-id. */
9758 if (tag_type != enum_type)
9759 {
9760 bool template_p = false;
9761 tree decl;
9762
9763 /* Allow the `template' keyword. */
9764 template_p = cp_parser_optional_template_keyword (parser);
9765 /* If we didn't see `template', we don't know if there's a
9766 template-id or not. */
9767 if (!template_p)
9768 cp_parser_parse_tentatively (parser);
9769 /* Parse the template-id. */
9770 decl = cp_parser_template_id (parser, template_p,
9771 /*check_dependency_p=*/true,
9772 is_declaration);
9773 /* If we didn't find a template-id, look for an ordinary
9774 identifier. */
9775 if (!template_p && !cp_parser_parse_definitely (parser))
9776 ;
9777 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9778 in effect, then we must assume that, upon instantiation, the
9779 template will correspond to a class. */
9780 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9781 && tag_type == typename_type)
9782 type = make_typename_type (parser->scope, decl,
9783 typename_type,
9784 /*complain=*/1);
9785 else
9786 type = TREE_TYPE (decl);
9787 }
9788
9789 /* For an enumeration type, consider only a plain identifier. */
9790 if (!type)
9791 {
9792 identifier = cp_parser_identifier (parser);
9793
9794 if (identifier == error_mark_node)
9795 {
9796 parser->scope = NULL_TREE;
9797 return error_mark_node;
9798 }
9799
9800 /* For a `typename', we needn't call xref_tag. */
9801 if (tag_type == typename_type
9802 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9803 return cp_parser_make_typename_type (parser, parser->scope,
9804 identifier);
9805 /* Look up a qualified name in the usual way. */
9806 if (parser->scope)
9807 {
9808 tree decl;
9809
9810 decl = cp_parser_lookup_name (parser, identifier,
9811 tag_type,
9812 /*is_template=*/false,
9813 /*is_namespace=*/false,
9814 /*check_dependency=*/true,
9815 /*ambiguous_p=*/NULL);
9816
9817 /* If we are parsing friend declaration, DECL may be a
9818 TEMPLATE_DECL tree node here. However, we need to check
9819 whether this TEMPLATE_DECL results in valid code. Consider
9820 the following example:
9821
9822 namespace N {
9823 template <class T> class C {};
9824 }
9825 class X {
9826 template <class T> friend class N::C; // #1, valid code
9827 };
9828 template <class T> class Y {
9829 friend class N::C; // #2, invalid code
9830 };
9831
9832 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9833 name lookup of `N::C'. We see that friend declaration must
9834 be template for the code to be valid. Note that
9835 processing_template_decl does not work here since it is
9836 always 1 for the above two cases. */
9837
9838 decl = (cp_parser_maybe_treat_template_as_class
9839 (decl, /*tag_name_p=*/is_friend
9840 && parser->num_template_parameter_lists));
9841
9842 if (TREE_CODE (decl) != TYPE_DECL)
9843 {
9844 cp_parser_diagnose_invalid_type_name (parser,
9845 parser->scope,
9846 identifier);
9847 return error_mark_node;
9848 }
9849
9850 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9851 check_elaborated_type_specifier
9852 (tag_type, decl,
9853 (parser->num_template_parameter_lists
9854 || DECL_SELF_REFERENCE_P (decl)));
9855
9856 type = TREE_TYPE (decl);
9857 }
9858 else
9859 {
9860 /* An elaborated-type-specifier sometimes introduces a new type and
9861 sometimes names an existing type. Normally, the rule is that it
9862 introduces a new type only if there is not an existing type of
9863 the same name already in scope. For example, given:
9864
9865 struct S {};
9866 void f() { struct S s; }
9867
9868 the `struct S' in the body of `f' is the same `struct S' as in
9869 the global scope; the existing definition is used. However, if
9870 there were no global declaration, this would introduce a new
9871 local class named `S'.
9872
9873 An exception to this rule applies to the following code:
9874
9875 namespace N { struct S; }
9876
9877 Here, the elaborated-type-specifier names a new type
9878 unconditionally; even if there is already an `S' in the
9879 containing scope this declaration names a new type.
9880 This exception only applies if the elaborated-type-specifier
9881 forms the complete declaration:
9882
9883 [class.name]
9884
9885 A declaration consisting solely of `class-key identifier ;' is
9886 either a redeclaration of the name in the current scope or a
9887 forward declaration of the identifier as a class name. It
9888 introduces the name into the current scope.
9889
9890 We are in this situation precisely when the next token is a `;'.
9891
9892 An exception to the exception is that a `friend' declaration does
9893 *not* name a new type; i.e., given:
9894
9895 struct S { friend struct T; };
9896
9897 `T' is not a new type in the scope of `S'.
9898
9899 Also, `new struct S' or `sizeof (struct S)' never results in the
9900 definition of a new type; a new type can only be declared in a
9901 declaration context. */
9902
9903 tag_scope ts;
9904 if (is_friend)
9905 /* Friends have special name lookup rules. */
9906 ts = ts_within_enclosing_non_class;
9907 else if (is_declaration
9908 && cp_lexer_next_token_is (parser->lexer,
9909 CPP_SEMICOLON))
9910 /* This is a `class-key identifier ;' */
9911 ts = ts_current;
9912 else
9913 ts = ts_global;
9914
9915 /* Warn about attributes. They are ignored. */
9916 if (attributes)
9917 warning (0, "type attributes are honored only at type definition");
9918
9919 type = xref_tag (tag_type, identifier, ts,
9920 parser->num_template_parameter_lists);
9921 }
9922 }
9923 if (tag_type != enum_type)
9924 cp_parser_check_class_key (tag_type, type);
9925
9926 /* A "<" cannot follow an elaborated type specifier. If that
9927 happens, the user was probably trying to form a template-id. */
9928 cp_parser_check_for_invalid_template_id (parser, type);
9929
9930 return type;
9931 }
9932
9933 /* Parse an enum-specifier.
9934
9935 enum-specifier:
9936 enum identifier [opt] { enumerator-list [opt] }
9937
9938 GNU Extensions:
9939 enum identifier [opt] { enumerator-list [opt] } attributes
9940
9941 Returns an ENUM_TYPE representing the enumeration. */
9942
9943 static tree
9944 cp_parser_enum_specifier (cp_parser* parser)
9945 {
9946 tree identifier;
9947 tree type;
9948
9949 /* Caller guarantees that the current token is 'enum', an identifier
9950 possibly follows, and the token after that is an opening brace.
9951 If we don't have an identifier, fabricate an anonymous name for
9952 the enumeration being defined. */
9953 cp_lexer_consume_token (parser->lexer);
9954
9955 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9956 identifier = cp_parser_identifier (parser);
9957 else
9958 identifier = make_anon_name ();
9959
9960 /* Issue an error message if type-definitions are forbidden here. */
9961 cp_parser_check_type_definition (parser);
9962
9963 /* Create the new type. We do this before consuming the opening brace
9964 so the enum will be recorded as being on the line of its tag (or the
9965 'enum' keyword, if there is no tag). */
9966 type = start_enum (identifier);
9967
9968 /* Consume the opening brace. */
9969 cp_lexer_consume_token (parser->lexer);
9970
9971 /* If the next token is not '}', then there are some enumerators. */
9972 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9973 cp_parser_enumerator_list (parser, type);
9974
9975 /* Consume the final '}'. */
9976 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9977
9978 /* Look for trailing attributes to apply to this enumeration, and
9979 apply them if appropriate. */
9980 if (cp_parser_allow_gnu_extensions_p (parser))
9981 {
9982 tree trailing_attr = cp_parser_attributes_opt (parser);
9983 cplus_decl_attributes (&type,
9984 trailing_attr,
9985 (int) ATTR_FLAG_TYPE_IN_PLACE);
9986 }
9987
9988 /* Finish up the enumeration. */
9989 finish_enum (type);
9990
9991 return type;
9992 }
9993
9994 /* Parse an enumerator-list. The enumerators all have the indicated
9995 TYPE.
9996
9997 enumerator-list:
9998 enumerator-definition
9999 enumerator-list , enumerator-definition */
10000
10001 static void
10002 cp_parser_enumerator_list (cp_parser* parser, tree type)
10003 {
10004 while (true)
10005 {
10006 /* Parse an enumerator-definition. */
10007 cp_parser_enumerator_definition (parser, type);
10008
10009 /* If the next token is not a ',', we've reached the end of
10010 the list. */
10011 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10012 break;
10013 /* Otherwise, consume the `,' and keep going. */
10014 cp_lexer_consume_token (parser->lexer);
10015 /* If the next token is a `}', there is a trailing comma. */
10016 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10017 {
10018 if (pedantic && !in_system_header)
10019 pedwarn ("comma at end of enumerator list");
10020 break;
10021 }
10022 }
10023 }
10024
10025 /* Parse an enumerator-definition. The enumerator has the indicated
10026 TYPE.
10027
10028 enumerator-definition:
10029 enumerator
10030 enumerator = constant-expression
10031
10032 enumerator:
10033 identifier */
10034
10035 static void
10036 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10037 {
10038 tree identifier;
10039 tree value;
10040
10041 /* Look for the identifier. */
10042 identifier = cp_parser_identifier (parser);
10043 if (identifier == error_mark_node)
10044 return;
10045
10046 /* If the next token is an '=', then there is an explicit value. */
10047 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10048 {
10049 /* Consume the `=' token. */
10050 cp_lexer_consume_token (parser->lexer);
10051 /* Parse the value. */
10052 value = cp_parser_constant_expression (parser,
10053 /*allow_non_constant_p=*/false,
10054 NULL);
10055 }
10056 else
10057 value = NULL_TREE;
10058
10059 /* Create the enumerator. */
10060 build_enumerator (identifier, value, type);
10061 }
10062
10063 /* Parse a namespace-name.
10064
10065 namespace-name:
10066 original-namespace-name
10067 namespace-alias
10068
10069 Returns the NAMESPACE_DECL for the namespace. */
10070
10071 static tree
10072 cp_parser_namespace_name (cp_parser* parser)
10073 {
10074 tree identifier;
10075 tree namespace_decl;
10076
10077 /* Get the name of the namespace. */
10078 identifier = cp_parser_identifier (parser);
10079 if (identifier == error_mark_node)
10080 return error_mark_node;
10081
10082 /* Look up the identifier in the currently active scope. Look only
10083 for namespaces, due to:
10084
10085 [basic.lookup.udir]
10086
10087 When looking up a namespace-name in a using-directive or alias
10088 definition, only namespace names are considered.
10089
10090 And:
10091
10092 [basic.lookup.qual]
10093
10094 During the lookup of a name preceding the :: scope resolution
10095 operator, object, function, and enumerator names are ignored.
10096
10097 (Note that cp_parser_class_or_namespace_name only calls this
10098 function if the token after the name is the scope resolution
10099 operator.) */
10100 namespace_decl = cp_parser_lookup_name (parser, identifier,
10101 none_type,
10102 /*is_template=*/false,
10103 /*is_namespace=*/true,
10104 /*check_dependency=*/true,
10105 /*ambiguous_p=*/NULL);
10106 /* If it's not a namespace, issue an error. */
10107 if (namespace_decl == error_mark_node
10108 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10109 {
10110 cp_parser_error (parser, "expected namespace-name");
10111 namespace_decl = error_mark_node;
10112 }
10113
10114 return namespace_decl;
10115 }
10116
10117 /* Parse a namespace-definition.
10118
10119 namespace-definition:
10120 named-namespace-definition
10121 unnamed-namespace-definition
10122
10123 named-namespace-definition:
10124 original-namespace-definition
10125 extension-namespace-definition
10126
10127 original-namespace-definition:
10128 namespace identifier { namespace-body }
10129
10130 extension-namespace-definition:
10131 namespace original-namespace-name { namespace-body }
10132
10133 unnamed-namespace-definition:
10134 namespace { namespace-body } */
10135
10136 static void
10137 cp_parser_namespace_definition (cp_parser* parser)
10138 {
10139 tree identifier;
10140
10141 /* Look for the `namespace' keyword. */
10142 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10143
10144 /* Get the name of the namespace. We do not attempt to distinguish
10145 between an original-namespace-definition and an
10146 extension-namespace-definition at this point. The semantic
10147 analysis routines are responsible for that. */
10148 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10149 identifier = cp_parser_identifier (parser);
10150 else
10151 identifier = NULL_TREE;
10152
10153 /* Look for the `{' to start the namespace. */
10154 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10155 /* Start the namespace. */
10156 push_namespace (identifier);
10157 /* Parse the body of the namespace. */
10158 cp_parser_namespace_body (parser);
10159 /* Finish the namespace. */
10160 pop_namespace ();
10161 /* Look for the final `}'. */
10162 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10163 }
10164
10165 /* Parse a namespace-body.
10166
10167 namespace-body:
10168 declaration-seq [opt] */
10169
10170 static void
10171 cp_parser_namespace_body (cp_parser* parser)
10172 {
10173 cp_parser_declaration_seq_opt (parser);
10174 }
10175
10176 /* Parse a namespace-alias-definition.
10177
10178 namespace-alias-definition:
10179 namespace identifier = qualified-namespace-specifier ; */
10180
10181 static void
10182 cp_parser_namespace_alias_definition (cp_parser* parser)
10183 {
10184 tree identifier;
10185 tree namespace_specifier;
10186
10187 /* Look for the `namespace' keyword. */
10188 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10189 /* Look for the identifier. */
10190 identifier = cp_parser_identifier (parser);
10191 if (identifier == error_mark_node)
10192 return;
10193 /* Look for the `=' token. */
10194 cp_parser_require (parser, CPP_EQ, "`='");
10195 /* Look for the qualified-namespace-specifier. */
10196 namespace_specifier
10197 = cp_parser_qualified_namespace_specifier (parser);
10198 /* Look for the `;' token. */
10199 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10200
10201 /* Register the alias in the symbol table. */
10202 do_namespace_alias (identifier, namespace_specifier);
10203 }
10204
10205 /* Parse a qualified-namespace-specifier.
10206
10207 qualified-namespace-specifier:
10208 :: [opt] nested-name-specifier [opt] namespace-name
10209
10210 Returns a NAMESPACE_DECL corresponding to the specified
10211 namespace. */
10212
10213 static tree
10214 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10215 {
10216 /* Look for the optional `::'. */
10217 cp_parser_global_scope_opt (parser,
10218 /*current_scope_valid_p=*/false);
10219
10220 /* Look for the optional nested-name-specifier. */
10221 cp_parser_nested_name_specifier_opt (parser,
10222 /*typename_keyword_p=*/false,
10223 /*check_dependency_p=*/true,
10224 /*type_p=*/false,
10225 /*is_declaration=*/true);
10226
10227 return cp_parser_namespace_name (parser);
10228 }
10229
10230 /* Parse a using-declaration.
10231
10232 using-declaration:
10233 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10234 using :: unqualified-id ; */
10235
10236 static void
10237 cp_parser_using_declaration (cp_parser* parser)
10238 {
10239 cp_token *token;
10240 bool typename_p = false;
10241 bool global_scope_p;
10242 tree decl;
10243 tree identifier;
10244 tree qscope;
10245
10246 /* Look for the `using' keyword. */
10247 cp_parser_require_keyword (parser, RID_USING, "`using'");
10248
10249 /* Peek at the next token. */
10250 token = cp_lexer_peek_token (parser->lexer);
10251 /* See if it's `typename'. */
10252 if (token->keyword == RID_TYPENAME)
10253 {
10254 /* Remember that we've seen it. */
10255 typename_p = true;
10256 /* Consume the `typename' token. */
10257 cp_lexer_consume_token (parser->lexer);
10258 }
10259
10260 /* Look for the optional global scope qualification. */
10261 global_scope_p
10262 = (cp_parser_global_scope_opt (parser,
10263 /*current_scope_valid_p=*/false)
10264 != NULL_TREE);
10265
10266 /* If we saw `typename', or didn't see `::', then there must be a
10267 nested-name-specifier present. */
10268 if (typename_p || !global_scope_p)
10269 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10270 /*check_dependency_p=*/true,
10271 /*type_p=*/false,
10272 /*is_declaration=*/true);
10273 /* Otherwise, we could be in either of the two productions. In that
10274 case, treat the nested-name-specifier as optional. */
10275 else
10276 qscope = cp_parser_nested_name_specifier_opt (parser,
10277 /*typename_keyword_p=*/false,
10278 /*check_dependency_p=*/true,
10279 /*type_p=*/false,
10280 /*is_declaration=*/true);
10281 if (!qscope)
10282 qscope = global_namespace;
10283
10284 /* Parse the unqualified-id. */
10285 identifier = cp_parser_unqualified_id (parser,
10286 /*template_keyword_p=*/false,
10287 /*check_dependency_p=*/true,
10288 /*declarator_p=*/true);
10289
10290 /* The function we call to handle a using-declaration is different
10291 depending on what scope we are in. */
10292 if (identifier == error_mark_node)
10293 ;
10294 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10295 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10296 /* [namespace.udecl]
10297
10298 A using declaration shall not name a template-id. */
10299 error ("a template-id may not appear in a using-declaration");
10300 else
10301 {
10302 if (at_class_scope_p ())
10303 {
10304 /* Create the USING_DECL. */
10305 decl = do_class_using_decl (parser->scope, identifier);
10306 /* Add it to the list of members in this class. */
10307 finish_member_declaration (decl);
10308 }
10309 else
10310 {
10311 decl = cp_parser_lookup_name_simple (parser, identifier);
10312 if (decl == error_mark_node)
10313 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10314 else if (!at_namespace_scope_p ())
10315 do_local_using_decl (decl, qscope, identifier);
10316 else
10317 do_toplevel_using_decl (decl, qscope, identifier);
10318 }
10319 }
10320
10321 /* Look for the final `;'. */
10322 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10323 }
10324
10325 /* Parse a using-directive.
10326
10327 using-directive:
10328 using namespace :: [opt] nested-name-specifier [opt]
10329 namespace-name ; */
10330
10331 static void
10332 cp_parser_using_directive (cp_parser* parser)
10333 {
10334 tree namespace_decl;
10335 tree attribs;
10336
10337 /* Look for the `using' keyword. */
10338 cp_parser_require_keyword (parser, RID_USING, "`using'");
10339 /* And the `namespace' keyword. */
10340 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10341 /* Look for the optional `::' operator. */
10342 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10343 /* And the optional nested-name-specifier. */
10344 cp_parser_nested_name_specifier_opt (parser,
10345 /*typename_keyword_p=*/false,
10346 /*check_dependency_p=*/true,
10347 /*type_p=*/false,
10348 /*is_declaration=*/true);
10349 /* Get the namespace being used. */
10350 namespace_decl = cp_parser_namespace_name (parser);
10351 /* And any specified attributes. */
10352 attribs = cp_parser_attributes_opt (parser);
10353 /* Update the symbol table. */
10354 parse_using_directive (namespace_decl, attribs);
10355 /* Look for the final `;'. */
10356 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10357 }
10358
10359 /* Parse an asm-definition.
10360
10361 asm-definition:
10362 asm ( string-literal ) ;
10363
10364 GNU Extension:
10365
10366 asm-definition:
10367 asm volatile [opt] ( string-literal ) ;
10368 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10369 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10370 : asm-operand-list [opt] ) ;
10371 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10372 : asm-operand-list [opt]
10373 : asm-operand-list [opt] ) ; */
10374
10375 static void
10376 cp_parser_asm_definition (cp_parser* parser)
10377 {
10378 tree string;
10379 tree outputs = NULL_TREE;
10380 tree inputs = NULL_TREE;
10381 tree clobbers = NULL_TREE;
10382 tree asm_stmt;
10383 bool volatile_p = false;
10384 bool extended_p = false;
10385
10386 /* Look for the `asm' keyword. */
10387 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10388 /* See if the next token is `volatile'. */
10389 if (cp_parser_allow_gnu_extensions_p (parser)
10390 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10391 {
10392 /* Remember that we saw the `volatile' keyword. */
10393 volatile_p = true;
10394 /* Consume the token. */
10395 cp_lexer_consume_token (parser->lexer);
10396 }
10397 /* Look for the opening `('. */
10398 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10399 return;
10400 /* Look for the string. */
10401 string = cp_parser_string_literal (parser, false, false);
10402 if (string == error_mark_node)
10403 {
10404 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10405 /*consume_paren=*/true);
10406 return;
10407 }
10408
10409 /* If we're allowing GNU extensions, check for the extended assembly
10410 syntax. Unfortunately, the `:' tokens need not be separated by
10411 a space in C, and so, for compatibility, we tolerate that here
10412 too. Doing that means that we have to treat the `::' operator as
10413 two `:' tokens. */
10414 if (cp_parser_allow_gnu_extensions_p (parser)
10415 && at_function_scope_p ()
10416 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10417 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10418 {
10419 bool inputs_p = false;
10420 bool clobbers_p = false;
10421
10422 /* The extended syntax was used. */
10423 extended_p = true;
10424
10425 /* Look for outputs. */
10426 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10427 {
10428 /* Consume the `:'. */
10429 cp_lexer_consume_token (parser->lexer);
10430 /* Parse the output-operands. */
10431 if (cp_lexer_next_token_is_not (parser->lexer,
10432 CPP_COLON)
10433 && cp_lexer_next_token_is_not (parser->lexer,
10434 CPP_SCOPE)
10435 && cp_lexer_next_token_is_not (parser->lexer,
10436 CPP_CLOSE_PAREN))
10437 outputs = cp_parser_asm_operand_list (parser);
10438 }
10439 /* If the next token is `::', there are no outputs, and the
10440 next token is the beginning of the inputs. */
10441 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10442 /* The inputs are coming next. */
10443 inputs_p = true;
10444
10445 /* Look for inputs. */
10446 if (inputs_p
10447 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10448 {
10449 /* Consume the `:' or `::'. */
10450 cp_lexer_consume_token (parser->lexer);
10451 /* Parse the output-operands. */
10452 if (cp_lexer_next_token_is_not (parser->lexer,
10453 CPP_COLON)
10454 && cp_lexer_next_token_is_not (parser->lexer,
10455 CPP_CLOSE_PAREN))
10456 inputs = cp_parser_asm_operand_list (parser);
10457 }
10458 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10459 /* The clobbers are coming next. */
10460 clobbers_p = true;
10461
10462 /* Look for clobbers. */
10463 if (clobbers_p
10464 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10465 {
10466 /* Consume the `:' or `::'. */
10467 cp_lexer_consume_token (parser->lexer);
10468 /* Parse the clobbers. */
10469 if (cp_lexer_next_token_is_not (parser->lexer,
10470 CPP_CLOSE_PAREN))
10471 clobbers = cp_parser_asm_clobber_list (parser);
10472 }
10473 }
10474 /* Look for the closing `)'. */
10475 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10476 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10477 /*consume_paren=*/true);
10478 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10479
10480 /* Create the ASM_EXPR. */
10481 if (at_function_scope_p ())
10482 {
10483 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10484 inputs, clobbers);
10485 /* If the extended syntax was not used, mark the ASM_EXPR. */
10486 if (!extended_p)
10487 {
10488 tree temp = asm_stmt;
10489 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10490 temp = TREE_OPERAND (temp, 0);
10491
10492 ASM_INPUT_P (temp) = 1;
10493 }
10494 }
10495 else
10496 assemble_asm (string);
10497 }
10498
10499 /* Declarators [gram.dcl.decl] */
10500
10501 /* Parse an init-declarator.
10502
10503 init-declarator:
10504 declarator initializer [opt]
10505
10506 GNU Extension:
10507
10508 init-declarator:
10509 declarator asm-specification [opt] attributes [opt] initializer [opt]
10510
10511 function-definition:
10512 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10513 function-body
10514 decl-specifier-seq [opt] declarator function-try-block
10515
10516 GNU Extension:
10517
10518 function-definition:
10519 __extension__ function-definition
10520
10521 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10522 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10523 then this declarator appears in a class scope. The new DECL created
10524 by this declarator is returned.
10525
10526 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10527 for a function-definition here as well. If the declarator is a
10528 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10529 be TRUE upon return. By that point, the function-definition will
10530 have been completely parsed.
10531
10532 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10533 is FALSE. */
10534
10535 static tree
10536 cp_parser_init_declarator (cp_parser* parser,
10537 cp_decl_specifier_seq *decl_specifiers,
10538 bool function_definition_allowed_p,
10539 bool member_p,
10540 int declares_class_or_enum,
10541 bool* function_definition_p)
10542 {
10543 cp_token *token;
10544 cp_declarator *declarator;
10545 tree prefix_attributes;
10546 tree attributes;
10547 tree asm_specification;
10548 tree initializer;
10549 tree decl = NULL_TREE;
10550 tree scope;
10551 bool is_initialized;
10552 bool is_parenthesized_init;
10553 bool is_non_constant_init;
10554 int ctor_dtor_or_conv_p;
10555 bool friend_p;
10556 tree pushed_scope = NULL;
10557
10558 /* Gather the attributes that were provided with the
10559 decl-specifiers. */
10560 prefix_attributes = decl_specifiers->attributes;
10561
10562 /* Assume that this is not the declarator for a function
10563 definition. */
10564 if (function_definition_p)
10565 *function_definition_p = false;
10566
10567 /* Defer access checks while parsing the declarator; we cannot know
10568 what names are accessible until we know what is being
10569 declared. */
10570 resume_deferring_access_checks ();
10571
10572 /* Parse the declarator. */
10573 declarator
10574 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10575 &ctor_dtor_or_conv_p,
10576 /*parenthesized_p=*/NULL,
10577 /*member_p=*/false);
10578 /* Gather up the deferred checks. */
10579 stop_deferring_access_checks ();
10580
10581 /* If the DECLARATOR was erroneous, there's no need to go
10582 further. */
10583 if (declarator == cp_error_declarator)
10584 return error_mark_node;
10585
10586 if (declares_class_or_enum & 2)
10587 cp_parser_check_for_definition_in_return_type (declarator,
10588 decl_specifiers->type);
10589
10590 /* Figure out what scope the entity declared by the DECLARATOR is
10591 located in. `grokdeclarator' sometimes changes the scope, so
10592 we compute it now. */
10593 scope = get_scope_of_declarator (declarator);
10594
10595 /* If we're allowing GNU extensions, look for an asm-specification
10596 and attributes. */
10597 if (cp_parser_allow_gnu_extensions_p (parser))
10598 {
10599 /* Look for an asm-specification. */
10600 asm_specification = cp_parser_asm_specification_opt (parser);
10601 /* And attributes. */
10602 attributes = cp_parser_attributes_opt (parser);
10603 }
10604 else
10605 {
10606 asm_specification = NULL_TREE;
10607 attributes = NULL_TREE;
10608 }
10609
10610 /* Peek at the next token. */
10611 token = cp_lexer_peek_token (parser->lexer);
10612 /* Check to see if the token indicates the start of a
10613 function-definition. */
10614 if (cp_parser_token_starts_function_definition_p (token))
10615 {
10616 if (!function_definition_allowed_p)
10617 {
10618 /* If a function-definition should not appear here, issue an
10619 error message. */
10620 cp_parser_error (parser,
10621 "a function-definition is not allowed here");
10622 return error_mark_node;
10623 }
10624 else
10625 {
10626 /* Neither attributes nor an asm-specification are allowed
10627 on a function-definition. */
10628 if (asm_specification)
10629 error ("an asm-specification is not allowed on a function-definition");
10630 if (attributes)
10631 error ("attributes are not allowed on a function-definition");
10632 /* This is a function-definition. */
10633 *function_definition_p = true;
10634
10635 /* Parse the function definition. */
10636 if (member_p)
10637 decl = cp_parser_save_member_function_body (parser,
10638 decl_specifiers,
10639 declarator,
10640 prefix_attributes);
10641 else
10642 decl
10643 = (cp_parser_function_definition_from_specifiers_and_declarator
10644 (parser, decl_specifiers, prefix_attributes, declarator));
10645
10646 return decl;
10647 }
10648 }
10649
10650 /* [dcl.dcl]
10651
10652 Only in function declarations for constructors, destructors, and
10653 type conversions can the decl-specifier-seq be omitted.
10654
10655 We explicitly postpone this check past the point where we handle
10656 function-definitions because we tolerate function-definitions
10657 that are missing their return types in some modes. */
10658 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10659 {
10660 cp_parser_error (parser,
10661 "expected constructor, destructor, or type conversion");
10662 return error_mark_node;
10663 }
10664
10665 /* An `=' or an `(' indicates an initializer. */
10666 is_initialized = (token->type == CPP_EQ
10667 || token->type == CPP_OPEN_PAREN);
10668 /* If the init-declarator isn't initialized and isn't followed by a
10669 `,' or `;', it's not a valid init-declarator. */
10670 if (!is_initialized
10671 && token->type != CPP_COMMA
10672 && token->type != CPP_SEMICOLON)
10673 {
10674 cp_parser_error (parser, "expected initializer");
10675 return error_mark_node;
10676 }
10677
10678 /* Because start_decl has side-effects, we should only call it if we
10679 know we're going ahead. By this point, we know that we cannot
10680 possibly be looking at any other construct. */
10681 cp_parser_commit_to_tentative_parse (parser);
10682
10683 /* If the decl specifiers were bad, issue an error now that we're
10684 sure this was intended to be a declarator. Then continue
10685 declaring the variable(s), as int, to try to cut down on further
10686 errors. */
10687 if (decl_specifiers->any_specifiers_p
10688 && decl_specifiers->type == error_mark_node)
10689 {
10690 cp_parser_error (parser, "invalid type in declaration");
10691 decl_specifiers->type = integer_type_node;
10692 }
10693
10694 /* Check to see whether or not this declaration is a friend. */
10695 friend_p = cp_parser_friend_p (decl_specifiers);
10696
10697 /* Check that the number of template-parameter-lists is OK. */
10698 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10699 return error_mark_node;
10700
10701 /* Enter the newly declared entry in the symbol table. If we're
10702 processing a declaration in a class-specifier, we wait until
10703 after processing the initializer. */
10704 if (!member_p)
10705 {
10706 if (parser->in_unbraced_linkage_specification_p)
10707 {
10708 decl_specifiers->storage_class = sc_extern;
10709 have_extern_spec = false;
10710 }
10711 decl = start_decl (declarator, decl_specifiers,
10712 is_initialized, attributes, prefix_attributes,
10713 &pushed_scope);
10714 }
10715 else if (scope)
10716 /* Enter the SCOPE. That way unqualified names appearing in the
10717 initializer will be looked up in SCOPE. */
10718 pushed_scope = push_scope (scope);
10719
10720 /* Perform deferred access control checks, now that we know in which
10721 SCOPE the declared entity resides. */
10722 if (!member_p && decl)
10723 {
10724 tree saved_current_function_decl = NULL_TREE;
10725
10726 /* If the entity being declared is a function, pretend that we
10727 are in its scope. If it is a `friend', it may have access to
10728 things that would not otherwise be accessible. */
10729 if (TREE_CODE (decl) == FUNCTION_DECL)
10730 {
10731 saved_current_function_decl = current_function_decl;
10732 current_function_decl = decl;
10733 }
10734
10735 /* Perform the access control checks for the declarator and the
10736 the decl-specifiers. */
10737 perform_deferred_access_checks ();
10738
10739 /* Restore the saved value. */
10740 if (TREE_CODE (decl) == FUNCTION_DECL)
10741 current_function_decl = saved_current_function_decl;
10742 }
10743
10744 /* Parse the initializer. */
10745 if (is_initialized)
10746 initializer = cp_parser_initializer (parser,
10747 &is_parenthesized_init,
10748 &is_non_constant_init);
10749 else
10750 {
10751 initializer = NULL_TREE;
10752 is_parenthesized_init = false;
10753 is_non_constant_init = true;
10754 }
10755
10756 /* The old parser allows attributes to appear after a parenthesized
10757 initializer. Mark Mitchell proposed removing this functionality
10758 on the GCC mailing lists on 2002-08-13. This parser accepts the
10759 attributes -- but ignores them. */
10760 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10761 if (cp_parser_attributes_opt (parser))
10762 warning (0, "attributes after parenthesized initializer ignored");
10763
10764 /* For an in-class declaration, use `grokfield' to create the
10765 declaration. */
10766 if (member_p)
10767 {
10768 if (pushed_scope)
10769 {
10770 pop_scope (pushed_scope);
10771 pushed_scope = false;
10772 }
10773 decl = grokfield (declarator, decl_specifiers,
10774 initializer, /*asmspec=*/NULL_TREE,
10775 /*attributes=*/NULL_TREE);
10776 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10777 cp_parser_save_default_args (parser, decl);
10778 }
10779
10780 /* Finish processing the declaration. But, skip friend
10781 declarations. */
10782 if (!friend_p && decl && decl != error_mark_node)
10783 {
10784 cp_finish_decl (decl,
10785 initializer,
10786 asm_specification,
10787 /* If the initializer is in parentheses, then this is
10788 a direct-initialization, which means that an
10789 `explicit' constructor is OK. Otherwise, an
10790 `explicit' constructor cannot be used. */
10791 ((is_parenthesized_init || !is_initialized)
10792 ? 0 : LOOKUP_ONLYCONVERTING));
10793 }
10794 if (!friend_p && pushed_scope)
10795 pop_scope (pushed_scope);
10796
10797 /* Remember whether or not variables were initialized by
10798 constant-expressions. */
10799 if (decl && TREE_CODE (decl) == VAR_DECL
10800 && is_initialized && !is_non_constant_init)
10801 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10802
10803 return decl;
10804 }
10805
10806 /* Parse a declarator.
10807
10808 declarator:
10809 direct-declarator
10810 ptr-operator declarator
10811
10812 abstract-declarator:
10813 ptr-operator abstract-declarator [opt]
10814 direct-abstract-declarator
10815
10816 GNU Extensions:
10817
10818 declarator:
10819 attributes [opt] direct-declarator
10820 attributes [opt] ptr-operator declarator
10821
10822 abstract-declarator:
10823 attributes [opt] ptr-operator abstract-declarator [opt]
10824 attributes [opt] direct-abstract-declarator
10825
10826 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10827 detect constructor, destructor or conversion operators. It is set
10828 to -1 if the declarator is a name, and +1 if it is a
10829 function. Otherwise it is set to zero. Usually you just want to
10830 test for >0, but internally the negative value is used.
10831
10832 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10833 a decl-specifier-seq unless it declares a constructor, destructor,
10834 or conversion. It might seem that we could check this condition in
10835 semantic analysis, rather than parsing, but that makes it difficult
10836 to handle something like `f()'. We want to notice that there are
10837 no decl-specifiers, and therefore realize that this is an
10838 expression, not a declaration.)
10839
10840 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10841 the declarator is a direct-declarator of the form "(...)".
10842
10843 MEMBER_P is true iff this declarator is a member-declarator. */
10844
10845 static cp_declarator *
10846 cp_parser_declarator (cp_parser* parser,
10847 cp_parser_declarator_kind dcl_kind,
10848 int* ctor_dtor_or_conv_p,
10849 bool* parenthesized_p,
10850 bool member_p)
10851 {
10852 cp_token *token;
10853 cp_declarator *declarator;
10854 enum tree_code code;
10855 cp_cv_quals cv_quals;
10856 tree class_type;
10857 tree attributes = NULL_TREE;
10858
10859 /* Assume this is not a constructor, destructor, or type-conversion
10860 operator. */
10861 if (ctor_dtor_or_conv_p)
10862 *ctor_dtor_or_conv_p = 0;
10863
10864 if (cp_parser_allow_gnu_extensions_p (parser))
10865 attributes = cp_parser_attributes_opt (parser);
10866
10867 /* Peek at the next token. */
10868 token = cp_lexer_peek_token (parser->lexer);
10869
10870 /* Check for the ptr-operator production. */
10871 cp_parser_parse_tentatively (parser);
10872 /* Parse the ptr-operator. */
10873 code = cp_parser_ptr_operator (parser,
10874 &class_type,
10875 &cv_quals);
10876 /* If that worked, then we have a ptr-operator. */
10877 if (cp_parser_parse_definitely (parser))
10878 {
10879 /* If a ptr-operator was found, then this declarator was not
10880 parenthesized. */
10881 if (parenthesized_p)
10882 *parenthesized_p = true;
10883 /* The dependent declarator is optional if we are parsing an
10884 abstract-declarator. */
10885 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10886 cp_parser_parse_tentatively (parser);
10887
10888 /* Parse the dependent declarator. */
10889 declarator = cp_parser_declarator (parser, dcl_kind,
10890 /*ctor_dtor_or_conv_p=*/NULL,
10891 /*parenthesized_p=*/NULL,
10892 /*member_p=*/false);
10893
10894 /* If we are parsing an abstract-declarator, we must handle the
10895 case where the dependent declarator is absent. */
10896 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10897 && !cp_parser_parse_definitely (parser))
10898 declarator = NULL;
10899
10900 /* Build the representation of the ptr-operator. */
10901 if (class_type)
10902 declarator = make_ptrmem_declarator (cv_quals,
10903 class_type,
10904 declarator);
10905 else if (code == INDIRECT_REF)
10906 declarator = make_pointer_declarator (cv_quals, declarator);
10907 else
10908 declarator = make_reference_declarator (cv_quals, declarator);
10909 }
10910 /* Everything else is a direct-declarator. */
10911 else
10912 {
10913 if (parenthesized_p)
10914 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10915 CPP_OPEN_PAREN);
10916 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10917 ctor_dtor_or_conv_p,
10918 member_p);
10919 }
10920
10921 if (attributes && declarator != cp_error_declarator)
10922 declarator->attributes = attributes;
10923
10924 return declarator;
10925 }
10926
10927 /* Parse a direct-declarator or direct-abstract-declarator.
10928
10929 direct-declarator:
10930 declarator-id
10931 direct-declarator ( parameter-declaration-clause )
10932 cv-qualifier-seq [opt]
10933 exception-specification [opt]
10934 direct-declarator [ constant-expression [opt] ]
10935 ( declarator )
10936
10937 direct-abstract-declarator:
10938 direct-abstract-declarator [opt]
10939 ( parameter-declaration-clause )
10940 cv-qualifier-seq [opt]
10941 exception-specification [opt]
10942 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10943 ( abstract-declarator )
10944
10945 Returns a representation of the declarator. DCL_KIND is
10946 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10947 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10948 we are parsing a direct-declarator. It is
10949 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10950 of ambiguity we prefer an abstract declarator, as per
10951 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10952 cp_parser_declarator. */
10953
10954 static cp_declarator *
10955 cp_parser_direct_declarator (cp_parser* parser,
10956 cp_parser_declarator_kind dcl_kind,
10957 int* ctor_dtor_or_conv_p,
10958 bool member_p)
10959 {
10960 cp_token *token;
10961 cp_declarator *declarator = NULL;
10962 tree scope = NULL_TREE;
10963 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10964 bool saved_in_declarator_p = parser->in_declarator_p;
10965 bool first = true;
10966 tree pushed_scope = NULL_TREE;
10967
10968 while (true)
10969 {
10970 /* Peek at the next token. */
10971 token = cp_lexer_peek_token (parser->lexer);
10972 if (token->type == CPP_OPEN_PAREN)
10973 {
10974 /* This is either a parameter-declaration-clause, or a
10975 parenthesized declarator. When we know we are parsing a
10976 named declarator, it must be a parenthesized declarator
10977 if FIRST is true. For instance, `(int)' is a
10978 parameter-declaration-clause, with an omitted
10979 direct-abstract-declarator. But `((*))', is a
10980 parenthesized abstract declarator. Finally, when T is a
10981 template parameter `(T)' is a
10982 parameter-declaration-clause, and not a parenthesized
10983 named declarator.
10984
10985 We first try and parse a parameter-declaration-clause,
10986 and then try a nested declarator (if FIRST is true).
10987
10988 It is not an error for it not to be a
10989 parameter-declaration-clause, even when FIRST is
10990 false. Consider,
10991
10992 int i (int);
10993 int i (3);
10994
10995 The first is the declaration of a function while the
10996 second is a the definition of a variable, including its
10997 initializer.
10998
10999 Having seen only the parenthesis, we cannot know which of
11000 these two alternatives should be selected. Even more
11001 complex are examples like:
11002
11003 int i (int (a));
11004 int i (int (3));
11005
11006 The former is a function-declaration; the latter is a
11007 variable initialization.
11008
11009 Thus again, we try a parameter-declaration-clause, and if
11010 that fails, we back out and return. */
11011
11012 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11013 {
11014 cp_parameter_declarator *params;
11015 unsigned saved_num_template_parameter_lists;
11016
11017 /* In a member-declarator, the only valid interpretation
11018 of a parenthesis is the start of a
11019 parameter-declaration-clause. (It is invalid to
11020 initialize a static data member with a parenthesized
11021 initializer; only the "=" form of initialization is
11022 permitted.) */
11023 if (!member_p)
11024 cp_parser_parse_tentatively (parser);
11025
11026 /* Consume the `('. */
11027 cp_lexer_consume_token (parser->lexer);
11028 if (first)
11029 {
11030 /* If this is going to be an abstract declarator, we're
11031 in a declarator and we can't have default args. */
11032 parser->default_arg_ok_p = false;
11033 parser->in_declarator_p = true;
11034 }
11035
11036 /* Inside the function parameter list, surrounding
11037 template-parameter-lists do not apply. */
11038 saved_num_template_parameter_lists
11039 = parser->num_template_parameter_lists;
11040 parser->num_template_parameter_lists = 0;
11041
11042 /* Parse the parameter-declaration-clause. */
11043 params = cp_parser_parameter_declaration_clause (parser);
11044
11045 parser->num_template_parameter_lists
11046 = saved_num_template_parameter_lists;
11047
11048 /* If all went well, parse the cv-qualifier-seq and the
11049 exception-specification. */
11050 if (member_p || cp_parser_parse_definitely (parser))
11051 {
11052 cp_cv_quals cv_quals;
11053 tree exception_specification;
11054
11055 if (ctor_dtor_or_conv_p)
11056 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11057 first = false;
11058 /* Consume the `)'. */
11059 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11060
11061 /* Parse the cv-qualifier-seq. */
11062 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11063 /* And the exception-specification. */
11064 exception_specification
11065 = cp_parser_exception_specification_opt (parser);
11066
11067 /* Create the function-declarator. */
11068 declarator = make_call_declarator (declarator,
11069 params,
11070 cv_quals,
11071 exception_specification);
11072 /* Any subsequent parameter lists are to do with
11073 return type, so are not those of the declared
11074 function. */
11075 parser->default_arg_ok_p = false;
11076
11077 /* Repeat the main loop. */
11078 continue;
11079 }
11080 }
11081
11082 /* If this is the first, we can try a parenthesized
11083 declarator. */
11084 if (first)
11085 {
11086 bool saved_in_type_id_in_expr_p;
11087
11088 parser->default_arg_ok_p = saved_default_arg_ok_p;
11089 parser->in_declarator_p = saved_in_declarator_p;
11090
11091 /* Consume the `('. */
11092 cp_lexer_consume_token (parser->lexer);
11093 /* Parse the nested declarator. */
11094 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11095 parser->in_type_id_in_expr_p = true;
11096 declarator
11097 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11098 /*parenthesized_p=*/NULL,
11099 member_p);
11100 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11101 first = false;
11102 /* Expect a `)'. */
11103 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11104 declarator = cp_error_declarator;
11105 if (declarator == cp_error_declarator)
11106 break;
11107
11108 goto handle_declarator;
11109 }
11110 /* Otherwise, we must be done. */
11111 else
11112 break;
11113 }
11114 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11115 && token->type == CPP_OPEN_SQUARE)
11116 {
11117 /* Parse an array-declarator. */
11118 tree bounds;
11119
11120 if (ctor_dtor_or_conv_p)
11121 *ctor_dtor_or_conv_p = 0;
11122
11123 first = false;
11124 parser->default_arg_ok_p = false;
11125 parser->in_declarator_p = true;
11126 /* Consume the `['. */
11127 cp_lexer_consume_token (parser->lexer);
11128 /* Peek at the next token. */
11129 token = cp_lexer_peek_token (parser->lexer);
11130 /* If the next token is `]', then there is no
11131 constant-expression. */
11132 if (token->type != CPP_CLOSE_SQUARE)
11133 {
11134 bool non_constant_p;
11135
11136 bounds
11137 = cp_parser_constant_expression (parser,
11138 /*allow_non_constant=*/true,
11139 &non_constant_p);
11140 if (!non_constant_p)
11141 bounds = fold_non_dependent_expr (bounds);
11142 /* Normally, the array bound must be an integral constant
11143 expression. However, as an extension, we allow VLAs
11144 in function scopes. */
11145 else if (!at_function_scope_p ())
11146 {
11147 error ("array bound is not an integer constant");
11148 bounds = error_mark_node;
11149 }
11150 }
11151 else
11152 bounds = NULL_TREE;
11153 /* Look for the closing `]'. */
11154 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11155 {
11156 declarator = cp_error_declarator;
11157 break;
11158 }
11159
11160 declarator = make_array_declarator (declarator, bounds);
11161 }
11162 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11163 {
11164 tree qualifying_scope;
11165 tree unqualified_name;
11166
11167 /* Parse a declarator-id */
11168 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11169 cp_parser_parse_tentatively (parser);
11170 unqualified_name = cp_parser_declarator_id (parser);
11171 qualifying_scope = parser->scope;
11172 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11173 {
11174 if (!cp_parser_parse_definitely (parser))
11175 unqualified_name = error_mark_node;
11176 else if (qualifying_scope
11177 || (TREE_CODE (unqualified_name)
11178 != IDENTIFIER_NODE))
11179 {
11180 cp_parser_error (parser, "expected unqualified-id");
11181 unqualified_name = error_mark_node;
11182 }
11183 }
11184
11185 if (unqualified_name == error_mark_node)
11186 {
11187 declarator = cp_error_declarator;
11188 break;
11189 }
11190
11191 if (qualifying_scope && at_namespace_scope_p ()
11192 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11193 {
11194 /* In the declaration of a member of a template class
11195 outside of the class itself, the SCOPE will sometimes
11196 be a TYPENAME_TYPE. For example, given:
11197
11198 template <typename T>
11199 int S<T>::R::i = 3;
11200
11201 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11202 this context, we must resolve S<T>::R to an ordinary
11203 type, rather than a typename type.
11204
11205 The reason we normally avoid resolving TYPENAME_TYPEs
11206 is that a specialization of `S' might render
11207 `S<T>::R' not a type. However, if `S' is
11208 specialized, then this `i' will not be used, so there
11209 is no harm in resolving the types here. */
11210 tree type;
11211
11212 /* Resolve the TYPENAME_TYPE. */
11213 type = resolve_typename_type (qualifying_scope,
11214 /*only_current_p=*/false);
11215 /* If that failed, the declarator is invalid. */
11216 if (type == error_mark_node)
11217 error ("%<%T::%D%> is not a type",
11218 TYPE_CONTEXT (qualifying_scope),
11219 TYPE_IDENTIFIER (qualifying_scope));
11220 qualifying_scope = type;
11221 }
11222
11223 declarator = make_id_declarator (qualifying_scope,
11224 unqualified_name);
11225 declarator->id_loc = token->location;
11226 if (unqualified_name)
11227 {
11228 tree class_type;
11229
11230 if (qualifying_scope
11231 && CLASS_TYPE_P (qualifying_scope))
11232 class_type = qualifying_scope;
11233 else
11234 class_type = current_class_type;
11235
11236 if (class_type)
11237 {
11238 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11239 declarator->u.id.sfk = sfk_destructor;
11240 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11241 declarator->u.id.sfk = sfk_conversion;
11242 else if (/* There's no way to declare a constructor
11243 for an anonymous type, even if the type
11244 got a name for linkage purposes. */
11245 !TYPE_WAS_ANONYMOUS (class_type)
11246 && (constructor_name_p (unqualified_name,
11247 class_type)
11248 || (TREE_CODE (unqualified_name) == TYPE_DECL
11249 && (same_type_p
11250 (TREE_TYPE (unqualified_name),
11251 class_type)))))
11252 declarator->u.id.sfk = sfk_constructor;
11253
11254 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11255 *ctor_dtor_or_conv_p = -1;
11256 if (qualifying_scope
11257 && TREE_CODE (unqualified_name) == TYPE_DECL
11258 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11259 {
11260 error ("invalid use of constructor as a template");
11261 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11262 "the constructor in a qualified name",
11263 class_type,
11264 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11265 class_type, class_type);
11266 }
11267 }
11268 }
11269
11270 handle_declarator:;
11271 scope = get_scope_of_declarator (declarator);
11272 if (scope)
11273 /* Any names that appear after the declarator-id for a
11274 member are looked up in the containing scope. */
11275 pushed_scope = push_scope (scope);
11276 parser->in_declarator_p = true;
11277 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11278 || (declarator && declarator->kind == cdk_id))
11279 /* Default args are only allowed on function
11280 declarations. */
11281 parser->default_arg_ok_p = saved_default_arg_ok_p;
11282 else
11283 parser->default_arg_ok_p = false;
11284
11285 first = false;
11286 }
11287 /* We're done. */
11288 else
11289 break;
11290 }
11291
11292 /* For an abstract declarator, we might wind up with nothing at this
11293 point. That's an error; the declarator is not optional. */
11294 if (!declarator)
11295 cp_parser_error (parser, "expected declarator");
11296
11297 /* If we entered a scope, we must exit it now. */
11298 if (pushed_scope)
11299 pop_scope (pushed_scope);
11300
11301 parser->default_arg_ok_p = saved_default_arg_ok_p;
11302 parser->in_declarator_p = saved_in_declarator_p;
11303
11304 return declarator;
11305 }
11306
11307 /* Parse a ptr-operator.
11308
11309 ptr-operator:
11310 * cv-qualifier-seq [opt]
11311 &
11312 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11313
11314 GNU Extension:
11315
11316 ptr-operator:
11317 & cv-qualifier-seq [opt]
11318
11319 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11320 Returns ADDR_EXPR if a reference was used. In the case of a
11321 pointer-to-member, *TYPE is filled in with the TYPE containing the
11322 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11323 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11324 ERROR_MARK if an error occurred. */
11325
11326 static enum tree_code
11327 cp_parser_ptr_operator (cp_parser* parser,
11328 tree* type,
11329 cp_cv_quals *cv_quals)
11330 {
11331 enum tree_code code = ERROR_MARK;
11332 cp_token *token;
11333
11334 /* Assume that it's not a pointer-to-member. */
11335 *type = NULL_TREE;
11336 /* And that there are no cv-qualifiers. */
11337 *cv_quals = TYPE_UNQUALIFIED;
11338
11339 /* Peek at the next token. */
11340 token = cp_lexer_peek_token (parser->lexer);
11341 /* If it's a `*' or `&' we have a pointer or reference. */
11342 if (token->type == CPP_MULT || token->type == CPP_AND)
11343 {
11344 /* Remember which ptr-operator we were processing. */
11345 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11346
11347 /* Consume the `*' or `&'. */
11348 cp_lexer_consume_token (parser->lexer);
11349
11350 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11351 `&', if we are allowing GNU extensions. (The only qualifier
11352 that can legally appear after `&' is `restrict', but that is
11353 enforced during semantic analysis. */
11354 if (code == INDIRECT_REF
11355 || cp_parser_allow_gnu_extensions_p (parser))
11356 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11357 }
11358 else
11359 {
11360 /* Try the pointer-to-member case. */
11361 cp_parser_parse_tentatively (parser);
11362 /* Look for the optional `::' operator. */
11363 cp_parser_global_scope_opt (parser,
11364 /*current_scope_valid_p=*/false);
11365 /* Look for the nested-name specifier. */
11366 cp_parser_nested_name_specifier (parser,
11367 /*typename_keyword_p=*/false,
11368 /*check_dependency_p=*/true,
11369 /*type_p=*/false,
11370 /*is_declaration=*/false);
11371 /* If we found it, and the next token is a `*', then we are
11372 indeed looking at a pointer-to-member operator. */
11373 if (!cp_parser_error_occurred (parser)
11374 && cp_parser_require (parser, CPP_MULT, "`*'"))
11375 {
11376 /* The type of which the member is a member is given by the
11377 current SCOPE. */
11378 *type = parser->scope;
11379 /* The next name will not be qualified. */
11380 parser->scope = NULL_TREE;
11381 parser->qualifying_scope = NULL_TREE;
11382 parser->object_scope = NULL_TREE;
11383 /* Indicate that the `*' operator was used. */
11384 code = INDIRECT_REF;
11385 /* Look for the optional cv-qualifier-seq. */
11386 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11387 }
11388 /* If that didn't work we don't have a ptr-operator. */
11389 if (!cp_parser_parse_definitely (parser))
11390 cp_parser_error (parser, "expected ptr-operator");
11391 }
11392
11393 return code;
11394 }
11395
11396 /* Parse an (optional) cv-qualifier-seq.
11397
11398 cv-qualifier-seq:
11399 cv-qualifier cv-qualifier-seq [opt]
11400
11401 cv-qualifier:
11402 const
11403 volatile
11404
11405 GNU Extension:
11406
11407 cv-qualifier:
11408 __restrict__
11409
11410 Returns a bitmask representing the cv-qualifiers. */
11411
11412 static cp_cv_quals
11413 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11414 {
11415 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11416
11417 while (true)
11418 {
11419 cp_token *token;
11420 cp_cv_quals cv_qualifier;
11421
11422 /* Peek at the next token. */
11423 token = cp_lexer_peek_token (parser->lexer);
11424 /* See if it's a cv-qualifier. */
11425 switch (token->keyword)
11426 {
11427 case RID_CONST:
11428 cv_qualifier = TYPE_QUAL_CONST;
11429 break;
11430
11431 case RID_VOLATILE:
11432 cv_qualifier = TYPE_QUAL_VOLATILE;
11433 break;
11434
11435 case RID_RESTRICT:
11436 cv_qualifier = TYPE_QUAL_RESTRICT;
11437 break;
11438
11439 default:
11440 cv_qualifier = TYPE_UNQUALIFIED;
11441 break;
11442 }
11443
11444 if (!cv_qualifier)
11445 break;
11446
11447 if (cv_quals & cv_qualifier)
11448 {
11449 error ("duplicate cv-qualifier");
11450 cp_lexer_purge_token (parser->lexer);
11451 }
11452 else
11453 {
11454 cp_lexer_consume_token (parser->lexer);
11455 cv_quals |= cv_qualifier;
11456 }
11457 }
11458
11459 return cv_quals;
11460 }
11461
11462 /* Parse a declarator-id.
11463
11464 declarator-id:
11465 id-expression
11466 :: [opt] nested-name-specifier [opt] type-name
11467
11468 In the `id-expression' case, the value returned is as for
11469 cp_parser_id_expression if the id-expression was an unqualified-id.
11470 If the id-expression was a qualified-id, then a SCOPE_REF is
11471 returned. The first operand is the scope (either a NAMESPACE_DECL
11472 or TREE_TYPE), but the second is still just a representation of an
11473 unqualified-id. */
11474
11475 static tree
11476 cp_parser_declarator_id (cp_parser* parser)
11477 {
11478 /* The expression must be an id-expression. Assume that qualified
11479 names are the names of types so that:
11480
11481 template <class T>
11482 int S<T>::R::i = 3;
11483
11484 will work; we must treat `S<T>::R' as the name of a type.
11485 Similarly, assume that qualified names are templates, where
11486 required, so that:
11487
11488 template <class T>
11489 int S<T>::R<T>::i = 3;
11490
11491 will work, too. */
11492 return cp_parser_id_expression (parser,
11493 /*template_keyword_p=*/false,
11494 /*check_dependency_p=*/false,
11495 /*template_p=*/NULL,
11496 /*declarator_p=*/true);
11497 }
11498
11499 /* Parse a type-id.
11500
11501 type-id:
11502 type-specifier-seq abstract-declarator [opt]
11503
11504 Returns the TYPE specified. */
11505
11506 static tree
11507 cp_parser_type_id (cp_parser* parser)
11508 {
11509 cp_decl_specifier_seq type_specifier_seq;
11510 cp_declarator *abstract_declarator;
11511
11512 /* Parse the type-specifier-seq. */
11513 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11514 &type_specifier_seq);
11515 if (type_specifier_seq.type == error_mark_node)
11516 return error_mark_node;
11517
11518 /* There might or might not be an abstract declarator. */
11519 cp_parser_parse_tentatively (parser);
11520 /* Look for the declarator. */
11521 abstract_declarator
11522 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11523 /*parenthesized_p=*/NULL,
11524 /*member_p=*/false);
11525 /* Check to see if there really was a declarator. */
11526 if (!cp_parser_parse_definitely (parser))
11527 abstract_declarator = NULL;
11528
11529 return groktypename (&type_specifier_seq, abstract_declarator);
11530 }
11531
11532 /* Parse a type-specifier-seq.
11533
11534 type-specifier-seq:
11535 type-specifier type-specifier-seq [opt]
11536
11537 GNU extension:
11538
11539 type-specifier-seq:
11540 attributes type-specifier-seq [opt]
11541
11542 If IS_CONDITION is true, we are at the start of a "condition",
11543 e.g., we've just seen "if (".
11544
11545 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11546
11547 static void
11548 cp_parser_type_specifier_seq (cp_parser* parser,
11549 bool is_condition,
11550 cp_decl_specifier_seq *type_specifier_seq)
11551 {
11552 bool seen_type_specifier = false;
11553 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11554
11555 /* Clear the TYPE_SPECIFIER_SEQ. */
11556 clear_decl_specs (type_specifier_seq);
11557
11558 /* Parse the type-specifiers and attributes. */
11559 while (true)
11560 {
11561 tree type_specifier;
11562 bool is_cv_qualifier;
11563
11564 /* Check for attributes first. */
11565 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11566 {
11567 type_specifier_seq->attributes =
11568 chainon (type_specifier_seq->attributes,
11569 cp_parser_attributes_opt (parser));
11570 continue;
11571 }
11572
11573 /* Look for the type-specifier. */
11574 type_specifier = cp_parser_type_specifier (parser,
11575 flags,
11576 type_specifier_seq,
11577 /*is_declaration=*/false,
11578 NULL,
11579 &is_cv_qualifier);
11580 if (!type_specifier)
11581 {
11582 /* If the first type-specifier could not be found, this is not a
11583 type-specifier-seq at all. */
11584 if (!seen_type_specifier)
11585 {
11586 cp_parser_error (parser, "expected type-specifier");
11587 type_specifier_seq->type = error_mark_node;
11588 return;
11589 }
11590 /* If subsequent type-specifiers could not be found, the
11591 type-specifier-seq is complete. */
11592 break;
11593 }
11594
11595 seen_type_specifier = true;
11596 /* The standard says that a condition can be:
11597
11598 type-specifier-seq declarator = assignment-expression
11599
11600 However, given:
11601
11602 struct S {};
11603 if (int S = ...)
11604
11605 we should treat the "S" as a declarator, not as a
11606 type-specifier. The standard doesn't say that explicitly for
11607 type-specifier-seq, but it does say that for
11608 decl-specifier-seq in an ordinary declaration. Perhaps it
11609 would be clearer just to allow a decl-specifier-seq here, and
11610 then add a semantic restriction that if any decl-specifiers
11611 that are not type-specifiers appear, the program is invalid. */
11612 if (is_condition && !is_cv_qualifier)
11613 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11614 }
11615
11616 return;
11617 }
11618
11619 /* Parse a parameter-declaration-clause.
11620
11621 parameter-declaration-clause:
11622 parameter-declaration-list [opt] ... [opt]
11623 parameter-declaration-list , ...
11624
11625 Returns a representation for the parameter declarations. A return
11626 value of NULL indicates a parameter-declaration-clause consisting
11627 only of an ellipsis. */
11628
11629 static cp_parameter_declarator *
11630 cp_parser_parameter_declaration_clause (cp_parser* parser)
11631 {
11632 cp_parameter_declarator *parameters;
11633 cp_token *token;
11634 bool ellipsis_p;
11635 bool is_error;
11636
11637 /* Peek at the next token. */
11638 token = cp_lexer_peek_token (parser->lexer);
11639 /* Check for trivial parameter-declaration-clauses. */
11640 if (token->type == CPP_ELLIPSIS)
11641 {
11642 /* Consume the `...' token. */
11643 cp_lexer_consume_token (parser->lexer);
11644 return NULL;
11645 }
11646 else if (token->type == CPP_CLOSE_PAREN)
11647 /* There are no parameters. */
11648 {
11649 #ifndef NO_IMPLICIT_EXTERN_C
11650 if (in_system_header && current_class_type == NULL
11651 && current_lang_name == lang_name_c)
11652 return NULL;
11653 else
11654 #endif
11655 return no_parameters;
11656 }
11657 /* Check for `(void)', too, which is a special case. */
11658 else if (token->keyword == RID_VOID
11659 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11660 == CPP_CLOSE_PAREN))
11661 {
11662 /* Consume the `void' token. */
11663 cp_lexer_consume_token (parser->lexer);
11664 /* There are no parameters. */
11665 return no_parameters;
11666 }
11667
11668 /* Parse the parameter-declaration-list. */
11669 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11670 /* If a parse error occurred while parsing the
11671 parameter-declaration-list, then the entire
11672 parameter-declaration-clause is erroneous. */
11673 if (is_error)
11674 return NULL;
11675
11676 /* Peek at the next token. */
11677 token = cp_lexer_peek_token (parser->lexer);
11678 /* If it's a `,', the clause should terminate with an ellipsis. */
11679 if (token->type == CPP_COMMA)
11680 {
11681 /* Consume the `,'. */
11682 cp_lexer_consume_token (parser->lexer);
11683 /* Expect an ellipsis. */
11684 ellipsis_p
11685 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11686 }
11687 /* It might also be `...' if the optional trailing `,' was
11688 omitted. */
11689 else if (token->type == CPP_ELLIPSIS)
11690 {
11691 /* Consume the `...' token. */
11692 cp_lexer_consume_token (parser->lexer);
11693 /* And remember that we saw it. */
11694 ellipsis_p = true;
11695 }
11696 else
11697 ellipsis_p = false;
11698
11699 /* Finish the parameter list. */
11700 if (parameters && ellipsis_p)
11701 parameters->ellipsis_p = true;
11702
11703 return parameters;
11704 }
11705
11706 /* Parse a parameter-declaration-list.
11707
11708 parameter-declaration-list:
11709 parameter-declaration
11710 parameter-declaration-list , parameter-declaration
11711
11712 Returns a representation of the parameter-declaration-list, as for
11713 cp_parser_parameter_declaration_clause. However, the
11714 `void_list_node' is never appended to the list. Upon return,
11715 *IS_ERROR will be true iff an error occurred. */
11716
11717 static cp_parameter_declarator *
11718 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11719 {
11720 cp_parameter_declarator *parameters = NULL;
11721 cp_parameter_declarator **tail = &parameters;
11722
11723 /* Assume all will go well. */
11724 *is_error = false;
11725
11726 /* Look for more parameters. */
11727 while (true)
11728 {
11729 cp_parameter_declarator *parameter;
11730 bool parenthesized_p;
11731 /* Parse the parameter. */
11732 parameter
11733 = cp_parser_parameter_declaration (parser,
11734 /*template_parm_p=*/false,
11735 &parenthesized_p);
11736
11737 /* If a parse error occurred parsing the parameter declaration,
11738 then the entire parameter-declaration-list is erroneous. */
11739 if (!parameter)
11740 {
11741 *is_error = true;
11742 parameters = NULL;
11743 break;
11744 }
11745 /* Add the new parameter to the list. */
11746 *tail = parameter;
11747 tail = &parameter->next;
11748
11749 /* Peek at the next token. */
11750 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11751 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11752 /* The parameter-declaration-list is complete. */
11753 break;
11754 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11755 {
11756 cp_token *token;
11757
11758 /* Peek at the next token. */
11759 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11760 /* If it's an ellipsis, then the list is complete. */
11761 if (token->type == CPP_ELLIPSIS)
11762 break;
11763 /* Otherwise, there must be more parameters. Consume the
11764 `,'. */
11765 cp_lexer_consume_token (parser->lexer);
11766 /* When parsing something like:
11767
11768 int i(float f, double d)
11769
11770 we can tell after seeing the declaration for "f" that we
11771 are not looking at an initialization of a variable "i",
11772 but rather at the declaration of a function "i".
11773
11774 Due to the fact that the parsing of template arguments
11775 (as specified to a template-id) requires backtracking we
11776 cannot use this technique when inside a template argument
11777 list. */
11778 if (!parser->in_template_argument_list_p
11779 && !parser->in_type_id_in_expr_p
11780 && cp_parser_uncommitted_to_tentative_parse_p (parser)
11781 /* However, a parameter-declaration of the form
11782 "foat(f)" (which is a valid declaration of a
11783 parameter "f") can also be interpreted as an
11784 expression (the conversion of "f" to "float"). */
11785 && !parenthesized_p)
11786 cp_parser_commit_to_tentative_parse (parser);
11787 }
11788 else
11789 {
11790 cp_parser_error (parser, "expected %<,%> or %<...%>");
11791 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11792 cp_parser_skip_to_closing_parenthesis (parser,
11793 /*recovering=*/true,
11794 /*or_comma=*/false,
11795 /*consume_paren=*/false);
11796 break;
11797 }
11798 }
11799
11800 return parameters;
11801 }
11802
11803 /* Parse a parameter declaration.
11804
11805 parameter-declaration:
11806 decl-specifier-seq declarator
11807 decl-specifier-seq declarator = assignment-expression
11808 decl-specifier-seq abstract-declarator [opt]
11809 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11810
11811 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11812 declares a template parameter. (In that case, a non-nested `>'
11813 token encountered during the parsing of the assignment-expression
11814 is not interpreted as a greater-than operator.)
11815
11816 Returns a representation of the parameter, or NULL if an error
11817 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11818 true iff the declarator is of the form "(p)". */
11819
11820 static cp_parameter_declarator *
11821 cp_parser_parameter_declaration (cp_parser *parser,
11822 bool template_parm_p,
11823 bool *parenthesized_p)
11824 {
11825 int declares_class_or_enum;
11826 bool greater_than_is_operator_p;
11827 cp_decl_specifier_seq decl_specifiers;
11828 cp_declarator *declarator;
11829 tree default_argument;
11830 cp_token *token;
11831 const char *saved_message;
11832
11833 /* In a template parameter, `>' is not an operator.
11834
11835 [temp.param]
11836
11837 When parsing a default template-argument for a non-type
11838 template-parameter, the first non-nested `>' is taken as the end
11839 of the template parameter-list rather than a greater-than
11840 operator. */
11841 greater_than_is_operator_p = !template_parm_p;
11842
11843 /* Type definitions may not appear in parameter types. */
11844 saved_message = parser->type_definition_forbidden_message;
11845 parser->type_definition_forbidden_message
11846 = "types may not be defined in parameter types";
11847
11848 /* Parse the declaration-specifiers. */
11849 cp_parser_decl_specifier_seq (parser,
11850 CP_PARSER_FLAGS_NONE,
11851 &decl_specifiers,
11852 &declares_class_or_enum);
11853 /* If an error occurred, there's no reason to attempt to parse the
11854 rest of the declaration. */
11855 if (cp_parser_error_occurred (parser))
11856 {
11857 parser->type_definition_forbidden_message = saved_message;
11858 return NULL;
11859 }
11860
11861 /* Peek at the next token. */
11862 token = cp_lexer_peek_token (parser->lexer);
11863 /* If the next token is a `)', `,', `=', `>', or `...', then there
11864 is no declarator. */
11865 if (token->type == CPP_CLOSE_PAREN
11866 || token->type == CPP_COMMA
11867 || token->type == CPP_EQ
11868 || token->type == CPP_ELLIPSIS
11869 || token->type == CPP_GREATER)
11870 {
11871 declarator = NULL;
11872 if (parenthesized_p)
11873 *parenthesized_p = false;
11874 }
11875 /* Otherwise, there should be a declarator. */
11876 else
11877 {
11878 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11879 parser->default_arg_ok_p = false;
11880
11881 /* After seeing a decl-specifier-seq, if the next token is not a
11882 "(", there is no possibility that the code is a valid
11883 expression. Therefore, if parsing tentatively, we commit at
11884 this point. */
11885 if (!parser->in_template_argument_list_p
11886 /* In an expression context, having seen:
11887
11888 (int((char ...
11889
11890 we cannot be sure whether we are looking at a
11891 function-type (taking a "char" as a parameter) or a cast
11892 of some object of type "char" to "int". */
11893 && !parser->in_type_id_in_expr_p
11894 && cp_parser_uncommitted_to_tentative_parse_p (parser)
11895 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11896 cp_parser_commit_to_tentative_parse (parser);
11897 /* Parse the declarator. */
11898 declarator = cp_parser_declarator (parser,
11899 CP_PARSER_DECLARATOR_EITHER,
11900 /*ctor_dtor_or_conv_p=*/NULL,
11901 parenthesized_p,
11902 /*member_p=*/false);
11903 parser->default_arg_ok_p = saved_default_arg_ok_p;
11904 /* After the declarator, allow more attributes. */
11905 decl_specifiers.attributes
11906 = chainon (decl_specifiers.attributes,
11907 cp_parser_attributes_opt (parser));
11908 }
11909
11910 /* The restriction on defining new types applies only to the type
11911 of the parameter, not to the default argument. */
11912 parser->type_definition_forbidden_message = saved_message;
11913
11914 /* If the next token is `=', then process a default argument. */
11915 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11916 {
11917 bool saved_greater_than_is_operator_p;
11918 /* Consume the `='. */
11919 cp_lexer_consume_token (parser->lexer);
11920
11921 /* If we are defining a class, then the tokens that make up the
11922 default argument must be saved and processed later. */
11923 if (!template_parm_p && at_class_scope_p ()
11924 && TYPE_BEING_DEFINED (current_class_type))
11925 {
11926 unsigned depth = 0;
11927 cp_token *first_token;
11928 cp_token *token;
11929
11930 /* Add tokens until we have processed the entire default
11931 argument. We add the range [first_token, token). */
11932 first_token = cp_lexer_peek_token (parser->lexer);
11933 while (true)
11934 {
11935 bool done = false;
11936
11937 /* Peek at the next token. */
11938 token = cp_lexer_peek_token (parser->lexer);
11939 /* What we do depends on what token we have. */
11940 switch (token->type)
11941 {
11942 /* In valid code, a default argument must be
11943 immediately followed by a `,' `)', or `...'. */
11944 case CPP_COMMA:
11945 case CPP_CLOSE_PAREN:
11946 case CPP_ELLIPSIS:
11947 /* If we run into a non-nested `;', `}', or `]',
11948 then the code is invalid -- but the default
11949 argument is certainly over. */
11950 case CPP_SEMICOLON:
11951 case CPP_CLOSE_BRACE:
11952 case CPP_CLOSE_SQUARE:
11953 if (depth == 0)
11954 done = true;
11955 /* Update DEPTH, if necessary. */
11956 else if (token->type == CPP_CLOSE_PAREN
11957 || token->type == CPP_CLOSE_BRACE
11958 || token->type == CPP_CLOSE_SQUARE)
11959 --depth;
11960 break;
11961
11962 case CPP_OPEN_PAREN:
11963 case CPP_OPEN_SQUARE:
11964 case CPP_OPEN_BRACE:
11965 ++depth;
11966 break;
11967
11968 case CPP_GREATER:
11969 /* If we see a non-nested `>', and `>' is not an
11970 operator, then it marks the end of the default
11971 argument. */
11972 if (!depth && !greater_than_is_operator_p)
11973 done = true;
11974 break;
11975
11976 /* If we run out of tokens, issue an error message. */
11977 case CPP_EOF:
11978 error ("file ends in default argument");
11979 done = true;
11980 break;
11981
11982 case CPP_NAME:
11983 case CPP_SCOPE:
11984 /* In these cases, we should look for template-ids.
11985 For example, if the default argument is
11986 `X<int, double>()', we need to do name lookup to
11987 figure out whether or not `X' is a template; if
11988 so, the `,' does not end the default argument.
11989
11990 That is not yet done. */
11991 break;
11992
11993 default:
11994 break;
11995 }
11996
11997 /* If we've reached the end, stop. */
11998 if (done)
11999 break;
12000
12001 /* Add the token to the token block. */
12002 token = cp_lexer_consume_token (parser->lexer);
12003 }
12004
12005 /* Create a DEFAULT_ARG to represented the unparsed default
12006 argument. */
12007 default_argument = make_node (DEFAULT_ARG);
12008 DEFARG_TOKENS (default_argument)
12009 = cp_token_cache_new (first_token, token);
12010 }
12011 /* Outside of a class definition, we can just parse the
12012 assignment-expression. */
12013 else
12014 {
12015 bool saved_local_variables_forbidden_p;
12016
12017 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12018 set correctly. */
12019 saved_greater_than_is_operator_p
12020 = parser->greater_than_is_operator_p;
12021 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12022 /* Local variable names (and the `this' keyword) may not
12023 appear in a default argument. */
12024 saved_local_variables_forbidden_p
12025 = parser->local_variables_forbidden_p;
12026 parser->local_variables_forbidden_p = true;
12027 /* Parse the assignment-expression. */
12028 default_argument
12029 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12030 /* Restore saved state. */
12031 parser->greater_than_is_operator_p
12032 = saved_greater_than_is_operator_p;
12033 parser->local_variables_forbidden_p
12034 = saved_local_variables_forbidden_p;
12035 }
12036 if (!parser->default_arg_ok_p)
12037 {
12038 if (!flag_pedantic_errors)
12039 warning (0, "deprecated use of default argument for parameter of non-function");
12040 else
12041 {
12042 error ("default arguments are only permitted for function parameters");
12043 default_argument = NULL_TREE;
12044 }
12045 }
12046 }
12047 else
12048 default_argument = NULL_TREE;
12049
12050 return make_parameter_declarator (&decl_specifiers,
12051 declarator,
12052 default_argument);
12053 }
12054
12055 /* Parse a function-body.
12056
12057 function-body:
12058 compound_statement */
12059
12060 static void
12061 cp_parser_function_body (cp_parser *parser)
12062 {
12063 cp_parser_compound_statement (parser, NULL, false);
12064 }
12065
12066 /* Parse a ctor-initializer-opt followed by a function-body. Return
12067 true if a ctor-initializer was present. */
12068
12069 static bool
12070 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12071 {
12072 tree body;
12073 bool ctor_initializer_p;
12074
12075 /* Begin the function body. */
12076 body = begin_function_body ();
12077 /* Parse the optional ctor-initializer. */
12078 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12079 /* Parse the function-body. */
12080 cp_parser_function_body (parser);
12081 /* Finish the function body. */
12082 finish_function_body (body);
12083
12084 return ctor_initializer_p;
12085 }
12086
12087 /* Parse an initializer.
12088
12089 initializer:
12090 = initializer-clause
12091 ( expression-list )
12092
12093 Returns a expression representing the initializer. If no
12094 initializer is present, NULL_TREE is returned.
12095
12096 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12097 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12098 set to FALSE if there is no initializer present. If there is an
12099 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12100 is set to true; otherwise it is set to false. */
12101
12102 static tree
12103 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12104 bool* non_constant_p)
12105 {
12106 cp_token *token;
12107 tree init;
12108
12109 /* Peek at the next token. */
12110 token = cp_lexer_peek_token (parser->lexer);
12111
12112 /* Let our caller know whether or not this initializer was
12113 parenthesized. */
12114 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12115 /* Assume that the initializer is constant. */
12116 *non_constant_p = false;
12117
12118 if (token->type == CPP_EQ)
12119 {
12120 /* Consume the `='. */
12121 cp_lexer_consume_token (parser->lexer);
12122 /* Parse the initializer-clause. */
12123 init = cp_parser_initializer_clause (parser, non_constant_p);
12124 }
12125 else if (token->type == CPP_OPEN_PAREN)
12126 init = cp_parser_parenthesized_expression_list (parser, false,
12127 /*cast_p=*/false,
12128 non_constant_p);
12129 else
12130 {
12131 /* Anything else is an error. */
12132 cp_parser_error (parser, "expected initializer");
12133 init = error_mark_node;
12134 }
12135
12136 return init;
12137 }
12138
12139 /* Parse an initializer-clause.
12140
12141 initializer-clause:
12142 assignment-expression
12143 { initializer-list , [opt] }
12144 { }
12145
12146 Returns an expression representing the initializer.
12147
12148 If the `assignment-expression' production is used the value
12149 returned is simply a representation for the expression.
12150
12151 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12152 the elements of the initializer-list (or NULL_TREE, if the last
12153 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12154 NULL_TREE. There is no way to detect whether or not the optional
12155 trailing `,' was provided. NON_CONSTANT_P is as for
12156 cp_parser_initializer. */
12157
12158 static tree
12159 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12160 {
12161 tree initializer;
12162
12163 /* Assume the expression is constant. */
12164 *non_constant_p = false;
12165
12166 /* If it is not a `{', then we are looking at an
12167 assignment-expression. */
12168 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12169 {
12170 initializer
12171 = cp_parser_constant_expression (parser,
12172 /*allow_non_constant_p=*/true,
12173 non_constant_p);
12174 if (!*non_constant_p)
12175 initializer = fold_non_dependent_expr (initializer);
12176 }
12177 else
12178 {
12179 /* Consume the `{' token. */
12180 cp_lexer_consume_token (parser->lexer);
12181 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12182 initializer = make_node (CONSTRUCTOR);
12183 /* If it's not a `}', then there is a non-trivial initializer. */
12184 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12185 {
12186 /* Parse the initializer list. */
12187 CONSTRUCTOR_ELTS (initializer)
12188 = cp_parser_initializer_list (parser, non_constant_p);
12189 /* A trailing `,' token is allowed. */
12190 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12191 cp_lexer_consume_token (parser->lexer);
12192 }
12193 /* Now, there should be a trailing `}'. */
12194 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12195 }
12196
12197 return initializer;
12198 }
12199
12200 /* Parse an initializer-list.
12201
12202 initializer-list:
12203 initializer-clause
12204 initializer-list , initializer-clause
12205
12206 GNU Extension:
12207
12208 initializer-list:
12209 identifier : initializer-clause
12210 initializer-list, identifier : initializer-clause
12211
12212 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12213 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12214 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12215 as for cp_parser_initializer. */
12216
12217 static tree
12218 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12219 {
12220 tree initializers = NULL_TREE;
12221
12222 /* Assume all of the expressions are constant. */
12223 *non_constant_p = false;
12224
12225 /* Parse the rest of the list. */
12226 while (true)
12227 {
12228 cp_token *token;
12229 tree identifier;
12230 tree initializer;
12231 bool clause_non_constant_p;
12232
12233 /* If the next token is an identifier and the following one is a
12234 colon, we are looking at the GNU designated-initializer
12235 syntax. */
12236 if (cp_parser_allow_gnu_extensions_p (parser)
12237 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12238 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12239 {
12240 /* Consume the identifier. */
12241 identifier = cp_lexer_consume_token (parser->lexer)->value;
12242 /* Consume the `:'. */
12243 cp_lexer_consume_token (parser->lexer);
12244 }
12245 else
12246 identifier = NULL_TREE;
12247
12248 /* Parse the initializer. */
12249 initializer = cp_parser_initializer_clause (parser,
12250 &clause_non_constant_p);
12251 /* If any clause is non-constant, so is the entire initializer. */
12252 if (clause_non_constant_p)
12253 *non_constant_p = true;
12254 /* Add it to the list. */
12255 initializers = tree_cons (identifier, initializer, initializers);
12256
12257 /* If the next token is not a comma, we have reached the end of
12258 the list. */
12259 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12260 break;
12261
12262 /* Peek at the next token. */
12263 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12264 /* If the next token is a `}', then we're still done. An
12265 initializer-clause can have a trailing `,' after the
12266 initializer-list and before the closing `}'. */
12267 if (token->type == CPP_CLOSE_BRACE)
12268 break;
12269
12270 /* Consume the `,' token. */
12271 cp_lexer_consume_token (parser->lexer);
12272 }
12273
12274 /* The initializers were built up in reverse order, so we need to
12275 reverse them now. */
12276 return nreverse (initializers);
12277 }
12278
12279 /* Classes [gram.class] */
12280
12281 /* Parse a class-name.
12282
12283 class-name:
12284 identifier
12285 template-id
12286
12287 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12288 to indicate that names looked up in dependent types should be
12289 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12290 keyword has been used to indicate that the name that appears next
12291 is a template. TAG_TYPE indicates the explicit tag given before
12292 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12293 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12294 is the class being defined in a class-head.
12295
12296 Returns the TYPE_DECL representing the class. */
12297
12298 static tree
12299 cp_parser_class_name (cp_parser *parser,
12300 bool typename_keyword_p,
12301 bool template_keyword_p,
12302 enum tag_types tag_type,
12303 bool check_dependency_p,
12304 bool class_head_p,
12305 bool is_declaration)
12306 {
12307 tree decl;
12308 tree scope;
12309 bool typename_p;
12310 cp_token *token;
12311
12312 /* All class-names start with an identifier. */
12313 token = cp_lexer_peek_token (parser->lexer);
12314 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12315 {
12316 cp_parser_error (parser, "expected class-name");
12317 return error_mark_node;
12318 }
12319
12320 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12321 to a template-id, so we save it here. */
12322 scope = parser->scope;
12323 if (scope == error_mark_node)
12324 return error_mark_node;
12325
12326 /* Any name names a type if we're following the `typename' keyword
12327 in a qualified name where the enclosing scope is type-dependent. */
12328 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12329 && dependent_type_p (scope));
12330 /* Handle the common case (an identifier, but not a template-id)
12331 efficiently. */
12332 if (token->type == CPP_NAME
12333 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12334 {
12335 tree identifier;
12336
12337 /* Look for the identifier. */
12338 identifier = cp_parser_identifier (parser);
12339 /* If the next token isn't an identifier, we are certainly not
12340 looking at a class-name. */
12341 if (identifier == error_mark_node)
12342 decl = error_mark_node;
12343 /* If we know this is a type-name, there's no need to look it
12344 up. */
12345 else if (typename_p)
12346 decl = identifier;
12347 else
12348 {
12349 /* If the next token is a `::', then the name must be a type
12350 name.
12351
12352 [basic.lookup.qual]
12353
12354 During the lookup for a name preceding the :: scope
12355 resolution operator, object, function, and enumerator
12356 names are ignored. */
12357 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12358 tag_type = typename_type;
12359 /* Look up the name. */
12360 decl = cp_parser_lookup_name (parser, identifier,
12361 tag_type,
12362 /*is_template=*/false,
12363 /*is_namespace=*/false,
12364 check_dependency_p,
12365 /*ambiguous_p=*/NULL);
12366 }
12367 }
12368 else
12369 {
12370 /* Try a template-id. */
12371 decl = cp_parser_template_id (parser, template_keyword_p,
12372 check_dependency_p,
12373 is_declaration);
12374 if (decl == error_mark_node)
12375 return error_mark_node;
12376 }
12377
12378 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12379
12380 /* If this is a typename, create a TYPENAME_TYPE. */
12381 if (typename_p && decl != error_mark_node)
12382 {
12383 decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12384 if (decl != error_mark_node)
12385 decl = TYPE_NAME (decl);
12386 }
12387
12388 /* Check to see that it is really the name of a class. */
12389 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12390 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12391 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12392 /* Situations like this:
12393
12394 template <typename T> struct A {
12395 typename T::template X<int>::I i;
12396 };
12397
12398 are problematic. Is `T::template X<int>' a class-name? The
12399 standard does not seem to be definitive, but there is no other
12400 valid interpretation of the following `::'. Therefore, those
12401 names are considered class-names. */
12402 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12403 else if (decl == error_mark_node
12404 || TREE_CODE (decl) != TYPE_DECL
12405 || TREE_TYPE (decl) == error_mark_node
12406 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12407 {
12408 cp_parser_error (parser, "expected class-name");
12409 return error_mark_node;
12410 }
12411
12412 return decl;
12413 }
12414
12415 /* Parse a class-specifier.
12416
12417 class-specifier:
12418 class-head { member-specification [opt] }
12419
12420 Returns the TREE_TYPE representing the class. */
12421
12422 static tree
12423 cp_parser_class_specifier (cp_parser* parser)
12424 {
12425 cp_token *token;
12426 tree type;
12427 tree attributes = NULL_TREE;
12428 int has_trailing_semicolon;
12429 bool nested_name_specifier_p;
12430 unsigned saved_num_template_parameter_lists;
12431 tree old_scope = NULL_TREE;
12432 tree scope = NULL_TREE;
12433
12434 push_deferring_access_checks (dk_no_deferred);
12435
12436 /* Parse the class-head. */
12437 type = cp_parser_class_head (parser,
12438 &nested_name_specifier_p,
12439 &attributes);
12440 /* If the class-head was a semantic disaster, skip the entire body
12441 of the class. */
12442 if (!type)
12443 {
12444 cp_parser_skip_to_end_of_block_or_statement (parser);
12445 pop_deferring_access_checks ();
12446 return error_mark_node;
12447 }
12448
12449 /* Look for the `{'. */
12450 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12451 {
12452 pop_deferring_access_checks ();
12453 return error_mark_node;
12454 }
12455
12456 /* Issue an error message if type-definitions are forbidden here. */
12457 cp_parser_check_type_definition (parser);
12458 /* Remember that we are defining one more class. */
12459 ++parser->num_classes_being_defined;
12460 /* Inside the class, surrounding template-parameter-lists do not
12461 apply. */
12462 saved_num_template_parameter_lists
12463 = parser->num_template_parameter_lists;
12464 parser->num_template_parameter_lists = 0;
12465
12466 /* Start the class. */
12467 if (nested_name_specifier_p)
12468 {
12469 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12470 old_scope = push_inner_scope (scope);
12471 }
12472 type = begin_class_definition (type);
12473
12474 if (type == error_mark_node)
12475 /* If the type is erroneous, skip the entire body of the class. */
12476 cp_parser_skip_to_closing_brace (parser);
12477 else
12478 /* Parse the member-specification. */
12479 cp_parser_member_specification_opt (parser);
12480
12481 /* Look for the trailing `}'. */
12482 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12483 /* We get better error messages by noticing a common problem: a
12484 missing trailing `;'. */
12485 token = cp_lexer_peek_token (parser->lexer);
12486 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12487 /* Look for trailing attributes to apply to this class. */
12488 if (cp_parser_allow_gnu_extensions_p (parser))
12489 {
12490 tree sub_attr = cp_parser_attributes_opt (parser);
12491 attributes = chainon (attributes, sub_attr);
12492 }
12493 if (type != error_mark_node)
12494 type = finish_struct (type, attributes);
12495 if (nested_name_specifier_p)
12496 pop_inner_scope (old_scope, scope);
12497 /* If this class is not itself within the scope of another class,
12498 then we need to parse the bodies of all of the queued function
12499 definitions. Note that the queued functions defined in a class
12500 are not always processed immediately following the
12501 class-specifier for that class. Consider:
12502
12503 struct A {
12504 struct B { void f() { sizeof (A); } };
12505 };
12506
12507 If `f' were processed before the processing of `A' were
12508 completed, there would be no way to compute the size of `A'.
12509 Note that the nesting we are interested in here is lexical --
12510 not the semantic nesting given by TYPE_CONTEXT. In particular,
12511 for:
12512
12513 struct A { struct B; };
12514 struct A::B { void f() { } };
12515
12516 there is no need to delay the parsing of `A::B::f'. */
12517 if (--parser->num_classes_being_defined == 0)
12518 {
12519 tree queue_entry;
12520 tree fn;
12521 tree class_type = NULL_TREE;
12522 tree pushed_scope = NULL_TREE;
12523
12524 /* In a first pass, parse default arguments to the functions.
12525 Then, in a second pass, parse the bodies of the functions.
12526 This two-phased approach handles cases like:
12527
12528 struct S {
12529 void f() { g(); }
12530 void g(int i = 3);
12531 };
12532
12533 */
12534 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12535 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12536 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12537 TREE_PURPOSE (parser->unparsed_functions_queues)
12538 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12539 {
12540 fn = TREE_VALUE (queue_entry);
12541 /* If there are default arguments that have not yet been processed,
12542 take care of them now. */
12543 if (class_type != TREE_PURPOSE (queue_entry))
12544 {
12545 if (pushed_scope)
12546 pop_scope (pushed_scope);
12547 class_type = TREE_PURPOSE (queue_entry);
12548 pushed_scope = push_scope (class_type);
12549 }
12550 /* Make sure that any template parameters are in scope. */
12551 maybe_begin_member_template_processing (fn);
12552 /* Parse the default argument expressions. */
12553 cp_parser_late_parsing_default_args (parser, fn);
12554 /* Remove any template parameters from the symbol table. */
12555 maybe_end_member_template_processing ();
12556 }
12557 if (pushed_scope)
12558 pop_scope (pushed_scope);
12559 /* Now parse the body of the functions. */
12560 for (TREE_VALUE (parser->unparsed_functions_queues)
12561 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12562 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12563 TREE_VALUE (parser->unparsed_functions_queues)
12564 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12565 {
12566 /* Figure out which function we need to process. */
12567 fn = TREE_VALUE (queue_entry);
12568
12569 /* A hack to prevent garbage collection. */
12570 function_depth++;
12571
12572 /* Parse the function. */
12573 cp_parser_late_parsing_for_member (parser, fn);
12574 function_depth--;
12575 }
12576 }
12577
12578 /* Put back any saved access checks. */
12579 pop_deferring_access_checks ();
12580
12581 /* Restore the count of active template-parameter-lists. */
12582 parser->num_template_parameter_lists
12583 = saved_num_template_parameter_lists;
12584
12585 return type;
12586 }
12587
12588 /* Parse a class-head.
12589
12590 class-head:
12591 class-key identifier [opt] base-clause [opt]
12592 class-key nested-name-specifier identifier base-clause [opt]
12593 class-key nested-name-specifier [opt] template-id
12594 base-clause [opt]
12595
12596 GNU Extensions:
12597 class-key attributes identifier [opt] base-clause [opt]
12598 class-key attributes nested-name-specifier identifier base-clause [opt]
12599 class-key attributes nested-name-specifier [opt] template-id
12600 base-clause [opt]
12601
12602 Returns the TYPE of the indicated class. Sets
12603 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12604 involving a nested-name-specifier was used, and FALSE otherwise.
12605
12606 Returns error_mark_node if this is not a class-head.
12607
12608 Returns NULL_TREE if the class-head is syntactically valid, but
12609 semantically invalid in a way that means we should skip the entire
12610 body of the class. */
12611
12612 static tree
12613 cp_parser_class_head (cp_parser* parser,
12614 bool* nested_name_specifier_p,
12615 tree *attributes_p)
12616 {
12617 tree nested_name_specifier;
12618 enum tag_types class_key;
12619 tree id = NULL_TREE;
12620 tree type = NULL_TREE;
12621 tree attributes;
12622 bool template_id_p = false;
12623 bool qualified_p = false;
12624 bool invalid_nested_name_p = false;
12625 bool invalid_explicit_specialization_p = false;
12626 tree pushed_scope = NULL_TREE;
12627 unsigned num_templates;
12628 tree bases;
12629
12630 /* Assume no nested-name-specifier will be present. */
12631 *nested_name_specifier_p = false;
12632 /* Assume no template parameter lists will be used in defining the
12633 type. */
12634 num_templates = 0;
12635
12636 /* Look for the class-key. */
12637 class_key = cp_parser_class_key (parser);
12638 if (class_key == none_type)
12639 return error_mark_node;
12640
12641 /* Parse the attributes. */
12642 attributes = cp_parser_attributes_opt (parser);
12643
12644 /* If the next token is `::', that is invalid -- but sometimes
12645 people do try to write:
12646
12647 struct ::S {};
12648
12649 Handle this gracefully by accepting the extra qualifier, and then
12650 issuing an error about it later if this really is a
12651 class-head. If it turns out just to be an elaborated type
12652 specifier, remain silent. */
12653 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12654 qualified_p = true;
12655
12656 push_deferring_access_checks (dk_no_check);
12657
12658 /* Determine the name of the class. Begin by looking for an
12659 optional nested-name-specifier. */
12660 nested_name_specifier
12661 = cp_parser_nested_name_specifier_opt (parser,
12662 /*typename_keyword_p=*/false,
12663 /*check_dependency_p=*/false,
12664 /*type_p=*/false,
12665 /*is_declaration=*/false);
12666 /* If there was a nested-name-specifier, then there *must* be an
12667 identifier. */
12668 if (nested_name_specifier)
12669 {
12670 /* Although the grammar says `identifier', it really means
12671 `class-name' or `template-name'. You are only allowed to
12672 define a class that has already been declared with this
12673 syntax.
12674
12675 The proposed resolution for Core Issue 180 says that whever
12676 you see `class T::X' you should treat `X' as a type-name.
12677
12678 It is OK to define an inaccessible class; for example:
12679
12680 class A { class B; };
12681 class A::B {};
12682
12683 We do not know if we will see a class-name, or a
12684 template-name. We look for a class-name first, in case the
12685 class-name is a template-id; if we looked for the
12686 template-name first we would stop after the template-name. */
12687 cp_parser_parse_tentatively (parser);
12688 type = cp_parser_class_name (parser,
12689 /*typename_keyword_p=*/false,
12690 /*template_keyword_p=*/false,
12691 class_type,
12692 /*check_dependency_p=*/false,
12693 /*class_head_p=*/true,
12694 /*is_declaration=*/false);
12695 /* If that didn't work, ignore the nested-name-specifier. */
12696 if (!cp_parser_parse_definitely (parser))
12697 {
12698 invalid_nested_name_p = true;
12699 id = cp_parser_identifier (parser);
12700 if (id == error_mark_node)
12701 id = NULL_TREE;
12702 }
12703 /* If we could not find a corresponding TYPE, treat this
12704 declaration like an unqualified declaration. */
12705 if (type == error_mark_node)
12706 nested_name_specifier = NULL_TREE;
12707 /* Otherwise, count the number of templates used in TYPE and its
12708 containing scopes. */
12709 else
12710 {
12711 tree scope;
12712
12713 for (scope = TREE_TYPE (type);
12714 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12715 scope = (TYPE_P (scope)
12716 ? TYPE_CONTEXT (scope)
12717 : DECL_CONTEXT (scope)))
12718 if (TYPE_P (scope)
12719 && CLASS_TYPE_P (scope)
12720 && CLASSTYPE_TEMPLATE_INFO (scope)
12721 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12722 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12723 ++num_templates;
12724 }
12725 }
12726 /* Otherwise, the identifier is optional. */
12727 else
12728 {
12729 /* We don't know whether what comes next is a template-id,
12730 an identifier, or nothing at all. */
12731 cp_parser_parse_tentatively (parser);
12732 /* Check for a template-id. */
12733 id = cp_parser_template_id (parser,
12734 /*template_keyword_p=*/false,
12735 /*check_dependency_p=*/true,
12736 /*is_declaration=*/true);
12737 /* If that didn't work, it could still be an identifier. */
12738 if (!cp_parser_parse_definitely (parser))
12739 {
12740 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12741 id = cp_parser_identifier (parser);
12742 else
12743 id = NULL_TREE;
12744 }
12745 else
12746 {
12747 template_id_p = true;
12748 ++num_templates;
12749 }
12750 }
12751
12752 pop_deferring_access_checks ();
12753
12754 if (id)
12755 cp_parser_check_for_invalid_template_id (parser, id);
12756
12757 /* If it's not a `:' or a `{' then we can't really be looking at a
12758 class-head, since a class-head only appears as part of a
12759 class-specifier. We have to detect this situation before calling
12760 xref_tag, since that has irreversible side-effects. */
12761 if (!cp_parser_next_token_starts_class_definition_p (parser))
12762 {
12763 cp_parser_error (parser, "expected %<{%> or %<:%>");
12764 return error_mark_node;
12765 }
12766
12767 /* At this point, we're going ahead with the class-specifier, even
12768 if some other problem occurs. */
12769 cp_parser_commit_to_tentative_parse (parser);
12770 /* Issue the error about the overly-qualified name now. */
12771 if (qualified_p)
12772 cp_parser_error (parser,
12773 "global qualification of class name is invalid");
12774 else if (invalid_nested_name_p)
12775 cp_parser_error (parser,
12776 "qualified name does not name a class");
12777 else if (nested_name_specifier)
12778 {
12779 tree scope;
12780
12781 /* Reject typedef-names in class heads. */
12782 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12783 {
12784 error ("invalid class name in declaration of %qD", type);
12785 type = NULL_TREE;
12786 goto done;
12787 }
12788
12789 /* Figure out in what scope the declaration is being placed. */
12790 scope = current_scope ();
12791 /* If that scope does not contain the scope in which the
12792 class was originally declared, the program is invalid. */
12793 if (scope && !is_ancestor (scope, nested_name_specifier))
12794 {
12795 error ("declaration of %qD in %qD which does not enclose %qD",
12796 type, scope, nested_name_specifier);
12797 type = NULL_TREE;
12798 goto done;
12799 }
12800 /* [dcl.meaning]
12801
12802 A declarator-id shall not be qualified exception of the
12803 definition of a ... nested class outside of its class
12804 ... [or] a the definition or explicit instantiation of a
12805 class member of a namespace outside of its namespace. */
12806 if (scope == nested_name_specifier)
12807 {
12808 pedwarn ("extra qualification ignored");
12809 nested_name_specifier = NULL_TREE;
12810 num_templates = 0;
12811 }
12812 }
12813 /* An explicit-specialization must be preceded by "template <>". If
12814 it is not, try to recover gracefully. */
12815 if (at_namespace_scope_p ()
12816 && parser->num_template_parameter_lists == 0
12817 && template_id_p)
12818 {
12819 error ("an explicit specialization must be preceded by %<template <>%>");
12820 invalid_explicit_specialization_p = true;
12821 /* Take the same action that would have been taken by
12822 cp_parser_explicit_specialization. */
12823 ++parser->num_template_parameter_lists;
12824 begin_specialization ();
12825 }
12826 /* There must be no "return" statements between this point and the
12827 end of this function; set "type "to the correct return value and
12828 use "goto done;" to return. */
12829 /* Make sure that the right number of template parameters were
12830 present. */
12831 if (!cp_parser_check_template_parameters (parser, num_templates))
12832 {
12833 /* If something went wrong, there is no point in even trying to
12834 process the class-definition. */
12835 type = NULL_TREE;
12836 goto done;
12837 }
12838
12839 /* Look up the type. */
12840 if (template_id_p)
12841 {
12842 type = TREE_TYPE (id);
12843 maybe_process_partial_specialization (type);
12844 if (nested_name_specifier)
12845 pushed_scope = push_scope (nested_name_specifier);
12846 }
12847 else if (nested_name_specifier)
12848 {
12849 tree class_type;
12850
12851 /* Given:
12852
12853 template <typename T> struct S { struct T };
12854 template <typename T> struct S<T>::T { };
12855
12856 we will get a TYPENAME_TYPE when processing the definition of
12857 `S::T'. We need to resolve it to the actual type before we
12858 try to define it. */
12859 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12860 {
12861 class_type = resolve_typename_type (TREE_TYPE (type),
12862 /*only_current_p=*/false);
12863 if (class_type != error_mark_node)
12864 type = TYPE_NAME (class_type);
12865 else
12866 {
12867 cp_parser_error (parser, "could not resolve typename type");
12868 type = error_mark_node;
12869 }
12870 }
12871
12872 maybe_process_partial_specialization (TREE_TYPE (type));
12873 class_type = current_class_type;
12874 /* Enter the scope indicated by the nested-name-specifier. */
12875 pushed_scope = push_scope (nested_name_specifier);
12876 /* Get the canonical version of this type. */
12877 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12878 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12879 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12880 {
12881 type = push_template_decl (type);
12882 if (type == error_mark_node)
12883 {
12884 type = NULL_TREE;
12885 goto done;
12886 }
12887 }
12888
12889 type = TREE_TYPE (type);
12890 *nested_name_specifier_p = true;
12891 }
12892 else /* The name is not a nested name. */
12893 {
12894 /* If the class was unnamed, create a dummy name. */
12895 if (!id)
12896 id = make_anon_name ();
12897 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
12898 parser->num_template_parameter_lists);
12899 }
12900
12901 /* Indicate whether this class was declared as a `class' or as a
12902 `struct'. */
12903 if (TREE_CODE (type) == RECORD_TYPE)
12904 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12905 cp_parser_check_class_key (class_key, type);
12906
12907 /* If this type was already complete, and we see another definition,
12908 that's an error. */
12909 if (type != error_mark_node && COMPLETE_TYPE_P (type))
12910 {
12911 error ("redefinition of %q#T", type);
12912 cp_error_at ("previous definition of %q#T", type);
12913 type = NULL_TREE;
12914 goto done;
12915 }
12916
12917 /* We will have entered the scope containing the class; the names of
12918 base classes should be looked up in that context. For example:
12919
12920 struct A { struct B {}; struct C; };
12921 struct A::C : B {};
12922
12923 is valid. */
12924 bases = NULL_TREE;
12925
12926 /* Get the list of base-classes, if there is one. */
12927 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12928 bases = cp_parser_base_clause (parser);
12929
12930 /* Process the base classes. */
12931 xref_basetypes (type, bases);
12932
12933 done:
12934 /* Leave the scope given by the nested-name-specifier. We will
12935 enter the class scope itself while processing the members. */
12936 if (pushed_scope)
12937 pop_scope (pushed_scope);
12938
12939 if (invalid_explicit_specialization_p)
12940 {
12941 end_specialization ();
12942 --parser->num_template_parameter_lists;
12943 }
12944 *attributes_p = attributes;
12945 return type;
12946 }
12947
12948 /* Parse a class-key.
12949
12950 class-key:
12951 class
12952 struct
12953 union
12954
12955 Returns the kind of class-key specified, or none_type to indicate
12956 error. */
12957
12958 static enum tag_types
12959 cp_parser_class_key (cp_parser* parser)
12960 {
12961 cp_token *token;
12962 enum tag_types tag_type;
12963
12964 /* Look for the class-key. */
12965 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12966 if (!token)
12967 return none_type;
12968
12969 /* Check to see if the TOKEN is a class-key. */
12970 tag_type = cp_parser_token_is_class_key (token);
12971 if (!tag_type)
12972 cp_parser_error (parser, "expected class-key");
12973 return tag_type;
12974 }
12975
12976 /* Parse an (optional) member-specification.
12977
12978 member-specification:
12979 member-declaration member-specification [opt]
12980 access-specifier : member-specification [opt] */
12981
12982 static void
12983 cp_parser_member_specification_opt (cp_parser* parser)
12984 {
12985 while (true)
12986 {
12987 cp_token *token;
12988 enum rid keyword;
12989
12990 /* Peek at the next token. */
12991 token = cp_lexer_peek_token (parser->lexer);
12992 /* If it's a `}', or EOF then we've seen all the members. */
12993 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12994 break;
12995
12996 /* See if this token is a keyword. */
12997 keyword = token->keyword;
12998 switch (keyword)
12999 {
13000 case RID_PUBLIC:
13001 case RID_PROTECTED:
13002 case RID_PRIVATE:
13003 /* Consume the access-specifier. */
13004 cp_lexer_consume_token (parser->lexer);
13005 /* Remember which access-specifier is active. */
13006 current_access_specifier = token->value;
13007 /* Look for the `:'. */
13008 cp_parser_require (parser, CPP_COLON, "`:'");
13009 break;
13010
13011 default:
13012 /* Accept #pragmas at class scope. */
13013 if (token->type == CPP_PRAGMA)
13014 {
13015 cp_lexer_handle_pragma (parser->lexer);
13016 break;
13017 }
13018
13019 /* Otherwise, the next construction must be a
13020 member-declaration. */
13021 cp_parser_member_declaration (parser);
13022 }
13023 }
13024 }
13025
13026 /* Parse a member-declaration.
13027
13028 member-declaration:
13029 decl-specifier-seq [opt] member-declarator-list [opt] ;
13030 function-definition ; [opt]
13031 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13032 using-declaration
13033 template-declaration
13034
13035 member-declarator-list:
13036 member-declarator
13037 member-declarator-list , member-declarator
13038
13039 member-declarator:
13040 declarator pure-specifier [opt]
13041 declarator constant-initializer [opt]
13042 identifier [opt] : constant-expression
13043
13044 GNU Extensions:
13045
13046 member-declaration:
13047 __extension__ member-declaration
13048
13049 member-declarator:
13050 declarator attributes [opt] pure-specifier [opt]
13051 declarator attributes [opt] constant-initializer [opt]
13052 identifier [opt] attributes [opt] : constant-expression */
13053
13054 static void
13055 cp_parser_member_declaration (cp_parser* parser)
13056 {
13057 cp_decl_specifier_seq decl_specifiers;
13058 tree prefix_attributes;
13059 tree decl;
13060 int declares_class_or_enum;
13061 bool friend_p;
13062 cp_token *token;
13063 int saved_pedantic;
13064
13065 /* Check for the `__extension__' keyword. */
13066 if (cp_parser_extension_opt (parser, &saved_pedantic))
13067 {
13068 /* Recurse. */
13069 cp_parser_member_declaration (parser);
13070 /* Restore the old value of the PEDANTIC flag. */
13071 pedantic = saved_pedantic;
13072
13073 return;
13074 }
13075
13076 /* Check for a template-declaration. */
13077 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13078 {
13079 /* Parse the template-declaration. */
13080 cp_parser_template_declaration (parser, /*member_p=*/true);
13081
13082 return;
13083 }
13084
13085 /* Check for a using-declaration. */
13086 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13087 {
13088 /* Parse the using-declaration. */
13089 cp_parser_using_declaration (parser);
13090
13091 return;
13092 }
13093
13094 /* Parse the decl-specifier-seq. */
13095 cp_parser_decl_specifier_seq (parser,
13096 CP_PARSER_FLAGS_OPTIONAL,
13097 &decl_specifiers,
13098 &declares_class_or_enum);
13099 prefix_attributes = decl_specifiers.attributes;
13100 decl_specifiers.attributes = NULL_TREE;
13101 /* Check for an invalid type-name. */
13102 if (!decl_specifiers.type
13103 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13104 return;
13105 /* If there is no declarator, then the decl-specifier-seq should
13106 specify a type. */
13107 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13108 {
13109 /* If there was no decl-specifier-seq, and the next token is a
13110 `;', then we have something like:
13111
13112 struct S { ; };
13113
13114 [class.mem]
13115
13116 Each member-declaration shall declare at least one member
13117 name of the class. */
13118 if (!decl_specifiers.any_specifiers_p)
13119 {
13120 cp_token *token = cp_lexer_peek_token (parser->lexer);
13121 if (pedantic && !token->in_system_header)
13122 pedwarn ("%Hextra %<;%>", &token->location);
13123 }
13124 else
13125 {
13126 tree type;
13127
13128 /* See if this declaration is a friend. */
13129 friend_p = cp_parser_friend_p (&decl_specifiers);
13130 /* If there were decl-specifiers, check to see if there was
13131 a class-declaration. */
13132 type = check_tag_decl (&decl_specifiers);
13133 /* Nested classes have already been added to the class, but
13134 a `friend' needs to be explicitly registered. */
13135 if (friend_p)
13136 {
13137 /* If the `friend' keyword was present, the friend must
13138 be introduced with a class-key. */
13139 if (!declares_class_or_enum)
13140 error ("a class-key must be used when declaring a friend");
13141 /* In this case:
13142
13143 template <typename T> struct A {
13144 friend struct A<T>::B;
13145 };
13146
13147 A<T>::B will be represented by a TYPENAME_TYPE, and
13148 therefore not recognized by check_tag_decl. */
13149 if (!type
13150 && decl_specifiers.type
13151 && TYPE_P (decl_specifiers.type))
13152 type = decl_specifiers.type;
13153 if (!type || !TYPE_P (type))
13154 error ("friend declaration does not name a class or "
13155 "function");
13156 else
13157 make_friend_class (current_class_type, type,
13158 /*complain=*/true);
13159 }
13160 /* If there is no TYPE, an error message will already have
13161 been issued. */
13162 else if (!type || type == error_mark_node)
13163 ;
13164 /* An anonymous aggregate has to be handled specially; such
13165 a declaration really declares a data member (with a
13166 particular type), as opposed to a nested class. */
13167 else if (ANON_AGGR_TYPE_P (type))
13168 {
13169 /* Remove constructors and such from TYPE, now that we
13170 know it is an anonymous aggregate. */
13171 fixup_anonymous_aggr (type);
13172 /* And make the corresponding data member. */
13173 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13174 /* Add it to the class. */
13175 finish_member_declaration (decl);
13176 }
13177 else
13178 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13179 }
13180 }
13181 else
13182 {
13183 /* See if these declarations will be friends. */
13184 friend_p = cp_parser_friend_p (&decl_specifiers);
13185
13186 /* Keep going until we hit the `;' at the end of the
13187 declaration. */
13188 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13189 {
13190 tree attributes = NULL_TREE;
13191 tree first_attribute;
13192
13193 /* Peek at the next token. */
13194 token = cp_lexer_peek_token (parser->lexer);
13195
13196 /* Check for a bitfield declaration. */
13197 if (token->type == CPP_COLON
13198 || (token->type == CPP_NAME
13199 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13200 == CPP_COLON))
13201 {
13202 tree identifier;
13203 tree width;
13204
13205 /* Get the name of the bitfield. Note that we cannot just
13206 check TOKEN here because it may have been invalidated by
13207 the call to cp_lexer_peek_nth_token above. */
13208 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13209 identifier = cp_parser_identifier (parser);
13210 else
13211 identifier = NULL_TREE;
13212
13213 /* Consume the `:' token. */
13214 cp_lexer_consume_token (parser->lexer);
13215 /* Get the width of the bitfield. */
13216 width
13217 = cp_parser_constant_expression (parser,
13218 /*allow_non_constant=*/false,
13219 NULL);
13220
13221 /* Look for attributes that apply to the bitfield. */
13222 attributes = cp_parser_attributes_opt (parser);
13223 /* Remember which attributes are prefix attributes and
13224 which are not. */
13225 first_attribute = attributes;
13226 /* Combine the attributes. */
13227 attributes = chainon (prefix_attributes, attributes);
13228
13229 /* Create the bitfield declaration. */
13230 decl = grokbitfield (identifier
13231 ? make_id_declarator (NULL_TREE,
13232 identifier)
13233 : NULL,
13234 &decl_specifiers,
13235 width);
13236 /* Apply the attributes. */
13237 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13238 }
13239 else
13240 {
13241 cp_declarator *declarator;
13242 tree initializer;
13243 tree asm_specification;
13244 int ctor_dtor_or_conv_p;
13245
13246 /* Parse the declarator. */
13247 declarator
13248 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13249 &ctor_dtor_or_conv_p,
13250 /*parenthesized_p=*/NULL,
13251 /*member_p=*/true);
13252
13253 /* If something went wrong parsing the declarator, make sure
13254 that we at least consume some tokens. */
13255 if (declarator == cp_error_declarator)
13256 {
13257 /* Skip to the end of the statement. */
13258 cp_parser_skip_to_end_of_statement (parser);
13259 /* If the next token is not a semicolon, that is
13260 probably because we just skipped over the body of
13261 a function. So, we consume a semicolon if
13262 present, but do not issue an error message if it
13263 is not present. */
13264 if (cp_lexer_next_token_is (parser->lexer,
13265 CPP_SEMICOLON))
13266 cp_lexer_consume_token (parser->lexer);
13267 return;
13268 }
13269
13270 if (declares_class_or_enum & 2)
13271 cp_parser_check_for_definition_in_return_type
13272 (declarator, decl_specifiers.type);
13273
13274 /* Look for an asm-specification. */
13275 asm_specification = cp_parser_asm_specification_opt (parser);
13276 /* Look for attributes that apply to the declaration. */
13277 attributes = cp_parser_attributes_opt (parser);
13278 /* Remember which attributes are prefix attributes and
13279 which are not. */
13280 first_attribute = attributes;
13281 /* Combine the attributes. */
13282 attributes = chainon (prefix_attributes, attributes);
13283
13284 /* If it's an `=', then we have a constant-initializer or a
13285 pure-specifier. It is not correct to parse the
13286 initializer before registering the member declaration
13287 since the member declaration should be in scope while
13288 its initializer is processed. However, the rest of the
13289 front end does not yet provide an interface that allows
13290 us to handle this correctly. */
13291 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13292 {
13293 /* In [class.mem]:
13294
13295 A pure-specifier shall be used only in the declaration of
13296 a virtual function.
13297
13298 A member-declarator can contain a constant-initializer
13299 only if it declares a static member of integral or
13300 enumeration type.
13301
13302 Therefore, if the DECLARATOR is for a function, we look
13303 for a pure-specifier; otherwise, we look for a
13304 constant-initializer. When we call `grokfield', it will
13305 perform more stringent semantics checks. */
13306 if (declarator->kind == cdk_function)
13307 initializer = cp_parser_pure_specifier (parser);
13308 else
13309 /* Parse the initializer. */
13310 initializer = cp_parser_constant_initializer (parser);
13311 }
13312 /* Otherwise, there is no initializer. */
13313 else
13314 initializer = NULL_TREE;
13315
13316 /* See if we are probably looking at a function
13317 definition. We are certainly not looking at a
13318 member-declarator. Calling `grokfield' has
13319 side-effects, so we must not do it unless we are sure
13320 that we are looking at a member-declarator. */
13321 if (cp_parser_token_starts_function_definition_p
13322 (cp_lexer_peek_token (parser->lexer)))
13323 {
13324 /* The grammar does not allow a pure-specifier to be
13325 used when a member function is defined. (It is
13326 possible that this fact is an oversight in the
13327 standard, since a pure function may be defined
13328 outside of the class-specifier. */
13329 if (initializer)
13330 error ("pure-specifier on function-definition");
13331 decl = cp_parser_save_member_function_body (parser,
13332 &decl_specifiers,
13333 declarator,
13334 attributes);
13335 /* If the member was not a friend, declare it here. */
13336 if (!friend_p)
13337 finish_member_declaration (decl);
13338 /* Peek at the next token. */
13339 token = cp_lexer_peek_token (parser->lexer);
13340 /* If the next token is a semicolon, consume it. */
13341 if (token->type == CPP_SEMICOLON)
13342 cp_lexer_consume_token (parser->lexer);
13343 return;
13344 }
13345 else
13346 {
13347 /* Create the declaration. */
13348 decl = grokfield (declarator, &decl_specifiers,
13349 initializer, asm_specification,
13350 attributes);
13351 /* Any initialization must have been from a
13352 constant-expression. */
13353 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13354 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13355 }
13356 }
13357
13358 /* Reset PREFIX_ATTRIBUTES. */
13359 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13360 attributes = TREE_CHAIN (attributes);
13361 if (attributes)
13362 TREE_CHAIN (attributes) = NULL_TREE;
13363
13364 /* If there is any qualification still in effect, clear it
13365 now; we will be starting fresh with the next declarator. */
13366 parser->scope = NULL_TREE;
13367 parser->qualifying_scope = NULL_TREE;
13368 parser->object_scope = NULL_TREE;
13369 /* If it's a `,', then there are more declarators. */
13370 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13371 cp_lexer_consume_token (parser->lexer);
13372 /* If the next token isn't a `;', then we have a parse error. */
13373 else if (cp_lexer_next_token_is_not (parser->lexer,
13374 CPP_SEMICOLON))
13375 {
13376 cp_parser_error (parser, "expected %<;%>");
13377 /* Skip tokens until we find a `;'. */
13378 cp_parser_skip_to_end_of_statement (parser);
13379
13380 break;
13381 }
13382
13383 if (decl)
13384 {
13385 /* Add DECL to the list of members. */
13386 if (!friend_p)
13387 finish_member_declaration (decl);
13388
13389 if (TREE_CODE (decl) == FUNCTION_DECL)
13390 cp_parser_save_default_args (parser, decl);
13391 }
13392 }
13393 }
13394
13395 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13396 }
13397
13398 /* Parse a pure-specifier.
13399
13400 pure-specifier:
13401 = 0
13402
13403 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13404 Otherwise, ERROR_MARK_NODE is returned. */
13405
13406 static tree
13407 cp_parser_pure_specifier (cp_parser* parser)
13408 {
13409 cp_token *token;
13410
13411 /* Look for the `=' token. */
13412 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13413 return error_mark_node;
13414 /* Look for the `0' token. */
13415 token = cp_lexer_consume_token (parser->lexer);
13416 if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13417 {
13418 cp_parser_error (parser,
13419 "invalid pure specifier (only `= 0' is allowed)");
13420 cp_parser_skip_to_end_of_statement (parser);
13421 return error_mark_node;
13422 }
13423
13424 /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13425 We need to get information from the lexer about how the number
13426 was spelled in order to fix this problem. */
13427 return integer_zero_node;
13428 }
13429
13430 /* Parse a constant-initializer.
13431
13432 constant-initializer:
13433 = constant-expression
13434
13435 Returns a representation of the constant-expression. */
13436
13437 static tree
13438 cp_parser_constant_initializer (cp_parser* parser)
13439 {
13440 /* Look for the `=' token. */
13441 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13442 return error_mark_node;
13443
13444 /* It is invalid to write:
13445
13446 struct S { static const int i = { 7 }; };
13447
13448 */
13449 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13450 {
13451 cp_parser_error (parser,
13452 "a brace-enclosed initializer is not allowed here");
13453 /* Consume the opening brace. */
13454 cp_lexer_consume_token (parser->lexer);
13455 /* Skip the initializer. */
13456 cp_parser_skip_to_closing_brace (parser);
13457 /* Look for the trailing `}'. */
13458 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13459
13460 return error_mark_node;
13461 }
13462
13463 return cp_parser_constant_expression (parser,
13464 /*allow_non_constant=*/false,
13465 NULL);
13466 }
13467
13468 /* Derived classes [gram.class.derived] */
13469
13470 /* Parse a base-clause.
13471
13472 base-clause:
13473 : base-specifier-list
13474
13475 base-specifier-list:
13476 base-specifier
13477 base-specifier-list , base-specifier
13478
13479 Returns a TREE_LIST representing the base-classes, in the order in
13480 which they were declared. The representation of each node is as
13481 described by cp_parser_base_specifier.
13482
13483 In the case that no bases are specified, this function will return
13484 NULL_TREE, not ERROR_MARK_NODE. */
13485
13486 static tree
13487 cp_parser_base_clause (cp_parser* parser)
13488 {
13489 tree bases = NULL_TREE;
13490
13491 /* Look for the `:' that begins the list. */
13492 cp_parser_require (parser, CPP_COLON, "`:'");
13493
13494 /* Scan the base-specifier-list. */
13495 while (true)
13496 {
13497 cp_token *token;
13498 tree base;
13499
13500 /* Look for the base-specifier. */
13501 base = cp_parser_base_specifier (parser);
13502 /* Add BASE to the front of the list. */
13503 if (base != error_mark_node)
13504 {
13505 TREE_CHAIN (base) = bases;
13506 bases = base;
13507 }
13508 /* Peek at the next token. */
13509 token = cp_lexer_peek_token (parser->lexer);
13510 /* If it's not a comma, then the list is complete. */
13511 if (token->type != CPP_COMMA)
13512 break;
13513 /* Consume the `,'. */
13514 cp_lexer_consume_token (parser->lexer);
13515 }
13516
13517 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13518 base class had a qualified name. However, the next name that
13519 appears is certainly not qualified. */
13520 parser->scope = NULL_TREE;
13521 parser->qualifying_scope = NULL_TREE;
13522 parser->object_scope = NULL_TREE;
13523
13524 return nreverse (bases);
13525 }
13526
13527 /* Parse a base-specifier.
13528
13529 base-specifier:
13530 :: [opt] nested-name-specifier [opt] class-name
13531 virtual access-specifier [opt] :: [opt] nested-name-specifier
13532 [opt] class-name
13533 access-specifier virtual [opt] :: [opt] nested-name-specifier
13534 [opt] class-name
13535
13536 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13537 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13538 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13539 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13540
13541 static tree
13542 cp_parser_base_specifier (cp_parser* parser)
13543 {
13544 cp_token *token;
13545 bool done = false;
13546 bool virtual_p = false;
13547 bool duplicate_virtual_error_issued_p = false;
13548 bool duplicate_access_error_issued_p = false;
13549 bool class_scope_p, template_p;
13550 tree access = access_default_node;
13551 tree type;
13552
13553 /* Process the optional `virtual' and `access-specifier'. */
13554 while (!done)
13555 {
13556 /* Peek at the next token. */
13557 token = cp_lexer_peek_token (parser->lexer);
13558 /* Process `virtual'. */
13559 switch (token->keyword)
13560 {
13561 case RID_VIRTUAL:
13562 /* If `virtual' appears more than once, issue an error. */
13563 if (virtual_p && !duplicate_virtual_error_issued_p)
13564 {
13565 cp_parser_error (parser,
13566 "%<virtual%> specified more than once in base-specified");
13567 duplicate_virtual_error_issued_p = true;
13568 }
13569
13570 virtual_p = true;
13571
13572 /* Consume the `virtual' token. */
13573 cp_lexer_consume_token (parser->lexer);
13574
13575 break;
13576
13577 case RID_PUBLIC:
13578 case RID_PROTECTED:
13579 case RID_PRIVATE:
13580 /* If more than one access specifier appears, issue an
13581 error. */
13582 if (access != access_default_node
13583 && !duplicate_access_error_issued_p)
13584 {
13585 cp_parser_error (parser,
13586 "more than one access specifier in base-specified");
13587 duplicate_access_error_issued_p = true;
13588 }
13589
13590 access = ridpointers[(int) token->keyword];
13591
13592 /* Consume the access-specifier. */
13593 cp_lexer_consume_token (parser->lexer);
13594
13595 break;
13596
13597 default:
13598 done = true;
13599 break;
13600 }
13601 }
13602 /* It is not uncommon to see programs mechanically, erroneously, use
13603 the 'typename' keyword to denote (dependent) qualified types
13604 as base classes. */
13605 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13606 {
13607 if (!processing_template_decl)
13608 error ("keyword %<typename%> not allowed outside of templates");
13609 else
13610 error ("keyword %<typename%> not allowed in this context "
13611 "(the base class is implicitly a type)");
13612 cp_lexer_consume_token (parser->lexer);
13613 }
13614
13615 /* Look for the optional `::' operator. */
13616 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13617 /* Look for the nested-name-specifier. The simplest way to
13618 implement:
13619
13620 [temp.res]
13621
13622 The keyword `typename' is not permitted in a base-specifier or
13623 mem-initializer; in these contexts a qualified name that
13624 depends on a template-parameter is implicitly assumed to be a
13625 type name.
13626
13627 is to pretend that we have seen the `typename' keyword at this
13628 point. */
13629 cp_parser_nested_name_specifier_opt (parser,
13630 /*typename_keyword_p=*/true,
13631 /*check_dependency_p=*/true,
13632 typename_type,
13633 /*is_declaration=*/true);
13634 /* If the base class is given by a qualified name, assume that names
13635 we see are type names or templates, as appropriate. */
13636 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13637 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13638
13639 /* Finally, look for the class-name. */
13640 type = cp_parser_class_name (parser,
13641 class_scope_p,
13642 template_p,
13643 typename_type,
13644 /*check_dependency_p=*/true,
13645 /*class_head_p=*/false,
13646 /*is_declaration=*/true);
13647
13648 if (type == error_mark_node)
13649 return error_mark_node;
13650
13651 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13652 }
13653
13654 /* Exception handling [gram.exception] */
13655
13656 /* Parse an (optional) exception-specification.
13657
13658 exception-specification:
13659 throw ( type-id-list [opt] )
13660
13661 Returns a TREE_LIST representing the exception-specification. The
13662 TREE_VALUE of each node is a type. */
13663
13664 static tree
13665 cp_parser_exception_specification_opt (cp_parser* parser)
13666 {
13667 cp_token *token;
13668 tree type_id_list;
13669
13670 /* Peek at the next token. */
13671 token = cp_lexer_peek_token (parser->lexer);
13672 /* If it's not `throw', then there's no exception-specification. */
13673 if (!cp_parser_is_keyword (token, RID_THROW))
13674 return NULL_TREE;
13675
13676 /* Consume the `throw'. */
13677 cp_lexer_consume_token (parser->lexer);
13678
13679 /* Look for the `('. */
13680 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13681
13682 /* Peek at the next token. */
13683 token = cp_lexer_peek_token (parser->lexer);
13684 /* If it's not a `)', then there is a type-id-list. */
13685 if (token->type != CPP_CLOSE_PAREN)
13686 {
13687 const char *saved_message;
13688
13689 /* Types may not be defined in an exception-specification. */
13690 saved_message = parser->type_definition_forbidden_message;
13691 parser->type_definition_forbidden_message
13692 = "types may not be defined in an exception-specification";
13693 /* Parse the type-id-list. */
13694 type_id_list = cp_parser_type_id_list (parser);
13695 /* Restore the saved message. */
13696 parser->type_definition_forbidden_message = saved_message;
13697 }
13698 else
13699 type_id_list = empty_except_spec;
13700
13701 /* Look for the `)'. */
13702 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13703
13704 return type_id_list;
13705 }
13706
13707 /* Parse an (optional) type-id-list.
13708
13709 type-id-list:
13710 type-id
13711 type-id-list , type-id
13712
13713 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13714 in the order that the types were presented. */
13715
13716 static tree
13717 cp_parser_type_id_list (cp_parser* parser)
13718 {
13719 tree types = NULL_TREE;
13720
13721 while (true)
13722 {
13723 cp_token *token;
13724 tree type;
13725
13726 /* Get the next type-id. */
13727 type = cp_parser_type_id (parser);
13728 /* Add it to the list. */
13729 types = add_exception_specifier (types, type, /*complain=*/1);
13730 /* Peek at the next token. */
13731 token = cp_lexer_peek_token (parser->lexer);
13732 /* If it is not a `,', we are done. */
13733 if (token->type != CPP_COMMA)
13734 break;
13735 /* Consume the `,'. */
13736 cp_lexer_consume_token (parser->lexer);
13737 }
13738
13739 return nreverse (types);
13740 }
13741
13742 /* Parse a try-block.
13743
13744 try-block:
13745 try compound-statement handler-seq */
13746
13747 static tree
13748 cp_parser_try_block (cp_parser* parser)
13749 {
13750 tree try_block;
13751
13752 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13753 try_block = begin_try_block ();
13754 cp_parser_compound_statement (parser, NULL, true);
13755 finish_try_block (try_block);
13756 cp_parser_handler_seq (parser);
13757 finish_handler_sequence (try_block);
13758
13759 return try_block;
13760 }
13761
13762 /* Parse a function-try-block.
13763
13764 function-try-block:
13765 try ctor-initializer [opt] function-body handler-seq */
13766
13767 static bool
13768 cp_parser_function_try_block (cp_parser* parser)
13769 {
13770 tree try_block;
13771 bool ctor_initializer_p;
13772
13773 /* Look for the `try' keyword. */
13774 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13775 return false;
13776 /* Let the rest of the front-end know where we are. */
13777 try_block = begin_function_try_block ();
13778 /* Parse the function-body. */
13779 ctor_initializer_p
13780 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13781 /* We're done with the `try' part. */
13782 finish_function_try_block (try_block);
13783 /* Parse the handlers. */
13784 cp_parser_handler_seq (parser);
13785 /* We're done with the handlers. */
13786 finish_function_handler_sequence (try_block);
13787
13788 return ctor_initializer_p;
13789 }
13790
13791 /* Parse a handler-seq.
13792
13793 handler-seq:
13794 handler handler-seq [opt] */
13795
13796 static void
13797 cp_parser_handler_seq (cp_parser* parser)
13798 {
13799 while (true)
13800 {
13801 cp_token *token;
13802
13803 /* Parse the handler. */
13804 cp_parser_handler (parser);
13805 /* Peek at the next token. */
13806 token = cp_lexer_peek_token (parser->lexer);
13807 /* If it's not `catch' then there are no more handlers. */
13808 if (!cp_parser_is_keyword (token, RID_CATCH))
13809 break;
13810 }
13811 }
13812
13813 /* Parse a handler.
13814
13815 handler:
13816 catch ( exception-declaration ) compound-statement */
13817
13818 static void
13819 cp_parser_handler (cp_parser* parser)
13820 {
13821 tree handler;
13822 tree declaration;
13823
13824 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13825 handler = begin_handler ();
13826 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13827 declaration = cp_parser_exception_declaration (parser);
13828 finish_handler_parms (declaration, handler);
13829 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13830 cp_parser_compound_statement (parser, NULL, false);
13831 finish_handler (handler);
13832 }
13833
13834 /* Parse an exception-declaration.
13835
13836 exception-declaration:
13837 type-specifier-seq declarator
13838 type-specifier-seq abstract-declarator
13839 type-specifier-seq
13840 ...
13841
13842 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13843 ellipsis variant is used. */
13844
13845 static tree
13846 cp_parser_exception_declaration (cp_parser* parser)
13847 {
13848 tree decl;
13849 cp_decl_specifier_seq type_specifiers;
13850 cp_declarator *declarator;
13851 const char *saved_message;
13852
13853 /* If it's an ellipsis, it's easy to handle. */
13854 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13855 {
13856 /* Consume the `...' token. */
13857 cp_lexer_consume_token (parser->lexer);
13858 return NULL_TREE;
13859 }
13860
13861 /* Types may not be defined in exception-declarations. */
13862 saved_message = parser->type_definition_forbidden_message;
13863 parser->type_definition_forbidden_message
13864 = "types may not be defined in exception-declarations";
13865
13866 /* Parse the type-specifier-seq. */
13867 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13868 &type_specifiers);
13869 /* If it's a `)', then there is no declarator. */
13870 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13871 declarator = NULL;
13872 else
13873 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13874 /*ctor_dtor_or_conv_p=*/NULL,
13875 /*parenthesized_p=*/NULL,
13876 /*member_p=*/false);
13877
13878 /* Restore the saved message. */
13879 parser->type_definition_forbidden_message = saved_message;
13880
13881 if (type_specifiers.any_specifiers_p)
13882 {
13883 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13884 if (decl == NULL_TREE)
13885 error ("invalid catch parameter");
13886 }
13887 else
13888 decl = NULL_TREE;
13889
13890 return decl;
13891 }
13892
13893 /* Parse a throw-expression.
13894
13895 throw-expression:
13896 throw assignment-expression [opt]
13897
13898 Returns a THROW_EXPR representing the throw-expression. */
13899
13900 static tree
13901 cp_parser_throw_expression (cp_parser* parser)
13902 {
13903 tree expression;
13904 cp_token* token;
13905
13906 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13907 token = cp_lexer_peek_token (parser->lexer);
13908 /* Figure out whether or not there is an assignment-expression
13909 following the "throw" keyword. */
13910 if (token->type == CPP_COMMA
13911 || token->type == CPP_SEMICOLON
13912 || token->type == CPP_CLOSE_PAREN
13913 || token->type == CPP_CLOSE_SQUARE
13914 || token->type == CPP_CLOSE_BRACE
13915 || token->type == CPP_COLON)
13916 expression = NULL_TREE;
13917 else
13918 expression = cp_parser_assignment_expression (parser,
13919 /*cast_p=*/false);
13920
13921 return build_throw (expression);
13922 }
13923
13924 /* GNU Extensions */
13925
13926 /* Parse an (optional) asm-specification.
13927
13928 asm-specification:
13929 asm ( string-literal )
13930
13931 If the asm-specification is present, returns a STRING_CST
13932 corresponding to the string-literal. Otherwise, returns
13933 NULL_TREE. */
13934
13935 static tree
13936 cp_parser_asm_specification_opt (cp_parser* parser)
13937 {
13938 cp_token *token;
13939 tree asm_specification;
13940
13941 /* Peek at the next token. */
13942 token = cp_lexer_peek_token (parser->lexer);
13943 /* If the next token isn't the `asm' keyword, then there's no
13944 asm-specification. */
13945 if (!cp_parser_is_keyword (token, RID_ASM))
13946 return NULL_TREE;
13947
13948 /* Consume the `asm' token. */
13949 cp_lexer_consume_token (parser->lexer);
13950 /* Look for the `('. */
13951 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13952
13953 /* Look for the string-literal. */
13954 asm_specification = cp_parser_string_literal (parser, false, false);
13955
13956 /* Look for the `)'. */
13957 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13958
13959 return asm_specification;
13960 }
13961
13962 /* Parse an asm-operand-list.
13963
13964 asm-operand-list:
13965 asm-operand
13966 asm-operand-list , asm-operand
13967
13968 asm-operand:
13969 string-literal ( expression )
13970 [ string-literal ] string-literal ( expression )
13971
13972 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13973 each node is the expression. The TREE_PURPOSE is itself a
13974 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13975 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13976 is a STRING_CST for the string literal before the parenthesis. */
13977
13978 static tree
13979 cp_parser_asm_operand_list (cp_parser* parser)
13980 {
13981 tree asm_operands = NULL_TREE;
13982
13983 while (true)
13984 {
13985 tree string_literal;
13986 tree expression;
13987 tree name;
13988
13989 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13990 {
13991 /* Consume the `[' token. */
13992 cp_lexer_consume_token (parser->lexer);
13993 /* Read the operand name. */
13994 name = cp_parser_identifier (parser);
13995 if (name != error_mark_node)
13996 name = build_string (IDENTIFIER_LENGTH (name),
13997 IDENTIFIER_POINTER (name));
13998 /* Look for the closing `]'. */
13999 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14000 }
14001 else
14002 name = NULL_TREE;
14003 /* Look for the string-literal. */
14004 string_literal = cp_parser_string_literal (parser, false, false);
14005
14006 /* Look for the `('. */
14007 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14008 /* Parse the expression. */
14009 expression = cp_parser_expression (parser, /*cast_p=*/false);
14010 /* Look for the `)'. */
14011 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14012
14013 /* Add this operand to the list. */
14014 asm_operands = tree_cons (build_tree_list (name, string_literal),
14015 expression,
14016 asm_operands);
14017 /* If the next token is not a `,', there are no more
14018 operands. */
14019 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14020 break;
14021 /* Consume the `,'. */
14022 cp_lexer_consume_token (parser->lexer);
14023 }
14024
14025 return nreverse (asm_operands);
14026 }
14027
14028 /* Parse an asm-clobber-list.
14029
14030 asm-clobber-list:
14031 string-literal
14032 asm-clobber-list , string-literal
14033
14034 Returns a TREE_LIST, indicating the clobbers in the order that they
14035 appeared. The TREE_VALUE of each node is a STRING_CST. */
14036
14037 static tree
14038 cp_parser_asm_clobber_list (cp_parser* parser)
14039 {
14040 tree clobbers = NULL_TREE;
14041
14042 while (true)
14043 {
14044 tree string_literal;
14045
14046 /* Look for the string literal. */
14047 string_literal = cp_parser_string_literal (parser, false, false);
14048 /* Add it to the list. */
14049 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14050 /* If the next token is not a `,', then the list is
14051 complete. */
14052 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14053 break;
14054 /* Consume the `,' token. */
14055 cp_lexer_consume_token (parser->lexer);
14056 }
14057
14058 return clobbers;
14059 }
14060
14061 /* Parse an (optional) series of attributes.
14062
14063 attributes:
14064 attributes attribute
14065
14066 attribute:
14067 __attribute__ (( attribute-list [opt] ))
14068
14069 The return value is as for cp_parser_attribute_list. */
14070
14071 static tree
14072 cp_parser_attributes_opt (cp_parser* parser)
14073 {
14074 tree attributes = NULL_TREE;
14075
14076 while (true)
14077 {
14078 cp_token *token;
14079 tree attribute_list;
14080
14081 /* Peek at the next token. */
14082 token = cp_lexer_peek_token (parser->lexer);
14083 /* If it's not `__attribute__', then we're done. */
14084 if (token->keyword != RID_ATTRIBUTE)
14085 break;
14086
14087 /* Consume the `__attribute__' keyword. */
14088 cp_lexer_consume_token (parser->lexer);
14089 /* Look for the two `(' tokens. */
14090 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14091 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14092
14093 /* Peek at the next token. */
14094 token = cp_lexer_peek_token (parser->lexer);
14095 if (token->type != CPP_CLOSE_PAREN)
14096 /* Parse the attribute-list. */
14097 attribute_list = cp_parser_attribute_list (parser);
14098 else
14099 /* If the next token is a `)', then there is no attribute
14100 list. */
14101 attribute_list = NULL;
14102
14103 /* Look for the two `)' tokens. */
14104 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14105 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14106
14107 /* Add these new attributes to the list. */
14108 attributes = chainon (attributes, attribute_list);
14109 }
14110
14111 return attributes;
14112 }
14113
14114 /* Parse an attribute-list.
14115
14116 attribute-list:
14117 attribute
14118 attribute-list , attribute
14119
14120 attribute:
14121 identifier
14122 identifier ( identifier )
14123 identifier ( identifier , expression-list )
14124 identifier ( expression-list )
14125
14126 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14127 to an attribute. The TREE_PURPOSE of each node is the identifier
14128 indicating which attribute is in use. The TREE_VALUE represents
14129 the arguments, if any. */
14130
14131 static tree
14132 cp_parser_attribute_list (cp_parser* parser)
14133 {
14134 tree attribute_list = NULL_TREE;
14135 bool save_translate_strings_p = parser->translate_strings_p;
14136
14137 parser->translate_strings_p = false;
14138 while (true)
14139 {
14140 cp_token *token;
14141 tree identifier;
14142 tree attribute;
14143
14144 /* Look for the identifier. We also allow keywords here; for
14145 example `__attribute__ ((const))' is legal. */
14146 token = cp_lexer_peek_token (parser->lexer);
14147 if (token->type == CPP_NAME
14148 || token->type == CPP_KEYWORD)
14149 {
14150 /* Consume the token. */
14151 token = cp_lexer_consume_token (parser->lexer);
14152
14153 /* Save away the identifier that indicates which attribute
14154 this is. */
14155 identifier = token->value;
14156 attribute = build_tree_list (identifier, NULL_TREE);
14157
14158 /* Peek at the next token. */
14159 token = cp_lexer_peek_token (parser->lexer);
14160 /* If it's an `(', then parse the attribute arguments. */
14161 if (token->type == CPP_OPEN_PAREN)
14162 {
14163 tree arguments;
14164
14165 arguments = (cp_parser_parenthesized_expression_list
14166 (parser, true, /*cast_p=*/false,
14167 /*non_constant_p=*/NULL));
14168 /* Save the identifier and arguments away. */
14169 TREE_VALUE (attribute) = arguments;
14170 }
14171
14172 /* Add this attribute to the list. */
14173 TREE_CHAIN (attribute) = attribute_list;
14174 attribute_list = attribute;
14175
14176 token = cp_lexer_peek_token (parser->lexer);
14177 }
14178 /* Now, look for more attributes. If the next token isn't a
14179 `,', we're done. */
14180 if (token->type != CPP_COMMA)
14181 break;
14182
14183 /* Consume the comma and keep going. */
14184 cp_lexer_consume_token (parser->lexer);
14185 }
14186 parser->translate_strings_p = save_translate_strings_p;
14187
14188 /* We built up the list in reverse order. */
14189 return nreverse (attribute_list);
14190 }
14191
14192 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14193 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14194 current value of the PEDANTIC flag, regardless of whether or not
14195 the `__extension__' keyword is present. The caller is responsible
14196 for restoring the value of the PEDANTIC flag. */
14197
14198 static bool
14199 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14200 {
14201 /* Save the old value of the PEDANTIC flag. */
14202 *saved_pedantic = pedantic;
14203
14204 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14205 {
14206 /* Consume the `__extension__' token. */
14207 cp_lexer_consume_token (parser->lexer);
14208 /* We're not being pedantic while the `__extension__' keyword is
14209 in effect. */
14210 pedantic = 0;
14211
14212 return true;
14213 }
14214
14215 return false;
14216 }
14217
14218 /* Parse a label declaration.
14219
14220 label-declaration:
14221 __label__ label-declarator-seq ;
14222
14223 label-declarator-seq:
14224 identifier , label-declarator-seq
14225 identifier */
14226
14227 static void
14228 cp_parser_label_declaration (cp_parser* parser)
14229 {
14230 /* Look for the `__label__' keyword. */
14231 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14232
14233 while (true)
14234 {
14235 tree identifier;
14236
14237 /* Look for an identifier. */
14238 identifier = cp_parser_identifier (parser);
14239 /* Declare it as a lobel. */
14240 finish_label_decl (identifier);
14241 /* If the next token is a `;', stop. */
14242 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14243 break;
14244 /* Look for the `,' separating the label declarations. */
14245 cp_parser_require (parser, CPP_COMMA, "`,'");
14246 }
14247
14248 /* Look for the final `;'. */
14249 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14250 }
14251
14252 /* Support Functions */
14253
14254 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14255 NAME should have one of the representations used for an
14256 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14257 is returned. If PARSER->SCOPE is a dependent type, then a
14258 SCOPE_REF is returned.
14259
14260 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14261 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14262 was formed. Abstractly, such entities should not be passed to this
14263 function, because they do not need to be looked up, but it is
14264 simpler to check for this special case here, rather than at the
14265 call-sites.
14266
14267 In cases not explicitly covered above, this function returns a
14268 DECL, OVERLOAD, or baselink representing the result of the lookup.
14269 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14270 is returned.
14271
14272 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14273 (e.g., "struct") that was used. In that case bindings that do not
14274 refer to types are ignored.
14275
14276 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14277 ignored.
14278
14279 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14280 are ignored.
14281
14282 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14283 types.
14284
14285 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14286 results in an ambiguity, and false otherwise. */
14287
14288 static tree
14289 cp_parser_lookup_name (cp_parser *parser, tree name,
14290 enum tag_types tag_type,
14291 bool is_template, bool is_namespace,
14292 bool check_dependency,
14293 bool *ambiguous_p)
14294 {
14295 tree decl;
14296 tree object_type = parser->context->object_type;
14297
14298 /* Assume that the lookup will be unambiguous. */
14299 if (ambiguous_p)
14300 *ambiguous_p = false;
14301
14302 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14303 no longer valid. Note that if we are parsing tentatively, and
14304 the parse fails, OBJECT_TYPE will be automatically restored. */
14305 parser->context->object_type = NULL_TREE;
14306
14307 if (name == error_mark_node)
14308 return error_mark_node;
14309
14310 /* A template-id has already been resolved; there is no lookup to
14311 do. */
14312 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14313 return name;
14314 if (BASELINK_P (name))
14315 {
14316 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14317 == TEMPLATE_ID_EXPR);
14318 return name;
14319 }
14320
14321 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14322 it should already have been checked to make sure that the name
14323 used matches the type being destroyed. */
14324 if (TREE_CODE (name) == BIT_NOT_EXPR)
14325 {
14326 tree type;
14327
14328 /* Figure out to which type this destructor applies. */
14329 if (parser->scope)
14330 type = parser->scope;
14331 else if (object_type)
14332 type = object_type;
14333 else
14334 type = current_class_type;
14335 /* If that's not a class type, there is no destructor. */
14336 if (!type || !CLASS_TYPE_P (type))
14337 return error_mark_node;
14338 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14339 lazily_declare_fn (sfk_destructor, type);
14340 if (!CLASSTYPE_DESTRUCTORS (type))
14341 return error_mark_node;
14342 /* If it was a class type, return the destructor. */
14343 return CLASSTYPE_DESTRUCTORS (type);
14344 }
14345
14346 /* By this point, the NAME should be an ordinary identifier. If
14347 the id-expression was a qualified name, the qualifying scope is
14348 stored in PARSER->SCOPE at this point. */
14349 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14350
14351 /* Perform the lookup. */
14352 if (parser->scope)
14353 {
14354 bool dependent_p;
14355
14356 if (parser->scope == error_mark_node)
14357 return error_mark_node;
14358
14359 /* If the SCOPE is dependent, the lookup must be deferred until
14360 the template is instantiated -- unless we are explicitly
14361 looking up names in uninstantiated templates. Even then, we
14362 cannot look up the name if the scope is not a class type; it
14363 might, for example, be a template type parameter. */
14364 dependent_p = (TYPE_P (parser->scope)
14365 && !(parser->in_declarator_p
14366 && currently_open_class (parser->scope))
14367 && dependent_type_p (parser->scope));
14368 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14369 && dependent_p)
14370 {
14371 if (tag_type)
14372 {
14373 tree type;
14374
14375 /* The resolution to Core Issue 180 says that `struct
14376 A::B' should be considered a type-name, even if `A'
14377 is dependent. */
14378 type = make_typename_type (parser->scope, name, tag_type,
14379 /*complain=*/1);
14380 decl = TYPE_NAME (type);
14381 }
14382 else if (is_template)
14383 decl = make_unbound_class_template (parser->scope,
14384 name, NULL_TREE,
14385 /*complain=*/1);
14386 else
14387 decl = build_nt (SCOPE_REF, parser->scope, name);
14388 }
14389 else
14390 {
14391 tree pushed_scope = NULL_TREE;
14392
14393 /* If PARSER->SCOPE is a dependent type, then it must be a
14394 class type, and we must not be checking dependencies;
14395 otherwise, we would have processed this lookup above. So
14396 that PARSER->SCOPE is not considered a dependent base by
14397 lookup_member, we must enter the scope here. */
14398 if (dependent_p)
14399 pushed_scope = push_scope (parser->scope);
14400 /* If the PARSER->SCOPE is a template specialization, it
14401 may be instantiated during name lookup. In that case,
14402 errors may be issued. Even if we rollback the current
14403 tentative parse, those errors are valid. */
14404 decl = lookup_qualified_name (parser->scope, name,
14405 tag_type != none_type,
14406 /*complain=*/true);
14407 if (pushed_scope)
14408 pop_scope (pushed_scope);
14409 }
14410 parser->qualifying_scope = parser->scope;
14411 parser->object_scope = NULL_TREE;
14412 }
14413 else if (object_type)
14414 {
14415 tree object_decl = NULL_TREE;
14416 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14417 OBJECT_TYPE is not a class. */
14418 if (CLASS_TYPE_P (object_type))
14419 /* If the OBJECT_TYPE is a template specialization, it may
14420 be instantiated during name lookup. In that case, errors
14421 may be issued. Even if we rollback the current tentative
14422 parse, those errors are valid. */
14423 object_decl = lookup_member (object_type,
14424 name,
14425 /*protect=*/0,
14426 tag_type != none_type);
14427 /* Look it up in the enclosing context, too. */
14428 decl = lookup_name_real (name, tag_type != none_type,
14429 /*nonclass=*/0,
14430 /*block_p=*/true, is_namespace,
14431 /*flags=*/0);
14432 parser->object_scope = object_type;
14433 parser->qualifying_scope = NULL_TREE;
14434 if (object_decl)
14435 decl = object_decl;
14436 }
14437 else
14438 {
14439 decl = lookup_name_real (name, tag_type != none_type,
14440 /*nonclass=*/0,
14441 /*block_p=*/true, is_namespace,
14442 /*flags=*/0);
14443 parser->qualifying_scope = NULL_TREE;
14444 parser->object_scope = NULL_TREE;
14445 }
14446
14447 /* If the lookup failed, let our caller know. */
14448 if (!decl || decl == error_mark_node)
14449 return error_mark_node;
14450
14451 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14452 if (TREE_CODE (decl) == TREE_LIST)
14453 {
14454 if (ambiguous_p)
14455 *ambiguous_p = true;
14456 /* The error message we have to print is too complicated for
14457 cp_parser_error, so we incorporate its actions directly. */
14458 if (!cp_parser_simulate_error (parser))
14459 {
14460 error ("reference to %qD is ambiguous", name);
14461 print_candidates (decl);
14462 }
14463 return error_mark_node;
14464 }
14465
14466 gcc_assert (DECL_P (decl)
14467 || TREE_CODE (decl) == OVERLOAD
14468 || TREE_CODE (decl) == SCOPE_REF
14469 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14470 || BASELINK_P (decl));
14471
14472 /* If we have resolved the name of a member declaration, check to
14473 see if the declaration is accessible. When the name resolves to
14474 set of overloaded functions, accessibility is checked when
14475 overload resolution is done.
14476
14477 During an explicit instantiation, access is not checked at all,
14478 as per [temp.explicit]. */
14479 if (DECL_P (decl))
14480 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14481
14482 return decl;
14483 }
14484
14485 /* Like cp_parser_lookup_name, but for use in the typical case where
14486 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14487 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14488
14489 static tree
14490 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14491 {
14492 return cp_parser_lookup_name (parser, name,
14493 none_type,
14494 /*is_template=*/false,
14495 /*is_namespace=*/false,
14496 /*check_dependency=*/true,
14497 /*ambiguous_p=*/NULL);
14498 }
14499
14500 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14501 the current context, return the TYPE_DECL. If TAG_NAME_P is
14502 true, the DECL indicates the class being defined in a class-head,
14503 or declared in an elaborated-type-specifier.
14504
14505 Otherwise, return DECL. */
14506
14507 static tree
14508 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14509 {
14510 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14511 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14512
14513 struct A {
14514 template <typename T> struct B;
14515 };
14516
14517 template <typename T> struct A::B {};
14518
14519 Similarly, in a elaborated-type-specifier:
14520
14521 namespace N { struct X{}; }
14522
14523 struct A {
14524 template <typename T> friend struct N::X;
14525 };
14526
14527 However, if the DECL refers to a class type, and we are in
14528 the scope of the class, then the name lookup automatically
14529 finds the TYPE_DECL created by build_self_reference rather
14530 than a TEMPLATE_DECL. For example, in:
14531
14532 template <class T> struct S {
14533 S s;
14534 };
14535
14536 there is no need to handle such case. */
14537
14538 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14539 return DECL_TEMPLATE_RESULT (decl);
14540
14541 return decl;
14542 }
14543
14544 /* If too many, or too few, template-parameter lists apply to the
14545 declarator, issue an error message. Returns TRUE if all went well,
14546 and FALSE otherwise. */
14547
14548 static bool
14549 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14550 cp_declarator *declarator)
14551 {
14552 unsigned num_templates;
14553
14554 /* We haven't seen any classes that involve template parameters yet. */
14555 num_templates = 0;
14556
14557 switch (declarator->kind)
14558 {
14559 case cdk_id:
14560 if (declarator->u.id.qualifying_scope)
14561 {
14562 tree scope;
14563 tree member;
14564
14565 scope = declarator->u.id.qualifying_scope;
14566 member = declarator->u.id.unqualified_name;
14567
14568 while (scope && CLASS_TYPE_P (scope))
14569 {
14570 /* You're supposed to have one `template <...>'
14571 for every template class, but you don't need one
14572 for a full specialization. For example:
14573
14574 template <class T> struct S{};
14575 template <> struct S<int> { void f(); };
14576 void S<int>::f () {}
14577
14578 is correct; there shouldn't be a `template <>' for
14579 the definition of `S<int>::f'. */
14580 if (CLASSTYPE_TEMPLATE_INFO (scope)
14581 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14582 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14583 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14584 ++num_templates;
14585
14586 scope = TYPE_CONTEXT (scope);
14587 }
14588 }
14589 else if (TREE_CODE (declarator->u.id.unqualified_name)
14590 == TEMPLATE_ID_EXPR)
14591 /* If the DECLARATOR has the form `X<y>' then it uses one
14592 additional level of template parameters. */
14593 ++num_templates;
14594
14595 return cp_parser_check_template_parameters (parser,
14596 num_templates);
14597
14598 case cdk_function:
14599 case cdk_array:
14600 case cdk_pointer:
14601 case cdk_reference:
14602 case cdk_ptrmem:
14603 return (cp_parser_check_declarator_template_parameters
14604 (parser, declarator->declarator));
14605
14606 case cdk_error:
14607 return true;
14608
14609 default:
14610 gcc_unreachable ();
14611 }
14612 return false;
14613 }
14614
14615 /* NUM_TEMPLATES were used in the current declaration. If that is
14616 invalid, return FALSE and issue an error messages. Otherwise,
14617 return TRUE. */
14618
14619 static bool
14620 cp_parser_check_template_parameters (cp_parser* parser,
14621 unsigned num_templates)
14622 {
14623 /* If there are more template classes than parameter lists, we have
14624 something like:
14625
14626 template <class T> void S<T>::R<T>::f (); */
14627 if (parser->num_template_parameter_lists < num_templates)
14628 {
14629 error ("too few template-parameter-lists");
14630 return false;
14631 }
14632 /* If there are the same number of template classes and parameter
14633 lists, that's OK. */
14634 if (parser->num_template_parameter_lists == num_templates)
14635 return true;
14636 /* If there are more, but only one more, then we are referring to a
14637 member template. That's OK too. */
14638 if (parser->num_template_parameter_lists == num_templates + 1)
14639 return true;
14640 /* Otherwise, there are too many template parameter lists. We have
14641 something like:
14642
14643 template <class T> template <class U> void S::f(); */
14644 error ("too many template-parameter-lists");
14645 return false;
14646 }
14647
14648 /* Parse an optional `::' token indicating that the following name is
14649 from the global namespace. If so, PARSER->SCOPE is set to the
14650 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14651 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14652 Returns the new value of PARSER->SCOPE, if the `::' token is
14653 present, and NULL_TREE otherwise. */
14654
14655 static tree
14656 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14657 {
14658 cp_token *token;
14659
14660 /* Peek at the next token. */
14661 token = cp_lexer_peek_token (parser->lexer);
14662 /* If we're looking at a `::' token then we're starting from the
14663 global namespace, not our current location. */
14664 if (token->type == CPP_SCOPE)
14665 {
14666 /* Consume the `::' token. */
14667 cp_lexer_consume_token (parser->lexer);
14668 /* Set the SCOPE so that we know where to start the lookup. */
14669 parser->scope = global_namespace;
14670 parser->qualifying_scope = global_namespace;
14671 parser->object_scope = NULL_TREE;
14672
14673 return parser->scope;
14674 }
14675 else if (!current_scope_valid_p)
14676 {
14677 parser->scope = NULL_TREE;
14678 parser->qualifying_scope = NULL_TREE;
14679 parser->object_scope = NULL_TREE;
14680 }
14681
14682 return NULL_TREE;
14683 }
14684
14685 /* Returns TRUE if the upcoming token sequence is the start of a
14686 constructor declarator. If FRIEND_P is true, the declarator is
14687 preceded by the `friend' specifier. */
14688
14689 static bool
14690 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14691 {
14692 bool constructor_p;
14693 tree type_decl = NULL_TREE;
14694 bool nested_name_p;
14695 cp_token *next_token;
14696
14697 /* The common case is that this is not a constructor declarator, so
14698 try to avoid doing lots of work if at all possible. It's not
14699 valid declare a constructor at function scope. */
14700 if (at_function_scope_p ())
14701 return false;
14702 /* And only certain tokens can begin a constructor declarator. */
14703 next_token = cp_lexer_peek_token (parser->lexer);
14704 if (next_token->type != CPP_NAME
14705 && next_token->type != CPP_SCOPE
14706 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14707 && next_token->type != CPP_TEMPLATE_ID)
14708 return false;
14709
14710 /* Parse tentatively; we are going to roll back all of the tokens
14711 consumed here. */
14712 cp_parser_parse_tentatively (parser);
14713 /* Assume that we are looking at a constructor declarator. */
14714 constructor_p = true;
14715
14716 /* Look for the optional `::' operator. */
14717 cp_parser_global_scope_opt (parser,
14718 /*current_scope_valid_p=*/false);
14719 /* Look for the nested-name-specifier. */
14720 nested_name_p
14721 = (cp_parser_nested_name_specifier_opt (parser,
14722 /*typename_keyword_p=*/false,
14723 /*check_dependency_p=*/false,
14724 /*type_p=*/false,
14725 /*is_declaration=*/false)
14726 != NULL_TREE);
14727 /* Outside of a class-specifier, there must be a
14728 nested-name-specifier. */
14729 if (!nested_name_p &&
14730 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14731 || friend_p))
14732 constructor_p = false;
14733 /* If we still think that this might be a constructor-declarator,
14734 look for a class-name. */
14735 if (constructor_p)
14736 {
14737 /* If we have:
14738
14739 template <typename T> struct S { S(); };
14740 template <typename T> S<T>::S ();
14741
14742 we must recognize that the nested `S' names a class.
14743 Similarly, for:
14744
14745 template <typename T> S<T>::S<T> ();
14746
14747 we must recognize that the nested `S' names a template. */
14748 type_decl = cp_parser_class_name (parser,
14749 /*typename_keyword_p=*/false,
14750 /*template_keyword_p=*/false,
14751 none_type,
14752 /*check_dependency_p=*/false,
14753 /*class_head_p=*/false,
14754 /*is_declaration=*/false);
14755 /* If there was no class-name, then this is not a constructor. */
14756 constructor_p = !cp_parser_error_occurred (parser);
14757 }
14758
14759 /* If we're still considering a constructor, we have to see a `(',
14760 to begin the parameter-declaration-clause, followed by either a
14761 `)', an `...', or a decl-specifier. We need to check for a
14762 type-specifier to avoid being fooled into thinking that:
14763
14764 S::S (f) (int);
14765
14766 is a constructor. (It is actually a function named `f' that
14767 takes one parameter (of type `int') and returns a value of type
14768 `S::S'. */
14769 if (constructor_p
14770 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14771 {
14772 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14773 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14774 /* A parameter declaration begins with a decl-specifier,
14775 which is either the "attribute" keyword, a storage class
14776 specifier, or (usually) a type-specifier. */
14777 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14778 && !cp_parser_storage_class_specifier_opt (parser))
14779 {
14780 tree type;
14781 tree pushed_scope = NULL_TREE;
14782 unsigned saved_num_template_parameter_lists;
14783
14784 /* Names appearing in the type-specifier should be looked up
14785 in the scope of the class. */
14786 if (current_class_type)
14787 type = NULL_TREE;
14788 else
14789 {
14790 type = TREE_TYPE (type_decl);
14791 if (TREE_CODE (type) == TYPENAME_TYPE)
14792 {
14793 type = resolve_typename_type (type,
14794 /*only_current_p=*/false);
14795 if (type == error_mark_node)
14796 {
14797 cp_parser_abort_tentative_parse (parser);
14798 return false;
14799 }
14800 }
14801 pushed_scope = push_scope (type);
14802 }
14803
14804 /* Inside the constructor parameter list, surrounding
14805 template-parameter-lists do not apply. */
14806 saved_num_template_parameter_lists
14807 = parser->num_template_parameter_lists;
14808 parser->num_template_parameter_lists = 0;
14809
14810 /* Look for the type-specifier. */
14811 cp_parser_type_specifier (parser,
14812 CP_PARSER_FLAGS_NONE,
14813 /*decl_specs=*/NULL,
14814 /*is_declarator=*/true,
14815 /*declares_class_or_enum=*/NULL,
14816 /*is_cv_qualifier=*/NULL);
14817
14818 parser->num_template_parameter_lists
14819 = saved_num_template_parameter_lists;
14820
14821 /* Leave the scope of the class. */
14822 if (pushed_scope)
14823 pop_scope (pushed_scope);
14824
14825 constructor_p = !cp_parser_error_occurred (parser);
14826 }
14827 }
14828 else
14829 constructor_p = false;
14830 /* We did not really want to consume any tokens. */
14831 cp_parser_abort_tentative_parse (parser);
14832
14833 return constructor_p;
14834 }
14835
14836 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14837 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14838 they must be performed once we are in the scope of the function.
14839
14840 Returns the function defined. */
14841
14842 static tree
14843 cp_parser_function_definition_from_specifiers_and_declarator
14844 (cp_parser* parser,
14845 cp_decl_specifier_seq *decl_specifiers,
14846 tree attributes,
14847 const cp_declarator *declarator)
14848 {
14849 tree fn;
14850 bool success_p;
14851
14852 /* Begin the function-definition. */
14853 success_p = start_function (decl_specifiers, declarator, attributes);
14854
14855 /* The things we're about to see are not directly qualified by any
14856 template headers we've seen thus far. */
14857 reset_specialization ();
14858
14859 /* If there were names looked up in the decl-specifier-seq that we
14860 did not check, check them now. We must wait until we are in the
14861 scope of the function to perform the checks, since the function
14862 might be a friend. */
14863 perform_deferred_access_checks ();
14864
14865 if (!success_p)
14866 {
14867 /* Skip the entire function. */
14868 error ("invalid function declaration");
14869 cp_parser_skip_to_end_of_block_or_statement (parser);
14870 fn = error_mark_node;
14871 }
14872 else
14873 fn = cp_parser_function_definition_after_declarator (parser,
14874 /*inline_p=*/false);
14875
14876 return fn;
14877 }
14878
14879 /* Parse the part of a function-definition that follows the
14880 declarator. INLINE_P is TRUE iff this function is an inline
14881 function defined with a class-specifier.
14882
14883 Returns the function defined. */
14884
14885 static tree
14886 cp_parser_function_definition_after_declarator (cp_parser* parser,
14887 bool inline_p)
14888 {
14889 tree fn;
14890 bool ctor_initializer_p = false;
14891 bool saved_in_unbraced_linkage_specification_p;
14892 unsigned saved_num_template_parameter_lists;
14893
14894 /* If the next token is `return', then the code may be trying to
14895 make use of the "named return value" extension that G++ used to
14896 support. */
14897 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14898 {
14899 /* Consume the `return' keyword. */
14900 cp_lexer_consume_token (parser->lexer);
14901 /* Look for the identifier that indicates what value is to be
14902 returned. */
14903 cp_parser_identifier (parser);
14904 /* Issue an error message. */
14905 error ("named return values are no longer supported");
14906 /* Skip tokens until we reach the start of the function body. */
14907 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14908 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14909 cp_lexer_consume_token (parser->lexer);
14910 }
14911 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14912 anything declared inside `f'. */
14913 saved_in_unbraced_linkage_specification_p
14914 = parser->in_unbraced_linkage_specification_p;
14915 parser->in_unbraced_linkage_specification_p = false;
14916 /* Inside the function, surrounding template-parameter-lists do not
14917 apply. */
14918 saved_num_template_parameter_lists
14919 = parser->num_template_parameter_lists;
14920 parser->num_template_parameter_lists = 0;
14921 /* If the next token is `try', then we are looking at a
14922 function-try-block. */
14923 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14924 ctor_initializer_p = cp_parser_function_try_block (parser);
14925 /* A function-try-block includes the function-body, so we only do
14926 this next part if we're not processing a function-try-block. */
14927 else
14928 ctor_initializer_p
14929 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14930
14931 /* Finish the function. */
14932 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14933 (inline_p ? 2 : 0));
14934 /* Generate code for it, if necessary. */
14935 expand_or_defer_fn (fn);
14936 /* Restore the saved values. */
14937 parser->in_unbraced_linkage_specification_p
14938 = saved_in_unbraced_linkage_specification_p;
14939 parser->num_template_parameter_lists
14940 = saved_num_template_parameter_lists;
14941
14942 return fn;
14943 }
14944
14945 /* Parse a template-declaration, assuming that the `export' (and
14946 `extern') keywords, if present, has already been scanned. MEMBER_P
14947 is as for cp_parser_template_declaration. */
14948
14949 static void
14950 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14951 {
14952 tree decl = NULL_TREE;
14953 tree parameter_list;
14954 bool friend_p = false;
14955
14956 /* Look for the `template' keyword. */
14957 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14958 return;
14959
14960 /* And the `<'. */
14961 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14962 return;
14963
14964 /* If the next token is `>', then we have an invalid
14965 specialization. Rather than complain about an invalid template
14966 parameter, issue an error message here. */
14967 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14968 {
14969 cp_parser_error (parser, "invalid explicit specialization");
14970 begin_specialization ();
14971 parameter_list = NULL_TREE;
14972 }
14973 else
14974 {
14975 /* Parse the template parameters. */
14976 begin_template_parm_list ();
14977 parameter_list = cp_parser_template_parameter_list (parser);
14978 parameter_list = end_template_parm_list (parameter_list);
14979 }
14980
14981 /* Look for the `>'. */
14982 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14983 /* We just processed one more parameter list. */
14984 ++parser->num_template_parameter_lists;
14985 /* If the next token is `template', there are more template
14986 parameters. */
14987 if (cp_lexer_next_token_is_keyword (parser->lexer,
14988 RID_TEMPLATE))
14989 cp_parser_template_declaration_after_export (parser, member_p);
14990 else
14991 {
14992 /* There are no access checks when parsing a template, as we do not
14993 know if a specialization will be a friend. */
14994 push_deferring_access_checks (dk_no_check);
14995
14996 decl = cp_parser_single_declaration (parser,
14997 member_p,
14998 &friend_p);
14999
15000 pop_deferring_access_checks ();
15001
15002 /* If this is a member template declaration, let the front
15003 end know. */
15004 if (member_p && !friend_p && decl)
15005 {
15006 if (TREE_CODE (decl) == TYPE_DECL)
15007 cp_parser_check_access_in_redeclaration (decl);
15008
15009 decl = finish_member_template_decl (decl);
15010 }
15011 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15012 make_friend_class (current_class_type, TREE_TYPE (decl),
15013 /*complain=*/true);
15014 }
15015 /* We are done with the current parameter list. */
15016 --parser->num_template_parameter_lists;
15017
15018 /* Finish up. */
15019 finish_template_decl (parameter_list);
15020
15021 /* Register member declarations. */
15022 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15023 finish_member_declaration (decl);
15024
15025 /* If DECL is a function template, we must return to parse it later.
15026 (Even though there is no definition, there might be default
15027 arguments that need handling.) */
15028 if (member_p && decl
15029 && (TREE_CODE (decl) == FUNCTION_DECL
15030 || DECL_FUNCTION_TEMPLATE_P (decl)))
15031 TREE_VALUE (parser->unparsed_functions_queues)
15032 = tree_cons (NULL_TREE, decl,
15033 TREE_VALUE (parser->unparsed_functions_queues));
15034 }
15035
15036 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15037 `function-definition' sequence. MEMBER_P is true, this declaration
15038 appears in a class scope.
15039
15040 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15041 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15042
15043 static tree
15044 cp_parser_single_declaration (cp_parser* parser,
15045 bool member_p,
15046 bool* friend_p)
15047 {
15048 int declares_class_or_enum;
15049 tree decl = NULL_TREE;
15050 cp_decl_specifier_seq decl_specifiers;
15051 bool function_definition_p = false;
15052
15053 /* This function is only used when processing a template
15054 declaration. */
15055 gcc_assert (innermost_scope_kind () == sk_template_parms
15056 || innermost_scope_kind () == sk_template_spec);
15057
15058 /* Defer access checks until we know what is being declared. */
15059 push_deferring_access_checks (dk_deferred);
15060
15061 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15062 alternative. */
15063 cp_parser_decl_specifier_seq (parser,
15064 CP_PARSER_FLAGS_OPTIONAL,
15065 &decl_specifiers,
15066 &declares_class_or_enum);
15067 if (friend_p)
15068 *friend_p = cp_parser_friend_p (&decl_specifiers);
15069
15070 /* There are no template typedefs. */
15071 if (decl_specifiers.specs[(int) ds_typedef])
15072 {
15073 error ("template declaration of %qs", "typedef");
15074 decl = error_mark_node;
15075 }
15076
15077 /* Gather up the access checks that occurred the
15078 decl-specifier-seq. */
15079 stop_deferring_access_checks ();
15080
15081 /* Check for the declaration of a template class. */
15082 if (declares_class_or_enum)
15083 {
15084 if (cp_parser_declares_only_class_p (parser))
15085 {
15086 decl = shadow_tag (&decl_specifiers);
15087
15088 /* In this case:
15089
15090 struct C {
15091 friend template <typename T> struct A<T>::B;
15092 };
15093
15094 A<T>::B will be represented by a TYPENAME_TYPE, and
15095 therefore not recognized by shadow_tag. */
15096 if (friend_p && *friend_p
15097 && !decl
15098 && decl_specifiers.type
15099 && TYPE_P (decl_specifiers.type))
15100 decl = decl_specifiers.type;
15101
15102 if (decl && decl != error_mark_node)
15103 decl = TYPE_NAME (decl);
15104 else
15105 decl = error_mark_node;
15106 }
15107 }
15108 /* If it's not a template class, try for a template function. If
15109 the next token is a `;', then this declaration does not declare
15110 anything. But, if there were errors in the decl-specifiers, then
15111 the error might well have come from an attempted class-specifier.
15112 In that case, there's no need to warn about a missing declarator. */
15113 if (!decl
15114 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15115 || decl_specifiers.type != error_mark_node))
15116 decl = cp_parser_init_declarator (parser,
15117 &decl_specifiers,
15118 /*function_definition_allowed_p=*/true,
15119 member_p,
15120 declares_class_or_enum,
15121 &function_definition_p);
15122
15123 pop_deferring_access_checks ();
15124
15125 /* Clear any current qualification; whatever comes next is the start
15126 of something new. */
15127 parser->scope = NULL_TREE;
15128 parser->qualifying_scope = NULL_TREE;
15129 parser->object_scope = NULL_TREE;
15130 /* Look for a trailing `;' after the declaration. */
15131 if (!function_definition_p
15132 && (decl == error_mark_node
15133 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15134 cp_parser_skip_to_end_of_block_or_statement (parser);
15135
15136 return decl;
15137 }
15138
15139 /* Parse a cast-expression that is not the operand of a unary "&". */
15140
15141 static tree
15142 cp_parser_simple_cast_expression (cp_parser *parser)
15143 {
15144 return cp_parser_cast_expression (parser, /*address_p=*/false,
15145 /*cast_p=*/false);
15146 }
15147
15148 /* Parse a functional cast to TYPE. Returns an expression
15149 representing the cast. */
15150
15151 static tree
15152 cp_parser_functional_cast (cp_parser* parser, tree type)
15153 {
15154 tree expression_list;
15155 tree cast;
15156
15157 expression_list
15158 = cp_parser_parenthesized_expression_list (parser, false,
15159 /*cast_p=*/true,
15160 /*non_constant_p=*/NULL);
15161
15162 cast = build_functional_cast (type, expression_list);
15163 /* [expr.const]/1: In an integral constant expression "only type
15164 conversions to integral or enumeration type can be used". */
15165 if (cast != error_mark_node && !type_dependent_expression_p (type)
15166 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15167 {
15168 if (cp_parser_non_integral_constant_expression
15169 (parser, "a call to a constructor"))
15170 return error_mark_node;
15171 }
15172 return cast;
15173 }
15174
15175 /* Save the tokens that make up the body of a member function defined
15176 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15177 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15178 specifiers applied to the declaration. Returns the FUNCTION_DECL
15179 for the member function. */
15180
15181 static tree
15182 cp_parser_save_member_function_body (cp_parser* parser,
15183 cp_decl_specifier_seq *decl_specifiers,
15184 cp_declarator *declarator,
15185 tree attributes)
15186 {
15187 cp_token *first;
15188 cp_token *last;
15189 tree fn;
15190
15191 /* Create the function-declaration. */
15192 fn = start_method (decl_specifiers, declarator, attributes);
15193 /* If something went badly wrong, bail out now. */
15194 if (fn == error_mark_node)
15195 {
15196 /* If there's a function-body, skip it. */
15197 if (cp_parser_token_starts_function_definition_p
15198 (cp_lexer_peek_token (parser->lexer)))
15199 cp_parser_skip_to_end_of_block_or_statement (parser);
15200 return error_mark_node;
15201 }
15202
15203 /* Remember it, if there default args to post process. */
15204 cp_parser_save_default_args (parser, fn);
15205
15206 /* Save away the tokens that make up the body of the
15207 function. */
15208 first = parser->lexer->next_token;
15209 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15210 /* Handle function try blocks. */
15211 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15212 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15213 last = parser->lexer->next_token;
15214
15215 /* Save away the inline definition; we will process it when the
15216 class is complete. */
15217 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15218 DECL_PENDING_INLINE_P (fn) = 1;
15219
15220 /* We need to know that this was defined in the class, so that
15221 friend templates are handled correctly. */
15222 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15223
15224 /* We're done with the inline definition. */
15225 finish_method (fn);
15226
15227 /* Add FN to the queue of functions to be parsed later. */
15228 TREE_VALUE (parser->unparsed_functions_queues)
15229 = tree_cons (NULL_TREE, fn,
15230 TREE_VALUE (parser->unparsed_functions_queues));
15231
15232 return fn;
15233 }
15234
15235 /* Parse a template-argument-list, as well as the trailing ">" (but
15236 not the opening ">"). See cp_parser_template_argument_list for the
15237 return value. */
15238
15239 static tree
15240 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15241 {
15242 tree arguments;
15243 tree saved_scope;
15244 tree saved_qualifying_scope;
15245 tree saved_object_scope;
15246 bool saved_greater_than_is_operator_p;
15247
15248 /* [temp.names]
15249
15250 When parsing a template-id, the first non-nested `>' is taken as
15251 the end of the template-argument-list rather than a greater-than
15252 operator. */
15253 saved_greater_than_is_operator_p
15254 = parser->greater_than_is_operator_p;
15255 parser->greater_than_is_operator_p = false;
15256 /* Parsing the argument list may modify SCOPE, so we save it
15257 here. */
15258 saved_scope = parser->scope;
15259 saved_qualifying_scope = parser->qualifying_scope;
15260 saved_object_scope = parser->object_scope;
15261 /* Parse the template-argument-list itself. */
15262 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15263 arguments = NULL_TREE;
15264 else
15265 arguments = cp_parser_template_argument_list (parser);
15266 /* Look for the `>' that ends the template-argument-list. If we find
15267 a '>>' instead, it's probably just a typo. */
15268 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15269 {
15270 if (!saved_greater_than_is_operator_p)
15271 {
15272 /* If we're in a nested template argument list, the '>>' has
15273 to be a typo for '> >'. We emit the error message, but we
15274 continue parsing and we push a '>' as next token, so that
15275 the argument list will be parsed correctly. Note that the
15276 global source location is still on the token before the
15277 '>>', so we need to say explicitly where we want it. */
15278 cp_token *token = cp_lexer_peek_token (parser->lexer);
15279 error ("%H%<>>%> should be %<> >%> "
15280 "within a nested template argument list",
15281 &token->location);
15282
15283 /* ??? Proper recovery should terminate two levels of
15284 template argument list here. */
15285 token->type = CPP_GREATER;
15286 }
15287 else
15288 {
15289 /* If this is not a nested template argument list, the '>>'
15290 is a typo for '>'. Emit an error message and continue.
15291 Same deal about the token location, but here we can get it
15292 right by consuming the '>>' before issuing the diagnostic. */
15293 cp_lexer_consume_token (parser->lexer);
15294 error ("spurious %<>>%>, use %<>%> to terminate "
15295 "a template argument list");
15296 }
15297 }
15298 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15299 error ("missing %<>%> to terminate the template argument list");
15300 else
15301 /* It's what we want, a '>'; consume it. */
15302 cp_lexer_consume_token (parser->lexer);
15303 /* The `>' token might be a greater-than operator again now. */
15304 parser->greater_than_is_operator_p
15305 = saved_greater_than_is_operator_p;
15306 /* Restore the SAVED_SCOPE. */
15307 parser->scope = saved_scope;
15308 parser->qualifying_scope = saved_qualifying_scope;
15309 parser->object_scope = saved_object_scope;
15310
15311 return arguments;
15312 }
15313
15314 /* MEMBER_FUNCTION is a member function, or a friend. If default
15315 arguments, or the body of the function have not yet been parsed,
15316 parse them now. */
15317
15318 static void
15319 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15320 {
15321 /* If this member is a template, get the underlying
15322 FUNCTION_DECL. */
15323 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15324 member_function = DECL_TEMPLATE_RESULT (member_function);
15325
15326 /* There should not be any class definitions in progress at this
15327 point; the bodies of members are only parsed outside of all class
15328 definitions. */
15329 gcc_assert (parser->num_classes_being_defined == 0);
15330 /* While we're parsing the member functions we might encounter more
15331 classes. We want to handle them right away, but we don't want
15332 them getting mixed up with functions that are currently in the
15333 queue. */
15334 parser->unparsed_functions_queues
15335 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15336
15337 /* Make sure that any template parameters are in scope. */
15338 maybe_begin_member_template_processing (member_function);
15339
15340 /* If the body of the function has not yet been parsed, parse it
15341 now. */
15342 if (DECL_PENDING_INLINE_P (member_function))
15343 {
15344 tree function_scope;
15345 cp_token_cache *tokens;
15346
15347 /* The function is no longer pending; we are processing it. */
15348 tokens = DECL_PENDING_INLINE_INFO (member_function);
15349 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15350 DECL_PENDING_INLINE_P (member_function) = 0;
15351
15352 /* If this is a local class, enter the scope of the containing
15353 function. */
15354 function_scope = current_function_decl;
15355 if (function_scope)
15356 push_function_context_to (function_scope);
15357
15358 /* Push the body of the function onto the lexer stack. */
15359 cp_parser_push_lexer_for_tokens (parser, tokens);
15360
15361 /* Let the front end know that we going to be defining this
15362 function. */
15363 start_preparsed_function (member_function, NULL_TREE,
15364 SF_PRE_PARSED | SF_INCLASS_INLINE);
15365
15366 /* Now, parse the body of the function. */
15367 cp_parser_function_definition_after_declarator (parser,
15368 /*inline_p=*/true);
15369
15370 /* Leave the scope of the containing function. */
15371 if (function_scope)
15372 pop_function_context_from (function_scope);
15373 cp_parser_pop_lexer (parser);
15374 }
15375
15376 /* Remove any template parameters from the symbol table. */
15377 maybe_end_member_template_processing ();
15378
15379 /* Restore the queue. */
15380 parser->unparsed_functions_queues
15381 = TREE_CHAIN (parser->unparsed_functions_queues);
15382 }
15383
15384 /* If DECL contains any default args, remember it on the unparsed
15385 functions queue. */
15386
15387 static void
15388 cp_parser_save_default_args (cp_parser* parser, tree decl)
15389 {
15390 tree probe;
15391
15392 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15393 probe;
15394 probe = TREE_CHAIN (probe))
15395 if (TREE_PURPOSE (probe))
15396 {
15397 TREE_PURPOSE (parser->unparsed_functions_queues)
15398 = tree_cons (current_class_type, decl,
15399 TREE_PURPOSE (parser->unparsed_functions_queues));
15400 break;
15401 }
15402 return;
15403 }
15404
15405 /* FN is a FUNCTION_DECL which may contains a parameter with an
15406 unparsed DEFAULT_ARG. Parse the default args now. This function
15407 assumes that the current scope is the scope in which the default
15408 argument should be processed. */
15409
15410 static void
15411 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15412 {
15413 bool saved_local_variables_forbidden_p;
15414 tree parm;
15415
15416 /* While we're parsing the default args, we might (due to the
15417 statement expression extension) encounter more classes. We want
15418 to handle them right away, but we don't want them getting mixed
15419 up with default args that are currently in the queue. */
15420 parser->unparsed_functions_queues
15421 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15422
15423 /* Local variable names (and the `this' keyword) may not appear
15424 in a default argument. */
15425 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15426 parser->local_variables_forbidden_p = true;
15427
15428 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15429 parm;
15430 parm = TREE_CHAIN (parm))
15431 {
15432 cp_token_cache *tokens;
15433
15434 if (!TREE_PURPOSE (parm)
15435 || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15436 continue;
15437
15438 /* Push the saved tokens for the default argument onto the parser's
15439 lexer stack. */
15440 tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15441 cp_parser_push_lexer_for_tokens (parser, tokens);
15442
15443 /* Parse the assignment-expression. */
15444 TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser,
15445 /*cast_p=*/false);
15446
15447 /* If the token stream has not been completely used up, then
15448 there was extra junk after the end of the default
15449 argument. */
15450 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15451 cp_parser_error (parser, "expected %<,%>");
15452
15453 /* Revert to the main lexer. */
15454 cp_parser_pop_lexer (parser);
15455 }
15456
15457 /* Restore the state of local_variables_forbidden_p. */
15458 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15459
15460 /* Restore the queue. */
15461 parser->unparsed_functions_queues
15462 = TREE_CHAIN (parser->unparsed_functions_queues);
15463 }
15464
15465 /* Parse the operand of `sizeof' (or a similar operator). Returns
15466 either a TYPE or an expression, depending on the form of the
15467 input. The KEYWORD indicates which kind of expression we have
15468 encountered. */
15469
15470 static tree
15471 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15472 {
15473 static const char *format;
15474 tree expr = NULL_TREE;
15475 const char *saved_message;
15476 bool saved_integral_constant_expression_p;
15477 bool saved_non_integral_constant_expression_p;
15478
15479 /* Initialize FORMAT the first time we get here. */
15480 if (!format)
15481 format = "types may not be defined in '%s' expressions";
15482
15483 /* Types cannot be defined in a `sizeof' expression. Save away the
15484 old message. */
15485 saved_message = parser->type_definition_forbidden_message;
15486 /* And create the new one. */
15487 parser->type_definition_forbidden_message
15488 = xmalloc (strlen (format)
15489 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15490 + 1 /* `\0' */);
15491 sprintf ((char *) parser->type_definition_forbidden_message,
15492 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15493
15494 /* The restrictions on constant-expressions do not apply inside
15495 sizeof expressions. */
15496 saved_integral_constant_expression_p
15497 = parser->integral_constant_expression_p;
15498 saved_non_integral_constant_expression_p
15499 = parser->non_integral_constant_expression_p;
15500 parser->integral_constant_expression_p = false;
15501
15502 /* Do not actually evaluate the expression. */
15503 ++skip_evaluation;
15504 /* If it's a `(', then we might be looking at the type-id
15505 construction. */
15506 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15507 {
15508 tree type;
15509 bool saved_in_type_id_in_expr_p;
15510
15511 /* We can't be sure yet whether we're looking at a type-id or an
15512 expression. */
15513 cp_parser_parse_tentatively (parser);
15514 /* Consume the `('. */
15515 cp_lexer_consume_token (parser->lexer);
15516 /* Parse the type-id. */
15517 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15518 parser->in_type_id_in_expr_p = true;
15519 type = cp_parser_type_id (parser);
15520 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15521 /* Now, look for the trailing `)'. */
15522 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15523 /* If all went well, then we're done. */
15524 if (cp_parser_parse_definitely (parser))
15525 {
15526 cp_decl_specifier_seq decl_specs;
15527
15528 /* Build a trivial decl-specifier-seq. */
15529 clear_decl_specs (&decl_specs);
15530 decl_specs.type = type;
15531
15532 /* Call grokdeclarator to figure out what type this is. */
15533 expr = grokdeclarator (NULL,
15534 &decl_specs,
15535 TYPENAME,
15536 /*initialized=*/0,
15537 /*attrlist=*/NULL);
15538 }
15539 }
15540
15541 /* If the type-id production did not work out, then we must be
15542 looking at the unary-expression production. */
15543 if (!expr)
15544 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15545 /*cast_p=*/false);
15546 /* Go back to evaluating expressions. */
15547 --skip_evaluation;
15548
15549 /* Free the message we created. */
15550 free ((char *) parser->type_definition_forbidden_message);
15551 /* And restore the old one. */
15552 parser->type_definition_forbidden_message = saved_message;
15553 parser->integral_constant_expression_p
15554 = saved_integral_constant_expression_p;
15555 parser->non_integral_constant_expression_p
15556 = saved_non_integral_constant_expression_p;
15557
15558 return expr;
15559 }
15560
15561 /* If the current declaration has no declarator, return true. */
15562
15563 static bool
15564 cp_parser_declares_only_class_p (cp_parser *parser)
15565 {
15566 /* If the next token is a `;' or a `,' then there is no
15567 declarator. */
15568 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15569 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15570 }
15571
15572 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15573
15574 static void
15575 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15576 cp_storage_class storage_class)
15577 {
15578 if (decl_specs->storage_class != sc_none)
15579 decl_specs->multiple_storage_classes_p = true;
15580 else
15581 decl_specs->storage_class = storage_class;
15582 }
15583
15584 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15585 is true, the type is a user-defined type; otherwise it is a
15586 built-in type specified by a keyword. */
15587
15588 static void
15589 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15590 tree type_spec,
15591 bool user_defined_p)
15592 {
15593 decl_specs->any_specifiers_p = true;
15594
15595 /* If the user tries to redeclare bool or wchar_t (with, for
15596 example, in "typedef int wchar_t;") we remember that this is what
15597 happened. In system headers, we ignore these declarations so
15598 that G++ can work with system headers that are not C++-safe. */
15599 if (decl_specs->specs[(int) ds_typedef]
15600 && !user_defined_p
15601 && (type_spec == boolean_type_node
15602 || type_spec == wchar_type_node)
15603 && (decl_specs->type
15604 || decl_specs->specs[(int) ds_long]
15605 || decl_specs->specs[(int) ds_short]
15606 || decl_specs->specs[(int) ds_unsigned]
15607 || decl_specs->specs[(int) ds_signed]))
15608 {
15609 decl_specs->redefined_builtin_type = type_spec;
15610 if (!decl_specs->type)
15611 {
15612 decl_specs->type = type_spec;
15613 decl_specs->user_defined_type_p = false;
15614 }
15615 }
15616 else if (decl_specs->type)
15617 decl_specs->multiple_types_p = true;
15618 else
15619 {
15620 decl_specs->type = type_spec;
15621 decl_specs->user_defined_type_p = user_defined_p;
15622 decl_specs->redefined_builtin_type = NULL_TREE;
15623 }
15624 }
15625
15626 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15627 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15628
15629 static bool
15630 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15631 {
15632 return decl_specifiers->specs[(int) ds_friend] != 0;
15633 }
15634
15635 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15636 issue an error message indicating that TOKEN_DESC was expected.
15637
15638 Returns the token consumed, if the token had the appropriate type.
15639 Otherwise, returns NULL. */
15640
15641 static cp_token *
15642 cp_parser_require (cp_parser* parser,
15643 enum cpp_ttype type,
15644 const char* token_desc)
15645 {
15646 if (cp_lexer_next_token_is (parser->lexer, type))
15647 return cp_lexer_consume_token (parser->lexer);
15648 else
15649 {
15650 /* Output the MESSAGE -- unless we're parsing tentatively. */
15651 if (!cp_parser_simulate_error (parser))
15652 {
15653 char *message = concat ("expected ", token_desc, NULL);
15654 cp_parser_error (parser, message);
15655 free (message);
15656 }
15657 return NULL;
15658 }
15659 }
15660
15661 /* Like cp_parser_require, except that tokens will be skipped until
15662 the desired token is found. An error message is still produced if
15663 the next token is not as expected. */
15664
15665 static void
15666 cp_parser_skip_until_found (cp_parser* parser,
15667 enum cpp_ttype type,
15668 const char* token_desc)
15669 {
15670 cp_token *token;
15671 unsigned nesting_depth = 0;
15672
15673 if (cp_parser_require (parser, type, token_desc))
15674 return;
15675
15676 /* Skip tokens until the desired token is found. */
15677 while (true)
15678 {
15679 /* Peek at the next token. */
15680 token = cp_lexer_peek_token (parser->lexer);
15681 /* If we've reached the token we want, consume it and
15682 stop. */
15683 if (token->type == type && !nesting_depth)
15684 {
15685 cp_lexer_consume_token (parser->lexer);
15686 return;
15687 }
15688 /* If we've run out of tokens, stop. */
15689 if (token->type == CPP_EOF)
15690 return;
15691 if (token->type == CPP_OPEN_BRACE
15692 || token->type == CPP_OPEN_PAREN
15693 || token->type == CPP_OPEN_SQUARE)
15694 ++nesting_depth;
15695 else if (token->type == CPP_CLOSE_BRACE
15696 || token->type == CPP_CLOSE_PAREN
15697 || token->type == CPP_CLOSE_SQUARE)
15698 {
15699 if (nesting_depth-- == 0)
15700 return;
15701 }
15702 /* Consume this token. */
15703 cp_lexer_consume_token (parser->lexer);
15704 }
15705 }
15706
15707 /* If the next token is the indicated keyword, consume it. Otherwise,
15708 issue an error message indicating that TOKEN_DESC was expected.
15709
15710 Returns the token consumed, if the token had the appropriate type.
15711 Otherwise, returns NULL. */
15712
15713 static cp_token *
15714 cp_parser_require_keyword (cp_parser* parser,
15715 enum rid keyword,
15716 const char* token_desc)
15717 {
15718 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15719
15720 if (token && token->keyword != keyword)
15721 {
15722 dyn_string_t error_msg;
15723
15724 /* Format the error message. */
15725 error_msg = dyn_string_new (0);
15726 dyn_string_append_cstr (error_msg, "expected ");
15727 dyn_string_append_cstr (error_msg, token_desc);
15728 cp_parser_error (parser, error_msg->s);
15729 dyn_string_delete (error_msg);
15730 return NULL;
15731 }
15732
15733 return token;
15734 }
15735
15736 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15737 function-definition. */
15738
15739 static bool
15740 cp_parser_token_starts_function_definition_p (cp_token* token)
15741 {
15742 return (/* An ordinary function-body begins with an `{'. */
15743 token->type == CPP_OPEN_BRACE
15744 /* A ctor-initializer begins with a `:'. */
15745 || token->type == CPP_COLON
15746 /* A function-try-block begins with `try'. */
15747 || token->keyword == RID_TRY
15748 /* The named return value extension begins with `return'. */
15749 || token->keyword == RID_RETURN);
15750 }
15751
15752 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15753 definition. */
15754
15755 static bool
15756 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15757 {
15758 cp_token *token;
15759
15760 token = cp_lexer_peek_token (parser->lexer);
15761 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15762 }
15763
15764 /* Returns TRUE iff the next token is the "," or ">" ending a
15765 template-argument. */
15766
15767 static bool
15768 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15769 {
15770 cp_token *token;
15771
15772 token = cp_lexer_peek_token (parser->lexer);
15773 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15774 }
15775
15776 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15777 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15778
15779 static bool
15780 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15781 size_t n)
15782 {
15783 cp_token *token;
15784
15785 token = cp_lexer_peek_nth_token (parser->lexer, n);
15786 if (token->type == CPP_LESS)
15787 return true;
15788 /* Check for the sequence `<::' in the original code. It would be lexed as
15789 `[:', where `[' is a digraph, and there is no whitespace before
15790 `:'. */
15791 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15792 {
15793 cp_token *token2;
15794 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15795 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15796 return true;
15797 }
15798 return false;
15799 }
15800
15801 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15802 or none_type otherwise. */
15803
15804 static enum tag_types
15805 cp_parser_token_is_class_key (cp_token* token)
15806 {
15807 switch (token->keyword)
15808 {
15809 case RID_CLASS:
15810 return class_type;
15811 case RID_STRUCT:
15812 return record_type;
15813 case RID_UNION:
15814 return union_type;
15815
15816 default:
15817 return none_type;
15818 }
15819 }
15820
15821 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15822
15823 static void
15824 cp_parser_check_class_key (enum tag_types class_key, tree type)
15825 {
15826 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15827 pedwarn ("%qs tag used in naming %q#T",
15828 class_key == union_type ? "union"
15829 : class_key == record_type ? "struct" : "class",
15830 type);
15831 }
15832
15833 /* Issue an error message if DECL is redeclared with different
15834 access than its original declaration [class.access.spec/3].
15835 This applies to nested classes and nested class templates.
15836 [class.mem/1]. */
15837
15838 static void
15839 cp_parser_check_access_in_redeclaration (tree decl)
15840 {
15841 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15842 return;
15843
15844 if ((TREE_PRIVATE (decl)
15845 != (current_access_specifier == access_private_node))
15846 || (TREE_PROTECTED (decl)
15847 != (current_access_specifier == access_protected_node)))
15848 error ("%qD redeclared with different access", decl);
15849 }
15850
15851 /* Look for the `template' keyword, as a syntactic disambiguator.
15852 Return TRUE iff it is present, in which case it will be
15853 consumed. */
15854
15855 static bool
15856 cp_parser_optional_template_keyword (cp_parser *parser)
15857 {
15858 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15859 {
15860 /* The `template' keyword can only be used within templates;
15861 outside templates the parser can always figure out what is a
15862 template and what is not. */
15863 if (!processing_template_decl)
15864 {
15865 error ("%<template%> (as a disambiguator) is only allowed "
15866 "within templates");
15867 /* If this part of the token stream is rescanned, the same
15868 error message would be generated. So, we purge the token
15869 from the stream. */
15870 cp_lexer_purge_token (parser->lexer);
15871 return false;
15872 }
15873 else
15874 {
15875 /* Consume the `template' keyword. */
15876 cp_lexer_consume_token (parser->lexer);
15877 return true;
15878 }
15879 }
15880
15881 return false;
15882 }
15883
15884 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15885 set PARSER->SCOPE, and perform other related actions. */
15886
15887 static void
15888 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15889 {
15890 tree value;
15891 tree check;
15892
15893 /* Get the stored value. */
15894 value = cp_lexer_consume_token (parser->lexer)->value;
15895 /* Perform any access checks that were deferred. */
15896 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15897 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15898 /* Set the scope from the stored value. */
15899 parser->scope = TREE_VALUE (value);
15900 parser->qualifying_scope = TREE_TYPE (value);
15901 parser->object_scope = NULL_TREE;
15902 }
15903
15904 /* Consume tokens up through a non-nested END token. */
15905
15906 static void
15907 cp_parser_cache_group (cp_parser *parser,
15908 enum cpp_ttype end,
15909 unsigned depth)
15910 {
15911 while (true)
15912 {
15913 cp_token *token;
15914
15915 /* Abort a parenthesized expression if we encounter a brace. */
15916 if ((end == CPP_CLOSE_PAREN || depth == 0)
15917 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15918 return;
15919 /* If we've reached the end of the file, stop. */
15920 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15921 return;
15922 /* Consume the next token. */
15923 token = cp_lexer_consume_token (parser->lexer);
15924 /* See if it starts a new group. */
15925 if (token->type == CPP_OPEN_BRACE)
15926 {
15927 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15928 if (depth == 0)
15929 return;
15930 }
15931 else if (token->type == CPP_OPEN_PAREN)
15932 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15933 else if (token->type == end)
15934 return;
15935 }
15936 }
15937
15938 /* Begin parsing tentatively. We always save tokens while parsing
15939 tentatively so that if the tentative parsing fails we can restore the
15940 tokens. */
15941
15942 static void
15943 cp_parser_parse_tentatively (cp_parser* parser)
15944 {
15945 /* Enter a new parsing context. */
15946 parser->context = cp_parser_context_new (parser->context);
15947 /* Begin saving tokens. */
15948 cp_lexer_save_tokens (parser->lexer);
15949 /* In order to avoid repetitive access control error messages,
15950 access checks are queued up until we are no longer parsing
15951 tentatively. */
15952 push_deferring_access_checks (dk_deferred);
15953 }
15954
15955 /* Commit to the currently active tentative parse. */
15956
15957 static void
15958 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15959 {
15960 cp_parser_context *context;
15961 cp_lexer *lexer;
15962
15963 /* Mark all of the levels as committed. */
15964 lexer = parser->lexer;
15965 for (context = parser->context; context->next; context = context->next)
15966 {
15967 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15968 break;
15969 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15970 while (!cp_lexer_saving_tokens (lexer))
15971 lexer = lexer->next;
15972 cp_lexer_commit_tokens (lexer);
15973 }
15974 }
15975
15976 /* Abort the currently active tentative parse. All consumed tokens
15977 will be rolled back, and no diagnostics will be issued. */
15978
15979 static void
15980 cp_parser_abort_tentative_parse (cp_parser* parser)
15981 {
15982 cp_parser_simulate_error (parser);
15983 /* Now, pretend that we want to see if the construct was
15984 successfully parsed. */
15985 cp_parser_parse_definitely (parser);
15986 }
15987
15988 /* Stop parsing tentatively. If a parse error has occurred, restore the
15989 token stream. Otherwise, commit to the tokens we have consumed.
15990 Returns true if no error occurred; false otherwise. */
15991
15992 static bool
15993 cp_parser_parse_definitely (cp_parser* parser)
15994 {
15995 bool error_occurred;
15996 cp_parser_context *context;
15997
15998 /* Remember whether or not an error occurred, since we are about to
15999 destroy that information. */
16000 error_occurred = cp_parser_error_occurred (parser);
16001 /* Remove the topmost context from the stack. */
16002 context = parser->context;
16003 parser->context = context->next;
16004 /* If no parse errors occurred, commit to the tentative parse. */
16005 if (!error_occurred)
16006 {
16007 /* Commit to the tokens read tentatively, unless that was
16008 already done. */
16009 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16010 cp_lexer_commit_tokens (parser->lexer);
16011
16012 pop_to_parent_deferring_access_checks ();
16013 }
16014 /* Otherwise, if errors occurred, roll back our state so that things
16015 are just as they were before we began the tentative parse. */
16016 else
16017 {
16018 cp_lexer_rollback_tokens (parser->lexer);
16019 pop_deferring_access_checks ();
16020 }
16021 /* Add the context to the front of the free list. */
16022 context->next = cp_parser_context_free_list;
16023 cp_parser_context_free_list = context;
16024
16025 return !error_occurred;
16026 }
16027
16028 /* Returns true if we are parsing tentatively and are not committed to
16029 this tentative parse. */
16030
16031 static bool
16032 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16033 {
16034 return (cp_parser_parsing_tentatively (parser)
16035 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16036 }
16037
16038 /* Returns nonzero iff an error has occurred during the most recent
16039 tentative parse. */
16040
16041 static bool
16042 cp_parser_error_occurred (cp_parser* parser)
16043 {
16044 return (cp_parser_parsing_tentatively (parser)
16045 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16046 }
16047
16048 /* Returns nonzero if GNU extensions are allowed. */
16049
16050 static bool
16051 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16052 {
16053 return parser->allow_gnu_extensions_p;
16054 }
16055
16056 \f
16057 /* The parser. */
16058
16059 static GTY (()) cp_parser *the_parser;
16060
16061 /* External interface. */
16062
16063 /* Parse one entire translation unit. */
16064
16065 void
16066 c_parse_file (void)
16067 {
16068 bool error_occurred;
16069 static bool already_called = false;
16070
16071 if (already_called)
16072 {
16073 sorry ("inter-module optimizations not implemented for C++");
16074 return;
16075 }
16076 already_called = true;
16077
16078 the_parser = cp_parser_new ();
16079 push_deferring_access_checks (flag_access_control
16080 ? dk_no_deferred : dk_no_check);
16081 error_occurred = cp_parser_translation_unit (the_parser);
16082 the_parser = NULL;
16083 }
16084
16085 /* This variable must be provided by every front end. */
16086
16087 int yydebug;
16088
16089 #include "gt-cp-parser.h"